diff --git a/logic/bitcoind.js b/logic/bitcoind.js index 2641b85..d8f1308 100644 --- a/logic/bitcoind.js +++ b/logic/bitcoind.js @@ -103,20 +103,6 @@ async function getVersion() { return { version: version }; // eslint-disable-line object-shorthand } -async function getBlock(hash) { - const blockObj = await bitcoindService.getBlock(hash); - return { - block: hash, - confirmations: blockObj.result.confirmations, - size: blockObj.result.size, - height: blockObj.result.height, - blocktime: blockObj.result.time, - prevblock: blockObj.result.previousblockhash, - nextblock: blockObj.result.nextblockhash, - transactions: blockObj.result.tx - } -} - async function getTransaction(txid) { const transactionObj = await bitcoindService.getTransaction(txid); return { @@ -137,6 +123,53 @@ async function getNetworkInfo() { return networkInfo.result; // eslint-disable-line object-shorthand } +async function getBlock(hash) { + const blockObj = await bitcoindService.getBlock(hash); + return { + block: hash, + confirmations: blockObj.result.confirmations, + size: blockObj.result.size, + height: blockObj.result.height, + blocktime: blockObj.result.time, + prevblock: blockObj.result.previousblockhash, + nextblock: blockObj.result.nextblockhash, + transactions: blockObj.result.tx + } +} + +async function getBlocks(fromHeight, toHeight) { + + const startingBlockHashObj = await bitcoindService.getBlockHash(toHeight); + + let currentHeight = toHeight; + let currentHash = startingBlockHashObj.result; + + const blocks = []; + //loop until we reach "toHeight" + + for (let currentHeight = toHeight; currentHeight >= fromHeight; currentHeight--) { + console.log(currentHeight); + const blockObj = await bitcoindService.getBlock(currentHash); + const block = blockObj.result; + currentHash = block.previousblockhash; + const formattedBlock = { + hash: block.hash, + height: block.height, + numTransactions: block.tx.length, + confirmations: block.confirmations, + time: block.time, + size: block.size + }; + blocks.push(formattedBlock); + //terminate loop if we reach the genesis block + if (!currentHash) { + break; + } + } + + return { blocks: blocks }; // eslint-disable-line object-shorthand +} + async function getBlockHash(height) { const getBlockHashObj = await bitcoindService.getBlockHash(height); @@ -179,6 +212,7 @@ module.exports = { getTransaction, getBlock, getBlockCount, + getBlocks, getConnectionsCount, getNetworkInfo, getMempoolInfo, diff --git a/routes/v1/bitcoind/info.js b/routes/v1/bitcoind/info.js index 7084957..a9f439d 100644 --- a/routes/v1/bitcoind/info.js +++ b/routes/v1/bitcoind/info.js @@ -67,6 +67,14 @@ router.get('/block/:id', auth.jwt, safeHandler((req, res) => .then(blockhash => res.json(blockhash)) )); +router.get('/blocks', auth.jwt, safeHandler((req, res) => { + const fromHeight = parseInt(req.query.from); + const toHeight = parseInt(req.query.to); + bitcoind.getBlocks(fromHeight, toHeight) + .then(blocks => res.json(blocks)) +} +)); + router.get('/txid/:id', auth.jwt, safeHandler((req, res) => bitcoind.getTransaction(req.params.id) .then(txhash => res.json(txhash)) diff --git a/services/bitcoind.js b/services/bitcoind.js index 02dc29c..a22640f 100644 --- a/services/bitcoind.js +++ b/services/bitcoind.js @@ -64,6 +64,10 @@ function promiseifyParamTwo(rpcObj, rpcFn, param1, param2, what) { }); } +function getBestBlockHash() { + return promiseify(rpcClient, rpcClient.getBestBlockHash, 'best block hash'); +} + function getBlockHash(height) { return promiseifyParam(rpcClient, rpcClient.getBlockHash, height, 'block height'); } @@ -113,6 +117,7 @@ function help() { module.exports = { getMiningInfo, + getBestBlockHash, getBlockHash, getBlock, getTransaction,