fix hd cache.

This commit is contained in:
Christopher Jeffrey 2016-07-15 08:36:00 -07:00
parent cbd8447d80
commit 678da4671a
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -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);