From 86c877c8e968c45fad740d44682615b26526eac8 Mon Sep 17 00:00:00 2001 From: softsimon Date: Sun, 26 Sep 2021 22:18:44 +0400 Subject: [PATCH] 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();