diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index ff87724c..f0968458 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -74,8 +74,10 @@ function Wallet(options) { this.purposeKeys = options.purposeKeys || []; this.keys = options.keys || []; + this.normal = false; this.hd = false; - this.hdpm = false; + this.bip44 = false; + this.bip45 = false; this.multisig = false; this.type = options.type || 'pubkeyhash'; @@ -94,11 +96,15 @@ function Wallet(options) { this.subtype = 'multisig'; } - if (this.master) + if (this.master) { this.hd = true; - - if (this.master && this.type === 'scripthash' && this.subtype === 'multisig') - this.hdpm = true; + if (this.type === 'scripthash' && this.subtype === 'multisig') + this.bip45 = true; + else + this.bip44 = true; + } else { + this.normal = true; + } if (this.type === 'multisig' || this.subtype === 'multisig') this.multisig = true; @@ -112,12 +118,12 @@ function Wallet(options) { if (this.n < 1 || this.n > this.nmax) throw new Error('n ranges between 1 and ' + this.nmax); - if (this.hdpm) { - this.purposeKey = this.master.hd.isPublic + if (this.bip45) { + this.purposeKey = this.master.hd.isPurpose45() ? this.master.hd : this.master.hd.derivePurpose45(); } else if (this.hd) { - this.accountKey = this.master.hd.isPublic + this.accountKey = this.master.hd.isAccount44() ? this.master.hd : this.master.hd.deriveAccount44(this.accountIndex); } @@ -151,20 +157,54 @@ function Wallet(options) { this.loading = true; this.lastTs = 0; - if (!this.hdpm) { - if (options.addresses.length) { - this.current = bcoin.address(options.addresses[options.addresses.length - 1]); - this._firstKey = { - priv: this.current.key._key.getPrivate().toArray(), - pub: this.current.key._key.getPublic(true, 'array') - }; + var key, receiving; + + // This is a chicken and egg problem for BIP45. Real address keys cannot be + // generated until all shared keys have been added to the wallet. The flow of + // this wallet is, the actual address objects will be generated once all + // shared keys have been added. This presents a problem for non-bip45 + // wallets: if they want to use the addKey() interface with + // wallet.getPublicKey(), we need to expose a key for them to use. Here, we + // generate the last receiving address. However, since "normal" wallets + // cannot deterministically generate keys, we have to buffer the generated + // key for later. + if (!this.bip45) { + if (this.hd) { + // Generate the last known receiving address + key = this.createKey(false, Math.max(0, this.addressDepth - 1)); + this.current = bcoin.address({ + priv: key.priv, + type: this.type, + subtype: this.subtype, + m: this.m, + n: this.n, + keys: options.keys + }); } else { - this._firstKey = this.createKey(false, Math.max(0, this.addressDepth - 1)); - this.current = bcoin.address({ priv: this._firstKey.priv }); + // Try to find the last receiving address if there is one. + receiving = options.addresses.filter(function(address) { + return !address.change && (address.priv || address.pub || address.key); + }).pop(); + if (receiving) { + this.current = bcoin.address(receiving); + } else { + // No receiving address is in this wallet yet, generate + // it and save it so createKey can recreate it later. + key = this.createKey(); + this._firstKey = key; + this.current = bcoin.address({ + priv: key.priv, + type: this.type, + subtype: this.subtype, + m: this.m, + n: this.n, + keys: options.keys + }); + } } } - if (this.hdpm) + if (this.bip45) this.addKey(this.purposeKey); else this.addKey(this.current.publicKey); @@ -176,50 +216,61 @@ function Wallet(options) { inherits(Wallet, EventEmitter); +// Wallet ID: +// bip45: Purpose key address +// HD: Account key address +// Normal: Address of first key in wallet +Wallet.prototype.getID = function() { + if (this.bip45) + return bcoin.address.key2addr(this.purposeKey.publicKey); + + if (this.hd) + return bcoin.address.key2addr(this.accountKey.publicKey); + + if (this.addresses.length) + return this.addresses[0].getKeyAddress(); + + if (this._firstKey) + return bcoin.address.key2addr(this._firstKey.pub); + + assert(false); +}; + Wallet.prototype._initAddresses = function() { var options = this.options; + assert(!this._initialized); + this._initialized = true; + options.addresses.forEach(function(address) { address = this.addAddress(address); - if (!address.change) - this.current = address; + if (!this.hd) { + if (!address.change) + this.current = address; + else + this.changeAddress = address; + } }, this); if (this.hd) { for (i = 0; i < this.addressDepth; i++) - this.createAddress(false, i); + this.current = this.createAddress(false, i); for (i = 0; i < this.changeDepth; i++) - this.createAddress(true, i); + this.changeAddress = this.createAddress(true, i); } - // Create a non-master account address if we don't have one. - if (this.addresses.length === 0) - this.createAddress(); + if (!this.current) + this.current = this.createAddress(); - // Find the last change address if there is one. - if (this.hd) { - if (this.changeDepth === 0) - this.changeAddress = this.createAddress(true); - else - this.changeAddress = this.addresses[this.addresses.length - 1]; - } else { - for (i = this.addresses.length - 1; i >= 0; i--) { - if (this.addresses[i].change) - break; - } - - if (i === -1) - this.changeAddress = this.createAddress(true); - else - this.changeAddress = this.addresses[i]; - } + if (!this.changeAddress) + this.changeAddress = this.createAddress(true); assert(this.current); assert(!this.current.change); assert(this.changeAddress.change); - this.prefix = 'bt/wallet/' + this.getKeyAddress() + '/'; + this.prefix = 'bt/wallet/' + this.getID() + '/'; this.tx = new bcoin.txPool(this); @@ -245,9 +296,9 @@ Wallet.prototype.addKey = function addKey(key) { key = hdKey.publicKey; } - if (this.hdpm) { + if (this.bip45) { if (!hdKey || !hdKey.isPurpose45()) - throw new Error('Must add HD purpose keys to HD wallet.'); + throw new Error('Must add HD purpose keys to BIP45 wallet.'); has = this.purposeKeys.some(function(pub) { return pub.xpubkey === hdKey.xpubkey; @@ -256,6 +307,8 @@ Wallet.prototype.addKey = function addKey(key) { if (has) return; + assert(!this._keysFinalized); + this.purposeKeys.push(hdKey); if (this.purposeKeys.length === this.n) @@ -273,6 +326,8 @@ Wallet.prototype.addKey = function addKey(key) { if (has) return; + assert(!this._keysFinalized); + this.keys.push(key); if (this.keys.length === this.n) @@ -280,11 +335,14 @@ Wallet.prototype.addKey = function addKey(key) { }; Wallet.prototype.finalizeKeys = function finalizeKeys(key) { - if (this.hdpm) { + assert(!this._keysFinalized); + this._keysFinalized = true; + + if (this.bip45) { this.purposeKeys = utils.sortHDKeys(this.purposeKeys); for (i = 0; i < this.purposeKeys.length; i++) { - if (utils.isEqual(this.purposeKeys[i].publicKey, this.purposeKey.publicKey)) { + if (this.purposeKeys[i].xpubkey === this.purposeKey.xpubkey) { this.cosignerIndex = i; break; } @@ -302,6 +360,8 @@ Wallet.prototype.finalizeKeys = function finalizeKeys(key) { Wallet.prototype.removeKey = function removeKey(key) { var hdKey, index; + assert(!this._keysFinalized); + if (bcoin.hd.priv.isExtended(key)) key = bcoin.hd.priv(key); else if (bcoin.hd.pub.isExtended(key)) @@ -318,9 +378,9 @@ Wallet.prototype.removeKey = function removeKey(key) { key = hd.publicKey; } - if (this.hdpm) { + if (this.bip45) { if (!hdKey || !hdKey.isPurpose45()) - throw new Error('Must add HD purpose keys to HD wallet.'); + throw new Error('Must add HD purpose keys to BIP45 wallet.'); index = this.purposeKeys.map(function(pub, i) { return pub.xpubkey === hdKey.xpubkey ? i : null; @@ -369,11 +429,13 @@ Wallet.prototype._init = function init() { }); this.tx.on('tx', function(tx) { - // TX using this change address was - // confirmed. Allocate a new change address. + // TX using this address was confirmed. + // Allocate a new address. if (tx.block) { + if (self.current.ownOutput(tx)) + self.current = self.createAddress(); if (self.changeAddress.ownOutput(tx)) - self.changeAddress = self.createChangeAddress(); + self.changeAddress = self.createAddress(true); } self.emit('tx', tx); }); @@ -432,6 +494,8 @@ Wallet.prototype.createAddress = function createAddress(change, index) { var key = this.createKey(change, index); var address; + assert(this._initialized); + var options = { priv: key.priv, pub: key.pub, @@ -443,7 +507,7 @@ Wallet.prototype.createAddress = function createAddress(change, index) { change: change }; - if (this.hdpm) { + if (this.bip45) { this.purposeKeys.forEach(function(key, cosignerIndex) { key = key .derive(cosignerIndex) @@ -469,9 +533,6 @@ Wallet.prototype.createAddress = function createAddress(change, index) { address = this.addAddress(options); - if (!change) - this.current = address; - return address; }; @@ -492,6 +553,8 @@ Wallet.prototype.addAddress = function addAddress(address) { var self = this; var index; + assert(this._initialized); + if (!(address instanceof bcoin.address)) address = bcoin.address(address); @@ -527,6 +590,8 @@ Wallet.prototype.addAddress = function addAddress(address) { Wallet.prototype.removeAddress = function removeAddress(address) { var i; + assert(this._initialized); + assert(address instanceof bcoin.address); i = this._addressIndex(address); @@ -595,7 +660,7 @@ Wallet.prototype.createKey = function createKey(change, index) { if (index == null) index = change ? this.changeDepth : this.addressDepth; - if (this.hdpm) { + if (this.bip45) { key = this.purposeKey .derive(this.cosignerIndex) .derive(change ? 1 : 0) @@ -642,6 +707,8 @@ Wallet.prototype.ownOutput = function ownOutput(tx, index) { Wallet.prototype.fill = function fill(tx, address, fee) { var unspent, items, result; + assert(this._initialized); + if (!address) address = this.changeAddress.getKeyAddress(); @@ -841,23 +908,32 @@ Wallet.prototype.__defineGetter__('address', function() { }); Wallet.prototype.toJSON = function toJSON(encrypt) { + assert(this._initialized); return { v: 3, name: 'wallet', network: network.type, + type: this.type, + subtype: this.subtype, + m: this.m, + n: this.n, accountIndex: this.accountIndex, addressDepth: this.addressDepth, changeDepth: this.changeDepth, + cosignerIndex: this.cosignerIndex, + keys: this.bip45 + ? this.purposeKeys.map(function(key) { + return key.xpubkey; + }) + : this.keys.map(function(key) { + return utils.toBase58(key); + }), master: this.master ? this.master.toJSON(encrypt) : null, addresses: this.addresses.filter(function(address) { - if (!address.key.hd) - return true; - - if (address.change) + if (this.hd) return false; - return true; - }).map(function(address) { + }, this).map(function(address) { return address.toJSON(encrypt); }), balance: utils.toBTC(this.getBalance()), @@ -875,12 +951,18 @@ Wallet.fromJSON = function fromJSON(json, decrypt) { assert.equal(json.network, network.type); w = new Wallet({ + type: json.type, + subtype: json.subtype, + m: json.m, + n: json.n, accountIndex: json.accountIndex, addressDepth: json.addressDepth, changeDepth: json.changeDepth, + cosignerIndex: json.cosignerIndex, master: json.master ? bcoin.address.fromJSON(json.master, decrypt) : null, + keys: json.keys, addresses: json.addresses.map(function(address) { return bcoin.address.fromJSON(address, decrypt); }) @@ -888,17 +970,6 @@ Wallet.fromJSON = function fromJSON(json, decrypt) { w.tx.fromJSON(json.tx); - // Make sure we have all the change - // addresses (we don't save them). - if (w.master) { - for (i = 0; i < w.changeDepth; i++) { - w.addAddress({ - change: true, - priv: w.master.key.hd.deriveChange(w.accountIndex, i) - }); - } - } - return w; };