Browse Source

Merge pull request #575 from bitcoinjs/ectypes2

ECPair cleanup
hk-custom-address
Daniel Cousens 9 years ago
parent
commit
f1a20f1ec8
  1. 25
      src/ecpair.js

25
src/ecpair.js

@ -1,5 +1,5 @@
var baddress = require('./address')
var bcrypto = require('./crypto')
var bs58check = require('bs58check')
var ecdsa = require('./ecdsa')
var randomBytes = require('randombytes')
var typeforce = require('typeforce')
@ -58,20 +58,24 @@ ECPair.fromPublicKeyBuffer = function (buffer, network) {
}
ECPair.fromWIF = function (string, network) {
var buffer = bs58check.decode(string)
var decoded = wif.decode(string)
var version = decoded.version
// [network, ...]
if (types.Array(network)) {
var version = buffer[0]
network = network.filter(function (network) {
return version === network.wif
}).pop()
if (!network) throw new Error('Unknown network version')
// network
} else {
network = network || NETWORKS.bitcoin
if (version !== network.wif) throw new Error('Invalid network version')
}
network = network || NETWORKS.bitcoin
var decoded = wif.decodeRaw(buffer, network.wif)
var d = BigInteger.fromBuffer(decoded.privateKey)
return new ECPair(d, null, {
@ -97,14 +101,7 @@ ECPair.makeRandom = function (options) {
}
ECPair.prototype.getAddress = function () {
var pubKey = this.getPublicKeyBuffer()
var pubKeyHash = bcrypto.hash160(pubKey)
var payload = new Buffer(21)
payload.writeUInt8(this.network.pubKeyHash, 0)
pubKeyHash.copy(payload, 1)
return bs58check.encode(payload)
return baddress.toBase58Check(bcrypto.hash160(this.getPublicKeyBuffer()), this.getNetwork().pubKeyHash)
}
ECPair.prototype.getNetwork = function () {

Loading…
Cancel
Save