From e48e6b0186af7c57309d2b765583816080a285da Mon Sep 17 00:00:00 2001 From: joeuhren <46763106+joeuhren@users.noreply.github.com> Date: Fri, 20 Nov 2020 14:32:27 -0700 Subject: [PATCH] Add gettx API --- app.js | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ views/info.pug | 7 +++++++ 2 files changed, 57 insertions(+) diff --git a/app.js b/app.js index f45761d..e353d35 100644 --- a/app.js +++ b/app.js @@ -85,6 +85,56 @@ app.use('/ext/getaddress/:hash', function(req,res){ }); }); +app.use('/ext/gettx/:txid', function(req, res) { + var txid = req.param('txid'); + db.get_tx(txid, function(tx) { + if (tx) { + lib.get_blockcount(function(blockcount) { + res.send({ active: 'tx', tx: tx, confirmations: settings.confirmations, blockcount: blockcount}); + }); + } + else { + lib.get_rawtransaction(txid, function(rtx) { + if (rtx.txid) { + lib.prepare_vin(rtx, function(vin) { + lib.prepare_vout(rtx.vout, rtx.txid, vin, function(rvout, rvin) { + lib.calculate_total(rvout, function(total){ + if (!rtx.confirmations > 0) { + var utx = { + txid: rtx.txid, + vin: rvin, + vout: rvout, + total: total.toFixed(8), + timestamp: rtx.time, + blockhash: '-', + blockindex: -1, + }; + res.send({ active: 'tx', tx: utx, confirmations: settings.confirmations, blockcount:-1}); + } else { + var utx = { + txid: rtx.txid, + vin: rvin, + vout: rvout, + total: total.toFixed(8), + timestamp: rtx.time, + blockhash: rtx.blockhash, + blockindex: rtx.blockheight, + }; + lib.get_blockcount(function(blockcount) { + res.send({ active: 'tx', tx: utx, confirmations: settings.confirmations, blockcount: blockcount}); + }); + } + }); + }); + }); + } else { + res.send({ error: 'tx not found.', hash: txid}); + } + }); + } + }); +}); + app.use('/ext/getbalance/:hash', function(req,res){ db.get_address(req.params.hash, false, function(address){ if (address) { diff --git a/views/info.pug b/views/info.pug index 7dc3113..3f73c60 100644 --- a/views/info.pug +++ b/views/info.pug @@ -138,6 +138,13 @@ block content div em Returns information for given address a(href='/ext/getaddress/' + hashes.address) #{address}/ext/getaddress/#{hashes.address} + li + li + p + div(style='font-weight:bold;') gettx (/ext/gettx/hash) + div + em Returns information for given tx hash + a(href='/ext/gettx/' + hashes.txhash) #{address}/ext/gettx/#{hashes.txhash} li p div(style='font-weight:bold;') getbalance (/ext/getbalance/hash)