From 0a217e7be2b6400dcfdf90b82ae89887228a0a06 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 5 Aug 2016 14:02:10 -0700 Subject: [PATCH] rpc: add wallet txs. --- lib/bcoin/http/rpc.js | 130 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 127 insertions(+), 3 deletions(-) diff --git a/lib/bcoin/http/rpc.js b/lib/bcoin/http/rpc.js index 784bb2b4..41ae18c3 100644 --- a/lib/bcoin/http/rpc.js +++ b/lib/bcoin/http/rpc.js @@ -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) {