diff --git a/lib/bcoin/hd.js b/lib/bcoin/hd.js index 24e5841b..9e0863c5 100644 --- a/lib/bcoin/hd.js +++ b/lib/bcoin/hd.js @@ -668,7 +668,7 @@ HDPrivateKey.prototype._unbuild = function _unbuild(xkey) { HDPrivateKey.prototype._build = function _build(data) { var sequence = new Buffer(82); var off = 0; - var checksum, xprivkey, privateKey, publicKey, size, fingerPrint; + var checksum; off += utils.copy(data.version, sequence, off); off += utils.copy(data.depth, sequence, off); @@ -682,13 +682,6 @@ HDPrivateKey.prototype._build = function _build(data) { off += utils.copy(checksum, sequence, off); assert(off === 82, off); - xprivkey = utils.toBase58(sequence); - - publicKey = bcoin.ec.publicKeyCreate(data.privateKey, true); - - size = constants.hd.parentFingerPrintSize; - fingerPrint = utils.ripesha(publicKey).slice(0, size); - this.version = data.version; this.depth = data.depth; this.parentFingerPrint = data.parentFingerPrint; @@ -697,16 +690,15 @@ HDPrivateKey.prototype._build = function _build(data) { this.privateKey = data.privateKey; this.checksum = null; - this.fingerPrint = fingerPrint; - this.publicKey = publicKey; + this.publicKey = bcoin.ec.publicKeyCreate(data.privateKey, true); + this.fingerPrint = null; this.hdPrivateKey = this; - this.xprivkey = xprivkey; + this.xprivkey = utils.toBase58(sequence); this.hdPublicKey = new HDPublicKey({ network: this.network, data: { - key: this.key, version: network[this.network].prefixes.xpubkey, depth: this.depth, parentFingerPrint: this.parentFingerPrint, @@ -755,6 +747,11 @@ HDPrivateKey.prototype.derive = function derive(index, hardened) { .mod(bcoin.ecdsa.curve.n) .toArray('be', 32)); + if (!this.fingerPrint) { + this.fingerPrint = utils.ripesha(this.publicKey) + .slice(0, constants.hd.parentFingerPrintSize); + } + child = new HDPrivateKey({ network: this.network, data: { @@ -1035,7 +1032,7 @@ HDPublicKey.prototype._unbuild = function _unbuild(xkey) { HDPublicKey.prototype._build = function _build(data) { var sequence = new Buffer(82); var off = 0; - var checksum, xpubkey, publicKey, size, fingerPrint; + var checksum; off += utils.copy(data.version, sequence, off); off += utils.copy(data.depth, sequence, off); @@ -1053,25 +1050,19 @@ HDPublicKey.prototype._build = function _build(data) { else if (utils.toHex(checksum) !== utils.toHex(data.checksum)) throw new Error('checksum mismatch'); - xpubkey = utils.toBase58(sequence); - - publicKey = data.publicKey; - size = constants.hd.parentFingerPrintSize; - fingerPrint = utils.ripesha(publicKey).slice(0, size); - this.version = data.version; this.depth = data.depth; this.parentFingerPrint = data.parentFingerPrint; this.childIndex = data.childIndex; this.chainCode = data.chainCode; - this.publicKey = publicKey; + this.publicKey = data.publicKey; this.checksum = null; - this.fingerPrint = fingerPrint; this.privateKey = null; + this.fingerPrint = null; this.hdPublicKey = this; - this.xpubkey = xpubkey; + this.xpubkey = utils.toBase58(sequence); this.hdPrivateKey = null; this.xprivkey = null; @@ -1108,6 +1099,11 @@ HDPublicKey.prototype.derive = function derive(index, hardened) { point = bcoin.ecdsa.curve.g.mul(leftPart).add(publicPoint); publicKey = new Buffer(point.encode('array', true)); + if (!this.fingerPrint) { + this.fingerPrint = utils.ripesha(this.publicKey) + .slice(0, constants.hd.parentFingerPrintSize); + } + child = new HDPublicKey({ network: this.network, data: {