From 0a5fcad5e80c15450c6bacc9612c6cdf78803354 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 14 Aug 2016 16:46:49 -0700 Subject: [PATCH] keyring: add derive method. --- lib/bcoin/http/rpc.js | 13 +++++-------- lib/bcoin/keyring.js | 12 ++++++++++++ lib/bcoin/wallet.js | 3 +-- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/lib/bcoin/http/rpc.js b/lib/bcoin/http/rpc.js index 4599f6d1..46d47b62 100644 --- a/lib/bcoin/http/rpc.js +++ b/lib/bcoin/http/rpc.js @@ -2577,8 +2577,7 @@ RPC.prototype.dumpprivkey = function dumpprivkey(args, callback) { if (!key) return callback(new RPCError('Wallet is locked.')); - key = key.deriveAccount44(ring.account); - key = key.derive(ring.change).derive(ring.index); + key = ring.derive(key); callback(null, key.toSecret()); }); @@ -2620,8 +2619,7 @@ RPC.prototype.dumpwallet = function dumpwallet(args, callback) { if (!key) return callback(new RPCError('Wallet is locked.')); - key = key.deriveAccount44(ring.account); - key = key.derive(ring.change).derive(ring.index); + key = ring.derive(key); address = ring.getAddress('base58'); fmt = '%s %s label= addr=%s'; @@ -3554,11 +3552,11 @@ RPC.prototype.signmessage = function signmessage(args, callback) { if (!address) return callback(new RPCError('Invalid address.')); - this.wallet.getKeyring(address, function(err, address) { + this.wallet.getKeyring(address, function(err, ring) { if (err) return callback(err); - if (!address) + if (!ring) return callback(new RPCError('Address not found.')); key = self.wallet.master.key; @@ -3566,8 +3564,7 @@ RPC.prototype.signmessage = function signmessage(args, callback) { if (!key) return callback(new RPCError('Wallet is locked.')); - key = key.deriveAccount44(address.account); - key = key.derive(address.change).derive(address.index); + key = ring.derive(key); msg = new Buffer(RPC.magic + msg, 'utf8'); msg = utils.hash256(msg); diff --git a/lib/bcoin/keyring.js b/lib/bcoin/keyring.js index 863e5570..0a24e68b 100644 --- a/lib/bcoin/keyring.js +++ b/lib/bcoin/keyring.js @@ -579,6 +579,18 @@ KeyRing.prototype.sign = function sign(tx, key, index, type) { return total; }; +/** + * Derive to address index. + * @param {HDPrivateKey} key + * @returns {HDPrivateKey} + */ + +KeyRing.prototype.derive = function derive(key) { + if (key.isMaster()) + key = key.deriveAccount44(this.account); + return key.derive(this.change).derive(this.index); +}; + KeyRing.prototype.__defineGetter__('publicKey', function() { return this.getPublicKey(); }); diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index 7dec14a7..3a6144c0 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -1350,8 +1350,7 @@ Wallet.sign = function sign(addresses, master, tx, index, type) { for (i = 0; i < addresses.length; i++) { address = addresses[i]; - key = master.deriveAccount44(address.account); - key = key.derive(address.change).derive(address.index); + key = address.derive(master); assert(utils.equal(key.getPublicKey(), address.key)); total += address.sign(tx, key, index, type); }