key version check always always relies on the network property

[#27]
This commit is contained in:
Wei Lu 2014-02-28 14:08:34 +08:00
parent cc0a042f2f
commit c352868080

View file

@ -13,10 +13,9 @@ var HDWallet = module.exports = function(seed, network) {
var I = Crypto.HMAC(Crypto.SHA512, seed, 'Bitcoin seed', { asBytes: true }) var I = Crypto.HMAC(Crypto.SHA512, seed, 'Bitcoin seed', { asBytes: true })
this.chaincode = I.slice(32) this.chaincode = I.slice(32)
this.keyVersion = network == 'Bitcoin' ? Address.address_types.prod : Address.address_types.testnet
this.priv = new ECKey(I.slice(0, 32).concat([1]), true, this.keyVersion)
this.pub = this.priv.getPub()
this.network = network || 'Bitcoin' this.network = network || 'Bitcoin'
this.priv = new ECKey(I.slice(0, 32).concat([1]), true, this.getKeyVersion())
this.pub = this.priv.getPub()
this.index = 0 this.index = 0
this.depth = 0 this.depth = 0
} }
@ -107,10 +106,10 @@ HDWallet.fromBytes = function(input) {
// 33 bytes: the public key or private key data (0x02 + X or 0x03 + X for // 33 bytes: the public key or private key data (0x02 + X or 0x03 + X for
// public keys, 0x00 + k for private keys) // public keys, 0x00 + k for private keys)
if (type == 'private') { if (type == 'private') {
hd.priv = new ECKey(input.slice(46, 78).concat([1]), true, this.keyVersion) hd.priv = new ECKey(input.slice(46, 78).concat([1]), true, hd.getKeyVersion())
hd.pub = hd.priv.getPub() hd.pub = hd.priv.getPub()
} else { } else {
hd.pub = new ECPubKey(input.slice(45, 78), true, this.keyVersion) hd.pub = new ECPubKey(input.slice(45, 78), true, hd.getKeyVersion())
} }
return hd return hd
@ -214,11 +213,11 @@ HDWallet.prototype.derive = function(i) {
// ki = IL + kpar (mod n). // ki = IL + kpar (mod n).
hd.priv = this.priv.add(new ECKey(IL.concat([1]))) hd.priv = this.priv.add(new ECKey(IL.concat([1])))
hd.priv.compressed = true hd.priv.compressed = true
hd.priv.version = this.keyVersion hd.priv.version = this.getKeyVersion()
hd.pub = hd.priv.getPub() hd.pub = hd.priv.getPub()
} else { } else {
// Ki = (IL + kpar)*G = IL*G + Kpar // Ki = (IL + kpar)*G = IL*G + Kpar
hd.pub = this.pub.add(new ECKey(IL.concat([1]), true, this.keyVersion).getPub()) hd.pub = this.pub.add(new ECKey(IL.concat([1]), true, this.getKeyVersion()).getPub())
} }
// ci = IR. // ci = IR.
@ -234,4 +233,8 @@ HDWallet.prototype.derivePrivate = function(index) {
return this.derive(index + HDWallet.HIGHEST_BIT) return this.derive(index + HDWallet.HIGHEST_BIT)
} }
HDWallet.prototype.getKeyVersion = function() {
return this.network == 'Bitcoin' ? Address.address_types.prod : Address.address_types.testnet
}
HDWallet.prototype.toString = HDWallet.prototype.toBase58 HDWallet.prototype.toString = HDWallet.prototype.toBase58