rpc: add listunspent.

This commit is contained in:
Christopher Jeffrey 2016-08-10 18:38:39 -07:00
parent 295e0d74bd
commit 1fca8fe645
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -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
*/