@btc-vision/bitcoin 7.0.0-beta.0 → 7.0.0-beta.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.
Files changed (116) hide show
  1. package/README.md +112 -13
  2. package/benchmark-compare/BENCHMARK.md +74 -59
  3. package/benchmark-compare/compare.bench.ts +249 -96
  4. package/benchmark-compare/harness.ts +23 -25
  5. package/benchmark-compare/package.json +1 -0
  6. package/browser/address.d.ts +4 -4
  7. package/browser/address.d.ts.map +1 -1
  8. package/browser/chunks/{psbt-parallel-B-dfm5GZ.js → psbt-parallel-jZ6QcCnM.js} +3128 -2731
  9. package/browser/index.d.ts +1 -1
  10. package/browser/index.d.ts.map +1 -1
  11. package/browser/index.js +603 -585
  12. package/browser/io/base58check.d.ts +1 -25
  13. package/browser/io/base58check.d.ts.map +1 -1
  14. package/browser/io/base64.d.ts.map +1 -1
  15. package/browser/networks.d.ts +1 -0
  16. package/browser/networks.d.ts.map +1 -1
  17. package/browser/payments/bip341.d.ts +17 -0
  18. package/browser/payments/bip341.d.ts.map +1 -1
  19. package/browser/payments/index.d.ts +3 -2
  20. package/browser/payments/index.d.ts.map +1 -1
  21. package/browser/payments/p2mr.d.ts +169 -0
  22. package/browser/payments/p2mr.d.ts.map +1 -0
  23. package/browser/payments/types.d.ts +11 -1
  24. package/browser/payments/types.d.ts.map +1 -1
  25. package/browser/psbt/bip371.d.ts +30 -0
  26. package/browser/psbt/bip371.d.ts.map +1 -1
  27. package/browser/psbt/psbtutils.d.ts +1 -0
  28. package/browser/psbt/psbtutils.d.ts.map +1 -1
  29. package/browser/psbt.d.ts.map +1 -1
  30. package/browser/workers/index.js +9 -9
  31. package/build/address.d.ts +4 -4
  32. package/build/address.d.ts.map +1 -1
  33. package/build/address.js +11 -1
  34. package/build/address.js.map +1 -1
  35. package/build/index.d.ts +1 -1
  36. package/build/index.d.ts.map +1 -1
  37. package/build/index.js.map +1 -1
  38. package/build/io/base58check.d.ts +1 -25
  39. package/build/io/base58check.d.ts.map +1 -1
  40. package/build/io/base58check.js +1 -31
  41. package/build/io/base58check.js.map +1 -1
  42. package/build/io/base64.d.ts.map +1 -1
  43. package/build/io/base64.js +3 -0
  44. package/build/io/base64.js.map +1 -1
  45. package/build/networks.d.ts +1 -0
  46. package/build/networks.d.ts.map +1 -1
  47. package/build/networks.js +12 -0
  48. package/build/networks.js.map +1 -1
  49. package/build/payments/bip341.d.ts +17 -0
  50. package/build/payments/bip341.d.ts.map +1 -1
  51. package/build/payments/bip341.js +32 -1
  52. package/build/payments/bip341.js.map +1 -1
  53. package/build/payments/index.d.ts +3 -2
  54. package/build/payments/index.d.ts.map +1 -1
  55. package/build/payments/index.js +2 -1
  56. package/build/payments/index.js.map +1 -1
  57. package/build/payments/p2mr.d.ts +178 -0
  58. package/build/payments/p2mr.d.ts.map +1 -0
  59. package/build/payments/p2mr.js +555 -0
  60. package/build/payments/p2mr.js.map +1 -0
  61. package/build/payments/types.d.ts +11 -1
  62. package/build/payments/types.d.ts.map +1 -1
  63. package/build/payments/types.js +1 -0
  64. package/build/payments/types.js.map +1 -1
  65. package/build/psbt/bip371.d.ts +30 -0
  66. package/build/psbt/bip371.d.ts.map +1 -1
  67. package/build/psbt/bip371.js +80 -15
  68. package/build/psbt/bip371.js.map +1 -1
  69. package/build/psbt/psbtutils.d.ts +1 -0
  70. package/build/psbt/psbtutils.d.ts.map +1 -1
  71. package/build/psbt/psbtutils.js +2 -0
  72. package/build/psbt/psbtutils.js.map +1 -1
  73. package/build/psbt.d.ts.map +1 -1
  74. package/build/psbt.js +3 -2
  75. package/build/psbt.js.map +1 -1
  76. package/build/pubkey.js +1 -1
  77. package/build/pubkey.js.map +1 -1
  78. package/build/tsconfig.build.tsbuildinfo +1 -1
  79. package/documentation/README.md +122 -0
  80. package/documentation/address.md +820 -0
  81. package/documentation/block.md +679 -0
  82. package/documentation/crypto.md +461 -0
  83. package/documentation/ecc.md +584 -0
  84. package/documentation/errors.md +656 -0
  85. package/documentation/io.md +942 -0
  86. package/documentation/networks.md +625 -0
  87. package/documentation/p2mr.md +380 -0
  88. package/documentation/payments.md +1485 -0
  89. package/documentation/psbt.md +1400 -0
  90. package/documentation/script.md +730 -0
  91. package/documentation/taproot.md +670 -0
  92. package/documentation/transaction.md +943 -0
  93. package/documentation/types.md +587 -0
  94. package/documentation/workers.md +1007 -0
  95. package/eslint.config.js +3 -0
  96. package/package.json +17 -14
  97. package/src/address.ts +22 -10
  98. package/src/index.ts +1 -0
  99. package/src/io/base58check.ts +1 -35
  100. package/src/io/base64.ts +5 -0
  101. package/src/networks.ts +13 -0
  102. package/src/payments/bip341.ts +36 -1
  103. package/src/payments/index.ts +4 -0
  104. package/src/payments/p2mr.ts +660 -0
  105. package/src/payments/types.ts +12 -0
  106. package/src/psbt/bip371.ts +84 -13
  107. package/src/psbt/psbtutils.ts +2 -0
  108. package/src/psbt.ts +4 -2
  109. package/src/pubkey.ts +1 -1
  110. package/test/bitcoin.core.spec.ts +1 -1
  111. package/test/fixtures/p2mr.json +270 -0
  112. package/test/integration/taproot.spec.ts +7 -3
  113. package/test/opnetTestnet.spec.ts +302 -0
  114. package/test/payments.spec.ts +3 -1
  115. package/test/psbt.spec.ts +297 -2
  116. package/test/tsconfig.json +2 -2
@@ -0,0 +1,625 @@
1
+ # Networks
2
+
3
+ The `networks` module defines configuration constants for every supported blockchain network. Each constant implements the `Network` interface and encodes the version bytes, prefixes, and BIP32 derivation parameters that the rest of the library -- address encoding, payments, PSBT construction -- relies on to produce chain-specific output.
4
+
5
+ ## Overview
6
+
7
+ | Network | Export name | Mainnet / Testnet | Bech32 / SegWit support |
8
+ |---------|-------------|-------------------|------------------------|
9
+ | Bitcoin | `bitcoin` | Mainnet | Yes |
10
+ | Bitcoin Testnet | `testnet` | Testnet | Yes |
11
+ | OPNet Testnet | `opnetTestnet` | Testnet | Yes |
12
+ | Bitcoin Regtest | `regtest` | Testnet (local) | Yes |
13
+ | Dogecoin | `dogecoin` | Mainnet | No |
14
+ | Dogecoin Testnet | `dogecoinTestnet` | Testnet | No |
15
+ | Litecoin | `litecoin` | Mainnet | Yes |
16
+ | Litecoin Testnet | `litecoinTestnet` | Testnet | Yes |
17
+ | Bitcoin Cash | `bitcoinCash` | Mainnet | Cashaddr only |
18
+ | Bitcoin Cash Testnet | `bitcoinCashTestnet` | Testnet | Cashaddr only |
19
+ | Dash | `dash` | Mainnet | No |
20
+ | Dash Testnet | `dashTestnet` | Testnet | No |
21
+
22
+ ---
23
+
24
+ ## Installation
25
+
26
+ ```typescript
27
+ import { networks } from '@btc-vision/bitcoin';
28
+
29
+ // Individual imports
30
+ import {
31
+ bitcoin,
32
+ testnet,
33
+ opnetTestnet,
34
+ regtest,
35
+ dogecoin,
36
+ dogecoinTestnet,
37
+ litecoin,
38
+ litecoinTestnet,
39
+ bitcoinCash,
40
+ bitcoinCashTestnet,
41
+ dash,
42
+ dashTestnet,
43
+ } from '@btc-vision/bitcoin/src/networks.js';
44
+
45
+ // Type import
46
+ import type { Network } from '@btc-vision/bitcoin';
47
+ // Note: The Bip32 interface is NOT re-exported from '@btc-vision/bitcoin'.
48
+ // It is defined in src/networks.ts. Access it via the Network.bip32 property type:
49
+ // type Bip32 = Network['bip32'];
50
+ ```
51
+
52
+ > **Note:** The `Bip32` interface is not exported from the main `@btc-vision/bitcoin` entry point. If you need to reference the type directly, derive it from the `Network` interface:
53
+ >
54
+ > ```typescript
55
+ > type Bip32 = Network['bip32'];
56
+ > ```
57
+
58
+ ---
59
+
60
+ ## Interface: Network
61
+
62
+ Every network constant satisfies the `Network` interface. Each field controls how keys and addresses are serialized on that chain.
63
+
64
+ ```typescript
65
+ interface Network {
66
+ readonly wif: number;
67
+ readonly bip32: Bip32;
68
+ readonly messagePrefix: string;
69
+ readonly bech32: string;
70
+ readonly bech32Opnet?: string;
71
+ readonly pubKeyHash: number;
72
+ readonly scriptHash: number;
73
+ }
74
+ ```
75
+
76
+ | Field | Type | Description |
77
+ |-------|------|-------------|
78
+ | `messagePrefix` | `string` | Prefix prepended to messages before signing (e.g. `"\x18Bitcoin Signed Message:\n"`). Used by message-signing utilities to domain-separate signatures across chains. |
79
+ | `bech32` | `string` | Human-readable part (HRP) for Bech32 / Bech32m addresses. For Bitcoin mainnet this is `"bc"`, producing addresses like `bc1q...`. Empty string for chains that do not support SegWit (Dogecoin, Dash). |
80
+ | `bech32Opnet` | `string` (optional) | Human-readable part for OP_NET addresses. For Bitcoin mainnet this is `"op"`. Empty string for chains without OP_NET support. |
81
+ | `bip32` | `Bip32` | Version bytes for BIP32 extended key serialization. Controls whether `xpub` / `xprv` or an alternative prefix is produced. |
82
+ | `pubKeyHash` | `number` | Version byte for Pay-to-Public-Key-Hash (P2PKH) addresses (base58check). Determines the leading character of legacy addresses (`1` for Bitcoin, `D` for Dogecoin, `L` for Litecoin, etc.). |
83
+ | `scriptHash` | `number` | Version byte for Pay-to-Script-Hash (P2SH) addresses (base58check). Determines the leading character of script addresses (`3` for Bitcoin). |
84
+ | `wif` | `number` | Version byte for Wallet Import Format (WIF) private key encoding. |
85
+
86
+ ---
87
+
88
+ ## Interface: Bip32
89
+
90
+ ```typescript
91
+ interface Bip32 {
92
+ readonly public: number;
93
+ readonly private: number;
94
+ }
95
+ ```
96
+
97
+ | Field | Type | Description |
98
+ |-------|------|-------------|
99
+ | `public` | `number` | 4-byte version prefix for BIP32 extended **public** keys. For Bitcoin mainnet (`0x0488b21e`) this produces keys starting with `xpub`. |
100
+ | `private` | `number` | 4-byte version prefix for BIP32 extended **private** keys. For Bitcoin mainnet (`0x0488ade4`) this produces keys starting with `xprv`. |
101
+
102
+ ---
103
+
104
+ ## Network Constants
105
+
106
+ ### bitcoin (Mainnet)
107
+
108
+ ```typescript
109
+ export const bitcoin: Network = {
110
+ messagePrefix: '\x18Bitcoin Signed Message:\n',
111
+ bech32: 'bc',
112
+ bech32Opnet: 'op',
113
+ bip32: {
114
+ public: 0x0488b21e,
115
+ private: 0x0488ade4,
116
+ },
117
+ pubKeyHash: 0x00,
118
+ scriptHash: 0x05,
119
+ wif: 0x80,
120
+ };
121
+ ```
122
+
123
+ - P2PKH addresses start with `1`
124
+ - P2SH addresses start with `3`
125
+ - Bech32 addresses start with `bc1q` (SegWit v0) or `bc1p` (SegWit v1 / Taproot)
126
+ - Extended public keys start with `xpub`, private keys with `xprv`
127
+
128
+ ---
129
+
130
+ ### testnet (Bitcoin Testnet)
131
+
132
+ ```typescript
133
+ export const testnet: Network = {
134
+ messagePrefix: '\x18Bitcoin Signed Message:\n',
135
+ bech32: 'tb',
136
+ bech32Opnet: 'opt',
137
+ bip32: {
138
+ public: 0x043587cf,
139
+ private: 0x04358394,
140
+ },
141
+ pubKeyHash: 0x6f,
142
+ scriptHash: 0xc4,
143
+ wif: 0xef,
144
+ };
145
+ ```
146
+
147
+ - P2PKH addresses start with `m` or `n`
148
+ - P2SH addresses start with `2`
149
+ - Bech32 addresses start with `tb1q` or `tb1p`
150
+ - Extended public keys start with `tpub`, private keys with `tprv`
151
+
152
+ ---
153
+
154
+ ### opnetTestnet (OPNet Testnet)
155
+
156
+ ```typescript
157
+ export const opnetTestnet: Network = {
158
+ messagePrefix: '\x18Bitcoin Signed Message:\n',
159
+ bech32: 'opt',
160
+ bech32Opnet: 'opt',
161
+ bip32: {
162
+ public: 0x043587cf,
163
+ private: 0x04358394,
164
+ },
165
+ pubKeyHash: 0x6f,
166
+ scriptHash: 0xc4,
167
+ wif: 0xef,
168
+ };
169
+ ```
170
+
171
+ - Shares `pubKeyHash`, `scriptHash`, `wif`, and `bip32` values with testnet
172
+ - Uses `'opt'` as both the `bech32` and `bech32Opnet` prefix, producing addresses like `opt1q...`
173
+
174
+ ---
175
+
176
+ ### regtest (Bitcoin Regtest)
177
+
178
+ ```typescript
179
+ export const regtest: Network = {
180
+ messagePrefix: '\x18Bitcoin Signed Message:\n',
181
+ bech32: 'bcrt',
182
+ bech32Opnet: 'opr',
183
+ bip32: {
184
+ public: 0x043587cf,
185
+ private: 0x04358394,
186
+ },
187
+ pubKeyHash: 0x6f,
188
+ scriptHash: 0xc4,
189
+ wif: 0xef,
190
+ };
191
+ ```
192
+
193
+ - Shares `pubKeyHash`, `scriptHash`, `wif`, and `bip32` values with testnet
194
+ - Distinguished by the `bech32` HRP `"bcrt"`, producing addresses like `bcrt1q...`
195
+
196
+ ---
197
+
198
+ ### dogecoin (Mainnet)
199
+
200
+ ```typescript
201
+ export const dogecoin: Network = {
202
+ messagePrefix: '\x19Dogecoin Signed Message:\n',
203
+ bech32: '',
204
+ bech32Opnet: '',
205
+ bip32: {
206
+ public: 0x02facafd,
207
+ private: 0x02fac398,
208
+ },
209
+ pubKeyHash: 0x1e,
210
+ scriptHash: 0x16,
211
+ wif: 0x9e,
212
+ };
213
+ ```
214
+
215
+ - P2PKH addresses start with `D`
216
+ - P2SH addresses start with `9` or `A`
217
+ - No Bech32 / SegWit support (empty `bech32` string)
218
+
219
+ ---
220
+
221
+ ### dogecoinTestnet
222
+
223
+ ```typescript
224
+ export const dogecoinTestnet: Network = {
225
+ messagePrefix: '\x19Dogecoin Signed Message:\n',
226
+ bech32: '',
227
+ bech32Opnet: '',
228
+ bip32: {
229
+ public: 0x0432a9a8,
230
+ private: 0x0432a243,
231
+ },
232
+ pubKeyHash: 0x71,
233
+ scriptHash: 0xc4,
234
+ wif: 0xf1,
235
+ };
236
+ ```
237
+
238
+ - No Bech32 / SegWit support
239
+
240
+ ---
241
+
242
+ ### litecoin (Mainnet)
243
+
244
+ ```typescript
245
+ export const litecoin: Network = {
246
+ messagePrefix: '\x19Litecoin Signed Message:\n',
247
+ bech32: 'ltc',
248
+ bech32Opnet: 'opl',
249
+ bip32: {
250
+ public: 0x019da462,
251
+ private: 0x019d9cfe,
252
+ },
253
+ pubKeyHash: 0x30,
254
+ scriptHash: 0x32,
255
+ wif: 0xb0,
256
+ };
257
+ ```
258
+
259
+ - P2PKH addresses start with `L`
260
+ - P2SH addresses start with `M`
261
+ - Bech32 addresses start with `ltc1q`
262
+
263
+ ---
264
+
265
+ ### litecoinTestnet
266
+
267
+ ```typescript
268
+ export const litecoinTestnet: Network = {
269
+ messagePrefix: '\x19Litecoin Signed Message:\n',
270
+ bech32: 'tltc',
271
+ bech32Opnet: 'oplt',
272
+ bip32: {
273
+ public: 0x0436ef7d,
274
+ private: 0x0436f6e1,
275
+ },
276
+ pubKeyHash: 0x6f,
277
+ scriptHash: 0x3a,
278
+ wif: 0xef,
279
+ };
280
+ ```
281
+
282
+ - Bech32 addresses start with `tltc1q`
283
+
284
+ ---
285
+
286
+ ### bitcoinCash (Mainnet)
287
+
288
+ ```typescript
289
+ export const bitcoinCash: Network = {
290
+ messagePrefix: '\x18Bitcoin Signed Message:\n',
291
+ bech32: 'bitcoincash',
292
+ bech32Opnet: 'opbch',
293
+ bip32: {
294
+ public: 0x0488b21e,
295
+ private: 0x0488ade4,
296
+ },
297
+ pubKeyHash: 0x00,
298
+ scriptHash: 0x05,
299
+ wif: 0x80,
300
+ };
301
+ ```
302
+
303
+ - Shares `pubKeyHash`, `scriptHash`, `wif`, and `bip32` values with Bitcoin mainnet (common fork heritage)
304
+ - The `bech32` field is set to `"bitcoincash"` for Cashaddr-style addresses, not SegWit
305
+ - Bitcoin Cash does not support SegWit; the Cashaddr prefix is provided for compatibility
306
+
307
+ ---
308
+
309
+ ### bitcoinCashTestnet
310
+
311
+ ```typescript
312
+ export const bitcoinCashTestnet: Network = {
313
+ messagePrefix: '\x18Bitcoin Signed Message:\n',
314
+ bech32: 'bchtest',
315
+ bech32Opnet: 'opbcht',
316
+ bip32: {
317
+ public: 0x043587cf,
318
+ private: 0x04358394,
319
+ },
320
+ pubKeyHash: 0x6f,
321
+ scriptHash: 0xc4,
322
+ wif: 0xef,
323
+ };
324
+ ```
325
+
326
+ ---
327
+
328
+ ### dash (Mainnet)
329
+
330
+ ```typescript
331
+ export const dash: Network = {
332
+ messagePrefix: '\x19DarkCoin Signed Message:\n',
333
+ bech32: '',
334
+ bech32Opnet: '',
335
+ bip32: {
336
+ public: 0x02fe52cc,
337
+ private: 0x02fe52f8,
338
+ },
339
+ pubKeyHash: 0x4c,
340
+ scriptHash: 0x10,
341
+ wif: 0xcc,
342
+ };
343
+ ```
344
+
345
+ - P2PKH addresses start with `X`
346
+ - Uses the legacy DarkCoin message prefix
347
+ - No Bech32 / SegWit support
348
+
349
+ ---
350
+
351
+ ### dashTestnet
352
+
353
+ ```typescript
354
+ export const dashTestnet: Network = {
355
+ messagePrefix: '\x19DarkCoin Signed Message:\n',
356
+ bech32: '',
357
+ bech32Opnet: '',
358
+ bip32: {
359
+ public: 0x3a8061a0,
360
+ private: 0x3a805837,
361
+ },
362
+ pubKeyHash: 0x8c,
363
+ scriptHash: 0x13,
364
+ wif: 0xef,
365
+ };
366
+ ```
367
+
368
+ - No Bech32 / SegWit support
369
+
370
+ ---
371
+
372
+ ## Network Comparison Table
373
+
374
+ ### Address Version Bytes
375
+
376
+ | Network | `pubKeyHash` | `scriptHash` | `wif` |
377
+ |---------|-------------|-------------|-------|
378
+ | bitcoin | `0x00` | `0x05` | `0x80` |
379
+ | testnet | `0x6f` | `0xc4` | `0xef` |
380
+ | opnetTestnet | `0x6f` | `0xc4` | `0xef` |
381
+ | regtest | `0x6f` | `0xc4` | `0xef` |
382
+ | dogecoin | `0x1e` | `0x16` | `0x9e` |
383
+ | dogecoinTestnet | `0x71` | `0xc4` | `0xf1` |
384
+ | litecoin | `0x30` | `0x32` | `0xb0` |
385
+ | litecoinTestnet | `0x6f` | `0x3a` | `0xef` |
386
+ | bitcoinCash | `0x00` | `0x05` | `0x80` |
387
+ | bitcoinCashTestnet | `0x6f` | `0xc4` | `0xef` |
388
+ | dash | `0x4c` | `0x10` | `0xcc` |
389
+ | dashTestnet | `0x8c` | `0x13` | `0xef` |
390
+
391
+ ### Bech32 and OP_NET Prefixes
392
+
393
+ | Network | `bech32` | `bech32Opnet` | SegWit supported |
394
+ |---------|----------|---------------|-----------------|
395
+ | bitcoin | `bc` | `op` | Yes |
396
+ | testnet | `tb` | `opt` | Yes |
397
+ | opnetTestnet | `opt` | `opt` | Yes |
398
+ | regtest | `bcrt` | `opr` | Yes |
399
+ | dogecoin | _(empty)_ | _(empty)_ | No |
400
+ | dogecoinTestnet | _(empty)_ | _(empty)_ | No |
401
+ | litecoin | `ltc` | `opl` | Yes |
402
+ | litecoinTestnet | `tltc` | `oplt` | Yes |
403
+ | bitcoinCash | `bitcoincash` | `opbch` | No (Cashaddr) |
404
+ | bitcoinCashTestnet | `bchtest` | `opbcht` | No (Cashaddr) |
405
+ | dash | _(empty)_ | _(empty)_ | No |
406
+ | dashTestnet | _(empty)_ | _(empty)_ | No |
407
+
408
+ ### BIP32 Extended Key Versions
409
+
410
+ | Network | `bip32.public` | `bip32.private` |
411
+ |---------|---------------|----------------|
412
+ | bitcoin | `0x0488b21e` | `0x0488ade4` |
413
+ | testnet | `0x043587cf` | `0x04358394` |
414
+ | opnetTestnet | `0x043587cf` | `0x04358394` |
415
+ | regtest | `0x043587cf` | `0x04358394` |
416
+ | dogecoin | `0x02facafd` | `0x02fac398` |
417
+ | dogecoinTestnet | `0x0432a9a8` | `0x0432a243` |
418
+ | litecoin | `0x019da462` | `0x019d9cfe` |
419
+ | litecoinTestnet | `0x0436ef7d` | `0x0436f6e1` |
420
+ | bitcoinCash | `0x0488b21e` | `0x0488ade4` |
421
+ | bitcoinCashTestnet | `0x043587cf` | `0x04358394` |
422
+ | dash | `0x02fe52cc` | `0x02fe52f8` |
423
+ | dashTestnet | `0x3a8061a0` | `0x3a805837` |
424
+
425
+ ---
426
+
427
+ ## Using Networks with Other Modules
428
+
429
+ The `Network` object threads through the entire library. Pass it explicitly to override the default (Bitcoin mainnet).
430
+
431
+ ### Address Encoding
432
+
433
+ `address.fromOutputScript` and `address.toOutputScript` accept a network parameter to produce chain-correct addresses.
434
+
435
+ ```typescript
436
+ import { address, networks } from '@btc-vision/bitcoin';
437
+
438
+ // Decode an output script into a testnet address
439
+ const testnetAddress = address.fromOutputScript(outputScript, networks.testnet);
440
+ // e.g. "tb1q..."
441
+
442
+ // Encode a Litecoin address into its output script
443
+ const ltcScript = address.toOutputScript('ltc1q...', networks.litecoin);
444
+ ```
445
+
446
+ ### Payments (P2PKH, P2SH, P2WPKH, P2WSH, P2TR, P2MR, P2OP)
447
+
448
+ Every payment constructor accepts an optional `network` field. When omitted, `networks.bitcoin` is used as the default.
449
+
450
+ ```typescript
451
+ import { payments, networks } from '@btc-vision/bitcoin';
452
+
453
+ // Create a P2WPKH payment on the testnet
454
+ const p2wpkh = payments.p2wpkh({
455
+ pubkey: publicKey,
456
+ network: networks.testnet,
457
+ });
458
+ console.log(p2wpkh.address); // "tb1q..."
459
+
460
+ // Create a P2PKH payment on Litecoin
461
+ const p2pkh = payments.p2pkh({
462
+ pubkey: publicKey,
463
+ network: networks.litecoin,
464
+ });
465
+ console.log(p2pkh.address); // "L..."
466
+
467
+ // Create a P2TR payment on regtest
468
+ const p2tr = payments.p2tr({
469
+ internalPubkey: xOnlyPubKey,
470
+ network: networks.regtest,
471
+ });
472
+ console.log(p2tr.address); // "bcrt1p..."
473
+ ```
474
+
475
+ ### PSBT (Partially Signed Bitcoin Transactions)
476
+
477
+ The `Psbt` constructor accepts an options object with a `network` field. This determines how addresses embedded in the PSBT are interpreted and encoded.
478
+
479
+ ```typescript
480
+ import { Psbt, networks } from '@btc-vision/bitcoin';
481
+
482
+ // Create a PSBT targeting the testnet
483
+ const psbt = new Psbt({ network: networks.testnet });
484
+
485
+ psbt.addInput({
486
+ hash: txid,
487
+ index: 0,
488
+ witnessUtxo: {
489
+ script: outputScript,
490
+ value: 50000n,
491
+ },
492
+ });
493
+
494
+ psbt.addOutput({
495
+ address: 'tb1q...',
496
+ value: 40000n,
497
+ });
498
+ ```
499
+
500
+ ### Key Pairs (ECPair / BIP32)
501
+
502
+ When creating or importing key pairs, pass the network so that WIF encoding and BIP32 serialization use the correct version bytes.
503
+
504
+ ```typescript
505
+ import { networks } from '@btc-vision/bitcoin';
506
+ import { ECPairFactory } from '@btc-vision/ecpair';
507
+
508
+ const ECPair = ECPairFactory(ecc);
509
+
510
+ // Import a WIF private key on testnet
511
+ const keyPair = ECPair.fromWIF(
512
+ 'cNaQCDwmmh4dS9LzCgVtyy1e1xjCJ21GUDHe9K98nzb689JvinGV',
513
+ networks.testnet,
514
+ );
515
+
516
+ // Generate a random key pair for Litecoin
517
+ const ltcKey = ECPair.makeRandom({ network: networks.litecoin });
518
+ console.log(ltcKey.toWIF()); // WIF with Litecoin version byte 0xb0
519
+ ```
520
+
521
+ ---
522
+
523
+ ## Code Examples
524
+
525
+ ### Switching Between Networks
526
+
527
+ A common pattern is to parameterize your application with a `Network` object so the same code works across chains.
528
+
529
+ ```typescript
530
+ import { payments, networks, type Network } from '@btc-vision/bitcoin';
531
+
532
+ function generateP2WPKHAddress(pubkey: Uint8Array, network: Network): string {
533
+ const payment = payments.p2wpkh({ pubkey, network });
534
+ if (!payment.address) throw new Error('Could not derive address');
535
+ return payment.address;
536
+ }
537
+
538
+ // Bitcoin mainnet
539
+ const btcAddr = generateP2WPKHAddress(pubkey, networks.bitcoin);
540
+ // "bc1q..."
541
+
542
+ // Bitcoin testnet
543
+ const tbtcAddr = generateP2WPKHAddress(pubkey, networks.testnet);
544
+ // "tb1q..."
545
+
546
+ // Litecoin mainnet
547
+ const ltcAddr = generateP2WPKHAddress(pubkey, networks.litecoin);
548
+ // "ltc1q..."
549
+ ```
550
+
551
+ ### Detecting the Network from an Address
552
+
553
+ You can iterate over network constants to determine which network an address belongs to.
554
+
555
+ ```typescript
556
+ import { address, networks, type Network } from '@btc-vision/bitcoin';
557
+
558
+ const allNetworks: { name: string; network: Network }[] = [
559
+ { name: 'bitcoin', network: networks.bitcoin },
560
+ { name: 'testnet', network: networks.testnet },
561
+ { name: 'opnetTestnet', network: networks.opnetTestnet },
562
+ { name: 'regtest', network: networks.regtest },
563
+ { name: 'litecoin', network: networks.litecoin },
564
+ { name: 'litecoinTestnet', network: networks.litecoinTestnet },
565
+ ];
566
+
567
+ function detectNetwork(addr: string): string | undefined {
568
+ for (const { name, network } of allNetworks) {
569
+ try {
570
+ address.toOutputScript(addr, network);
571
+ return name;
572
+ } catch {
573
+ // Not this network, try the next
574
+ }
575
+ }
576
+ return undefined;
577
+ }
578
+
579
+ console.log(detectNetwork('bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4'));
580
+ // "bitcoin"
581
+
582
+ console.log(detectNetwork('tb1qw508d6qejxtdg4y5r3zarvary0c5xw7kxpjzsx'));
583
+ // "testnet"
584
+ ```
585
+
586
+ ### Building a Transaction for a Non-Default Network
587
+
588
+ ```typescript
589
+ import { Psbt, payments, networks } from '@btc-vision/bitcoin';
590
+
591
+ const network = networks.testnet;
592
+
593
+ // Derive a P2WPKH payment for the sender
594
+ const senderPayment = payments.p2wpkh({
595
+ pubkey: senderPubKey,
596
+ network,
597
+ });
598
+
599
+ // Create the PSBT on the correct network
600
+ const psbt = new Psbt({ network });
601
+
602
+ psbt.addInput({
603
+ hash: prevTxId,
604
+ index: 0,
605
+ witnessUtxo: {
606
+ script: senderPayment.output!,
607
+ value: 100000n,
608
+ },
609
+ });
610
+
611
+ psbt.addOutput({
612
+ address: recipientTestnetAddress, // "tb1q..." or "2..." or "m..."
613
+ value: 90000n,
614
+ });
615
+
616
+ // Sign, finalize, and extract as usual
617
+ ```
618
+
619
+ ---
620
+
621
+ ## Source Reference
622
+
623
+ All network constants are defined in a single file:
624
+
625
+ - [`src/networks.ts`](../src/networks.ts) -- based on the [Bitcoin wiki list of address prefixes](https://en.bitcoin.it/wiki/List_of_address_prefixes)