do not use object literals in hd.js.

This commit is contained in:
Christopher Jeffrey 2016-06-30 07:25:42 -07:00
parent 667fa4b796
commit 3e13badc7a
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -908,17 +908,20 @@ HDPrivateKey.fromOptions = function fromOptions(options) {
}; };
HDPrivateKey.prototype.__defineGetter__('hdPublicKey', function() { HDPrivateKey.prototype.__defineGetter__('hdPublicKey', function() {
if (!this._hdPublicKey) { var key = this._hdPublicKey;
this._hdPublicKey = new HDPublicKey({
network: this.network, if (!key) {
depth: this.depth, key = new HDPublicKey();
parentFingerPrint: this.parentFingerPrint, key.network = this.network;
childIndex: this.childIndex, key.depth = this.depth;
chainCode: this.chainCode, key.parentFingerPrint = this.parentFingerPrint;
publicKey: this.publicKey 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() { HDPrivateKey.prototype.__defineGetter__('xprivkey', function() {
@ -958,6 +961,9 @@ HDPrivateKey.prototype.derive = function derive(index, hardened) {
if (!(index >= 0 && index < constants.hd.MAX_INDEX)) if (!(index >= 0 && index < constants.hd.MAX_INDEX))
throw new Error('Index out of range.'); throw new Error('Index out of range.');
if (this.depth >= 0xff)
throw new Error('Depth too high.');
p = new BufferWriter(); p = new BufferWriter();
if (hardened) { if (hardened) {
@ -983,14 +989,14 @@ HDPrivateKey.prototype.derive = function derive(index, hardened) {
if (!this.fingerPrint) if (!this.fingerPrint)
this.fingerPrint = utils.hash160(this.publicKey).slice(0, 4); this.fingerPrint = utils.hash160(this.publicKey).slice(0, 4);
child = new HDPrivateKey({ child = new HDPrivateKey();
network: this.network, child.network = this.network;
depth: this.depth + 1, child.depth = this.depth + 1;
parentFingerPrint: this.fingerPrint, child.parentFingerPrint = this.fingerPrint;
childIndex: index, child.childIndex = index;
chainCode: chainCode, child.chainCode = chainCode;
privateKey: privateKey child.privateKey = privateKey;
}); child.publicKey = ec.publicKeyCreate(privateKey, true);
HD.cache.set(id, child); HD.cache.set(id, child);
@ -1587,8 +1593,7 @@ 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, chainCode; var p, id, data, hash, left, chainCode, point, publicKey, child;
var publicPoint, point, publicKey, child;
if (typeof index === 'string') if (typeof index === 'string')
return this.derivePath(index); return this.derivePath(index);
@ -1605,6 +1610,9 @@ HDPublicKey.prototype.derive = function derive(index, hardened) {
if (index < 0) if (index < 0)
throw new Error('Index out of range.'); throw new Error('Index out of range.');
if (this.depth >= 0xff)
throw new Error('Depth too high.');
p = new BufferWriter(); p = new BufferWriter();
p.writeBytes(this.publicKey); p.writeBytes(this.publicKey);
p.writeU32BE(index); p.writeU32BE(index);
@ -1614,22 +1622,21 @@ HDPublicKey.prototype.derive = function derive(index, hardened) {
left = new bn(hash.slice(0, 32)); left = new bn(hash.slice(0, 32));
chainCode = hash.slice(32, 64); chainCode = hash.slice(32, 64);
publicPoint = ec.elliptic.curve.decodePoint(this.publicKey); point = ec.elliptic.curve.decodePoint(this.publicKey);
point = ec.elliptic.curve.g.mul(left).add(publicPoint); point = ec.elliptic.curve.g.mul(left).add(point);
publicKey = new Buffer(point.encode('array', true)); publicKey = new Buffer(point.encode('array', true));
assert(publicKey.length === 33); assert(publicKey.length === 33);
if (!this.fingerPrint) if (!this.fingerPrint)
this.fingerPrint = utils.hash160(this.publicKey).slice(0, 4); this.fingerPrint = utils.hash160(this.publicKey).slice(0, 4);
child = new HDPublicKey({ child = new HDPublicKey();
network: this.network, child.network = this.network;
depth: this.depth + 1, child.depth = this.depth + 1;
parentFingerPrint: this.fingerPrint, child.parentFingerPrint = this.fingerPrint;
childIndex: index, child.childIndex = index;
chainCode: chainCode, child.chainCode = chainCode;
publicKey: publicKey child.publicKey = publicKey;
});
HD.cache.set(id, child); HD.cache.set(id, child);
@ -1988,13 +1995,22 @@ HDPublicKey.isHDPublicKey = function isHDPublicKey(obj) {
HD.prototype.compressed = true; HD.prototype.compressed = true;
}); });
/**
* Convert HDPrivateKey to a KeyPair.
* @returns {KeyPair}
*/
HDPrivateKey.prototype.toKeyPair = function toKeyPair() {
return new KeyPair(this);
};
/** /**
* Convert HDPrivateKey to CBitcoinSecret. * Convert HDPrivateKey to CBitcoinSecret.
* @returns {Base58String} * @returns {Base58String}
*/ */
HDPrivateKey.prototype.toSecret = function toSecret(network) { HDPrivateKey.prototype.toSecret = function toSecret(network) {
return KeyPair.prototype.toSecret.call(this, network); return this.toKeyPair().toSecret();
}; };
/* /*