diff --git a/lib/server.js b/lib/server.js index 9786c46..7f3a7c9 100644 --- a/lib/server.js +++ b/lib/server.js @@ -855,44 +855,53 @@ WalletService.prototype._getUtxosForAddresses = function(addresses, cb) { }); }; -WalletService.prototype._getUtxosForCurrentWallet = function(cb) { +WalletService.prototype._getUtxosForCurrentWallet = function(addresses, cb) { var self = this; function utxoKey(utxo) { return utxo.txid + '|' + utxo.vout }; - // Get addresses for this wallet - self.storage.fetchAddresses(self.walletId, function(err, addresses) { - if (err) return cb(err); + async.waterfall([ - var addressStrs = _.pluck(addresses, 'address'); - self._getUtxosForAddresses(addressStrs, function(err, utxos) { - if (err) return cb(err); - if (utxos.length == 0) return cb(null, []); + function(next) { + if (_.isArray(addresses) && addresses.length > 0) { + next(null, addresses); + } else { + self.storage.fetchAddresses(self.walletId, next); + } + }, + function(addresses, next) { + if (addresses.length == 0) return next(null, []); - self.getPendingTxs({}, function(err, txps) { - if (err) return cb(err); + var addressStrs = _.pluck(addresses, 'address'); + self._getUtxosForAddresses(addressStrs, function(err, utxos) { + if (err) return next(err); + if (utxos.length == 0) return next(null, []); - var lockedInputs = _.map(_.flatten(_.pluck(txps, 'inputs')), utxoKey); - var utxoIndex = _.indexBy(utxos, utxoKey); - _.each(lockedInputs, function(input) { - if (utxoIndex[input]) { - utxoIndex[input].locked = true; - } + self.getPendingTxs({}, function(err, txps) { + if (err) return next(err); + + var lockedInputs = _.map(_.flatten(_.pluck(txps, 'inputs')), utxoKey); + var utxoIndex = _.indexBy(utxos, utxoKey); + _.each(lockedInputs, function(input) { + if (utxoIndex[input]) { + utxoIndex[input].locked = true; + } + }); + + // Needed for the clients to sign UTXOs + var addressToPath = _.indexBy(addresses, 'address'); + _.each(utxos, function(utxo) { + utxo.path = addressToPath[utxo.address].path; + utxo.publicKeys = addressToPath[utxo.address].publicKeys; + }); + + return next(null, utxos); }); - - // Needed for the clients to sign UTXOs - var addressToPath = _.indexBy(addresses, 'address'); - _.each(utxos, function(utxo) { - utxo.path = addressToPath[utxo.address].path; - utxo.publicKeys = addressToPath[utxo.address].publicKeys; - }); - - return cb(null, utxos); }); - }); - }); + }, + ], cb); }; /** @@ -907,7 +916,7 @@ WalletService.prototype.getUtxos = function(opts, cb) { opts = opts || {}; if (_.isUndefined(opts.addresses)) { - self._getUtxosForCurrentWallet(cb); + self._getUtxosForCurrentWallet(null, cb); } else { self._getUtxosForAddresses(opts.addresses, cb); } @@ -981,7 +990,7 @@ WalletService.prototype.getBalance = function(opts, cb) { if (err) { log.error('Could not compute size of send max transaction', err); } - balance.totalBytesToSendMax = size || null; + balance.totalBytesToSendMax = _.isNumber(size) ? size : null; return cb(null, balance); }); }); @@ -1600,7 +1609,7 @@ WalletService.prototype._broadcastRawTx = function(network, raw, cb) { bc.broadcast(raw, function(err, txid) { if (err) return cb(err); return cb(null, txid); - }) + }); }; /**