From 3e13badc7ac6bf0b891432ac0830ea5f89b21708 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 30 Jun 2016 07:25:42 -0700 Subject: [PATCH] do not use object literals in hd.js. --- lib/bcoin/hd.js | 78 +++++++++++++++++++++++++++++-------------------- 1 file changed, 47 insertions(+), 31 deletions(-) diff --git a/lib/bcoin/hd.js b/lib/bcoin/hd.js index a3964dad..78ff23b2 100644 --- a/lib/bcoin/hd.js +++ b/lib/bcoin/hd.js @@ -908,17 +908,20 @@ HDPrivateKey.fromOptions = function fromOptions(options) { }; HDPrivateKey.prototype.__defineGetter__('hdPublicKey', function() { - if (!this._hdPublicKey) { - this._hdPublicKey = new HDPublicKey({ - network: this.network, - depth: this.depth, - parentFingerPrint: this.parentFingerPrint, - childIndex: this.childIndex, - chainCode: this.chainCode, - publicKey: this.publicKey - }); + var key = this._hdPublicKey; + + if (!key) { + key = new HDPublicKey(); + key.network = this.network; + key.depth = this.depth; + key.parentFingerPrint = this.parentFingerPrint; + key.childIndex = this.childIndex; + key.chainCode = this.chainCode; + key.publicKey = this.publicKey; + this._hdPublicKey = key; } - return this._hdPublicKey; + + return key; }); HDPrivateKey.prototype.__defineGetter__('xprivkey', function() { @@ -958,6 +961,9 @@ HDPrivateKey.prototype.derive = function derive(index, hardened) { if (!(index >= 0 && index < constants.hd.MAX_INDEX)) throw new Error('Index out of range.'); + if (this.depth >= 0xff) + throw new Error('Depth too high.'); + p = new BufferWriter(); if (hardened) { @@ -983,14 +989,14 @@ HDPrivateKey.prototype.derive = function derive(index, hardened) { if (!this.fingerPrint) this.fingerPrint = utils.hash160(this.publicKey).slice(0, 4); - child = new HDPrivateKey({ - network: this.network, - depth: this.depth + 1, - parentFingerPrint: this.fingerPrint, - childIndex: index, - chainCode: chainCode, - privateKey: privateKey - }); + child = new HDPrivateKey(); + child.network = this.network; + child.depth = this.depth + 1; + child.parentFingerPrint = this.fingerPrint; + child.childIndex = index; + child.chainCode = chainCode; + child.privateKey = privateKey; + child.publicKey = ec.publicKeyCreate(privateKey, true); HD.cache.set(id, child); @@ -1587,8 +1593,7 @@ HDPublicKey.prototype.__defineGetter__('xpubkey', function() { */ HDPublicKey.prototype.derive = function derive(index, hardened) { - var p, id, data, hash, left, chainCode; - var publicPoint, point, publicKey, child; + var p, id, data, hash, left, chainCode, point, publicKey, child; if (typeof index === 'string') return this.derivePath(index); @@ -1605,6 +1610,9 @@ HDPublicKey.prototype.derive = function derive(index, hardened) { if (index < 0) throw new Error('Index out of range.'); + if (this.depth >= 0xff) + throw new Error('Depth too high.'); + p = new BufferWriter(); p.writeBytes(this.publicKey); p.writeU32BE(index); @@ -1614,22 +1622,21 @@ HDPublicKey.prototype.derive = function derive(index, hardened) { left = new bn(hash.slice(0, 32)); chainCode = hash.slice(32, 64); - publicPoint = ec.elliptic.curve.decodePoint(this.publicKey); - point = ec.elliptic.curve.g.mul(left).add(publicPoint); + point = ec.elliptic.curve.decodePoint(this.publicKey); + point = ec.elliptic.curve.g.mul(left).add(point); publicKey = new Buffer(point.encode('array', true)); assert(publicKey.length === 33); if (!this.fingerPrint) this.fingerPrint = utils.hash160(this.publicKey).slice(0, 4); - child = new HDPublicKey({ - network: this.network, - depth: this.depth + 1, - parentFingerPrint: this.fingerPrint, - childIndex: index, - chainCode: chainCode, - publicKey: publicKey - }); + child = new HDPublicKey(); + child.network = this.network; + child.depth = this.depth + 1; + child.parentFingerPrint = this.fingerPrint; + child.childIndex = index; + child.chainCode = chainCode; + child.publicKey = publicKey; HD.cache.set(id, child); @@ -1988,13 +1995,22 @@ HDPublicKey.isHDPublicKey = function isHDPublicKey(obj) { HD.prototype.compressed = true; }); +/** + * Convert HDPrivateKey to a KeyPair. + * @returns {KeyPair} + */ + +HDPrivateKey.prototype.toKeyPair = function toKeyPair() { + return new KeyPair(this); +}; + /** * Convert HDPrivateKey to CBitcoinSecret. * @returns {Base58String} */ HDPrivateKey.prototype.toSecret = function toSecret(network) { - return KeyPair.prototype.toSecret.call(this, network); + return this.toKeyPair().toSecret(); }; /*