From 678da4671a93b9595ec0380c7cd7fc715d648893 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 15 Jul 2016 08:36:00 -0700 Subject: [PATCH] fix hd cache. --- lib/bcoin/hd.js | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/lib/bcoin/hd.js b/lib/bcoin/hd.js index 3cd0dd7f..48cee117 100644 --- a/lib/bcoin/hd.js +++ b/lib/bcoin/hd.js @@ -945,17 +945,11 @@ HDPrivateKey.prototype.__defineGetter__('xpubkey', function() { */ HDPrivateKey.prototype.derive = function derive(index, hardened) { - var p, id, data, hash, left, right, privateKey, child; + var p, id, data, hash, left, right, key, child; if (typeof index === 'string') return this.derivePath(index); - id = this.xprivkey + '/' + index; - child = HD.cache.get(id); - - if (child) - return child; - hardened = index >= constants.hd.HARDENED ? true : hardened; if (index < constants.hd.HARDENED && hardened) @@ -967,6 +961,12 @@ HDPrivateKey.prototype.derive = function derive(index, hardened) { if (this.depth >= 0xff) throw new Error('Depth too high.'); + id = this.xprivkey + '/' + index; + child = HD.cache.get(id); + + if (child) + return child; + p = new BufferWriter(); if (hardened) { @@ -985,7 +985,7 @@ HDPrivateKey.prototype.derive = function derive(index, hardened) { right = hash.slice(32, 64); try { - privateKey = ec.privateKeyTweakAdd(this.privateKey, left); + key = ec.privateKeyTweakAdd(this.privateKey, left); } catch (e) { return this.derive(index + 1); } @@ -999,8 +999,8 @@ HDPrivateKey.prototype.derive = function derive(index, hardened) { child.parentFingerPrint = this.fingerPrint; child.childIndex = index; child.chainCode = right; - child.privateKey = privateKey; - child.publicKey = ec.publicKeyCreate(privateKey, true); + child.privateKey = key; + child.publicKey = ec.publicKeyCreate(key, true); HD.cache.set(id, child); @@ -1594,17 +1594,11 @@ HDPublicKey.prototype.__defineGetter__('xpubkey', function() { */ HDPublicKey.prototype.derive = function derive(index, hardened) { - var p, id, data, hash, left, right, publicKey, child; + var p, id, data, hash, left, right, key, child; if (typeof index === 'string') return this.derivePath(index); - id = this.xpubkey + '/' + index; - child = HD.cache.get(id); - - if (child) - return child; - if (index >= constants.hd.HARDENED || hardened) throw new Error('Index out of range.'); @@ -1614,6 +1608,12 @@ HDPublicKey.prototype.derive = function derive(index, hardened) { if (this.depth >= 0xff) throw new Error('Depth too high.'); + id = this.xpubkey + '/' + index; + child = HD.cache.get(id); + + if (child) + return child; + p = new BufferWriter(); p.writeBytes(this.publicKey); p.writeU32BE(index); @@ -1624,7 +1624,7 @@ HDPublicKey.prototype.derive = function derive(index, hardened) { right = hash.slice(32, 64); try { - publicKey = ec.publicKeyTweakAdd(this.publicKey, left, true); + key = ec.publicKeyTweakAdd(this.publicKey, left, true); } catch (e) { return this.derive(index + 1); } @@ -1638,7 +1638,7 @@ HDPublicKey.prototype.derive = function derive(index, hardened) { child.parentFingerPrint = this.fingerPrint; child.childIndex = index; child.chainCode = right; - child.publicKey = publicKey; + child.publicKey = key; HD.cache.set(id, child);