fix hd cache.
This commit is contained in:
parent
cbd8447d80
commit
678da4671a
@ -945,17 +945,11 @@ HDPrivateKey.prototype.__defineGetter__('xpubkey', function() {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
HDPrivateKey.prototype.derive = function derive(index, hardened) {
|
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')
|
if (typeof index === 'string')
|
||||||
return this.derivePath(index);
|
return this.derivePath(index);
|
||||||
|
|
||||||
id = this.xprivkey + '/' + index;
|
|
||||||
child = HD.cache.get(id);
|
|
||||||
|
|
||||||
if (child)
|
|
||||||
return child;
|
|
||||||
|
|
||||||
hardened = index >= constants.hd.HARDENED ? true : hardened;
|
hardened = index >= constants.hd.HARDENED ? true : hardened;
|
||||||
|
|
||||||
if (index < constants.hd.HARDENED && hardened)
|
if (index < constants.hd.HARDENED && hardened)
|
||||||
@ -967,6 +961,12 @@ HDPrivateKey.prototype.derive = function derive(index, hardened) {
|
|||||||
if (this.depth >= 0xff)
|
if (this.depth >= 0xff)
|
||||||
throw new Error('Depth too high.');
|
throw new Error('Depth too high.');
|
||||||
|
|
||||||
|
id = this.xprivkey + '/' + index;
|
||||||
|
child = HD.cache.get(id);
|
||||||
|
|
||||||
|
if (child)
|
||||||
|
return child;
|
||||||
|
|
||||||
p = new BufferWriter();
|
p = new BufferWriter();
|
||||||
|
|
||||||
if (hardened) {
|
if (hardened) {
|
||||||
@ -985,7 +985,7 @@ HDPrivateKey.prototype.derive = function derive(index, hardened) {
|
|||||||
right = hash.slice(32, 64);
|
right = hash.slice(32, 64);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
privateKey = ec.privateKeyTweakAdd(this.privateKey, left);
|
key = ec.privateKeyTweakAdd(this.privateKey, left);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return this.derive(index + 1);
|
return this.derive(index + 1);
|
||||||
}
|
}
|
||||||
@ -999,8 +999,8 @@ HDPrivateKey.prototype.derive = function derive(index, hardened) {
|
|||||||
child.parentFingerPrint = this.fingerPrint;
|
child.parentFingerPrint = this.fingerPrint;
|
||||||
child.childIndex = index;
|
child.childIndex = index;
|
||||||
child.chainCode = right;
|
child.chainCode = right;
|
||||||
child.privateKey = privateKey;
|
child.privateKey = key;
|
||||||
child.publicKey = ec.publicKeyCreate(privateKey, true);
|
child.publicKey = ec.publicKeyCreate(key, true);
|
||||||
|
|
||||||
HD.cache.set(id, child);
|
HD.cache.set(id, child);
|
||||||
|
|
||||||
@ -1594,17 +1594,11 @@ HDPublicKey.prototype.__defineGetter__('xpubkey', function() {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
HDPublicKey.prototype.derive = function derive(index, hardened) {
|
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')
|
if (typeof index === 'string')
|
||||||
return this.derivePath(index);
|
return this.derivePath(index);
|
||||||
|
|
||||||
id = this.xpubkey + '/' + index;
|
|
||||||
child = HD.cache.get(id);
|
|
||||||
|
|
||||||
if (child)
|
|
||||||
return child;
|
|
||||||
|
|
||||||
if (index >= constants.hd.HARDENED || hardened)
|
if (index >= constants.hd.HARDENED || hardened)
|
||||||
throw new Error('Index out of range.');
|
throw new Error('Index out of range.');
|
||||||
|
|
||||||
@ -1614,6 +1608,12 @@ HDPublicKey.prototype.derive = function derive(index, hardened) {
|
|||||||
if (this.depth >= 0xff)
|
if (this.depth >= 0xff)
|
||||||
throw new Error('Depth too high.');
|
throw new Error('Depth too high.');
|
||||||
|
|
||||||
|
id = this.xpubkey + '/' + index;
|
||||||
|
child = HD.cache.get(id);
|
||||||
|
|
||||||
|
if (child)
|
||||||
|
return child;
|
||||||
|
|
||||||
p = new BufferWriter();
|
p = new BufferWriter();
|
||||||
p.writeBytes(this.publicKey);
|
p.writeBytes(this.publicKey);
|
||||||
p.writeU32BE(index);
|
p.writeU32BE(index);
|
||||||
@ -1624,7 +1624,7 @@ HDPublicKey.prototype.derive = function derive(index, hardened) {
|
|||||||
right = hash.slice(32, 64);
|
right = hash.slice(32, 64);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
publicKey = ec.publicKeyTweakAdd(this.publicKey, left, true);
|
key = ec.publicKeyTweakAdd(this.publicKey, left, true);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
return this.derive(index + 1);
|
return this.derive(index + 1);
|
||||||
}
|
}
|
||||||
@ -1638,7 +1638,7 @@ HDPublicKey.prototype.derive = function derive(index, hardened) {
|
|||||||
child.parentFingerPrint = this.fingerPrint;
|
child.parentFingerPrint = this.fingerPrint;
|
||||||
child.childIndex = index;
|
child.childIndex = index;
|
||||||
child.chainCode = right;
|
child.chainCode = right;
|
||||||
child.publicKey = publicKey;
|
child.publicKey = key;
|
||||||
|
|
||||||
HD.cache.set(id, child);
|
HD.cache.set(id, child);
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user