|
|
@ -154,7 +154,6 @@ WalletService.getInstance = function(opts) { |
|
|
|
* @param {string} opts.clientVersion - A string that identifies the client issuing the request |
|
|
|
*/ |
|
|
|
WalletService.getInstanceWithAuth = function(opts, cb) { |
|
|
|
|
|
|
|
if (!Utils.checkRequired(opts, ['copayerId', 'message', 'signature'])) |
|
|
|
return cb(new ClientError('Required argument missing')); |
|
|
|
|
|
|
@ -618,10 +617,30 @@ WalletService.prototype._getBlockchainExplorer = function(network) { |
|
|
|
return this.blockchainExplorer; |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* Returns list of UTXOs |
|
|
|
*/ |
|
|
|
WalletService.prototype.getUtxos = function(cb) { |
|
|
|
WalletService.prototype._getUtxosForAddresses = function(addresses, cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
if (addresses.length == 0) return cb(null, []); |
|
|
|
var networkName = Bitcore.Address(addresses[0]).toObject().network; |
|
|
|
|
|
|
|
var bc = self._getBlockchainExplorer(networkName); |
|
|
|
bc.getUnspentUtxos(addresses, function(err, utxos) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
var utxos = _.map(utxos, function(utxo) { |
|
|
|
var u = _.pick(utxo, ['txid', 'vout', 'address', 'scriptPubKey', 'amount', 'satoshis', 'confirmations']); |
|
|
|
u.confirmations = u.confirmations || 0; |
|
|
|
u.locked = false; |
|
|
|
u.satoshis = u.satoshis ? +u.satoshis : Utils.strip(u.amount * 1e8); |
|
|
|
delete u.amount; |
|
|
|
return u; |
|
|
|
}); |
|
|
|
|
|
|
|
return cb(null, utxos); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
WalletService.prototype._getUtxosForCurrentWallet = function(cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
function utxoKey(utxo) { |
|
|
@ -631,29 +650,17 @@ WalletService.prototype.getUtxos = function(cb) { |
|
|
|
// Get addresses for this wallet
|
|
|
|
self.storage.fetchAddresses(self.walletId, function(err, addresses) { |
|
|
|
if (err) return cb(err); |
|
|
|
if (addresses.length == 0) return cb(null, []); |
|
|
|
|
|
|
|
var addressStrs = _.pluck(addresses, 'address'); |
|
|
|
var addressToPath = _.indexBy(addresses, 'address'); // TODO : check performance
|
|
|
|
var networkName = Bitcore.Address(addressStrs[0]).toObject().network; |
|
|
|
|
|
|
|
var bc = self._getBlockchainExplorer(networkName); |
|
|
|
bc.getUnspentUtxos(addressStrs, function(err, inutxos) { |
|
|
|
self._getUtxosForAddresses(addressStrs, function(err, utxos) { |
|
|
|
if (err) return cb(err); |
|
|
|
if (utxos.length == 0) return cb(null, []); |
|
|
|
|
|
|
|
var utxos = _.map(inutxos, function(utxo) { |
|
|
|
var u = _.pick(utxo, ['txid', 'vout', 'address', 'scriptPubKey', 'amount', 'satoshis', 'confirmations']); |
|
|
|
u.confirmations = u.confirmations || 0; |
|
|
|
u.locked = false; |
|
|
|
return u; |
|
|
|
}); |
|
|
|
self.getPendingTxs({}, function(err, txps) { |
|
|
|
if (err) return cb(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; |
|
|
@ -661,9 +668,8 @@ WalletService.prototype.getUtxos = function(cb) { |
|
|
|
}); |
|
|
|
|
|
|
|
// Needed for the clients to sign UTXOs
|
|
|
|
var addressToPath = _.indexBy(addresses, 'address'); |
|
|
|
_.each(utxos, function(utxo) { |
|
|
|
utxo.satoshis = utxo.satoshis ? +utxo.satoshis : Utils.strip(utxo.amount * 1e8); |
|
|
|
delete utxo.amount; |
|
|
|
utxo.path = addressToPath[utxo.address].path; |
|
|
|
utxo.publicKeys = addressToPath[utxo.address].publicKeys; |
|
|
|
}); |
|
|
@ -674,6 +680,24 @@ WalletService.prototype.getUtxos = function(cb) { |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* Returns list of UTXOs |
|
|
|
* @param {Object} opts |
|
|
|
* @param {Array} opts.addresses (optional) - List of addresses from where to fetch UTXOs. |
|
|
|
* @returns {Array} utxos - List of UTXOs. |
|
|
|
*/ |
|
|
|
WalletService.prototype.getUtxos = function(opts, cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
opts = opts || {}; |
|
|
|
|
|
|
|
if (_.isUndefined(opts.addresses)) { |
|
|
|
self._getUtxosForCurrentWallet(cb); |
|
|
|
} else { |
|
|
|
self._getUtxosForAddresses(opts.addresses, cb); |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
WalletService.prototype._totalizeUtxos = function(utxos) { |
|
|
|
var balance = { |
|
|
|
totalAmount: _.sum(utxos, 'satoshis'), |
|
|
@ -718,7 +742,7 @@ WalletService.prototype._computeBytesToSendMax = function(utxos, cb) { |
|
|
|
WalletService.prototype.getBalance = function(opts, cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
self.getUtxos(function(err, utxos) { |
|
|
|
self.getUtxos({}, function(err, utxos) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
var balance = self._totalizeUtxos(utxos); |
|
|
@ -828,7 +852,7 @@ WalletService.prototype._selectTxInputs = function(txp, cb) { |
|
|
|
return _.pluck(_.sortBy(list, 'order'), 'utxo'); |
|
|
|
}; |
|
|
|
|
|
|
|
self.getUtxos(function(err, utxos) { |
|
|
|
self.getUtxos({}, function(err, utxos) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
var totalAmount; |
|
|
|