From cf5050223879fad1a398430071302eadc5983187 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 3 Feb 2016 21:46:02 -0800 Subject: [PATCH] more fixes. --- lib/bcoin/wallet.js | 46 +++++++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index 8729b581..ec529740 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -70,7 +70,7 @@ function Wallet(options) { this.accountIndex = options.accountIndex || 0; this.addressDepth = options.addressDepth || 0; this.changeDepth = options.changeDepth || 0; - this.cosignerIndex = 0; + this.cosignerIndex = -1; this.purposeKeys = options.purposeKeys || []; this.keys = options.keys || []; @@ -350,12 +350,23 @@ Wallet.prototype.finalizeKeys = function finalizeKeys(key) { } } + assert(this.cosignerIndex !== -1); + this._initAddresses(); return; } this.keys = utils.sortKeys(this.keys); + for (i = 0; i < this.keys.length; i++) { + if (utils.isEqual(this.keys[i], this.current.publicKey)) { + this.cosignerIndex = i; + break; + } + } + + assert(this.cosignerIndex !== -1); + this._initAddresses(); }; @@ -509,22 +520,8 @@ Wallet.prototype.createAddress = function createAddress(change, index) { change: change }; - if (this.bip45) { - this.purposeKeys.forEach(function(key, cosignerIndex) { - key = key - .derive(cosignerIndex) - .derive(change ? 1 : 0) - .derive(change ? self.changeDepth : self.addressDepth); - options.keys.push(key.publicKey); - }); - this.keys = utils.sortKeys(options.keys); - } else { - this.keys.forEach(function(key, i) { - options.keys.push(key); - }); - } - if (index == null) { + index = change ? self.changeDepth : self.addressDepth; if (this.hd) { if (change) this.changeDepth++; @@ -533,6 +530,23 @@ Wallet.prototype.createAddress = function createAddress(change, index) { } } + if (this.bip45) { + this.purposeKeys.forEach(function(key, cosignerIndex) { + key = key + .derive(cosignerIndex) + .derive(change ? 1 : 0) + .derive(index); + options.keys.push(key.publicKey); + }); + this.keys = utils.sortKeys(options.keys); + } else { + this.keys.forEach(function(key, i) { + if (i !== this.cosignerIndex) + options.keys.push(key); + }, this); + options.keys.push(key.pub); + } + address = this.addAddress(options); return address;