@btc-vision/bitcoin 7.0.0-alpha.0 → 7.0.0-alpha.10

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 (369) hide show
  1. package/README.md +455 -155
  2. package/browser/address.d.ts +6 -2
  3. package/browser/address.d.ts.map +1 -1
  4. package/browser/block.d.ts.map +1 -1
  5. package/browser/branded.d.ts +3 -14
  6. package/browser/branded.d.ts.map +1 -1
  7. package/browser/chunks/psbt-parallel-BBFlkmiv.js +10717 -0
  8. package/browser/crypto.d.ts +1 -1
  9. package/browser/ecc/context.d.ts +25 -24
  10. package/browser/ecc/context.d.ts.map +1 -1
  11. package/browser/ecc/index.d.ts +1 -1
  12. package/browser/ecc/index.d.ts.map +1 -1
  13. package/browser/ecc/types.d.ts +10 -123
  14. package/browser/ecc/types.d.ts.map +1 -1
  15. package/browser/env.d.ts +13 -0
  16. package/browser/env.d.ts.map +1 -0
  17. package/browser/index.d.ts +7 -7
  18. package/browser/index.d.ts.map +1 -1
  19. package/browser/index.js +2497 -11686
  20. package/browser/io/BinaryReader.d.ts +15 -15
  21. package/browser/io/BinaryReader.d.ts.map +1 -1
  22. package/browser/io/BinaryWriter.d.ts +17 -17
  23. package/browser/io/BinaryWriter.d.ts.map +1 -1
  24. package/browser/io/hex.d.ts.map +1 -1
  25. package/browser/io/index.d.ts +0 -1
  26. package/browser/io/index.d.ts.map +1 -1
  27. package/browser/opcodes.d.ts +11 -0
  28. package/browser/opcodes.d.ts.map +1 -1
  29. package/browser/payments/bip341.d.ts +1 -1
  30. package/browser/payments/bip341.d.ts.map +1 -1
  31. package/browser/payments/embed.d.ts +1 -1
  32. package/browser/payments/embed.d.ts.map +1 -1
  33. package/browser/payments/p2ms.d.ts.map +1 -1
  34. package/browser/payments/p2op.d.ts +1 -1
  35. package/browser/payments/p2op.d.ts.map +1 -1
  36. package/browser/payments/p2pk.d.ts +1 -1
  37. package/browser/payments/p2pk.d.ts.map +1 -1
  38. package/browser/payments/p2pkh.d.ts +1 -1
  39. package/browser/payments/p2pkh.d.ts.map +1 -1
  40. package/browser/payments/p2sh.d.ts.map +1 -1
  41. package/browser/payments/p2tr.d.ts +2 -2
  42. package/browser/payments/p2tr.d.ts.map +1 -1
  43. package/browser/payments/p2wpkh.d.ts +1 -1
  44. package/browser/payments/p2wpkh.d.ts.map +1 -1
  45. package/browser/payments/p2wsh.d.ts.map +1 -1
  46. package/browser/payments/types.d.ts +1 -1
  47. package/browser/payments/types.d.ts.map +1 -1
  48. package/browser/psbt/PsbtCache.d.ts +54 -0
  49. package/browser/psbt/PsbtCache.d.ts.map +1 -0
  50. package/browser/psbt/PsbtFinalizer.d.ts +21 -0
  51. package/browser/psbt/PsbtFinalizer.d.ts.map +1 -0
  52. package/browser/psbt/PsbtSigner.d.ts +32 -0
  53. package/browser/psbt/PsbtSigner.d.ts.map +1 -0
  54. package/browser/psbt/PsbtTransaction.d.ts +25 -0
  55. package/browser/psbt/PsbtTransaction.d.ts.map +1 -0
  56. package/browser/psbt/bip371.d.ts.map +1 -1
  57. package/browser/psbt/types.d.ts +5 -71
  58. package/browser/psbt/types.d.ts.map +1 -1
  59. package/browser/psbt/validation.d.ts +1 -1
  60. package/browser/psbt/validation.d.ts.map +1 -1
  61. package/browser/psbt.d.ts +26 -40
  62. package/browser/psbt.d.ts.map +1 -1
  63. package/browser/script.d.ts.map +1 -1
  64. package/browser/transaction.d.ts +4 -4
  65. package/browser/transaction.d.ts.map +1 -1
  66. package/browser/types.d.ts +5 -3
  67. package/browser/types.d.ts.map +1 -1
  68. package/browser/workers/WorkerSigningPool.d.ts +24 -17
  69. package/browser/workers/WorkerSigningPool.d.ts.map +1 -1
  70. package/browser/workers/WorkerSigningPool.node.d.ts +19 -12
  71. package/browser/workers/WorkerSigningPool.node.d.ts.map +1 -1
  72. package/browser/workers/WorkerSigningPool.sequential.d.ts +67 -0
  73. package/browser/workers/WorkerSigningPool.sequential.d.ts.map +1 -0
  74. package/browser/workers/WorkerSigningPool.worklet.d.ts +64 -0
  75. package/browser/workers/WorkerSigningPool.worklet.d.ts.map +1 -0
  76. package/browser/workers/index.browser.d.ts +16 -0
  77. package/browser/workers/index.browser.d.ts.map +1 -0
  78. package/browser/workers/index.d.ts +4 -64
  79. package/browser/workers/index.d.ts.map +1 -1
  80. package/browser/workers/index.js +28 -0
  81. package/browser/workers/index.node.d.ts +17 -0
  82. package/browser/workers/index.node.d.ts.map +1 -0
  83. package/browser/workers/index.react-native.d.ts +28 -0
  84. package/browser/workers/index.react-native.d.ts.map +1 -0
  85. package/browser/workers/index.shared.d.ts +15 -0
  86. package/browser/workers/index.shared.d.ts.map +1 -0
  87. package/browser/workers/psbt-parallel.d.ts +2 -3
  88. package/browser/workers/psbt-parallel.d.ts.map +1 -1
  89. package/browser/workers/types.d.ts +17 -0
  90. package/browser/workers/types.d.ts.map +1 -1
  91. package/build/address.d.ts +6 -2
  92. package/build/address.d.ts.map +1 -1
  93. package/build/address.js +32 -19
  94. package/build/address.js.map +1 -1
  95. package/build/bech32utils.js.map +1 -1
  96. package/build/block.d.ts.map +1 -1
  97. package/build/block.js +2 -4
  98. package/build/block.js.map +1 -1
  99. package/build/branded.d.ts +3 -14
  100. package/build/branded.d.ts.map +1 -1
  101. package/build/branded.js +0 -5
  102. package/build/branded.js.map +1 -1
  103. package/build/crypto.d.ts +1 -1
  104. package/build/ecc/context.d.ts +25 -24
  105. package/build/ecc/context.d.ts.map +1 -1
  106. package/build/ecc/context.js +29 -101
  107. package/build/ecc/context.js.map +1 -1
  108. package/build/ecc/index.d.ts +1 -1
  109. package/build/ecc/index.d.ts.map +1 -1
  110. package/build/ecc/types.d.ts +7 -126
  111. package/build/ecc/types.d.ts.map +1 -1
  112. package/build/ecc/types.js +4 -1
  113. package/build/ecc/types.js.map +1 -1
  114. package/build/env.d.ts +13 -0
  115. package/build/env.d.ts.map +1 -0
  116. package/build/env.js +198 -0
  117. package/build/env.js.map +1 -0
  118. package/build/index.d.ts +8 -7
  119. package/build/index.d.ts.map +1 -1
  120. package/build/index.js +9 -7
  121. package/build/index.js.map +1 -1
  122. package/build/io/BinaryReader.d.ts +15 -15
  123. package/build/io/BinaryReader.d.ts.map +1 -1
  124. package/build/io/BinaryReader.js +17 -17
  125. package/build/io/BinaryReader.js.map +1 -1
  126. package/build/io/BinaryWriter.d.ts +17 -17
  127. package/build/io/BinaryWriter.d.ts.map +1 -1
  128. package/build/io/BinaryWriter.js +39 -39
  129. package/build/io/BinaryWriter.js.map +1 -1
  130. package/build/io/hex.d.ts.map +1 -1
  131. package/build/io/hex.js +2 -1
  132. package/build/io/hex.js.map +1 -1
  133. package/build/io/index.d.ts +0 -1
  134. package/build/io/index.d.ts.map +1 -1
  135. package/build/io/index.js +0 -2
  136. package/build/io/index.js.map +1 -1
  137. package/build/opcodes.d.ts +11 -0
  138. package/build/opcodes.d.ts.map +1 -1
  139. package/build/opcodes.js +19 -4
  140. package/build/opcodes.js.map +1 -1
  141. package/build/payments/bip341.d.ts +1 -2
  142. package/build/payments/bip341.d.ts.map +1 -1
  143. package/build/payments/bip341.js +1 -2
  144. package/build/payments/bip341.js.map +1 -1
  145. package/build/payments/embed.d.ts +1 -1
  146. package/build/payments/embed.d.ts.map +1 -1
  147. package/build/payments/embed.js +14 -14
  148. package/build/payments/embed.js.map +1 -1
  149. package/build/payments/p2ms.d.ts.map +1 -1
  150. package/build/payments/p2ms.js +21 -21
  151. package/build/payments/p2ms.js.map +1 -1
  152. package/build/payments/p2op.d.ts +1 -1
  153. package/build/payments/p2op.d.ts.map +1 -1
  154. package/build/payments/p2op.js +18 -18
  155. package/build/payments/p2op.js.map +1 -1
  156. package/build/payments/p2pk.d.ts +1 -1
  157. package/build/payments/p2pk.d.ts.map +1 -1
  158. package/build/payments/p2pk.js +17 -17
  159. package/build/payments/p2pk.js.map +1 -1
  160. package/build/payments/p2pkh.d.ts +1 -1
  161. package/build/payments/p2pkh.d.ts.map +1 -1
  162. package/build/payments/p2pkh.js +20 -20
  163. package/build/payments/p2pkh.js.map +1 -1
  164. package/build/payments/p2sh.d.ts.map +1 -1
  165. package/build/payments/p2sh.js +22 -20
  166. package/build/payments/p2sh.js.map +1 -1
  167. package/build/payments/p2tr.d.ts +2 -2
  168. package/build/payments/p2tr.d.ts.map +1 -1
  169. package/build/payments/p2tr.js +25 -26
  170. package/build/payments/p2tr.js.map +1 -1
  171. package/build/payments/p2wpkh.d.ts +1 -1
  172. package/build/payments/p2wpkh.d.ts.map +1 -1
  173. package/build/payments/p2wpkh.js +20 -20
  174. package/build/payments/p2wpkh.js.map +1 -1
  175. package/build/payments/p2wsh.d.ts.map +1 -1
  176. package/build/payments/p2wsh.js +22 -22
  177. package/build/payments/p2wsh.js.map +1 -1
  178. package/build/payments/types.d.ts +1 -1
  179. package/build/payments/types.d.ts.map +1 -1
  180. package/build/psbt/PsbtCache.d.ts +54 -0
  181. package/build/psbt/PsbtCache.d.ts.map +1 -0
  182. package/build/psbt/PsbtCache.js +249 -0
  183. package/build/psbt/PsbtCache.js.map +1 -0
  184. package/build/psbt/PsbtFinalizer.d.ts +21 -0
  185. package/build/psbt/PsbtFinalizer.d.ts.map +1 -0
  186. package/build/psbt/PsbtFinalizer.js +157 -0
  187. package/build/psbt/PsbtFinalizer.js.map +1 -0
  188. package/build/psbt/PsbtSigner.d.ts +32 -0
  189. package/build/psbt/PsbtSigner.d.ts.map +1 -0
  190. package/build/psbt/PsbtSigner.js +192 -0
  191. package/build/psbt/PsbtSigner.js.map +1 -0
  192. package/build/psbt/PsbtTransaction.d.ts +25 -0
  193. package/build/psbt/PsbtTransaction.d.ts.map +1 -0
  194. package/build/psbt/PsbtTransaction.js +61 -0
  195. package/build/psbt/PsbtTransaction.js.map +1 -0
  196. package/build/psbt/bip371.d.ts.map +1 -1
  197. package/build/psbt/bip371.js +6 -2
  198. package/build/psbt/bip371.js.map +1 -1
  199. package/build/psbt/psbtutils.js +1 -1
  200. package/build/psbt/psbtutils.js.map +1 -1
  201. package/build/psbt/types.d.ts +5 -71
  202. package/build/psbt/types.d.ts.map +1 -1
  203. package/build/psbt/validation.d.ts +1 -1
  204. package/build/psbt/validation.d.ts.map +1 -1
  205. package/build/psbt/validation.js +1 -1
  206. package/build/psbt/validation.js.map +1 -1
  207. package/build/psbt.d.ts +26 -40
  208. package/build/psbt.d.ts.map +1 -1
  209. package/build/psbt.js +180 -808
  210. package/build/psbt.js.map +1 -1
  211. package/build/script.d.ts.map +1 -1
  212. package/build/script.js +4 -4
  213. package/build/script.js.map +1 -1
  214. package/build/transaction.d.ts +4 -4
  215. package/build/transaction.d.ts.map +1 -1
  216. package/build/transaction.js +6 -5
  217. package/build/transaction.js.map +1 -1
  218. package/build/tsconfig.build.tsbuildinfo +1 -1
  219. package/build/types.d.ts +5 -3
  220. package/build/types.d.ts.map +1 -1
  221. package/build/types.js +14 -25
  222. package/build/types.js.map +1 -1
  223. package/build/workers/WorkerSigningPool.d.ts +24 -17
  224. package/build/workers/WorkerSigningPool.d.ts.map +1 -1
  225. package/build/workers/WorkerSigningPool.js +36 -25
  226. package/build/workers/WorkerSigningPool.js.map +1 -1
  227. package/build/workers/WorkerSigningPool.node.d.ts +19 -12
  228. package/build/workers/WorkerSigningPool.node.d.ts.map +1 -1
  229. package/build/workers/WorkerSigningPool.node.js +60 -28
  230. package/build/workers/WorkerSigningPool.node.js.map +1 -1
  231. package/build/workers/WorkerSigningPool.sequential.d.ts +76 -0
  232. package/build/workers/WorkerSigningPool.sequential.d.ts.map +1 -0
  233. package/build/workers/WorkerSigningPool.sequential.js +160 -0
  234. package/build/workers/WorkerSigningPool.sequential.js.map +1 -0
  235. package/build/workers/WorkerSigningPool.worklet.d.ts +79 -0
  236. package/build/workers/WorkerSigningPool.worklet.d.ts.map +1 -0
  237. package/build/workers/WorkerSigningPool.worklet.js +390 -0
  238. package/build/workers/WorkerSigningPool.worklet.js.map +1 -0
  239. package/build/workers/index.browser.d.ts +24 -0
  240. package/build/workers/index.browser.d.ts.map +1 -0
  241. package/build/workers/index.browser.js +30 -0
  242. package/build/workers/index.browser.js.map +1 -0
  243. package/build/workers/index.d.ts +6 -18
  244. package/build/workers/index.d.ts.map +1 -1
  245. package/build/workers/index.js +12 -14
  246. package/build/workers/index.js.map +1 -1
  247. package/build/workers/index.node.d.ts +38 -0
  248. package/build/workers/index.node.d.ts.map +1 -0
  249. package/build/workers/index.node.js +45 -0
  250. package/build/workers/index.node.js.map +1 -0
  251. package/build/workers/index.react-native.d.ts +28 -0
  252. package/build/workers/index.react-native.d.ts.map +1 -0
  253. package/build/workers/index.react-native.js +67 -0
  254. package/build/workers/index.react-native.js.map +1 -0
  255. package/build/workers/index.shared.d.ts +15 -0
  256. package/build/workers/index.shared.d.ts.map +1 -0
  257. package/build/workers/index.shared.js +20 -0
  258. package/build/workers/index.shared.js.map +1 -0
  259. package/build/workers/psbt-parallel.d.ts +2 -3
  260. package/build/workers/psbt-parallel.d.ts.map +1 -1
  261. package/build/workers/psbt-parallel.js +4 -4
  262. package/build/workers/psbt-parallel.js.map +1 -1
  263. package/build/workers/types.d.ts +17 -0
  264. package/build/workers/types.d.ts.map +1 -1
  265. package/build/workers/types.js.map +1 -1
  266. package/package.json +48 -9
  267. package/src/address.ts +53 -21
  268. package/src/bech32utils.ts +3 -3
  269. package/src/block.ts +17 -10
  270. package/src/branded.ts +15 -13
  271. package/src/crypto.ts +1 -1
  272. package/src/ecc/context.ts +36 -136
  273. package/src/ecc/index.ts +2 -2
  274. package/src/ecc/types.ts +7 -145
  275. package/src/env.ts +239 -0
  276. package/src/index.ts +57 -22
  277. package/src/io/BinaryReader.ts +18 -18
  278. package/src/io/BinaryWriter.ts +43 -43
  279. package/src/io/hex.ts +2 -1
  280. package/src/io/index.ts +0 -3
  281. package/src/opcodes.ts +21 -4
  282. package/src/payments/bip341.ts +5 -7
  283. package/src/payments/embed.ts +19 -19
  284. package/src/payments/p2ms.ts +34 -27
  285. package/src/payments/p2op.ts +22 -22
  286. package/src/payments/p2pk.ts +22 -22
  287. package/src/payments/p2pkh.ts +28 -28
  288. package/src/payments/p2sh.ts +33 -30
  289. package/src/payments/p2tr.ts +40 -40
  290. package/src/payments/p2wpkh.ts +30 -30
  291. package/src/payments/p2wsh.ts +29 -29
  292. package/src/payments/types.ts +1 -1
  293. package/src/psbt/PsbtCache.ts +325 -0
  294. package/src/psbt/PsbtFinalizer.ts +213 -0
  295. package/src/psbt/PsbtSigner.ts +302 -0
  296. package/src/psbt/PsbtTransaction.ts +82 -0
  297. package/src/psbt/bip371.ts +7 -3
  298. package/src/psbt/psbtutils.ts +1 -1
  299. package/src/psbt/types.ts +5 -94
  300. package/src/psbt/validation.ts +5 -12
  301. package/src/psbt.ts +376 -1201
  302. package/src/script.ts +6 -9
  303. package/src/transaction.ts +19 -15
  304. package/src/types.ts +33 -45
  305. package/src/workers/WorkerSigningPool.node.ts +72 -36
  306. package/src/workers/WorkerSigningPool.sequential.ts +191 -0
  307. package/src/workers/WorkerSigningPool.ts +48 -39
  308. package/src/workers/WorkerSigningPool.worklet.ts +522 -0
  309. package/src/workers/index.browser.ts +34 -0
  310. package/src/workers/index.node.ts +50 -0
  311. package/src/workers/index.react-native.ts +110 -0
  312. package/src/workers/index.shared.ts +58 -0
  313. package/src/workers/index.ts +14 -65
  314. package/src/workers/psbt-parallel.ts +8 -13
  315. package/src/workers/types.ts +26 -1
  316. package/test/address.spec.ts +2 -2
  317. package/test/bitcoin.core.spec.ts +5 -2
  318. package/test/browser/payments.spec.ts +151 -0
  319. package/test/browser/psbt.spec.ts +1510 -0
  320. package/test/browser/script.spec.ts +223 -0
  321. package/test/browser/setup.ts +13 -0
  322. package/test/browser/workers-signing.spec.ts +537 -0
  323. package/test/crypto.spec.ts +2 -2
  324. package/test/env.spec.ts +418 -0
  325. package/test/fixtures/core/base58_encode_decode.json +12 -48
  326. package/test/fixtures/core/base58_keys_invalid.json +50 -150
  327. package/test/fixtures/core/sighash.json +1 -3
  328. package/test/fixtures/core/tx_valid.json +133 -501
  329. package/test/fixtures/embed.json +3 -11
  330. package/test/fixtures/p2ms.json +21 -91
  331. package/test/fixtures/p2pk.json +5 -24
  332. package/test/fixtures/p2pkh.json +7 -36
  333. package/test/fixtures/p2sh.json +8 -54
  334. package/test/fixtures/p2tr.json +2 -6
  335. package/test/fixtures/p2wpkh.json +7 -36
  336. package/test/fixtures/p2wsh.json +14 -59
  337. package/test/fixtures/psbt.json +2 -6
  338. package/test/fixtures/script.json +12 -48
  339. package/test/integration/addresses.spec.ts +11 -5
  340. package/test/integration/bip32.spec.ts +1 -1
  341. package/test/integration/cltv.spec.ts +10 -6
  342. package/test/integration/csv.spec.ts +10 -9
  343. package/test/integration/payments.spec.ts +8 -4
  344. package/test/integration/taproot.spec.ts +26 -6
  345. package/test/integration/transactions.spec.ts +22 -8
  346. package/test/payments.spec.ts +1 -1
  347. package/test/payments.utils.ts +1 -1
  348. package/test/psbt.spec.ts +250 -64
  349. package/test/script_signature.spec.ts +1 -1
  350. package/test/transaction.spec.ts +18 -5
  351. package/test/tsconfig.json +6 -20
  352. package/test/workers-pool.spec.ts +65 -23
  353. package/test/workers-sequential.spec.ts +669 -0
  354. package/test/workers-signing.spec.ts +7 -3
  355. package/test/workers-worklet.spec.ts +500 -0
  356. package/test/workers.spec.ts +6 -7
  357. package/typedoc.json +39 -0
  358. package/vite.config.browser.ts +31 -6
  359. package/vitest.config.browser.ts +68 -0
  360. package/browser/ecpair.d.ts +0 -99
  361. package/browser/io/MemoryPool.d.ts +0 -220
  362. package/browser/io/MemoryPool.d.ts.map +0 -1
  363. package/build/io/MemoryPool.d.ts +0 -220
  364. package/build/io/MemoryPool.d.ts.map +0 -1
  365. package/build/io/MemoryPool.js +0 -309
  366. package/build/io/MemoryPool.js.map +0 -1
  367. package/src/ecpair.d.ts +0 -99
  368. package/src/io/MemoryPool.ts +0 -343
  369. package/test/taproot-cache.spec.ts +0 -694
package/src/ecc/types.ts CHANGED
@@ -1,154 +1,16 @@
1
1
  /**
2
2
  * ECC (Elliptic Curve Cryptography) type definitions.
3
- * Defines interfaces for secp256k1 operations used in Taproot and signatures.
3
+ *
4
+ * Re-exports {@link CryptoBackend} from `@btc-vision/ecpair` as the canonical
5
+ * interface for secp256k1 operations. The legacy `EccLib` name is kept as a
6
+ * type alias for backward compatibility.
4
7
  *
5
8
  * @packageDocumentation
6
9
  */
7
10
 
8
- import type {
9
- Bytes32,
10
- PrivateKey,
11
- PublicKey,
12
- Signature,
13
- SchnorrSignature,
14
- XOnlyPublicKey,
15
- } from '../branded.js';
16
-
17
- /**
18
- * Parity of the y-coordinate for an x-only public key.
19
- * - 0: even y-coordinate
20
- * - 1: odd y-coordinate
21
- */
22
- export type Parity = 0 | 1;
23
-
24
- /**
25
- * Result of x-only point addition with tweak.
26
- */
27
- export interface XOnlyPointAddTweakResult {
28
- /** Parity of the resulting y-coordinate (0 = even, 1 = odd) */
29
- readonly parity: Parity;
30
- /** The resulting x-only public key */
31
- readonly xOnlyPubkey: XOnlyPublicKey;
32
- }
11
+ export type { CryptoBackend, XOnlyPointAddTweakResult, Parity } from '@btc-vision/ecpair';
33
12
 
34
13
  /**
35
- * Interface for the ECC library used by this library.
36
- * This is compatible with tiny-secp256k1 and @noble/secp256k1.
37
- *
38
- * @example
39
- * ```typescript
40
- * import { EccLib, initEccLib } from '@btc-vision/bitcoin';
41
- * import * as secp256k1 from 'tiny-secp256k1';
42
- *
43
- * // tiny-secp256k1 implements EccLib
44
- * const ecc: EccLib = secp256k1;
45
- * initEccLib(ecc);
46
- * ```
14
+ * @deprecated Use {@link CryptoBackend} from `@btc-vision/ecpair` instead.
47
15
  */
48
- export interface EccLib {
49
- /**
50
- * Checks if a 32-byte value is a valid x-only public key.
51
- *
52
- * @param p - 32-byte x-coordinate
53
- * @returns True if the point is valid on the secp256k1 curve
54
- */
55
- isXOnlyPoint(p: Uint8Array): boolean;
56
-
57
- /**
58
- * Adds a tweak to an x-only public key.
59
- *
60
- * @param p - 32-byte x-only public key
61
- * @param tweak - 32-byte scalar to add
62
- * @returns The tweaked public key with parity, or null if result is invalid
63
- */
64
- xOnlyPointAddTweak(p: XOnlyPublicKey, tweak: Bytes32): XOnlyPointAddTweakResult | null;
65
-
66
- /**
67
- * Signs a 32-byte message hash with a private key (ECDSA).
68
- * Optional - only needed for signing operations.
69
- *
70
- * @param hash - 32-byte message hash
71
- * @param privateKey - 32-byte private key
72
- * @returns DER-encoded signature
73
- */
74
- sign?(hash: Bytes32, privateKey: PrivateKey): Signature;
75
-
76
- /**
77
- * Signs a 32-byte message hash with a private key (Schnorr/BIP340).
78
- * Optional - only needed for Taproot key-path signing.
79
- *
80
- * @param hash - 32-byte message hash
81
- * @param privateKey - 32-byte private key
82
- * @returns 64-byte Schnorr signature
83
- */
84
- signSchnorr?(hash: Bytes32, privateKey: PrivateKey): SchnorrSignature;
85
-
86
- /**
87
- * Verifies an ECDSA signature.
88
- * Optional - only needed for signature verification.
89
- *
90
- * @param hash - 32-byte message hash
91
- * @param publicKey - 33 or 65-byte public key
92
- * @param signature - DER-encoded signature
93
- * @returns True if signature is valid
94
- */
95
- verify?(hash: Bytes32, publicKey: PublicKey, signature: Signature): boolean;
96
-
97
- /**
98
- * Verifies a Schnorr/BIP340 signature.
99
- * Optional - only needed for Taproot signature verification.
100
- *
101
- * @param hash - 32-byte message hash
102
- * @param publicKey - 32-byte x-only public key
103
- * @param signature - 64-byte Schnorr signature
104
- * @returns True if signature is valid
105
- */
106
- verifySchnorr?(hash: Bytes32, publicKey: XOnlyPublicKey, signature: SchnorrSignature): boolean;
107
-
108
- /**
109
- * Derives a public key from a private key.
110
- * Optional - only needed for key derivation.
111
- *
112
- * @param privateKey - 32-byte private key
113
- * @param compressed - Whether to return compressed (33-byte) or uncompressed (65-byte)
114
- * @returns The public key, or null if private key is invalid
115
- */
116
- pointFromScalar?(privateKey: PrivateKey, compressed?: boolean): PublicKey | null;
117
-
118
- /**
119
- * Computes the x-only public key from a private key.
120
- * Optional - only needed for Taproot key derivation.
121
- *
122
- * @param privateKey - 32-byte private key
123
- * @returns 32-byte x-only public key, or null if private key is invalid
124
- */
125
- xOnlyPointFromScalar?(privateKey: PrivateKey): XOnlyPublicKey | null;
126
-
127
- /**
128
- * Converts a full public key to x-only format.
129
- * Optional - only needed when working with x-only keys.
130
- *
131
- * @param pubkey - 33 or 65-byte public key
132
- * @returns 32-byte x-only public key
133
- */
134
- xOnlyPointFromPoint?(pubkey: PublicKey): XOnlyPublicKey;
135
-
136
- /**
137
- * Adds a scalar to a private key.
138
- * Optional - only needed for key tweaking.
139
- *
140
- * @param privateKey - 32-byte private key
141
- * @param tweak - 32-byte scalar to add
142
- * @returns The tweaked private key, or null if result is invalid
143
- */
144
- privateAdd?(privateKey: PrivateKey, tweak: Bytes32): PrivateKey | null;
145
-
146
- /**
147
- * Negates a private key.
148
- * Optional - only needed for Taproot parity handling.
149
- *
150
- * @param privateKey - 32-byte private key
151
- * @returns The negated private key
152
- */
153
- privateNegate?(privateKey: PrivateKey): PrivateKey;
154
- }
16
+ export type { CryptoBackend as EccLib } from '@btc-vision/ecpair';
package/src/env.ts ADDED
@@ -0,0 +1,239 @@
1
+ /**
2
+ * Runtime capability verification and polyfills.
3
+ *
4
+ * Polyfills features that can be shimmed (TextEncoder, Map, Promise.allSettled, Symbol.dispose).
5
+ * Throws early with a clear error for features that cannot be polyfilled (BigInt, Uint8Array,
6
+ * structuredClone, DataView BigInt methods).
7
+ *
8
+ * Imported as a side effect by the main entry point.
9
+ *
10
+ * @packageDocumentation
11
+ */
12
+
13
+ // ---------------------------------------------------------------------------
14
+ // Hard requirements — cannot be polyfilled
15
+ // ---------------------------------------------------------------------------
16
+
17
+ const missing: string[] = [];
18
+
19
+ if (typeof BigInt === 'undefined') {
20
+ missing.push('BigInt (ES2020 — required for satoshi values)');
21
+ }
22
+
23
+ if (typeof Uint8Array === 'undefined') {
24
+ missing.push('Uint8Array (TypedArrays — required for all binary operations)');
25
+ }
26
+
27
+ if (typeof DataView !== 'undefined' && typeof DataView.prototype.getBigInt64 !== 'function') {
28
+ missing.push('DataView.prototype.getBigInt64 (ES2020 — required for transaction parsing)');
29
+ }
30
+
31
+ if (missing.length > 0) {
32
+ throw new Error(
33
+ `@btc-vision/bitcoin: unsupported runtime. Missing capabilities:\n` +
34
+ missing.map((cap) => ` - ${cap}`).join('\n') +
35
+ `\n\nThese features cannot be polyfilled. This library requires a modern JavaScript engine (ES2022+).`,
36
+ );
37
+ }
38
+
39
+ // ---------------------------------------------------------------------------
40
+ // Polyfills — shim features that can be reasonably implemented
41
+ // ---------------------------------------------------------------------------
42
+
43
+ // TextEncoder / TextDecoder (for older WebView / jsdom environments)
44
+ if (typeof globalThis.TextEncoder === 'undefined') {
45
+ globalThis.TextEncoder = class TextEncoder {
46
+ readonly encoding = 'utf-8';
47
+
48
+ encode(input: string = ''): Uint8Array {
49
+ // Fast path for ASCII
50
+ const utf8: number[] = [];
51
+ for (let i = 0; i < input.length; i++) {
52
+ let code = input.charCodeAt(i);
53
+ if (code < 0x80) {
54
+ utf8.push(code);
55
+ } else if (code < 0x800) {
56
+ utf8.push(0xc0 | (code >> 6), 0x80 | (code & 0x3f));
57
+ } else if (code >= 0xd800 && code <= 0xdbff) {
58
+ // Surrogate pair
59
+ const next = input.charCodeAt(++i);
60
+ code = 0x10000 + ((code - 0xd800) << 10) + (next - 0xdc00);
61
+ utf8.push(
62
+ 0xf0 | (code >> 18),
63
+ 0x80 | ((code >> 12) & 0x3f),
64
+ 0x80 | ((code >> 6) & 0x3f),
65
+ 0x80 | (code & 0x3f),
66
+ );
67
+ } else {
68
+ utf8.push(0xe0 | (code >> 12), 0x80 | ((code >> 6) & 0x3f), 0x80 | (code & 0x3f));
69
+ }
70
+ }
71
+ return new Uint8Array(utf8);
72
+ }
73
+
74
+ encodeInto(src: string, dest: Uint8Array): { read: number; written: number } {
75
+ const encoded = this.encode(src);
76
+ const written = Math.min(encoded.length, dest.length);
77
+ dest.set(encoded.subarray(0, written));
78
+ return { read: src.length, written };
79
+ }
80
+ } as unknown as typeof TextEncoder;
81
+ }
82
+
83
+ if (typeof globalThis.TextDecoder === 'undefined') {
84
+ globalThis.TextDecoder = class TextDecoder {
85
+ readonly encoding = 'utf-8';
86
+ readonly fatal = false;
87
+ readonly ignoreBOM = false;
88
+
89
+ decode(input?: ArrayBufferView | ArrayBuffer): string {
90
+ if (!input) return '';
91
+ const bytes =
92
+ input instanceof ArrayBuffer
93
+ ? new Uint8Array(input)
94
+ : new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
95
+
96
+ let result = '';
97
+ for (let i = 0; i < bytes.length; ) {
98
+ const byte = bytes[i]!;
99
+ let code: number;
100
+ if (byte < 0x80) {
101
+ code = byte;
102
+ i++;
103
+ } else if ((byte & 0xe0) === 0xc0) {
104
+ code = ((byte & 0x1f) << 6) | (bytes[i + 1]! & 0x3f);
105
+ i += 2;
106
+ } else if ((byte & 0xf0) === 0xe0) {
107
+ code = ((byte & 0x0f) << 12) | ((bytes[i + 1]! & 0x3f) << 6) | (bytes[i + 2]! & 0x3f);
108
+ i += 3;
109
+ } else {
110
+ code =
111
+ ((byte & 0x07) << 18) |
112
+ ((bytes[i + 1]! & 0x3f) << 12) |
113
+ ((bytes[i + 2]! & 0x3f) << 6) |
114
+ (bytes[i + 3]! & 0x3f);
115
+ i += 4;
116
+ }
117
+
118
+ if (code <= 0xffff) {
119
+ result += String.fromCharCode(code);
120
+ } else {
121
+ // Surrogate pair
122
+ code -= 0x10000;
123
+ result += String.fromCharCode(0xd800 + (code >> 10), 0xdc00 + (code & 0x3ff));
124
+ }
125
+ }
126
+ return result;
127
+ }
128
+ } as unknown as typeof TextDecoder;
129
+ }
130
+
131
+ // Map (for extremely old environments)
132
+ if (typeof globalThis.Map === 'undefined') {
133
+ globalThis.Map = class Map<K, V> {
134
+ readonly #entries: Array<[K, V]> = [];
135
+
136
+ constructor(entries?: Iterable<[K, V]> | null) {
137
+ if (entries) {
138
+ for (const [k, v] of entries) {
139
+ this.set(k, v);
140
+ }
141
+ }
142
+ }
143
+
144
+ get size(): number {
145
+ return this.#entries.length;
146
+ }
147
+
148
+ has(key: K): boolean {
149
+ return this.#entries.some(([k]) => k === key);
150
+ }
151
+
152
+ get(key: K): V | undefined {
153
+ const entry = this.#entries.find(([k]) => k === key);
154
+ return entry?.[1];
155
+ }
156
+
157
+ set(key: K, value: V): this {
158
+ const idx = this.#entries.findIndex(([k]) => k === key);
159
+ if (idx >= 0) {
160
+ this.#entries[idx] = [key, value];
161
+ } else {
162
+ this.#entries.push([key, value]);
163
+ }
164
+ return this;
165
+ }
166
+
167
+ delete(key: K): boolean {
168
+ const idx = this.#entries.findIndex(([k]) => k === key);
169
+ if (idx >= 0) {
170
+ this.#entries.splice(idx, 1);
171
+ return true;
172
+ }
173
+ return false;
174
+ }
175
+
176
+ clear(): void {
177
+ this.#entries.length = 0;
178
+ }
179
+
180
+ forEach(cb: (value: V, key: K, map: Map<K, V>) => void): void {
181
+ for (const [k, v] of this.#entries) {
182
+ cb(v, k, this);
183
+ }
184
+ }
185
+
186
+ keys(): IterableIterator<K> {
187
+ return this.#entries.map(([k]) => k)[Symbol.iterator]() as IterableIterator<K>;
188
+ }
189
+
190
+ values(): IterableIterator<V> {
191
+ return this.#entries.map(([, v]) => v)[Symbol.iterator]() as IterableIterator<V>;
192
+ }
193
+
194
+ entries(): IterableIterator<[K, V]> {
195
+ return this.#entries[Symbol.iterator]() as IterableIterator<[K, V]>;
196
+ }
197
+
198
+ [Symbol.iterator](): IterableIterator<[K, V]> {
199
+ return this.entries();
200
+ }
201
+
202
+ get [Symbol.toStringTag](): string {
203
+ return 'Map';
204
+ }
205
+ } as unknown as MapConstructor;
206
+ }
207
+
208
+ // Promise.allSettled (ES2020)
209
+ if (typeof Promise.allSettled !== 'function') {
210
+ Promise.allSettled = function allSettled<T extends readonly unknown[]>(
211
+ promises: T,
212
+ ): Promise<{
213
+ -readonly [K in keyof T]: PromiseSettledResult<Awaited<T[K]>>;
214
+ }> {
215
+ return Promise.all(
216
+ Array.from(promises as Iterable<unknown>, (p) =>
217
+ Promise.resolve(p).then(
218
+ (value) => ({ status: 'fulfilled' as const, value }),
219
+ (reason: unknown) => ({ status: 'rejected' as const, reason }),
220
+ ),
221
+ ),
222
+ ) as Promise<{
223
+ -readonly [K in keyof T]: PromiseSettledResult<Awaited<T[K]>>;
224
+ }>;
225
+ };
226
+ }
227
+
228
+ // structuredClone (ES2022 — used for PSBT cloning)
229
+ if (typeof globalThis.structuredClone === 'undefined') {
230
+ globalThis.structuredClone = <T>(value: T): T => JSON.parse(JSON.stringify(value)) as T;
231
+ }
232
+
233
+ // Symbol.dispose / Symbol.asyncDispose (ES2024 — Explicit Resource Management)
234
+ if (typeof Symbol['dispose'] === 'undefined') {
235
+ (Symbol as unknown as Record<string, symbol>)['dispose'] = Symbol.for('Symbol.dispose');
236
+ }
237
+ if (typeof Symbol['asyncDispose'] === 'undefined') {
238
+ (Symbol as unknown as Record<string, symbol>)['asyncDispose'] = Symbol.for('Symbol.asyncDispose');
239
+ }
package/src/index.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import './env.js';
1
2
  import type {
2
3
  PsbtInput as _PsbtInput,
3
4
  PsbtInputUpdate as _PsbtInputUpdate,
@@ -17,6 +18,18 @@ import * as payments from './payments/index.js';
17
18
  import * as script from './script.js';
18
19
  import * as crypto from './crypto.js';
19
20
  import * as Transaction from './transaction.js';
21
+ // Custom error types
22
+ import {
23
+ AddressError,
24
+ BitcoinError,
25
+ EccError,
26
+ InvalidInputError,
27
+ InvalidOutputError,
28
+ PsbtError,
29
+ ScriptError,
30
+ SignatureError,
31
+ ValidationError,
32
+ } from './errors.js';
20
33
 
21
34
  export * as address from './address.js';
22
35
  export * as crypto from './crypto.js';
@@ -27,7 +40,41 @@ export * as script from './script.js';
27
40
  export { Block } from './block.js';
28
41
  /** @hidden */
29
42
  export * from './crypto.js';
30
- export * from './psbt.js';
43
+ export {
44
+ Psbt,
45
+ PsbtCache,
46
+ PsbtSigner,
47
+ PsbtFinalizer,
48
+ PsbtTransaction,
49
+ transactionFromBuffer,
50
+ getFinalScripts,
51
+ prepareFinalScripts,
52
+ } from './psbt.js';
53
+ export type {
54
+ TransactionInput,
55
+ PsbtTxInput,
56
+ TransactionOutput,
57
+ PsbtTxOutput,
58
+ ValidateSigFunction,
59
+ PsbtBaseExtended,
60
+ PsbtOptsOptional,
61
+ PsbtOpts,
62
+ PsbtInputExtended,
63
+ PsbtOutputExtended,
64
+ PsbtOutputExtendedScript,
65
+ HDSigner,
66
+ HDSignerAsync,
67
+ Signer,
68
+ SignerAsync,
69
+ TaprootHashCheckSigner,
70
+ PsbtCacheInterface,
71
+ TxCacheNumberKey,
72
+ ScriptType,
73
+ AllScriptType,
74
+ GetScriptReturn,
75
+ FinalScriptsFunc,
76
+ FinalTaprootScriptsFunc,
77
+ } from './psbt.js';
31
78
  /** @hidden */
32
79
  export { opcodes } from './opcodes.js';
33
80
  export { Transaction } from './transaction.js';
@@ -36,7 +83,7 @@ export type { TaprootHashCache } from './transaction.js';
36
83
  export type { Network } from './networks.js';
37
84
  /** @hidden */
38
85
  export { initEccLib, getEccLib, EccContext } from './ecc/context.js';
39
- export type { EccLib } from './ecc/types.js';
86
+ export type { CryptoBackend, EccLib } from './ecc/types.js';
40
87
  export { PaymentType } from './payments/index.js';
41
88
  export type {
42
89
  Payment,
@@ -96,8 +143,6 @@ export {
96
143
  xor,
97
144
  fromUtf8,
98
145
  toUtf8,
99
- MemoryPool,
100
- SimpleMemoryPool,
101
146
  varuint,
102
147
  } from './io/index.js';
103
148
  export * from './payments/bip341.js';
@@ -140,19 +185,6 @@ export {
140
185
  } from './types.js';
141
186
  export type { XOnlyPointAddTweakResult } from './ecc/types.js';
142
187
 
143
- // Custom error types
144
- import {
145
- BitcoinError,
146
- ValidationError,
147
- InvalidInputError,
148
- InvalidOutputError,
149
- ScriptError,
150
- PsbtError,
151
- EccError,
152
- AddressError,
153
- SignatureError,
154
- } from './errors.js';
155
-
156
188
  export {
157
189
  BitcoinError,
158
190
  ValidationError,
@@ -178,23 +210,26 @@ export const errors = {
178
210
  SignatureError,
179
211
  } as const;
180
212
 
181
- // Worker-based parallel signing
182
- export * as workers from './workers/index.js';
213
+ // Worker-based parallel signing (browser-safe shared exports only)
214
+ // For createSigningPool / detectRuntime, use '@btc-vision/bitcoin/workers' subpath export
215
+ // which routes to the correct platform-specific implementation.
183
216
  export {
184
217
  WorkerSigningPool,
185
218
  getSigningPool,
186
219
  SignatureType,
187
- createSigningPool,
188
220
  signPsbtParallel,
189
- } from './workers/index.js';
221
+ prepareSigningTasks,
222
+ applySignaturesToPsbt,
223
+ } from './workers/index.shared.js';
190
224
  export type {
191
225
  WorkerPoolConfig,
192
226
  SigningTask,
227
+ SigningPoolLike,
193
228
  ParallelSignerKeyPair,
194
229
  ParallelSigningResult,
195
230
  ParallelSignOptions,
196
231
  PsbtParallelKeyPair,
197
- } from './workers/index.js';
232
+ } from './workers/index.shared.js';
198
233
 
199
234
  const bitcoin = {
200
235
  networks,
@@ -70,24 +70,6 @@ export class BinaryReader {
70
70
  this.#offset = offset;
71
71
  }
72
72
 
73
- /**
74
- * Creates a BinaryReader from a hex string.
75
- *
76
- * @param hex - Hex string (with or without 0x prefix)
77
- * @returns A new BinaryReader instance
78
- *
79
- * @example
80
- * ```typescript
81
- * import { BinaryReader } from '@btc-vision/bitcoin';
82
- *
83
- * const reader = BinaryReader.fromHex('01000000');
84
- * const version = reader.readInt32LE(); // 1
85
- * ```
86
- */
87
- public static fromHex(hex: string): BinaryReader {
88
- return new BinaryReader(fromHex(hex));
89
- }
90
-
91
73
  /**
92
74
  * Current read position in the buffer.
93
75
  */
@@ -129,6 +111,24 @@ export class BinaryReader {
129
111
  return this.#data;
130
112
  }
131
113
 
114
+ /**
115
+ * Creates a BinaryReader from a hex string.
116
+ *
117
+ * @param hex - Hex string (with or without 0x prefix)
118
+ * @returns A new BinaryReader instance
119
+ *
120
+ * @example
121
+ * ```typescript
122
+ * import { BinaryReader } from '@btc-vision/bitcoin';
123
+ *
124
+ * const reader = BinaryReader.fromHex('01000000');
125
+ * const version = reader.readInt32LE(); // 1
126
+ * ```
127
+ */
128
+ public static fromHex(hex: string): BinaryReader {
129
+ return new BinaryReader(fromHex(hex));
130
+ }
131
+
132
132
  /**
133
133
  * Reads an 8-bit unsigned integer.
134
134
  *
@@ -91,26 +91,6 @@ export class BinaryWriter {
91
91
  this.#view = new DataView(this.#data.buffer, this.#data.byteOffset, this.#data.byteLength);
92
92
  }
93
93
 
94
- /**
95
- * Creates a BinaryWriter with automatic capacity management.
96
- *
97
- * Initial capacity is 256 bytes, grows as needed.
98
- *
99
- * @returns A new GrowableBinaryWriter instance
100
- *
101
- * @example
102
- * ```typescript
103
- * import { BinaryWriter } from '@btc-vision/bitcoin';
104
- *
105
- * const writer = BinaryWriter.growable();
106
- * writer.writeUInt32LE(1);
107
- * writer.writeBytes(new Uint8Array(1000)); // Automatically grows
108
- * ```
109
- */
110
- public static growable(initialCapacity: number = 256): GrowableBinaryWriter {
111
- return new GrowableBinaryWriter(initialCapacity);
112
- }
113
-
114
94
  /**
115
95
  * Current write position in the buffer.
116
96
  */
@@ -152,6 +132,26 @@ export class BinaryWriter {
152
132
  return this.#data;
153
133
  }
154
134
 
135
+ /**
136
+ * Creates a BinaryWriter with automatic capacity management.
137
+ *
138
+ * Initial capacity is 256 bytes, grows as needed.
139
+ *
140
+ * @returns A new GrowableBinaryWriter instance
141
+ *
142
+ * @example
143
+ * ```typescript
144
+ * import { BinaryWriter } from '@btc-vision/bitcoin';
145
+ *
146
+ * const writer = BinaryWriter.growable();
147
+ * writer.writeUInt32LE(1);
148
+ * writer.writeBytes(new Uint8Array(1000)); // Automatically grows
149
+ * ```
150
+ */
151
+ public static growable(initialCapacity: number = 256): GrowableBinaryWriter {
152
+ return new GrowableBinaryWriter(initialCapacity);
153
+ }
154
+
155
155
  /**
156
156
  * Writes an 8-bit unsigned integer.
157
157
  *
@@ -583,29 +583,6 @@ export class GrowableBinaryWriter {
583
583
  return this.#data.length;
584
584
  }
585
585
 
586
- /**
587
- * Ensures the buffer has enough space for additional bytes.
588
- *
589
- * @param additionalBytes - Number of additional bytes needed
590
- */
591
- #ensureCapacity(additionalBytes: number): void {
592
- const required = this.#offset + additionalBytes;
593
- if (required <= this.#data.length) {
594
- return;
595
- }
596
-
597
- // Grow by at least 2x or to required size
598
- let newCapacity = this.#data.length * 2;
599
- while (newCapacity < required) {
600
- newCapacity *= 2;
601
- }
602
-
603
- const newData = new Uint8Array(newCapacity);
604
- newData.set(this.#data.subarray(0, this.#offset));
605
- this.#data = newData;
606
- this.#view = new DataView(this.#data.buffer);
607
- }
608
-
609
586
  public writeUInt8(value: number): this {
610
587
  this.#ensureCapacity(1);
611
588
  this.#data[this.#offset++] = value & 0xff;
@@ -693,4 +670,27 @@ export class GrowableBinaryWriter {
693
670
  public toHex(): string {
694
671
  return toHex(this.finish());
695
672
  }
673
+
674
+ /**
675
+ * Ensures the buffer has enough space for additional bytes.
676
+ *
677
+ * @param additionalBytes - Number of additional bytes needed
678
+ */
679
+ #ensureCapacity(additionalBytes: number): void {
680
+ const required = this.#offset + additionalBytes;
681
+ if (required <= this.#data.length) {
682
+ return;
683
+ }
684
+
685
+ // Grow by at least 2x or to required size
686
+ let newCapacity = this.#data.length * 2;
687
+ while (newCapacity < required) {
688
+ newCapacity *= 2;
689
+ }
690
+
691
+ const newData = new Uint8Array(newCapacity);
692
+ newData.set(this.#data.subarray(0, this.#offset));
693
+ this.#data = newData;
694
+ this.#view = new DataView(this.#data.buffer);
695
+ }
696
696
  }
package/src/io/hex.ts CHANGED
@@ -49,7 +49,8 @@ const HEX_TO_NIBBLE: readonly number[] = (() => {
49
49
  export function toHex(bytes: Uint8Array): string {
50
50
  let result = '';
51
51
  for (let i = 0; i < bytes.length; i++) {
52
- result += BYTE_TO_HEX[bytes[i]!];
52
+ const hex = BYTE_TO_HEX[bytes[i] as number];
53
+ result += hex as string;
53
54
  }
54
55
  return result;
55
56
  }