fix pool. hd improvements.

This commit is contained in:
Christopher Jeffrey 2016-02-12 17:54:44 -08:00
parent dbba098190
commit 88c131c7b3
4 changed files with 39 additions and 7 deletions

View File

@ -142,6 +142,9 @@ function HDPrivateKey(options) {
if (!options) if (!options)
options = { seed: bcoin.hd.seed() }; options = { seed: bcoin.hd.seed() };
// if (!options)
// options = this._generate();
if (HDPrivateKey.isExtended(options)) if (HDPrivateKey.isExtended(options))
options = { xkey: options }; options = { xkey: options };
@ -216,6 +219,9 @@ HDPrivateKey.prototype.scan44 = function scan44(options, txByAddress, callback)
? self ? self
: root.derive(accountIndex, true); : root.derive(accountIndex, true);
if (isAccount)
accountIndex = new bn(self.childIndex).toNumber() - constants.hd.hardened;
// 2. derive the external chain node of this account // 2. derive the external chain node of this account
var chain = account.derive(chainConstant); var chain = account.derive(chainConstant);
@ -467,10 +473,14 @@ HDPrivateKey.prototype.deriveCosignerAddress = function deriveCosignerAddress(co
}; };
HDPrivateKey.prototype.isPurpose45 = function isPurpose45(options) { HDPrivateKey.prototype.isPurpose45 = function isPurpose45(options) {
if (new bn(this.depth).toNumber() !== 1)
return false;
return new bn(this.childIndex).toNumber() === constants.hd.hardened + 45; return new bn(this.childIndex).toNumber() === constants.hd.hardened + 45;
}; };
HDPrivateKey.prototype.isAccount44 = function isAccount44(options) { HDPrivateKey.prototype.isAccount44 = function isAccount44(options) {
if (new bn(this.childIndex).toNumber() < constants.hd.hardened)
return false;
return new bn(this.depth).toNumber() === 3; return new bn(this.depth).toNumber() === 3;
}; };
@ -595,6 +605,26 @@ HDPrivateKey.prototype._seed = function _seed(seed) {
}; };
}; };
HDPrivateKey.prototype._generate = function _generate(privateKey) {
if (!privateKey)
privateKey = bcoin.ecdsa.genKeyPair().getPrivate().toArray();
if (utils.isHex(privateKey))
privateKey = utils.toArray(privateKey, 'hex');
else if (utils.isBase58(privateKey))
privateKey = bcoin.keypair.fromSecret(privateKey).getPrivate().toArray();
return {
version: null,
depth: 0,
parentFingerPrint: 0,
childIndex: 0,
chainCode: elliptic.rand(32),
privateKey: privateKey,
checksum: null
};
};
HDPrivateKey.prototype._unbuild = function _unbuild(xkey) { HDPrivateKey.prototype._unbuild = function _unbuild(xkey) {
var raw = utils.fromBase58(xkey); var raw = utils.fromBase58(xkey);
var data = {}; var data = {};
@ -662,7 +692,7 @@ HDPrivateKey.prototype._build = function _build(data) {
this.fingerPrint = fingerPrint; this.fingerPrint = fingerPrint;
this.publicKey = publicKey; this.publicKey = publicKey;
this.hdpub = new HDPublicKey({ this.hdPublicKey = new HDPublicKey({
version: this.version, version: this.version,
depth: this.depth, depth: this.depth,
parentFingerPrint: this.parentFingerPrint, parentFingerPrint: this.parentFingerPrint,
@ -672,7 +702,7 @@ HDPrivateKey.prototype._build = function _build(data) {
publicKey: this.publicKey publicKey: this.publicKey
}); });
this.xpubkey = this.hdpub.xpubkey; this.xpubkey = this.hdPublicKey.xpubkey;
this.pair = bcoin.ecdsa.keyPair({ priv: this.privateKey }); this.pair = bcoin.ecdsa.keyPair({ priv: this.privateKey });
}; };

View File

@ -189,7 +189,7 @@ Wallet.prototype.addKey = function addKey(key) {
key = bcoin.hd.publicKey(key); key = bcoin.hd.publicKey(key);
if (key instanceof bcoin.hd.privateKey) if (key instanceof bcoin.hd.privateKey)
key = key.hdpub; key = key.hdPublicKey;
if (this.derivation === 'bip44') { if (this.derivation === 'bip44') {
if (!key || !key.isAccount44()) if (!key || !key.isAccount44())
@ -233,7 +233,7 @@ Wallet.prototype.removeKey = function removeKey(key) {
key = key.hd; key = key.hd;
if (key instanceof bcoin.hd.privateKey) if (key instanceof bcoin.hd.privateKey)
key = key.hdpub; key = key.hdPublicKey;
if (this.derivation === 'bip44') { if (this.derivation === 'bip44') {
if (!key || !key.isAccount44()) if (!key || !key.isAccount44())
@ -377,6 +377,8 @@ Wallet.prototype.deriveAddress = function deriveAddress(change, index) {
if (this.prefixType === 'scripthash') if (this.prefixType === 'scripthash')
this.addressMap[address.getScriptAddress()] = data.path; this.addressMap[address.getScriptAddress()] = data.path;
this.emit('add address', address);
return address; return address;
}; };

View File

@ -49,7 +49,7 @@ describe('HD', function() {
}); });
it('should derive(1) child from master public key', function() { it('should derive(1) child from master public key', function() {
child2 = master.hdpub.derive(1); child2 = master.hdPublicKey.derive(1);
assert.equal(child2.xpubkey, child2_pub); assert.equal(child2.xpubkey, child2_pub);
}); });
@ -82,7 +82,7 @@ describe('HD', function() {
}); });
it('should deserialize master public key', function() { it('should deserialize master public key', function() {
master.hdpub._unbuild(master.hdpub.xpubkey); master.hdPublicKey._unbuild(master.hdPublicKey.xpubkey);
}); });
it('should deserialize and reserialize', function() { it('should deserialize and reserialize', function() {

View File

@ -52,7 +52,7 @@ describe('Wallet', function() {
m: 1, m: 1,
n: 2 n: 2
}); });
var k2 = bcoin.hd.priv().deriveAccount44(0).hdpub; var k2 = bcoin.hd.priv().deriveAccount44(0).hdPublicKey;
w.addKey(k2); w.addKey(k2);
// Input transcation // Input transcation