@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/io/index.ts CHANGED
@@ -34,8 +34,5 @@ export {
34
34
  toUtf8,
35
35
  } from './utils.js';
36
36
 
37
- // Memory pools
38
- export { MemoryPool, SimpleMemoryPool } from './MemoryPool.js';
39
-
40
37
  // Re-export varuint for Bitcoin CompactSize encoding
41
38
  export { varuint };
package/src/opcodes.ts CHANGED
@@ -273,8 +273,25 @@ export const opcodes: Opcodes = {
273
273
  OP_INVALIDOPCODE: 255,
274
274
  };
275
275
 
276
- export const REVERSE_OPS: { [key: number]: string } = {};
277
- for (const op of Object.keys(opcodes)) {
278
- const code = opcodes[op as keyof Opcodes];
279
- REVERSE_OPS[code] = op;
276
+ let _reverseOps: { [key: number]: string } | undefined;
277
+
278
+ /**
279
+ * Returns the reverse mapping from opcode number to opcode name.
280
+ * Lazily computed on first call.
281
+ */
282
+ export function getReverseOps(): { [key: number]: string } {
283
+ if (!_reverseOps) {
284
+ _reverseOps = {};
285
+ for (const op of Object.keys(opcodes)) {
286
+ const code = opcodes[op as keyof Opcodes];
287
+ _reverseOps[code] = op;
288
+ }
289
+ }
290
+ return _reverseOps;
280
291
  }
292
+
293
+ /**
294
+ * @deprecated Use {@link getReverseOps}() for lazy initialization.
295
+ * This eagerly-initialized alias exists for backward compatibility.
296
+ */
297
+ export const REVERSE_OPS: { [key: number]: string } = getReverseOps();
@@ -1,10 +1,8 @@
1
1
  import * as bcrypto from '../crypto.js';
2
2
  import { getEccLib } from '../ecc/context.js';
3
- import { concat, compare, equals, alloc } from '../io/index.js';
4
- import { varuint } from '../io/index.js';
3
+ import { alloc, compare, concat, equals, varuint } from '../io/index.js';
4
+ import type { Bytes32, Tapleaf, Taptree, XOnlyPublicKey } from '../types.js';
5
5
  import { isTapleaf } from '../types.js';
6
- import type { Tapleaf, Taptree } from '../types.js';
7
- import type { Bytes32, XOnlyPublicKey } from '../types.js';
8
6
 
9
7
  export const LEAF_VERSION_TAPSCRIPT = 0xc0;
10
8
  export const MAX_TAPTREE_DEPTH = 128;
@@ -106,11 +104,11 @@ export function tapleafHash(leaf: Tapleaf): Bytes32 {
106
104
  return bcrypto.taggedHash(
107
105
  'TapLeaf',
108
106
  concat([new Uint8Array([version]), serializeScript(leaf.output)]),
109
- ) as Bytes32;
107
+ );
110
108
  }
111
109
 
112
110
  export function tapTweakHash(pubKey: XOnlyPublicKey, h: Bytes32 | undefined): Bytes32 {
113
- return bcrypto.taggedHash('TapTweak', h ? concat([pubKey, h]) : pubKey) as Bytes32;
111
+ return bcrypto.taggedHash('TapTweak', h ? concat([pubKey, h]) : pubKey);
114
112
  }
115
113
 
116
114
  export function tweakKey(pubKey: XOnlyPublicKey, h: Bytes32 | undefined): TweakedPublicKey | null {
@@ -130,7 +128,7 @@ export function tweakKey(pubKey: XOnlyPublicKey, h: Bytes32 | undefined): Tweake
130
128
  }
131
129
 
132
130
  function tapBranchHash(a: Uint8Array, b: Uint8Array): Bytes32 {
133
- return bcrypto.taggedHash('TapBranch', concat([a, b])) as Bytes32;
131
+ return bcrypto.taggedHash('TapBranch', concat([a, b]));
134
132
  }
135
133
 
136
134
  function serializeScript(s: Uint8Array): Uint8Array {
@@ -9,9 +9,9 @@
9
9
 
10
10
  import { bitcoin as BITCOIN_NETWORK, type Network } from '../networks.js';
11
11
  import * as bscript from '../script.js';
12
- import { stacksEqual, type Stack } from '../types.js';
13
12
  import type { Script } from '../types.js';
14
- import { PaymentType, type EmbedPayment, type PaymentOpts } from './types.js';
13
+ import { type Stack, stacksEqual } from '../types.js';
14
+ import { type EmbedPayment, type PaymentOpts, PaymentType } from './types.js';
15
15
 
16
16
  const OPS = bscript.opcodes;
17
17
 
@@ -162,6 +162,20 @@ export class Embed {
162
162
 
163
163
  // Private computation methods
164
164
 
165
+ /**
166
+ * Converts to a plain EmbedPayment object for backwards compatibility.
167
+ *
168
+ * @returns An EmbedPayment object
169
+ */
170
+ toPayment(): EmbedPayment {
171
+ return {
172
+ name: this.name,
173
+ network: this.network,
174
+ data: this.data,
175
+ output: this.output,
176
+ };
177
+ }
178
+
165
179
  #computeData(): Uint8Array[] | undefined {
166
180
  if (this.#inputData) {
167
181
  return this.#inputData;
@@ -176,6 +190,8 @@ export class Embed {
176
190
  return undefined;
177
191
  }
178
192
 
193
+ // Validation
194
+
179
195
  #computeOutput(): Uint8Array | undefined {
180
196
  if (this.#inputOutput) {
181
197
  return this.#inputOutput;
@@ -183,11 +199,9 @@ export class Embed {
183
199
  if (!this.#inputData) {
184
200
  return undefined;
185
201
  }
186
- return bscript.compile(([OPS.OP_RETURN] as Stack).concat(this.#inputData)) as Script;
202
+ return bscript.compile(([OPS.OP_RETURN] as Stack).concat(this.#inputData));
187
203
  }
188
204
 
189
- // Validation
190
-
191
205
  #validate(): void {
192
206
  if (this.#inputOutput) {
193
207
  const chunks = bscript.decompile(this.#inputOutput);
@@ -206,20 +220,6 @@ export class Embed {
206
220
  }
207
221
  }
208
222
  }
209
-
210
- /**
211
- * Converts to a plain EmbedPayment object for backwards compatibility.
212
- *
213
- * @returns An EmbedPayment object
214
- */
215
- toPayment(): EmbedPayment {
216
- return {
217
- name: this.name,
218
- network: this.network,
219
- data: this.data,
220
- output: this.output,
221
- };
222
- }
223
223
  }
224
224
 
225
225
  /**
@@ -9,8 +9,15 @@
9
9
 
10
10
  import { bitcoin as BITCOIN_NETWORK, type Network } from '../networks.js';
11
11
  import * as bscript from '../script.js';
12
- import { isPoint, stacksEqual, type PublicKey, type Script, type Signature, type Stack } from '../types.js';
13
- import { PaymentType, type P2MSPayment, type PaymentOpts } from './types.js';
12
+ import {
13
+ isPoint,
14
+ type PublicKey,
15
+ type Script,
16
+ type Signature,
17
+ type Stack,
18
+ stacksEqual,
19
+ } from '../types.js';
20
+ import { type P2MSPayment, type PaymentOpts, PaymentType } from './types.js';
14
21
 
15
22
  const OPS = bscript.opcodes;
16
23
  const OP_INT_BASE = OPS.OP_RESERVED; // OP_1 - 1
@@ -274,6 +281,27 @@ export class P2MS {
274
281
 
275
282
  // Private helper methods
276
283
 
284
+ /**
285
+ * Converts to a plain P2MSPayment object for backwards compatibility.
286
+ *
287
+ * @returns A P2MSPayment object
288
+ */
289
+ toPayment(): P2MSPayment {
290
+ return {
291
+ name: this.name,
292
+ network: this.network,
293
+ m: this.m,
294
+ n: this.n,
295
+ pubkeys: this.pubkeys,
296
+ signatures: this.signatures,
297
+ output: this.output,
298
+ input: this.input,
299
+ witness: this.witness,
300
+ };
301
+ }
302
+
303
+ // Private computation methods
304
+
277
305
  #decode(output: Uint8Array | Stack): void {
278
306
  if (this.#decoded) return;
279
307
  this.#decoded = true;
@@ -286,8 +314,6 @@ export class P2MS {
286
314
  this.#pubkeysComputed = true;
287
315
  }
288
316
 
289
- // Private computation methods
290
-
291
317
  #computeM(): number | undefined {
292
318
  if (this.#inputM !== undefined) {
293
319
  return this.#inputM;
@@ -351,7 +377,7 @@ export class P2MS {
351
377
  }
352
378
  return bscript.compile(
353
379
  ([] as Stack).concat(OP_INT_BASE + m, pubkeys, OP_INT_BASE + n, OPS.OP_CHECKMULTISIG),
354
- ) as Script;
380
+ );
355
381
  }
356
382
 
357
383
  #computeInput(): Script | undefined {
@@ -361,9 +387,11 @@ export class P2MS {
361
387
  if (!this.#inputSignatures) {
362
388
  return undefined;
363
389
  }
364
- return bscript.compile(([OPS.OP_0] as Stack).concat(this.#inputSignatures)) as Script;
390
+ return bscript.compile(([OPS.OP_0] as Stack).concat(this.#inputSignatures));
365
391
  }
366
392
 
393
+ // Validation
394
+
367
395
  #computeWitness(): Uint8Array[] | undefined {
368
396
  if (this.input) {
369
397
  return [];
@@ -371,8 +399,6 @@ export class P2MS {
371
399
  return undefined;
372
400
  }
373
401
 
374
- // Validation
375
-
376
402
  #isAcceptableSignature(x: Uint8Array | number): boolean {
377
403
  return (
378
404
  bscript.isCanonicalScriptSignature(x as Uint8Array) ||
@@ -465,25 +491,6 @@ export class P2MS {
465
491
  }
466
492
  }
467
493
  }
468
-
469
- /**
470
- * Converts to a plain P2MSPayment object for backwards compatibility.
471
- *
472
- * @returns A P2MSPayment object
473
- */
474
- toPayment(): P2MSPayment {
475
- return {
476
- name: this.name,
477
- network: this.network,
478
- m: this.m,
479
- n: this.n,
480
- pubkeys: this.pubkeys,
481
- signatures: this.signatures,
482
- output: this.output,
483
- input: this.input,
484
- witness: this.witness,
485
- };
486
- }
487
494
  }
488
495
 
489
496
  /**
@@ -13,7 +13,7 @@ import { bitcoin as BITCOIN_NETWORK, type Network } from '../networks.js';
13
13
  import * as bscript from '../script.js';
14
14
  import { alloc, concat, equals } from '../io/index.js';
15
15
  import type { Bytes20, Script } from '../types.js';
16
- import { PaymentType, type P2OPPayment, type PaymentOpts } from './types.js';
16
+ import { type P2OPPayment, type PaymentOpts, PaymentType } from './types.js';
17
17
 
18
18
  const OPS = bscript.opcodes;
19
19
  const P2OP_WITNESS_VERSION = 0x10;
@@ -241,6 +241,23 @@ export class P2OP {
241
241
 
242
242
  // Private helper methods
243
243
 
244
+ /**
245
+ * Converts to a plain P2OPPayment object for backwards compatibility.
246
+ *
247
+ * @returns A P2OPPayment object
248
+ */
249
+ toPayment(): P2OPPayment {
250
+ return {
251
+ name: this.name,
252
+ network: this.network,
253
+ address: this.address,
254
+ program: this.program,
255
+ deploymentVersion: this.deploymentVersion,
256
+ hash160: this.hash160,
257
+ output: this.output as Script | undefined,
258
+ };
259
+ }
260
+
244
261
  #getDecodedAddress(): { version: number; prefix: string; data: Uint8Array } | undefined {
245
262
  if (!this.#decodedAddressComputed) {
246
263
  if (this.#inputAddress) {
@@ -258,6 +275,8 @@ export class P2OP {
258
275
  return this.#decodedAddress;
259
276
  }
260
277
 
278
+ // Private computation methods
279
+
261
280
  #makeProgramFromParts(): Uint8Array | undefined {
262
281
  if (
263
282
  typeof this.#inputDeploymentVersion !== 'undefined' &&
@@ -274,8 +293,6 @@ export class P2OP {
274
293
  return undefined;
275
294
  }
276
295
 
277
- // Private computation methods
278
-
279
296
  #computeAddress(): string | undefined {
280
297
  if (this.#inputAddress) {
281
298
  return this.#inputAddress;
@@ -346,6 +363,8 @@ export class P2OP {
346
363
  return prog.subarray(1) as Bytes20;
347
364
  }
348
365
 
366
+ // Validation
367
+
349
368
  #computeOutput(): Uint8Array | undefined {
350
369
  if (this.#inputOutput) {
351
370
  return this.#inputOutput;
@@ -356,8 +375,6 @@ export class P2OP {
356
375
  return bscript.compile([OPS.OP_16, prog]);
357
376
  }
358
377
 
359
- // Validation
360
-
361
378
  #validate(): void {
362
379
  let prog: Uint8Array = alloc(0);
363
380
 
@@ -416,23 +433,6 @@ export class P2OP {
416
433
  throw new TypeError('hash160 mismatch');
417
434
  }
418
435
  }
419
-
420
- /**
421
- * Converts to a plain P2OPPayment object for backwards compatibility.
422
- *
423
- * @returns A P2OPPayment object
424
- */
425
- toPayment(): P2OPPayment {
426
- return {
427
- name: this.name,
428
- network: this.network,
429
- address: this.address,
430
- program: this.program,
431
- deploymentVersion: this.deploymentVersion,
432
- hash160: this.hash160,
433
- output: this.output as Script | undefined,
434
- };
435
- }
436
436
  }
437
437
 
438
438
  /**
@@ -11,7 +11,7 @@ import { bitcoin as BITCOIN_NETWORK, type Network } from '../networks.js';
11
11
  import * as bscript from '../script.js';
12
12
  import { isPoint, type PublicKey, type Script, type Signature } from '../types.js';
13
13
  import { equals } from '../io/index.js';
14
- import { PaymentType, type P2PKPayment, type PaymentOpts } from './types.js';
14
+ import { type P2PKPayment, type PaymentOpts, PaymentType } from './types.js';
15
15
 
16
16
  const OPS = bscript.opcodes;
17
17
 
@@ -238,6 +238,23 @@ export class P2PK {
238
238
 
239
239
  // Private computation methods
240
240
 
241
+ /**
242
+ * Converts to a plain P2PKPayment object for backwards compatibility.
243
+ *
244
+ * @returns A P2PKPayment object
245
+ */
246
+ toPayment(): P2PKPayment {
247
+ return {
248
+ name: this.name,
249
+ network: this.network,
250
+ pubkey: this.pubkey,
251
+ signature: this.signature,
252
+ output: this.output,
253
+ input: this.input,
254
+ witness: this.witness,
255
+ };
256
+ }
257
+
241
258
  #computePubkey(): PublicKey | undefined {
242
259
  if (this.#inputPubkey) {
243
260
  return this.#inputPubkey as PublicKey;
@@ -268,7 +285,7 @@ export class P2PK {
268
285
  }
269
286
  const pubkey = this.#inputPubkey;
270
287
  if (pubkey) {
271
- return bscript.compile([pubkey, OPS.OP_CHECKSIG]) as Script;
288
+ return bscript.compile([pubkey, OPS.OP_CHECKSIG]);
272
289
  }
273
290
  return undefined;
274
291
  }
@@ -279,11 +296,13 @@ export class P2PK {
279
296
  }
280
297
  const signature = this.#inputSignature;
281
298
  if (signature) {
282
- return bscript.compile([signature]) as Script;
299
+ return bscript.compile([signature]);
283
300
  }
284
301
  return undefined;
285
302
  }
286
303
 
304
+ // Validation
305
+
287
306
  #computeWitness(): Uint8Array[] | undefined {
288
307
  if (this.input) {
289
308
  return [];
@@ -291,8 +310,6 @@ export class P2PK {
291
310
  return undefined;
292
311
  }
293
312
 
294
- // Validation
295
-
296
313
  #validate(): void {
297
314
  if (this.#inputOutput) {
298
315
  if (this.#inputOutput[this.#inputOutput.length - 1] !== OPS.OP_CHECKSIG) {
@@ -329,23 +346,6 @@ export class P2PK {
329
346
  }
330
347
  }
331
348
  }
332
-
333
- /**
334
- * Converts to a plain P2PKPayment object for backwards compatibility.
335
- *
336
- * @returns A P2PKPayment object
337
- */
338
- toPayment(): P2PKPayment {
339
- return {
340
- name: this.name,
341
- network: this.network,
342
- pubkey: this.pubkey,
343
- signature: this.signature,
344
- output: this.output,
345
- input: this.input,
346
- witness: this.witness,
347
- };
348
- }
349
349
  }
350
350
 
351
351
  /**
@@ -13,9 +13,9 @@ import * as bcrypto from '../crypto.js';
13
13
  import { bitcoin as BITCOIN_NETWORK, type Network } from '../networks.js';
14
14
  import { decompressPublicKey } from '../pubkey.js';
15
15
  import * as bscript from '../script.js';
16
- import { isPoint, type Bytes20, type PublicKey, type Script, type Signature } from '../types.js';
16
+ import { type Bytes20, isPoint, type PublicKey, type Script, type Signature } from '../types.js';
17
17
  import { alloc, equals } from '../io/index.js';
18
- import { PaymentType, type P2PKHPayment, type PaymentOpts } from './types.js';
18
+ import { type P2PKHPayment, type PaymentOpts, PaymentType } from './types.js';
19
19
 
20
20
  const OPS = bscript.opcodes;
21
21
 
@@ -303,6 +303,25 @@ export class P2PKH {
303
303
 
304
304
  // Private helper methods
305
305
 
306
+ /**
307
+ * Converts to a plain P2PKHPayment object for backwards compatibility.
308
+ *
309
+ * @returns A P2PKHPayment object
310
+ */
311
+ toPayment(): P2PKHPayment {
312
+ return {
313
+ name: this.name,
314
+ network: this.network,
315
+ address: this.address,
316
+ hash: this.hash,
317
+ pubkey: this.pubkey,
318
+ signature: this.signature,
319
+ output: this.output,
320
+ input: this.input,
321
+ witness: this.witness,
322
+ };
323
+ }
324
+
306
325
  #getDecodedAddress(): { version: number; hash: Uint8Array } | undefined {
307
326
  if (!this.#decodedAddressComputed) {
308
327
  if (this.#inputAddress) {
@@ -317,6 +336,8 @@ export class P2PKH {
317
336
  return this.#decodedAddress;
318
337
  }
319
338
 
339
+ // Private computation methods
340
+
320
341
  #getInputChunks(): (Uint8Array | number)[] | undefined {
321
342
  if (!this.#inputChunksComputed) {
322
343
  if (this.#inputInput) {
@@ -327,8 +348,6 @@ export class P2PKH {
327
348
  return this.#inputChunks;
328
349
  }
329
350
 
330
- // Private computation methods
331
-
332
351
  #computeAddress(): string | undefined {
333
352
  if (this.#inputAddress) {
334
353
  return this.#inputAddress;
@@ -355,7 +374,7 @@ export class P2PKH {
355
374
  // Use the pubkey getter to derive pubkey from input if available
356
375
  const pk = this.pubkey;
357
376
  if (pk) {
358
- return bcrypto.hash160(pk) as Bytes20;
377
+ return bcrypto.hash160(pk);
359
378
  }
360
379
  return undefined;
361
380
  }
@@ -399,7 +418,7 @@ export class P2PKH {
399
418
  h,
400
419
  OPS.OP_EQUALVERIFY,
401
420
  OPS.OP_CHECKSIG,
402
- ]) as Script;
421
+ ]);
403
422
  }
404
423
 
405
424
  #computeInput(): Script | undefined {
@@ -422,9 +441,11 @@ export class P2PKH {
422
441
  }
423
442
  }
424
443
 
425
- return bscript.compile([this.#inputSignature, pubKey]) as Script;
444
+ return bscript.compile([this.#inputSignature, pubKey]);
426
445
  }
427
446
 
447
+ // Validation
448
+
428
449
  #computeWitness(): Uint8Array[] | undefined {
429
450
  if (this.input) {
430
451
  return [];
@@ -432,8 +453,6 @@ export class P2PKH {
432
453
  return undefined;
433
454
  }
434
455
 
435
- // Validation
436
-
437
456
  #validate(): void {
438
457
  let hash: Uint8Array = new Uint8Array(0);
439
458
 
@@ -540,25 +559,6 @@ export class P2PKH {
540
559
  }
541
560
  }
542
561
  }
543
-
544
- /**
545
- * Converts to a plain P2PKHPayment object for backwards compatibility.
546
- *
547
- * @returns A P2PKHPayment object
548
- */
549
- toPayment(): P2PKHPayment {
550
- return {
551
- name: this.name,
552
- network: this.network,
553
- address: this.address,
554
- hash: this.hash,
555
- pubkey: this.pubkey,
556
- signature: this.signature,
557
- output: this.output,
558
- input: this.input,
559
- witness: this.witness,
560
- };
561
- }
562
562
  }
563
563
 
564
564
  /**
@@ -12,13 +12,13 @@ import * as bs58check from 'bs58check';
12
12
  import * as bcrypto from '../crypto.js';
13
13
  import { bitcoin as BITCOIN_NETWORK, type Network } from '../networks.js';
14
14
  import * as bscript from '../script.js';
15
- import { stacksEqual, type Bytes20, type Script, type Stack } from '../types.js';
15
+ import { type Bytes20, type Script, type Stack, stacksEqual } from '../types.js';
16
16
  import { alloc, equals } from '../io/index.js';
17
17
  import {
18
- PaymentType,
19
18
  type P2SHPayment,
20
19
  type Payment,
21
20
  type PaymentOpts,
21
+ PaymentType,
22
22
  type ScriptRedeem,
23
23
  } from './types.js';
24
24
 
@@ -282,6 +282,24 @@ export class P2SH {
282
282
 
283
283
  // Private helper methods
284
284
 
285
+ /**
286
+ * Converts to a plain P2SHPayment object for backwards compatibility.
287
+ *
288
+ * @returns A P2SHPayment object
289
+ */
290
+ toPayment(): P2SHPayment {
291
+ return {
292
+ name: this.name,
293
+ network: this.network,
294
+ address: this.address,
295
+ hash: this.hash,
296
+ output: this.output,
297
+ input: this.input,
298
+ redeem: this.redeem,
299
+ witness: this.witness,
300
+ };
301
+ }
302
+
285
303
  #getDecodedAddress(): { version: number; hash: Uint8Array } | undefined {
286
304
  if (!this.#decodedAddressComputed) {
287
305
  if (this.#inputAddress) {
@@ -306,6 +324,8 @@ export class P2SH {
306
324
  return this.#inputChunks;
307
325
  }
308
326
 
327
+ // Private computation methods
328
+
309
329
  #getDerivedRedeem(): ScriptRedeem | undefined {
310
330
  if (!this.#derivedRedeemComputed) {
311
331
  const chunks = this.#getInputChunks();
@@ -313,9 +333,10 @@ export class P2SH {
313
333
  const lastChunk = chunks[chunks.length - 1];
314
334
  this.#derivedRedeem = {
315
335
  network: this.#network,
316
- output:
317
- (lastChunk === OPS.OP_FALSE ? new Uint8Array(0) : (lastChunk as Uint8Array)) as Script,
318
- input: bscript.compile(chunks.slice(0, -1)) as Script,
336
+ output: (lastChunk === OPS.OP_FALSE
337
+ ? new Uint8Array(0)
338
+ : (lastChunk as Uint8Array)) as Script,
339
+ input: bscript.compile(chunks.slice(0, -1)),
319
340
  witness: this.#inputWitness || [],
320
341
  };
321
342
  }
@@ -324,8 +345,6 @@ export class P2SH {
324
345
  return this.#derivedRedeem;
325
346
  }
326
347
 
327
- // Private computation methods
328
-
329
348
  #computeAddress(): string | undefined {
330
349
  if (this.#inputAddress) {
331
350
  return this.#inputAddress;
@@ -351,7 +370,7 @@ export class P2SH {
351
370
  }
352
371
  const r = this.redeem;
353
372
  if (r && r.output) {
354
- return bcrypto.hash160(r.output) as Bytes20;
373
+ return bcrypto.hash160(r.output);
355
374
  }
356
375
  return undefined;
357
376
  }
@@ -363,7 +382,7 @@ export class P2SH {
363
382
  const h = this.hash;
364
383
  if (!h) return undefined;
365
384
 
366
- return bscript.compile([OPS.OP_HASH160, h, OPS.OP_EQUAL]) as Script;
385
+ return bscript.compile([OPS.OP_HASH160, h, OPS.OP_EQUAL]);
367
386
  }
368
387
 
369
388
  #computeInput(): Script | undefined {
@@ -374,7 +393,9 @@ export class P2SH {
374
393
  if (!r || !r.input || !r.output) {
375
394
  return undefined;
376
395
  }
377
- return bscript.compile(([] as Stack).concat(bscript.decompile(r.input) as Stack, r.output)) as Script;
396
+ return bscript.compile(
397
+ ([] as Stack).concat(bscript.decompile(r.input) as Stack, r.output),
398
+ );
378
399
  }
379
400
 
380
401
  #computeRedeem(): ScriptRedeem | undefined {
@@ -387,6 +408,8 @@ export class P2SH {
387
408
  return undefined;
388
409
  }
389
410
 
411
+ // Validation
412
+
390
413
  #computeWitness(): Uint8Array[] | undefined {
391
414
  if (this.#inputWitness) {
392
415
  return this.#inputWitness;
@@ -401,8 +424,6 @@ export class P2SH {
401
424
  return undefined;
402
425
  }
403
426
 
404
- // Validation
405
-
406
427
  #checkRedeem(redeem: Payment): void {
407
428
  // Is the redeem output empty/invalid?
408
429
  if (redeem.output) {
@@ -547,24 +568,6 @@ export class P2SH {
547
568
  }
548
569
  }
549
570
  }
550
-
551
- /**
552
- * Converts to a plain P2SHPayment object for backwards compatibility.
553
- *
554
- * @returns A P2SHPayment object
555
- */
556
- toPayment(): P2SHPayment {
557
- return {
558
- name: this.name,
559
- network: this.network,
560
- address: this.address,
561
- hash: this.hash,
562
- output: this.output,
563
- input: this.input,
564
- redeem: this.redeem,
565
- witness: this.witness,
566
- };
567
- }
568
571
  }
569
572
 
570
573
  /**