diff --git a/lib/bcoin/hd.js b/lib/bcoin/hd.js index 9e0863c5..c645c1bd 100644 --- a/lib/bcoin/hd.js +++ b/lib/bcoin/hd.js @@ -662,25 +662,31 @@ HDPrivateKey.prototype._unbuild = function _unbuild(xkey) { else this.network = 'testnet'; + this.xprivkey = xkey; + return data; }; HDPrivateKey.prototype._build = function _build(data) { - var sequence = new Buffer(82); var off = 0; - var checksum; + var sequence, checksum; - off += utils.copy(data.version, sequence, off); - off += utils.copy(data.depth, sequence, off); - off += utils.copy(data.parentFingerPrint, sequence, off); - off += utils.copy(data.childIndex, sequence, off); - off += utils.copy(data.chainCode, sequence, off); - off += utils.writeU8(sequence, 0, off); - off += utils.copy(data.privateKey, sequence, off); - assert(off === 78, off); - checksum = utils.dsha256(sequence.slice(0, off)).slice(0, 4); - off += utils.copy(checksum, sequence, off); - assert(off === 82, off); + if (!this.xprivkey) { + sequence = new Buffer(82); + off += utils.copy(data.version, sequence, off); + off += utils.copy(data.depth, sequence, off); + off += utils.copy(data.parentFingerPrint, sequence, off); + off += utils.copy(data.childIndex, sequence, off); + off += utils.copy(data.chainCode, sequence, off); + off += utils.writeU8(sequence, 0, off); + off += utils.copy(data.privateKey, sequence, off); + assert(off === 78, off); + checksum = utils.dsha256(sequence.slice(0, off)).slice(0, 4); + off += utils.copy(checksum, sequence, off); + assert(off === 82, off); + + this.xprivkey = utils.toBase58(sequence); + } this.version = data.version; this.depth = data.depth; @@ -694,7 +700,6 @@ HDPrivateKey.prototype._build = function _build(data) { this.fingerPrint = null; this.hdPrivateKey = this; - this.xprivkey = utils.toBase58(sequence); this.hdPublicKey = new HDPublicKey({ network: this.network, @@ -1026,29 +1031,35 @@ HDPublicKey.prototype._unbuild = function _unbuild(xkey) { else this.network = 'testnet'; + this.xpubkey = xkey; + return data; }; HDPublicKey.prototype._build = function _build(data) { - var sequence = new Buffer(82); var off = 0; - var checksum; + var sequence, checksum; - off += utils.copy(data.version, sequence, off); - off += utils.copy(data.depth, sequence, off); - off += utils.copy(data.parentFingerPrint, sequence, off); - off += utils.copy(data.childIndex, sequence, off); - off += utils.copy(data.chainCode, sequence, off); - off += utils.copy(data.publicKey, sequence, off); - assert(off === 78, off); - checksum = utils.dsha256(sequence.slice(0, off)).slice(0, 4); - off += utils.copy(checksum, sequence, off); - assert(off === 82, off); + if (!this.xpubkey) { + sequence = new Buffer(82); + off += utils.copy(data.version, sequence, off); + off += utils.copy(data.depth, sequence, off); + off += utils.copy(data.parentFingerPrint, sequence, off); + off += utils.copy(data.childIndex, sequence, off); + off += utils.copy(data.chainCode, sequence, off); + off += utils.copy(data.publicKey, sequence, off); + assert(off === 78, off); + checksum = utils.dsha256(sequence.slice(0, off)).slice(0, 4); + off += utils.copy(checksum, sequence, off); + assert(off === 82, off); - if (!data.checksum || !data.checksum.length) - data.checksum = checksum; - else if (utils.toHex(checksum) !== utils.toHex(data.checksum)) - throw new Error('checksum mismatch'); + if (!data.checksum || !data.checksum.length) + data.checksum = checksum; + else if (utils.toHex(checksum) !== utils.toHex(data.checksum)) + throw new Error('checksum mismatch'); + + this.xpubkey = utils.toBase58(sequence); + } this.version = data.version; this.depth = data.depth; @@ -1062,7 +1073,6 @@ HDPublicKey.prototype._build = function _build(data) { this.fingerPrint = null; this.hdPublicKey = this; - this.xpubkey = utils.toBase58(sequence); this.hdPrivateKey = null; this.xprivkey = null;