rpc: add wallet txs.

This commit is contained in:
Christopher Jeffrey 2016-08-05 14:02:10 -07:00
parent ce7f300260
commit 0a217e7be2
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -2378,6 +2378,47 @@ RPC.prototype.getreceivedbyaddress = function getreceivedbyaddress(args, callbac
});
};
RPC.prototype._toWalletTX = function _toWalletTX(tx, callback) {
var self = this;
var receive, member, json;
this.walletdb.tx.getMap(tx, function(err, map) {
if (err)
return callback(err);
if (!map)
return callback(new Error('TX not found.'));
receive = map.inputs.length === 0;
member = receive ? map.outputs[0] : map.inputs[0];
assert(member);
json = {
amount: +utils.btc(tx.getOutputValue()),
confirmations: tx.getConfirmations(),
blockhash: tx.block ? utils.revHex(tx.block) : null,
blockindex: tx.index,
blocktime: tx.ts,
txid: tx.rhash,
walletconflicts: [],
time: tx.ps,
timereceived: tx.ps,
'bip125-replaceable': 'no',
details: [{
account: member.name,
address: member.address.toBase58(),
category: receive ? 'receive' : 'send',
amount: +utils.btc(member.value),
label: member.name,
vout: 0
}],
hex: tx.toRaw().toString('hex')
};
callback(null, json);
});
};
RPC.prototype.gettransaction = function gettransaction(args, callback) {
var hash;
@ -2391,8 +2432,15 @@ RPC.prototype.gettransaction = function gettransaction(args, callback) {
hash = utils.revHex(hash);
// XXX TODO
callback(new Error('Not implemented.'));
this.walletdb.getTX(hash, function(err, tx) {
if (err)
return callback(err);
if (!tx)
return callback(new Error('TX not found.'));
self._toWalletTX(tx, callback);
});
};
RPC.prototype.abandontransaction = function abandontransaction(args, callback) {
@ -2531,8 +2579,84 @@ RPC.prototype.listsinceblock = function listsinceblock(args, callback) {
callback(new Error('Not implemented.'));
};
RPC.prototype._toListTX = function _toListTX(tx, callback) {
var self = this;
var receive, member, json;
this.walletdb.tx.getMap(tx, function(err, map) {
if (err)
return callback(err);
if (!map)
return callback(new Error('TX not found.'));
receive = map.inputs.length === 0;
member = receive ? map.outputs[0] : map.inputs[0];
assert(member);
json = {
account: member.name,
address: member.address.toBase58(),
category: receive ? 'receive' : 'send',
amount: +utils.btc(member.value),
label: member.name,
vout: 0,
confirmations: tx.getConfirmations(),
blockhash: tx.block ? utils.revHex(tx.block) : null,
blockindex: tx.index,
blocktime: tx.ts,
txid: tx.rhash,
walletconflicts: [],
time: tx.ps,
timereceived: tx.ps,
'bip125-replaceable': 'no'
};
callback(null, json);
});
};
RPC.prototype.listtransactions = function listtransactions(args, callback) {
callback(new Error('Not implemented.'));
var account, count;
if (args.help || args.length > 4) {
return callback(new Error('listtransactions'
+ ' ( "account" count from includeWatchonly)'));
}
account = null;
if (args.length > 0) {
account = String(args[0]);
if (!account)
account = 'default';
}
count = 10;
if (args.length > 1)
count = Number(args[1]);
if (!utils.isNumber(count) || count < 0)
count = 10;
this.wallet.getHistory(account, function(err, txs) {
if (err)
return callback(err);
utils.forEachSerial(txs, function(tx, next, i) {
self._toListTX(tx, function(err, json) {
if (err)
return next(err);
txs[i] = json;
next();
});
}, function(err) {
if (err)
return callback(err);
callback(null, txs);
});
});
};
RPC.prototype.listunspent = function listunspent(args, callback) {