@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
@@ -9,29 +9,29 @@
9
9
 
10
10
  import { bech32m } from 'bech32';
11
11
  import { fromBech32 } from '../bech32utils.js';
12
- import { getEccLib } from '../ecc/context.js';
13
12
  import { bitcoin as BITCOIN_NETWORK, type Network } from '../networks.js';
14
13
  import * as bscript from '../script.js';
15
14
  import {
16
- stacksEqual,
17
- TAPLEAF_VERSION_MASK,
18
15
  type Bytes32,
19
- type Script,
16
+ isXOnlyPublicKey,
20
17
  type SchnorrSignature,
18
+ type Script,
19
+ stacksEqual,
20
+ TAPLEAF_VERSION_MASK,
21
21
  type Taptree,
22
22
  type XOnlyPublicKey,
23
23
  } from '../types.js';
24
24
  import {
25
25
  findScriptPath,
26
+ type HashTree,
26
27
  LEAF_VERSION_TAPSCRIPT,
27
28
  rootHashFromPath,
28
29
  tapleafHash,
29
30
  toHashTree,
30
31
  tweakKey,
31
- type HashTree,
32
32
  } from './bip341.js';
33
33
  import { concat, equals } from '../io/index.js';
34
- import { PaymentType, type P2TRPayment, type PaymentOpts, type ScriptRedeem } from './types.js';
34
+ import { type P2TRPayment, type PaymentOpts, PaymentType, type ScriptRedeem } from './types.js';
35
35
 
36
36
  const OPS = bscript.opcodes;
37
37
  const TAPROOT_WITNESS_VERSION = 0x01;
@@ -362,6 +362,28 @@ export class P2TR {
362
362
 
363
363
  // Private helper methods
364
364
 
365
+ /**
366
+ * Converts to a plain P2TRPayment object for backwards compatibility.
367
+ *
368
+ * @returns A P2TRPayment object
369
+ */
370
+ toPayment(): P2TRPayment {
371
+ return {
372
+ name: this.name,
373
+ network: this.network,
374
+ address: this.address,
375
+ pubkey: this.pubkey,
376
+ internalPubkey: this.internalPubkey,
377
+ hash: this.hash,
378
+ scriptTree: this.#inputScriptTree,
379
+ signature: this.signature,
380
+ output: this.output,
381
+ redeem: this.redeem,
382
+ redeemVersion: this.redeemVersion,
383
+ witness: this.witness,
384
+ };
385
+ }
386
+
365
387
  #getDecodedAddress(): { version: number; prefix: string; data: Uint8Array } | undefined {
366
388
  if (!this.#decodedAddressComputed) {
367
389
  if (this.#inputAddress) {
@@ -397,6 +419,8 @@ export class P2TR {
397
419
  return this.#witnessWithoutAnnex;
398
420
  }
399
421
 
422
+ // Private computation methods
423
+
400
424
  #getHashTree(): HashTree | undefined {
401
425
  if (!this.#hashTreeComputed) {
402
426
  if (this.#inputScriptTree) {
@@ -409,8 +433,6 @@ export class P2TR {
409
433
  return this.#hashTree;
410
434
  }
411
435
 
412
- // Private computation methods
413
-
414
436
  #computeAddress(): string | undefined {
415
437
  if (this.#inputAddress) {
416
438
  return this.#inputAddress;
@@ -437,7 +459,7 @@ export class P2TR {
437
459
  if (internalPk) {
438
460
  const tweakedKey = tweakKey(internalPk, this.hash);
439
461
  if (tweakedKey) {
440
- return tweakedKey.x as XOnlyPublicKey;
462
+ return tweakedKey.x;
441
463
  }
442
464
  }
443
465
  return undefined;
@@ -457,7 +479,7 @@ export class P2TR {
457
479
  #computeHash(): Bytes32 | undefined {
458
480
  const hashTree = this.#getHashTree();
459
481
  if (hashTree) {
460
- return hashTree.hash as Bytes32;
482
+ return hashTree.hash;
461
483
  }
462
484
 
463
485
  const w = this.#getWitnessWithoutAnnex();
@@ -469,7 +491,7 @@ export class P2TR {
469
491
  output: script,
470
492
  version: leafVersion,
471
493
  });
472
- return rootHashFromPath(controlBlock, leafHash) as Bytes32;
494
+ return rootHashFromPath(controlBlock, leafHash);
473
495
  }
474
496
 
475
497
  return undefined;
@@ -493,7 +515,7 @@ export class P2TR {
493
515
  const pk = this.pubkey;
494
516
  if (!pk) return undefined;
495
517
 
496
- return bscript.compile([OPS.OP_1, pk]) as Script;
518
+ return bscript.compile([OPS.OP_1, pk]);
497
519
  }
498
520
 
499
521
  #computeRedeem(): ScriptRedeem | undefined {
@@ -525,6 +547,8 @@ export class P2TR {
525
547
  return LEAF_VERSION_TAPSCRIPT;
526
548
  }
527
549
 
550
+ // Validation
551
+
528
552
  #computeWitness(): Uint8Array[] | undefined {
529
553
  if (this.#inputWitness) {
530
554
  return this.#inputWitness;
@@ -558,8 +582,6 @@ export class P2TR {
558
582
  return undefined;
559
583
  }
560
584
 
561
- // Validation
562
-
563
585
  #validate(): void {
564
586
  let pubkey: Uint8Array = new Uint8Array(0);
565
587
 
@@ -666,7 +688,7 @@ export class P2TR {
666
688
  if (witness && witness.length > 0) {
667
689
  if (witness.length === 1) {
668
690
  // Key-path spending
669
- if (this.#inputSignature && !equals(this.#inputSignature!, witness[0]!)) {
691
+ if (this.#inputSignature && !equals(this.#inputSignature, witness[0]!)) {
670
692
  throw new TypeError('Signature mismatch');
671
693
  }
672
694
  } else {
@@ -690,11 +712,11 @@ export class P2TR {
690
712
  }
691
713
 
692
714
  const internalPk = controlBlock.subarray(1, 33);
693
- if (this.#inputInternalPubkey && !equals(this.#inputInternalPubkey!, internalPk)) {
715
+ if (this.#inputInternalPubkey && !equals(this.#inputInternalPubkey, internalPk)) {
694
716
  throw new TypeError('Internal pubkey mismatch');
695
717
  }
696
718
 
697
- if (!getEccLib().isXOnlyPoint(internalPk)) {
719
+ if (!isXOnlyPublicKey(internalPk)) {
698
720
  throw new TypeError('Invalid internalPubkey for p2tr witness');
699
721
  }
700
722
 
@@ -707,7 +729,7 @@ export class P2TR {
707
729
  });
708
730
  const computedHash = rootHashFromPath(controlBlock, leafHash);
709
731
 
710
- const outputKey = tweakKey(internalPk as XOnlyPublicKey, computedHash);
732
+ const outputKey = tweakKey(internalPk, computedHash);
711
733
  if (!outputKey) {
712
734
  throw new TypeError('Invalid outputKey for p2tr witness');
713
735
  }
@@ -722,28 +744,6 @@ export class P2TR {
722
744
  }
723
745
  }
724
746
  }
725
-
726
- /**
727
- * Converts to a plain P2TRPayment object for backwards compatibility.
728
- *
729
- * @returns A P2TRPayment object
730
- */
731
- toPayment(): P2TRPayment {
732
- return {
733
- name: this.name,
734
- network: this.network,
735
- address: this.address,
736
- pubkey: this.pubkey,
737
- internalPubkey: this.internalPubkey,
738
- hash: this.hash,
739
- scriptTree: this.#inputScriptTree,
740
- signature: this.signature,
741
- output: this.output,
742
- redeem: this.redeem,
743
- redeemVersion: this.redeemVersion,
744
- witness: this.witness,
745
- };
746
- }
747
747
  }
748
748
 
749
749
  /**
@@ -12,10 +12,10 @@ import { bech32 } from 'bech32';
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 { isPoint } from '../types.js';
16
15
  import type { Bytes20, PublicKey, Script, Signature } from '../types.js';
16
+ import { isPoint } from '../types.js';
17
17
  import { equals } from '../io/index.js';
18
- import { PaymentType, type P2WPKHPayment, type PaymentOpts } from './types.js';
18
+ import { type P2WPKHPayment, type PaymentOpts, PaymentType } from './types.js';
19
19
 
20
20
  const OPS = bscript.opcodes;
21
21
  const EMPTY_BUFFER = new Uint8Array(0);
@@ -279,6 +279,27 @@ export class P2WPKH {
279
279
 
280
280
  // Private helper methods
281
281
 
282
+ /**
283
+ * Converts to a plain P2WPKHPayment object for backwards compatibility.
284
+ *
285
+ * @returns A P2WPKHPayment object
286
+ */
287
+ toPayment(): P2WPKHPayment {
288
+ return {
289
+ name: this.name,
290
+ network: this.network,
291
+ address: this.address,
292
+ hash: this.hash,
293
+ pubkey: this.pubkey,
294
+ signature: this.signature,
295
+ output: this.output,
296
+ input: this.input,
297
+ witness: this.witness,
298
+ };
299
+ }
300
+
301
+ // Private computation methods
302
+
282
303
  #getDecodedAddress(): { version: number; prefix: string; data: Uint8Array } | undefined {
283
304
  if (!this.#decodedAddressComputed) {
284
305
  if (this.#inputAddress) {
@@ -296,8 +317,6 @@ export class P2WPKH {
296
317
  return this.#decodedAddress;
297
318
  }
298
319
 
299
- // Private computation methods
300
-
301
320
  #computeAddress(): string | undefined {
302
321
  if (this.#inputAddress) {
303
322
  return this.#inputAddress;
@@ -322,7 +341,7 @@ export class P2WPKH {
322
341
  }
323
342
  const pk = this.#inputPubkey ?? this.pubkey;
324
343
  if (pk) {
325
- return bcrypto.hash160(pk) as Bytes20;
344
+ return bcrypto.hash160(pk);
326
345
  }
327
346
  return undefined;
328
347
  }
@@ -354,7 +373,7 @@ export class P2WPKH {
354
373
  const h = this.hash;
355
374
  if (!h) return undefined;
356
375
 
357
- return bscript.compile([OPS.OP_0, h]) as Script;
376
+ return bscript.compile([OPS.OP_0, h]);
358
377
  }
359
378
 
360
379
  #computeInput(): Uint8Array | undefined {
@@ -364,6 +383,8 @@ export class P2WPKH {
364
383
  return undefined;
365
384
  }
366
385
 
386
+ // Validation
387
+
367
388
  #computeWitness(): Uint8Array[] | undefined {
368
389
  if (this.#inputWitness) {
369
390
  return this.#inputWitness;
@@ -374,8 +395,6 @@ export class P2WPKH {
374
395
  return [this.#inputSignature, this.#inputPubkey];
375
396
  }
376
397
 
377
- // Validation
378
-
379
398
  #validate(): void {
380
399
  let hash: Uint8Array = new Uint8Array(0);
381
400
 
@@ -438,14 +457,14 @@ export class P2WPKH {
438
457
  if (!bscript.isCanonicalScriptSignature(this.#inputWitness[0]!)) {
439
458
  throw new TypeError('Witness has invalid signature');
440
459
  }
441
- if (!isPoint(this.#inputWitness[1]!) || this.#inputWitness[1]!.length !== 33) {
460
+ if (!isPoint(this.#inputWitness[1]!) || this.#inputWitness[1].length !== 33) {
442
461
  throw new TypeError('Witness has invalid pubkey');
443
462
  }
444
463
 
445
- if (this.#inputSignature && !equals(this.#inputSignature!, this.#inputWitness[0]!)) {
464
+ if (this.#inputSignature && !equals(this.#inputSignature, this.#inputWitness[0]!)) {
446
465
  throw new TypeError('Signature mismatch');
447
466
  }
448
- if (this.#inputPubkey && !equals(this.#inputPubkey!, this.#inputWitness[1]!)) {
467
+ if (this.#inputPubkey && !equals(this.#inputPubkey, this.#inputWitness[1])) {
449
468
  throw new TypeError('Pubkey mismatch');
450
469
  }
451
470
 
@@ -455,25 +474,6 @@ export class P2WPKH {
455
474
  }
456
475
  }
457
476
  }
458
-
459
- /**
460
- * Converts to a plain P2WPKHPayment object for backwards compatibility.
461
- *
462
- * @returns A P2WPKHPayment object
463
- */
464
- toPayment(): P2WPKHPayment {
465
- return {
466
- name: this.name,
467
- network: this.network,
468
- address: this.address,
469
- hash: this.hash,
470
- pubkey: this.pubkey,
471
- signature: this.signature,
472
- output: this.output,
473
- input: this.input,
474
- witness: this.witness,
475
- };
476
- }
477
477
  }
478
478
 
479
479
  /**
@@ -12,10 +12,10 @@ import { bech32 } from 'bech32';
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 { isPoint, stacksEqual, type StackElement } from '../types.js';
16
15
  import type { Bytes32, Script } from '../types.js';
16
+ import { isPoint, type StackElement, stacksEqual } from '../types.js';
17
17
  import { equals } from '../io/index.js';
18
- import { PaymentType, type P2WSHPayment, type PaymentOpts, type ScriptRedeem } from './types.js';
18
+ import { type P2WSHPayment, type PaymentOpts, PaymentType, type ScriptRedeem } from './types.js';
19
19
 
20
20
  const OPS = bscript.opcodes;
21
21
  const EMPTY_BUFFER = new Uint8Array(0);
@@ -278,6 +278,27 @@ export class P2WSH {
278
278
 
279
279
  // Private helper methods
280
280
 
281
+ /**
282
+ * Converts to a plain P2WSHPayment object for backwards compatibility.
283
+ *
284
+ * @returns A P2WSHPayment object
285
+ */
286
+ toPayment(): P2WSHPayment {
287
+ // Access witness first as it may modify redeem (transform input to witness)
288
+ const witness = this.witness;
289
+ const redeem = this.redeem;
290
+ return {
291
+ name: this.name,
292
+ network: this.network,
293
+ address: this.address,
294
+ hash: this.hash,
295
+ output: this.output,
296
+ input: this.input,
297
+ redeem,
298
+ witness,
299
+ };
300
+ }
301
+
281
302
  #getDecodedAddress(): { version: number; prefix: string; data: Uint8Array } | undefined {
282
303
  if (!this.#decodedAddressComputed) {
283
304
  if (this.#inputAddress) {
@@ -295,6 +316,8 @@ export class P2WSH {
295
316
  return this.#decodedAddress;
296
317
  }
297
318
 
319
+ // Private computation methods
320
+
298
321
  #getRedeemChunks(): (Uint8Array | number)[] | undefined {
299
322
  if (!this.#redeemChunksComputed) {
300
323
  if (this.#inputRedeem?.input) {
@@ -305,8 +328,6 @@ export class P2WSH {
305
328
  return this.#redeemChunks;
306
329
  }
307
330
 
308
- // Private computation methods
309
-
310
331
  #computeAddress(): string | undefined {
311
332
  if (this.#inputAddress) {
312
333
  return this.#inputAddress;
@@ -331,7 +352,7 @@ export class P2WSH {
331
352
  }
332
353
  const r = this.redeem;
333
354
  if (r && r.output) {
334
- return bcrypto.sha256(r.output) as Bytes32;
355
+ return bcrypto.sha256(r.output);
335
356
  }
336
357
  return undefined;
337
358
  }
@@ -343,7 +364,7 @@ export class P2WSH {
343
364
  const h = this.hash;
344
365
  if (!h) return undefined;
345
366
 
346
- return bscript.compile([OPS.OP_0, h]) as Script;
367
+ return bscript.compile([OPS.OP_0, h]);
347
368
  }
348
369
 
349
370
  #computeInput(): Uint8Array | undefined {
@@ -367,6 +388,8 @@ export class P2WSH {
367
388
  return undefined;
368
389
  }
369
390
 
391
+ // Validation
392
+
370
393
  #computeWitness(): Uint8Array[] | undefined {
371
394
  if (this.#inputWitness) {
372
395
  return this.#inputWitness;
@@ -394,8 +417,6 @@ export class P2WSH {
394
417
  return undefined;
395
418
  }
396
419
 
397
- // Validation
398
-
399
420
  #validate(): void {
400
421
  let hash: Uint8Array = new Uint8Array(0);
401
422
 
@@ -518,27 +539,6 @@ export class P2WSH {
518
539
  }
519
540
  }
520
541
  }
521
-
522
- /**
523
- * Converts to a plain P2WSHPayment object for backwards compatibility.
524
- *
525
- * @returns A P2WSHPayment object
526
- */
527
- toPayment(): P2WSHPayment {
528
- // Access witness first as it may modify redeem (transform input to witness)
529
- const witness = this.witness;
530
- const redeem = this.redeem;
531
- return {
532
- name: this.name,
533
- network: this.network,
534
- address: this.address,
535
- hash: this.hash,
536
- output: this.output,
537
- input: this.input,
538
- redeem,
539
- witness,
540
- };
541
- }
542
542
  }
543
543
 
544
544
  /**
@@ -8,9 +8,9 @@ import type {
8
8
  Bytes20,
9
9
  Bytes32,
10
10
  PublicKey,
11
+ SchnorrSignature,
11
12
  Script,
12
13
  Signature,
13
- SchnorrSignature,
14
14
  Taptree,
15
15
  XOnlyPublicKey,
16
16
  } from '../types.js';