@mr-zwets/bchn-api-wrapper 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/README.md +129 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/interfaces/interfaces.d.ts +70 -0
- package/dist/interfaces/interfaces.js +1 -0
- package/dist/interfaces/restInterfaces/interfaces.d.ts +109 -0
- package/dist/interfaces/restInterfaces/interfaces.js +1 -0
- package/dist/interfaces/rpcInterfaces/blockchain.d.ts +692 -0
- package/dist/interfaces/rpcInterfaces/blockchain.js +3 -0
- package/dist/interfaces/rpcInterfaces/control.d.ts +54 -0
- package/dist/interfaces/rpcInterfaces/control.js +3 -0
- package/dist/interfaces/rpcInterfaces/generating.d.ts +17 -0
- package/dist/interfaces/rpcInterfaces/generating.js +3 -0
- package/dist/interfaces/rpcInterfaces/index.d.ts +9 -0
- package/dist/interfaces/rpcInterfaces/index.js +12 -0
- package/dist/interfaces/rpcInterfaces/mining.d.ts +131 -0
- package/dist/interfaces/rpcInterfaces/mining.js +3 -0
- package/dist/interfaces/rpcInterfaces/network.d.ts +179 -0
- package/dist/interfaces/rpcInterfaces/network.js +3 -0
- package/dist/interfaces/rpcInterfaces/rawtransactions.d.ts +283 -0
- package/dist/interfaces/rpcInterfaces/rawtransactions.js +3 -0
- package/dist/interfaces/rpcInterfaces/util.d.ts +44 -0
- package/dist/interfaces/rpcInterfaces/util.js +3 -0
- package/dist/interfaces/rpcInterfaces/wallet.d.ts +620 -0
- package/dist/interfaces/rpcInterfaces/wallet.js +3 -0
- package/dist/interfaces/rpcInterfaces/zmq.d.ts +8 -0
- package/dist/interfaces/rpcInterfaces/zmq.js +3 -0
- package/dist/restClient.d.ts +17 -0
- package/dist/restClient.js +100 -0
- package/dist/rpcClient.d.ts +12 -0
- package/dist/rpcClient.js +85 -0
- package/dist/utils/errors.d.ts +3 -0
- package/dist/utils/errors.js +6 -0
- package/dist/utils/utils.d.ts +11 -0
- package/dist/utils/utils.js +49 -0
- package/package.json +40 -0
- package/src/index.ts +4 -0
- package/src/interfaces/interfaces.ts +87 -0
- package/src/interfaces/restInterfaces/interfaces.ts +117 -0
- package/src/interfaces/rpcInterfaces/blockchain.ts +759 -0
- package/src/interfaces/rpcInterfaces/control.ts +62 -0
- package/src/interfaces/rpcInterfaces/generating.ts +21 -0
- package/src/interfaces/rpcInterfaces/index.ts +14 -0
- package/src/interfaces/rpcInterfaces/mining.ts +143 -0
- package/src/interfaces/rpcInterfaces/network.ts +195 -0
- package/src/interfaces/rpcInterfaces/rawtransactions.ts +314 -0
- package/src/interfaces/rpcInterfaces/util.ts +52 -0
- package/src/interfaces/rpcInterfaces/wallet.ts +674 -0
- package/src/interfaces/rpcInterfaces/zmq.ts +11 -0
- package/src/restClient.ts +119 -0
- package/src/rpcClient.ts +93 -0
- package/src/utils/errors.ts +6 -0
- package/src/utils/utils.ts +55 -0
- package/test/restClient.test.ts +32 -0
- package/test/rpcClient.test.ts +115 -0
- package/test/setupTests.ts +54 -0
- package/test/tsconfig.json +4 -0
- package/tsconfig.json +13 -0
- package/vitest.config.ts +9 -0
package/CHANGELOG.md
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
# BCHN-API-Wrapper
|
|
2
|
+
|
|
3
|
+
This library is a Typescript wrapper for interacting with the Bitcoin Cash Node (BCHN) RPC and REST interfaces.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
The library is a simple wrapper for using the BCHN REST and RPC-endpoints in a type-safe way.
|
|
8
|
+
|
|
9
|
+
The library is designed to be easy to use and get started with.
|
|
10
|
+
|
|
11
|
+
The library has good documentation, automated tests and zero dependencies.
|
|
12
|
+
|
|
13
|
+
## Details
|
|
14
|
+
|
|
15
|
+
The `BchnRestClient` uses a class with unique methods for each of the endpoints.
|
|
16
|
+
|
|
17
|
+
The `BchnRpcClient` uses a request function which uses generics to type arguments and responses.
|
|
18
|
+
|
|
19
|
+
The **REST API** is ideal for **read-only** access to general blockchain information such as transactions, blocks, and UTXO data. In contrast, the **RPC API** allows for **full interaction** with the Bitcoin Cash node, including managing the built-in wallet, sending transactions, performing mining operations, and issuing control commands like pruning or stopping the node. While the REST API provides 9 endpoints, the RPC API offers a much broader set of 136 commands.
|
|
20
|
+
|
|
21
|
+
## Configuration
|
|
22
|
+
|
|
23
|
+
To use the RPC and REST APIs on your BCHN node, you need to enable them in your node's configuration file.
|
|
24
|
+
|
|
25
|
+
<details>
|
|
26
|
+
<summary>BCHN Configuration</summary>
|
|
27
|
+
In the BCHN '.conf' file add the following settings:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
server=1
|
|
31
|
+
rest=1
|
|
32
|
+
txindex=1
|
|
33
|
+
rpcuser=rpcuser
|
|
34
|
+
rpcpassword=rpcpassword
|
|
35
|
+
rpcallowip=127.0.0.1
|
|
36
|
+
rpcport=8332
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
To learn more about the `.conf` settings, see the [BCHN documentation](https://docs.bitcoincashnode.org/doc/bitcoin-conf/).
|
|
40
|
+
</details>
|
|
41
|
+
|
|
42
|
+
Note that the REST-endpoints can be made public, but the RPC-endpoints should never be made public. If you want to use the RPC-endpoints of your own node remotely, you need a secure connection using SSL/TLS to encrypt your communication and protect your credentials and data from being exposed. Additionally, ensure you have strong, unique RPC credentials (username and password) set in your node's configuration file.
|
|
43
|
+
|
|
44
|
+
## Install
|
|
45
|
+
|
|
46
|
+
Install the Bchn-API-Wrapper from NPM with:
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
npm install @mr-zwets/bchn-api-wrapper
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
or using yarn
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
yarn add @mr-zwets/bchn-api-wrapper
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## REST usage
|
|
59
|
+
|
|
60
|
+
The `BchnRestClient` is a wrapper over the 9 BCHN REST-endpoints. For the list of the BCHN REST-endpoints see the [REST documentation](https://docs.bitcoincashnode.org/doc/REST-interface/).
|
|
61
|
+
|
|
62
|
+
The `RestClientConfig` object accepts optional parameters for `logger` & `timeoutMs`
|
|
63
|
+
|
|
64
|
+
### REST example
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
import { BchnRestClient, RestClientConfig } from 'bchn-api-wrapper'
|
|
68
|
+
|
|
69
|
+
// Create the RestClientConfig
|
|
70
|
+
const clientOptions: RestClientConfig = {
|
|
71
|
+
url: "http://localhost:8332",
|
|
72
|
+
}
|
|
73
|
+
// Instantiate the REST client to query your BCHN node
|
|
74
|
+
const restClient = new BchnRestClient(clientOptions)
|
|
75
|
+
|
|
76
|
+
// Get the latest blockhash
|
|
77
|
+
const chainInfo = await restClient.getChainInfo()
|
|
78
|
+
const latestBlockHash = chainInfo.bestblockhash
|
|
79
|
+
console.log(`The latest blockhash is ${latestBlockHash}`)
|
|
80
|
+
|
|
81
|
+
// Get block info with includeTxDetails flag
|
|
82
|
+
const fullBlockInfo = await restClient.getBlock(latestBlockHash, true)
|
|
83
|
+
console.log(JSON.stringify(fullBlockInfo))
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## RPC usage
|
|
87
|
+
|
|
88
|
+
The `BchnRpcClient` is a thin type-wrapper over the actual RPC endpoints, with request interfaces for each endpoint. For a complete list of all BCHN RPC-endpoints see the [RPC documentation](https://docs.bitcoincashnode.org/doc/json-rpc/).
|
|
89
|
+
|
|
90
|
+
The `RpcClientConfig` object accepts optional parameters for `logger`, `timeoutMs`, `retryDelayMs` & `maxRetries`
|
|
91
|
+
|
|
92
|
+
The library does not currently support making batched RPC requests.
|
|
93
|
+
|
|
94
|
+
### RPC example
|
|
95
|
+
|
|
96
|
+
```ts
|
|
97
|
+
import { BchnRpcClient, RpcClientConfig, GetBestBlockHash, GetBlockVerbosity1 } from 'bchn-api-wrapper'
|
|
98
|
+
|
|
99
|
+
// Create the RpcClientConfig
|
|
100
|
+
const clientOptions: RpcClientConfig = {
|
|
101
|
+
url: "http://localhost:8332",
|
|
102
|
+
rpcUser: "rpcUser",
|
|
103
|
+
rpcPassword: "rpcPassword"
|
|
104
|
+
}
|
|
105
|
+
// Instantiate the RPC client to query your BCHN node
|
|
106
|
+
const rpcClient = new BchnRpcClient(clientOptions)
|
|
107
|
+
|
|
108
|
+
// Get the latest blockhash
|
|
109
|
+
const latestBlockHash = await rpcClient.request<GetBestBlockHash>("getbestblockhash")
|
|
110
|
+
console.log(`The latest blockhash is ${latestBlockHash}`)
|
|
111
|
+
|
|
112
|
+
// Get verbosity1 info about the latest block contents
|
|
113
|
+
const fullBlockInfo = await rpcClient.request<GetBlockVerbosity1>("getblock", latestBlockHash, 1)
|
|
114
|
+
console.log(JSON.stringify(fullBlockInfo))
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### Run Tests
|
|
118
|
+
|
|
119
|
+
The library has automated tests using vitest, run the testing suite with:
|
|
120
|
+
|
|
121
|
+
```bash
|
|
122
|
+
npm run test
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
or using yarn:
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
yarn test
|
|
129
|
+
```
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
export interface BaseRpcClientConfig {
|
|
2
|
+
rpcUser: string;
|
|
3
|
+
rpcPassword: string;
|
|
4
|
+
maxRetries?: number;
|
|
5
|
+
retryDelayMs?: number;
|
|
6
|
+
logger?: typeof console;
|
|
7
|
+
timeoutMs?: number;
|
|
8
|
+
}
|
|
9
|
+
export interface RpcClientUrlConfig extends BaseRpcClientConfig {
|
|
10
|
+
url: string;
|
|
11
|
+
}
|
|
12
|
+
export interface RpcClientHostConfig extends BaseRpcClientConfig {
|
|
13
|
+
protocol: 'http' | 'https';
|
|
14
|
+
host: string;
|
|
15
|
+
port: number;
|
|
16
|
+
}
|
|
17
|
+
export type RpcClientConfig = RpcClientUrlConfig | RpcClientHostConfig;
|
|
18
|
+
export type RPCParameter = string | number | boolean | undefined | object;
|
|
19
|
+
declare type RequestResponse = object | string | number | boolean | null | RequestResponse[];
|
|
20
|
+
export interface RpcRequest {
|
|
21
|
+
method: string;
|
|
22
|
+
params: Array<RPCParameter>;
|
|
23
|
+
response: RequestResponse;
|
|
24
|
+
}
|
|
25
|
+
export interface RestClientConfig {
|
|
26
|
+
url: string;
|
|
27
|
+
logger?: typeof console;
|
|
28
|
+
timeoutMs?: number;
|
|
29
|
+
}
|
|
30
|
+
export type formatOptions = 'bin' | 'hex' | 'json';
|
|
31
|
+
export type ResponseType<TFormat extends formatOptions, TJson> = TFormat extends 'json' ? TJson : TFormat extends 'hex' | 'bin' ? string : never;
|
|
32
|
+
export interface Transaction {
|
|
33
|
+
txid: string;
|
|
34
|
+
hash: string;
|
|
35
|
+
size: number;
|
|
36
|
+
version: number;
|
|
37
|
+
locktime: number;
|
|
38
|
+
vin: TransactionInput[];
|
|
39
|
+
vout: TransactionOutput[];
|
|
40
|
+
}
|
|
41
|
+
export interface TransactionInput {
|
|
42
|
+
txid: string;
|
|
43
|
+
vout: number;
|
|
44
|
+
scriptSig: {
|
|
45
|
+
asm: string;
|
|
46
|
+
hex: string;
|
|
47
|
+
};
|
|
48
|
+
sequence: number;
|
|
49
|
+
}
|
|
50
|
+
export interface TransactionOutput {
|
|
51
|
+
value: number;
|
|
52
|
+
n: number;
|
|
53
|
+
scriptPubKey: {
|
|
54
|
+
asm: string;
|
|
55
|
+
hex: string;
|
|
56
|
+
reqSigs: number;
|
|
57
|
+
type: string;
|
|
58
|
+
addresses: string[];
|
|
59
|
+
tokenData: TokenData;
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
export interface TokenData {
|
|
63
|
+
category: string;
|
|
64
|
+
amount: string;
|
|
65
|
+
nft?: {
|
|
66
|
+
capability: 'none' | 'mutable' | 'minting';
|
|
67
|
+
commitment: string;
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import type { Transaction } from "../interfaces.js";
|
|
2
|
+
export interface BlockInfoNoTxDetails {
|
|
3
|
+
hash: string;
|
|
4
|
+
confirmations: number;
|
|
5
|
+
size: number;
|
|
6
|
+
height: number;
|
|
7
|
+
version: number;
|
|
8
|
+
versionHex: string;
|
|
9
|
+
merkleroot: string;
|
|
10
|
+
tx: string[];
|
|
11
|
+
time: number;
|
|
12
|
+
mediantime: number;
|
|
13
|
+
nonce: number;
|
|
14
|
+
bits: string;
|
|
15
|
+
difficulty: number;
|
|
16
|
+
chainwork: string;
|
|
17
|
+
nTx: number;
|
|
18
|
+
previousblockhash: string;
|
|
19
|
+
nextblockhash: string;
|
|
20
|
+
ablastate: {
|
|
21
|
+
epsilon: number;
|
|
22
|
+
beta: number;
|
|
23
|
+
blocksize: number;
|
|
24
|
+
blocksizelimit: number;
|
|
25
|
+
nextblocksizelimit: number;
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
export interface BlockInfoTxDetails extends Omit<BlockInfoNoTxDetails, 'tx'> {
|
|
29
|
+
tx: Transaction[];
|
|
30
|
+
}
|
|
31
|
+
export interface HeaderInfo {
|
|
32
|
+
hash: string;
|
|
33
|
+
confirmations: number;
|
|
34
|
+
height: number;
|
|
35
|
+
version: number;
|
|
36
|
+
versionHex: string;
|
|
37
|
+
merkleroot: string;
|
|
38
|
+
time: number;
|
|
39
|
+
mediantime: number;
|
|
40
|
+
nonce: number;
|
|
41
|
+
bits: string;
|
|
42
|
+
difficulty: number;
|
|
43
|
+
chainwork: string;
|
|
44
|
+
nTx: number;
|
|
45
|
+
previousblockhash: string;
|
|
46
|
+
nextblockhash: string;
|
|
47
|
+
ablastate: {
|
|
48
|
+
epsilon: number;
|
|
49
|
+
beta: number;
|
|
50
|
+
blocksize: number;
|
|
51
|
+
blocksizelimit: number;
|
|
52
|
+
nextblocksizelimit: number;
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
export interface ChainInfo {
|
|
56
|
+
chain: 'main' | 'test' | 'regtest';
|
|
57
|
+
blocks: number;
|
|
58
|
+
headers: number;
|
|
59
|
+
bestblockhash: string;
|
|
60
|
+
difficulty: number;
|
|
61
|
+
mediantime: number;
|
|
62
|
+
verificationprogress: number;
|
|
63
|
+
initialblockdownload: boolean;
|
|
64
|
+
chainwork: string;
|
|
65
|
+
size_on_disk: number;
|
|
66
|
+
pruned: boolean;
|
|
67
|
+
warnings: string;
|
|
68
|
+
}
|
|
69
|
+
export interface UtxosInfo {
|
|
70
|
+
chaintipHash: string;
|
|
71
|
+
chainHeight: number;
|
|
72
|
+
utxos: {
|
|
73
|
+
scriptPubKey: {
|
|
74
|
+
addresses: string[];
|
|
75
|
+
type: string;
|
|
76
|
+
hex: string;
|
|
77
|
+
reqSigs: number;
|
|
78
|
+
asm: string;
|
|
79
|
+
};
|
|
80
|
+
value: number;
|
|
81
|
+
height: number;
|
|
82
|
+
txvers: number;
|
|
83
|
+
}[];
|
|
84
|
+
bitmap: string;
|
|
85
|
+
}
|
|
86
|
+
export interface MempoolInfo {
|
|
87
|
+
loaded: boolean;
|
|
88
|
+
size: number;
|
|
89
|
+
bytes: number;
|
|
90
|
+
usage: number;
|
|
91
|
+
maxmempool: number;
|
|
92
|
+
mempoolminfee: number;
|
|
93
|
+
minrelaytxfee: number;
|
|
94
|
+
}
|
|
95
|
+
export interface MempoolContent {
|
|
96
|
+
[txid: string]: {
|
|
97
|
+
fees: {
|
|
98
|
+
base: number;
|
|
99
|
+
modified: number;
|
|
100
|
+
};
|
|
101
|
+
size: number;
|
|
102
|
+
time: number;
|
|
103
|
+
depends: string[];
|
|
104
|
+
spentby: string[];
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
export interface TxDetails extends Transaction {
|
|
108
|
+
blockhash: string;
|
|
109
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|