Skip to content

Commit

Permalink
Replace bitcoin-rpc-promise-retry by @mr-zwets/bchn-api-wrapper (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-zwets authored Oct 29, 2024
1 parent 6a787bf commit 53cb61f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 61 deletions.
1 change: 0 additions & 1 deletion .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,5 @@
"**/grammar/**",
// Do not spellcheck vendor code
"**/node_modules/**",
"**/bitcoin-rpc-promise-retry.d.ts"
]
}
2 changes: 1 addition & 1 deletion packages/cashscript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

This file was deleted.

64 changes: 18 additions & 46 deletions packages/cashscript/src/network/BitcoinRpcNetworkProvider.ts
Original file line number Diff line number Diff line change
@@ -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<Utxo[]> {
const result = await this.rpcClient.listUnspent(0, 9999999, [address]);
const result = await this.rpcClient.request<ListUnspent>('listunspent', 0, 9999999, [address]);

const utxos = result.map((utxo) => ({
txid: utxo.txid,
Expand All @@ -26,55 +35,18 @@ export default class BitcoinRpcNetworkProvider implements NetworkProvider {
}

async getBlockHeight(): Promise<number> {
return this.rpcClient.getBlockCount();
return this.rpcClient.request<GetBlockCount>('getblockcount');
}

async getRawTransaction(txid: string): Promise<string> {
return this.rpcClient.getRawTransaction(txid);
return this.rpcClient.request<GetRawTransactionVerbosity0>('getrawtransaction', txid, 0);
}

async sendRawTransaction(txHex: string): Promise<string> {
return this.rpcClient.sendRawTransaction(txHex);
return this.rpcClient.request<SendRawTransaction>('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<ListUnspentItem[]>;
getBlockCount(): Promise<number>;
getRawTransaction(txid: string, verbose?: boolean, blockHash?: string): Promise<string>;
sendRawTransaction(hexString: string, allowHighFees?: boolean): Promise<string>;

// below are not required for NetworkProvider interface, but very useful
generate(nBlocks: number, maxTries?: number): Promise<string[]>;
generateToAddress(nBlocks: number, address: string, maxTries?: number): Promise<string[]>;
getNewAddress(label?: string): Promise<string>;
dumpPrivKey(address: string): Promise<string>;
getBalance(dummy?: string, minConf?: number, includeWatchOnly?: boolean): Promise<number>;
getBlock(blockHash: string, verbosity?: number): Promise<string>;
importAddress(address: string, label?: string, rescan?: boolean, p2sh?: boolean): Promise<void>;
}
17 changes: 5 additions & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 53cb61f

Please sign in to comment.