From 53cb61fa697e4a731e32768748cc3d69851e45e2 Mon Sep 17 00:00:00 2001 From: mr-zwets <53938059+mr-zwets@users.noreply.github.com> Date: Tue, 29 Oct 2024 10:13:50 +0100 Subject: [PATCH] Replace bitcoin-rpc-promise-retry by @mr-zwets/bchn-api-wrapper (#229) --- .cspell.json | 1 - packages/cashscript/package.json | 2 +- .../bitcoin-rpc-promise-retry.d.ts | 1 - .../src/network/BitcoinRpcNetworkProvider.ts | 64 ++++++------------- yarn.lock | 17 ++--- 5 files changed, 24 insertions(+), 61 deletions(-) delete mode 100644 packages/cashscript/src/external-types/bitcoin-rpc-promise-retry.d.ts diff --git a/.cspell.json b/.cspell.json index 29a1b2b9..f321eba6 100644 --- a/.cspell.json +++ b/.cspell.json @@ -261,6 +261,5 @@ "**/grammar/**", // Do not spellcheck vendor code "**/node_modules/**", - "**/bitcoin-rpc-promise-retry.d.ts" ] } diff --git a/packages/cashscript/package.json b/packages/cashscript/package.json index 66af3c71..dc3e2128 100644 --- a/packages/cashscript/package.json +++ b/packages/cashscript/package.json @@ -45,8 +45,8 @@ "dependencies": { "@bitauth/libauth": "^3.0.0", "@cashscript/utils": "^0.10.1", + "@mr-zwets/bchn-api-wrapper": "^1.0.1", "bip68": "^1.0.4", - "bitcoin-rpc-promise-retry": "^1.3.0", "delay": "^5.0.0", "electrum-cash": "^2.0.10", "fast-deep-equal": "^3.1.3", diff --git a/packages/cashscript/src/external-types/bitcoin-rpc-promise-retry.d.ts b/packages/cashscript/src/external-types/bitcoin-rpc-promise-retry.d.ts deleted file mode 100644 index 650d4d75..00000000 --- a/packages/cashscript/src/external-types/bitcoin-rpc-promise-retry.d.ts +++ /dev/null @@ -1 +0,0 @@ -declare module 'bitcoin-rpc-promise-retry'; diff --git a/packages/cashscript/src/network/BitcoinRpcNetworkProvider.ts b/packages/cashscript/src/network/BitcoinRpcNetworkProvider.ts index 8aa883dc..bde9098d 100644 --- a/packages/cashscript/src/network/BitcoinRpcNetworkProvider.ts +++ b/packages/cashscript/src/network/BitcoinRpcNetworkProvider.ts @@ -1,20 +1,29 @@ -import RpcClientRetry from 'bitcoin-rpc-promise-retry'; import { Utxo, Network } from '../interfaces.js'; import NetworkProvider from './NetworkProvider.js'; +import { + BchnRpcClient, + type GetBlockCount, + type GetRawTransactionVerbosity0, + type ListUnspent, + type SendRawTransaction, +} from '@mr-zwets/bchn-api-wrapper'; export default class BitcoinRpcNetworkProvider implements NetworkProvider { - private rpcClient: IRpcClientRetry; + private rpcClient: BchnRpcClient; constructor( public network: Network, url: string, - opts?: object, + opts: { + rpcUser: string; + rpcPassword: string; + }, ) { - this.rpcClient = new RpcClientRetry(url, opts); + this.rpcClient = new BchnRpcClient({ url, ...opts }); } async getUtxos(address: string): Promise { - const result = await this.rpcClient.listUnspent(0, 9999999, [address]); + const result = await this.rpcClient.request('listunspent', 0, 9999999, [address]); const utxos = result.map((utxo) => ({ txid: utxo.txid, @@ -26,55 +35,18 @@ export default class BitcoinRpcNetworkProvider implements NetworkProvider { } async getBlockHeight(): Promise { - return this.rpcClient.getBlockCount(); + return this.rpcClient.request('getblockcount'); } async getRawTransaction(txid: string): Promise { - return this.rpcClient.getRawTransaction(txid); + return this.rpcClient.request('getrawtransaction', txid, 0); } async sendRawTransaction(txHex: string): Promise { - return this.rpcClient.sendRawTransaction(txHex); + return this.rpcClient.request('sendrawtransaction', txHex); } - getClient(): IRpcClientRetry { + getClient(): BchnRpcClient { return this.rpcClient; } } - -interface ListUnspentItem { - txid: string; - vout: number; - address: string; - label: string; - scriptPubKey: string; - amount: number; - confirmations: number; - redeemScript: string; - spendable: boolean; - solvable: boolean; - safe: boolean; -} - -interface IRpcClientRetry { - constructor(url: string, opts?: object): void; - listUnspent( - minConf?: number, - maxConf?: number, - addresses?: string[], - includeUnsafe?: boolean, - queryOptions?: object, - ): Promise; - getBlockCount(): Promise; - getRawTransaction(txid: string, verbose?: boolean, blockHash?: string): Promise; - sendRawTransaction(hexString: string, allowHighFees?: boolean): Promise; - - // below are not required for NetworkProvider interface, but very useful - generate(nBlocks: number, maxTries?: number): Promise; - generateToAddress(nBlocks: number, address: string, maxTries?: number): Promise; - getNewAddress(label?: string): Promise; - dumpPrivKey(address: string): Promise; - getBalance(dummy?: string, minConf?: number, includeWatchOnly?: boolean): Promise; - getBlock(blockHash: string, verbosity?: number): Promise; - importAddress(address: string, label?: string, rescan?: boolean, p2sh?: boolean): Promise; -} diff --git a/yarn.lock b/yarn.lock index 1cfb6b96..fa9c965f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2036,6 +2036,11 @@ npmlog "^4.1.2" write-file-atomic "^2.3.0" +"@mr-zwets/bchn-api-wrapper@^1.0.1": + version "1.0.1" + resolved "https://registry.yarnpkg.com/@mr-zwets/bchn-api-wrapper/-/bchn-api-wrapper-1.0.1.tgz#1ecd9fca91ed7e33df9769e243ae04871d2d356f" + integrity sha512-EyKT6zgXh31JqTRsi+KCX04WPDjIqs80nfsafrKiUZTg8FgWzql1wSyYdggmeByB74yLHD0LouC40S8PMmvcZA== + "@mrmlnc/readdir-enhanced@^2.2.1": version "2.2.1" resolved "https://registry.yarnpkg.com/@mrmlnc/readdir-enhanced/-/readdir-enhanced-2.2.1.tgz#524af240d1a360527b730475ecfa1344aa540dde" @@ -3422,18 +3427,6 @@ bip68@^1.0.4: resolved "https://registry.yarnpkg.com/bip68/-/bip68-1.0.4.tgz#78a95c7a43fad183957995cc2e08d79b0c372c4d" integrity sha512-O1htyufFTYy3EO0JkHg2CLykdXEtV2ssqw47Gq9A0WByp662xpJnMEB9m43LZjsSDjIAOozWRExlFQk2hlV1XQ== -bitcoin-rpc-promise-retry@^1.3.0: - version "1.3.0" - resolved "https://registry.yarnpkg.com/bitcoin-rpc-promise-retry/-/bitcoin-rpc-promise-retry-1.3.0.tgz#28a9f5aa7b5528b0518b0bcccde63e7384e1626b" - integrity sha512-xhldBhYwiUNY4spzkXXcU9LEq+nbSKJ5tvYWEUe/XTCe1VYoe8tEb5jJq4MBA2d+5egiUOA+P1QTTYm1FqPtOg== - dependencies: - bitcoind-rpc "^0.8.0" - -bitcoind-rpc@^0.8.0: - version "0.8.1" - resolved "https://registry.yarnpkg.com/bitcoind-rpc/-/bitcoind-rpc-0.8.1.tgz#11889972e46c346870d26cf680e3a7e3e52b1ff1" - integrity sha512-NfhykAT/x/P1SOiog8UzltvTiv6A6d2X5VWJ3UjGeAqFLXv+IYHy+E4fFCBmgQRyIb1EIcyIZK1SVpSOGRHsaw== - bitcoinjs-message@^2.0.0: version "2.1.1" resolved "https://registry.yarnpkg.com/bitcoinjs-message/-/bitcoinjs-message-2.1.1.tgz#c55d78f4461691b77fa5f9341216f8cd7ae0d0f4"