fix hd privkey derivation.

This commit is contained in:
Christopher Jeffrey 2016-02-04 13:08:10 -08:00
parent 49bcfc17e9
commit b9149a0855

View File

@ -152,14 +152,13 @@ function HDPrivateKey(options) {
this._build(data); this._build(data);
if (new bn(data.parentFingerPrint).cmpn(0) === 0) { if (new bn(data.parentFingerPrint).cmpn(0) === 0)
this.isMaster = true; this.isMaster = true;
this.master = this; else
} else { this.isMaster = false;
this.master = options.master;
}
this.isPrivate = true; this.isPrivate = true;
this.isPublic = false;
} }
HDPrivateKey.prototype.scan44 = function scan44(options, txByAddress, callback) { HDPrivateKey.prototype.scan44 = function scan44(options, txByAddress, callback) {
@ -594,9 +593,10 @@ HDPrivateKey.prototype._build = function _build(data) {
off += utils.copy(data.chainCode, sequence, off); off += utils.copy(data.chainCode, sequence, off);
off += utils.copy([0], sequence, off); off += utils.copy([0], sequence, off);
off += utils.copy(data.privateKey, sequence, off); off += utils.copy(data.privateKey, sequence, off);
assert(off === 78, off);
checksum = utils.dsha256(sequence.slice(0, off)).slice(0, 4); checksum = utils.dsha256(sequence.slice(0, off)).slice(0, 4);
off += utils.copy(checksum, sequence, off); off += utils.copy(checksum, sequence, off);
assert(off === 82); assert(off === 82, off);
xprivkey = utils.toBase58(sequence); xprivkey = utils.toBase58(sequence);
@ -625,10 +625,8 @@ HDPrivateKey.prototype._build = function _build(data) {
parentFingerPrint: this.parentFingerPrint, parentFingerPrint: this.parentFingerPrint,
childIndex: this.childIndex, childIndex: this.childIndex,
chainCode: this.chainCode, chainCode: this.chainCode,
privateKey: this.privateKey,
checksum: this.checksum, checksum: this.checksum,
publicKey: this.publicKey, publicKey: this.publicKey
master: this.master
}); });
this.xpubkey = this.hdpub.xpubkey; this.xpubkey = this.hdpub.xpubkey;
@ -653,17 +651,19 @@ HDPrivateKey.prototype.derive = function derive(index, hardened) {
leftPart = new bn(hash.slice(0, 32)); leftPart = new bn(hash.slice(0, 32));
chainCode = hash.slice(32, 64); chainCode = hash.slice(32, 64);
privateKey = leftPart.add(new bn(this.privateKey)).mod(ec.curve.n).toArray(); privateKey = leftPart
.add(new bn(this.privateKey))
.mod(ec.curve.n)
.toArray('be', 32);
return new HDPrivateKey({ return new HDPrivateKey({
version: null, version: this.version,
depth: new bn(this.depth).toNumber() + 1, depth: new bn(this.depth).toNumber() + 1,
parentFingerPrint: this.fingerPrint, parentFingerPrint: this.fingerPrint,
childIndex: index, childIndex: index,
chainCode: chainCode, chainCode: chainCode,
privateKey: privateKey, privateKey: privateKey,
checksum: null, checksum: null
master: this.master
}); });
}; };
@ -822,13 +822,12 @@ function HDPublicKey(options) {
this._build(data); this._build(data);
if (new bn(data.parentFingerPrint).cmpn(0) === 0) { if (new bn(data.parentFingerPrint).cmpn(0) === 0)
this.isMaster = true; this.isMaster = true;
this.master = this; else
} else { this.isMaster = false;
this.master = options.master;
}
this.isPrivate = false;
this.isPublic = true; this.isPublic = true;
} }
@ -897,9 +896,10 @@ HDPublicKey.prototype._build = function _build(data) {
off += utils.copy(data.childIndex, sequence, off); off += utils.copy(data.childIndex, sequence, off);
off += utils.copy(data.chainCode, sequence, off); off += utils.copy(data.chainCode, sequence, off);
off += utils.copy(data.publicKey, sequence, off); off += utils.copy(data.publicKey, sequence, off);
assert(off === 78, off);
checksum = utils.dsha256(sequence.slice(0, off)).slice(0, 4); checksum = utils.dsha256(sequence.slice(0, off)).slice(0, 4);
off += utils.copy(checksum, sequence, off); off += utils.copy(checksum, sequence, off);
assert(off === 82); assert(off === 82, off);
if (!data.checksum || !data.checksum.length) if (!data.checksum || !data.checksum.length)
data.checksum = checksum; data.checksum = checksum;
@ -949,14 +949,13 @@ HDPublicKey.prototype.derive = function derive(index, hardened) {
publicKey = bcoin.ecdsa.keyPair({ pub: point }).getPublic(true, 'array'); publicKey = bcoin.ecdsa.keyPair({ pub: point }).getPublic(true, 'array');
return new HDPublicKey({ return new HDPublicKey({
version: null, version: this.version,
depth: new bn(this.depth).toNumber() + 1, depth: new bn(this.depth).toNumber() + 1,
parentFingerPrint: this.fingerPrint, parentFingerPrint: this.fingerPrint,
childIndex: index, childIndex: index,
chainCode: chainCode, chainCode: chainCode,
publicKey: publicKey, publicKey: publicKey,
checksum: null, checksum: null
master: this.master
}); });
}; };