@did-btcr2/bitcoin 0.3.4 → 0.4.0
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/dist/cjs/bitcoin.js +83 -84
- package/dist/cjs/bitcoin.js.map +1 -1
- package/dist/cjs/client/http.js +9 -0
- package/dist/cjs/client/http.js.map +1 -0
- package/dist/cjs/client/rest/address.js +20 -28
- package/dist/cjs/client/rest/address.js.map +1 -1
- package/dist/cjs/client/rest/block.js +23 -22
- package/dist/cjs/client/rest/block.js.map +1 -1
- package/dist/cjs/client/rest/index.js +31 -84
- package/dist/cjs/client/rest/index.js.map +1 -1
- package/dist/cjs/client/rest/protocol.js +129 -0
- package/dist/cjs/client/rest/protocol.js.map +1 -0
- package/dist/cjs/client/rest/transaction.js +19 -21
- package/dist/cjs/client/rest/transaction.js.map +1 -1
- package/dist/cjs/client/rpc/index.js +75 -337
- package/dist/cjs/client/rpc/index.js.map +1 -1
- package/dist/cjs/client/rpc/interface.js +1 -6
- package/dist/cjs/client/rpc/interface.js.map +1 -1
- package/dist/cjs/client/rpc/json-rpc.js +40 -63
- package/dist/cjs/client/rpc/json-rpc.js.map +1 -1
- package/dist/cjs/client/rpc/protocol.js +134 -0
- package/dist/cjs/client/rpc/protocol.js.map +1 -0
- package/dist/cjs/constants.js +16 -5
- package/dist/cjs/constants.js.map +1 -1
- package/dist/cjs/errors.js.map +1 -1
- package/dist/cjs/index.js +21 -13
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/types.js +5 -55
- package/dist/cjs/types.js.map +1 -1
- package/dist/esm/bitcoin.js +83 -84
- package/dist/esm/bitcoin.js.map +1 -1
- package/dist/esm/client/http.js +9 -0
- package/dist/esm/client/http.js.map +1 -0
- package/dist/esm/client/rest/address.js +20 -28
- package/dist/esm/client/rest/address.js.map +1 -1
- package/dist/esm/client/rest/block.js +23 -22
- package/dist/esm/client/rest/block.js.map +1 -1
- package/dist/esm/client/rest/index.js +31 -84
- package/dist/esm/client/rest/index.js.map +1 -1
- package/dist/esm/client/rest/protocol.js +129 -0
- package/dist/esm/client/rest/protocol.js.map +1 -0
- package/dist/esm/client/rest/transaction.js +19 -21
- package/dist/esm/client/rest/transaction.js.map +1 -1
- package/dist/esm/client/rpc/index.js +75 -337
- package/dist/esm/client/rpc/index.js.map +1 -1
- package/dist/esm/client/rpc/interface.js +1 -6
- package/dist/esm/client/rpc/interface.js.map +1 -1
- package/dist/esm/client/rpc/json-rpc.js +40 -63
- package/dist/esm/client/rpc/json-rpc.js.map +1 -1
- package/dist/esm/client/rpc/protocol.js +134 -0
- package/dist/esm/client/rpc/protocol.js.map +1 -0
- package/dist/esm/constants.js +16 -5
- package/dist/esm/constants.js.map +1 -1
- package/dist/esm/errors.js.map +1 -1
- package/dist/esm/index.js +21 -13
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/types.js +5 -55
- package/dist/esm/types.js.map +1 -1
- package/dist/types/bitcoin.d.ts +66 -45
- package/dist/types/bitcoin.d.ts.map +1 -1
- package/dist/types/client/http.d.ts +21 -0
- package/dist/types/client/http.d.ts.map +1 -0
- package/dist/types/client/rest/address.d.ts +16 -19
- package/dist/types/client/rest/address.d.ts.map +1 -1
- package/dist/types/client/rest/block.d.ts +24 -16
- package/dist/types/client/rest/block.d.ts.map +1 -1
- package/dist/types/client/rest/index.d.ts +20 -51
- package/dist/types/client/rest/index.d.ts.map +1 -1
- package/dist/types/client/rest/protocol.d.ts +58 -0
- package/dist/types/client/rest/protocol.d.ts.map +1 -0
- package/dist/types/client/rest/transaction.d.ts +16 -17
- package/dist/types/client/rest/transaction.d.ts.map +1 -1
- package/dist/types/client/rpc/index.d.ts +105 -253
- package/dist/types/client/rpc/index.d.ts.map +1 -1
- package/dist/types/client/rpc/interface.d.ts +1 -4
- package/dist/types/client/rpc/interface.d.ts.map +1 -1
- package/dist/types/client/rpc/json-rpc.d.ts +20 -18
- package/dist/types/client/rpc/json-rpc.d.ts.map +1 -1
- package/dist/types/client/rpc/protocol.d.ts +82 -0
- package/dist/types/client/rpc/protocol.d.ts.map +1 -0
- package/dist/types/constants.d.ts +16 -5
- package/dist/types/constants.d.ts.map +1 -1
- package/dist/types/errors.d.ts +4 -3
- package/dist/types/errors.d.ts.map +1 -1
- package/dist/types/index.d.ts +20 -13
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/types.d.ts +98 -696
- package/dist/types/types.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/bitcoin.ts +105 -118
- package/src/client/http.ts +28 -0
- package/src/client/rest/address.ts +24 -30
- package/src/client/rest/block.ts +29 -26
- package/src/client/rest/index.ts +38 -94
- package/src/client/rest/protocol.ts +153 -0
- package/src/client/rest/transaction.ts +23 -23
- package/src/client/rpc/index.ts +140 -379
- package/src/client/rpc/interface.ts +42 -51
- package/src/client/rpc/json-rpc.ts +53 -60
- package/src/client/rpc/protocol.ts +160 -0
- package/src/constants.ts +17 -5
- package/src/errors.ts +10 -3
- package/src/index.ts +81 -13
- package/src/types.ts +332 -972
package/src/client/rpc/index.ts
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
import { JSONUtils } from '@did-btcr2/common';
|
|
2
|
-
import { DEFAULT_BITCOIN_NETWORK_CONFIG } from '../../constants.js';
|
|
3
2
|
import { BitcoinRpcError } from '../../errors.js';
|
|
4
3
|
import {
|
|
5
|
-
BatchOption,
|
|
6
|
-
BitcoinRpcClientConfig,
|
|
7
4
|
BlockResponse,
|
|
8
5
|
BlockV0,
|
|
9
6
|
BlockV1,
|
|
@@ -16,505 +13,269 @@ import {
|
|
|
16
13
|
GetBlockParams,
|
|
17
14
|
ListTransactionsParams,
|
|
18
15
|
ListTransactionsResult,
|
|
19
|
-
MethodNameInLowerCase,
|
|
20
16
|
RawTransactionResponse,
|
|
21
17
|
RawTransactionV0,
|
|
22
18
|
RawTransactionV1,
|
|
23
19
|
RawTransactionV2,
|
|
24
|
-
|
|
20
|
+
RpcConfig,
|
|
25
21
|
SignedRawTx,
|
|
26
22
|
UnspentTxInfo,
|
|
27
23
|
VerbosityLevel,
|
|
28
24
|
WalletTransaction
|
|
29
25
|
} from '../../types.js';
|
|
26
|
+
import { HttpExecutor } from '../http.js';
|
|
30
27
|
import { BitcoinRpcClient } from './interface.js';
|
|
31
28
|
import { JsonRpcTransport } from './json-rpc.js';
|
|
32
29
|
|
|
30
|
+
// ── Typed RPC method map ────────────────────────────────────────────
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Maps Bitcoin Core JSON-RPC method names to their parameter and return types.
|
|
34
|
+
* Used by {@link BitcoinCoreRpcClient.executeRpc} for compile-time safety.
|
|
35
|
+
*/
|
|
36
|
+
export interface RpcMethodMap {
|
|
37
|
+
getbestblockhash: { params: []; result: string };
|
|
38
|
+
getblock: { params: [string, number?]; result: BlockResponse };
|
|
39
|
+
getblockchaininfo: { params: []; result: ChainInfo };
|
|
40
|
+
getblockcount: { params: []; result: number };
|
|
41
|
+
getblockhash: { params: [number]; result: string };
|
|
42
|
+
getbalance: { params: []; result: number };
|
|
43
|
+
getnewaddress: { params: [string?, string?]; result: string };
|
|
44
|
+
gettransaction: { params: [string, boolean?]; result: WalletTransaction };
|
|
45
|
+
getrawtransaction: { params: [string, number?, string?]; result: RawTransactionResponse };
|
|
46
|
+
createrawtransaction: { params: [CreateRawTxInputs[], CreateRawTxOutputs[], number?, boolean?]; result: string };
|
|
47
|
+
signrawtransactionwithwallet: { params: [string]; result: SignedRawTx };
|
|
48
|
+
sendrawtransaction: { params: [string, (number | string)?, (number | string)?]; result: string };
|
|
49
|
+
listtransactions: { params: [string?, number?, number?, boolean?]; result: ListTransactionsResult };
|
|
50
|
+
listunspent: { params: [number?, number?, string[]?, boolean?]; result: UnspentTxInfo[] };
|
|
51
|
+
sendtoaddress: { params: [string, number]; result: string };
|
|
52
|
+
signmessage: { params: [string, string]; result: string };
|
|
53
|
+
verifymessage: { params: [string, string, string]; result: boolean };
|
|
54
|
+
deriveaddresses: { params: [string, number[]?]; result: DerivedAddresses[] };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Method names that have typed definitions in {@link RpcMethodMap}. */
|
|
58
|
+
export type TypedRpcMethod = keyof RpcMethodMap;
|
|
59
|
+
|
|
60
|
+
// ── Client ──────────────────────────────────────────────────────────
|
|
61
|
+
|
|
33
62
|
/**
|
|
34
|
-
*
|
|
35
|
-
* @class BitcoinCoreRpcClient
|
|
36
|
-
* @type {BitcoinCoreRpcClient}
|
|
63
|
+
* Bitcoin Core RPC client.
|
|
37
64
|
* @implements {BitcoinRpcClient}
|
|
38
65
|
*/
|
|
39
66
|
export class BitcoinCoreRpcClient implements BitcoinRpcClient {
|
|
40
|
-
/**
|
|
41
|
-
* The singleton instance of the BitcoinCoreRpcClient.
|
|
42
|
-
* @type {BitcoinCoreRpcClient}
|
|
43
|
-
* @private
|
|
44
|
-
*/
|
|
45
|
-
static #instance: BitcoinCoreRpcClient;
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* The JSON-RPC transport layer.
|
|
49
|
-
* @type {JsonRpcTransport}
|
|
50
|
-
* @private
|
|
51
|
-
*/
|
|
52
67
|
readonly #transport: JsonRpcTransport;
|
|
68
|
+
readonly #config: RpcConfig;
|
|
53
69
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
* @private
|
|
58
|
-
*/
|
|
59
|
-
readonly #config: RpcClientConfig;
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* Constructs a new {@link BitcoinCoreRpcClient} instance from a new {@link RpcClient | RpcClient}.
|
|
63
|
-
* @param {RpcClientConfig} config The bitcoin-core client instance.
|
|
64
|
-
* @example
|
|
65
|
-
* ```
|
|
66
|
-
* import BitcoinRpcClient from '@did-btcr2/method';
|
|
67
|
-
* const bob = BitcoinRpcClient.connect(); // To use default polar config, pass no args. Polar must run locally.
|
|
68
|
-
* ```
|
|
69
|
-
*/
|
|
70
|
-
constructor(config: RpcClientConfig) {
|
|
71
|
-
this.#config = new BitcoinRpcClientConfig(config);
|
|
72
|
-
this.#transport = new JsonRpcTransport(this.#config);
|
|
70
|
+
constructor(config: RpcConfig, executor?: HttpExecutor) {
|
|
71
|
+
this.#config = config;
|
|
72
|
+
this.#transport = new JsonRpcTransport(config, executor);
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
* @returns {BitcoinRpcClient} The encapsulated {@link BitcoinRpcClient} object.
|
|
78
|
-
* @example
|
|
79
|
-
* ```
|
|
80
|
-
* import BitcoinRpcClient from '@did-btcr2/method';
|
|
81
|
-
* const alice = BitcoinRpcClient.connect();
|
|
82
|
-
* const config = alice.config;
|
|
83
|
-
* ```
|
|
84
|
-
*/
|
|
85
|
-
get config(): BitcoinRpcClientConfig {
|
|
86
|
-
const config = this.#config;
|
|
87
|
-
return config;
|
|
75
|
+
get config(): RpcConfig {
|
|
76
|
+
return this.#config;
|
|
88
77
|
}
|
|
89
78
|
|
|
90
|
-
/**
|
|
91
|
-
* Get the client for the current BitcoinRpcClient object.
|
|
92
|
-
* @returns {RpcClient} The encapsulated {@link RpcClient} object.
|
|
93
|
-
* @example
|
|
94
|
-
* ```
|
|
95
|
-
* const alice = BitcoinRpcClient.connect();
|
|
96
|
-
* const config = alice.client;
|
|
97
|
-
* ```
|
|
98
|
-
*/
|
|
99
79
|
get client(): JsonRpcTransport {
|
|
100
|
-
|
|
101
|
-
return client;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Static method initializes a static instance of BitcoinCoreRpcClient with the given configuration.
|
|
106
|
-
* The RpcClient returned by this method does not have any named methods.
|
|
107
|
-
* Use this method to create and pass a new RpcClient instance to a BitcoinCoreRpcClient constructor.
|
|
108
|
-
*
|
|
109
|
-
* @param {RpcClientConfig} config The configuration object for the client (optional).
|
|
110
|
-
* @returns {BitcoinCoreRpcClient} A new RpcClient instance.
|
|
111
|
-
* @example
|
|
112
|
-
* ```
|
|
113
|
-
* const options: RpcClientConfig = {
|
|
114
|
-
* host: 'http://localhost:18443',
|
|
115
|
-
* username: 'alice',
|
|
116
|
-
* password: 'alicepass',
|
|
117
|
-
* version: '28.1.0',
|
|
118
|
-
* }
|
|
119
|
-
* const alice = BitcoinCoreRpcClient.initialize(options); // Client config required
|
|
120
|
-
* ```
|
|
121
|
-
*/
|
|
122
|
-
public static initialize(config?: RpcClientConfig): BitcoinCoreRpcClient {
|
|
123
|
-
const cfg = new BitcoinRpcClientConfig(config);
|
|
124
|
-
BitcoinCoreRpcClient.#instance = new BitcoinCoreRpcClient(cfg);
|
|
125
|
-
return BitcoinCoreRpcClient.#instance;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/**
|
|
129
|
-
* Static method connects to a bitcoin node running the bitcoin core daemon (bitcoind).
|
|
130
|
-
* To use default polar config, do not pass a config. See {@link DEFAULT_BITCOIN_NETWORK_CONFIG} for default config.
|
|
131
|
-
* @required A locally running {@link https://github.com/jamaljsr/polar | Polar Lightning} regtest node.
|
|
132
|
-
*
|
|
133
|
-
* @param {?RpcClientConfig} config The configuration object for the client (optional).
|
|
134
|
-
* @returns A new {@link BitcoinRpcClient} instance.
|
|
135
|
-
* @example
|
|
136
|
-
* ```
|
|
137
|
-
* const alice = BitcoinRpcClient.connect();
|
|
138
|
-
* ```
|
|
139
|
-
*/
|
|
140
|
-
public static connect(config?: RpcClientConfig): BitcoinCoreRpcClient {
|
|
141
|
-
return new BitcoinCoreRpcClient(config ?? DEFAULT_BITCOIN_NETWORK_CONFIG.regtest.rpc);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
/**
|
|
145
|
-
* Check if the given error is a JSON-RPC error.
|
|
146
|
-
* @param {unknown} e The error to check.
|
|
147
|
-
* @returns {boolean} True if the error is a JSON-RPC error, false otherwise.
|
|
148
|
-
*/
|
|
149
|
-
public isJsonRpcError(e: unknown): e is Error & { name: 'RpcError'; code?: number } {
|
|
150
|
-
return (
|
|
151
|
-
e instanceof Error &&
|
|
152
|
-
e.name === 'RpcError' &&
|
|
153
|
-
typeof (e as any).code === 'number'
|
|
154
|
-
);
|
|
80
|
+
return this.#transport;
|
|
155
81
|
}
|
|
156
82
|
|
|
157
83
|
/**
|
|
158
|
-
* Executes a JSON-RPC command on the bitcoind node.
|
|
159
|
-
* @param {MethodNameInLowerCase} method The name of the method to call.
|
|
160
|
-
* @param {Array<any>} parameters The parameters to pass to the method.
|
|
161
|
-
* @returns {Promise<T>} A promise resolving to the result of the command.
|
|
84
|
+
* Executes a typed JSON-RPC command on the bitcoind node.
|
|
162
85
|
*/
|
|
163
|
-
private async executeRpc<
|
|
86
|
+
private async executeRpc<M extends TypedRpcMethod>(
|
|
87
|
+
method: M,
|
|
88
|
+
parameters: RpcMethodMap[M]['params'] = [] as unknown as RpcMethodMap[M]['params']
|
|
89
|
+
): Promise<RpcMethodMap[M]['result']> {
|
|
164
90
|
try {
|
|
165
|
-
const raw = await this.
|
|
91
|
+
const raw = await this.#transport.call(method, parameters as unknown[]);
|
|
166
92
|
const normalized = JSONUtils.isUnprototyped(raw) ? JSONUtils.normalize(raw) : raw;
|
|
167
|
-
|
|
168
|
-
? normalized[normalized.length - 1]
|
|
169
|
-
: normalized;
|
|
170
|
-
return result as T;
|
|
93
|
+
return normalized as RpcMethodMap[M]['result'];
|
|
171
94
|
} catch (err: unknown) {
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
*
|
|
178
|
-
* Map JSON-RPC error codes to HTTP status codes.
|
|
179
|
-
* @param {number | undefined} code The JSON-RPC error code.
|
|
180
|
-
* @returns {number} The corresponding HTTP status code.
|
|
181
|
-
*
|
|
182
|
-
* | Error type | HTTP code |
|
|
183
|
-
* | -------------------------------| --------------------|
|
|
184
|
-
* | Valid JSON-RPC failure | **400 / 404 / 422** |
|
|
185
|
-
* | JSON-RPC authentication issues | **401 / 403** |
|
|
186
|
-
* | Upstream bitcoind unreachable | **502** |
|
|
187
|
-
* | Upstream timeout | **504** |
|
|
188
|
-
* | Network transient errors | **503** |
|
|
189
|
-
* | Unknown unexpected errors | **500** |
|
|
190
|
-
*/
|
|
191
|
-
private mapRpcCodeToHttp(code?: number): number {
|
|
192
|
-
switch (code) {
|
|
193
|
-
case -32700:
|
|
194
|
-
case -32600:
|
|
195
|
-
case -32602:
|
|
196
|
-
return 400;
|
|
197
|
-
|
|
198
|
-
case -32601:
|
|
199
|
-
return 404;
|
|
200
|
-
|
|
201
|
-
default:
|
|
202
|
-
return 422;
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
/**
|
|
207
|
-
* Handle errors that occur while executing commands.
|
|
208
|
-
* @param methods An array of {@link BatchOption} objects.
|
|
209
|
-
* @param error The error that was thrown.
|
|
210
|
-
* @throws Throws a {@link BitcoinRpcError} with the error message.
|
|
211
|
-
*/
|
|
212
|
-
private handleError(err: unknown, method: string, params: any[]): never {
|
|
213
|
-
if (this.isJsonRpcError(err)) {
|
|
95
|
+
if (err instanceof BitcoinRpcError) throw err;
|
|
96
|
+
const cause = err instanceof Error ? err.message : String(err);
|
|
214
97
|
throw new BitcoinRpcError(
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
`
|
|
218
|
-
{ method
|
|
98
|
+
'UNKNOWN_ERROR',
|
|
99
|
+
500,
|
|
100
|
+
`Unknown failure in ${method}: ${cause}`,
|
|
101
|
+
{ method }
|
|
219
102
|
);
|
|
220
103
|
}
|
|
221
|
-
|
|
222
|
-
throw new BitcoinRpcError(
|
|
223
|
-
'UNKNOWN_ERROR',
|
|
224
|
-
500,
|
|
225
|
-
`Unknown failure in ${method}`,
|
|
226
|
-
{ method, params, err }
|
|
227
|
-
);
|
|
228
104
|
}
|
|
229
105
|
|
|
230
106
|
/**
|
|
231
107
|
* Returns the block data associated with a `blockhash` of a valid block.
|
|
232
|
-
* @param {GetBlockParams} params See {@link GetBlockParams} for details.
|
|
233
|
-
* @param {?string} params.blockhash The blockhash of the block to query.
|
|
234
|
-
* @param {?number} params.height The block height of the block to query.
|
|
235
|
-
* @param {?VerbosityLevel} params.verbosity The verbosity level. See {@link VerbosityLevel}.
|
|
236
|
-
* @returns {BlockResponse} A promise resolving to a {@link BlockResponse} formatted depending on `verbosity` level.
|
|
237
|
-
* @throws {BitcoinRpcError} If neither `blockhash` nor `height` is provided.
|
|
238
108
|
*/
|
|
239
109
|
public async getBlock({ blockhash, height, verbosity }: GetBlockParams): Promise<BlockResponse | undefined> {
|
|
240
|
-
|
|
241
|
-
if(!blockhash && height === undefined) {
|
|
110
|
+
if (!blockhash && height === undefined) {
|
|
242
111
|
throw new BitcoinRpcError('INVALID_PARAMS_GET_BLOCK', 400, 'blockhash or height required', { blockhash, height });
|
|
243
112
|
}
|
|
244
113
|
|
|
245
|
-
// If height is provided, get the blockhash
|
|
246
114
|
blockhash ??= await this.getBlockHash(height!);
|
|
247
|
-
if(!blockhash || typeof blockhash !== 'string') {
|
|
115
|
+
if (!blockhash || typeof blockhash !== 'string') {
|
|
248
116
|
return undefined;
|
|
249
117
|
}
|
|
250
|
-
|
|
118
|
+
|
|
251
119
|
const block = await this.executeRpc('getblock', [blockhash, verbosity ?? 3]);
|
|
252
120
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
case
|
|
256
|
-
|
|
257
|
-
case
|
|
258
|
-
|
|
259
|
-
case 2:
|
|
260
|
-
return block as BlockV2;
|
|
261
|
-
case 3:
|
|
262
|
-
return block as BlockV3;
|
|
263
|
-
default:
|
|
264
|
-
return block as BlockV3;
|
|
121
|
+
switch (verbosity) {
|
|
122
|
+
case 0: return block as BlockV0;
|
|
123
|
+
case 1: return block as BlockV1;
|
|
124
|
+
case 2: return block as BlockV2;
|
|
125
|
+
case 3: return block as BlockV3;
|
|
126
|
+
default: return block as BlockV3;
|
|
265
127
|
}
|
|
266
128
|
}
|
|
267
129
|
|
|
268
|
-
/**
|
|
269
|
-
* Returns the blockheight of the most-work fully-validated chain. The genesis block has height 0.
|
|
270
|
-
* @returns {Blockheight} The number of the blockheight with the most-work of the fully-validated chain.
|
|
271
|
-
*/
|
|
130
|
+
/** Returns the blockheight of the most-work fully-validated chain. */
|
|
272
131
|
public async getBlockCount(): Promise<number> {
|
|
273
132
|
return await this.executeRpc('getblockcount');
|
|
274
133
|
}
|
|
275
134
|
|
|
276
|
-
/**
|
|
277
|
-
* Returns the blockhash of the block at the given height in the active chain.
|
|
278
|
-
*/
|
|
135
|
+
/** Returns the blockhash of the block at the given height. */
|
|
279
136
|
public async getBlockHash(height: number): Promise<string> {
|
|
280
137
|
return await this.executeRpc('getblockhash', [height]);
|
|
281
138
|
}
|
|
282
139
|
|
|
283
|
-
/**
|
|
284
|
-
* Returns an object containing various blockchain state info.
|
|
285
|
-
*/
|
|
140
|
+
/** Returns various blockchain state info. */
|
|
286
141
|
public async getBlockchainInfo(): Promise<ChainInfo> {
|
|
287
|
-
return this.executeRpc('getblockchaininfo');
|
|
142
|
+
return await this.executeRpc('getblockchaininfo');
|
|
288
143
|
}
|
|
289
144
|
|
|
290
|
-
/**
|
|
291
|
-
* Sign inputs for raw transaction (serialized, hex-encoded).
|
|
292
|
-
* The second optional argument (may be null) is an array of previous transaction outputs that
|
|
293
|
-
* this transaction depends on but may not yet be in the block chain.
|
|
294
|
-
* Requires wallet passphrase to be set with walletpassphrase call if wallet is encrypted.
|
|
295
|
-
* @param {string} hexstring The hex-encoded transaction to send.
|
|
296
|
-
*/
|
|
145
|
+
/** Sign inputs for raw transaction (serialized, hex-encoded). */
|
|
297
146
|
public async signRawTransaction(hexstring: string): Promise<SignedRawTx> {
|
|
298
|
-
return await this.executeRpc
|
|
147
|
+
return await this.executeRpc('signrawtransactionwithwallet', [hexstring]);
|
|
299
148
|
}
|
|
300
149
|
|
|
301
|
-
/**
|
|
302
|
-
* Submit a raw transaction (serialized, hex-encoded) to local node and network.
|
|
303
|
-
*
|
|
304
|
-
* The transaction will be sent unconditionally to all peers, so using sendrawtransaction
|
|
305
|
-
* for manual rebroadcast may degrade privacy by leaking the transaction's origin, as
|
|
306
|
-
* nodes will normally not rebroadcast non-wallet transactions already in their mempool.
|
|
307
|
-
*
|
|
308
|
-
* @param {string} hexstring The hex-encoded transaction to send.
|
|
309
|
-
* @param {numbner} [maxfeerate] If not passed, default is 0.10.
|
|
310
|
-
* @returns {Promise<string>} A promise resolving to the transaction hash in hex.
|
|
311
|
-
*/
|
|
150
|
+
/** Submit a raw transaction (serialized, hex-encoded) to local node and network. */
|
|
312
151
|
public async sendRawTransaction(
|
|
313
152
|
hexstring: string,
|
|
314
153
|
maxfeerate?: number | string,
|
|
315
154
|
maxBurnAmount?: number | string
|
|
316
155
|
): Promise<string> {
|
|
317
|
-
return await this.executeRpc
|
|
156
|
+
return await this.executeRpc('sendrawtransaction', [hexstring, maxfeerate ?? 0.10, maxBurnAmount ?? 0.00]);
|
|
318
157
|
}
|
|
319
158
|
|
|
320
|
-
/**
|
|
321
|
-
* Combines calls to `signRawTransaction` and `sendRawTransaction`.
|
|
322
|
-
* @param {string} params.hexstring The hex-encoded transaction to send.
|
|
323
|
-
* @returns {Promise<string>} A promise resolving to the transaction hash in hex.
|
|
324
|
-
*/
|
|
159
|
+
/** Signs and sends a raw transaction. */
|
|
325
160
|
public async signAndSendRawTransaction(hexstring: string): Promise<string> {
|
|
326
|
-
const signedRawTx = await this.signRawTransaction(hexstring
|
|
161
|
+
const signedRawTx = await this.signRawTransaction(hexstring);
|
|
162
|
+
if (!signedRawTx.complete) {
|
|
163
|
+
throw new BitcoinRpcError(
|
|
164
|
+
'SIGNING_INCOMPLETE',
|
|
165
|
+
400,
|
|
166
|
+
'Transaction signing incomplete',
|
|
167
|
+
signedRawTx.errors
|
|
168
|
+
);
|
|
169
|
+
}
|
|
327
170
|
return await this.sendRawTransaction(signedRawTx.hex);
|
|
328
171
|
}
|
|
329
172
|
|
|
330
|
-
/**
|
|
331
|
-
* Combines calls to `createRawTransaction`, `signRawTransaction` and `sendRawTransaction`.
|
|
332
|
-
* @param {CreateRawTxInputs[]} inputs The inputs to the transaction (required).
|
|
333
|
-
* @param {CreateRawTxOutputs[]} outputs The outputs of the transaction (required).
|
|
334
|
-
* @returns {Promise<string>} A promise resolving to the transaction hash in hex.
|
|
335
|
-
*/
|
|
173
|
+
/** Creates, signs, and sends a raw transaction. */
|
|
336
174
|
public async createSignSendRawTransaction(inputs: CreateRawTxInputs[], outputs: CreateRawTxOutputs[]): Promise<string> {
|
|
337
175
|
const rawTx = await this.createRawTransaction(inputs, outputs);
|
|
338
176
|
const signedRawTx = await this.signRawTransaction(rawTx);
|
|
339
|
-
|
|
340
|
-
|
|
177
|
+
if (!signedRawTx.complete) {
|
|
178
|
+
throw new BitcoinRpcError(
|
|
179
|
+
'SIGNING_INCOMPLETE',
|
|
180
|
+
400,
|
|
181
|
+
'Transaction signing incomplete',
|
|
182
|
+
signedRawTx.errors
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
return await this.sendRawTransaction(signedRawTx.hex);
|
|
341
186
|
}
|
|
342
187
|
|
|
343
|
-
/**
|
|
344
|
-
* Returns up to 'count' most recent transactions skipping the first 'from' transactions for account 'label'.
|
|
345
|
-
* @param {ListTransactionsParams} params The parameters for the listTransactions command.
|
|
346
|
-
* @returns {Promise<ListTransactionsResult>} A promise resolving to a {@link ListTransactionsResult} object.
|
|
347
|
-
*/
|
|
188
|
+
/** Returns up to 'count' most recent transactions. */
|
|
348
189
|
public async listTransactions(params: ListTransactionsParams): Promise<ListTransactionsResult> {
|
|
349
|
-
return await this.executeRpc('listtransactions', [
|
|
190
|
+
return await this.executeRpc('listtransactions', [
|
|
191
|
+
params.account ?? '*',
|
|
192
|
+
params.count ?? 10,
|
|
193
|
+
params.skip ?? 0,
|
|
194
|
+
params.include_watchonly ?? false,
|
|
195
|
+
]);
|
|
350
196
|
}
|
|
351
197
|
|
|
352
|
-
/**
|
|
353
|
-
* Create a transaction spending the given inputs and creating new outputs.
|
|
354
|
-
* Outputs can be addresses or data.
|
|
355
|
-
* Returns hex-encoded raw transaction.
|
|
356
|
-
* Note that the transaction's inputs are not signed, and
|
|
357
|
-
* it is not stored in the wallet or transmitted to the network.
|
|
358
|
-
* @param {TxInForCreateRaw[]} inputs The inputs to the transaction (required).
|
|
359
|
-
* @param {CreateRawTxOutputs[]} outputs The outputs of the transaction (required).
|
|
360
|
-
* @param {number} [locktime] The locktime of the transaction (optional).
|
|
361
|
-
* @param {boolean} [replacable] Whether the transaction is replaceable (optional).
|
|
362
|
-
* @returns {string} The hex-encoded raw transaction.
|
|
363
|
-
*/
|
|
198
|
+
/** Create a transaction spending the given inputs and creating new outputs. */
|
|
364
199
|
public async createRawTransaction(inputs: CreateRawTxInputs[], outputs: CreateRawTxOutputs[], locktime?: number, replacable?: boolean): Promise<string> {
|
|
365
|
-
return await this.executeRpc
|
|
200
|
+
return await this.executeRpc('createrawtransaction', [inputs, outputs, locktime, replacable]);
|
|
366
201
|
}
|
|
367
202
|
|
|
368
|
-
/**
|
|
369
|
-
* Derives one or more addresses corresponding to an output descriptor.
|
|
370
|
-
* Examples of output descriptors are:
|
|
371
|
-
* pkh(<pubkey>) P2PKH outputs for the given pubkey
|
|
372
|
-
* wpkh(<pubkey>) Native segwit P2PKH outputs for the given pubkey
|
|
373
|
-
* sh(multi(<n>,<pubkey>,<pubkey>,...)) P2SH-multisig outputs for the given threshold and pubkeys
|
|
374
|
-
* raw(<hex script>) Outputs whose output script equals the specified hex-encoded bytes
|
|
375
|
-
* tr(<pubkey>,multi_a(<n>,<pubkey>,<pubkey>,...)) P2TR-multisig outputs for the given threshold and pubkeys
|
|
376
|
-
*
|
|
377
|
-
* In the above, <pubkey> either refers to a fixed public key in hexadecimal notation, or to an xpub/xprv optionally followed by one
|
|
378
|
-
* or more path elements separated by "/", where "h" represents a hardened child key.
|
|
379
|
-
*
|
|
380
|
-
* See {@link https://github.com/bitcoin/bitcoin/blob/master/doc/descriptors.md | github.com/bitcoin/bitcoin/descriptors.md}
|
|
381
|
-
* for more information.
|
|
382
|
-
* @param {string} descriptor The descriptor.
|
|
383
|
-
* @param {Array<number>} range If descriptor is ranged, must specify end or [begin,end] to derive.
|
|
384
|
-
* @returns {Array<DerivedAddresses>} a list of derived addresses
|
|
385
|
-
* @example First three native segwit receive addresses
|
|
386
|
-
* ```
|
|
387
|
-
* const bitcoind = BitcoinRpcClient.connect()
|
|
388
|
-
* const addresses = bitcoind.deriveAddresses("wpkh([d34db33f/84h/0h/0h]xpub6DJ2dN.../0/*)#cjjspncu", [0,2])
|
|
389
|
-
* ```
|
|
390
|
-
*/
|
|
203
|
+
/** Derives one or more addresses corresponding to an output descriptor. */
|
|
391
204
|
public async deriveAddresses(descriptor: string, range?: Array<number>): Promise<Array<DerivedAddresses>> {
|
|
392
205
|
return await this.executeRpc('deriveaddresses', [descriptor, range]);
|
|
393
206
|
}
|
|
394
207
|
|
|
395
|
-
/**
|
|
396
|
-
* Returns the total available balance. The available balance is what the wallet
|
|
397
|
-
* considers currently spendable, and is thus affected by options which limit
|
|
398
|
-
* spendability such as -spendzeroconfchange.
|
|
399
|
-
* @returns {Promise<number>} A promise resolving to the total available balance in BTC.
|
|
400
|
-
*/
|
|
208
|
+
/** Returns the total available balance. */
|
|
401
209
|
public async getBalance(): Promise<number> {
|
|
402
210
|
return await this.executeRpc('getbalance');
|
|
403
211
|
}
|
|
404
212
|
|
|
405
|
-
/**
|
|
406
|
-
* Returns a new Bitcoin address for receiving payments. If 'label' is specified,
|
|
407
|
-
* it is added to the address book so payments received with the address will be associated with 'label'.
|
|
408
|
-
* The 'address_type' can be one of "legacy", "p2sh-segwit", "bech32", or "bech32m".
|
|
409
|
-
* @param {string} addressType The address type to use (required, options=["legacy", "p2sh-segwit", "bech32", "bech32m"], default="bech32").
|
|
410
|
-
* @param {string} [label] The label to associate with the new address (optional).
|
|
411
|
-
* @returns {Promise<string>} A promise resolving to the new address.
|
|
412
|
-
*/
|
|
213
|
+
/** Returns a new Bitcoin address for receiving payments. */
|
|
413
214
|
public async getNewAddress(addressType: string, label?: string): Promise<string> {
|
|
414
215
|
return await this.executeRpc('getnewaddress', [label ?? '', addressType]);
|
|
415
216
|
}
|
|
416
217
|
|
|
417
|
-
/**
|
|
418
|
-
* Returns array of unspent transaction outputs with between minconf and maxconf (inclusive) confirmations.
|
|
419
|
-
* Optionally filter to only include txouts paid to specified addresses.
|
|
420
|
-
* @param {Object} params The parameters for the listUnspent command.
|
|
421
|
-
* @param {number} [params.minconf=0] The minimum number of confirmations an output must have to be included.
|
|
422
|
-
* @param {number} [params.maxconf=9999999] The maximum number of confirmations an output can have to be included.
|
|
423
|
-
* @param {string[]} [params.address] Only include outputs paid to these addresses.
|
|
424
|
-
* @param {boolean} [params.include_unsafe=true] Whether to include outputs that are not safe to spend.
|
|
425
|
-
* @returns {Promise<UnspentTxInfo[]>} A promise resolving to an array of {@link UnspentTxInfo} objects.
|
|
426
|
-
*/
|
|
218
|
+
/** Returns array of unspent transaction outputs. */
|
|
427
219
|
public async listUnspent(params: {
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
220
|
+
minconf?: number;
|
|
221
|
+
maxconf?: number;
|
|
222
|
+
address?: string[];
|
|
223
|
+
include_unsafe?: boolean;
|
|
432
224
|
}): Promise<UnspentTxInfo[]> {
|
|
433
|
-
|
|
434
|
-
|
|
225
|
+
return await this.executeRpc('listunspent', [
|
|
226
|
+
params.minconf ?? 0,
|
|
227
|
+
params.maxconf ?? 9999999,
|
|
228
|
+
params.address ?? [],
|
|
229
|
+
params.include_unsafe ?? true,
|
|
230
|
+
]);
|
|
435
231
|
}
|
|
436
232
|
|
|
437
|
-
/**
|
|
438
|
-
* Send an amount to a given address.
|
|
439
|
-
* @param {string} address The address to send to.
|
|
440
|
-
* @param {number} amount The amount to send in BTC.
|
|
441
|
-
* @returns {Promise<SendToAddressResult>} A promise resolving to the transaction id.
|
|
442
|
-
*/
|
|
233
|
+
/** Send an amount to a given address. */
|
|
443
234
|
public async sendToAddress(address: string, amount: number): Promise<RawTransactionV2> {
|
|
444
|
-
const txid = await this.executeRpc
|
|
235
|
+
const txid = await this.executeRpc('sendtoaddress', [address, amount]);
|
|
445
236
|
return await this.getRawTransaction(txid) as RawTransactionV2;
|
|
446
237
|
}
|
|
447
238
|
|
|
448
|
-
/**
|
|
449
|
-
* Sign a message with the private key of an address.
|
|
450
|
-
* @param {string} address The address to sign the message with.
|
|
451
|
-
* @param {string} message The message to sign.
|
|
452
|
-
* @returns {Promise<string>} A promise resolving to the signature in base64.
|
|
453
|
-
*/
|
|
239
|
+
/** Sign a message with the private key of an address. */
|
|
454
240
|
public async signMessage(address: string, message: string): Promise<string> {
|
|
455
241
|
return await this.executeRpc('signmessage', [address, message]);
|
|
456
242
|
}
|
|
457
243
|
|
|
458
|
-
/**
|
|
459
|
-
* Verify a signed message.
|
|
460
|
-
* @param {string} address The address to verify the message with.
|
|
461
|
-
* @param {string} signature The signature to verify in base64.
|
|
462
|
-
* @param {string} message The message to verify.
|
|
463
|
-
* @returns {Promise<boolean>} A promise resolving to true if the signature is valid, false otherwise.
|
|
464
|
-
*/
|
|
244
|
+
/** Verify a signed message. */
|
|
465
245
|
public async verifyMessage(address: string, signature: string, message: string): Promise<boolean> {
|
|
466
246
|
return await this.executeRpc('verifymessage', [address, signature, message]);
|
|
467
247
|
}
|
|
468
248
|
|
|
469
|
-
/**
|
|
470
|
-
* Get detailed information about in-wallet transaction <txid>.
|
|
471
|
-
* @param txid: The transaction id. (string, required)
|
|
472
|
-
* @param {boolean} include_watchonly Whether to include watch-only addresses in balance calculation and details.
|
|
473
|
-
* @returns {WalletTransaction} A promise resolving to a {@link WalletTransaction} object.
|
|
474
|
-
*/
|
|
249
|
+
/** Get detailed information about in-wallet transaction. */
|
|
475
250
|
public async getTransaction(txid: string, include_watchonly?: boolean): Promise<WalletTransaction> {
|
|
476
251
|
return await this.executeRpc('gettransaction', [txid, include_watchonly]);
|
|
477
252
|
}
|
|
478
253
|
|
|
479
|
-
/**
|
|
480
|
-
* Get detailed information about a transaction.
|
|
481
|
-
* By default, this call only returns a transaction if it is in the mempool. If -txindex is enabled
|
|
482
|
-
* and no blockhash argument is passed, it will return the transaction if it is in the mempool or any block.
|
|
483
|
-
* If a blockhash argument is passed, it will return the transaction if the specified block is available and
|
|
484
|
-
* the transaction is in that block.
|
|
485
|
-
* @param {string} txid The transaction id (required).
|
|
486
|
-
* @param {?VerbosityLevel} verbosity Response format: 0 (hex), 1 (json) or 2 (jsonext).
|
|
487
|
-
* @param {?string} blockhash The block in which to look for the transaction (optional).
|
|
488
|
-
* @returns {GetRawTransaction} A promise resolving to data about a transaction in the form specified by verbosity.
|
|
489
|
-
*/
|
|
254
|
+
/** Get detailed information about a transaction. */
|
|
490
255
|
public async getRawTransaction(txid: string, verbosity?: VerbosityLevel, blockhash?: string): Promise<RawTransactionResponse> {
|
|
491
|
-
// Get the raw transaction
|
|
492
256
|
const rawTransaction = await this.executeRpc('getrawtransaction', [txid, verbosity ?? 2, blockhash]);
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
case
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
return rawTransaction as RawTransactionV1;
|
|
499
|
-
case 2:
|
|
500
|
-
return rawTransaction as RawTransactionV2;
|
|
501
|
-
default:
|
|
502
|
-
return rawTransaction as RawTransactionV2;
|
|
257
|
+
switch (verbosity) {
|
|
258
|
+
case 0: return rawTransaction as RawTransactionV0;
|
|
259
|
+
case 1: return rawTransaction as RawTransactionV1;
|
|
260
|
+
case 2: return rawTransaction as RawTransactionV2;
|
|
261
|
+
default: return rawTransaction as RawTransactionV2;
|
|
503
262
|
}
|
|
504
263
|
}
|
|
505
264
|
|
|
506
|
-
/**
|
|
507
|
-
* Get detailed information about multiple transactions. An extension of {@link getRawTransaction}.
|
|
508
|
-
* @param {Array<string>} txids An array of transaction ids.
|
|
509
|
-
* @param {?VerbosityLevel} verbosity Response format: 0 (hex), 1 (json) or 2 (jsonext).
|
|
510
|
-
* @returns {Promise<Array<RawTransactionResponse>>}
|
|
511
|
-
*/
|
|
265
|
+
/** Get detailed information about multiple transactions using JSON-RPC batching. */
|
|
512
266
|
public async getRawTransactions(txids: string[], verbosity?: VerbosityLevel): Promise<RawTransactionResponse[]> {
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
)
|
|
267
|
+
const v = verbosity ?? 2;
|
|
268
|
+
const results = await this.#transport.batch(
|
|
269
|
+
txids.map(txid => ({ method: 'getrawtransaction', params: [txid, v] as unknown[] }))
|
|
517
270
|
);
|
|
271
|
+
return results.map(raw => {
|
|
272
|
+
const normalized = JSONUtils.isUnprototyped(raw) ? JSONUtils.normalize(raw) : raw;
|
|
273
|
+
switch (v) {
|
|
274
|
+
case 0: return normalized as RawTransactionV0;
|
|
275
|
+
case 1: return normalized as RawTransactionV1;
|
|
276
|
+
case 2: return normalized as RawTransactionV2;
|
|
277
|
+
default: return normalized as RawTransactionV2;
|
|
278
|
+
}
|
|
279
|
+
});
|
|
518
280
|
}
|
|
519
|
-
|
|
520
|
-
}
|
|
281
|
+
}
|