@did-btcr2/bitcoin 0.5.3 → 0.7.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/README.md +102 -2
- package/dist/.tsbuildinfo +1 -1
- package/dist/cjs/client/rest/protocol.js +2 -7
- package/dist/cjs/client/rpc/index.js +0 -1
- package/dist/cjs/client/rpc/protocol.js +1 -1
- package/dist/cjs/connection.js +1 -1
- package/dist/cjs/constants.js +1 -1
- package/dist/cjs/fee-estimator.js +33 -0
- package/dist/cjs/index.js +4 -1
- package/dist/cjs/network.js +16 -4
- package/dist/esm/client/rest/protocol.js +2 -7
- package/dist/esm/client/rest/protocol.js.map +1 -1
- package/dist/esm/client/rpc/index.js +0 -1
- package/dist/esm/client/rpc/index.js.map +1 -1
- package/dist/esm/client/rpc/protocol.js +1 -1
- package/dist/esm/connection.js +1 -1
- package/dist/esm/connection.js.map +1 -1
- package/dist/esm/constants.js +1 -1
- package/dist/esm/fee-estimator.js +30 -0
- package/dist/esm/fee-estimator.js.map +1 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/network.js +16 -4
- package/dist/esm/network.js.map +1 -1
- package/dist/esm/types.js.map +1 -1
- package/dist/types/client/http.d.ts +1 -1
- package/dist/types/client/rest/protocol.d.ts +2 -2
- package/dist/types/client/rest/protocol.d.ts.map +1 -1
- package/dist/types/client/rpc/index.d.ts.map +1 -1
- package/dist/types/client/rpc/protocol.d.ts +1 -1
- package/dist/types/connection.d.ts +3 -3
- package/dist/types/connection.d.ts.map +1 -1
- package/dist/types/constants.d.ts +1 -1
- package/dist/types/fee-estimator.d.ts +40 -0
- package/dist/types/fee-estimator.d.ts.map +1 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/network.d.ts +12 -2
- package/dist/types/network.d.ts.map +1 -1
- package/dist/types/types.d.ts.map +1 -1
- package/package.json +4 -5
- package/src/client/http.ts +1 -1
- package/src/client/rest/protocol.ts +2 -12
- package/src/client/rpc/index.ts +0 -4
- package/src/client/rpc/protocol.ts +1 -1
- package/src/connection.ts +3 -3
- package/src/constants.ts +1 -1
- package/src/fee-estimator.ts +52 -0
- package/src/index.ts +5 -0
- package/src/network.ts +25 -5
- package/src/types.ts +1 -8
|
@@ -7,7 +7,7 @@ import { toBase64 } from '../utils.js';
|
|
|
7
7
|
* Sans-I/O JSON-RPC protocol for Bitcoin Core.
|
|
8
8
|
*
|
|
9
9
|
* Builds {@link HttpRequest} descriptors for JSON-RPC method calls and
|
|
10
|
-
* provides response parsing
|
|
10
|
+
* provides response parsing, without performing any I/O.
|
|
11
11
|
*
|
|
12
12
|
* **Security note:** Built requests include an `Authorization` header when
|
|
13
13
|
* credentials are configured. Do not log or persist {@link HttpRequest}
|
package/src/connection.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { MethodError } from '@did-btcr2/common';
|
|
2
|
-
import type { networks } from 'bitcoinjs-lib';
|
|
3
2
|
import type { HttpExecutor } from './client/http.js';
|
|
4
3
|
import { BitcoinRestClient } from './client/rest/index.js';
|
|
5
4
|
import { BitcoinCoreRpcClient } from './client/rpc/index.js';
|
|
5
|
+
import type { BTCNetwork } from './network.js';
|
|
6
6
|
import { getNetwork } from './network.js';
|
|
7
7
|
import type { NetworkName, RestConfig, RpcConfig } from './types.js';
|
|
8
8
|
import { DEFAULT_BITCOIN_NETWORK_CONFIG } from './constants.js';
|
|
@@ -57,8 +57,8 @@ export class BitcoinConnection {
|
|
|
57
57
|
/** RPC client (Bitcoin Core). May be undefined if not configured. */
|
|
58
58
|
readonly rpc?: BitcoinCoreRpcClient;
|
|
59
59
|
|
|
60
|
-
/**
|
|
61
|
-
readonly data:
|
|
60
|
+
/** Bitcoin network params (for address derivation, PSBT signing, etc.). */
|
|
61
|
+
readonly data: BTCNetwork;
|
|
62
62
|
|
|
63
63
|
constructor(options: BitcoinConnectionOptions) {
|
|
64
64
|
this.name = options.network;
|
package/src/constants.ts
CHANGED
|
@@ -8,7 +8,7 @@ export const GENESIS_TX_ID = '4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2
|
|
|
8
8
|
/**
|
|
9
9
|
* Default endpoint configuration per Bitcoin network.
|
|
10
10
|
*
|
|
11
|
-
* **Regtest RPC:** Credentials are intentionally omitted
|
|
11
|
+
* **Regtest RPC:** Credentials are intentionally omitted - callers must
|
|
12
12
|
* provide `username` and `password` via overrides or explicit config.
|
|
13
13
|
* This prevents accidentally using hardcoded credentials in non-local
|
|
14
14
|
* environments.
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Estimates the fee (in satoshis) for a transaction of a given virtual size.
|
|
3
|
+
*
|
|
4
|
+
* Callers that build Bitcoin transactions (the did:btcr2 beacon broadcast paths,
|
|
5
|
+
* single-party and aggregated) delegate fee calculation to a `FeeEstimator` so they
|
|
6
|
+
* can plug in different strategies: static fee rates for tests/regtest, mempool APIs
|
|
7
|
+
* for mainnet, Bitcoin Core `estimatesmartfee` RPC for full-node setups, etc.
|
|
8
|
+
*
|
|
9
|
+
* Implementations return the **total fee in satoshis** for a transaction of the given
|
|
10
|
+
* virtual size (`vsize`). Callers pass the vsize; the estimator decides the fee rate
|
|
11
|
+
* and computes the total.
|
|
12
|
+
*/
|
|
13
|
+
export interface FeeEstimator {
|
|
14
|
+
/**
|
|
15
|
+
* Estimate the total fee in satoshis for a transaction of the given vsize.
|
|
16
|
+
* @param vsize Transaction virtual size in vbytes.
|
|
17
|
+
* @returns Total fee in satoshis.
|
|
18
|
+
*/
|
|
19
|
+
estimateFee(vsize: number): Promise<bigint>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Fee estimator that returns a fixed fee rate regardless of network conditions.
|
|
24
|
+
*
|
|
25
|
+
* Suitable for:
|
|
26
|
+
* - Tests (deterministic outputs)
|
|
27
|
+
* - Regtest (no real fee market)
|
|
28
|
+
* - Environments where a fee rate is supplied out-of-band
|
|
29
|
+
*
|
|
30
|
+
* For mainnet production use, prefer a dynamic estimator that queries current
|
|
31
|
+
* network conditions (mempool APIs, Bitcoin Core RPC).
|
|
32
|
+
*/
|
|
33
|
+
export class StaticFeeEstimator implements FeeEstimator {
|
|
34
|
+
readonly satsPerVbyte: number;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @param satsPerVbyte Fee rate in satoshis per virtual byte. Default: 5 sat/vB.
|
|
38
|
+
*/
|
|
39
|
+
constructor(satsPerVbyte: number = 5) {
|
|
40
|
+
if(satsPerVbyte < 0 || !Number.isFinite(satsPerVbyte)) {
|
|
41
|
+
throw new Error(`Invalid satsPerVbyte: ${satsPerVbyte}`);
|
|
42
|
+
}
|
|
43
|
+
this.satsPerVbyte = satsPerVbyte;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async estimateFee(vsize: number): Promise<bigint> {
|
|
47
|
+
if(vsize < 0 || !Number.isFinite(vsize)) {
|
|
48
|
+
throw new Error(`Invalid vsize: ${vsize}`);
|
|
49
|
+
}
|
|
50
|
+
return BigInt(Math.ceil(vsize * this.satsPerVbyte));
|
|
51
|
+
}
|
|
52
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -23,8 +23,13 @@ export { JsonRpcTransport } from './client/rpc/json-rpc.js';
|
|
|
23
23
|
export { BitcoinRpcError, BitcoinRestError } from './errors.js';
|
|
24
24
|
export type { RpcErrorType } from './errors.js';
|
|
25
25
|
|
|
26
|
+
// Fee estimation
|
|
27
|
+
export { StaticFeeEstimator } from './fee-estimator.js';
|
|
28
|
+
export type { FeeEstimator } from './fee-estimator.js';
|
|
29
|
+
|
|
26
30
|
// Helpers
|
|
27
31
|
export { getNetwork } from './network.js';
|
|
32
|
+
export type { BTCNetwork } from './network.js';
|
|
28
33
|
export { toBase64, safeText } from './client/utils.js';
|
|
29
34
|
|
|
30
35
|
// Constants
|
package/src/network.ts
CHANGED
|
@@ -1,16 +1,36 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { NETWORK, TEST_NETWORK } from '@scure/btc-signer';
|
|
2
|
+
import type { BTC_NETWORK } from '@scure/btc-signer/utils';
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
/**
|
|
5
|
+
* Bitcoin network parameters: bech32 HRP, address version bytes, WIF prefix.
|
|
6
|
+
* Mirrors @scure/btc-signer's `BTC_NETWORK` type.
|
|
7
|
+
*/
|
|
8
|
+
export type BTCNetwork = BTC_NETWORK;
|
|
9
|
+
|
|
10
|
+
/** Regtest network params (bcrt HRP). */
|
|
11
|
+
const REGTEST_NETWORK: BTCNetwork = {
|
|
12
|
+
bech32 : 'bcrt',
|
|
13
|
+
pubKeyHash : 0x6f,
|
|
14
|
+
scriptHash : 0xc4,
|
|
15
|
+
wif : 0xef,
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Resolve a named Bitcoin network to its @scure/btc-signer `BTC_NETWORK` params.
|
|
20
|
+
* Mainnet maps to `NETWORK`; testnet3/testnet4/signet/mutinynet all share `TEST_NETWORK`
|
|
21
|
+
* (identical address formats); regtest uses its own `bcrt` HRP.
|
|
22
|
+
*/
|
|
23
|
+
export function getNetwork(network: string): BTCNetwork {
|
|
4
24
|
switch (network) {
|
|
5
25
|
case 'bitcoin':
|
|
6
|
-
return
|
|
26
|
+
return NETWORK;
|
|
7
27
|
case 'testnet3':
|
|
8
28
|
case 'testnet4':
|
|
9
29
|
case 'signet':
|
|
10
30
|
case 'mutinynet':
|
|
11
|
-
return
|
|
31
|
+
return TEST_NETWORK;
|
|
12
32
|
case 'regtest':
|
|
13
|
-
return
|
|
33
|
+
return REGTEST_NETWORK;
|
|
14
34
|
default:
|
|
15
35
|
throw new Error(`Unknown network "${network}"`);
|
|
16
36
|
}
|
package/src/types.ts
CHANGED
|
@@ -9,8 +9,6 @@ export type NetworkName =
|
|
|
9
9
|
| 'mutinynet'
|
|
10
10
|
| 'regtest';
|
|
11
11
|
|
|
12
|
-
// ── REST types ──────────────────────────────────────────────────────
|
|
13
|
-
|
|
14
12
|
export type TransactionStatus = {
|
|
15
13
|
confirmed: boolean;
|
|
16
14
|
block_height: number;
|
|
@@ -110,8 +108,6 @@ export interface RestApiCallParams {
|
|
|
110
108
|
|
|
111
109
|
export type RestResponse = Response;
|
|
112
110
|
|
|
113
|
-
// ── RPC types ───────────────────────────────────────────────────────
|
|
114
|
-
|
|
115
111
|
export interface RpcConfig {
|
|
116
112
|
headers?: Record<string, string>;
|
|
117
113
|
host?: string;
|
|
@@ -197,7 +193,7 @@ export type ChainInfo = {
|
|
|
197
193
|
warnings?: string;
|
|
198
194
|
};
|
|
199
195
|
|
|
200
|
-
|
|
196
|
+
|
|
201
197
|
|
|
202
198
|
export type Block = {
|
|
203
199
|
hash: string;
|
|
@@ -233,8 +229,6 @@ export interface BlockV3 extends Block {
|
|
|
233
229
|
|
|
234
230
|
export type BlockResponse = BlockV0 | BlockV1 | BlockV2 | BlockV3;
|
|
235
231
|
|
|
236
|
-
// ── Transaction types ───────────────────────────────────────────────
|
|
237
|
-
|
|
238
232
|
export type Transaction = {
|
|
239
233
|
hex: string;
|
|
240
234
|
txid: string;
|
|
@@ -482,4 +476,3 @@ export type MethodNameInLowerCase =
|
|
|
482
476
|
| 'signrawtransactionwithwallet'
|
|
483
477
|
| 'send'
|
|
484
478
|
| 'sendall';
|
|
485
|
-
|