diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index 7c4df38b..7bbe073a 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -800,22 +800,23 @@ Wallet.prototype.syncOutputDepth = function syncOutputDepth(tx, callback) { utils.forEachSerial(Object.keys(accounts), function(index, next) { var paths = accounts[index]; - var depth = { changeDepth: -1, receiveDepth: -1 }; + var receiveDepth = -1; + var changeDepth = -1; for (i = 0; i < paths.length; i++) { path = paths[i]; if (path.change) { - if (path.index > depth.changeDepth) - depth.changeDepth = path.index; + if (path.index > changeDepth) + changeDepth = path.index; } else { - if (path.index > depth.receiveDepth) - depth.receiveDepth = path.index; + if (path.index > receiveDepth) + receiveDepth = path.index; } } - depth.changeDepth++; - depth.receiveDepth++; + receiveDepth += 2; + changeDepth += 2; self.getAccount(+index, function(err, account) { if (err) @@ -824,7 +825,7 @@ Wallet.prototype.syncOutputDepth = function syncOutputDepth(tx, callback) { if (!account) return next(); - account.setDepth(depth.receiveDepth + 1, depth.changeDepth + 1, function(err, rec, chng) { + account.setDepth(receiveDepth, changeDepth, function(err, rec, chng) { if (err) return next(err); @@ -1547,8 +1548,8 @@ function Account(options) { this.witness = options.witness || false; this.accountKey = options.accountKey; this.accountIndex = options.accountIndex; - this.receiveDepth = options.receiveDepth || 1; - this.changeDepth = options.changeDepth || 1; + this.receiveDepth = options.receiveDepth || 0; + this.changeDepth = options.changeDepth || 0; this.type = options.type || 'pubkeyhash'; this.keys = []; this.m = options.m || 1; @@ -1670,29 +1671,10 @@ Account.prototype.init = function init(callback) { this.initialized = true; - for (i = 0; i < this.receiveDepth - 1; i++) - addresses.push(this.deriveReceive(i)); + assert(this.receiveDepth === 0); + assert(this.changeDepth === 0); - for (i = 0; i < this.changeDepth - 1; i++) - addresses.push(this.deriveChange(i)); - - for (i = this.receiveDepth; i < this.receiveDepth + this.lookahead; i++) - addresses.push(this.deriveReceive(i)); - - for (i = this.changeDepth; i < this.changeDepth + this.lookahead; i++) - addresses.push(this.deriveChange(i)); - - this.receiveAddress = this.deriveReceive(this.receiveDepth - 1); - this.changeAddress = this.deriveChange(this.changeDepth - 1); - - addresses.push(this.receiveAddress); - addresses.push(this.changeAddress); - - return this.saveAddress(addresses, function(err) { - if (err) - return callback(err); - return self.save(callback); - }); + this.setDepth(1, 1, callback); }; /** @@ -1882,7 +1864,7 @@ Account.prototype.createChange = function createChange(callback) { /** * Create a new address (increments depth). * @param {Boolean} change - * @returns {KeyRing} + * @param {Function} callback - Returns [Error, {@link KeyRing}]. */ Account.prototype.createAddress = function createAddress(change, callback) { @@ -2028,7 +2010,7 @@ Account.prototype.setDepth = function setDepth(receiveDepth, changeDepth, callba if (changeDepth > this.changeDepth) { for (i = this.changeDepth; i < changeDepth; i++) { change = this.deriveChange(i); - addresses.push(this.changeAddress); + addresses.push(change); } for (i = changeDepth; i < changeDepth + this.lookahead; i++)