@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
@@ -0,0 +1,302 @@
1
+ import type { Bip32Derivation, PsbtInput } from 'bip174';
2
+ import { checkForInput } from 'bip174';
3
+ import { equals, toHex } from '../io/index.js';
4
+ import * as payments from '../payments/index.js';
5
+ import { tapleafHash } from '../payments/bip341.js';
6
+ import { toXOnly } from '../pubkey.js';
7
+ import { isP2WPKH, pubkeyInScript } from './psbtutils.js';
8
+ import { getMeaningfulScript, sighashTypeToString } from './utils.js';
9
+ import { checkScriptForPubkey } from './validation.js';
10
+ import type { Output } from '../transaction.js';
11
+ import { Transaction } from '../transaction.js';
12
+ import type { Bytes20, Bytes32, MessageHash, PublicKey, Satoshi, Script } from '../types.js';
13
+ import type { PsbtCache } from './PsbtCache.js';
14
+ import type { HDSigner, HDSignerAsync } from './types.js';
15
+
16
+ export interface HashForSig {
17
+ readonly pubkey: PublicKey;
18
+ readonly hash: MessageHash;
19
+ readonly leafHash?: Bytes32;
20
+ }
21
+
22
+ /**
23
+ * Class wrapping all signing-related logic for PSBT.
24
+ */
25
+ export class PsbtSigner {
26
+ readonly #cache: PsbtCache;
27
+ readonly #txFromBuffer: (buf: Uint8Array) => Transaction;
28
+
29
+ public constructor(cache: PsbtCache, txFromBuffer: (buf: Uint8Array) => Transaction) {
30
+ this.#cache = cache;
31
+ this.#txFromBuffer = txFromBuffer;
32
+ }
33
+
34
+ public getHashAndSighashType(
35
+ inputs: PsbtInput[],
36
+ inputIndex: number,
37
+ pubkey: Uint8Array,
38
+ sighashTypes: number[],
39
+ ): { hash: MessageHash; sighashType: number } {
40
+ const input = checkForInput(inputs, inputIndex);
41
+ const { hash, sighashType, script } = this.getHashForSig(
42
+ inputIndex,
43
+ input,
44
+ false,
45
+ sighashTypes,
46
+ );
47
+
48
+ checkScriptForPubkey(pubkey as PublicKey, script, 'sign');
49
+ return { hash, sighashType };
50
+ }
51
+
52
+ public getHashForSig(
53
+ inputIndex: number,
54
+ input: PsbtInput,
55
+ forValidate: boolean,
56
+ sighashTypes?: number[],
57
+ ): { script: Script; hash: MessageHash; sighashType: number } {
58
+ const unsignedTx = this.#cache.tx;
59
+ const sighashType = input.sighashType || Transaction.SIGHASH_ALL;
60
+ checkSighashTypeAllowed(sighashType, sighashTypes);
61
+
62
+ let hash: MessageHash;
63
+ let prevout: Output;
64
+
65
+ if (input.nonWitnessUtxo) {
66
+ const nonWitnessUtxoTx = this.#cache.getNonWitnessUtxoTx(
67
+ input,
68
+ inputIndex,
69
+ this.#txFromBuffer,
70
+ );
71
+
72
+ const prevoutHash = unsignedTx.ins[inputIndex]!.hash;
73
+ const utxoHash = nonWitnessUtxoTx.getHash();
74
+
75
+ if (!equals(prevoutHash, utxoHash)) {
76
+ throw new Error(
77
+ `Non-witness UTXO hash for input #${inputIndex} doesn't match the hash specified in the prevout`,
78
+ );
79
+ }
80
+
81
+ const prevoutIndex = unsignedTx.ins[inputIndex]!.index;
82
+ prevout = nonWitnessUtxoTx.outs[prevoutIndex]!;
83
+ } else if (input.witnessUtxo) {
84
+ prevout = {
85
+ script: input.witnessUtxo.script as Script,
86
+ value: input.witnessUtxo.value as Satoshi,
87
+ };
88
+ } else {
89
+ throw new Error('Need a Utxo input item for signing');
90
+ }
91
+
92
+ const { meaningfulScript, type } = getMeaningfulScript(
93
+ prevout.script,
94
+ inputIndex,
95
+ 'input',
96
+ input.redeemScript,
97
+ input.witnessScript,
98
+ );
99
+
100
+ const script = meaningfulScript as Script;
101
+
102
+ if (['p2sh-p2wsh', 'p2wsh'].includes(type)) {
103
+ hash = unsignedTx.hashForWitnessV0(inputIndex, script, prevout.value, sighashType);
104
+ } else if (isP2WPKH(meaningfulScript)) {
105
+ const p2pkhPayment = payments.p2pkh({
106
+ hash: meaningfulScript.subarray(2) as Bytes20,
107
+ });
108
+ if (!p2pkhPayment.output) throw new Error('Unable to create signing script');
109
+ hash = unsignedTx.hashForWitnessV0(
110
+ inputIndex,
111
+ p2pkhPayment.output,
112
+ prevout.value,
113
+ sighashType,
114
+ );
115
+ } else {
116
+ // non-segwit
117
+ if (input.nonWitnessUtxo === undefined && !this.#cache.unsafeSignNonSegwit)
118
+ throw new Error(
119
+ `Input #${inputIndex} has witnessUtxo but non-segwit script: ` +
120
+ toHex(meaningfulScript),
121
+ );
122
+ if (!forValidate && this.#cache.unsafeSignNonSegwit)
123
+ console.warn(
124
+ 'Warning: Signing non-segwit inputs without the full parent transaction ' +
125
+ 'means there is a chance that a miner could feed you incorrect information ' +
126
+ "to trick you into paying large fees. This behavior is the same as Psbt's predecessor " +
127
+ '(TransactionBuilder - now removed) when signing non-segwit scripts. You are not ' +
128
+ 'able to export this Psbt with toBuffer|toBase64|toHex since it is not ' +
129
+ 'BIP174 compliant.\n*********************\nPROCEED WITH CAUTION!\n' +
130
+ '*********************',
131
+ );
132
+ hash = unsignedTx.hashForSignature(inputIndex, script, sighashType);
133
+ }
134
+
135
+ return { script, sighashType, hash };
136
+ }
137
+
138
+ public getTaprootHashesForSig(
139
+ inputIndex: number,
140
+ input: PsbtInput,
141
+ inputs: PsbtInput[],
142
+ pubkey: Uint8Array,
143
+ tapLeafHashToSign?: Uint8Array,
144
+ allowedSighashTypes?: number[],
145
+ ): HashForSig[] {
146
+ const unsignedTx = this.#cache.tx;
147
+ const sighashType = input.sighashType || Transaction.SIGHASH_DEFAULT;
148
+ checkSighashTypeAllowed(sighashType, allowedSighashTypes);
149
+
150
+ if (!this.#cache.prevOuts) {
151
+ const prevOuts = inputs.map((i, index) =>
152
+ this.#cache.getScriptAndAmountFromUtxo(index, i, this.#txFromBuffer),
153
+ );
154
+ this.#cache.prevOuts = prevOuts;
155
+ this.#cache.signingScripts = prevOuts.map((o) => o.script);
156
+ this.#cache.values = prevOuts.map((o) => o.value);
157
+ }
158
+ const signingScripts = this.#cache.signingScripts as readonly Script[];
159
+ const values = this.#cache.values as readonly Satoshi[];
160
+
161
+ if (!this.#cache.taprootHashCache) {
162
+ this.#cache.taprootHashCache = unsignedTx.getTaprootHashCache(signingScripts, values);
163
+ }
164
+ const taprootCache = this.#cache.taprootHashCache;
165
+
166
+ const hashes: HashForSig[] = [];
167
+ if (input.tapInternalKey && !tapLeafHashToSign) {
168
+ const outputKey =
169
+ this.#cache.getPrevoutTaprootKey(inputIndex, input, this.#txFromBuffer) ||
170
+ new Uint8Array(0);
171
+ if (equals(toXOnly(pubkey as PublicKey), outputKey)) {
172
+ const tapKeyHash = unsignedTx.hashForWitnessV1(
173
+ inputIndex,
174
+ signingScripts,
175
+ values,
176
+ sighashType,
177
+ undefined,
178
+ undefined,
179
+ taprootCache,
180
+ );
181
+ hashes.push({ pubkey: pubkey as PublicKey, hash: tapKeyHash });
182
+ }
183
+ }
184
+
185
+ const tapLeafHashes = (input.tapLeafScript || [])
186
+ .filter((tapLeaf) => pubkeyInScript(pubkey, tapLeaf.script))
187
+ .map((tapLeaf) => {
188
+ const hash = tapleafHash({
189
+ output: tapLeaf.script,
190
+ version: tapLeaf.leafVersion,
191
+ });
192
+ return Object.assign({ hash }, tapLeaf);
193
+ })
194
+ .filter((tapLeaf) => !tapLeafHashToSign || equals(tapLeafHashToSign, tapLeaf.hash))
195
+ .map((tapLeaf) => {
196
+ const tapScriptHash = unsignedTx.hashForWitnessV1(
197
+ inputIndex,
198
+ signingScripts,
199
+ values,
200
+ sighashType,
201
+ tapLeaf.hash,
202
+ undefined,
203
+ taprootCache,
204
+ );
205
+
206
+ return {
207
+ pubkey: pubkey as PublicKey,
208
+ hash: tapScriptHash,
209
+ leafHash: tapLeaf.hash,
210
+ };
211
+ });
212
+
213
+ return hashes.concat(tapLeafHashes);
214
+ }
215
+
216
+ public getAllTaprootHashesForSig(
217
+ inputIndex: number,
218
+ input: PsbtInput,
219
+ inputs: PsbtInput[],
220
+ ): HashForSig[] {
221
+ const allPublicKeys: Uint8Array[] = [];
222
+ if (input.tapInternalKey) {
223
+ const key = this.#cache.getPrevoutTaprootKey(inputIndex, input, this.#txFromBuffer);
224
+ if (key) {
225
+ allPublicKeys.push(key);
226
+ }
227
+ }
228
+
229
+ if (input.tapScriptSig) {
230
+ const tapScriptPubkeys = input.tapScriptSig.map((tss) => tss.pubkey);
231
+ allPublicKeys.push(...tapScriptPubkeys);
232
+ }
233
+
234
+ const allHashes = allPublicKeys.map((pubicKey) =>
235
+ this.getTaprootHashesForSig(inputIndex, input, inputs, pubicKey),
236
+ );
237
+
238
+ return allHashes.flat();
239
+ }
240
+
241
+ public trimTaprootSig(signature: Uint8Array): Uint8Array {
242
+ return signature.length === 64 ? signature : signature.subarray(0, 64);
243
+ }
244
+
245
+ public getSignersFromHD<T extends HDSigner | HDSignerAsync>(
246
+ inputIndex: number,
247
+ inputs: PsbtInput[],
248
+ hdKeyPair: T,
249
+ ): T[] {
250
+ const input = checkForInput(inputs, inputIndex);
251
+ if (!input.bip32Derivation || input.bip32Derivation.length === 0) {
252
+ throw new Error('Need bip32Derivation to sign with HD');
253
+ }
254
+ const myDerivations = input.bip32Derivation
255
+ .map((bipDv) => {
256
+ if (equals(bipDv.masterFingerprint, hdKeyPair.fingerprint)) {
257
+ return bipDv;
258
+ } else {
259
+ return;
260
+ }
261
+ })
262
+ .filter((v) => !!v);
263
+ if (myDerivations.length === 0) {
264
+ throw new Error(
265
+ 'Need one bip32Derivation masterFingerprint to match the HDSigner fingerprint',
266
+ );
267
+ }
268
+
269
+ return myDerivations.map((bipDv) => {
270
+ const node = hdKeyPair.derivePath(bipDv.path) as T;
271
+ if (!equals(bipDv.pubkey, node.publicKey)) {
272
+ throw new Error('pubkey did not match bip32Derivation');
273
+ }
274
+ return node;
275
+ });
276
+ }
277
+
278
+ public bip32DerivationIsMine(root: HDSigner): (d: Bip32Derivation) => boolean {
279
+ return (d: Bip32Derivation): boolean => {
280
+ const fingerprint =
281
+ root.fingerprint instanceof Uint8Array
282
+ ? root.fingerprint
283
+ : new Uint8Array(root.fingerprint);
284
+ if (!equals(d.masterFingerprint, fingerprint)) return false;
285
+ const derivedPubkey = root.derivePath(d.path).publicKey;
286
+ const pubkey =
287
+ derivedPubkey instanceof Uint8Array ? derivedPubkey : new Uint8Array(derivedPubkey);
288
+ if (!equals(pubkey, d.pubkey)) return false;
289
+ return true;
290
+ };
291
+ }
292
+ }
293
+
294
+ function checkSighashTypeAllowed(sighashType: number, sighashTypes?: number[]): void {
295
+ if (sighashTypes && !sighashTypes.includes(sighashType)) {
296
+ const str = sighashTypeToString(sighashType);
297
+ throw new Error(
298
+ `Sighash type is not allowed. Retry the sign method passing the ` +
299
+ `sighashTypes array of whitelisted types. Sighash type: ${str}`,
300
+ );
301
+ }
302
+ }
@@ -0,0 +1,82 @@
1
+ import type { Transaction as ITransaction, TransactionFromBuffer } from 'bip174';
2
+ import { fromHex, reverse } from '../io/index.js';
3
+ import { Transaction } from '../transaction.js';
4
+ import type { Bytes32 } from '../types.js';
5
+ import { checkTxEmpty } from './validation.js';
6
+ import type { TransactionInput, TransactionOutput } from './types.js';
7
+
8
+ /**
9
+ * Empty version-2 transaction with zero inputs and zero outputs.
10
+ * Used as the default buffer when constructing a new PsbtTransaction.
11
+ *
12
+ * Layout: [version(4LE)] [inputCount(varint)] [outputCount(varint)] [locktime(4LE)]
13
+ * [02 00 00 00] [00] [00] [00 00 00 00]
14
+ */
15
+ const EMPTY_TX_V2 = new Uint8Array([2, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
16
+
17
+ /**
18
+ * This class implements the Transaction interface from bip174 library.
19
+ * It contains a bitcoinjs-lib Transaction object.
20
+ */
21
+ export class PsbtTransaction implements ITransaction {
22
+ public tx: Transaction;
23
+
24
+ public constructor(buffer: Uint8Array = EMPTY_TX_V2) {
25
+ this.tx = Transaction.fromBuffer(buffer);
26
+ checkTxEmpty(this.tx);
27
+ Object.defineProperty(this, 'tx', {
28
+ enumerable: false,
29
+ writable: true,
30
+ });
31
+ }
32
+
33
+ public getInputOutputCounts(): {
34
+ inputCount: number;
35
+ outputCount: number;
36
+ } {
37
+ return {
38
+ inputCount: this.tx.ins.length,
39
+ outputCount: this.tx.outs.length,
40
+ };
41
+ }
42
+
43
+ public addInput(input: TransactionInput): void {
44
+ if (
45
+ input.hash === undefined ||
46
+ input.index === undefined ||
47
+ (!(input.hash instanceof Uint8Array) && typeof input.hash !== 'string') ||
48
+ typeof input.index !== 'number'
49
+ ) {
50
+ throw new Error('Error adding input.');
51
+ }
52
+ const hash = (
53
+ typeof input.hash === 'string' ? reverse(fromHex(input.hash)) : input.hash
54
+ ) as Bytes32;
55
+
56
+ this.tx.addInput(hash, input.index, input.sequence);
57
+ }
58
+
59
+ public addOutput(output: TransactionOutput): void {
60
+ if (
61
+ output.script === undefined ||
62
+ output.value === undefined ||
63
+ !(output.script instanceof Uint8Array) ||
64
+ typeof output.value !== 'bigint'
65
+ ) {
66
+ throw new Error('Error adding output.');
67
+ }
68
+ this.tx.addOutput(output.script, output.value);
69
+ }
70
+
71
+ public toBuffer(): Uint8Array {
72
+ return this.tx.toBuffer();
73
+ }
74
+ }
75
+
76
+ /**
77
+ * This function is needed to pass to the bip174 base class's fromBuffer.
78
+ * It takes the "transaction buffer" portion of the psbt buffer and returns a
79
+ * Transaction (From the bip174 library) interface.
80
+ */
81
+ export const transactionFromBuffer: TransactionFromBuffer = (buffer: Uint8Array): ITransaction =>
82
+ new PsbtTransaction(buffer);
@@ -16,8 +16,8 @@ import {
16
16
  } from '../payments/bip341.js';
17
17
  import { p2tr } from '../payments/p2tr.js';
18
18
  import { Transaction } from '../transaction.js';
19
+ import type { Bytes32, Tapleaf, Taptree, XOnlyPublicKey } from '../types.js';
19
20
  import { isTapleaf, isTaptree } from '../types.js';
20
- import type { Tapleaf, Taptree, XOnlyPublicKey, Bytes32 } from '../types.js';
21
21
  import { concat, equals } from '../io/index.js';
22
22
  import {
23
23
  isP2TR,
@@ -137,8 +137,12 @@ function getTaprootScripPubkey(tapInternalKey: TapInternalKey, tapTree?: TapTree
137
137
 
138
138
  export function tweakInternalPubKey(inputIndex: number, input: PsbtInput): Uint8Array {
139
139
  const tapInternalKey = input.tapInternalKey;
140
- const tapInternalKeyBuf = tapInternalKey ? new Uint8Array(tapInternalKey) as XOnlyPublicKey : undefined;
141
- const tapMerkleRootBuf = input.tapMerkleRoot ? new Uint8Array(input.tapMerkleRoot) as Bytes32 : undefined;
140
+ const tapInternalKeyBuf = tapInternalKey
141
+ ? (new Uint8Array(tapInternalKey) as XOnlyPublicKey)
142
+ : undefined;
143
+ const tapMerkleRootBuf = input.tapMerkleRoot
144
+ ? (new Uint8Array(input.tapMerkleRoot) as Bytes32)
145
+ : undefined;
142
146
  const outputKey = tapInternalKeyBuf && tweakKey(tapInternalKeyBuf, tapMerkleRootBuf);
143
147
 
144
148
  if (!outputKey)
@@ -1,5 +1,5 @@
1
1
  import type { PartialSig, PsbtInput } from 'bip174';
2
- import { varuint, concat, alloc, equals } from '../io/index.js';
2
+ import { alloc, concat, equals, varuint } from '../io/index.js';
3
3
  import { hash160 } from '../crypto.js';
4
4
  import { p2ms } from '../payments/p2ms.js';
5
5
  import { p2pk } from '../payments/p2pk.js';
package/src/psbt/types.ts CHANGED
@@ -5,15 +5,8 @@
5
5
 
6
6
  import type { Psbt as PsbtBase, PsbtGlobal, PsbtInput, PsbtOutput } from 'bip174';
7
7
  import type { Network } from '../networks.js';
8
- import type { Transaction, TaprootHashCache } from '../transaction.js';
9
- import type {
10
- Bytes32,
11
- PublicKey,
12
- Satoshi,
13
- Script,
14
- Signature,
15
- SchnorrSignature,
16
- } from '../types.js';
8
+ import type { TaprootHashCache, Transaction } from '../transaction.js';
9
+ import type { Bytes32, MessageHash, PublicKey, Satoshi, Script } from '../types.js';
17
10
 
18
11
  /**
19
12
  * Transaction input interface for PSBT.
@@ -52,7 +45,7 @@ export interface PsbtTxOutput extends TransactionOutput {
52
45
  */
53
46
  export type ValidateSigFunction = (
54
47
  pubkey: PublicKey,
55
- msghash: Bytes32,
48
+ msghash: MessageHash,
56
49
  signature: Uint8Array,
57
50
  ) => boolean;
58
51
 
@@ -109,89 +102,7 @@ export interface PsbtOutputExtendedScript extends PsbtOutput {
109
102
  readonly value: Satoshi;
110
103
  }
111
104
 
112
- /**
113
- * Base interface for HD signers.
114
- */
115
- interface HDSignerBase {
116
- /**
117
- * DER format compressed publicKey Uint8Array
118
- */
119
- readonly publicKey: PublicKey;
120
- /**
121
- * The first 4 bytes of the sha256-ripemd160 of the publicKey
122
- */
123
- readonly fingerprint: Uint8Array;
124
- }
125
-
126
- /**
127
- * HD signer interface for synchronous signing.
128
- */
129
- export interface HDSigner extends HDSignerBase {
130
- /**
131
- * The path string must match /^m(\/\d+'?)+$/
132
- * ex. m/44'/0'/0'/1/23 levels with ' must be hard derivations
133
- */
134
- derivePath(path: string): HDSigner;
135
-
136
- /**
137
- * Input hash (the "message digest") for the signature algorithm
138
- * Return a 64 byte signature (32 byte r and 32 byte s in that order)
139
- */
140
- sign(hash: Bytes32): Uint8Array;
141
- }
142
-
143
- /**
144
- * HD signer interface for asynchronous signing.
145
- */
146
- export interface HDSignerAsync extends HDSignerBase {
147
- derivePath(path: string): HDSignerAsync;
148
-
149
- sign(hash: Bytes32): Promise<Uint8Array>;
150
- }
151
-
152
- /**
153
- * Alternative signer interface with lowR support.
154
- */
155
- export interface SignerAlternative {
156
- readonly publicKey: PublicKey;
157
- readonly lowR: boolean;
158
-
159
- sign(hash: Bytes32, lowR?: boolean): Signature;
160
-
161
- verify(hash: Bytes32, signature: Signature): boolean;
162
-
163
- signSchnorr(hash: Bytes32): SchnorrSignature;
164
-
165
- verifySchnorr(hash: Bytes32, signature: SchnorrSignature): boolean;
166
- }
167
-
168
- /**
169
- * Basic signer interface for synchronous signing.
170
- */
171
- export interface Signer {
172
- readonly publicKey: PublicKey;
173
- readonly network?: Network | undefined;
174
-
175
- sign(hash: Bytes32, lowR?: boolean): Signature;
176
-
177
- signSchnorr?(hash: Bytes32): SchnorrSignature;
178
-
179
- getPublicKey?(): PublicKey;
180
- }
181
-
182
- /**
183
- * Basic signer interface for asynchronous signing.
184
- */
185
- export interface SignerAsync {
186
- readonly publicKey: PublicKey;
187
- readonly network?: Network | undefined;
188
-
189
- sign(hash: Bytes32, lowR?: boolean): Promise<Signature>;
190
-
191
- signSchnorr?(hash: Bytes32): Promise<SchnorrSignature>;
192
-
193
- getPublicKey?(): PublicKey;
194
- }
105
+ export type { Signer, SignerAsync, HDSigner, HDSignerAsync } from '@btc-vision/ecpair';
195
106
 
196
107
  /**
197
108
  * Minimal key pair interface for checking Taproot hashes.
@@ -206,7 +117,7 @@ export interface TaprootHashCheckSigner {
206
117
  /**
207
118
  * Internal PSBT cache for computed values.
208
119
  */
209
- export interface PsbtCache {
120
+ export interface PsbtCacheInterface {
210
121
  nonWitnessUtxoTxCache: Transaction[];
211
122
  nonWitnessUtxoBufCache: Uint8Array[];
212
123
  txInCache: TxInCacheMap;
@@ -4,14 +4,14 @@
4
4
  */
5
5
 
6
6
  import type { PsbtInput } from 'bip174';
7
- import { reverse, equals, toHex } from '../io/index.js';
8
- import * as payments from '../payments/index.js';
7
+ import { equals, reverse, toHex } from '../io/index.js';
9
8
  import type { P2SHPayment, Payment, PaymentOpts } from '../payments/index.js';
9
+ import * as payments from '../payments/index.js';
10
10
  import { checkTaprootInputForSigs, isTaprootInput } from './bip371.js';
11
11
  import { checkInputForSig, pubkeyInScript } from './psbtutils.js';
12
12
  import * as bscript from '../script.js';
13
13
  import type { Transaction } from '../transaction.js';
14
- import type { PsbtCache } from './types.js';
14
+ import type { PsbtCacheInterface as PsbtCache } from './types.js';
15
15
  import type { PublicKey, Script } from '../types.js';
16
16
 
17
17
  /**
@@ -142,9 +142,7 @@ export function checkPartialSigSighashes(input: PsbtInput): void {
142
142
  */
143
143
  export function checkScriptForPubkey(pubkey: PublicKey, script: Script, action: string): void {
144
144
  if (!pubkeyInScript(pubkey, script)) {
145
- throw new Error(
146
- `Can not ${action} for this input with the key ${toHex(pubkey)}`,
147
- );
145
+ throw new Error(`Can not ${action} for this input with the key ${toHex(pubkey)}`);
148
146
  }
149
147
  }
150
148
 
@@ -157,12 +155,7 @@ export function checkScriptForPubkey(pubkey: PublicKey, script: Script, action:
157
155
  export function scriptCheckerFactory(
158
156
  payment: (a: Omit<Payment, 'name'>, opts?: PaymentOpts) => Payment,
159
157
  paymentScriptName: string,
160
- ): (
161
- idx: number,
162
- scriptPubKey: Script,
163
- redeemScript: Script,
164
- ioType: 'input' | 'output',
165
- ) => void {
158
+ ): (idx: number, scriptPubKey: Script, redeemScript: Script, ioType: 'input' | 'output') => void {
166
159
  return (
167
160
  inputIndex: number,
168
161
  scriptPubKey: Script,