diff --git a/lib/bcoin/hd.js b/lib/bcoin/hd.js index 6d3ced77..0deeac33 100644 --- a/lib/bcoin/hd.js +++ b/lib/bcoin/hd.js @@ -152,14 +152,13 @@ function HDPrivateKey(options) { this._build(data); - if (new bn(data.parentFingerPrint).cmpn(0) === 0) { + if (new bn(data.parentFingerPrint).cmpn(0) === 0) this.isMaster = true; - this.master = this; - } else { - this.master = options.master; - } + else + this.isMaster = false; this.isPrivate = true; + this.isPublic = false; } HDPrivateKey.prototype.scan44 = function scan44(options, txByAddress, callback) { @@ -594,9 +593,10 @@ HDPrivateKey.prototype._build = function _build(data) { off += utils.copy(data.chainCode, sequence, off); off += utils.copy([0], sequence, 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); + assert(off === 82, off); xprivkey = utils.toBase58(sequence); @@ -625,10 +625,8 @@ HDPrivateKey.prototype._build = function _build(data) { parentFingerPrint: this.parentFingerPrint, childIndex: this.childIndex, chainCode: this.chainCode, - privateKey: this.privateKey, checksum: this.checksum, - publicKey: this.publicKey, - master: this.master + publicKey: this.publicKey }); this.xpubkey = this.hdpub.xpubkey; @@ -653,17 +651,19 @@ HDPrivateKey.prototype.derive = function derive(index, hardened) { leftPart = new bn(hash.slice(0, 32)); chainCode = hash.slice(32, 64); - privateKey = leftPart.add(new bn(this.privateKey)).mod(ec.curve.n).toArray(); + privateKey = leftPart + .add(new bn(this.privateKey)) + .mod(ec.curve.n) + .toArray('be', 32); return new HDPrivateKey({ - version: null, + version: this.version, depth: new bn(this.depth).toNumber() + 1, parentFingerPrint: this.fingerPrint, childIndex: index, chainCode: chainCode, privateKey: privateKey, - checksum: null, - master: this.master + checksum: null }); }; @@ -822,13 +822,12 @@ function HDPublicKey(options) { this._build(data); - if (new bn(data.parentFingerPrint).cmpn(0) === 0) { + if (new bn(data.parentFingerPrint).cmpn(0) === 0) this.isMaster = true; - this.master = this; - } else { - this.master = options.master; - } + else + this.isMaster = false; + this.isPrivate = false; this.isPublic = true; } @@ -897,9 +896,10 @@ HDPublicKey.prototype._build = function _build(data) { 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); + assert(off === 82, off); if (!data.checksum || !data.checksum.length) data.checksum = checksum; @@ -949,14 +949,13 @@ HDPublicKey.prototype.derive = function derive(index, hardened) { publicKey = bcoin.ecdsa.keyPair({ pub: point }).getPublic(true, 'array'); return new HDPublicKey({ - version: null, + version: this.version, depth: new bn(this.depth).toNumber() + 1, parentFingerPrint: this.fingerPrint, childIndex: index, chainCode: chainCode, publicKey: publicKey, - checksum: null, - master: this.master + checksum: null }); };