rpc: refactor listunspent.

This commit is contained in:
Christopher Jeffrey 2016-08-11 03:04:47 -07:00
parent e910147544
commit e098fdeca4
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -3365,8 +3365,7 @@ RPC.prototype.listunspent = function listunspent(args, callback) {
var minDepth = 1;
var maxDepth = 9999999;
var out = [];
var map = {};
var i, addresses, coin, depth, address, hash, ring;
var i, addresses, addrs, depth, address, hash;
if (args.help || args.length > 3) {
return callback(new RPCError('listunspent'
@ -3379,76 +3378,75 @@ RPC.prototype.listunspent = function listunspent(args, callback) {
if (args.length > 1)
maxDepth = Number(args[1]);
if (args.length > 2)
addrs = args[2];
if (!utils.isNumber(minDepth) || !utils.isNumber(maxDepth))
return callback(new RPCError('Invalid parameter.'));
if (Array.isArray(args[2])) {
addresses = true;
utils.forEachSerial(args[2], function(address, next) {
hash = bcoin.address.getHash(String(address), 'hex');
if (Array.isArray(addrs)) {
addresses = {};
for (i = 0; i < addrs.length; i++) {
address = String(addrs[i]);
hash = bcoin.address.getHash(address, 'hex');
if (!hash)
return next(new RPCError('Invalid address.'));
return callback(new RPCError('Invalid address.'));
if (map[hash])
return next(new RPCError('Duplicate address.'));
if (addresses[hash])
return callback(new RPCError('Duplicate address.'));
addresses[hash] = true;
}
}
this.wallet.getCoins(function(err, coins) {
if (err)
return callback(err);
utils.forEachSerial(coins, function(coin, next) {
depth = coin.height !== -1
? self.chain.height - coin.height + 1
: 0;
if (!(depth > minDepth && depth < maxDepth))
return next();
address = coin.getAddress();
hash = coin.getHash('hex');
if (addresses) {
if (!hash || !addresses[hash])
return next();
}
self.wallet.getKeyring(hash, function(err, ring) {
if (err)
return callback(err);
return next(err);
if (ring)
map[hash] = ring;
out.push({
txid: utils.revHex(coin.hash),
vout: coin.index,
address: address ? address.toBase58(self.network) : 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
});
next();
});
}, function(err) {
if (err)
return callback(err);
self.wallet.getCoins(done);
callback(null, out);
});
} 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(self.network) : 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) {