@btc-vision/bitcoin 6.4.10 → 6.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (220) hide show
  1. package/.babelrc +13 -4
  2. package/.mocharc.json +13 -13
  3. package/.prettierrc.json +12 -12
  4. package/CHANGELOG.md +403 -403
  5. package/CONTRIBUTING.md +83 -83
  6. package/LICENSE +21 -21
  7. package/README.md +201 -201
  8. package/browser/chunks/crypto-C6FlKKmp.js +2006 -0
  9. package/browser/chunks/payments-BE4vwHhV.js +1045 -0
  10. package/browser/chunks/psbt-Dlosf9CT.js +3853 -0
  11. package/browser/chunks/script-COWGdiOo.js +318 -0
  12. package/browser/chunks/transaction-BiXwH2v4.js +421 -0
  13. package/browser/chunks/utils-BKmkTzNZ.js +759 -0
  14. package/browser/crypto/crypto.d.ts +1 -1
  15. package/browser/index.d.ts +7 -27
  16. package/browser/index.js +92 -2
  17. package/build/index.d.ts +7 -27
  18. package/build/index.js +2 -2
  19. package/cjs/package.json +3 -3
  20. package/gulpfile.js +42 -42
  21. package/package.json +153 -150
  22. package/src/bip66.ts +107 -107
  23. package/src/block.ts +233 -233
  24. package/src/bufferutils.ts +188 -188
  25. package/src/crypto.ts +108 -108
  26. package/src/ecc_lib.ts +94 -94
  27. package/src/hooks/AdvancedSignatureManager.ts +104 -104
  28. package/src/hooks/SignatureManager.ts +84 -84
  29. package/src/index.ts +83 -105
  30. package/src/merkle.ts +31 -31
  31. package/src/opcodes.ts +280 -280
  32. package/src/payments/bip341.ts +140 -140
  33. package/src/payments/embed.ts +61 -61
  34. package/src/payments/index.ts +172 -172
  35. package/src/payments/lazy.ts +28 -28
  36. package/src/payments/p2ms.ts +156 -156
  37. package/src/payments/p2op.ts +195 -195
  38. package/src/payments/p2pk.ts +93 -93
  39. package/src/payments/p2pkh.ts +214 -214
  40. package/src/payments/p2tr.ts +309 -309
  41. package/src/psbt/bip371.ts +441 -441
  42. package/src/push_data.ts +97 -97
  43. package/src/script.ts +247 -247
  44. package/src/script_number.ts +72 -72
  45. package/src/script_signature.ts +91 -91
  46. package/src/transaction.ts +656 -656
  47. package/test/address.spec.ts +155 -155
  48. package/test/bitcoin.core.spec.ts +212 -212
  49. package/test/block.spec.ts +171 -171
  50. package/test/bufferutils.spec.ts +450 -450
  51. package/test/crypto.spec.ts +49 -49
  52. package/test/fixtures/address.json +329 -329
  53. package/test/fixtures/block.json +148 -148
  54. package/test/fixtures/bufferutils.json +102 -102
  55. package/test/fixtures/core/README.md +26 -26
  56. package/test/fixtures/core/base58_encode_decode.json +50 -50
  57. package/test/fixtures/core/base58_keys_invalid.json +152 -152
  58. package/test/fixtures/core/base58_keys_valid.json +452 -452
  59. package/test/fixtures/core/blocks.json +27 -27
  60. package/test/fixtures/core/sig_canonical.json +7 -7
  61. package/test/fixtures/core/sig_noncanonical.json +33 -33
  62. package/test/fixtures/core/sighash.json +3505 -3505
  63. package/test/fixtures/core/tx_valid.json +2023 -2023
  64. package/test/fixtures/crypto.json +43 -43
  65. package/test/fixtures/ecdsa.json +217 -217
  66. package/test/fixtures/ecpair.json +141 -141
  67. package/test/fixtures/embed.json +108 -108
  68. package/test/fixtures/p2ms.json +434 -434
  69. package/test/fixtures/p2pk.json +179 -179
  70. package/test/fixtures/p2pkh.json +276 -276
  71. package/test/fixtures/p2sh.json +508 -508
  72. package/test/fixtures/p2tr.json +1198 -1198
  73. package/test/fixtures/p2wpkh.json +290 -290
  74. package/test/fixtures/p2wsh.json +489 -489
  75. package/test/fixtures/psbt.json +924 -924
  76. package/test/fixtures/script.json +465 -465
  77. package/test/fixtures/script_number.json +225 -225
  78. package/test/fixtures/signature.json +140 -140
  79. package/test/fixtures/transaction.json +916 -916
  80. package/test/integration/_regtest.ts +6 -6
  81. package/test/integration/addresses.spec.ts +142 -142
  82. package/test/integration/bip32.spec.ts +130 -130
  83. package/test/integration/blocks.spec.ts +28 -28
  84. package/test/integration/cltv.spec.ts +241 -241
  85. package/test/integration/csv.spec.ts +452 -452
  86. package/test/integration/payments.spec.ts +110 -110
  87. package/test/integration/taproot.spec.ts +663 -663
  88. package/test/integration/transactions.spec.ts +668 -668
  89. package/test/payments.spec.ts +114 -114
  90. package/test/payments.utils.ts +165 -165
  91. package/test/psbt.spec.ts +1285 -1285
  92. package/test/script.spec.ts +186 -186
  93. package/test/script_number.spec.ts +26 -26
  94. package/test/script_signature.spec.ts +66 -66
  95. package/test/transaction.spec.ts +337 -337
  96. package/test/ts-node-register.js +7 -7
  97. package/test/tsconfig.json +48 -48
  98. package/test/types.spec.ts +53 -53
  99. package/tsconfig.base.json +27 -27
  100. package/tsconfig.json +19 -19
  101. package/tsconfig.webpack.json +18 -18
  102. package/vite.config.browser.ts +93 -0
  103. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/.ready +0 -0
  104. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/.release-please-manifest.json +0 -3
  105. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/CHANGELOG.md +0 -962
  106. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/CONTRIBUTING.md +0 -34
  107. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/LICENSE +0 -24
  108. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/README.md +0 -273
  109. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/SECURITY.md +0 -2
  110. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/addon.gypi +0 -204
  111. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/bin/node-gyp.js +0 -138
  112. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/.release-please-manifest.json +0 -3
  113. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/LICENSE +0 -28
  114. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/data/ninja/build.ninja +0 -4
  115. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/data/win/large-pdb-shim.cc +0 -12
  116. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/docs/GypVsCMake.md +0 -116
  117. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/docs/Hacking.md +0 -46
  118. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/docs/InputFormatReference.md +0 -1080
  119. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/docs/LanguageSpecification.md +0 -430
  120. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/docs/README.md +0 -27
  121. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/docs/Testing.md +0 -450
  122. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/docs/UserDocumentation.md +0 -965
  123. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/gyp +0 -8
  124. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/gyp.bat +0 -5
  125. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/gyp_main.py +0 -45
  126. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/MSVSNew.py +0 -365
  127. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/MSVSProject.py +0 -206
  128. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/MSVSSettings.py +0 -1272
  129. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/MSVSSettings_test.py +0 -1547
  130. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/MSVSToolFile.py +0 -59
  131. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/MSVSUserFile.py +0 -153
  132. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/MSVSUtil.py +0 -271
  133. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/MSVSVersion.py +0 -574
  134. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/__init__.py +0 -692
  135. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/common.py +0 -711
  136. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/common_test.py +0 -171
  137. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/easy_xml.py +0 -169
  138. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/easy_xml_test.py +0 -113
  139. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/flock_tool.py +0 -55
  140. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/generator/__init__.py +0 -0
  141. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/generator/analyzer.py +0 -804
  142. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/generator/android.py +0 -1173
  143. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/generator/cmake.py +0 -1318
  144. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/generator/compile_commands_json.py +0 -127
  145. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/generator/dump_dependency_json.py +0 -103
  146. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/generator/eclipse.py +0 -461
  147. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/generator/gypd.py +0 -89
  148. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/generator/gypsh.py +0 -57
  149. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/generator/make.py +0 -2745
  150. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/generator/msvs.py +0 -3976
  151. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/generator/msvs_test.py +0 -44
  152. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/generator/ninja.py +0 -2964
  153. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/generator/ninja_test.py +0 -67
  154. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/generator/xcode.py +0 -1391
  155. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/generator/xcode_test.py +0 -25
  156. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/input.py +0 -3115
  157. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/input_test.py +0 -98
  158. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/mac_tool.py +0 -771
  159. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/msvs_emulation.py +0 -1260
  160. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/ninja_syntax.py +0 -174
  161. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/simple_copy.py +0 -61
  162. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/win_tool.py +0 -373
  163. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/xcode_emulation.py +0 -1938
  164. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/xcode_emulation_test.py +0 -53
  165. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/xcode_ninja.py +0 -302
  166. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/xcodeproj_file.py +0 -3198
  167. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/xml_fix.py +0 -65
  168. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/packaging/LICENSE +0 -3
  169. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/packaging/LICENSE.APACHE +0 -177
  170. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/packaging/LICENSE.BSD +0 -23
  171. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/packaging/__init__.py +0 -15
  172. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/packaging/_elffile.py +0 -108
  173. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/packaging/_manylinux.py +0 -252
  174. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/packaging/_musllinux.py +0 -83
  175. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/packaging/_parser.py +0 -359
  176. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/packaging/_structures.py +0 -61
  177. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/packaging/_tokenizer.py +0 -192
  178. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/packaging/markers.py +0 -252
  179. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/packaging/metadata.py +0 -825
  180. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/packaging/py.typed +0 -0
  181. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/packaging/requirements.py +0 -90
  182. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/packaging/specifiers.py +0 -1030
  183. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/packaging/tags.py +0 -553
  184. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/packaging/utils.py +0 -172
  185. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/packaging/version.py +0 -563
  186. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pyproject.toml +0 -120
  187. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/release-please-config.json +0 -11
  188. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/test_gyp.py +0 -261
  189. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/lib/Find-VisualStudio.cs +0 -250
  190. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/lib/build.js +0 -227
  191. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/lib/clean.js +0 -15
  192. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/lib/configure.js +0 -328
  193. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/lib/create-config-gypi.js +0 -150
  194. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/lib/download.js +0 -39
  195. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/lib/find-node-directory.js +0 -63
  196. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/lib/find-python.js +0 -310
  197. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/lib/find-visualstudio.js +0 -590
  198. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/lib/install.js +0 -415
  199. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/lib/list.js +0 -26
  200. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/lib/log.js +0 -168
  201. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/lib/node-gyp.js +0 -188
  202. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/lib/process-release.js +0 -146
  203. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/lib/rebuild.js +0 -12
  204. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/lib/remove.js +0 -43
  205. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/lib/util.js +0 -81
  206. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/macOS_Catalina_acid_test.sh +0 -21
  207. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/package.json +0 -51
  208. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/release-please-config.json +0 -40
  209. package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/src/win_delay_load_hook.cc +0 -39
  210. package/browser/index.js.LICENSE.txt +0 -14
  211. package/browser/ops.d.ts +0 -126
  212. package/build/ops.d.ts +0 -126
  213. package/build/ops.js +0 -127
  214. package/coverage/tmp/coverage-31752-1735543485354-0.json +0 -1
  215. package/coverage/tmp/coverage-59920-1735543484555-0.json +0 -1
  216. package/coverage/tmp/coverage-66252-1735543483919-0.json +0 -1
  217. package/coverage/tmp/coverage-68440-1735543485236-0.json +0 -1
  218. package/coverage/tmp/coverage-70588-1735543484426-0.json +0 -1
  219. package/coverage/tmp/coverage-79292-1735543485296-0.json +0 -1
  220. package/coverage/tmp/coverage-80212-1735543483980-0.json +0 -1
@@ -1,668 +1,668 @@
1
- import assert from 'assert';
2
- import { BIP32Factory } from 'bip32';
3
- import * as ecc from 'tiny-secp256k1';
4
- import { ECPairFactory } from 'ecpair';
5
- import { describe, it } from 'mocha';
6
- import * as bitcoin from '../../src/index.js';
7
- import { regtestUtils } from './_regtest.js';
8
-
9
- import rng from 'randombytes';
10
-
11
- const ECPair = ECPairFactory(ecc);
12
- const regtest = { ...regtestUtils.network, bech32Opnet: 'opreg' };
13
- const bip32 = BIP32Factory(ecc);
14
-
15
- const validator = (pubkey: Buffer, msghash: Buffer, signature: Buffer): boolean =>
16
- ECPair.fromPublicKey(pubkey).verify(msghash, signature);
17
-
18
- // See bottom of file for some helper functions used to make the payment objects needed.
19
-
20
- describe('bitcoinjs-lib (transactions with psbt)', () => {
21
- it('can create a 1-to-1 Transaction', () => {
22
- const alice = ECPair.fromWIF('L2uPYXe17xSTqbCjZvL2DsyXPCbXspvcu5mHLDYUgzdUbZGSKrSr');
23
- const psbt = new bitcoin.Psbt();
24
- psbt.setVersion(2); // These are defaults. This line is not needed.
25
- psbt.setLocktime(0); // These are defaults. This line is not needed.
26
- psbt.addInput({
27
- // if hash is string, txid, if hash is Buffer, is reversed compared to txid
28
- hash: '7d067b4a697a09d2c3cff7d4d9506c9955e93bff41bf82d439da7d030382bc3e',
29
- index: 0,
30
- sequence: 0xffffffff, // These are defaults. This line is not needed.
31
-
32
- // non-segwit inputs now require passing the whole previous tx as Buffer
33
- nonWitnessUtxo: Buffer.from(
34
- '0200000001f9f34e95b9d5c8abcd20fc5bd4a825d1517be62f0f775e5f36da944d9' +
35
- '452e550000000006b483045022100c86e9a111afc90f64b4904bd609e9eaed80d48' +
36
- 'ca17c162b1aca0a788ac3526f002207bb79b60d4fc6526329bf18a77135dc566020' +
37
- '9e761da46e1c2f1152ec013215801210211755115eabf846720f5cb18f248666fec' +
38
- '631e5e1e66009ce3710ceea5b1ad13ffffffff01' +
39
- // value in satoshis (Int64LE) = 0x015f90 = 90000
40
- '905f010000000000' +
41
- // scriptPubkey length
42
- '19' +
43
- // scriptPubkey
44
- '76a9148bbc95d2709c71607c60ee3f097c1217482f518d88ac' +
45
- // locktime
46
- '00000000',
47
- 'hex',
48
- ),
49
-
50
- // // If this input was segwit, instead of nonWitnessUtxo, you would add
51
- // // a witnessUtxo as follows. The scriptPubkey and the value only are needed.
52
- // witnessUtxo: {
53
- // script: Buffer.from(
54
- // '76a9148bbc95d2709c71607c60ee3f097c1217482f518d88ac',
55
- // 'hex',
56
- // ),
57
- // value: 90000,
58
- // },
59
-
60
- // Not featured here:
61
- // redeemScript. A Buffer of the redeemScript for P2SH
62
- // witnessScript. A Buffer of the witnessScript for P2WSH
63
- });
64
- psbt.addOutput({
65
- address: '1KRMKfeZcmosxALVYESdPNez1AP1mEtywp',
66
- value: 80000,
67
- });
68
- psbt.signInput(0, alice);
69
- psbt.validateSignaturesOfInput(0, validator);
70
- psbt.finalizeAllInputs();
71
- assert.strictEqual(
72
- psbt.extractTransaction().toHex(),
73
- '02000000013ebc8203037dda39d482bf41ff3be955996c50d9d4f7cfc3d2097a694a7' +
74
- 'b067d000000006b483045022100931b6db94aed25d5486884d83fc37160f37f3368c0' +
75
- 'd7f48c757112abefec983802205fda64cff98c849577026eb2ce916a50ea70626a766' +
76
- '9f8596dd89b720a26b4d501210365db9da3f8a260078a7e8f8b708a1161468fb2323f' +
77
- 'fda5ec16b261ec1056f455ffffffff0180380100000000001976a914ca0d36044e0dc' +
78
- '08a22724efa6f6a07b0ec4c79aa88ac00000000',
79
- );
80
- });
81
-
82
- it('can create (and broadcast via 3PBP) a typical Transaction', async () => {
83
- // these are { payment: Payment; keys: ECPair[] }
84
- const alice1 = createPayment('p2pkh');
85
- const alice2 = createPayment('p2pkh');
86
-
87
- // give Alice 2 unspent outputs
88
- const inputData1 = await getInputData(5e4, alice1.payment, false, 'noredeem');
89
- const inputData2 = await getInputData(7e4, alice2.payment, false, 'noredeem');
90
- {
91
- const {
92
- hash, // string of txid or Buffer of tx hash. (txid and hash are reverse order)
93
- index, // the output index of the txo you are spending
94
- nonWitnessUtxo, // the full previous transaction as a Buffer
95
- } = inputData1;
96
- assert.deepStrictEqual({ hash, index, nonWitnessUtxo }, inputData1);
97
- }
98
-
99
- // network is only needed if you pass an address to addOutput
100
- // using script (Buffer of scriptPubkey) instead will avoid needed network.
101
- const psbt = new bitcoin.Psbt({ network: regtest })
102
- .addInput(inputData1) // alice1 unspent
103
- .addInput(inputData2) // alice2 unspent
104
- .addOutput({
105
- address: 'mwCwTceJvYV27KXBc3NJZys6CjsgsoeHmf',
106
- value: 8e4,
107
- }) // the actual "spend"
108
- .addOutput({
109
- address: alice2.payment.address, // OR script, which is a Buffer.
110
- value: 1e4,
111
- }); // Alice's change
112
- // (in)(5e4 + 7e4) - (out)(8e4 + 1e4) = (fee)3e4 = 30000, this is the miner fee
113
-
114
- // Let's show a new feature with PSBT.
115
- // We can have multiple signers sign in parallel and combine them.
116
- // (this is not necessary, but a nice feature)
117
-
118
- // encode to send out to the signers
119
- const psbtBaseText = psbt.toBase64();
120
-
121
- // each signer imports
122
- const signer1 = bitcoin.Psbt.fromBase64(psbtBaseText);
123
- const signer2 = bitcoin.Psbt.fromBase64(psbtBaseText);
124
-
125
- // Alice signs each input with the respective private keys
126
- // signInput and signInputAsync are better
127
- // (They take the input index explicitly as the first arg)
128
- signer1.signAllInputs(alice1.keys[0]);
129
- signer2.signAllInputs(alice2.keys[0]);
130
-
131
- // If your signer object's sign method returns a promise, use the following
132
- // await signer2.signAllInputsAsync(alice2.keys[0])
133
-
134
- // encode to send back to combiner (signer 1 and 2 are not near each other)
135
- const s1text = signer1.toBase64();
136
- const s2text = signer2.toBase64();
137
-
138
- const final1 = bitcoin.Psbt.fromBase64(s1text);
139
- const final2 = bitcoin.Psbt.fromBase64(s2text);
140
-
141
- // final1.combine(final2) would give the exact same result
142
- psbt.combine(final1, final2);
143
-
144
- // Finalizer wants to check all signatures are valid before finalizing.
145
- // If the finalizer wants to check for specific pubkeys, the second arg
146
- // can be passed. See the first multisig example below.
147
- assert.strictEqual(psbt.validateSignaturesOfInput(0, validator), true);
148
- assert.strictEqual(psbt.validateSignaturesOfInput(1, validator), true);
149
-
150
- // This step it new. Since we separate the signing operation and
151
- // the creation of the scriptSig and witness stack, we are able to
152
- psbt.finalizeAllInputs();
153
-
154
- // build and broadcast our RegTest network
155
- await regtestUtils.broadcast(psbt.extractTransaction().toHex());
156
- // to build and broadcast to the actual Bitcoin network, see https://github.com/bitcoinjs/bitcoinjs-lib/issues/839
157
- });
158
-
159
- /*it('can create (and broadcast via 3PBP) a Transaction with an OP_RETURN output', async () => {
160
- const alice1 = createPayment('p2pkh');
161
- const inputData1 = await getInputData(2e5, alice1.payment, false, 'noredeem');
162
-
163
- const data = Buffer.from('bitcoinjs-lib', 'utf8');
164
- const embed = bitcoin.payments.embed({ data: [data] });
165
-
166
- const psbt = new bitcoin.Psbt({ network: regtest })
167
- .addInput(inputData1)
168
- .addOutput({
169
- script: embed.output!,
170
- value: 1000,
171
- })
172
- .addOutput({
173
- address: regtestUtils.RANDOM_ADDRESS,
174
- value: 1e5,
175
- })
176
- .signInput(0, alice1.keys[0]);
177
-
178
- assert.strictEqual(psbt.validateSignaturesOfInput(0, validator), true);
179
- psbt.finalizeAllInputs();
180
-
181
- // build and broadcast to the RegTest network
182
- await regtestUtils.broadcast(psbt.extractTransaction().toHex());
183
- });*/
184
-
185
- it('can create (and broadcast via 3PBP) a Transaction, w/ a P2SH(P2MS(2 of 4)) (multisig) input', async () => {
186
- const multisig = createPayment('p2sh-p2ms(2 of 4)');
187
- const inputData1 = await getInputData(2e4, multisig.payment, false, 'p2sh');
188
- {
189
- const {
190
- hash,
191
- index,
192
- nonWitnessUtxo,
193
- redeemScript, // NEW: P2SH needs to give redeemScript when adding an input.
194
- } = inputData1;
195
- assert.deepStrictEqual({ hash, index, nonWitnessUtxo, redeemScript }, inputData1);
196
- }
197
-
198
- const psbt = new bitcoin.Psbt({ network: regtest })
199
- .addInput(inputData1)
200
- .addOutput({
201
- address: regtestUtils.RANDOM_ADDRESS,
202
- value: 1e4,
203
- })
204
- .signInput(0, multisig.keys[0])
205
- .signInput(0, multisig.keys[2]);
206
-
207
- assert.strictEqual(psbt.validateSignaturesOfInput(0, validator), true);
208
- assert.strictEqual(
209
- psbt.validateSignaturesOfInput(0, validator, multisig.keys[0].publicKey),
210
- true,
211
- );
212
- assert.throws(() => {
213
- psbt.validateSignaturesOfInput(0, validator, multisig.keys[3].publicKey);
214
- }, new RegExp('No signatures for this pubkey'));
215
- psbt.finalizeAllInputs();
216
-
217
- const tx = psbt.extractTransaction();
218
-
219
- // build and broadcast to the Bitcoin RegTest network
220
- await regtestUtils.broadcast(tx.toHex());
221
-
222
- await regtestUtils.verify({
223
- txId: tx.getId(),
224
- address: regtestUtils.RANDOM_ADDRESS,
225
- vout: 0,
226
- value: 1e4,
227
- });
228
- });
229
-
230
- it('can create (and broadcast via 3PBP) a Transaction, w/ a P2SH(P2WPKH) input', async () => {
231
- const p2sh = createPayment('p2sh-p2wpkh');
232
- const inputData = await getInputData(5e4, p2sh.payment, true, 'p2sh');
233
- const inputData2 = await getInputData(5e4, p2sh.payment, true, 'p2sh');
234
- {
235
- const {
236
- hash,
237
- index,
238
- witnessUtxo, // NEW: this is an object of the output being spent { script: Buffer; value: Satoshis; }
239
- redeemScript,
240
- } = inputData;
241
- assert.deepStrictEqual({ hash, index, witnessUtxo, redeemScript }, inputData);
242
- }
243
- const keyPair = p2sh.keys[0];
244
- const outputData = {
245
- script: p2sh.payment.output, // sending to myself for fun
246
- value: 2e4,
247
- };
248
- const outputData2 = {
249
- script: p2sh.payment.output, // sending to myself for fun
250
- value: 7e4,
251
- };
252
-
253
- const tx = new bitcoin.Psbt()
254
- .addInputs([inputData, inputData2])
255
- .addOutputs([outputData, outputData2])
256
- .signAllInputs(keyPair)
257
- .finalizeAllInputs()
258
- .extractTransaction();
259
-
260
- // build and broadcast to the Bitcoin RegTest network
261
- await regtestUtils.broadcast(tx.toHex());
262
-
263
- await regtestUtils.verify({
264
- txId: tx.getId(),
265
- address: p2sh.payment.address,
266
- vout: 0,
267
- value: 2e4,
268
- });
269
- });
270
-
271
- it('can create (and broadcast via 3PBP) a Transaction, w/ a P2SH(P2WPKH) input with nonWitnessUtxo', async () => {
272
- // For learning purposes, ignore this test.
273
- // REPEATING ABOVE BUT WITH nonWitnessUtxo by passing false to getInputData
274
- const p2sh = createPayment('p2sh-p2wpkh');
275
- const inputData = await getInputData(5e4, p2sh.payment, false, 'p2sh');
276
- const inputData2 = await getInputData(5e4, p2sh.payment, false, 'p2sh');
277
- const keyPair = p2sh.keys[0];
278
- const outputData = {
279
- script: p2sh.payment.output,
280
- value: 2e4,
281
- };
282
- const outputData2 = {
283
- script: p2sh.payment.output,
284
- value: 7e4,
285
- };
286
- const tx = new bitcoin.Psbt()
287
- .addInputs([inputData, inputData2])
288
- .addOutputs([outputData, outputData2])
289
- .signAllInputs(keyPair)
290
- .finalizeAllInputs()
291
- .extractTransaction();
292
- await regtestUtils.broadcast(tx.toHex());
293
- await regtestUtils.verify({
294
- txId: tx.getId(),
295
- address: p2sh.payment.address,
296
- vout: 0,
297
- value: 2e4,
298
- });
299
- });
300
-
301
- it('can create (and broadcast via 3PBP) a Transaction, w/ a P2WPKH input', async () => {
302
- // the only thing that changes is you don't give a redeemscript for input data
303
-
304
- const p2wpkh = createPayment('p2wpkh');
305
- const inputData = await getInputData(5e4, p2wpkh.payment, true, 'noredeem');
306
- {
307
- const { hash, index, witnessUtxo } = inputData;
308
- assert.deepStrictEqual({ hash, index, witnessUtxo }, inputData);
309
- }
310
-
311
- const psbt = new bitcoin.Psbt({ network: regtest })
312
- .addInput(inputData)
313
- .addOutput({
314
- address: regtestUtils.RANDOM_ADDRESS,
315
- value: 2e4,
316
- })
317
- .signInput(0, p2wpkh.keys[0]);
318
-
319
- assert.strictEqual(psbt.validateSignaturesOfInput(0, validator), true);
320
- psbt.finalizeAllInputs();
321
-
322
- const tx = psbt.extractTransaction();
323
-
324
- // build and broadcast to the Bitcoin RegTest network
325
- await regtestUtils.broadcast(tx.toHex());
326
-
327
- await regtestUtils.verify({
328
- txId: tx.getId(),
329
- address: regtestUtils.RANDOM_ADDRESS,
330
- vout: 0,
331
- value: 2e4,
332
- });
333
- });
334
-
335
- it('can create (and broadcast via 3PBP) a Transaction, w/ a P2WPKH input with nonWitnessUtxo', async () => {
336
- // For learning purposes, ignore this test.
337
- // REPEATING ABOVE BUT WITH nonWitnessUtxo by passing false to getInputData
338
- const p2wpkh = createPayment('p2wpkh');
339
- const inputData = await getInputData(5e4, p2wpkh.payment, false, 'noredeem');
340
- const psbt = new bitcoin.Psbt({ network: regtest })
341
- .addInput(inputData)
342
- .addOutput({
343
- address: regtestUtils.RANDOM_ADDRESS,
344
- value: 2e4,
345
- })
346
- .signInput(0, p2wpkh.keys[0]);
347
- psbt.finalizeAllInputs();
348
- const tx = psbt.extractTransaction();
349
- await regtestUtils.broadcast(tx.toHex());
350
- await regtestUtils.verify({
351
- txId: tx.getId(),
352
- address: regtestUtils.RANDOM_ADDRESS,
353
- vout: 0,
354
- value: 2e4,
355
- });
356
- });
357
-
358
- it('can create (and broadcast via 3PBP) a Transaction, w/ a P2WSH(P2PK) input', async () => {
359
- const p2wsh = createPayment('p2wsh-p2pk');
360
- const inputData = await getInputData(5e4, p2wsh.payment, true, 'p2wsh');
361
- {
362
- const {
363
- hash,
364
- index,
365
- witnessUtxo,
366
- witnessScript, // NEW: A Buffer of the witnessScript
367
- } = inputData;
368
- assert.deepStrictEqual({ hash, index, witnessUtxo, witnessScript }, inputData);
369
- }
370
-
371
- const psbt = new bitcoin.Psbt({ network: regtest })
372
- .addInput(inputData)
373
- .addOutput({
374
- address: regtestUtils.RANDOM_ADDRESS,
375
- value: 2e4,
376
- })
377
- .signInput(0, p2wsh.keys[0]);
378
-
379
- assert.strictEqual(psbt.validateSignaturesOfInput(0, validator), true);
380
- psbt.finalizeAllInputs();
381
-
382
- const tx = psbt.extractTransaction();
383
-
384
- // build and broadcast to the Bitcoin RegTest network
385
- await regtestUtils.broadcast(tx.toHex());
386
-
387
- await regtestUtils.verify({
388
- txId: tx.getId(),
389
- address: regtestUtils.RANDOM_ADDRESS,
390
- vout: 0,
391
- value: 2e4,
392
- });
393
- });
394
-
395
- it('can create (and broadcast via 3PBP) a Transaction, w/ a P2WSH(P2PK) input with nonWitnessUtxo', async () => {
396
- // For learning purposes, ignore this test.
397
- // REPEATING ABOVE BUT WITH nonWitnessUtxo by passing false to getInputData
398
- const p2wsh = createPayment('p2wsh-p2pk');
399
- const inputData = await getInputData(5e4, p2wsh.payment, false, 'p2wsh');
400
- const psbt = new bitcoin.Psbt({ network: regtest })
401
- .addInput(inputData)
402
- .addOutput({
403
- address: regtestUtils.RANDOM_ADDRESS,
404
- value: 2e4,
405
- })
406
- .signInput(0, p2wsh.keys[0]);
407
- psbt.finalizeAllInputs();
408
- const tx = psbt.extractTransaction();
409
- await regtestUtils.broadcast(tx.toHex());
410
- await regtestUtils.verify({
411
- txId: tx.getId(),
412
- address: regtestUtils.RANDOM_ADDRESS,
413
- vout: 0,
414
- value: 2e4,
415
- });
416
- });
417
-
418
- it(
419
- 'can create (and broadcast via 3PBP) a Transaction, w/ a ' +
420
- 'P2SH(P2WSH(P2MS(3 of 4))) (SegWit multisig) input',
421
- async () => {
422
- const p2sh = createPayment('p2sh-p2wsh-p2ms(3 of 4)');
423
- const inputData = await getInputData(5e4, p2sh.payment, true, 'p2sh-p2wsh');
424
- {
425
- const { hash, index, witnessUtxo, redeemScript, witnessScript } = inputData;
426
- assert.deepStrictEqual(
427
- { hash, index, witnessUtxo, redeemScript, witnessScript },
428
- inputData,
429
- );
430
- }
431
-
432
- const psbt = new bitcoin.Psbt({ network: regtest })
433
- .addInput(inputData)
434
- .addOutput({
435
- address: regtestUtils.RANDOM_ADDRESS,
436
- value: 2e4,
437
- })
438
- .signInput(0, p2sh.keys[0])
439
- .signInput(0, p2sh.keys[2])
440
- .signInput(0, p2sh.keys[3]);
441
-
442
- assert.strictEqual(psbt.validateSignaturesOfInput(0, validator), true);
443
- assert.strictEqual(
444
- psbt.validateSignaturesOfInput(0, validator, p2sh.keys[3].publicKey),
445
- true,
446
- );
447
- assert.throws(() => {
448
- psbt.validateSignaturesOfInput(0, validator, p2sh.keys[1].publicKey);
449
- }, new RegExp('No signatures for this pubkey'));
450
- psbt.finalizeAllInputs();
451
-
452
- const tx = psbt.extractTransaction();
453
-
454
- // build and broadcast to the Bitcoin RegTest network
455
- await regtestUtils.broadcast(tx.toHex());
456
-
457
- await regtestUtils.verify({
458
- txId: tx.getId(),
459
- address: regtestUtils.RANDOM_ADDRESS,
460
- vout: 0,
461
- value: 2e4,
462
- });
463
- },
464
- );
465
-
466
- it(
467
- 'can create (and broadcast via 3PBP) a Transaction, w/ a ' +
468
- 'P2SH(P2WSH(P2MS(3 of 4))) (SegWit multisig) input with nonWitnessUtxo',
469
- async () => {
470
- // For learning purposes, ignore this test.
471
- // REPEATING ABOVE BUT WITH nonWitnessUtxo by passing false to getInputData
472
- const p2sh = createPayment('p2sh-p2wsh-p2ms(3 of 4)');
473
- const inputData = await getInputData(5e4, p2sh.payment, false, 'p2sh-p2wsh');
474
- const psbt = new bitcoin.Psbt({ network: regtest })
475
- .addInput(inputData)
476
- .addOutput({
477
- address: regtestUtils.RANDOM_ADDRESS,
478
- value: 2e4,
479
- })
480
- .signInput(0, p2sh.keys[0])
481
- .signInput(0, p2sh.keys[2])
482
- .signInput(0, p2sh.keys[3]);
483
- psbt.finalizeAllInputs();
484
- const tx = psbt.extractTransaction();
485
- await regtestUtils.broadcast(tx.toHex());
486
- await regtestUtils.verify({
487
- txId: tx.getId(),
488
- address: regtestUtils.RANDOM_ADDRESS,
489
- vout: 0,
490
- value: 2e4,
491
- });
492
- },
493
- );
494
-
495
- it(
496
- 'can create (and broadcast via 3PBP) a Transaction, w/ a ' +
497
- 'P2SH(P2MS(2 of 2)) input with nonWitnessUtxo',
498
- async () => {
499
- const myKey = ECPair.makeRandom({ network: regtest });
500
- const myKeys = [myKey, ECPair.fromPrivateKey(myKey.privateKey!, { network: regtest })];
501
- const p2sh = createPayment('p2sh-p2ms(2 of 2)', myKeys);
502
- const inputData = await getInputData(5e4, p2sh.payment, false, 'p2sh');
503
- const psbt = new bitcoin.Psbt({ network: regtest })
504
- .addInput(inputData)
505
- .addOutput({
506
- address: regtestUtils.RANDOM_ADDRESS,
507
- value: 2e4,
508
- })
509
- .signInput(0, p2sh.keys[0]);
510
- psbt.finalizeAllInputs();
511
- const tx = psbt.extractTransaction();
512
- await regtestUtils.broadcast(tx.toHex());
513
- await regtestUtils.verify({
514
- txId: tx.getId(),
515
- address: regtestUtils.RANDOM_ADDRESS,
516
- vout: 0,
517
- value: 2e4,
518
- });
519
- },
520
- );
521
-
522
- it('can create (and broadcast via 3PBP) a Transaction, w/ a P2WPKH input using HD', async () => {
523
- const hdRoot = bip32.fromSeed(rng(64));
524
- const masterFingerprint = hdRoot.fingerprint;
525
- const path = "m/84'/0'/0'/0/0";
526
- const childNode = hdRoot.derivePath(path);
527
- const pubkey = childNode.publicKey;
528
-
529
- // This information should be added to your input via updateInput
530
- // You can add multiple bip32Derivation objects for multisig, but
531
- // each must have a unique pubkey.
532
- //
533
- // This is useful because as long as you store the masterFingerprint on
534
- // the PSBT Creator's server, you can have the PSBT Creator do the heavy
535
- // lifting with derivation from your m/84'/0'/0' xpub, (deriving only 0/0 )
536
- // and your signer just needs to pass in an HDSigner interface (ie. bip32 library)
537
- const updateData = {
538
- bip32Derivation: [
539
- {
540
- masterFingerprint,
541
- path,
542
- pubkey,
543
- },
544
- ],
545
- };
546
- const p2wpkh = createPayment('p2wpkh', [childNode]);
547
- const inputData = await getInputData(5e4, p2wpkh.payment, true, 'noredeem');
548
- {
549
- const { hash, index, witnessUtxo } = inputData;
550
- assert.deepStrictEqual({ hash, index, witnessUtxo }, inputData);
551
- }
552
-
553
- // You can add extra attributes for updateData into the addInput(s) object(s)
554
- Object.assign(inputData, updateData);
555
-
556
- const psbt = new bitcoin.Psbt({ network: regtest })
557
- .addInput(inputData)
558
- // .updateInput(0, updateData) // if you didn't merge the bip32Derivation with inputData
559
- .addOutput({
560
- address: regtestUtils.RANDOM_ADDRESS,
561
- value: 2e4,
562
- })
563
- .signInputHD(0, hdRoot); // must sign with root!!!
564
-
565
- assert.strictEqual(psbt.validateSignaturesOfInput(0, validator), true);
566
- assert.strictEqual(psbt.validateSignaturesOfInput(0, validator, childNode.publicKey), true);
567
- psbt.finalizeAllInputs();
568
-
569
- const tx = psbt.extractTransaction();
570
-
571
- // build and broadcast to the Bitcoin RegTest network
572
- await regtestUtils.broadcast(tx.toHex());
573
-
574
- await regtestUtils.verify({
575
- txId: tx.getId(),
576
- address: regtestUtils.RANDOM_ADDRESS,
577
- vout: 0,
578
- value: 2e4,
579
- });
580
- });
581
- });
582
-
583
- function createPayment(_type: string, myKeys?: any[], network?: any): any {
584
- network = network || regtest;
585
- const splitType = _type.split('-').reverse();
586
- const isMultisig = splitType[0].slice(0, 4) === 'p2ms';
587
- const keys = myKeys || [];
588
- let m: number | undefined;
589
- if (isMultisig) {
590
- const match = splitType[0].match(/^p2ms\((\d+) of (\d+)\)$/);
591
- m = parseInt(match![1], 10);
592
- let n = parseInt(match![2], 10);
593
- if (keys.length > 0 && keys.length !== n) {
594
- throw new Error('Need n keys for multisig');
595
- }
596
- while (!myKeys && n > 1) {
597
- keys.push(ECPair.makeRandom({ network }));
598
- n--;
599
- }
600
- }
601
- if (!myKeys) keys.push(ECPair.makeRandom({ network }));
602
-
603
- let payment: any;
604
- splitType.forEach((type) => {
605
- if (type.slice(0, 4) === 'p2ms') {
606
- payment = bitcoin.payments.p2ms({
607
- m,
608
- pubkeys: keys.map((key) => key.publicKey).sort((a, b) => a.compare(b)),
609
- network,
610
- });
611
- } else if (['p2sh', 'p2wsh'].indexOf(type) > -1) {
612
- payment = (bitcoin.payments as any)[type]({
613
- redeem: payment,
614
- network,
615
- });
616
- } else {
617
- payment = (bitcoin.payments as any)[type]({
618
- pubkey: keys[0].publicKey,
619
- network,
620
- });
621
- }
622
- });
623
-
624
- return {
625
- payment,
626
- keys,
627
- };
628
- }
629
-
630
- function getWitnessUtxo(out: any): any {
631
- delete out.address;
632
- out.script = Buffer.from(out.script, 'hex');
633
- return out;
634
- }
635
-
636
- async function getInputData(
637
- amount: number,
638
- payment: any,
639
- isSegwit: boolean,
640
- redeemType: string,
641
- ): Promise<any> {
642
- const unspent = await regtestUtils.faucetComplex(payment.output, amount);
643
- const utx = await regtestUtils.fetch(unspent.txId);
644
- // for non segwit inputs, you must pass the full transaction buffer
645
- const nonWitnessUtxo = Buffer.from(utx.txHex, 'hex');
646
- // for segwit inputs, you only need the output script and value as an object.
647
- const witnessUtxo = getWitnessUtxo(utx.outs[unspent.vout]);
648
- const mixin = isSegwit ? { witnessUtxo } : { nonWitnessUtxo };
649
- const mixin2: any = {};
650
- switch (redeemType) {
651
- case 'p2sh':
652
- mixin2.redeemScript = payment.redeem.output;
653
- break;
654
- case 'p2wsh':
655
- mixin2.witnessScript = payment.redeem.output;
656
- break;
657
- case 'p2sh-p2wsh':
658
- mixin2.witnessScript = payment.redeem.redeem.output;
659
- mixin2.redeemScript = payment.redeem.output;
660
- break;
661
- }
662
- return {
663
- hash: unspent.txId,
664
- index: unspent.vout,
665
- ...mixin,
666
- ...mixin2,
667
- };
668
- }
1
+ import assert from 'assert';
2
+ import { BIP32Factory } from 'bip32';
3
+ import * as ecc from 'tiny-secp256k1';
4
+ import { ECPairFactory } from 'ecpair';
5
+ import { describe, it } from 'mocha';
6
+ import * as bitcoin from '../../src/index.js';
7
+ import { regtestUtils } from './_regtest.js';
8
+
9
+ import rng from 'randombytes';
10
+
11
+ const ECPair = ECPairFactory(ecc);
12
+ const regtest = { ...regtestUtils.network, bech32Opnet: 'opreg' };
13
+ const bip32 = BIP32Factory(ecc);
14
+
15
+ const validator = (pubkey: Buffer, msghash: Buffer, signature: Buffer): boolean =>
16
+ ECPair.fromPublicKey(pubkey).verify(msghash, signature);
17
+
18
+ // See bottom of file for some helper functions used to make the payment objects needed.
19
+
20
+ describe('bitcoinjs-lib (transactions with psbt)', () => {
21
+ it('can create a 1-to-1 Transaction', () => {
22
+ const alice = ECPair.fromWIF('L2uPYXe17xSTqbCjZvL2DsyXPCbXspvcu5mHLDYUgzdUbZGSKrSr');
23
+ const psbt = new bitcoin.Psbt();
24
+ psbt.setVersion(2); // These are defaults. This line is not needed.
25
+ psbt.setLocktime(0); // These are defaults. This line is not needed.
26
+ psbt.addInput({
27
+ // if hash is string, txid, if hash is Buffer, is reversed compared to txid
28
+ hash: '7d067b4a697a09d2c3cff7d4d9506c9955e93bff41bf82d439da7d030382bc3e',
29
+ index: 0,
30
+ sequence: 0xffffffff, // These are defaults. This line is not needed.
31
+
32
+ // non-segwit inputs now require passing the whole previous tx as Buffer
33
+ nonWitnessUtxo: Buffer.from(
34
+ '0200000001f9f34e95b9d5c8abcd20fc5bd4a825d1517be62f0f775e5f36da944d9' +
35
+ '452e550000000006b483045022100c86e9a111afc90f64b4904bd609e9eaed80d48' +
36
+ 'ca17c162b1aca0a788ac3526f002207bb79b60d4fc6526329bf18a77135dc566020' +
37
+ '9e761da46e1c2f1152ec013215801210211755115eabf846720f5cb18f248666fec' +
38
+ '631e5e1e66009ce3710ceea5b1ad13ffffffff01' +
39
+ // value in satoshis (Int64LE) = 0x015f90 = 90000
40
+ '905f010000000000' +
41
+ // scriptPubkey length
42
+ '19' +
43
+ // scriptPubkey
44
+ '76a9148bbc95d2709c71607c60ee3f097c1217482f518d88ac' +
45
+ // locktime
46
+ '00000000',
47
+ 'hex',
48
+ ),
49
+
50
+ // // If this input was segwit, instead of nonWitnessUtxo, you would add
51
+ // // a witnessUtxo as follows. The scriptPubkey and the value only are needed.
52
+ // witnessUtxo: {
53
+ // script: Buffer.from(
54
+ // '76a9148bbc95d2709c71607c60ee3f097c1217482f518d88ac',
55
+ // 'hex',
56
+ // ),
57
+ // value: 90000,
58
+ // },
59
+
60
+ // Not featured here:
61
+ // redeemScript. A Buffer of the redeemScript for P2SH
62
+ // witnessScript. A Buffer of the witnessScript for P2WSH
63
+ });
64
+ psbt.addOutput({
65
+ address: '1KRMKfeZcmosxALVYESdPNez1AP1mEtywp',
66
+ value: 80000,
67
+ });
68
+ psbt.signInput(0, alice);
69
+ psbt.validateSignaturesOfInput(0, validator);
70
+ psbt.finalizeAllInputs();
71
+ assert.strictEqual(
72
+ psbt.extractTransaction().toHex(),
73
+ '02000000013ebc8203037dda39d482bf41ff3be955996c50d9d4f7cfc3d2097a694a7' +
74
+ 'b067d000000006b483045022100931b6db94aed25d5486884d83fc37160f37f3368c0' +
75
+ 'd7f48c757112abefec983802205fda64cff98c849577026eb2ce916a50ea70626a766' +
76
+ '9f8596dd89b720a26b4d501210365db9da3f8a260078a7e8f8b708a1161468fb2323f' +
77
+ 'fda5ec16b261ec1056f455ffffffff0180380100000000001976a914ca0d36044e0dc' +
78
+ '08a22724efa6f6a07b0ec4c79aa88ac00000000',
79
+ );
80
+ });
81
+
82
+ it('can create (and broadcast via 3PBP) a typical Transaction', async () => {
83
+ // these are { payment: Payment; keys: ECPair[] }
84
+ const alice1 = createPayment('p2pkh');
85
+ const alice2 = createPayment('p2pkh');
86
+
87
+ // give Alice 2 unspent outputs
88
+ const inputData1 = await getInputData(5e4, alice1.payment, false, 'noredeem');
89
+ const inputData2 = await getInputData(7e4, alice2.payment, false, 'noredeem');
90
+ {
91
+ const {
92
+ hash, // string of txid or Buffer of tx hash. (txid and hash are reverse order)
93
+ index, // the output index of the txo you are spending
94
+ nonWitnessUtxo, // the full previous transaction as a Buffer
95
+ } = inputData1;
96
+ assert.deepStrictEqual({ hash, index, nonWitnessUtxo }, inputData1);
97
+ }
98
+
99
+ // network is only needed if you pass an address to addOutput
100
+ // using script (Buffer of scriptPubkey) instead will avoid needed network.
101
+ const psbt = new bitcoin.Psbt({ network: regtest })
102
+ .addInput(inputData1) // alice1 unspent
103
+ .addInput(inputData2) // alice2 unspent
104
+ .addOutput({
105
+ address: 'mwCwTceJvYV27KXBc3NJZys6CjsgsoeHmf',
106
+ value: 8e4,
107
+ }) // the actual "spend"
108
+ .addOutput({
109
+ address: alice2.payment.address, // OR script, which is a Buffer.
110
+ value: 1e4,
111
+ }); // Alice's change
112
+ // (in)(5e4 + 7e4) - (out)(8e4 + 1e4) = (fee)3e4 = 30000, this is the miner fee
113
+
114
+ // Let's show a new feature with PSBT.
115
+ // We can have multiple signers sign in parallel and combine them.
116
+ // (this is not necessary, but a nice feature)
117
+
118
+ // encode to send out to the signers
119
+ const psbtBaseText = psbt.toBase64();
120
+
121
+ // each signer imports
122
+ const signer1 = bitcoin.Psbt.fromBase64(psbtBaseText);
123
+ const signer2 = bitcoin.Psbt.fromBase64(psbtBaseText);
124
+
125
+ // Alice signs each input with the respective private keys
126
+ // signInput and signInputAsync are better
127
+ // (They take the input index explicitly as the first arg)
128
+ signer1.signAllInputs(alice1.keys[0]);
129
+ signer2.signAllInputs(alice2.keys[0]);
130
+
131
+ // If your signer object's sign method returns a promise, use the following
132
+ // await signer2.signAllInputsAsync(alice2.keys[0])
133
+
134
+ // encode to send back to combiner (signer 1 and 2 are not near each other)
135
+ const s1text = signer1.toBase64();
136
+ const s2text = signer2.toBase64();
137
+
138
+ const final1 = bitcoin.Psbt.fromBase64(s1text);
139
+ const final2 = bitcoin.Psbt.fromBase64(s2text);
140
+
141
+ // final1.combine(final2) would give the exact same result
142
+ psbt.combine(final1, final2);
143
+
144
+ // Finalizer wants to check all signatures are valid before finalizing.
145
+ // If the finalizer wants to check for specific pubkeys, the second arg
146
+ // can be passed. See the first multisig example below.
147
+ assert.strictEqual(psbt.validateSignaturesOfInput(0, validator), true);
148
+ assert.strictEqual(psbt.validateSignaturesOfInput(1, validator), true);
149
+
150
+ // This step it new. Since we separate the signing operation and
151
+ // the creation of the scriptSig and witness stack, we are able to
152
+ psbt.finalizeAllInputs();
153
+
154
+ // build and broadcast our RegTest network
155
+ await regtestUtils.broadcast(psbt.extractTransaction().toHex());
156
+ // to build and broadcast to the actual Bitcoin network, see https://github.com/bitcoinjs/bitcoinjs-lib/issues/839
157
+ });
158
+
159
+ /*it('can create (and broadcast via 3PBP) a Transaction with an OP_RETURN output', async () => {
160
+ const alice1 = createPayment('p2pkh');
161
+ const inputData1 = await getInputData(2e5, alice1.payment, false, 'noredeem');
162
+
163
+ const data = Buffer.from('bitcoinjs-lib', 'utf8');
164
+ const embed = bitcoin.payments.embed({ data: [data] });
165
+
166
+ const psbt = new bitcoin.Psbt({ network: regtest })
167
+ .addInput(inputData1)
168
+ .addOutput({
169
+ script: embed.output!,
170
+ value: 1000,
171
+ })
172
+ .addOutput({
173
+ address: regtestUtils.RANDOM_ADDRESS,
174
+ value: 1e5,
175
+ })
176
+ .signInput(0, alice1.keys[0]);
177
+
178
+ assert.strictEqual(psbt.validateSignaturesOfInput(0, validator), true);
179
+ psbt.finalizeAllInputs();
180
+
181
+ // build and broadcast to the RegTest network
182
+ await regtestUtils.broadcast(psbt.extractTransaction().toHex());
183
+ });*/
184
+
185
+ it('can create (and broadcast via 3PBP) a Transaction, w/ a P2SH(P2MS(2 of 4)) (multisig) input', async () => {
186
+ const multisig = createPayment('p2sh-p2ms(2 of 4)');
187
+ const inputData1 = await getInputData(2e4, multisig.payment, false, 'p2sh');
188
+ {
189
+ const {
190
+ hash,
191
+ index,
192
+ nonWitnessUtxo,
193
+ redeemScript, // NEW: P2SH needs to give redeemScript when adding an input.
194
+ } = inputData1;
195
+ assert.deepStrictEqual({ hash, index, nonWitnessUtxo, redeemScript }, inputData1);
196
+ }
197
+
198
+ const psbt = new bitcoin.Psbt({ network: regtest })
199
+ .addInput(inputData1)
200
+ .addOutput({
201
+ address: regtestUtils.RANDOM_ADDRESS,
202
+ value: 1e4,
203
+ })
204
+ .signInput(0, multisig.keys[0])
205
+ .signInput(0, multisig.keys[2]);
206
+
207
+ assert.strictEqual(psbt.validateSignaturesOfInput(0, validator), true);
208
+ assert.strictEqual(
209
+ psbt.validateSignaturesOfInput(0, validator, multisig.keys[0].publicKey),
210
+ true,
211
+ );
212
+ assert.throws(() => {
213
+ psbt.validateSignaturesOfInput(0, validator, multisig.keys[3].publicKey);
214
+ }, new RegExp('No signatures for this pubkey'));
215
+ psbt.finalizeAllInputs();
216
+
217
+ const tx = psbt.extractTransaction();
218
+
219
+ // build and broadcast to the Bitcoin RegTest network
220
+ await regtestUtils.broadcast(tx.toHex());
221
+
222
+ await regtestUtils.verify({
223
+ txId: tx.getId(),
224
+ address: regtestUtils.RANDOM_ADDRESS,
225
+ vout: 0,
226
+ value: 1e4,
227
+ });
228
+ });
229
+
230
+ it('can create (and broadcast via 3PBP) a Transaction, w/ a P2SH(P2WPKH) input', async () => {
231
+ const p2sh = createPayment('p2sh-p2wpkh');
232
+ const inputData = await getInputData(5e4, p2sh.payment, true, 'p2sh');
233
+ const inputData2 = await getInputData(5e4, p2sh.payment, true, 'p2sh');
234
+ {
235
+ const {
236
+ hash,
237
+ index,
238
+ witnessUtxo, // NEW: this is an object of the output being spent { script: Buffer; value: Satoshis; }
239
+ redeemScript,
240
+ } = inputData;
241
+ assert.deepStrictEqual({ hash, index, witnessUtxo, redeemScript }, inputData);
242
+ }
243
+ const keyPair = p2sh.keys[0];
244
+ const outputData = {
245
+ script: p2sh.payment.output, // sending to myself for fun
246
+ value: 2e4,
247
+ };
248
+ const outputData2 = {
249
+ script: p2sh.payment.output, // sending to myself for fun
250
+ value: 7e4,
251
+ };
252
+
253
+ const tx = new bitcoin.Psbt()
254
+ .addInputs([inputData, inputData2])
255
+ .addOutputs([outputData, outputData2])
256
+ .signAllInputs(keyPair)
257
+ .finalizeAllInputs()
258
+ .extractTransaction();
259
+
260
+ // build and broadcast to the Bitcoin RegTest network
261
+ await regtestUtils.broadcast(tx.toHex());
262
+
263
+ await regtestUtils.verify({
264
+ txId: tx.getId(),
265
+ address: p2sh.payment.address,
266
+ vout: 0,
267
+ value: 2e4,
268
+ });
269
+ });
270
+
271
+ it('can create (and broadcast via 3PBP) a Transaction, w/ a P2SH(P2WPKH) input with nonWitnessUtxo', async () => {
272
+ // For learning purposes, ignore this test.
273
+ // REPEATING ABOVE BUT WITH nonWitnessUtxo by passing false to getInputData
274
+ const p2sh = createPayment('p2sh-p2wpkh');
275
+ const inputData = await getInputData(5e4, p2sh.payment, false, 'p2sh');
276
+ const inputData2 = await getInputData(5e4, p2sh.payment, false, 'p2sh');
277
+ const keyPair = p2sh.keys[0];
278
+ const outputData = {
279
+ script: p2sh.payment.output,
280
+ value: 2e4,
281
+ };
282
+ const outputData2 = {
283
+ script: p2sh.payment.output,
284
+ value: 7e4,
285
+ };
286
+ const tx = new bitcoin.Psbt()
287
+ .addInputs([inputData, inputData2])
288
+ .addOutputs([outputData, outputData2])
289
+ .signAllInputs(keyPair)
290
+ .finalizeAllInputs()
291
+ .extractTransaction();
292
+ await regtestUtils.broadcast(tx.toHex());
293
+ await regtestUtils.verify({
294
+ txId: tx.getId(),
295
+ address: p2sh.payment.address,
296
+ vout: 0,
297
+ value: 2e4,
298
+ });
299
+ });
300
+
301
+ it('can create (and broadcast via 3PBP) a Transaction, w/ a P2WPKH input', async () => {
302
+ // the only thing that changes is you don't give a redeemscript for input data
303
+
304
+ const p2wpkh = createPayment('p2wpkh');
305
+ const inputData = await getInputData(5e4, p2wpkh.payment, true, 'noredeem');
306
+ {
307
+ const { hash, index, witnessUtxo } = inputData;
308
+ assert.deepStrictEqual({ hash, index, witnessUtxo }, inputData);
309
+ }
310
+
311
+ const psbt = new bitcoin.Psbt({ network: regtest })
312
+ .addInput(inputData)
313
+ .addOutput({
314
+ address: regtestUtils.RANDOM_ADDRESS,
315
+ value: 2e4,
316
+ })
317
+ .signInput(0, p2wpkh.keys[0]);
318
+
319
+ assert.strictEqual(psbt.validateSignaturesOfInput(0, validator), true);
320
+ psbt.finalizeAllInputs();
321
+
322
+ const tx = psbt.extractTransaction();
323
+
324
+ // build and broadcast to the Bitcoin RegTest network
325
+ await regtestUtils.broadcast(tx.toHex());
326
+
327
+ await regtestUtils.verify({
328
+ txId: tx.getId(),
329
+ address: regtestUtils.RANDOM_ADDRESS,
330
+ vout: 0,
331
+ value: 2e4,
332
+ });
333
+ });
334
+
335
+ it('can create (and broadcast via 3PBP) a Transaction, w/ a P2WPKH input with nonWitnessUtxo', async () => {
336
+ // For learning purposes, ignore this test.
337
+ // REPEATING ABOVE BUT WITH nonWitnessUtxo by passing false to getInputData
338
+ const p2wpkh = createPayment('p2wpkh');
339
+ const inputData = await getInputData(5e4, p2wpkh.payment, false, 'noredeem');
340
+ const psbt = new bitcoin.Psbt({ network: regtest })
341
+ .addInput(inputData)
342
+ .addOutput({
343
+ address: regtestUtils.RANDOM_ADDRESS,
344
+ value: 2e4,
345
+ })
346
+ .signInput(0, p2wpkh.keys[0]);
347
+ psbt.finalizeAllInputs();
348
+ const tx = psbt.extractTransaction();
349
+ await regtestUtils.broadcast(tx.toHex());
350
+ await regtestUtils.verify({
351
+ txId: tx.getId(),
352
+ address: regtestUtils.RANDOM_ADDRESS,
353
+ vout: 0,
354
+ value: 2e4,
355
+ });
356
+ });
357
+
358
+ it('can create (and broadcast via 3PBP) a Transaction, w/ a P2WSH(P2PK) input', async () => {
359
+ const p2wsh = createPayment('p2wsh-p2pk');
360
+ const inputData = await getInputData(5e4, p2wsh.payment, true, 'p2wsh');
361
+ {
362
+ const {
363
+ hash,
364
+ index,
365
+ witnessUtxo,
366
+ witnessScript, // NEW: A Buffer of the witnessScript
367
+ } = inputData;
368
+ assert.deepStrictEqual({ hash, index, witnessUtxo, witnessScript }, inputData);
369
+ }
370
+
371
+ const psbt = new bitcoin.Psbt({ network: regtest })
372
+ .addInput(inputData)
373
+ .addOutput({
374
+ address: regtestUtils.RANDOM_ADDRESS,
375
+ value: 2e4,
376
+ })
377
+ .signInput(0, p2wsh.keys[0]);
378
+
379
+ assert.strictEqual(psbt.validateSignaturesOfInput(0, validator), true);
380
+ psbt.finalizeAllInputs();
381
+
382
+ const tx = psbt.extractTransaction();
383
+
384
+ // build and broadcast to the Bitcoin RegTest network
385
+ await regtestUtils.broadcast(tx.toHex());
386
+
387
+ await regtestUtils.verify({
388
+ txId: tx.getId(),
389
+ address: regtestUtils.RANDOM_ADDRESS,
390
+ vout: 0,
391
+ value: 2e4,
392
+ });
393
+ });
394
+
395
+ it('can create (and broadcast via 3PBP) a Transaction, w/ a P2WSH(P2PK) input with nonWitnessUtxo', async () => {
396
+ // For learning purposes, ignore this test.
397
+ // REPEATING ABOVE BUT WITH nonWitnessUtxo by passing false to getInputData
398
+ const p2wsh = createPayment('p2wsh-p2pk');
399
+ const inputData = await getInputData(5e4, p2wsh.payment, false, 'p2wsh');
400
+ const psbt = new bitcoin.Psbt({ network: regtest })
401
+ .addInput(inputData)
402
+ .addOutput({
403
+ address: regtestUtils.RANDOM_ADDRESS,
404
+ value: 2e4,
405
+ })
406
+ .signInput(0, p2wsh.keys[0]);
407
+ psbt.finalizeAllInputs();
408
+ const tx = psbt.extractTransaction();
409
+ await regtestUtils.broadcast(tx.toHex());
410
+ await regtestUtils.verify({
411
+ txId: tx.getId(),
412
+ address: regtestUtils.RANDOM_ADDRESS,
413
+ vout: 0,
414
+ value: 2e4,
415
+ });
416
+ });
417
+
418
+ it(
419
+ 'can create (and broadcast via 3PBP) a Transaction, w/ a ' +
420
+ 'P2SH(P2WSH(P2MS(3 of 4))) (SegWit multisig) input',
421
+ async () => {
422
+ const p2sh = createPayment('p2sh-p2wsh-p2ms(3 of 4)');
423
+ const inputData = await getInputData(5e4, p2sh.payment, true, 'p2sh-p2wsh');
424
+ {
425
+ const { hash, index, witnessUtxo, redeemScript, witnessScript } = inputData;
426
+ assert.deepStrictEqual(
427
+ { hash, index, witnessUtxo, redeemScript, witnessScript },
428
+ inputData,
429
+ );
430
+ }
431
+
432
+ const psbt = new bitcoin.Psbt({ network: regtest })
433
+ .addInput(inputData)
434
+ .addOutput({
435
+ address: regtestUtils.RANDOM_ADDRESS,
436
+ value: 2e4,
437
+ })
438
+ .signInput(0, p2sh.keys[0])
439
+ .signInput(0, p2sh.keys[2])
440
+ .signInput(0, p2sh.keys[3]);
441
+
442
+ assert.strictEqual(psbt.validateSignaturesOfInput(0, validator), true);
443
+ assert.strictEqual(
444
+ psbt.validateSignaturesOfInput(0, validator, p2sh.keys[3].publicKey),
445
+ true,
446
+ );
447
+ assert.throws(() => {
448
+ psbt.validateSignaturesOfInput(0, validator, p2sh.keys[1].publicKey);
449
+ }, new RegExp('No signatures for this pubkey'));
450
+ psbt.finalizeAllInputs();
451
+
452
+ const tx = psbt.extractTransaction();
453
+
454
+ // build and broadcast to the Bitcoin RegTest network
455
+ await regtestUtils.broadcast(tx.toHex());
456
+
457
+ await regtestUtils.verify({
458
+ txId: tx.getId(),
459
+ address: regtestUtils.RANDOM_ADDRESS,
460
+ vout: 0,
461
+ value: 2e4,
462
+ });
463
+ },
464
+ );
465
+
466
+ it(
467
+ 'can create (and broadcast via 3PBP) a Transaction, w/ a ' +
468
+ 'P2SH(P2WSH(P2MS(3 of 4))) (SegWit multisig) input with nonWitnessUtxo',
469
+ async () => {
470
+ // For learning purposes, ignore this test.
471
+ // REPEATING ABOVE BUT WITH nonWitnessUtxo by passing false to getInputData
472
+ const p2sh = createPayment('p2sh-p2wsh-p2ms(3 of 4)');
473
+ const inputData = await getInputData(5e4, p2sh.payment, false, 'p2sh-p2wsh');
474
+ const psbt = new bitcoin.Psbt({ network: regtest })
475
+ .addInput(inputData)
476
+ .addOutput({
477
+ address: regtestUtils.RANDOM_ADDRESS,
478
+ value: 2e4,
479
+ })
480
+ .signInput(0, p2sh.keys[0])
481
+ .signInput(0, p2sh.keys[2])
482
+ .signInput(0, p2sh.keys[3]);
483
+ psbt.finalizeAllInputs();
484
+ const tx = psbt.extractTransaction();
485
+ await regtestUtils.broadcast(tx.toHex());
486
+ await regtestUtils.verify({
487
+ txId: tx.getId(),
488
+ address: regtestUtils.RANDOM_ADDRESS,
489
+ vout: 0,
490
+ value: 2e4,
491
+ });
492
+ },
493
+ );
494
+
495
+ it(
496
+ 'can create (and broadcast via 3PBP) a Transaction, w/ a ' +
497
+ 'P2SH(P2MS(2 of 2)) input with nonWitnessUtxo',
498
+ async () => {
499
+ const myKey = ECPair.makeRandom({ network: regtest });
500
+ const myKeys = [myKey, ECPair.fromPrivateKey(myKey.privateKey!, { network: regtest })];
501
+ const p2sh = createPayment('p2sh-p2ms(2 of 2)', myKeys);
502
+ const inputData = await getInputData(5e4, p2sh.payment, false, 'p2sh');
503
+ const psbt = new bitcoin.Psbt({ network: regtest })
504
+ .addInput(inputData)
505
+ .addOutput({
506
+ address: regtestUtils.RANDOM_ADDRESS,
507
+ value: 2e4,
508
+ })
509
+ .signInput(0, p2sh.keys[0]);
510
+ psbt.finalizeAllInputs();
511
+ const tx = psbt.extractTransaction();
512
+ await regtestUtils.broadcast(tx.toHex());
513
+ await regtestUtils.verify({
514
+ txId: tx.getId(),
515
+ address: regtestUtils.RANDOM_ADDRESS,
516
+ vout: 0,
517
+ value: 2e4,
518
+ });
519
+ },
520
+ );
521
+
522
+ it('can create (and broadcast via 3PBP) a Transaction, w/ a P2WPKH input using HD', async () => {
523
+ const hdRoot = bip32.fromSeed(rng(64));
524
+ const masterFingerprint = hdRoot.fingerprint;
525
+ const path = "m/84'/0'/0'/0/0";
526
+ const childNode = hdRoot.derivePath(path);
527
+ const pubkey = childNode.publicKey;
528
+
529
+ // This information should be added to your input via updateInput
530
+ // You can add multiple bip32Derivation objects for multisig, but
531
+ // each must have a unique pubkey.
532
+ //
533
+ // This is useful because as long as you store the masterFingerprint on
534
+ // the PSBT Creator's server, you can have the PSBT Creator do the heavy
535
+ // lifting with derivation from your m/84'/0'/0' xpub, (deriving only 0/0 )
536
+ // and your signer just needs to pass in an HDSigner interface (ie. bip32 library)
537
+ const updateData = {
538
+ bip32Derivation: [
539
+ {
540
+ masterFingerprint,
541
+ path,
542
+ pubkey,
543
+ },
544
+ ],
545
+ };
546
+ const p2wpkh = createPayment('p2wpkh', [childNode]);
547
+ const inputData = await getInputData(5e4, p2wpkh.payment, true, 'noredeem');
548
+ {
549
+ const { hash, index, witnessUtxo } = inputData;
550
+ assert.deepStrictEqual({ hash, index, witnessUtxo }, inputData);
551
+ }
552
+
553
+ // You can add extra attributes for updateData into the addInput(s) object(s)
554
+ Object.assign(inputData, updateData);
555
+
556
+ const psbt = new bitcoin.Psbt({ network: regtest })
557
+ .addInput(inputData)
558
+ // .updateInput(0, updateData) // if you didn't merge the bip32Derivation with inputData
559
+ .addOutput({
560
+ address: regtestUtils.RANDOM_ADDRESS,
561
+ value: 2e4,
562
+ })
563
+ .signInputHD(0, hdRoot); // must sign with root!!!
564
+
565
+ assert.strictEqual(psbt.validateSignaturesOfInput(0, validator), true);
566
+ assert.strictEqual(psbt.validateSignaturesOfInput(0, validator, childNode.publicKey), true);
567
+ psbt.finalizeAllInputs();
568
+
569
+ const tx = psbt.extractTransaction();
570
+
571
+ // build and broadcast to the Bitcoin RegTest network
572
+ await regtestUtils.broadcast(tx.toHex());
573
+
574
+ await regtestUtils.verify({
575
+ txId: tx.getId(),
576
+ address: regtestUtils.RANDOM_ADDRESS,
577
+ vout: 0,
578
+ value: 2e4,
579
+ });
580
+ });
581
+ });
582
+
583
+ function createPayment(_type: string, myKeys?: any[], network?: any): any {
584
+ network = network || regtest;
585
+ const splitType = _type.split('-').reverse();
586
+ const isMultisig = splitType[0].slice(0, 4) === 'p2ms';
587
+ const keys = myKeys || [];
588
+ let m: number | undefined;
589
+ if (isMultisig) {
590
+ const match = splitType[0].match(/^p2ms\((\d+) of (\d+)\)$/);
591
+ m = parseInt(match![1], 10);
592
+ let n = parseInt(match![2], 10);
593
+ if (keys.length > 0 && keys.length !== n) {
594
+ throw new Error('Need n keys for multisig');
595
+ }
596
+ while (!myKeys && n > 1) {
597
+ keys.push(ECPair.makeRandom({ network }));
598
+ n--;
599
+ }
600
+ }
601
+ if (!myKeys) keys.push(ECPair.makeRandom({ network }));
602
+
603
+ let payment: any;
604
+ splitType.forEach((type) => {
605
+ if (type.slice(0, 4) === 'p2ms') {
606
+ payment = bitcoin.payments.p2ms({
607
+ m,
608
+ pubkeys: keys.map((key) => key.publicKey).sort((a, b) => a.compare(b)),
609
+ network,
610
+ });
611
+ } else if (['p2sh', 'p2wsh'].indexOf(type) > -1) {
612
+ payment = (bitcoin.payments as any)[type]({
613
+ redeem: payment,
614
+ network,
615
+ });
616
+ } else {
617
+ payment = (bitcoin.payments as any)[type]({
618
+ pubkey: keys[0].publicKey,
619
+ network,
620
+ });
621
+ }
622
+ });
623
+
624
+ return {
625
+ payment,
626
+ keys,
627
+ };
628
+ }
629
+
630
+ function getWitnessUtxo(out: any): any {
631
+ delete out.address;
632
+ out.script = Buffer.from(out.script, 'hex');
633
+ return out;
634
+ }
635
+
636
+ async function getInputData(
637
+ amount: number,
638
+ payment: any,
639
+ isSegwit: boolean,
640
+ redeemType: string,
641
+ ): Promise<any> {
642
+ const unspent = await regtestUtils.faucetComplex(payment.output, amount);
643
+ const utx = await regtestUtils.fetch(unspent.txId);
644
+ // for non segwit inputs, you must pass the full transaction buffer
645
+ const nonWitnessUtxo = Buffer.from(utx.txHex, 'hex');
646
+ // for segwit inputs, you only need the output script and value as an object.
647
+ const witnessUtxo = getWitnessUtxo(utx.outs[unspent.vout]);
648
+ const mixin = isSegwit ? { witnessUtxo } : { nonWitnessUtxo };
649
+ const mixin2: any = {};
650
+ switch (redeemType) {
651
+ case 'p2sh':
652
+ mixin2.redeemScript = payment.redeem.output;
653
+ break;
654
+ case 'p2wsh':
655
+ mixin2.witnessScript = payment.redeem.output;
656
+ break;
657
+ case 'p2sh-p2wsh':
658
+ mixin2.witnessScript = payment.redeem.redeem.output;
659
+ mixin2.redeemScript = payment.redeem.output;
660
+ break;
661
+ }
662
+ return {
663
+ hash: unspent.txId,
664
+ index: unspent.vout,
665
+ ...mixin,
666
+ ...mixin2,
667
+ };
668
+ }