From 6ae05c202386021f6374e3fb18d675321927f9c8 Mon Sep 17 00:00:00 2001 From: Leonardo Lima Date: Mon, 25 Jul 2022 14:07:45 -0300 Subject: [PATCH 1/4] chore: accept the CLA for @oleonardolima --- contributors/oleonardolima.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 contributors/oleonardolima.txt diff --git a/contributors/oleonardolima.txt b/contributors/oleonardolima.txt new file mode 100644 index 000000000..79adbcb78 --- /dev/null +++ b/contributors/oleonardolima.txt @@ -0,0 +1,3 @@ +I hereby accept the terms of the Contributor License Agreement in the CONTRIBUTING.md file of the mempool/mempool git repository as of July 25, 2022. + +Signed: oleonardolima From 46e63ca6cf0d83b265727c427cdd20ea96dd0d63 Mon Sep 17 00:00:00 2001 From: Leonardo Lima Date: Mon, 25 Jul 2022 14:54:00 -0300 Subject: [PATCH 2/4] feat: add /block/:hash/raw api route --- .../src/api/bitcoin/bitcoin-api-abstract-factory.ts | 1 + backend/src/api/bitcoin/bitcoin.routes.ts | 13 ++++++++++++- backend/src/api/bitcoin/esplora-api.ts | 5 +++++ frontend/src/app/docs/api-docs/api-docs-data.ts | 2 +- 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/backend/src/api/bitcoin/bitcoin-api-abstract-factory.ts b/backend/src/api/bitcoin/bitcoin-api-abstract-factory.ts index f3f7011dd..358bd29e4 100644 --- a/backend/src/api/bitcoin/bitcoin-api-abstract-factory.ts +++ b/backend/src/api/bitcoin/bitcoin-api-abstract-factory.ts @@ -9,6 +9,7 @@ export interface AbstractBitcoinApi { $getBlockHash(height: number): Promise; $getBlockHeader(hash: string): Promise; $getBlock(hash: string): Promise; + $getRawBlock(hash: string): Promise; $getAddress(address: string): Promise; $getAddressTransactions(address: string, lastSeenTxId: string): Promise; $getAddressPrefix(prefix: string): string[]; diff --git a/backend/src/api/bitcoin/bitcoin.routes.ts b/backend/src/api/bitcoin/bitcoin.routes.ts index a04a78117..ffdcd0c26 100644 --- a/backend/src/api/bitcoin/bitcoin.routes.ts +++ b/backend/src/api/bitcoin/bitcoin.routes.ts @@ -103,9 +103,10 @@ class BitcoinRoutes { .get(config.MEMPOOL.API_URL_PREFIX + 'block/:hash/header', this.getBlockHeader) .get(config.MEMPOOL.API_URL_PREFIX + 'blocks/tip/height', this.getBlockTipHeight) .get(config.MEMPOOL.API_URL_PREFIX + 'blocks/tip/hash', this.getBlockTipHash) + .get(config.MEMPOOL.API_URL_PREFIX + 'block/:hash/raw', this.getRawBlock) + .get(config.MEMPOOL.API_URL_PREFIX + 'block/:hash/txids', this.getTxIdsForBlock) .get(config.MEMPOOL.API_URL_PREFIX + 'block/:hash/txs', this.getBlockTransactions) .get(config.MEMPOOL.API_URL_PREFIX + 'block/:hash/txs/:index', this.getBlockTransactions) - .get(config.MEMPOOL.API_URL_PREFIX + 'block/:hash/txids', this.getTxIdsForBlock) .get(config.MEMPOOL.API_URL_PREFIX + 'block-height/:height', this.getBlockHeight) .get(config.MEMPOOL.API_URL_PREFIX + 'address/:address', this.getAddress) .get(config.MEMPOOL.API_URL_PREFIX + 'address/:address/txs', this.getAddressTransactions) @@ -470,6 +471,16 @@ class BitcoinRoutes { } } + private async getRawBlock(req: Request, res: Response) { + try { + const result = await bitcoinApi.$getRawBlock(req.params.hash); + res.setHeader('content-type', 'text/plain'); + res.send(result); + } catch (e) { + res.status(500).send(e instanceof Error ? e.message : e); + } + } + private async getTxIdsForBlock(req: Request, res: Response) { try { const result = await bitcoinApi.$getTxIdsForBlock(req.params.hash); diff --git a/backend/src/api/bitcoin/esplora-api.ts b/backend/src/api/bitcoin/esplora-api.ts index e8eee343a..ebaf2f6a0 100644 --- a/backend/src/api/bitcoin/esplora-api.ts +++ b/backend/src/api/bitcoin/esplora-api.ts @@ -50,6 +50,11 @@ class ElectrsApi implements AbstractBitcoinApi { .then((response) => response.data); } + $getRawBlock(hash: string): Promise { + return axios.get(config.ESPLORA.REST_API_URL + '/block/' + hash + "/raw", this.axiosConfig) + .then((response) => response.data); + } + $getAddress(address: string): Promise { throw new Error('Method getAddress not implemented.'); } diff --git a/frontend/src/app/docs/api-docs/api-docs-data.ts b/frontend/src/app/docs/api-docs/api-docs-data.ts index f8f0e23b8..80aab5f15 100644 --- a/frontend/src/app/docs/api-docs/api-docs-data.ts +++ b/frontend/src/app/docs/api-docs/api-docs-data.ts @@ -2070,7 +2070,7 @@ export const restApiDocsData = [ fragment: "get-block-raw", title: "GET Block Raw", description: { - default: "Returns the raw block representation in binary." + default: "Returns the raw block representation in binary for Esplora backend, or hex for Bitcoin Core RPC backend." }, urlString: "/block/:hash/raw", showConditions: bitcoinNetworks.concat(liquidNetworks), From a9248a5f136e1457f673cd9c2c42304124e3df3d Mon Sep 17 00:00:00 2001 From: Leonardo Lima Date: Wed, 27 Jul 2022 17:12:33 -0300 Subject: [PATCH 3/4] feat: parse rpc full block from hex to binary representation --- backend/src/api/bitcoin/bitcoin-api.ts | 3 ++- frontend/src/app/docs/api-docs/api-docs-data.ts | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/backend/src/api/bitcoin/bitcoin-api.ts b/backend/src/api/bitcoin/bitcoin-api.ts index 3152954c1..ebde5cc07 100644 --- a/backend/src/api/bitcoin/bitcoin-api.ts +++ b/backend/src/api/bitcoin/bitcoin-api.ts @@ -77,7 +77,8 @@ class BitcoinApi implements AbstractBitcoinApi { } $getRawBlock(hash: string): Promise { - return this.bitcoindClient.getBlock(hash, 0); + return this.bitcoindClient.getBlock(hash, 0) + .then((raw: string) => Buffer.from(raw, "hex")); } $getBlockHash(height: number): Promise { diff --git a/frontend/src/app/docs/api-docs/api-docs-data.ts b/frontend/src/app/docs/api-docs/api-docs-data.ts index 80aab5f15..f8f0e23b8 100644 --- a/frontend/src/app/docs/api-docs/api-docs-data.ts +++ b/frontend/src/app/docs/api-docs/api-docs-data.ts @@ -2070,7 +2070,7 @@ export const restApiDocsData = [ fragment: "get-block-raw", title: "GET Block Raw", description: { - default: "Returns the raw block representation in binary for Esplora backend, or hex for Bitcoin Core RPC backend." + default: "Returns the raw block representation in binary." }, urlString: "/block/:hash/raw", showConditions: bitcoinNetworks.concat(liquidNetworks), From b0fe87950318d8aadf2dd6a5e49c11994c67a3fd Mon Sep 17 00:00:00 2001 From: softsimon Date: Wed, 27 Jul 2022 23:33:18 +0200 Subject: [PATCH 4/4] Update backend/src/api/bitcoin/bitcoin.routes.ts --- backend/src/api/bitcoin/bitcoin.routes.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/api/bitcoin/bitcoin.routes.ts b/backend/src/api/bitcoin/bitcoin.routes.ts index ffdcd0c26..66bcb2569 100644 --- a/backend/src/api/bitcoin/bitcoin.routes.ts +++ b/backend/src/api/bitcoin/bitcoin.routes.ts @@ -474,7 +474,7 @@ class BitcoinRoutes { private async getRawBlock(req: Request, res: Response) { try { const result = await bitcoinApi.$getRawBlock(req.params.hash); - res.setHeader('content-type', 'text/plain'); + res.setHeader('content-type', 'application/octet-stream'); res.send(result); } catch (e) { res.status(500).send(e instanceof Error ? e.message : e);