From c4f16f8d7225d10f7ba4cbeaf12dc9ee7f110f5e Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 12 Aug 2016 15:32:56 -0700 Subject: [PATCH] wallet: refactor. --- lib/bcoin/lowlevelup.js | 10 +++++++++- lib/bcoin/txdb.js | 2 +- lib/bcoin/wallet.js | 16 ++++++++-------- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/lib/bcoin/lowlevelup.js b/lib/bcoin/lowlevelup.js index d075cfd1..c8247340 100644 --- a/lib/bcoin/lowlevelup.js +++ b/lib/bcoin/lowlevelup.js @@ -251,7 +251,15 @@ LowlevelUp.prototype.fetch = function fetch(key, parse, callback) { }); }; +/** + * Iterate over each record. + * @param {Object} options + * @param {Function} handler + * @param {Function} callback - Returns [Error, Object]. + */ + LowlevelUp.prototype.each = function each(options, handler, callback) { + var i = 0; var opt, iter; opt = { @@ -284,7 +292,7 @@ LowlevelUp.prototype.each = function each(options, handler, callback) { if (key === undefined) return iter.end(callback); - handler(key, value, next); + handler(key, value, next, i++); }); })(); }; diff --git a/lib/bcoin/txdb.js b/lib/bcoin/txdb.js index be44e456..4a2f9dba 100644 --- a/lib/bcoin/txdb.js +++ b/lib/bcoin/txdb.js @@ -517,8 +517,8 @@ TXDB.prototype.add = function add(tx, info, callback) { return self._addOrphan(key, outpoint, next); self.del('c/' + key); - self.put('d/' + hash + '/' + i, input.coin.toRaw()); self.del('C/' + path.account + '/' + key); + self.put('d/' + hash + '/' + i, input.coin.toRaw()); self.coinCache.remove(key); diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index 35430f74..cffbd46a 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -881,6 +881,7 @@ Wallet.prototype.send = function send(options, callback) { if (err) return callback(err); + self.logger.debug('Sending wallet tx (%s): %s', self.id, tx.rhash); self.db.emit('send', tx); return callback(null, tx); @@ -899,6 +900,7 @@ Wallet.prototype.send = function send(options, callback) { Wallet.prototype.deriveInputs = function deriveInputs(tx, callback) { var self = this; var addresses = []; + var address; this.getInputPaths(tx, function(err, paths) { if (err) @@ -912,7 +914,8 @@ Wallet.prototype.deriveInputs = function deriveInputs(tx, callback) { if (!account) return next(); - addresses.push(account.deriveAddress(path.change, path.index)); + address = account.deriveAddress(path.change, path.index); + addresses.push(address); return next(); }); @@ -2317,8 +2320,7 @@ Account.prototype.createChange = function createChange(callback) { Account.prototype.createAddress = function createAddress(change, callback) { var self = this; - var addresses = []; - var address; + var address, lookahead; if (typeof change === 'function') { callback = change; @@ -2327,19 +2329,17 @@ Account.prototype.createAddress = function createAddress(change, callback) { if (change) { address = this.deriveChange(this.changeDepth); - addresses.push(address); - addresses.push(this.deriveChange(this.changeDepth + this.lookahead)); + lookahead = this.deriveChange(this.changeDepth + this.lookahead); this.changeDepth++; this.changeAddress = address; } else { address = this.deriveReceive(this.receiveDepth); - addresses.push(address); - addresses.push(this.deriveReceive(this.receiveDepth + this.lookahead)); + lookahead = this.deriveReceive(this.receiveDepth + this.lookahead); this.receiveDepth++; this.receiveAddress = address; } - this.saveAddress(addresses, function(err) { + this.saveAddress([address, lookahead], function(err) { if (err) return callback(err);