From 86c877c8e968c45fad740d44682615b26526eac8 Mon Sep 17 00:00:00 2001 From: softsimon Date: Sun, 26 Sep 2021 22:18:44 +0400 Subject: [PATCH 1/2] Adding POST /tx API to bitcoind mode fixes #777 --- .../src/api/bitcoin/bitcoin-api-abstract-factory.ts | 1 + backend/src/api/bitcoin/bitcoin-api.ts | 4 ++++ backend/src/api/bitcoin/esplora-api.ts | 4 ++++ backend/src/index.ts | 1 + backend/src/routes.ts | 12 ++++++++++++ 5 files changed, 22 insertions(+) diff --git a/backend/src/api/bitcoin/bitcoin-api-abstract-factory.ts b/backend/src/api/bitcoin/bitcoin-api-abstract-factory.ts index 72c7150cc..5201d47a5 100644 --- a/backend/src/api/bitcoin/bitcoin-api-abstract-factory.ts +++ b/backend/src/api/bitcoin/bitcoin-api-abstract-factory.ts @@ -11,6 +11,7 @@ export interface AbstractBitcoinApi { $getAddress(address: string): Promise; $getAddressTransactions(address: string, lastSeenTxId: string): Promise; $getAddressPrefix(prefix: string): string[]; + $sendRawTransaction(rawTransaction: string): Promise; } export interface BitcoinRpcCredentials { host: string; diff --git a/backend/src/api/bitcoin/bitcoin-api.ts b/backend/src/api/bitcoin/bitcoin-api.ts index 4ad5d5cd5..887a8c2ce 100644 --- a/backend/src/api/bitcoin/bitcoin-api.ts +++ b/backend/src/api/bitcoin/bitcoin-api.ts @@ -98,6 +98,10 @@ class BitcoinApi implements AbstractBitcoinApi { return found; } + $sendRawTransaction(rawTransaction: string): Promise { + return this.bitcoindClient.sendRawTransaction(rawTransaction); + } + protected async $convertTransaction(transaction: IBitcoinApi.Transaction, addPrevout: boolean): Promise { let esploraTransaction: IEsploraApi.Transaction = { txid: transaction.txid, diff --git a/backend/src/api/bitcoin/esplora-api.ts b/backend/src/api/bitcoin/esplora-api.ts index 86d4179ad..142815c02 100644 --- a/backend/src/api/bitcoin/esplora-api.ts +++ b/backend/src/api/bitcoin/esplora-api.ts @@ -56,6 +56,10 @@ class ElectrsApi implements AbstractBitcoinApi { $getAddressPrefix(prefix: string): string[] { throw new Error('Method not implemented.'); } + + $sendRawTransaction(rawTransaction: string): Promise { + throw new Error('Method not implemented.'); + } } export default ElectrsApi; diff --git a/backend/src/index.ts b/backend/src/index.ts index cfca7cc65..e65a18ab6 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -246,6 +246,7 @@ class Server { .get(config.MEMPOOL.API_URL_PREFIX + 'mempool/txids', routes.getMempoolTxIds) .get(config.MEMPOOL.API_URL_PREFIX + 'mempool/recent', routes.getRecentMempoolTransactions) .get(config.MEMPOOL.API_URL_PREFIX + 'tx/:txId', routes.getTransaction) + .post(config.MEMPOOL.API_URL_PREFIX + 'tx', routes.$postTransaction) .get(config.MEMPOOL.API_URL_PREFIX + 'tx/:txId/hex', routes.getRawTransaction) .get(config.MEMPOOL.API_URL_PREFIX + 'tx/:txId/status', routes.getTransactionStatus) .get(config.MEMPOOL.API_URL_PREFIX + 'tx/:txId/outspends', routes.getTransactionOutspends) diff --git a/backend/src/routes.ts b/backend/src/routes.ts index 8db3e4b89..b1f9a2b15 100644 --- a/backend/src/routes.ts +++ b/backend/src/routes.ts @@ -764,6 +764,18 @@ class Routes { res.status(500).send(e instanceof Error ? e.message : e); } } + + public async $postTransaction(req: Request, res: Response) { + res.setHeader('content-type', 'text/plain'); + try { + const rawtx = Object.keys(req.body)[0]; + const txIdResult = await bitcoinApi.$sendRawTransaction(rawtx); + res.send(txIdResult); + } catch (e: any) { + res.status(400).send(e.message && e.code ? 'sendrawtransaction RPC error: ' + JSON.stringify({ code: e.code, message: e.message }) + : (e.message || 'Error')); + } + } } export default new Routes(); From 16e807c4b02c986bb219dccc98710fd083f5e1d9 Mon Sep 17 00:00:00 2001 From: wiz Date: Mon, 27 Sep 2021 03:49:20 +0900 Subject: [PATCH 2/2] Fix API docs curl example for `POST /api/tx` --- .../src/app/components/api-docs/code-template.component.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frontend/src/app/components/api-docs/code-template.component.ts b/frontend/src/app/components/api-docs/code-template.component.ts index 417653b01..5df582399 100644 --- a/frontend/src/app/components/api-docs/code-template.component.ts +++ b/frontend/src/app/components/api-docs/code-template.component.ts @@ -284,13 +284,13 @@ yarn add @mempool/liquid.js`; if (this.env.BASE_MODULE === 'mempool') { if (this.network === 'main' || this.network === '') { if (this.method === 'post') { - return `curl POST -sSLd "${text}"`; + return `curl -X POST -sSLd "${text}"`; } return `curl -sSL "${this.hostname}${text}"`; } if (this.method === 'post') { text = text.replace('/api', `/${this.network}/api`); - return `curl POST -sSLd "${text}"`; + return `curl -X POST -sSLd "${text}"`; } return `curl -sSL "${this.hostname}/${this.network}${text}"`; }