From 3f558f6acee4ce940df2a2579f10e6239b74769a Mon Sep 17 00:00:00 2001 From: Chris Kleeschulte Date: Mon, 16 Jan 2017 19:23:17 -0500 Subject: [PATCH] Added balance by address. --- lib/services/wallet-api/index.js | 39 +++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/lib/services/wallet-api/index.js b/lib/services/wallet-api/index.js index 1da0615f..457964d7 100644 --- a/lib/services/wallet-api/index.js +++ b/lib/services/wallet-api/index.js @@ -87,6 +87,7 @@ WalletService.prototype._endpointGetBalance= function() { return function(req, res) { var walletId = req.params.walletId; var queryMempool = req.query.queryMempool === false ? false : true; + var byAddress = req.query.byAddress; //var tip = self.node.bitcoind.tip; // TODO: get the height of the tip @@ -94,17 +95,15 @@ WalletService.prototype._endpointGetBalance= function() { var height = null; var options = { - queryMempool: queryMempool + queryMempool: queryMempool, + byAddress: byAddress }; - self._getBalance(walletId, height, options, function(err, balance) { + self._getBalance(walletId, height, options, function(err, result) { if(err) { return utils.sendError(err); } - res.status(200).jsonp({ - balance: balance, - height: height - }); + res.status(200).jsonp(result); }); }; }; @@ -227,20 +226,44 @@ WalletService.prototype._getBalance = function(walletId, height, options, callba return callback(err); } - self.node.services.bitcoind.getAddressUnspentOutputs(addresses, options, function(err, utxos) { if(err) { return callback(err); } + if (options.byAddress) { + var result = self._getBalanceByAddress(addresses, utxos); + return callback(null, { + addresses: result, + height: null + }); + } var balance = 0; utxos.forEach(function(utxo) { balance += utxo.satoshis; }); - callback(null, balance); + callback(null, { + balance: balance, + height: null + }); }); }); }; +WalletService.prototype._getBalanceByAddress = function(addresses, utxos) { + var res = {}; + utxos.forEach(function(utxo) { + if (res[utxo.address]) { + res[utxo.address] += utxo.satoshis; + } else { + res[utxo.address] = utxo.satoshis; + } + }); + addresses.forEach(function(address) { + res[address] = res[address] || 0; + }); + return res; +}; + WalletService.prototype._chunkAdresses = function(addresses) { var maxLength = this.node.services.bitcoind.maxAddressesQuery; var groups = [];