From 85e544dc8e57e2c92bda3eb719973f73e8ad9d1b Mon Sep 17 00:00:00 2001 From: softsimon Date: Wed, 22 Jun 2022 13:15:44 +0200 Subject: [PATCH] Adding missing Block Tip Hash API --- .../src/api/bitcoin/bitcoin-api-abstract-factory.ts | 1 + backend/src/api/bitcoin/bitcoin-api.ts | 7 +++++++ backend/src/api/bitcoin/esplora-api.ts | 5 +++++ backend/src/index.ts | 1 + backend/src/routes.ts | 10 ++++++++++ 5 files changed, 24 insertions(+) diff --git a/backend/src/api/bitcoin/bitcoin-api-abstract-factory.ts b/backend/src/api/bitcoin/bitcoin-api-abstract-factory.ts index 71269da31..0c64a9d63 100644 --- a/backend/src/api/bitcoin/bitcoin-api-abstract-factory.ts +++ b/backend/src/api/bitcoin/bitcoin-api-abstract-factory.ts @@ -4,6 +4,7 @@ export interface AbstractBitcoinApi { $getRawMempool(): Promise; $getRawTransaction(txId: string, skipConversion?: boolean, addPrevout?: boolean, lazyPrevouts?: boolean): Promise; $getBlockHeightTip(): Promise; + $getBlockHashTip(): Promise; $getTxIdsForBlock(hash: string): Promise; $getBlockHash(height: number): Promise; $getBlockHeader(hash: string): Promise; diff --git a/backend/src/api/bitcoin/bitcoin-api.ts b/backend/src/api/bitcoin/bitcoin-api.ts index 41671ede1..1ff8486f4 100644 --- a/backend/src/api/bitcoin/bitcoin-api.ts +++ b/backend/src/api/bitcoin/bitcoin-api.ts @@ -64,6 +64,13 @@ class BitcoinApi implements AbstractBitcoinApi { }); } + $getBlockHashTip(): Promise { + return this.bitcoindClient.getChainTips() + .then((result: IBitcoinApi.ChainTips[]) => { + return result.find(tip => tip.status === 'active')!.hash; + }); + } + $getTxIdsForBlock(hash: string): Promise { return this.bitcoindClient.getBlock(hash, 1) .then((rpcBlock: IBitcoinApi.Block) => rpcBlock.tx); diff --git a/backend/src/api/bitcoin/esplora-api.ts b/backend/src/api/bitcoin/esplora-api.ts index d92bba1bf..fbed1c429 100644 --- a/backend/src/api/bitcoin/esplora-api.ts +++ b/backend/src/api/bitcoin/esplora-api.ts @@ -25,6 +25,11 @@ class ElectrsApi implements AbstractBitcoinApi { .then((response) => response.data); } + $getBlockHashTip(): Promise { + return axios.get(config.ESPLORA.REST_API_URL + '/blocks/tip/hash', this.axiosConfig) + .then((response) => response.data); + } + $getTxIdsForBlock(hash: string): Promise { return axios.get(config.ESPLORA.REST_API_URL + '/block/' + hash + '/txids', this.axiosConfig) .then((response) => response.data); diff --git a/backend/src/index.ts b/backend/src/index.ts index 6bd8de841..c61a45c69 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -329,6 +329,7 @@ class Server { .get(config.MEMPOOL.API_URL_PREFIX + 'tx/:txId/outspends', routes.getTransactionOutspends) .get(config.MEMPOOL.API_URL_PREFIX + 'block/:hash/header', routes.getBlockHeader) .get(config.MEMPOOL.API_URL_PREFIX + 'blocks/tip/height', routes.getBlockTipHeight) + .get(config.MEMPOOL.API_URL_PREFIX + 'blocks/tip/hash', routes.getBlockTipHash) .get(config.MEMPOOL.API_URL_PREFIX + 'block/:hash/txs', routes.getBlockTransactions) .get(config.MEMPOOL.API_URL_PREFIX + 'block/:hash/txs/:index', routes.getBlockTransactions) .get(config.MEMPOOL.API_URL_PREFIX + 'block/:hash/txids', routes.getTxIdsForBlock) diff --git a/backend/src/routes.ts b/backend/src/routes.ts index 99f54a9f8..e8db4e25d 100644 --- a/backend/src/routes.ts +++ b/backend/src/routes.ts @@ -901,6 +901,16 @@ class Routes { } } + public async getBlockTipHash(req: Request, res: Response) { + try { + const result = await bitcoinApi.$getBlockHashTip(); + res.setHeader('content-type', 'text/plain'); + res.send(result); + } catch (e) { + res.status(500).send(e instanceof Error ? e.message : e); + } + } + public async getTxIdsForBlock(req: Request, res: Response) { try { const result = await bitcoinApi.$getTxIdsForBlock(req.params.hash);