@did-btcr2/bitcoin 0.6.0 → 0.8.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.
@@ -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,6 +23,10 @@ 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';
28
32
  export type { BTCNetwork } from './network.js';
@@ -30,7 +34,6 @@ export { toBase64, safeText } from './client/utils.js';
30
34
 
31
35
  // Constants
32
36
  export {
33
- DEFAULT_BITCOIN_NETWORK_CONFIG,
34
37
  INITIAL_BLOCK_REWARD,
35
38
  HALVING_INTERVAL,
36
39
  COINBASE_MATURITY_DELAY,