From 1fca8fe64558a378608df1071818e1f039656e2a Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 10 Aug 2016 18:38:39 -0700 Subject: [PATCH] rpc: add listunspent. --- lib/bcoin/http/rpc.js | 107 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 106 insertions(+), 1 deletion(-) diff --git a/lib/bcoin/http/rpc.js b/lib/bcoin/http/rpc.js index c4665db9..15e2c648 100644 --- a/lib/bcoin/http/rpc.js +++ b/lib/bcoin/http/rpc.js @@ -3019,10 +3019,101 @@ RPC.prototype.listtransactions = function listtransactions(args, callback) { }; RPC.prototype.listunspent = function listunspent(args, callback) { - callback(new Error('Not implemented.')); + var self = this; + var minDepth = 1; + var maxDepth = 9999999; + var out = []; + var i, addresses, map, coin, depth, address, hash, ring; + + if (args.help || args.length > 3) { + return callback(new RPCError('listunspent' + + ' ( minconf maxconf ["address",...] )')); + } + + if (args.length > 0) + minDepth = Number(args[0]); + + if (args.length > 1) + maxDepth = Number(args[1]); + + if (!utils.isNumber(minDepth) || !utils.isNumber(maxDepth)) + return callback(new RPCError('Invalid parameter.')); + + if (Array.isArray(args[2])) { + addresses = true; + map = {}; + utils.forEachSerial(args[2], function(address, next) { + hash = bcoin.address.getHash(String(address), 'hex'); + + if (!hash) + return next(new RPCError('Invalid address.')); + + if (map[hash]) + return next(new RPCError('Duplicate address.')); + + self.wallet.getKeyring(hash, function(err, ring) { + if (err) + return callback(err); + + if (ring) + map[hash] = ring; + + next(); + }); + }, function(err) { + if (err) + return callback(err); + self.wallet.getCoins(done); + }); + } else { + this.wallet.getCoins(done); + } + + function done(err, coins) { + if (err) + return callback(err); + + for (i = 0; i < coins.length; i++) { + coin = coins[i]; + depth = coin.height !== -1 + ? self.chain.height - coin.height + 1 + : 0; + + if (!(depth > minDepth && depth < maxDepth)) + continue; + + address = coin.getAddress(); + hash = coin.getHash('hex'); + ring = hash ? map[hash] : null; + + if (addresses && !ring) + continue; + + out.push({ + txid: utils.revHex(coin.hash), + vout: coin.index, + address: address ? address.toBase58() : null, + account: ring ? ring.name : undefined, + redeemScript: ring && ring.script + ? ring.script.toJSON() + : undefined, + scriptPubKey: coin.script.toJSON(), + amount: +utils.btc(coin.value), + confirmations: depth, + spendable: true, + solvable: true + }); + } + + callback(null, out); + } }; RPC.prototype.lockunspent = function lockunspent(args, callback) { + if (args.help || args.length < 1 || args.length > 2) { + return callback(new RPCError('lockunspent' + + ' unlock ([{"txid":"txid","vout":n},...])')); + } callback(new Error('Not implemented.')); }; @@ -3206,6 +3297,20 @@ RPC.prototype.walletpassphrase = function walletpassphrase(args, callback) { }); }; +RPC.prototype.importprunedfunds = function importprunedfunds(args, callback) { + if (args.help || args.length < 2 || args.length > 3) { + return callback(new RPCError('importprunedfunds' + + ' "rawtransaction" "txoutproof" ( "label" )')); + } + callback(new Error('Not implemented.')); +}; + +RPC.prototype.removeprunedfunds = function removeprunedfunds(args, callback) { + if (args.help || args.length !== 1) + return callback(new RPCError('removeprunedfunds "txid"')); + callback(new Error('Not implemented.')); +}; + /* * Helpers */