From 88c131c7b3dfcc5e7629b0b27a133b921e90f7c2 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 12 Feb 2016 17:54:44 -0800 Subject: [PATCH] fix pool. hd improvements. --- lib/bcoin/hd.js | 34 ++++++++++++++++++++++++++++++++-- lib/bcoin/wallet.js | 6 ++++-- test/hd-test.js | 4 ++-- test/wallet-test.js | 2 +- 4 files changed, 39 insertions(+), 7 deletions(-) diff --git a/lib/bcoin/hd.js b/lib/bcoin/hd.js index 0e6296cd..fe7b294a 100644 --- a/lib/bcoin/hd.js +++ b/lib/bcoin/hd.js @@ -142,6 +142,9 @@ function HDPrivateKey(options) { if (!options) options = { seed: bcoin.hd.seed() }; + // if (!options) + // options = this._generate(); + if (HDPrivateKey.isExtended(options)) options = { xkey: options }; @@ -216,6 +219,9 @@ HDPrivateKey.prototype.scan44 = function scan44(options, txByAddress, callback) ? self : root.derive(accountIndex, true); + if (isAccount) + accountIndex = new bn(self.childIndex).toNumber() - constants.hd.hardened; + // 2. derive the external chain node of this account var chain = account.derive(chainConstant); @@ -467,10 +473,14 @@ HDPrivateKey.prototype.deriveCosignerAddress = function deriveCosignerAddress(co }; 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; }; HDPrivateKey.prototype.isAccount44 = function isAccount44(options) { + if (new bn(this.childIndex).toNumber() < constants.hd.hardened) + return false; 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) { var raw = utils.fromBase58(xkey); var data = {}; @@ -662,7 +692,7 @@ HDPrivateKey.prototype._build = function _build(data) { this.fingerPrint = fingerPrint; this.publicKey = publicKey; - this.hdpub = new HDPublicKey({ + this.hdPublicKey = new HDPublicKey({ version: this.version, depth: this.depth, parentFingerPrint: this.parentFingerPrint, @@ -672,7 +702,7 @@ HDPrivateKey.prototype._build = function _build(data) { publicKey: this.publicKey }); - this.xpubkey = this.hdpub.xpubkey; + this.xpubkey = this.hdPublicKey.xpubkey; this.pair = bcoin.ecdsa.keyPair({ priv: this.privateKey }); }; diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index a9067323..26a9eb82 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -189,7 +189,7 @@ Wallet.prototype.addKey = function addKey(key) { key = bcoin.hd.publicKey(key); if (key instanceof bcoin.hd.privateKey) - key = key.hdpub; + key = key.hdPublicKey; if (this.derivation === 'bip44') { if (!key || !key.isAccount44()) @@ -233,7 +233,7 @@ Wallet.prototype.removeKey = function removeKey(key) { key = key.hd; if (key instanceof bcoin.hd.privateKey) - key = key.hdpub; + key = key.hdPublicKey; if (this.derivation === 'bip44') { if (!key || !key.isAccount44()) @@ -377,6 +377,8 @@ Wallet.prototype.deriveAddress = function deriveAddress(change, index) { if (this.prefixType === 'scripthash') this.addressMap[address.getScriptAddress()] = data.path; + this.emit('add address', address); + return address; }; diff --git a/test/hd-test.js b/test/hd-test.js index 10feb094..76a41bb4 100644 --- a/test/hd-test.js +++ b/test/hd-test.js @@ -49,7 +49,7 @@ describe('HD', 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); }); @@ -82,7 +82,7 @@ describe('HD', 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() { diff --git a/test/wallet-test.js b/test/wallet-test.js index 126af919..6b2aad90 100644 --- a/test/wallet-test.js +++ b/test/wallet-test.js @@ -52,7 +52,7 @@ describe('Wallet', function() { m: 1, n: 2 }); - var k2 = bcoin.hd.priv().deriveAccount44(0).hdpub; + var k2 = bcoin.hd.priv().deriveAccount44(0).hdPublicKey; w.addKey(k2); // Input transcation