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,24 +855,32 @@ 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([
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, []);
var addressStrs = _.pluck(addresses, 'address');
self._getUtxosForAddresses(addressStrs, function(err, utxos) {
if (err) return cb(err);
if (utxos.length == 0) return cb(null, []);
if (err) return next(err);
if (utxos.length == 0) return next(null, []);
self.getPendingTxs({}, function(err, txps) {
if (err) return cb(err);
if (err) return next(err);
var lockedInputs = _.map(_.flatten(_.pluck(txps, 'inputs')), utxoKey);
var utxoIndex = _.indexBy(utxos, utxoKey);
@ -889,10 +897,11 @@ WalletService.prototype._getUtxosForCurrentWallet = function(cb) {
utxo.publicKeys = addressToPath[utxo.address].publicKeys;
});
return cb(null, utxos);
});
return next(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);
})
});
};
/**