Skip to content

Commit

Permalink
add support for getting block by hash
Browse files Browse the repository at this point in the history
  • Loading branch information
Cifko committed Nov 10, 2023
1 parent 83c65bb commit bd50b0e
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions routes/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,34 @@ var { createClient } = require("../baseNodeClient");
var express = require("express");
var router = express.Router();

router.get("/:height", async function (req, res) {
function fromHexString(hexString) {
let res = [];
for (let i = 0; i < hexString.length; i += 2) {
res.push(Number("0x" + hexString.substring(i, i + 2)));
}
return res;
}


router.get("/:height_or_hash", async function (req, res) {
try {
let client = createClient();
let height = parseInt(req.params.height);
let block = await client.getBlocks({ heights: [height] });
let height_or_hash = req.params.height_or_hash;
let block;
let height;
if (height_or_hash.length === 64) {
block = await client.getHeaderByHash({ hash: fromHexString(height_or_hash) });
if (!block) {
res.status(404);
res.render("404", { message: `Block with hash ${height_or_hash} not found` });
return;
}
height = parseInt(block.header.height);
} else {
height = parseInt(height_or_hash);
}

block = await client.getBlocks({ heights: [height] });
if (!block || block.length === 0) {
res.status(404);
res.render("404", { message: `Block at height ${height} not found` });
Expand Down

0 comments on commit bd50b0e

Please sign in to comment.