From 07f3f8f706c96f06ce84cc8df7fe78ee869335c5 Mon Sep 17 00:00:00 2001 From: Dan Janosik Date: Sat, 22 Jul 2017 15:13:04 -0400 Subject: [PATCH] search support for block heights, block hashes, and txids --- app/rpcApi.js | 17 +++++++++++++ routes/baseActionsRouter.js | 48 +++++++++++++++++++++++++++++++++++++ views/layout.pug | 7 ++++-- 3 files changed, 70 insertions(+), 2 deletions(-) diff --git a/app/rpcApi.js b/app/rpcApi.js index 6c4b39c..58fa515 100644 --- a/app/rpcApi.js +++ b/app/rpcApi.js @@ -108,6 +108,22 @@ function getBlocksByHeight(blockHeights) { }); } +function getBlockByHash(blockHash) { + console.log("getBlockByHash: " + blockHash); + + return new Promise(function(resolve, reject) { + var client = global.client; + + client.cmd('getblock', blockHash, function(err, result, resHeaders) { + if (err) { + console.log("Error 0u2fgewue: " + err); + } + + resolve(result); + }); + }); +} + function getTransactionInputs(rpcClient, transaction) { console.log("getTransactionInputs: " + transaction.txid); @@ -246,6 +262,7 @@ module.exports = { getInfo: getInfo, getBlockByHeight: getBlockByHeight, getBlocksByHeight: getBlocksByHeight, + getBlockByHash: getBlockByHash, getTransactionInputs: getTransactionInputs, getBlockData: getBlockData, getRawTransaction: getRawTransaction, diff --git a/routes/baseActionsRouter.js b/routes/baseActionsRouter.js index b04835a..b3c22e3 100644 --- a/routes/baseActionsRouter.js +++ b/routes/baseActionsRouter.js @@ -125,6 +125,54 @@ router.get("/blocks", function(req, res) { }); }); +router.post("/search", function(req, res) { + if (!req.body.query) { + req.session.userMessage = "Enter a block height, block hash, or transaction id."; + + res.redirect("/"); + + return; + } + + var query = req.body.query; + + rpcApi.getRawTransaction(query).then(function(tx) { + if (tx) { + res.redirect("/tx/" + query); + + return; + } + + rpcApi.getBlockByHash(query).then(function(blockByHash) { + if (blockByHash) { + res.redirect("/block/" + query); + + return; + } + + if (isNaN(query)) { + req.session.userMessage = "No results found for query: " + query; + + res.redirect("/"); + + return; + } + + rpcApi.getBlockByHeight(parseInt(query)).then(function(blockByHeight) { + if (blockByHeight) { + res.redirect("/block-height/" + query); + + return; + } + + req.session.userMessage = "No results found for query: " + query; + + res.redirect("/"); + }); + }); + }); +}); + router.get("/block-height/:blockHeight", function(req, res) { var client = global.client; diff --git a/views/layout.pug b/views/layout.pug index a236a92..c734663 100644 --- a/views/layout.pug +++ b/views/layout.pug @@ -30,10 +30,13 @@ html span BTC RPC Explorer div(class="collapse navbar-collapse", id="navbarNav") if (client) - ul(class="navbar-nav") + ul(class="navbar-nav mr-auto") li(class="nav-item") a(href="/terminal", class="nav-link") RPC Terminal - + form(method="post", action="/search", class="form-inline") + input(class="form-control form-control-sm mr-sm-2", type="text", name="query", placeholder="block height, block hash, txid", style="width: 300px;") + button(class="btn btn-primary btn-sm", type="submit") Search + div(class="container") if (userMessage) div(class="alert", class=(userMessageType ? ("alert-" + userMessageType) : "alert-info"), role="alert")