accept address list when fetching utxos for current wallet

This commit is contained in:
Ivan Socolsky 2015-12-04 15:56:42 -03:00
parent a838978b3f
commit 8c0882bf82
1 changed files with 39 additions and 30 deletions

View File

@ -855,44 +855,53 @@ WalletService.prototype._getUtxosForAddresses = function(addresses, cb) {
}); });
}; };
WalletService.prototype._getUtxosForCurrentWallet = function(cb) { WalletService.prototype._getUtxosForCurrentWallet = function(addresses, cb) {
var self = this; var self = this;
function utxoKey(utxo) { function utxoKey(utxo) {
return utxo.txid + '|' + utxo.vout return utxo.txid + '|' + utxo.vout
}; };
// Get addresses for this wallet async.waterfall([
self.storage.fetchAddresses(self.walletId, function(err, addresses) {
if (err) return cb(err);
var addressStrs = _.pluck(addresses, 'address'); function(next) {
self._getUtxosForAddresses(addressStrs, function(err, utxos) { if (_.isArray(addresses) && addresses.length > 0) {
if (err) return cb(err); next(null, addresses);
if (utxos.length == 0) return cb(null, []); } else {
self.storage.fetchAddresses(self.walletId, next);
}
},
function(addresses, next) {
if (addresses.length == 0) return next(null, []);
self.getPendingTxs({}, function(err, txps) { var addressStrs = _.pluck(addresses, 'address');
if (err) return cb(err); 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); self.getPendingTxs({}, function(err, txps) {
var utxoIndex = _.indexBy(utxos, utxoKey); if (err) return next(err);
_.each(lockedInputs, function(input) {
if (utxoIndex[input]) { var lockedInputs = _.map(_.flatten(_.pluck(txps, 'inputs')), utxoKey);
utxoIndex[input].locked = true; 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 || {}; opts = opts || {};
if (_.isUndefined(opts.addresses)) { if (_.isUndefined(opts.addresses)) {
self._getUtxosForCurrentWallet(cb); self._getUtxosForCurrentWallet(null, cb);
} else { } else {
self._getUtxosForAddresses(opts.addresses, cb); self._getUtxosForAddresses(opts.addresses, cb);
} }
@ -981,7 +990,7 @@ WalletService.prototype.getBalance = function(opts, cb) {
if (err) { if (err) {
log.error('Could not compute size of send max transaction', 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); return cb(null, balance);
}); });
}); });
@ -1600,7 +1609,7 @@ WalletService.prototype._broadcastRawTx = function(network, raw, cb) {
bc.broadcast(raw, function(err, txid) { bc.broadcast(raw, function(err, txid) {
if (err) return cb(err); if (err) return cb(err);
return cb(null, txid); return cb(null, txid);
}) });
}; };
/** /**