@btc-vision/bitcoin 6.4.9 → 6.4.11
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.
- package/.babelrc +4 -4
- package/.mocharc.json +13 -13
- package/.prettierrc.json +12 -12
- package/CHANGELOG.md +403 -403
- package/CONTRIBUTING.md +83 -83
- package/LICENSE +21 -21
- package/README.md +201 -201
- package/browser/index.d.ts +1 -1
- package/browser/index.js +1 -1
- package/browser/{ops.d.ts → opcodes.d.ts} +2 -3
- package/browser/script.d.ts +2 -2
- package/build/address.js +2 -1
- package/build/index.d.ts +1 -1
- package/build/index.js +1 -1
- package/build/{ops.d.ts → opcodes.d.ts} +2 -3
- package/build/{ops.js → opcodes.js} +4 -5
- package/build/payments/embed.js +1 -1
- package/build/payments/p2ms.js +1 -1
- package/build/payments/p2op.js +1 -1
- package/build/payments/p2pk.js +1 -1
- package/build/payments/p2pkh.js +1 -1
- package/build/payments/p2sh.js +3 -3
- package/build/payments/p2tr.js +1 -1
- package/build/payments/p2wpkh.js +2 -2
- package/build/payments/p2wsh.js +2 -2
- package/build/push_data.js +9 -9
- package/build/script.d.ts +2 -2
- package/build/script.js +12 -12
- package/build/transaction.js +1 -1
- package/cjs/package.json +3 -3
- package/gulpfile.js +42 -42
- package/package.json +150 -150
- package/src/address.ts +304 -303
- package/src/bip66.ts +107 -107
- package/src/block.ts +233 -233
- package/src/bufferutils.ts +188 -188
- package/src/crypto.ts +108 -108
- package/src/ecc_lib.ts +94 -94
- package/src/hooks/AdvancedSignatureManager.ts +104 -104
- package/src/hooks/SignatureManager.ts +84 -84
- package/src/index.ts +105 -105
- package/src/merkle.ts +31 -31
- package/src/{ops.ts → opcodes.ts} +280 -282
- package/src/payments/bip341.ts +140 -140
- package/src/payments/embed.ts +61 -61
- package/src/payments/index.ts +172 -172
- package/src/payments/lazy.ts +28 -28
- package/src/payments/p2ms.ts +156 -156
- package/src/payments/p2op.ts +195 -195
- package/src/payments/p2pk.ts +93 -93
- package/src/payments/p2pkh.ts +214 -214
- package/src/payments/p2sh.ts +210 -210
- package/src/payments/p2tr.ts +309 -309
- package/src/payments/p2wpkh.ts +144 -144
- package/src/payments/p2wsh.ts +217 -217
- package/src/psbt/bip371.ts +441 -441
- package/src/push_data.ts +97 -97
- package/src/script.ts +247 -247
- package/src/script_number.ts +72 -72
- package/src/script_signature.ts +91 -91
- package/src/transaction.ts +656 -656
- package/test/address.spec.ts +155 -155
- package/test/bitcoin.core.spec.ts +212 -212
- package/test/block.spec.ts +171 -171
- package/test/bufferutils.spec.ts +450 -450
- package/test/crypto.spec.ts +49 -49
- package/test/fixtures/address.json +329 -329
- package/test/fixtures/block.json +148 -148
- package/test/fixtures/bufferutils.json +102 -102
- package/test/fixtures/core/README.md +26 -26
- package/test/fixtures/core/base58_encode_decode.json +50 -50
- package/test/fixtures/core/base58_keys_invalid.json +152 -152
- package/test/fixtures/core/base58_keys_valid.json +452 -452
- package/test/fixtures/core/blocks.json +27 -27
- package/test/fixtures/core/sig_canonical.json +7 -7
- package/test/fixtures/core/sig_noncanonical.json +33 -33
- package/test/fixtures/core/sighash.json +3505 -3505
- package/test/fixtures/core/tx_valid.json +2023 -2023
- package/test/fixtures/crypto.json +43 -43
- package/test/fixtures/ecdsa.json +217 -217
- package/test/fixtures/ecpair.json +141 -141
- package/test/fixtures/embed.json +108 -108
- package/test/fixtures/p2ms.json +434 -434
- package/test/fixtures/p2pk.json +179 -179
- package/test/fixtures/p2pkh.json +276 -276
- package/test/fixtures/p2sh.json +508 -508
- package/test/fixtures/p2tr.json +1198 -1198
- package/test/fixtures/p2wpkh.json +290 -290
- package/test/fixtures/p2wsh.json +489 -489
- package/test/fixtures/psbt.json +924 -924
- package/test/fixtures/script.json +465 -465
- package/test/fixtures/script_number.json +225 -225
- package/test/fixtures/signature.json +140 -140
- package/test/fixtures/transaction.json +916 -916
- package/test/integration/_regtest.ts +6 -6
- package/test/integration/addresses.spec.ts +142 -142
- package/test/integration/bip32.spec.ts +130 -130
- package/test/integration/blocks.spec.ts +28 -28
- package/test/integration/cltv.spec.ts +241 -241
- package/test/integration/csv.spec.ts +452 -452
- package/test/integration/payments.spec.ts +110 -110
- package/test/integration/taproot.spec.ts +663 -663
- package/test/integration/transactions.spec.ts +668 -668
- package/test/payments.spec.ts +114 -114
- package/test/payments.utils.ts +165 -165
- package/test/psbt.spec.ts +1285 -1285
- package/test/script.spec.ts +186 -186
- package/test/script_number.spec.ts +26 -26
- package/test/script_signature.spec.ts +66 -66
- package/test/transaction.spec.ts +337 -337
- package/test/ts-node-register.js +7 -7
- package/test/tsconfig.json +48 -48
- package/test/types.spec.ts +53 -53
- package/tsconfig.base.json +27 -27
- package/tsconfig.json +19 -19
- package/tsconfig.webpack.json +18 -18
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/.ready +0 -0
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/.release-please-manifest.json +0 -3
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/CHANGELOG.md +0 -962
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/CONTRIBUTING.md +0 -34
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/LICENSE +0 -24
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/README.md +0 -273
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/SECURITY.md +0 -2
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/addon.gypi +0 -204
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/bin/node-gyp.js +0 -138
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/.release-please-manifest.json +0 -3
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/LICENSE +0 -28
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/data/ninja/build.ninja +0 -4
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/data/win/large-pdb-shim.cc +0 -12
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/docs/GypVsCMake.md +0 -116
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/docs/Hacking.md +0 -46
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/docs/InputFormatReference.md +0 -1080
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/docs/LanguageSpecification.md +0 -430
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/docs/README.md +0 -27
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/docs/Testing.md +0 -450
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/docs/UserDocumentation.md +0 -965
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/gyp +0 -8
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/gyp.bat +0 -5
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/gyp_main.py +0 -45
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/MSVSNew.py +0 -365
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/MSVSProject.py +0 -206
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/MSVSSettings.py +0 -1272
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/MSVSSettings_test.py +0 -1547
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/MSVSToolFile.py +0 -59
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/MSVSUserFile.py +0 -153
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/MSVSUtil.py +0 -271
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/MSVSVersion.py +0 -574
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/__init__.py +0 -692
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/common.py +0 -711
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/common_test.py +0 -171
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/easy_xml.py +0 -169
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/easy_xml_test.py +0 -113
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/flock_tool.py +0 -55
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/generator/__init__.py +0 -0
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/generator/analyzer.py +0 -804
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/generator/android.py +0 -1173
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/generator/cmake.py +0 -1318
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/generator/compile_commands_json.py +0 -127
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/generator/dump_dependency_json.py +0 -103
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/generator/eclipse.py +0 -461
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/generator/gypd.py +0 -89
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/generator/gypsh.py +0 -57
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/generator/make.py +0 -2745
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/generator/msvs.py +0 -3976
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/generator/msvs_test.py +0 -44
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/generator/ninja.py +0 -2964
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/generator/ninja_test.py +0 -67
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/generator/xcode.py +0 -1391
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/generator/xcode_test.py +0 -25
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/input.py +0 -3115
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/input_test.py +0 -98
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/mac_tool.py +0 -771
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/msvs_emulation.py +0 -1260
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/ninja_syntax.py +0 -174
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/simple_copy.py +0 -61
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/win_tool.py +0 -373
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/xcode_emulation.py +0 -1938
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/xcode_emulation_test.py +0 -53
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/xcode_ninja.py +0 -302
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/xcodeproj_file.py +0 -3198
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/gyp/xml_fix.py +0 -65
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/packaging/LICENSE +0 -3
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/packaging/LICENSE.APACHE +0 -177
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/packaging/LICENSE.BSD +0 -23
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/packaging/__init__.py +0 -15
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/packaging/_elffile.py +0 -108
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/packaging/_manylinux.py +0 -252
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/packaging/_musllinux.py +0 -83
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/packaging/_parser.py +0 -359
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/packaging/_structures.py +0 -61
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/packaging/_tokenizer.py +0 -192
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/packaging/markers.py +0 -252
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/packaging/metadata.py +0 -825
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/packaging/py.typed +0 -0
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/packaging/requirements.py +0 -90
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/packaging/specifiers.py +0 -1030
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/packaging/tags.py +0 -553
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/packaging/utils.py +0 -172
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pylib/packaging/version.py +0 -563
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/pyproject.toml +0 -120
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/release-please-config.json +0 -11
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/gyp/test_gyp.py +0 -261
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/lib/Find-VisualStudio.cs +0 -250
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/lib/build.js +0 -227
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/lib/clean.js +0 -15
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/lib/configure.js +0 -328
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/lib/create-config-gypi.js +0 -150
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/lib/download.js +0 -39
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/lib/find-node-directory.js +0 -63
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/lib/find-python.js +0 -310
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/lib/find-visualstudio.js +0 -590
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/lib/install.js +0 -415
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/lib/list.js +0 -26
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/lib/log.js +0 -168
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/lib/node-gyp.js +0 -188
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/lib/process-release.js +0 -146
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/lib/rebuild.js +0 -12
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/lib/remove.js +0 -43
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/lib/util.js +0 -81
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/macOS_Catalina_acid_test.sh +0 -21
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/package.json +0 -51
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/release-please-config.json +0 -40
- package/.yarn/unplugged/node-gyp-npm-10.2.0-cad1109948/node_modules/node-gyp/src/win_delay_load_hook.cc +0 -39
- package/coverage/tmp/coverage-31752-1735543485354-0.json +0 -1
- package/coverage/tmp/coverage-59920-1735543484555-0.json +0 -1
- package/coverage/tmp/coverage-66252-1735543483919-0.json +0 -1
- package/coverage/tmp/coverage-68440-1735543485236-0.json +0 -1
- package/coverage/tmp/coverage-70588-1735543484426-0.json +0 -1
- package/coverage/tmp/coverage-79292-1735543485296-0.json +0 -1
- package/coverage/tmp/coverage-80212-1735543483980-0.json +0 -1
package/test/psbt.spec.ts
CHANGED
|
@@ -1,1285 +1,1285 @@
|
|
|
1
|
-
import assert from 'assert';
|
|
2
|
-
import { BIP32Factory } from 'bip32';
|
|
3
|
-
import * as ecc from 'tiny-secp256k1';
|
|
4
|
-
import * as crypto from 'crypto';
|
|
5
|
-
import { ECPairFactory } from 'ecpair';
|
|
6
|
-
import { describe, it } from 'mocha';
|
|
7
|
-
|
|
8
|
-
import { convertScriptTree } from './payments.utils.js';
|
|
9
|
-
import { LEAF_VERSION_TAPSCRIPT } from '../src/payments/bip341.js';
|
|
10
|
-
import { tapTreeFromList, tapTreeToList } from '../src/psbt/bip371.js';
|
|
11
|
-
import { Taptree } from '../src/types.js';
|
|
12
|
-
import {
|
|
13
|
-
initEccLib,
|
|
14
|
-
networks as NETWORKS,
|
|
15
|
-
payments,
|
|
16
|
-
Psbt,
|
|
17
|
-
Signer,
|
|
18
|
-
SignerAsync,
|
|
19
|
-
} from '../src/index.js';
|
|
20
|
-
|
|
21
|
-
import preFixtures from './fixtures/psbt.json' with { type: 'json' };
|
|
22
|
-
import taprootFixtures from './fixtures/p2tr.json' with { type: 'json' };
|
|
23
|
-
|
|
24
|
-
const bip32 = BIP32Factory(ecc);
|
|
25
|
-
const ECPair = ECPairFactory(ecc);
|
|
26
|
-
|
|
27
|
-
const validator = (pubkey: Buffer, msghash: Buffer, signature: Buffer): boolean =>
|
|
28
|
-
ECPair.fromPublicKey(pubkey).verify(msghash, signature);
|
|
29
|
-
|
|
30
|
-
const schnorrValidator = (pubkey: Buffer, msghash: Buffer, signature: Buffer): boolean =>
|
|
31
|
-
ecc.verifySchnorr(msghash, pubkey, signature);
|
|
32
|
-
|
|
33
|
-
const initBuffers = (object: any): typeof preFixtures =>
|
|
34
|
-
JSON.parse(JSON.stringify(object), (_, value) => {
|
|
35
|
-
const regex = new RegExp(/^Buffer.from\(['"](.*)['"], ['"](.*)['"]\)$/);
|
|
36
|
-
const result = regex.exec(value);
|
|
37
|
-
if (!result) return value;
|
|
38
|
-
|
|
39
|
-
const data = result[1];
|
|
40
|
-
const encoding = result[2];
|
|
41
|
-
|
|
42
|
-
return Buffer.from(data, encoding as BufferEncoding);
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
const fixtures = initBuffers(preFixtures);
|
|
46
|
-
|
|
47
|
-
const upperCaseFirstLetter = (str: string): string => str.replace(/^./, (s) => s.toUpperCase());
|
|
48
|
-
|
|
49
|
-
const toAsyncSigner = (signer: Signer): SignerAsync => {
|
|
50
|
-
return {
|
|
51
|
-
publicKey: signer.publicKey,
|
|
52
|
-
sign: (hash: Buffer, lowerR: boolean | undefined): Promise<Buffer> => {
|
|
53
|
-
return new Promise((resolve, rejects): void => {
|
|
54
|
-
setTimeout(() => {
|
|
55
|
-
try {
|
|
56
|
-
const r = signer.sign(hash, lowerR);
|
|
57
|
-
resolve(r);
|
|
58
|
-
} catch (e) {
|
|
59
|
-
rejects(e as Error);
|
|
60
|
-
}
|
|
61
|
-
}, 10);
|
|
62
|
-
});
|
|
63
|
-
},
|
|
64
|
-
};
|
|
65
|
-
};
|
|
66
|
-
const failedAsyncSigner = (publicKey: Buffer): SignerAsync => {
|
|
67
|
-
return <SignerAsync>{
|
|
68
|
-
publicKey,
|
|
69
|
-
sign: (__: Buffer): Promise<Buffer> => {
|
|
70
|
-
return new Promise((_, reject): void => {
|
|
71
|
-
setTimeout(() => {
|
|
72
|
-
reject(new Error('sign failed'));
|
|
73
|
-
}, 10);
|
|
74
|
-
});
|
|
75
|
-
},
|
|
76
|
-
};
|
|
77
|
-
};
|
|
78
|
-
// const b = (hex: string) => Buffer.from(hex, 'hex');
|
|
79
|
-
|
|
80
|
-
describe(`Psbt`, () => {
|
|
81
|
-
beforeEach(() => {
|
|
82
|
-
// provide the ECC lib only when required
|
|
83
|
-
initEccLib(undefined);
|
|
84
|
-
});
|
|
85
|
-
describe('BIP174 Test Vectors', () => {
|
|
86
|
-
fixtures.bip174.invalid.forEach((f) => {
|
|
87
|
-
it(`Invalid: ${f.description}`, () => {
|
|
88
|
-
assert.throws(() => {
|
|
89
|
-
Psbt.fromBase64(f.psbt);
|
|
90
|
-
}, new RegExp(f.errorMessage));
|
|
91
|
-
});
|
|
92
|
-
});
|
|
93
|
-
|
|
94
|
-
fixtures.bip174.valid.forEach((f) => {
|
|
95
|
-
it(`Valid: ${f.description}`, () => {
|
|
96
|
-
assert.doesNotThrow(() => {
|
|
97
|
-
Psbt.fromBase64(f.psbt);
|
|
98
|
-
});
|
|
99
|
-
});
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
fixtures.bip174.failSignChecks.forEach((f) => {
|
|
103
|
-
const keyPair = ECPair.makeRandom();
|
|
104
|
-
it(`Fails Signer checks: ${f.description}`, () => {
|
|
105
|
-
const psbt = Psbt.fromBase64(f.psbt);
|
|
106
|
-
assert.throws(() => {
|
|
107
|
-
psbt.signInput(f.inputToCheck, keyPair);
|
|
108
|
-
}, new RegExp(f.errorMessage));
|
|
109
|
-
});
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
fixtures.bip174.creator.forEach((f) => {
|
|
113
|
-
it('Creates expected PSBT', () => {
|
|
114
|
-
const psbt = new Psbt();
|
|
115
|
-
for (const input of f.inputs) {
|
|
116
|
-
psbt.addInput(input);
|
|
117
|
-
}
|
|
118
|
-
for (const output of f.outputs) {
|
|
119
|
-
const script = Buffer.from(output.script, 'hex');
|
|
120
|
-
psbt.addOutput({ ...output, script });
|
|
121
|
-
}
|
|
122
|
-
assert.strictEqual(psbt.toBase64(), f.result);
|
|
123
|
-
});
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
fixtures.bip174.updater.forEach((f) => {
|
|
127
|
-
it('Updates PSBT to the expected result', () => {
|
|
128
|
-
if (f.isTaproot) initEccLib(ecc);
|
|
129
|
-
const psbt = Psbt.fromBase64(f.psbt);
|
|
130
|
-
|
|
131
|
-
for (const inputOrOutput of ['input', 'output']) {
|
|
132
|
-
const fixtureData = (f as any)[`${inputOrOutput}Data`];
|
|
133
|
-
if (fixtureData) {
|
|
134
|
-
for (const [i, data] of fixtureData.entries()) {
|
|
135
|
-
const txt = upperCaseFirstLetter(inputOrOutput);
|
|
136
|
-
(psbt as any)[`update${txt}`](i, data);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
assert.strictEqual(psbt.toBase64(), f.result);
|
|
142
|
-
});
|
|
143
|
-
});
|
|
144
|
-
|
|
145
|
-
fixtures.bip174.signer.forEach((f) => {
|
|
146
|
-
it('Signs PSBT to the expected result', () => {
|
|
147
|
-
if (f.isTaproot) initEccLib(ecc);
|
|
148
|
-
const psbt = Psbt.fromBase64(f.psbt);
|
|
149
|
-
|
|
150
|
-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
151
|
-
// @ts-ignore // cannot find tapLeafHashToSign
|
|
152
|
-
f.keys.forEach(({ inputToSign, tapLeafHashToSign, WIF }) => {
|
|
153
|
-
const keyPair = ECPair.fromWIF(WIF, NETWORKS.testnet);
|
|
154
|
-
if (tapLeafHashToSign)
|
|
155
|
-
psbt.signTaprootInput(
|
|
156
|
-
inputToSign,
|
|
157
|
-
keyPair,
|
|
158
|
-
Buffer.from(tapLeafHashToSign, 'hex'),
|
|
159
|
-
);
|
|
160
|
-
else psbt.signInput(inputToSign, keyPair);
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
assert.strictEqual(psbt.toBase64(), f.result);
|
|
164
|
-
});
|
|
165
|
-
});
|
|
166
|
-
|
|
167
|
-
fixtures.bip174.combiner.forEach((f) => {
|
|
168
|
-
it('Combines two PSBTs to the expected result', () => {
|
|
169
|
-
const psbts = f.psbts.map((psbt) => Psbt.fromBase64(psbt));
|
|
170
|
-
|
|
171
|
-
psbts[0].combine(psbts[1]);
|
|
172
|
-
|
|
173
|
-
// Produces a different Base64 string due to implemetation specific key-value ordering.
|
|
174
|
-
// That means this test will fail:
|
|
175
|
-
// assert.strictEqual(psbts[0].toBase64(), f.result)
|
|
176
|
-
// However, if we compare the actual PSBT properties we can see they are logically identical:
|
|
177
|
-
assert.deepStrictEqual(psbts[0], Psbt.fromBase64(f.result));
|
|
178
|
-
});
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
fixtures.bip174.finalizer.forEach((f) => {
|
|
182
|
-
it('Finalizes inputs and gives the expected PSBT', () => {
|
|
183
|
-
if (f.isTaproot) initEccLib(ecc);
|
|
184
|
-
const psbt = Psbt.fromBase64(f.psbt);
|
|
185
|
-
|
|
186
|
-
psbt.finalizeAllInputs();
|
|
187
|
-
|
|
188
|
-
assert.strictEqual(psbt.toBase64(), f.result);
|
|
189
|
-
});
|
|
190
|
-
});
|
|
191
|
-
|
|
192
|
-
fixtures.bip174.extractor.forEach((f) => {
|
|
193
|
-
it('Extracts the expected transaction from a PSBT', () => {
|
|
194
|
-
const psbt1 = Psbt.fromBase64(f.psbt);
|
|
195
|
-
const transaction1 = psbt1.extractTransaction(true).toHex();
|
|
196
|
-
|
|
197
|
-
const psbt2 = Psbt.fromBase64(f.psbt);
|
|
198
|
-
const transaction2 = psbt2.extractTransaction().toHex();
|
|
199
|
-
|
|
200
|
-
assert.strictEqual(transaction1, transaction2);
|
|
201
|
-
assert.strictEqual(transaction1, f.transaction);
|
|
202
|
-
|
|
203
|
-
const psbt3 = Psbt.fromBase64(f.psbt);
|
|
204
|
-
delete psbt3.data.inputs[0].finalScriptSig;
|
|
205
|
-
delete psbt3.data.inputs[0].finalScriptWitness;
|
|
206
|
-
assert.throws(() => {
|
|
207
|
-
psbt3.extractTransaction();
|
|
208
|
-
}, new RegExp('Not finalized'));
|
|
209
|
-
|
|
210
|
-
const psbt4 = Psbt.fromBase64(f.psbt);
|
|
211
|
-
psbt4.setMaximumFeeRate(1);
|
|
212
|
-
assert.throws(() => {
|
|
213
|
-
psbt4.extractTransaction();
|
|
214
|
-
}, new RegExp('Warning: You are paying around [\\d.]+ in fees'));
|
|
215
|
-
|
|
216
|
-
const psbt5 = Psbt.fromBase64(f.psbt);
|
|
217
|
-
psbt5.extractTransaction(true);
|
|
218
|
-
const fr1 = psbt5.getFeeRate();
|
|
219
|
-
const fr2 = psbt5.getFeeRate();
|
|
220
|
-
assert.strictEqual(fr1, fr2);
|
|
221
|
-
|
|
222
|
-
const psbt6 = Psbt.fromBase64(f.psbt);
|
|
223
|
-
const f1 = psbt6.getFee();
|
|
224
|
-
const f2 = psbt6.getFee();
|
|
225
|
-
assert.strictEqual(f1, f2);
|
|
226
|
-
});
|
|
227
|
-
});
|
|
228
|
-
});
|
|
229
|
-
|
|
230
|
-
describe('signInputAsync', () => {
|
|
231
|
-
fixtures.signInput.checks.forEach((f) => {
|
|
232
|
-
it(f.description, async () => {
|
|
233
|
-
if (f.isTaproot) initEccLib(ecc);
|
|
234
|
-
if (f.shouldSign) {
|
|
235
|
-
const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt);
|
|
236
|
-
await assert.doesNotReject(async () => {
|
|
237
|
-
await psbtThatShouldsign.signInputAsync(
|
|
238
|
-
f.shouldSign.inputToCheck,
|
|
239
|
-
ECPair.fromWIF(f.shouldSign.WIF),
|
|
240
|
-
f.shouldSign.sighashTypes || undefined,
|
|
241
|
-
);
|
|
242
|
-
if (f.shouldSign.result) {
|
|
243
|
-
assert.strictEqual(psbtThatShouldsign.toBase64(), f.shouldSign.result);
|
|
244
|
-
}
|
|
245
|
-
});
|
|
246
|
-
const failMessage = f.isTaproot
|
|
247
|
-
? /Need Schnorr Signer to sign taproot input #0./
|
|
248
|
-
: /sign failed/;
|
|
249
|
-
await assert.rejects(async () => {
|
|
250
|
-
await psbtThatShouldsign.signInputAsync(
|
|
251
|
-
f.shouldSign.inputToCheck,
|
|
252
|
-
failedAsyncSigner(ECPair.fromWIF(f.shouldSign.WIF).publicKey),
|
|
253
|
-
f.shouldSign.sighashTypes || undefined,
|
|
254
|
-
);
|
|
255
|
-
}, failMessage);
|
|
256
|
-
}
|
|
257
|
-
|
|
258
|
-
if (f.shouldThrow) {
|
|
259
|
-
const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt);
|
|
260
|
-
await assert.rejects(async () => {
|
|
261
|
-
await psbtThatShouldThrow.signInputAsync(
|
|
262
|
-
f.shouldThrow.inputToCheck,
|
|
263
|
-
ECPair.fromWIF(f.shouldThrow.WIF),
|
|
264
|
-
(f.shouldThrow as any).sighashTypes || undefined,
|
|
265
|
-
);
|
|
266
|
-
}, new RegExp(f.shouldThrow.errorMessage));
|
|
267
|
-
await assert.rejects(async () => {
|
|
268
|
-
await psbtThatShouldThrow.signInputAsync(
|
|
269
|
-
f.shouldThrow.inputToCheck,
|
|
270
|
-
toAsyncSigner(ECPair.fromWIF(f.shouldThrow.WIF)),
|
|
271
|
-
(f.shouldThrow as any).sighashTypes || undefined,
|
|
272
|
-
);
|
|
273
|
-
}, new RegExp(f.shouldThrow.errorMessage));
|
|
274
|
-
await assert.rejects(async () => {
|
|
275
|
-
await (psbtThatShouldThrow.signInputAsync as any)(
|
|
276
|
-
f.shouldThrow.inputToCheck,
|
|
277
|
-
);
|
|
278
|
-
}, new RegExp('Need Signer to sign input'));
|
|
279
|
-
}
|
|
280
|
-
});
|
|
281
|
-
});
|
|
282
|
-
});
|
|
283
|
-
|
|
284
|
-
describe('signInput', () => {
|
|
285
|
-
fixtures.signInput.checks.forEach((f) => {
|
|
286
|
-
it(f.description, () => {
|
|
287
|
-
if (f.isTaproot) initEccLib(ecc);
|
|
288
|
-
if (f.shouldSign) {
|
|
289
|
-
const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt);
|
|
290
|
-
assert.doesNotThrow(() => {
|
|
291
|
-
psbtThatShouldsign.signInput(
|
|
292
|
-
f.shouldSign.inputToCheck,
|
|
293
|
-
ECPair.fromWIF(f.shouldSign.WIF),
|
|
294
|
-
f.shouldSign.sighashTypes || undefined,
|
|
295
|
-
);
|
|
296
|
-
});
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
if (f.shouldThrow) {
|
|
300
|
-
const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt);
|
|
301
|
-
assert.throws(() => {
|
|
302
|
-
psbtThatShouldThrow.signInput(
|
|
303
|
-
f.shouldThrow.inputToCheck,
|
|
304
|
-
ECPair.fromWIF(f.shouldThrow.WIF),
|
|
305
|
-
(f.shouldThrow as any).sighashTypes || undefined,
|
|
306
|
-
);
|
|
307
|
-
}, new RegExp(f.shouldThrow.errorMessage));
|
|
308
|
-
assert.throws(() => {
|
|
309
|
-
(psbtThatShouldThrow.signInput as any)(f.shouldThrow.inputToCheck);
|
|
310
|
-
}, new RegExp('Need Signer to sign input'));
|
|
311
|
-
}
|
|
312
|
-
});
|
|
313
|
-
});
|
|
314
|
-
});
|
|
315
|
-
|
|
316
|
-
describe('signAllInputsAsync', () => {
|
|
317
|
-
fixtures.signInput.checks.forEach((f) => {
|
|
318
|
-
if (f.description === 'checks the input exists') return;
|
|
319
|
-
it(f.description, async () => {
|
|
320
|
-
if (f.isTaproot) initEccLib(ecc);
|
|
321
|
-
if (f.shouldSign) {
|
|
322
|
-
const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt);
|
|
323
|
-
await assert.doesNotReject(async () => {
|
|
324
|
-
await psbtThatShouldsign.signAllInputsAsync(
|
|
325
|
-
ECPair.fromWIF(f.shouldSign.WIF),
|
|
326
|
-
f.shouldSign.sighashTypes || undefined,
|
|
327
|
-
);
|
|
328
|
-
});
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
if (f.shouldThrow) {
|
|
332
|
-
const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt);
|
|
333
|
-
await assert.rejects(async () => {
|
|
334
|
-
await psbtThatShouldThrow.signAllInputsAsync(
|
|
335
|
-
ECPair.fromWIF(f.shouldThrow.WIF),
|
|
336
|
-
(f.shouldThrow as any).sighashTypes || undefined,
|
|
337
|
-
);
|
|
338
|
-
}, new RegExp('No inputs were signed'));
|
|
339
|
-
await assert.rejects(async () => {
|
|
340
|
-
await (psbtThatShouldThrow.signAllInputsAsync as any)();
|
|
341
|
-
}, new RegExp('Need Signer to sign input'));
|
|
342
|
-
}
|
|
343
|
-
});
|
|
344
|
-
});
|
|
345
|
-
});
|
|
346
|
-
|
|
347
|
-
describe('signAllInputs', () => {
|
|
348
|
-
fixtures.signInput.checks.forEach((f) => {
|
|
349
|
-
if (f.description === 'checks the input exists') return;
|
|
350
|
-
it(f.description, () => {
|
|
351
|
-
if (f.isTaproot) initEccLib(ecc);
|
|
352
|
-
if (f.shouldSign) {
|
|
353
|
-
const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt);
|
|
354
|
-
assert.doesNotThrow(() => {
|
|
355
|
-
psbtThatShouldsign.signAllInputs(
|
|
356
|
-
ECPair.fromWIF(f.shouldSign.WIF),
|
|
357
|
-
f.shouldSign.sighashTypes || undefined,
|
|
358
|
-
);
|
|
359
|
-
});
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
if (f.shouldThrow) {
|
|
363
|
-
const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt);
|
|
364
|
-
assert.throws(() => {
|
|
365
|
-
psbtThatShouldThrow.signAllInputs(
|
|
366
|
-
ECPair.fromWIF(f.shouldThrow.WIF),
|
|
367
|
-
(f.shouldThrow as any).sighashTypes || undefined,
|
|
368
|
-
);
|
|
369
|
-
}, new RegExp('No inputs were signed'));
|
|
370
|
-
assert.throws(() => {
|
|
371
|
-
(psbtThatShouldThrow.signAllInputs as any)();
|
|
372
|
-
}, new RegExp('Need Signer to sign input'));
|
|
373
|
-
}
|
|
374
|
-
});
|
|
375
|
-
});
|
|
376
|
-
});
|
|
377
|
-
|
|
378
|
-
describe('signInputHDAsync', () => {
|
|
379
|
-
fixtures.signInputHD.checks.forEach((f) => {
|
|
380
|
-
it(f.description, async () => {
|
|
381
|
-
if (f.shouldSign) {
|
|
382
|
-
const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt);
|
|
383
|
-
await assert.doesNotReject(async () => {
|
|
384
|
-
await psbtThatShouldsign.signInputHDAsync(
|
|
385
|
-
f.shouldSign.inputToCheck,
|
|
386
|
-
bip32.fromBase58(f.shouldSign.xprv),
|
|
387
|
-
(f.shouldSign as any).sighashTypes || undefined,
|
|
388
|
-
);
|
|
389
|
-
});
|
|
390
|
-
}
|
|
391
|
-
|
|
392
|
-
if (f.shouldThrow) {
|
|
393
|
-
const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt);
|
|
394
|
-
await assert.rejects(async () => {
|
|
395
|
-
await psbtThatShouldThrow.signInputHDAsync(
|
|
396
|
-
f.shouldThrow.inputToCheck,
|
|
397
|
-
bip32.fromBase58(f.shouldThrow.xprv),
|
|
398
|
-
(f.shouldThrow as any).sighashTypes || undefined,
|
|
399
|
-
);
|
|
400
|
-
}, new RegExp(f.shouldThrow.errorMessage));
|
|
401
|
-
await assert.rejects(async () => {
|
|
402
|
-
await (psbtThatShouldThrow.signInputHDAsync as any)(
|
|
403
|
-
f.shouldThrow.inputToCheck,
|
|
404
|
-
);
|
|
405
|
-
}, new RegExp('Need HDSigner to sign input'));
|
|
406
|
-
}
|
|
407
|
-
});
|
|
408
|
-
});
|
|
409
|
-
});
|
|
410
|
-
|
|
411
|
-
describe('signInputHD', () => {
|
|
412
|
-
fixtures.signInputHD.checks.forEach((f) => {
|
|
413
|
-
it(f.description, () => {
|
|
414
|
-
if (f.shouldSign) {
|
|
415
|
-
const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt);
|
|
416
|
-
assert.doesNotThrow(() => {
|
|
417
|
-
psbtThatShouldsign.signInputHD(
|
|
418
|
-
f.shouldSign.inputToCheck,
|
|
419
|
-
bip32.fromBase58(f.shouldSign.xprv),
|
|
420
|
-
(f.shouldSign as any).sighashTypes || undefined,
|
|
421
|
-
);
|
|
422
|
-
});
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
if (f.shouldThrow) {
|
|
426
|
-
const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt);
|
|
427
|
-
assert.throws(() => {
|
|
428
|
-
psbtThatShouldThrow.signInputHD(
|
|
429
|
-
f.shouldThrow.inputToCheck,
|
|
430
|
-
bip32.fromBase58(f.shouldThrow.xprv),
|
|
431
|
-
(f.shouldThrow as any).sighashTypes || undefined,
|
|
432
|
-
);
|
|
433
|
-
}, new RegExp(f.shouldThrow.errorMessage));
|
|
434
|
-
assert.throws(() => {
|
|
435
|
-
(psbtThatShouldThrow.signInputHD as any)(f.shouldThrow.inputToCheck);
|
|
436
|
-
}, new RegExp('Need HDSigner to sign input'));
|
|
437
|
-
}
|
|
438
|
-
});
|
|
439
|
-
});
|
|
440
|
-
});
|
|
441
|
-
|
|
442
|
-
describe('signAllInputsHDAsync', () => {
|
|
443
|
-
fixtures.signInputHD.checks.forEach((f) => {
|
|
444
|
-
it(f.description, async () => {
|
|
445
|
-
if (f.shouldSign) {
|
|
446
|
-
const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt);
|
|
447
|
-
await assert.doesNotReject(async () => {
|
|
448
|
-
await psbtThatShouldsign.signAllInputsHDAsync(
|
|
449
|
-
bip32.fromBase58(f.shouldSign.xprv),
|
|
450
|
-
(f.shouldSign as any).sighashTypes || undefined,
|
|
451
|
-
);
|
|
452
|
-
});
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
if (f.shouldThrow) {
|
|
456
|
-
const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt);
|
|
457
|
-
await assert.rejects(async () => {
|
|
458
|
-
await psbtThatShouldThrow.signAllInputsHDAsync(
|
|
459
|
-
bip32.fromBase58(f.shouldThrow.xprv),
|
|
460
|
-
(f.shouldThrow as any).sighashTypes || undefined,
|
|
461
|
-
);
|
|
462
|
-
}, new RegExp('No inputs were signed'));
|
|
463
|
-
await assert.rejects(async () => {
|
|
464
|
-
await (psbtThatShouldThrow.signAllInputsHDAsync as any)();
|
|
465
|
-
}, new RegExp('Need HDSigner to sign input'));
|
|
466
|
-
}
|
|
467
|
-
});
|
|
468
|
-
});
|
|
469
|
-
});
|
|
470
|
-
|
|
471
|
-
describe('signAllInputsHD', () => {
|
|
472
|
-
fixtures.signInputHD.checks.forEach((f) => {
|
|
473
|
-
it(f.description, () => {
|
|
474
|
-
if (f.shouldSign) {
|
|
475
|
-
const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt);
|
|
476
|
-
assert.doesNotThrow(() => {
|
|
477
|
-
psbtThatShouldsign.signAllInputsHD(
|
|
478
|
-
bip32.fromBase58(f.shouldSign.xprv),
|
|
479
|
-
(f.shouldSign as any).sighashTypes || undefined,
|
|
480
|
-
);
|
|
481
|
-
});
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
if (f.shouldThrow) {
|
|
485
|
-
const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt);
|
|
486
|
-
assert.throws(() => {
|
|
487
|
-
psbtThatShouldThrow.signAllInputsHD(
|
|
488
|
-
bip32.fromBase58(f.shouldThrow.xprv),
|
|
489
|
-
(f.shouldThrow as any).sighashTypes || undefined,
|
|
490
|
-
);
|
|
491
|
-
}, new RegExp('No inputs were signed'));
|
|
492
|
-
assert.throws(() => {
|
|
493
|
-
(psbtThatShouldThrow.signAllInputsHD as any)();
|
|
494
|
-
}, new RegExp('Need HDSigner to sign input'));
|
|
495
|
-
}
|
|
496
|
-
});
|
|
497
|
-
});
|
|
498
|
-
});
|
|
499
|
-
|
|
500
|
-
describe('finalizeInput', () => {
|
|
501
|
-
it(`Finalizes tapleaf by hash`, () => {
|
|
502
|
-
const f = fixtures.finalizeInput.finalizeTapleafByHash;
|
|
503
|
-
const psbt = Psbt.fromBase64(f.psbt);
|
|
504
|
-
|
|
505
|
-
psbt.finalizeTaprootInput(f.index, Buffer.from(f.leafHash, 'hex'));
|
|
506
|
-
|
|
507
|
-
assert.strictEqual(psbt.toBase64(), f.result);
|
|
508
|
-
});
|
|
509
|
-
|
|
510
|
-
it(`fails if tapleaf hash not found`, () => {
|
|
511
|
-
const f = fixtures.finalizeInput.finalizeTapleafByHash;
|
|
512
|
-
const psbt = Psbt.fromBase64(f.psbt);
|
|
513
|
-
|
|
514
|
-
assert.throws(() => {
|
|
515
|
-
psbt.finalizeTaprootInput(f.index, Buffer.from(f.leafHash, 'hex').reverse());
|
|
516
|
-
}, new RegExp('Can not finalize taproot input #0. Signature for tapleaf script not found.'));
|
|
517
|
-
});
|
|
518
|
-
|
|
519
|
-
it(`fails if trying to finalzie non-taproot input`, () => {
|
|
520
|
-
const psbt = new Psbt();
|
|
521
|
-
psbt.addInput({
|
|
522
|
-
hash: '0000000000000000000000000000000000000000000000000000000000000000',
|
|
523
|
-
index: 0,
|
|
524
|
-
});
|
|
525
|
-
|
|
526
|
-
assert.throws(() => {
|
|
527
|
-
psbt.finalizeTaprootInput(0);
|
|
528
|
-
}, new RegExp('Cannot finalize input #0. Not Taproot.'));
|
|
529
|
-
});
|
|
530
|
-
});
|
|
531
|
-
|
|
532
|
-
describe('finalizeAllInputs', () => {
|
|
533
|
-
fixtures.finalizeAllInputs.forEach((f) => {
|
|
534
|
-
it(`Finalizes inputs of type "${f.type}"`, () => {
|
|
535
|
-
const psbt = Psbt.fromBase64(f.psbt);
|
|
536
|
-
|
|
537
|
-
psbt.finalizeAllInputs();
|
|
538
|
-
|
|
539
|
-
assert.strictEqual(psbt.toBase64(), f.result);
|
|
540
|
-
});
|
|
541
|
-
});
|
|
542
|
-
it('fails if no script found', () => {
|
|
543
|
-
const psbt = new Psbt();
|
|
544
|
-
psbt.addInput({
|
|
545
|
-
hash: '0000000000000000000000000000000000000000000000000000000000000000',
|
|
546
|
-
index: 0,
|
|
547
|
-
});
|
|
548
|
-
assert.throws(() => {
|
|
549
|
-
psbt.finalizeAllInputs();
|
|
550
|
-
}, new RegExp('No script found for input #0'));
|
|
551
|
-
psbt.updateInput(0, {
|
|
552
|
-
witnessUtxo: {
|
|
553
|
-
script: Buffer.from('0014d85c2b71d0060b09c9886aeb815e50991dda124d', 'hex'),
|
|
554
|
-
value: 2e5,
|
|
555
|
-
},
|
|
556
|
-
});
|
|
557
|
-
assert.throws(() => {
|
|
558
|
-
psbt.finalizeAllInputs();
|
|
559
|
-
}, new RegExp('Can not finalize input #0'));
|
|
560
|
-
});
|
|
561
|
-
});
|
|
562
|
-
|
|
563
|
-
describe('addInput', () => {
|
|
564
|
-
fixtures.addInput.checks.forEach((f) => {
|
|
565
|
-
it(f.description, () => {
|
|
566
|
-
const psbt = new Psbt();
|
|
567
|
-
|
|
568
|
-
if (f.exception) {
|
|
569
|
-
assert.throws(() => {
|
|
570
|
-
psbt.addInput(f.inputData as any);
|
|
571
|
-
}, new RegExp(f.exception));
|
|
572
|
-
assert.throws(() => {
|
|
573
|
-
psbt.addInputs([f.inputData as any]);
|
|
574
|
-
}, new RegExp(f.exception));
|
|
575
|
-
} else {
|
|
576
|
-
assert.doesNotThrow(() => {
|
|
577
|
-
psbt.addInputs([f.inputData as any]);
|
|
578
|
-
if (f.equals) {
|
|
579
|
-
assert.strictEqual(psbt.toBase64(), f.equals);
|
|
580
|
-
}
|
|
581
|
-
});
|
|
582
|
-
assert.throws(() => {
|
|
583
|
-
psbt.addInput(f.inputData as any);
|
|
584
|
-
}, new RegExp('Duplicate input detected.'));
|
|
585
|
-
}
|
|
586
|
-
});
|
|
587
|
-
});
|
|
588
|
-
});
|
|
589
|
-
|
|
590
|
-
describe('updateInput', () => {
|
|
591
|
-
fixtures.updateInput.checks.forEach((f) => {
|
|
592
|
-
it(f.description, () => {
|
|
593
|
-
const psbt = Psbt.fromBase64(f.psbt);
|
|
594
|
-
|
|
595
|
-
if (f.exception) {
|
|
596
|
-
assert.throws(() => {
|
|
597
|
-
psbt.updateInput(f.index, f.inputData as any);
|
|
598
|
-
}, new RegExp(f.exception));
|
|
599
|
-
}
|
|
600
|
-
});
|
|
601
|
-
});
|
|
602
|
-
});
|
|
603
|
-
|
|
604
|
-
describe('addOutput', () => {
|
|
605
|
-
fixtures.addOutput.checks.forEach((f) => {
|
|
606
|
-
it(f.description, () => {
|
|
607
|
-
if (f.isTaproot) initEccLib(ecc);
|
|
608
|
-
const psbt = f.psbt ? Psbt.fromBase64(f.psbt) : new Psbt();
|
|
609
|
-
|
|
610
|
-
if (f.exception) {
|
|
611
|
-
assert.throws(() => {
|
|
612
|
-
psbt.addOutput(f.outputData as any);
|
|
613
|
-
}, new RegExp(f.exception));
|
|
614
|
-
assert.throws(() => {
|
|
615
|
-
psbt.addOutputs([f.outputData as any]);
|
|
616
|
-
}, new RegExp(f.exception));
|
|
617
|
-
} else {
|
|
618
|
-
assert.doesNotThrow(() => {
|
|
619
|
-
psbt.addOutput(f.outputData as any);
|
|
620
|
-
});
|
|
621
|
-
if (f.result) {
|
|
622
|
-
assert.strictEqual(psbt.toBase64(), f.result);
|
|
623
|
-
}
|
|
624
|
-
assert.doesNotThrow(() => {
|
|
625
|
-
psbt.addOutputs([f.outputData as any]);
|
|
626
|
-
});
|
|
627
|
-
}
|
|
628
|
-
});
|
|
629
|
-
});
|
|
630
|
-
});
|
|
631
|
-
|
|
632
|
-
describe('setVersion', () => {
|
|
633
|
-
it('Sets the version value of the unsigned transaction', () => {
|
|
634
|
-
const psbt = new Psbt();
|
|
635
|
-
|
|
636
|
-
assert.strictEqual(psbt.extractTransaction().version, 2);
|
|
637
|
-
psbt.setVersion(1);
|
|
638
|
-
assert.strictEqual(psbt.extractTransaction().version, 1);
|
|
639
|
-
});
|
|
640
|
-
});
|
|
641
|
-
|
|
642
|
-
describe('setLocktime', () => {
|
|
643
|
-
it('Sets the nLockTime value of the unsigned transaction', () => {
|
|
644
|
-
const psbt = new Psbt();
|
|
645
|
-
|
|
646
|
-
assert.strictEqual(psbt.extractTransaction().locktime, 0);
|
|
647
|
-
psbt.setLocktime(1);
|
|
648
|
-
assert.strictEqual(psbt.extractTransaction().locktime, 1);
|
|
649
|
-
});
|
|
650
|
-
});
|
|
651
|
-
|
|
652
|
-
describe('setInputSequence', () => {
|
|
653
|
-
it('Sets the sequence number for a given input', () => {
|
|
654
|
-
const psbt = new Psbt();
|
|
655
|
-
psbt.addInput({
|
|
656
|
-
hash: '0000000000000000000000000000000000000000000000000000000000000000',
|
|
657
|
-
index: 0,
|
|
658
|
-
});
|
|
659
|
-
|
|
660
|
-
assert.strictEqual(psbt.inputCount, 1);
|
|
661
|
-
assert.strictEqual(psbt.txInputs[0].sequence, 0xffffffff);
|
|
662
|
-
psbt.setInputSequence(0, 0);
|
|
663
|
-
assert.strictEqual(psbt.txInputs[0].sequence, 0);
|
|
664
|
-
});
|
|
665
|
-
|
|
666
|
-
it('throws if input index is too high', () => {
|
|
667
|
-
const psbt = new Psbt();
|
|
668
|
-
psbt.addInput({
|
|
669
|
-
hash: '0000000000000000000000000000000000000000000000000000000000000000',
|
|
670
|
-
index: 0,
|
|
671
|
-
});
|
|
672
|
-
|
|
673
|
-
assert.throws(() => {
|
|
674
|
-
psbt.setInputSequence(1, 0);
|
|
675
|
-
}, new RegExp('Input index too high'));
|
|
676
|
-
});
|
|
677
|
-
});
|
|
678
|
-
|
|
679
|
-
describe('getInputType', () => {
|
|
680
|
-
const key = ECPair.makeRandom();
|
|
681
|
-
const { publicKey } = key;
|
|
682
|
-
const p2wpkhPub = (pubkey: Buffer): Buffer =>
|
|
683
|
-
payments.p2wpkh({
|
|
684
|
-
pubkey,
|
|
685
|
-
}).output!;
|
|
686
|
-
const p2pkhPub = (pubkey: Buffer): Buffer =>
|
|
687
|
-
payments.p2pkh({
|
|
688
|
-
pubkey,
|
|
689
|
-
}).output!;
|
|
690
|
-
const p2shOut = (output: Buffer): Buffer =>
|
|
691
|
-
payments.p2sh({
|
|
692
|
-
redeem: { output },
|
|
693
|
-
}).output!;
|
|
694
|
-
const p2wshOut = (output: Buffer): Buffer =>
|
|
695
|
-
payments.p2wsh({
|
|
696
|
-
redeem: { output },
|
|
697
|
-
}).output!;
|
|
698
|
-
const p2shp2wshOut = (output: Buffer): Buffer => p2shOut(p2wshOut(output));
|
|
699
|
-
const noOuter = (output: Buffer): Buffer => output;
|
|
700
|
-
|
|
701
|
-
function getInputTypeTest({
|
|
702
|
-
innerScript,
|
|
703
|
-
outerScript,
|
|
704
|
-
redeemGetter,
|
|
705
|
-
witnessGetter,
|
|
706
|
-
expectedType,
|
|
707
|
-
finalize,
|
|
708
|
-
}: any): void {
|
|
709
|
-
const psbt = new Psbt();
|
|
710
|
-
psbt.addInput({
|
|
711
|
-
hash: '0000000000000000000000000000000000000000000000000000000000000000',
|
|
712
|
-
index: 0,
|
|
713
|
-
witnessUtxo: {
|
|
714
|
-
script: outerScript(innerScript(publicKey)),
|
|
715
|
-
value: 2e3,
|
|
716
|
-
},
|
|
717
|
-
...(redeemGetter ? { redeemScript: redeemGetter(publicKey) } : {}),
|
|
718
|
-
...(witnessGetter ? { witnessScript: witnessGetter(publicKey) } : {}),
|
|
719
|
-
}).addOutput({
|
|
720
|
-
script: Buffer.from('0014d85c2b71d0060b09c9886aeb815e50991dda124d'),
|
|
721
|
-
value: 1800,
|
|
722
|
-
});
|
|
723
|
-
if (finalize) psbt.signInput(0, key).finalizeInput(0);
|
|
724
|
-
const type = psbt.getInputType(0);
|
|
725
|
-
assert.strictEqual(type, expectedType, 'incorrect input type');
|
|
726
|
-
}
|
|
727
|
-
|
|
728
|
-
[
|
|
729
|
-
{
|
|
730
|
-
innerScript: p2pkhPub,
|
|
731
|
-
outerScript: noOuter,
|
|
732
|
-
redeemGetter: null,
|
|
733
|
-
witnessGetter: null,
|
|
734
|
-
expectedType: 'pubkeyhash',
|
|
735
|
-
},
|
|
736
|
-
{
|
|
737
|
-
innerScript: p2wpkhPub,
|
|
738
|
-
outerScript: noOuter,
|
|
739
|
-
redeemGetter: null,
|
|
740
|
-
witnessGetter: null,
|
|
741
|
-
expectedType: 'witnesspubkeyhash',
|
|
742
|
-
},
|
|
743
|
-
{
|
|
744
|
-
innerScript: p2pkhPub,
|
|
745
|
-
outerScript: p2shOut,
|
|
746
|
-
redeemGetter: p2pkhPub,
|
|
747
|
-
witnessGetter: null,
|
|
748
|
-
expectedType: 'p2sh-pubkeyhash',
|
|
749
|
-
},
|
|
750
|
-
{
|
|
751
|
-
innerScript: p2wpkhPub,
|
|
752
|
-
outerScript: p2shOut,
|
|
753
|
-
redeemGetter: p2wpkhPub,
|
|
754
|
-
witnessGetter: null,
|
|
755
|
-
expectedType: 'p2sh-witnesspubkeyhash',
|
|
756
|
-
finalize: true,
|
|
757
|
-
},
|
|
758
|
-
{
|
|
759
|
-
innerScript: p2pkhPub,
|
|
760
|
-
outerScript: p2wshOut,
|
|
761
|
-
redeemGetter: null,
|
|
762
|
-
witnessGetter: p2pkhPub,
|
|
763
|
-
expectedType: 'p2wsh-pubkeyhash',
|
|
764
|
-
finalize: true,
|
|
765
|
-
},
|
|
766
|
-
{
|
|
767
|
-
innerScript: p2pkhPub,
|
|
768
|
-
outerScript: p2shp2wshOut,
|
|
769
|
-
redeemGetter: (pk: Buffer): Buffer => p2wshOut(p2pkhPub(pk)),
|
|
770
|
-
witnessGetter: p2pkhPub,
|
|
771
|
-
expectedType: 'p2sh-p2wsh-pubkeyhash',
|
|
772
|
-
},
|
|
773
|
-
].forEach(getInputTypeTest);
|
|
774
|
-
});
|
|
775
|
-
|
|
776
|
-
describe('inputHasHDKey', () => {
|
|
777
|
-
it('should return true if HD key is present', () => {
|
|
778
|
-
const root = bip32.fromSeed(crypto.randomBytes(32));
|
|
779
|
-
const root2 = bip32.fromSeed(crypto.randomBytes(32));
|
|
780
|
-
const path = "m/0'/0";
|
|
781
|
-
const psbt = new Psbt();
|
|
782
|
-
psbt.addInput({
|
|
783
|
-
hash: '0000000000000000000000000000000000000000000000000000000000000000',
|
|
784
|
-
index: 0,
|
|
785
|
-
bip32Derivation: [
|
|
786
|
-
{
|
|
787
|
-
masterFingerprint: root.fingerprint,
|
|
788
|
-
path,
|
|
789
|
-
pubkey: root.derivePath(path).publicKey,
|
|
790
|
-
},
|
|
791
|
-
],
|
|
792
|
-
});
|
|
793
|
-
assert.strictEqual(psbt.inputHasHDKey(0, root), true);
|
|
794
|
-
assert.strictEqual(psbt.inputHasHDKey(0, root2), false);
|
|
795
|
-
});
|
|
796
|
-
});
|
|
797
|
-
|
|
798
|
-
describe('inputHasPubkey', () => {
|
|
799
|
-
it('should throw', () => {
|
|
800
|
-
const psbt = new Psbt();
|
|
801
|
-
psbt.addInput({
|
|
802
|
-
hash: '0000000000000000000000000000000000000000000000000000000000000000',
|
|
803
|
-
index: 0,
|
|
804
|
-
});
|
|
805
|
-
|
|
806
|
-
assert.throws(() => {
|
|
807
|
-
psbt.inputHasPubkey(0, Buffer.from([]));
|
|
808
|
-
}, new RegExp("Can't find pubkey in input without Utxo data"));
|
|
809
|
-
|
|
810
|
-
psbt.updateInput(0, {
|
|
811
|
-
witnessUtxo: {
|
|
812
|
-
value: 1337,
|
|
813
|
-
script: payments.p2sh({
|
|
814
|
-
redeem: { output: Buffer.from([0x51]) },
|
|
815
|
-
}).output!,
|
|
816
|
-
},
|
|
817
|
-
});
|
|
818
|
-
|
|
819
|
-
assert.throws(() => {
|
|
820
|
-
psbt.inputHasPubkey(0, Buffer.from([]));
|
|
821
|
-
}, new RegExp('scriptPubkey is P2SH but redeemScript missing'));
|
|
822
|
-
|
|
823
|
-
delete psbt.data.inputs[0].witnessUtxo;
|
|
824
|
-
|
|
825
|
-
psbt.updateInput(0, {
|
|
826
|
-
witnessUtxo: {
|
|
827
|
-
value: 1337,
|
|
828
|
-
script: payments.p2wsh({
|
|
829
|
-
redeem: { output: Buffer.from([0x51]) },
|
|
830
|
-
}).output!,
|
|
831
|
-
},
|
|
832
|
-
});
|
|
833
|
-
|
|
834
|
-
assert.throws(() => {
|
|
835
|
-
psbt.inputHasPubkey(0, Buffer.from([]));
|
|
836
|
-
}, new RegExp('scriptPubkey or redeemScript is P2WSH but witnessScript missing'));
|
|
837
|
-
|
|
838
|
-
delete psbt.data.inputs[0].witnessUtxo;
|
|
839
|
-
|
|
840
|
-
psbt.updateInput(0, {
|
|
841
|
-
witnessUtxo: {
|
|
842
|
-
value: 1337,
|
|
843
|
-
script: payments.p2sh({
|
|
844
|
-
redeem: payments.p2wsh({
|
|
845
|
-
redeem: { output: Buffer.from([0x51]) },
|
|
846
|
-
}),
|
|
847
|
-
}).output!,
|
|
848
|
-
},
|
|
849
|
-
redeemScript: payments.p2wsh({
|
|
850
|
-
redeem: { output: Buffer.from([0x51]) },
|
|
851
|
-
}).output!,
|
|
852
|
-
});
|
|
853
|
-
|
|
854
|
-
assert.throws(() => {
|
|
855
|
-
psbt.inputHasPubkey(0, Buffer.from([]));
|
|
856
|
-
}, new RegExp('scriptPubkey or redeemScript is P2WSH but witnessScript missing'));
|
|
857
|
-
|
|
858
|
-
psbt.updateInput(0, {
|
|
859
|
-
witnessScript: Buffer.from([0x51]),
|
|
860
|
-
});
|
|
861
|
-
|
|
862
|
-
assert.doesNotThrow(() => {
|
|
863
|
-
psbt.inputHasPubkey(0, Buffer.from([0x51]));
|
|
864
|
-
});
|
|
865
|
-
});
|
|
866
|
-
});
|
|
867
|
-
|
|
868
|
-
describe('outputHasHDKey', () => {
|
|
869
|
-
it('should return true if HD key is present', () => {
|
|
870
|
-
const root = bip32.fromSeed(crypto.randomBytes(32));
|
|
871
|
-
const root2 = bip32.fromSeed(crypto.randomBytes(32));
|
|
872
|
-
const path = "m/0'/0";
|
|
873
|
-
const psbt = new Psbt();
|
|
874
|
-
psbt.addInput({
|
|
875
|
-
hash: '0000000000000000000000000000000000000000000000000000000000000000',
|
|
876
|
-
index: 0,
|
|
877
|
-
}).addOutput({
|
|
878
|
-
script: Buffer.from('0014000102030405060708090a0b0c0d0e0f00010203', 'hex'),
|
|
879
|
-
value: 2000,
|
|
880
|
-
bip32Derivation: [
|
|
881
|
-
{
|
|
882
|
-
masterFingerprint: root.fingerprint,
|
|
883
|
-
path,
|
|
884
|
-
pubkey: root.derivePath(path).publicKey,
|
|
885
|
-
},
|
|
886
|
-
],
|
|
887
|
-
});
|
|
888
|
-
assert.strictEqual(psbt.outputHasHDKey(0, root), true);
|
|
889
|
-
assert.strictEqual(psbt.outputHasHDKey(0, root2), false);
|
|
890
|
-
});
|
|
891
|
-
});
|
|
892
|
-
|
|
893
|
-
describe('outputHasPubkey', () => {
|
|
894
|
-
it('should throw', () => {
|
|
895
|
-
const psbt = new Psbt();
|
|
896
|
-
psbt.addInput({
|
|
897
|
-
hash: '0000000000000000000000000000000000000000000000000000000000000000',
|
|
898
|
-
index: 0,
|
|
899
|
-
}).addOutput({
|
|
900
|
-
script: payments.p2sh({
|
|
901
|
-
redeem: { output: Buffer.from([0x51]) },
|
|
902
|
-
}).output!,
|
|
903
|
-
value: 1337,
|
|
904
|
-
});
|
|
905
|
-
|
|
906
|
-
assert.throws(() => {
|
|
907
|
-
psbt.outputHasPubkey(0, Buffer.from([]));
|
|
908
|
-
}, new RegExp('scriptPubkey is P2SH but redeemScript missing'));
|
|
909
|
-
|
|
910
|
-
(psbt as any).__CACHE.__TX.outs[0].script = payments.p2wsh({
|
|
911
|
-
redeem: { output: Buffer.from([0x51]) },
|
|
912
|
-
}).output!;
|
|
913
|
-
|
|
914
|
-
assert.throws(() => {
|
|
915
|
-
psbt.outputHasPubkey(0, Buffer.from([]));
|
|
916
|
-
}, new RegExp('scriptPubkey or redeemScript is P2WSH but witnessScript missing'));
|
|
917
|
-
|
|
918
|
-
(psbt as any).__CACHE.__TX.outs[0].script = payments.p2sh({
|
|
919
|
-
redeem: payments.p2wsh({
|
|
920
|
-
redeem: { output: Buffer.from([0x51]) },
|
|
921
|
-
}),
|
|
922
|
-
}).output!;
|
|
923
|
-
|
|
924
|
-
psbt.updateOutput(0, {
|
|
925
|
-
redeemScript: payments.p2wsh({
|
|
926
|
-
redeem: { output: Buffer.from([0x51]) },
|
|
927
|
-
}).output!,
|
|
928
|
-
});
|
|
929
|
-
|
|
930
|
-
assert.throws(() => {
|
|
931
|
-
psbt.outputHasPubkey(0, Buffer.from([]));
|
|
932
|
-
}, new RegExp('scriptPubkey or redeemScript is P2WSH but witnessScript missing'));
|
|
933
|
-
|
|
934
|
-
delete psbt.data.outputs[0].redeemScript;
|
|
935
|
-
|
|
936
|
-
psbt.updateOutput(0, {
|
|
937
|
-
witnessScript: Buffer.from([0x51]),
|
|
938
|
-
});
|
|
939
|
-
|
|
940
|
-
assert.throws(() => {
|
|
941
|
-
psbt.outputHasPubkey(0, Buffer.from([]));
|
|
942
|
-
}, new RegExp('scriptPubkey is P2SH but redeemScript missing'));
|
|
943
|
-
|
|
944
|
-
psbt.updateOutput(0, {
|
|
945
|
-
redeemScript: payments.p2wsh({
|
|
946
|
-
redeem: { output: Buffer.from([0x51]) },
|
|
947
|
-
}).output!,
|
|
948
|
-
});
|
|
949
|
-
|
|
950
|
-
assert.doesNotThrow(() => {
|
|
951
|
-
psbt.outputHasPubkey(0, Buffer.from([0x51]));
|
|
952
|
-
});
|
|
953
|
-
});
|
|
954
|
-
});
|
|
955
|
-
|
|
956
|
-
describe('clone', () => {
|
|
957
|
-
it('Should clone a psbt exactly with no reference', () => {
|
|
958
|
-
const f = fixtures.clone;
|
|
959
|
-
const psbt = Psbt.fromBase64(f.psbt);
|
|
960
|
-
const notAClone = Object.assign(new Psbt(), psbt); // references still active
|
|
961
|
-
const clone = psbt.clone();
|
|
962
|
-
|
|
963
|
-
assert.strictEqual(psbt.validateSignaturesOfAllInputs(validator), true);
|
|
964
|
-
|
|
965
|
-
assert.strictEqual(clone.toBase64(), psbt.toBase64());
|
|
966
|
-
assert.strictEqual(clone.toBase64(), notAClone.toBase64());
|
|
967
|
-
assert.strictEqual(psbt.toBase64(), notAClone.toBase64());
|
|
968
|
-
(psbt as any).__CACHE.__TX.version |= 0xff0000;
|
|
969
|
-
assert.notStrictEqual(clone.toBase64(), psbt.toBase64());
|
|
970
|
-
assert.notStrictEqual(clone.toBase64(), notAClone.toBase64());
|
|
971
|
-
assert.strictEqual(psbt.toBase64(), notAClone.toBase64());
|
|
972
|
-
});
|
|
973
|
-
});
|
|
974
|
-
|
|
975
|
-
describe('setMaximumFeeRate', () => {
|
|
976
|
-
it('Sets the maximumFeeRate value', () => {
|
|
977
|
-
const psbt = new Psbt();
|
|
978
|
-
|
|
979
|
-
assert.strictEqual((psbt as any).opts.maximumFeeRate, 5000);
|
|
980
|
-
psbt.setMaximumFeeRate(6000);
|
|
981
|
-
assert.strictEqual((psbt as any).opts.maximumFeeRate, 6000);
|
|
982
|
-
});
|
|
983
|
-
});
|
|
984
|
-
|
|
985
|
-
describe('validateSignaturesOfInput', () => {
|
|
986
|
-
const f = fixtures.validateSignaturesOfInput;
|
|
987
|
-
it('Correctly validates a signature', () => {
|
|
988
|
-
const psbt = Psbt.fromBase64(f.psbt);
|
|
989
|
-
|
|
990
|
-
assert.strictEqual(psbt.validateSignaturesOfInput(f.index, validator), true);
|
|
991
|
-
assert.throws(() => {
|
|
992
|
-
psbt.validateSignaturesOfInput(f.nonExistantIndex, validator);
|
|
993
|
-
}, new RegExp('No signatures to validate'));
|
|
994
|
-
});
|
|
995
|
-
|
|
996
|
-
it('Correctly validates a signature against a pubkey', () => {
|
|
997
|
-
const psbt = Psbt.fromBase64(f.psbt);
|
|
998
|
-
assert.strictEqual(
|
|
999
|
-
psbt.validateSignaturesOfInput(f.index, validator, f.pubkey as any),
|
|
1000
|
-
true,
|
|
1001
|
-
);
|
|
1002
|
-
assert.throws(() => {
|
|
1003
|
-
psbt.validateSignaturesOfInput(f.index, validator, f.incorrectPubkey as any);
|
|
1004
|
-
}, new RegExp('No signatures for this pubkey'));
|
|
1005
|
-
});
|
|
1006
|
-
});
|
|
1007
|
-
|
|
1008
|
-
describe('validateSignaturesOfTapKeyInput', () => {
|
|
1009
|
-
const f = fixtures.validateSignaturesOfTapKeyInput;
|
|
1010
|
-
it('Correctly validates all signatures', () => {
|
|
1011
|
-
initEccLib(ecc);
|
|
1012
|
-
const psbt = Psbt.fromBase64(f.psbt);
|
|
1013
|
-
assert.strictEqual(psbt.validateSignaturesOfInput(f.index, schnorrValidator), true);
|
|
1014
|
-
});
|
|
1015
|
-
|
|
1016
|
-
it('Correctly validates a signature against a pubkey', () => {
|
|
1017
|
-
initEccLib(ecc);
|
|
1018
|
-
const psbt = Psbt.fromBase64(f.psbt);
|
|
1019
|
-
assert.strictEqual(
|
|
1020
|
-
psbt.validateSignaturesOfInput(f.index, schnorrValidator, f.pubkey as any),
|
|
1021
|
-
true,
|
|
1022
|
-
);
|
|
1023
|
-
assert.throws(() => {
|
|
1024
|
-
psbt.validateSignaturesOfInput(f.index, schnorrValidator, f.incorrectPubkey as any);
|
|
1025
|
-
}, new RegExp('No signatures for this pubkey'));
|
|
1026
|
-
});
|
|
1027
|
-
});
|
|
1028
|
-
|
|
1029
|
-
describe('validateSignaturesOfTapScriptInput', () => {
|
|
1030
|
-
const f = fixtures.validateSignaturesOfTapScriptInput;
|
|
1031
|
-
it('Correctly validates all signatures', () => {
|
|
1032
|
-
initEccLib(ecc);
|
|
1033
|
-
const psbt = Psbt.fromBase64(f.psbt);
|
|
1034
|
-
assert.strictEqual(psbt.validateSignaturesOfInput(f.index, schnorrValidator), true);
|
|
1035
|
-
});
|
|
1036
|
-
|
|
1037
|
-
it('Correctly validates a signature against a pubkey', () => {
|
|
1038
|
-
initEccLib(ecc);
|
|
1039
|
-
const psbt = Psbt.fromBase64(f.psbt);
|
|
1040
|
-
assert.strictEqual(
|
|
1041
|
-
psbt.validateSignaturesOfInput(f.index, schnorrValidator, f.pubkey as any),
|
|
1042
|
-
true,
|
|
1043
|
-
);
|
|
1044
|
-
assert.throws(() => {
|
|
1045
|
-
psbt.validateSignaturesOfInput(f.index, schnorrValidator, f.incorrectPubkey as any);
|
|
1046
|
-
}, new RegExp('No signatures for this pubkey'));
|
|
1047
|
-
});
|
|
1048
|
-
});
|
|
1049
|
-
|
|
1050
|
-
describe('tapTreeToList/tapTreeFromList', () => {
|
|
1051
|
-
it('Correctly converts a Taptree to a Tapleaf list and back', () => {
|
|
1052
|
-
taprootFixtures.valid
|
|
1053
|
-
.filter((f) => f.arguments.scriptTree)
|
|
1054
|
-
.map((f) => f.arguments.scriptTree)
|
|
1055
|
-
.forEach((scriptTree) => {
|
|
1056
|
-
const originalTree = convertScriptTree(scriptTree, LEAF_VERSION_TAPSCRIPT);
|
|
1057
|
-
const list = tapTreeToList(originalTree);
|
|
1058
|
-
const treeFromList = tapTreeFromList(list);
|
|
1059
|
-
|
|
1060
|
-
assert.deepStrictEqual(treeFromList, originalTree);
|
|
1061
|
-
});
|
|
1062
|
-
});
|
|
1063
|
-
|
|
1064
|
-
it('Throws if too many leaves on a given level', () => {
|
|
1065
|
-
const list = Array.from({ length: 5 }).map(() => ({
|
|
1066
|
-
depth: 2,
|
|
1067
|
-
leafVersion: LEAF_VERSION_TAPSCRIPT,
|
|
1068
|
-
script: Buffer.from([]),
|
|
1069
|
-
}));
|
|
1070
|
-
assert.throws(() => {
|
|
1071
|
-
tapTreeFromList(list);
|
|
1072
|
-
}, new RegExp('No room left to insert tapleaf in tree'));
|
|
1073
|
-
});
|
|
1074
|
-
|
|
1075
|
-
it('Throws if taptree depth is exceeded', () => {
|
|
1076
|
-
let tree: Taptree = [{ output: Buffer.from([]) }, { output: Buffer.from([]) }];
|
|
1077
|
-
Array.from({ length: 129 }).forEach(() => (tree = [tree, { output: Buffer.from([]) }]));
|
|
1078
|
-
assert.throws(() => {
|
|
1079
|
-
tapTreeToList(tree as Taptree);
|
|
1080
|
-
}, new RegExp('Max taptree depth exceeded.'));
|
|
1081
|
-
});
|
|
1082
|
-
|
|
1083
|
-
it('Throws if tapleaf depth is to high', () => {
|
|
1084
|
-
const list = [
|
|
1085
|
-
{
|
|
1086
|
-
depth: 129,
|
|
1087
|
-
leafVersion: LEAF_VERSION_TAPSCRIPT,
|
|
1088
|
-
script: Buffer.from([]),
|
|
1089
|
-
},
|
|
1090
|
-
];
|
|
1091
|
-
assert.throws(() => {
|
|
1092
|
-
tapTreeFromList(list);
|
|
1093
|
-
}, new RegExp('Max taptree depth exceeded.'));
|
|
1094
|
-
});
|
|
1095
|
-
|
|
1096
|
-
it('Throws if not a valid taptree structure', () => {
|
|
1097
|
-
const tree = Array.from({ length: 3 }).map(() => ({
|
|
1098
|
-
output: Buffer.from([]),
|
|
1099
|
-
}));
|
|
1100
|
-
|
|
1101
|
-
assert.throws(() => {
|
|
1102
|
-
tapTreeToList(tree as Taptree);
|
|
1103
|
-
}, new RegExp('Cannot convert taptree to tapleaf list. Expecting a tapree structure.'));
|
|
1104
|
-
});
|
|
1105
|
-
});
|
|
1106
|
-
|
|
1107
|
-
describe('getFeeRate', () => {
|
|
1108
|
-
it('Throws error if called before inputs are finalized', () => {
|
|
1109
|
-
const f = fixtures.getFeeRate;
|
|
1110
|
-
const psbt = Psbt.fromBase64(f.psbt);
|
|
1111
|
-
|
|
1112
|
-
assert.throws(() => {
|
|
1113
|
-
psbt.getFeeRate();
|
|
1114
|
-
}, new RegExp('PSBT must be finalized to calculate fee rate'));
|
|
1115
|
-
|
|
1116
|
-
psbt.finalizeAllInputs();
|
|
1117
|
-
|
|
1118
|
-
assert.strictEqual(psbt.getFeeRate(), f.fee);
|
|
1119
|
-
(psbt as any).__CACHE.__FEE_RATE = undefined;
|
|
1120
|
-
assert.strictEqual(psbt.getFeeRate(), f.fee);
|
|
1121
|
-
});
|
|
1122
|
-
});
|
|
1123
|
-
|
|
1124
|
-
describe('create 1-to-1 transaction', () => {
|
|
1125
|
-
const alice = ECPair.fromWIF('L2uPYXe17xSTqbCjZvL2DsyXPCbXspvcu5mHLDYUgzdUbZGSKrSr');
|
|
1126
|
-
const psbt = new Psbt();
|
|
1127
|
-
psbt.addInput({
|
|
1128
|
-
hash: '7d067b4a697a09d2c3cff7d4d9506c9955e93bff41bf82d439da7d030382bc3e',
|
|
1129
|
-
index: 0,
|
|
1130
|
-
nonWitnessUtxo: Buffer.from(
|
|
1131
|
-
'0200000001f9f34e95b9d5c8abcd20fc5bd4a825d1517be62f0f775e5f36da944d9' +
|
|
1132
|
-
'452e550000000006b483045022100c86e9a111afc90f64b4904bd609e9eaed80d48' +
|
|
1133
|
-
'ca17c162b1aca0a788ac3526f002207bb79b60d4fc6526329bf18a77135dc566020' +
|
|
1134
|
-
'9e761da46e1c2f1152ec013215801210211755115eabf846720f5cb18f248666fec' +
|
|
1135
|
-
'631e5e1e66009ce3710ceea5b1ad13ffffffff01905f0100000000001976a9148bb' +
|
|
1136
|
-
'c95d2709c71607c60ee3f097c1217482f518d88ac00000000',
|
|
1137
|
-
'hex',
|
|
1138
|
-
),
|
|
1139
|
-
sighashType: 1,
|
|
1140
|
-
});
|
|
1141
|
-
psbt.addOutput({
|
|
1142
|
-
address: '1KRMKfeZcmosxALVYESdPNez1AP1mEtywp',
|
|
1143
|
-
value: 80000,
|
|
1144
|
-
});
|
|
1145
|
-
psbt.signInput(0, alice);
|
|
1146
|
-
assert.throws(() => {
|
|
1147
|
-
psbt.setVersion(3);
|
|
1148
|
-
}, new RegExp('Can not modify transaction, signatures exist.'));
|
|
1149
|
-
psbt.validateSignaturesOfInput(0, validator);
|
|
1150
|
-
psbt.finalizeAllInputs();
|
|
1151
|
-
assert.throws(() => {
|
|
1152
|
-
psbt.setVersion(3);
|
|
1153
|
-
}, new RegExp('Can not modify transaction, signatures exist.'));
|
|
1154
|
-
assert.strictEqual(psbt.inputHasPubkey(0, alice.publicKey), true);
|
|
1155
|
-
assert.strictEqual(psbt.outputHasPubkey(0, alice.publicKey), false);
|
|
1156
|
-
assert.strictEqual(
|
|
1157
|
-
psbt.extractTransaction().toHex(),
|
|
1158
|
-
'02000000013ebc8203037dda39d482bf41ff3be955996c50d9d4f7cfc3d2097a694a7' +
|
|
1159
|
-
'b067d000000006b483045022100931b6db94aed25d5486884d83fc37160f37f3368c0' +
|
|
1160
|
-
'd7f48c757112abefec983802205fda64cff98c849577026eb2ce916a50ea70626a766' +
|
|
1161
|
-
'9f8596dd89b720a26b4d501210365db9da3f8a260078a7e8f8b708a1161468fb2323f' +
|
|
1162
|
-
'fda5ec16b261ec1056f455ffffffff0180380100000000001976a914ca0d36044e0dc' +
|
|
1163
|
-
'08a22724efa6f6a07b0ec4c79aa88ac00000000',
|
|
1164
|
-
);
|
|
1165
|
-
});
|
|
1166
|
-
|
|
1167
|
-
describe('Method return types', () => {
|
|
1168
|
-
it('fromBuffer returns Psbt type (not base class)', () => {
|
|
1169
|
-
const psbt = Psbt.fromBuffer(
|
|
1170
|
-
Buffer.from(
|
|
1171
|
-
'70736274ff01000a01000000000000000000000000',
|
|
1172
|
-
'hex', // cHNidP8BAAoBAAAAAAAAAAAAAAAA
|
|
1173
|
-
),
|
|
1174
|
-
);
|
|
1175
|
-
assert.strictEqual(psbt instanceof Psbt, true);
|
|
1176
|
-
assert.ok((psbt as any).__CACHE.__TX);
|
|
1177
|
-
});
|
|
1178
|
-
it('fromBase64 returns Psbt type (not base class)', () => {
|
|
1179
|
-
const psbt = Psbt.fromBase64('cHNidP8BAAoBAAAAAAAAAAAAAAAA');
|
|
1180
|
-
assert.strictEqual(psbt instanceof Psbt, true);
|
|
1181
|
-
assert.ok((psbt as any).__CACHE.__TX);
|
|
1182
|
-
});
|
|
1183
|
-
it('fromHex returns Psbt type (not base class)', () => {
|
|
1184
|
-
const psbt = Psbt.fromHex('70736274ff01000a01000000000000000000000000');
|
|
1185
|
-
assert.strictEqual(psbt instanceof Psbt, true);
|
|
1186
|
-
assert.ok((psbt as any).__CACHE.__TX);
|
|
1187
|
-
});
|
|
1188
|
-
});
|
|
1189
|
-
|
|
1190
|
-
describe('Cache', () => {
|
|
1191
|
-
it('non-witness UTXOs are cached', () => {
|
|
1192
|
-
const f = fixtures.cache.nonWitnessUtxo;
|
|
1193
|
-
const psbt = Psbt.fromBase64(f.psbt);
|
|
1194
|
-
const index = f.inputIndex;
|
|
1195
|
-
|
|
1196
|
-
// Cache is empty
|
|
1197
|
-
assert.strictEqual(
|
|
1198
|
-
(psbt as any).__CACHE.__NON_WITNESS_UTXO_BUF_CACHE[index],
|
|
1199
|
-
undefined,
|
|
1200
|
-
);
|
|
1201
|
-
|
|
1202
|
-
// Cache is populated
|
|
1203
|
-
psbt.updateInput(index, {
|
|
1204
|
-
nonWitnessUtxo: f.nonWitnessUtxo as any,
|
|
1205
|
-
});
|
|
1206
|
-
const value = psbt.data.inputs[index].nonWitnessUtxo;
|
|
1207
|
-
assert.ok((psbt as any).__CACHE.__NON_WITNESS_UTXO_BUF_CACHE[index].equals(value));
|
|
1208
|
-
assert.ok(
|
|
1209
|
-
(psbt as any).__CACHE.__NON_WITNESS_UTXO_BUF_CACHE[index].equals(f.nonWitnessUtxo),
|
|
1210
|
-
);
|
|
1211
|
-
|
|
1212
|
-
// Cache is rebuilt from internal transaction object when cleared
|
|
1213
|
-
psbt.data.inputs[index].nonWitnessUtxo = Buffer.from([1, 2, 3]);
|
|
1214
|
-
(psbt as any).__CACHE.__NON_WITNESS_UTXO_BUF_CACHE[index] = undefined;
|
|
1215
|
-
assert.ok((psbt as any).data.inputs[index].nonWitnessUtxo.equals(value));
|
|
1216
|
-
});
|
|
1217
|
-
});
|
|
1218
|
-
|
|
1219
|
-
describe('Transaction properties', () => {
|
|
1220
|
-
it('.version is exposed and is settable', () => {
|
|
1221
|
-
const psbt = new Psbt();
|
|
1222
|
-
|
|
1223
|
-
assert.strictEqual(psbt.version, 2);
|
|
1224
|
-
assert.strictEqual(psbt.version, (psbt as any).__CACHE.__TX.version);
|
|
1225
|
-
|
|
1226
|
-
psbt.version = 1;
|
|
1227
|
-
assert.strictEqual(psbt.version, 1);
|
|
1228
|
-
assert.strictEqual(psbt.version, (psbt as any).__CACHE.__TX.version);
|
|
1229
|
-
});
|
|
1230
|
-
|
|
1231
|
-
it('.locktime is exposed and is settable', () => {
|
|
1232
|
-
const psbt = new Psbt();
|
|
1233
|
-
|
|
1234
|
-
assert.strictEqual(psbt.locktime, 0);
|
|
1235
|
-
assert.strictEqual(psbt.locktime, (psbt as any).__CACHE.__TX.locktime);
|
|
1236
|
-
|
|
1237
|
-
psbt.locktime = 123;
|
|
1238
|
-
assert.strictEqual(psbt.locktime, 123);
|
|
1239
|
-
assert.strictEqual(psbt.locktime, (psbt as any).__CACHE.__TX.locktime);
|
|
1240
|
-
});
|
|
1241
|
-
|
|
1242
|
-
it('.txInputs is exposed as a readonly clone', () => {
|
|
1243
|
-
const psbt = new Psbt();
|
|
1244
|
-
const hash = Buffer.alloc(32);
|
|
1245
|
-
const index = 0;
|
|
1246
|
-
psbt.addInput({ hash, index });
|
|
1247
|
-
|
|
1248
|
-
const input = psbt.txInputs[0];
|
|
1249
|
-
const internalInput = (psbt as any).__CACHE.__TX.ins[0];
|
|
1250
|
-
|
|
1251
|
-
assert.ok(input.hash.equals(internalInput.hash));
|
|
1252
|
-
assert.strictEqual(input.index, internalInput.index);
|
|
1253
|
-
assert.strictEqual(input.sequence, internalInput.sequence);
|
|
1254
|
-
|
|
1255
|
-
input.hash[0] = 123;
|
|
1256
|
-
input.index = 123;
|
|
1257
|
-
input.sequence = 123;
|
|
1258
|
-
|
|
1259
|
-
assert.ok(!input.hash.equals(internalInput.hash));
|
|
1260
|
-
assert.notEqual(input.index, internalInput.index);
|
|
1261
|
-
assert.notEqual(input.sequence, internalInput.sequence);
|
|
1262
|
-
});
|
|
1263
|
-
|
|
1264
|
-
it('.txOutputs is exposed as a readonly clone', () => {
|
|
1265
|
-
const psbt = new Psbt();
|
|
1266
|
-
const address = '1LukeQU5jwebXbMLDVydeH4vFSobRV9rkj';
|
|
1267
|
-
const value = 100000;
|
|
1268
|
-
psbt.addOutput({ address, value });
|
|
1269
|
-
|
|
1270
|
-
const output = psbt.txOutputs[0];
|
|
1271
|
-
const internalInput = (psbt as any).__CACHE.__TX.outs[0];
|
|
1272
|
-
|
|
1273
|
-
assert.strictEqual(output.address, address);
|
|
1274
|
-
|
|
1275
|
-
assert.ok(output.script.equals(internalInput.script));
|
|
1276
|
-
assert.strictEqual(output.value, internalInput.value);
|
|
1277
|
-
|
|
1278
|
-
output.script[0] = 123;
|
|
1279
|
-
output.value = 123;
|
|
1280
|
-
|
|
1281
|
-
assert.ok(!output.script.equals(internalInput.script));
|
|
1282
|
-
assert.notEqual(output.value, internalInput.value);
|
|
1283
|
-
});
|
|
1284
|
-
});
|
|
1285
|
-
});
|
|
1
|
+
import assert from 'assert';
|
|
2
|
+
import { BIP32Factory } from 'bip32';
|
|
3
|
+
import * as ecc from 'tiny-secp256k1';
|
|
4
|
+
import * as crypto from 'crypto';
|
|
5
|
+
import { ECPairFactory } from 'ecpair';
|
|
6
|
+
import { describe, it } from 'mocha';
|
|
7
|
+
|
|
8
|
+
import { convertScriptTree } from './payments.utils.js';
|
|
9
|
+
import { LEAF_VERSION_TAPSCRIPT } from '../src/payments/bip341.js';
|
|
10
|
+
import { tapTreeFromList, tapTreeToList } from '../src/psbt/bip371.js';
|
|
11
|
+
import { Taptree } from '../src/types.js';
|
|
12
|
+
import {
|
|
13
|
+
initEccLib,
|
|
14
|
+
networks as NETWORKS,
|
|
15
|
+
payments,
|
|
16
|
+
Psbt,
|
|
17
|
+
Signer,
|
|
18
|
+
SignerAsync,
|
|
19
|
+
} from '../src/index.js';
|
|
20
|
+
|
|
21
|
+
import preFixtures from './fixtures/psbt.json' with { type: 'json' };
|
|
22
|
+
import taprootFixtures from './fixtures/p2tr.json' with { type: 'json' };
|
|
23
|
+
|
|
24
|
+
const bip32 = BIP32Factory(ecc);
|
|
25
|
+
const ECPair = ECPairFactory(ecc);
|
|
26
|
+
|
|
27
|
+
const validator = (pubkey: Buffer, msghash: Buffer, signature: Buffer): boolean =>
|
|
28
|
+
ECPair.fromPublicKey(pubkey).verify(msghash, signature);
|
|
29
|
+
|
|
30
|
+
const schnorrValidator = (pubkey: Buffer, msghash: Buffer, signature: Buffer): boolean =>
|
|
31
|
+
ecc.verifySchnorr(msghash, pubkey, signature);
|
|
32
|
+
|
|
33
|
+
const initBuffers = (object: any): typeof preFixtures =>
|
|
34
|
+
JSON.parse(JSON.stringify(object), (_, value) => {
|
|
35
|
+
const regex = new RegExp(/^Buffer.from\(['"](.*)['"], ['"](.*)['"]\)$/);
|
|
36
|
+
const result = regex.exec(value);
|
|
37
|
+
if (!result) return value;
|
|
38
|
+
|
|
39
|
+
const data = result[1];
|
|
40
|
+
const encoding = result[2];
|
|
41
|
+
|
|
42
|
+
return Buffer.from(data, encoding as BufferEncoding);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const fixtures = initBuffers(preFixtures);
|
|
46
|
+
|
|
47
|
+
const upperCaseFirstLetter = (str: string): string => str.replace(/^./, (s) => s.toUpperCase());
|
|
48
|
+
|
|
49
|
+
const toAsyncSigner = (signer: Signer): SignerAsync => {
|
|
50
|
+
return {
|
|
51
|
+
publicKey: signer.publicKey,
|
|
52
|
+
sign: (hash: Buffer, lowerR: boolean | undefined): Promise<Buffer> => {
|
|
53
|
+
return new Promise((resolve, rejects): void => {
|
|
54
|
+
setTimeout(() => {
|
|
55
|
+
try {
|
|
56
|
+
const r = signer.sign(hash, lowerR);
|
|
57
|
+
resolve(r);
|
|
58
|
+
} catch (e) {
|
|
59
|
+
rejects(e as Error);
|
|
60
|
+
}
|
|
61
|
+
}, 10);
|
|
62
|
+
});
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
const failedAsyncSigner = (publicKey: Buffer): SignerAsync => {
|
|
67
|
+
return <SignerAsync>{
|
|
68
|
+
publicKey,
|
|
69
|
+
sign: (__: Buffer): Promise<Buffer> => {
|
|
70
|
+
return new Promise((_, reject): void => {
|
|
71
|
+
setTimeout(() => {
|
|
72
|
+
reject(new Error('sign failed'));
|
|
73
|
+
}, 10);
|
|
74
|
+
});
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
// const b = (hex: string) => Buffer.from(hex, 'hex');
|
|
79
|
+
|
|
80
|
+
describe(`Psbt`, () => {
|
|
81
|
+
beforeEach(() => {
|
|
82
|
+
// provide the ECC lib only when required
|
|
83
|
+
initEccLib(undefined);
|
|
84
|
+
});
|
|
85
|
+
describe('BIP174 Test Vectors', () => {
|
|
86
|
+
fixtures.bip174.invalid.forEach((f) => {
|
|
87
|
+
it(`Invalid: ${f.description}`, () => {
|
|
88
|
+
assert.throws(() => {
|
|
89
|
+
Psbt.fromBase64(f.psbt);
|
|
90
|
+
}, new RegExp(f.errorMessage));
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
fixtures.bip174.valid.forEach((f) => {
|
|
95
|
+
it(`Valid: ${f.description}`, () => {
|
|
96
|
+
assert.doesNotThrow(() => {
|
|
97
|
+
Psbt.fromBase64(f.psbt);
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
fixtures.bip174.failSignChecks.forEach((f) => {
|
|
103
|
+
const keyPair = ECPair.makeRandom();
|
|
104
|
+
it(`Fails Signer checks: ${f.description}`, () => {
|
|
105
|
+
const psbt = Psbt.fromBase64(f.psbt);
|
|
106
|
+
assert.throws(() => {
|
|
107
|
+
psbt.signInput(f.inputToCheck, keyPair);
|
|
108
|
+
}, new RegExp(f.errorMessage));
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
fixtures.bip174.creator.forEach((f) => {
|
|
113
|
+
it('Creates expected PSBT', () => {
|
|
114
|
+
const psbt = new Psbt();
|
|
115
|
+
for (const input of f.inputs) {
|
|
116
|
+
psbt.addInput(input);
|
|
117
|
+
}
|
|
118
|
+
for (const output of f.outputs) {
|
|
119
|
+
const script = Buffer.from(output.script, 'hex');
|
|
120
|
+
psbt.addOutput({ ...output, script });
|
|
121
|
+
}
|
|
122
|
+
assert.strictEqual(psbt.toBase64(), f.result);
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
fixtures.bip174.updater.forEach((f) => {
|
|
127
|
+
it('Updates PSBT to the expected result', () => {
|
|
128
|
+
if (f.isTaproot) initEccLib(ecc);
|
|
129
|
+
const psbt = Psbt.fromBase64(f.psbt);
|
|
130
|
+
|
|
131
|
+
for (const inputOrOutput of ['input', 'output']) {
|
|
132
|
+
const fixtureData = (f as any)[`${inputOrOutput}Data`];
|
|
133
|
+
if (fixtureData) {
|
|
134
|
+
for (const [i, data] of fixtureData.entries()) {
|
|
135
|
+
const txt = upperCaseFirstLetter(inputOrOutput);
|
|
136
|
+
(psbt as any)[`update${txt}`](i, data);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
assert.strictEqual(psbt.toBase64(), f.result);
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
fixtures.bip174.signer.forEach((f) => {
|
|
146
|
+
it('Signs PSBT to the expected result', () => {
|
|
147
|
+
if (f.isTaproot) initEccLib(ecc);
|
|
148
|
+
const psbt = Psbt.fromBase64(f.psbt);
|
|
149
|
+
|
|
150
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
151
|
+
// @ts-ignore // cannot find tapLeafHashToSign
|
|
152
|
+
f.keys.forEach(({ inputToSign, tapLeafHashToSign, WIF }) => {
|
|
153
|
+
const keyPair = ECPair.fromWIF(WIF, NETWORKS.testnet);
|
|
154
|
+
if (tapLeafHashToSign)
|
|
155
|
+
psbt.signTaprootInput(
|
|
156
|
+
inputToSign,
|
|
157
|
+
keyPair,
|
|
158
|
+
Buffer.from(tapLeafHashToSign, 'hex'),
|
|
159
|
+
);
|
|
160
|
+
else psbt.signInput(inputToSign, keyPair);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
assert.strictEqual(psbt.toBase64(), f.result);
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
fixtures.bip174.combiner.forEach((f) => {
|
|
168
|
+
it('Combines two PSBTs to the expected result', () => {
|
|
169
|
+
const psbts = f.psbts.map((psbt) => Psbt.fromBase64(psbt));
|
|
170
|
+
|
|
171
|
+
psbts[0].combine(psbts[1]);
|
|
172
|
+
|
|
173
|
+
// Produces a different Base64 string due to implemetation specific key-value ordering.
|
|
174
|
+
// That means this test will fail:
|
|
175
|
+
// assert.strictEqual(psbts[0].toBase64(), f.result)
|
|
176
|
+
// However, if we compare the actual PSBT properties we can see they are logically identical:
|
|
177
|
+
assert.deepStrictEqual(psbts[0], Psbt.fromBase64(f.result));
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
fixtures.bip174.finalizer.forEach((f) => {
|
|
182
|
+
it('Finalizes inputs and gives the expected PSBT', () => {
|
|
183
|
+
if (f.isTaproot) initEccLib(ecc);
|
|
184
|
+
const psbt = Psbt.fromBase64(f.psbt);
|
|
185
|
+
|
|
186
|
+
psbt.finalizeAllInputs();
|
|
187
|
+
|
|
188
|
+
assert.strictEqual(psbt.toBase64(), f.result);
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
fixtures.bip174.extractor.forEach((f) => {
|
|
193
|
+
it('Extracts the expected transaction from a PSBT', () => {
|
|
194
|
+
const psbt1 = Psbt.fromBase64(f.psbt);
|
|
195
|
+
const transaction1 = psbt1.extractTransaction(true).toHex();
|
|
196
|
+
|
|
197
|
+
const psbt2 = Psbt.fromBase64(f.psbt);
|
|
198
|
+
const transaction2 = psbt2.extractTransaction().toHex();
|
|
199
|
+
|
|
200
|
+
assert.strictEqual(transaction1, transaction2);
|
|
201
|
+
assert.strictEqual(transaction1, f.transaction);
|
|
202
|
+
|
|
203
|
+
const psbt3 = Psbt.fromBase64(f.psbt);
|
|
204
|
+
delete psbt3.data.inputs[0].finalScriptSig;
|
|
205
|
+
delete psbt3.data.inputs[0].finalScriptWitness;
|
|
206
|
+
assert.throws(() => {
|
|
207
|
+
psbt3.extractTransaction();
|
|
208
|
+
}, new RegExp('Not finalized'));
|
|
209
|
+
|
|
210
|
+
const psbt4 = Psbt.fromBase64(f.psbt);
|
|
211
|
+
psbt4.setMaximumFeeRate(1);
|
|
212
|
+
assert.throws(() => {
|
|
213
|
+
psbt4.extractTransaction();
|
|
214
|
+
}, new RegExp('Warning: You are paying around [\\d.]+ in fees'));
|
|
215
|
+
|
|
216
|
+
const psbt5 = Psbt.fromBase64(f.psbt);
|
|
217
|
+
psbt5.extractTransaction(true);
|
|
218
|
+
const fr1 = psbt5.getFeeRate();
|
|
219
|
+
const fr2 = psbt5.getFeeRate();
|
|
220
|
+
assert.strictEqual(fr1, fr2);
|
|
221
|
+
|
|
222
|
+
const psbt6 = Psbt.fromBase64(f.psbt);
|
|
223
|
+
const f1 = psbt6.getFee();
|
|
224
|
+
const f2 = psbt6.getFee();
|
|
225
|
+
assert.strictEqual(f1, f2);
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
describe('signInputAsync', () => {
|
|
231
|
+
fixtures.signInput.checks.forEach((f) => {
|
|
232
|
+
it(f.description, async () => {
|
|
233
|
+
if (f.isTaproot) initEccLib(ecc);
|
|
234
|
+
if (f.shouldSign) {
|
|
235
|
+
const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt);
|
|
236
|
+
await assert.doesNotReject(async () => {
|
|
237
|
+
await psbtThatShouldsign.signInputAsync(
|
|
238
|
+
f.shouldSign.inputToCheck,
|
|
239
|
+
ECPair.fromWIF(f.shouldSign.WIF),
|
|
240
|
+
f.shouldSign.sighashTypes || undefined,
|
|
241
|
+
);
|
|
242
|
+
if (f.shouldSign.result) {
|
|
243
|
+
assert.strictEqual(psbtThatShouldsign.toBase64(), f.shouldSign.result);
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
const failMessage = f.isTaproot
|
|
247
|
+
? /Need Schnorr Signer to sign taproot input #0./
|
|
248
|
+
: /sign failed/;
|
|
249
|
+
await assert.rejects(async () => {
|
|
250
|
+
await psbtThatShouldsign.signInputAsync(
|
|
251
|
+
f.shouldSign.inputToCheck,
|
|
252
|
+
failedAsyncSigner(ECPair.fromWIF(f.shouldSign.WIF).publicKey),
|
|
253
|
+
f.shouldSign.sighashTypes || undefined,
|
|
254
|
+
);
|
|
255
|
+
}, failMessage);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
if (f.shouldThrow) {
|
|
259
|
+
const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt);
|
|
260
|
+
await assert.rejects(async () => {
|
|
261
|
+
await psbtThatShouldThrow.signInputAsync(
|
|
262
|
+
f.shouldThrow.inputToCheck,
|
|
263
|
+
ECPair.fromWIF(f.shouldThrow.WIF),
|
|
264
|
+
(f.shouldThrow as any).sighashTypes || undefined,
|
|
265
|
+
);
|
|
266
|
+
}, new RegExp(f.shouldThrow.errorMessage));
|
|
267
|
+
await assert.rejects(async () => {
|
|
268
|
+
await psbtThatShouldThrow.signInputAsync(
|
|
269
|
+
f.shouldThrow.inputToCheck,
|
|
270
|
+
toAsyncSigner(ECPair.fromWIF(f.shouldThrow.WIF)),
|
|
271
|
+
(f.shouldThrow as any).sighashTypes || undefined,
|
|
272
|
+
);
|
|
273
|
+
}, new RegExp(f.shouldThrow.errorMessage));
|
|
274
|
+
await assert.rejects(async () => {
|
|
275
|
+
await (psbtThatShouldThrow.signInputAsync as any)(
|
|
276
|
+
f.shouldThrow.inputToCheck,
|
|
277
|
+
);
|
|
278
|
+
}, new RegExp('Need Signer to sign input'));
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
});
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
describe('signInput', () => {
|
|
285
|
+
fixtures.signInput.checks.forEach((f) => {
|
|
286
|
+
it(f.description, () => {
|
|
287
|
+
if (f.isTaproot) initEccLib(ecc);
|
|
288
|
+
if (f.shouldSign) {
|
|
289
|
+
const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt);
|
|
290
|
+
assert.doesNotThrow(() => {
|
|
291
|
+
psbtThatShouldsign.signInput(
|
|
292
|
+
f.shouldSign.inputToCheck,
|
|
293
|
+
ECPair.fromWIF(f.shouldSign.WIF),
|
|
294
|
+
f.shouldSign.sighashTypes || undefined,
|
|
295
|
+
);
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
if (f.shouldThrow) {
|
|
300
|
+
const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt);
|
|
301
|
+
assert.throws(() => {
|
|
302
|
+
psbtThatShouldThrow.signInput(
|
|
303
|
+
f.shouldThrow.inputToCheck,
|
|
304
|
+
ECPair.fromWIF(f.shouldThrow.WIF),
|
|
305
|
+
(f.shouldThrow as any).sighashTypes || undefined,
|
|
306
|
+
);
|
|
307
|
+
}, new RegExp(f.shouldThrow.errorMessage));
|
|
308
|
+
assert.throws(() => {
|
|
309
|
+
(psbtThatShouldThrow.signInput as any)(f.shouldThrow.inputToCheck);
|
|
310
|
+
}, new RegExp('Need Signer to sign input'));
|
|
311
|
+
}
|
|
312
|
+
});
|
|
313
|
+
});
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
describe('signAllInputsAsync', () => {
|
|
317
|
+
fixtures.signInput.checks.forEach((f) => {
|
|
318
|
+
if (f.description === 'checks the input exists') return;
|
|
319
|
+
it(f.description, async () => {
|
|
320
|
+
if (f.isTaproot) initEccLib(ecc);
|
|
321
|
+
if (f.shouldSign) {
|
|
322
|
+
const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt);
|
|
323
|
+
await assert.doesNotReject(async () => {
|
|
324
|
+
await psbtThatShouldsign.signAllInputsAsync(
|
|
325
|
+
ECPair.fromWIF(f.shouldSign.WIF),
|
|
326
|
+
f.shouldSign.sighashTypes || undefined,
|
|
327
|
+
);
|
|
328
|
+
});
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
if (f.shouldThrow) {
|
|
332
|
+
const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt);
|
|
333
|
+
await assert.rejects(async () => {
|
|
334
|
+
await psbtThatShouldThrow.signAllInputsAsync(
|
|
335
|
+
ECPair.fromWIF(f.shouldThrow.WIF),
|
|
336
|
+
(f.shouldThrow as any).sighashTypes || undefined,
|
|
337
|
+
);
|
|
338
|
+
}, new RegExp('No inputs were signed'));
|
|
339
|
+
await assert.rejects(async () => {
|
|
340
|
+
await (psbtThatShouldThrow.signAllInputsAsync as any)();
|
|
341
|
+
}, new RegExp('Need Signer to sign input'));
|
|
342
|
+
}
|
|
343
|
+
});
|
|
344
|
+
});
|
|
345
|
+
});
|
|
346
|
+
|
|
347
|
+
describe('signAllInputs', () => {
|
|
348
|
+
fixtures.signInput.checks.forEach((f) => {
|
|
349
|
+
if (f.description === 'checks the input exists') return;
|
|
350
|
+
it(f.description, () => {
|
|
351
|
+
if (f.isTaproot) initEccLib(ecc);
|
|
352
|
+
if (f.shouldSign) {
|
|
353
|
+
const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt);
|
|
354
|
+
assert.doesNotThrow(() => {
|
|
355
|
+
psbtThatShouldsign.signAllInputs(
|
|
356
|
+
ECPair.fromWIF(f.shouldSign.WIF),
|
|
357
|
+
f.shouldSign.sighashTypes || undefined,
|
|
358
|
+
);
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
if (f.shouldThrow) {
|
|
363
|
+
const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt);
|
|
364
|
+
assert.throws(() => {
|
|
365
|
+
psbtThatShouldThrow.signAllInputs(
|
|
366
|
+
ECPair.fromWIF(f.shouldThrow.WIF),
|
|
367
|
+
(f.shouldThrow as any).sighashTypes || undefined,
|
|
368
|
+
);
|
|
369
|
+
}, new RegExp('No inputs were signed'));
|
|
370
|
+
assert.throws(() => {
|
|
371
|
+
(psbtThatShouldThrow.signAllInputs as any)();
|
|
372
|
+
}, new RegExp('Need Signer to sign input'));
|
|
373
|
+
}
|
|
374
|
+
});
|
|
375
|
+
});
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
describe('signInputHDAsync', () => {
|
|
379
|
+
fixtures.signInputHD.checks.forEach((f) => {
|
|
380
|
+
it(f.description, async () => {
|
|
381
|
+
if (f.shouldSign) {
|
|
382
|
+
const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt);
|
|
383
|
+
await assert.doesNotReject(async () => {
|
|
384
|
+
await psbtThatShouldsign.signInputHDAsync(
|
|
385
|
+
f.shouldSign.inputToCheck,
|
|
386
|
+
bip32.fromBase58(f.shouldSign.xprv),
|
|
387
|
+
(f.shouldSign as any).sighashTypes || undefined,
|
|
388
|
+
);
|
|
389
|
+
});
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
if (f.shouldThrow) {
|
|
393
|
+
const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt);
|
|
394
|
+
await assert.rejects(async () => {
|
|
395
|
+
await psbtThatShouldThrow.signInputHDAsync(
|
|
396
|
+
f.shouldThrow.inputToCheck,
|
|
397
|
+
bip32.fromBase58(f.shouldThrow.xprv),
|
|
398
|
+
(f.shouldThrow as any).sighashTypes || undefined,
|
|
399
|
+
);
|
|
400
|
+
}, new RegExp(f.shouldThrow.errorMessage));
|
|
401
|
+
await assert.rejects(async () => {
|
|
402
|
+
await (psbtThatShouldThrow.signInputHDAsync as any)(
|
|
403
|
+
f.shouldThrow.inputToCheck,
|
|
404
|
+
);
|
|
405
|
+
}, new RegExp('Need HDSigner to sign input'));
|
|
406
|
+
}
|
|
407
|
+
});
|
|
408
|
+
});
|
|
409
|
+
});
|
|
410
|
+
|
|
411
|
+
describe('signInputHD', () => {
|
|
412
|
+
fixtures.signInputHD.checks.forEach((f) => {
|
|
413
|
+
it(f.description, () => {
|
|
414
|
+
if (f.shouldSign) {
|
|
415
|
+
const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt);
|
|
416
|
+
assert.doesNotThrow(() => {
|
|
417
|
+
psbtThatShouldsign.signInputHD(
|
|
418
|
+
f.shouldSign.inputToCheck,
|
|
419
|
+
bip32.fromBase58(f.shouldSign.xprv),
|
|
420
|
+
(f.shouldSign as any).sighashTypes || undefined,
|
|
421
|
+
);
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
if (f.shouldThrow) {
|
|
426
|
+
const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt);
|
|
427
|
+
assert.throws(() => {
|
|
428
|
+
psbtThatShouldThrow.signInputHD(
|
|
429
|
+
f.shouldThrow.inputToCheck,
|
|
430
|
+
bip32.fromBase58(f.shouldThrow.xprv),
|
|
431
|
+
(f.shouldThrow as any).sighashTypes || undefined,
|
|
432
|
+
);
|
|
433
|
+
}, new RegExp(f.shouldThrow.errorMessage));
|
|
434
|
+
assert.throws(() => {
|
|
435
|
+
(psbtThatShouldThrow.signInputHD as any)(f.shouldThrow.inputToCheck);
|
|
436
|
+
}, new RegExp('Need HDSigner to sign input'));
|
|
437
|
+
}
|
|
438
|
+
});
|
|
439
|
+
});
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
describe('signAllInputsHDAsync', () => {
|
|
443
|
+
fixtures.signInputHD.checks.forEach((f) => {
|
|
444
|
+
it(f.description, async () => {
|
|
445
|
+
if (f.shouldSign) {
|
|
446
|
+
const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt);
|
|
447
|
+
await assert.doesNotReject(async () => {
|
|
448
|
+
await psbtThatShouldsign.signAllInputsHDAsync(
|
|
449
|
+
bip32.fromBase58(f.shouldSign.xprv),
|
|
450
|
+
(f.shouldSign as any).sighashTypes || undefined,
|
|
451
|
+
);
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
if (f.shouldThrow) {
|
|
456
|
+
const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt);
|
|
457
|
+
await assert.rejects(async () => {
|
|
458
|
+
await psbtThatShouldThrow.signAllInputsHDAsync(
|
|
459
|
+
bip32.fromBase58(f.shouldThrow.xprv),
|
|
460
|
+
(f.shouldThrow as any).sighashTypes || undefined,
|
|
461
|
+
);
|
|
462
|
+
}, new RegExp('No inputs were signed'));
|
|
463
|
+
await assert.rejects(async () => {
|
|
464
|
+
await (psbtThatShouldThrow.signAllInputsHDAsync as any)();
|
|
465
|
+
}, new RegExp('Need HDSigner to sign input'));
|
|
466
|
+
}
|
|
467
|
+
});
|
|
468
|
+
});
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
describe('signAllInputsHD', () => {
|
|
472
|
+
fixtures.signInputHD.checks.forEach((f) => {
|
|
473
|
+
it(f.description, () => {
|
|
474
|
+
if (f.shouldSign) {
|
|
475
|
+
const psbtThatShouldsign = Psbt.fromBase64(f.shouldSign.psbt);
|
|
476
|
+
assert.doesNotThrow(() => {
|
|
477
|
+
psbtThatShouldsign.signAllInputsHD(
|
|
478
|
+
bip32.fromBase58(f.shouldSign.xprv),
|
|
479
|
+
(f.shouldSign as any).sighashTypes || undefined,
|
|
480
|
+
);
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
if (f.shouldThrow) {
|
|
485
|
+
const psbtThatShouldThrow = Psbt.fromBase64(f.shouldThrow.psbt);
|
|
486
|
+
assert.throws(() => {
|
|
487
|
+
psbtThatShouldThrow.signAllInputsHD(
|
|
488
|
+
bip32.fromBase58(f.shouldThrow.xprv),
|
|
489
|
+
(f.shouldThrow as any).sighashTypes || undefined,
|
|
490
|
+
);
|
|
491
|
+
}, new RegExp('No inputs were signed'));
|
|
492
|
+
assert.throws(() => {
|
|
493
|
+
(psbtThatShouldThrow.signAllInputsHD as any)();
|
|
494
|
+
}, new RegExp('Need HDSigner to sign input'));
|
|
495
|
+
}
|
|
496
|
+
});
|
|
497
|
+
});
|
|
498
|
+
});
|
|
499
|
+
|
|
500
|
+
describe('finalizeInput', () => {
|
|
501
|
+
it(`Finalizes tapleaf by hash`, () => {
|
|
502
|
+
const f = fixtures.finalizeInput.finalizeTapleafByHash;
|
|
503
|
+
const psbt = Psbt.fromBase64(f.psbt);
|
|
504
|
+
|
|
505
|
+
psbt.finalizeTaprootInput(f.index, Buffer.from(f.leafHash, 'hex'));
|
|
506
|
+
|
|
507
|
+
assert.strictEqual(psbt.toBase64(), f.result);
|
|
508
|
+
});
|
|
509
|
+
|
|
510
|
+
it(`fails if tapleaf hash not found`, () => {
|
|
511
|
+
const f = fixtures.finalizeInput.finalizeTapleafByHash;
|
|
512
|
+
const psbt = Psbt.fromBase64(f.psbt);
|
|
513
|
+
|
|
514
|
+
assert.throws(() => {
|
|
515
|
+
psbt.finalizeTaprootInput(f.index, Buffer.from(f.leafHash, 'hex').reverse());
|
|
516
|
+
}, new RegExp('Can not finalize taproot input #0. Signature for tapleaf script not found.'));
|
|
517
|
+
});
|
|
518
|
+
|
|
519
|
+
it(`fails if trying to finalzie non-taproot input`, () => {
|
|
520
|
+
const psbt = new Psbt();
|
|
521
|
+
psbt.addInput({
|
|
522
|
+
hash: '0000000000000000000000000000000000000000000000000000000000000000',
|
|
523
|
+
index: 0,
|
|
524
|
+
});
|
|
525
|
+
|
|
526
|
+
assert.throws(() => {
|
|
527
|
+
psbt.finalizeTaprootInput(0);
|
|
528
|
+
}, new RegExp('Cannot finalize input #0. Not Taproot.'));
|
|
529
|
+
});
|
|
530
|
+
});
|
|
531
|
+
|
|
532
|
+
describe('finalizeAllInputs', () => {
|
|
533
|
+
fixtures.finalizeAllInputs.forEach((f) => {
|
|
534
|
+
it(`Finalizes inputs of type "${f.type}"`, () => {
|
|
535
|
+
const psbt = Psbt.fromBase64(f.psbt);
|
|
536
|
+
|
|
537
|
+
psbt.finalizeAllInputs();
|
|
538
|
+
|
|
539
|
+
assert.strictEqual(psbt.toBase64(), f.result);
|
|
540
|
+
});
|
|
541
|
+
});
|
|
542
|
+
it('fails if no script found', () => {
|
|
543
|
+
const psbt = new Psbt();
|
|
544
|
+
psbt.addInput({
|
|
545
|
+
hash: '0000000000000000000000000000000000000000000000000000000000000000',
|
|
546
|
+
index: 0,
|
|
547
|
+
});
|
|
548
|
+
assert.throws(() => {
|
|
549
|
+
psbt.finalizeAllInputs();
|
|
550
|
+
}, new RegExp('No script found for input #0'));
|
|
551
|
+
psbt.updateInput(0, {
|
|
552
|
+
witnessUtxo: {
|
|
553
|
+
script: Buffer.from('0014d85c2b71d0060b09c9886aeb815e50991dda124d', 'hex'),
|
|
554
|
+
value: 2e5,
|
|
555
|
+
},
|
|
556
|
+
});
|
|
557
|
+
assert.throws(() => {
|
|
558
|
+
psbt.finalizeAllInputs();
|
|
559
|
+
}, new RegExp('Can not finalize input #0'));
|
|
560
|
+
});
|
|
561
|
+
});
|
|
562
|
+
|
|
563
|
+
describe('addInput', () => {
|
|
564
|
+
fixtures.addInput.checks.forEach((f) => {
|
|
565
|
+
it(f.description, () => {
|
|
566
|
+
const psbt = new Psbt();
|
|
567
|
+
|
|
568
|
+
if (f.exception) {
|
|
569
|
+
assert.throws(() => {
|
|
570
|
+
psbt.addInput(f.inputData as any);
|
|
571
|
+
}, new RegExp(f.exception));
|
|
572
|
+
assert.throws(() => {
|
|
573
|
+
psbt.addInputs([f.inputData as any]);
|
|
574
|
+
}, new RegExp(f.exception));
|
|
575
|
+
} else {
|
|
576
|
+
assert.doesNotThrow(() => {
|
|
577
|
+
psbt.addInputs([f.inputData as any]);
|
|
578
|
+
if (f.equals) {
|
|
579
|
+
assert.strictEqual(psbt.toBase64(), f.equals);
|
|
580
|
+
}
|
|
581
|
+
});
|
|
582
|
+
assert.throws(() => {
|
|
583
|
+
psbt.addInput(f.inputData as any);
|
|
584
|
+
}, new RegExp('Duplicate input detected.'));
|
|
585
|
+
}
|
|
586
|
+
});
|
|
587
|
+
});
|
|
588
|
+
});
|
|
589
|
+
|
|
590
|
+
describe('updateInput', () => {
|
|
591
|
+
fixtures.updateInput.checks.forEach((f) => {
|
|
592
|
+
it(f.description, () => {
|
|
593
|
+
const psbt = Psbt.fromBase64(f.psbt);
|
|
594
|
+
|
|
595
|
+
if (f.exception) {
|
|
596
|
+
assert.throws(() => {
|
|
597
|
+
psbt.updateInput(f.index, f.inputData as any);
|
|
598
|
+
}, new RegExp(f.exception));
|
|
599
|
+
}
|
|
600
|
+
});
|
|
601
|
+
});
|
|
602
|
+
});
|
|
603
|
+
|
|
604
|
+
describe('addOutput', () => {
|
|
605
|
+
fixtures.addOutput.checks.forEach((f) => {
|
|
606
|
+
it(f.description, () => {
|
|
607
|
+
if (f.isTaproot) initEccLib(ecc);
|
|
608
|
+
const psbt = f.psbt ? Psbt.fromBase64(f.psbt) : new Psbt();
|
|
609
|
+
|
|
610
|
+
if (f.exception) {
|
|
611
|
+
assert.throws(() => {
|
|
612
|
+
psbt.addOutput(f.outputData as any);
|
|
613
|
+
}, new RegExp(f.exception));
|
|
614
|
+
assert.throws(() => {
|
|
615
|
+
psbt.addOutputs([f.outputData as any]);
|
|
616
|
+
}, new RegExp(f.exception));
|
|
617
|
+
} else {
|
|
618
|
+
assert.doesNotThrow(() => {
|
|
619
|
+
psbt.addOutput(f.outputData as any);
|
|
620
|
+
});
|
|
621
|
+
if (f.result) {
|
|
622
|
+
assert.strictEqual(psbt.toBase64(), f.result);
|
|
623
|
+
}
|
|
624
|
+
assert.doesNotThrow(() => {
|
|
625
|
+
psbt.addOutputs([f.outputData as any]);
|
|
626
|
+
});
|
|
627
|
+
}
|
|
628
|
+
});
|
|
629
|
+
});
|
|
630
|
+
});
|
|
631
|
+
|
|
632
|
+
describe('setVersion', () => {
|
|
633
|
+
it('Sets the version value of the unsigned transaction', () => {
|
|
634
|
+
const psbt = new Psbt();
|
|
635
|
+
|
|
636
|
+
assert.strictEqual(psbt.extractTransaction().version, 2);
|
|
637
|
+
psbt.setVersion(1);
|
|
638
|
+
assert.strictEqual(psbt.extractTransaction().version, 1);
|
|
639
|
+
});
|
|
640
|
+
});
|
|
641
|
+
|
|
642
|
+
describe('setLocktime', () => {
|
|
643
|
+
it('Sets the nLockTime value of the unsigned transaction', () => {
|
|
644
|
+
const psbt = new Psbt();
|
|
645
|
+
|
|
646
|
+
assert.strictEqual(psbt.extractTransaction().locktime, 0);
|
|
647
|
+
psbt.setLocktime(1);
|
|
648
|
+
assert.strictEqual(psbt.extractTransaction().locktime, 1);
|
|
649
|
+
});
|
|
650
|
+
});
|
|
651
|
+
|
|
652
|
+
describe('setInputSequence', () => {
|
|
653
|
+
it('Sets the sequence number for a given input', () => {
|
|
654
|
+
const psbt = new Psbt();
|
|
655
|
+
psbt.addInput({
|
|
656
|
+
hash: '0000000000000000000000000000000000000000000000000000000000000000',
|
|
657
|
+
index: 0,
|
|
658
|
+
});
|
|
659
|
+
|
|
660
|
+
assert.strictEqual(psbt.inputCount, 1);
|
|
661
|
+
assert.strictEqual(psbt.txInputs[0].sequence, 0xffffffff);
|
|
662
|
+
psbt.setInputSequence(0, 0);
|
|
663
|
+
assert.strictEqual(psbt.txInputs[0].sequence, 0);
|
|
664
|
+
});
|
|
665
|
+
|
|
666
|
+
it('throws if input index is too high', () => {
|
|
667
|
+
const psbt = new Psbt();
|
|
668
|
+
psbt.addInput({
|
|
669
|
+
hash: '0000000000000000000000000000000000000000000000000000000000000000',
|
|
670
|
+
index: 0,
|
|
671
|
+
});
|
|
672
|
+
|
|
673
|
+
assert.throws(() => {
|
|
674
|
+
psbt.setInputSequence(1, 0);
|
|
675
|
+
}, new RegExp('Input index too high'));
|
|
676
|
+
});
|
|
677
|
+
});
|
|
678
|
+
|
|
679
|
+
describe('getInputType', () => {
|
|
680
|
+
const key = ECPair.makeRandom();
|
|
681
|
+
const { publicKey } = key;
|
|
682
|
+
const p2wpkhPub = (pubkey: Buffer): Buffer =>
|
|
683
|
+
payments.p2wpkh({
|
|
684
|
+
pubkey,
|
|
685
|
+
}).output!;
|
|
686
|
+
const p2pkhPub = (pubkey: Buffer): Buffer =>
|
|
687
|
+
payments.p2pkh({
|
|
688
|
+
pubkey,
|
|
689
|
+
}).output!;
|
|
690
|
+
const p2shOut = (output: Buffer): Buffer =>
|
|
691
|
+
payments.p2sh({
|
|
692
|
+
redeem: { output },
|
|
693
|
+
}).output!;
|
|
694
|
+
const p2wshOut = (output: Buffer): Buffer =>
|
|
695
|
+
payments.p2wsh({
|
|
696
|
+
redeem: { output },
|
|
697
|
+
}).output!;
|
|
698
|
+
const p2shp2wshOut = (output: Buffer): Buffer => p2shOut(p2wshOut(output));
|
|
699
|
+
const noOuter = (output: Buffer): Buffer => output;
|
|
700
|
+
|
|
701
|
+
function getInputTypeTest({
|
|
702
|
+
innerScript,
|
|
703
|
+
outerScript,
|
|
704
|
+
redeemGetter,
|
|
705
|
+
witnessGetter,
|
|
706
|
+
expectedType,
|
|
707
|
+
finalize,
|
|
708
|
+
}: any): void {
|
|
709
|
+
const psbt = new Psbt();
|
|
710
|
+
psbt.addInput({
|
|
711
|
+
hash: '0000000000000000000000000000000000000000000000000000000000000000',
|
|
712
|
+
index: 0,
|
|
713
|
+
witnessUtxo: {
|
|
714
|
+
script: outerScript(innerScript(publicKey)),
|
|
715
|
+
value: 2e3,
|
|
716
|
+
},
|
|
717
|
+
...(redeemGetter ? { redeemScript: redeemGetter(publicKey) } : {}),
|
|
718
|
+
...(witnessGetter ? { witnessScript: witnessGetter(publicKey) } : {}),
|
|
719
|
+
}).addOutput({
|
|
720
|
+
script: Buffer.from('0014d85c2b71d0060b09c9886aeb815e50991dda124d'),
|
|
721
|
+
value: 1800,
|
|
722
|
+
});
|
|
723
|
+
if (finalize) psbt.signInput(0, key).finalizeInput(0);
|
|
724
|
+
const type = psbt.getInputType(0);
|
|
725
|
+
assert.strictEqual(type, expectedType, 'incorrect input type');
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
[
|
|
729
|
+
{
|
|
730
|
+
innerScript: p2pkhPub,
|
|
731
|
+
outerScript: noOuter,
|
|
732
|
+
redeemGetter: null,
|
|
733
|
+
witnessGetter: null,
|
|
734
|
+
expectedType: 'pubkeyhash',
|
|
735
|
+
},
|
|
736
|
+
{
|
|
737
|
+
innerScript: p2wpkhPub,
|
|
738
|
+
outerScript: noOuter,
|
|
739
|
+
redeemGetter: null,
|
|
740
|
+
witnessGetter: null,
|
|
741
|
+
expectedType: 'witnesspubkeyhash',
|
|
742
|
+
},
|
|
743
|
+
{
|
|
744
|
+
innerScript: p2pkhPub,
|
|
745
|
+
outerScript: p2shOut,
|
|
746
|
+
redeemGetter: p2pkhPub,
|
|
747
|
+
witnessGetter: null,
|
|
748
|
+
expectedType: 'p2sh-pubkeyhash',
|
|
749
|
+
},
|
|
750
|
+
{
|
|
751
|
+
innerScript: p2wpkhPub,
|
|
752
|
+
outerScript: p2shOut,
|
|
753
|
+
redeemGetter: p2wpkhPub,
|
|
754
|
+
witnessGetter: null,
|
|
755
|
+
expectedType: 'p2sh-witnesspubkeyhash',
|
|
756
|
+
finalize: true,
|
|
757
|
+
},
|
|
758
|
+
{
|
|
759
|
+
innerScript: p2pkhPub,
|
|
760
|
+
outerScript: p2wshOut,
|
|
761
|
+
redeemGetter: null,
|
|
762
|
+
witnessGetter: p2pkhPub,
|
|
763
|
+
expectedType: 'p2wsh-pubkeyhash',
|
|
764
|
+
finalize: true,
|
|
765
|
+
},
|
|
766
|
+
{
|
|
767
|
+
innerScript: p2pkhPub,
|
|
768
|
+
outerScript: p2shp2wshOut,
|
|
769
|
+
redeemGetter: (pk: Buffer): Buffer => p2wshOut(p2pkhPub(pk)),
|
|
770
|
+
witnessGetter: p2pkhPub,
|
|
771
|
+
expectedType: 'p2sh-p2wsh-pubkeyhash',
|
|
772
|
+
},
|
|
773
|
+
].forEach(getInputTypeTest);
|
|
774
|
+
});
|
|
775
|
+
|
|
776
|
+
describe('inputHasHDKey', () => {
|
|
777
|
+
it('should return true if HD key is present', () => {
|
|
778
|
+
const root = bip32.fromSeed(crypto.randomBytes(32));
|
|
779
|
+
const root2 = bip32.fromSeed(crypto.randomBytes(32));
|
|
780
|
+
const path = "m/0'/0";
|
|
781
|
+
const psbt = new Psbt();
|
|
782
|
+
psbt.addInput({
|
|
783
|
+
hash: '0000000000000000000000000000000000000000000000000000000000000000',
|
|
784
|
+
index: 0,
|
|
785
|
+
bip32Derivation: [
|
|
786
|
+
{
|
|
787
|
+
masterFingerprint: root.fingerprint,
|
|
788
|
+
path,
|
|
789
|
+
pubkey: root.derivePath(path).publicKey,
|
|
790
|
+
},
|
|
791
|
+
],
|
|
792
|
+
});
|
|
793
|
+
assert.strictEqual(psbt.inputHasHDKey(0, root), true);
|
|
794
|
+
assert.strictEqual(psbt.inputHasHDKey(0, root2), false);
|
|
795
|
+
});
|
|
796
|
+
});
|
|
797
|
+
|
|
798
|
+
describe('inputHasPubkey', () => {
|
|
799
|
+
it('should throw', () => {
|
|
800
|
+
const psbt = new Psbt();
|
|
801
|
+
psbt.addInput({
|
|
802
|
+
hash: '0000000000000000000000000000000000000000000000000000000000000000',
|
|
803
|
+
index: 0,
|
|
804
|
+
});
|
|
805
|
+
|
|
806
|
+
assert.throws(() => {
|
|
807
|
+
psbt.inputHasPubkey(0, Buffer.from([]));
|
|
808
|
+
}, new RegExp("Can't find pubkey in input without Utxo data"));
|
|
809
|
+
|
|
810
|
+
psbt.updateInput(0, {
|
|
811
|
+
witnessUtxo: {
|
|
812
|
+
value: 1337,
|
|
813
|
+
script: payments.p2sh({
|
|
814
|
+
redeem: { output: Buffer.from([0x51]) },
|
|
815
|
+
}).output!,
|
|
816
|
+
},
|
|
817
|
+
});
|
|
818
|
+
|
|
819
|
+
assert.throws(() => {
|
|
820
|
+
psbt.inputHasPubkey(0, Buffer.from([]));
|
|
821
|
+
}, new RegExp('scriptPubkey is P2SH but redeemScript missing'));
|
|
822
|
+
|
|
823
|
+
delete psbt.data.inputs[0].witnessUtxo;
|
|
824
|
+
|
|
825
|
+
psbt.updateInput(0, {
|
|
826
|
+
witnessUtxo: {
|
|
827
|
+
value: 1337,
|
|
828
|
+
script: payments.p2wsh({
|
|
829
|
+
redeem: { output: Buffer.from([0x51]) },
|
|
830
|
+
}).output!,
|
|
831
|
+
},
|
|
832
|
+
});
|
|
833
|
+
|
|
834
|
+
assert.throws(() => {
|
|
835
|
+
psbt.inputHasPubkey(0, Buffer.from([]));
|
|
836
|
+
}, new RegExp('scriptPubkey or redeemScript is P2WSH but witnessScript missing'));
|
|
837
|
+
|
|
838
|
+
delete psbt.data.inputs[0].witnessUtxo;
|
|
839
|
+
|
|
840
|
+
psbt.updateInput(0, {
|
|
841
|
+
witnessUtxo: {
|
|
842
|
+
value: 1337,
|
|
843
|
+
script: payments.p2sh({
|
|
844
|
+
redeem: payments.p2wsh({
|
|
845
|
+
redeem: { output: Buffer.from([0x51]) },
|
|
846
|
+
}),
|
|
847
|
+
}).output!,
|
|
848
|
+
},
|
|
849
|
+
redeemScript: payments.p2wsh({
|
|
850
|
+
redeem: { output: Buffer.from([0x51]) },
|
|
851
|
+
}).output!,
|
|
852
|
+
});
|
|
853
|
+
|
|
854
|
+
assert.throws(() => {
|
|
855
|
+
psbt.inputHasPubkey(0, Buffer.from([]));
|
|
856
|
+
}, new RegExp('scriptPubkey or redeemScript is P2WSH but witnessScript missing'));
|
|
857
|
+
|
|
858
|
+
psbt.updateInput(0, {
|
|
859
|
+
witnessScript: Buffer.from([0x51]),
|
|
860
|
+
});
|
|
861
|
+
|
|
862
|
+
assert.doesNotThrow(() => {
|
|
863
|
+
psbt.inputHasPubkey(0, Buffer.from([0x51]));
|
|
864
|
+
});
|
|
865
|
+
});
|
|
866
|
+
});
|
|
867
|
+
|
|
868
|
+
describe('outputHasHDKey', () => {
|
|
869
|
+
it('should return true if HD key is present', () => {
|
|
870
|
+
const root = bip32.fromSeed(crypto.randomBytes(32));
|
|
871
|
+
const root2 = bip32.fromSeed(crypto.randomBytes(32));
|
|
872
|
+
const path = "m/0'/0";
|
|
873
|
+
const psbt = new Psbt();
|
|
874
|
+
psbt.addInput({
|
|
875
|
+
hash: '0000000000000000000000000000000000000000000000000000000000000000',
|
|
876
|
+
index: 0,
|
|
877
|
+
}).addOutput({
|
|
878
|
+
script: Buffer.from('0014000102030405060708090a0b0c0d0e0f00010203', 'hex'),
|
|
879
|
+
value: 2000,
|
|
880
|
+
bip32Derivation: [
|
|
881
|
+
{
|
|
882
|
+
masterFingerprint: root.fingerprint,
|
|
883
|
+
path,
|
|
884
|
+
pubkey: root.derivePath(path).publicKey,
|
|
885
|
+
},
|
|
886
|
+
],
|
|
887
|
+
});
|
|
888
|
+
assert.strictEqual(psbt.outputHasHDKey(0, root), true);
|
|
889
|
+
assert.strictEqual(psbt.outputHasHDKey(0, root2), false);
|
|
890
|
+
});
|
|
891
|
+
});
|
|
892
|
+
|
|
893
|
+
describe('outputHasPubkey', () => {
|
|
894
|
+
it('should throw', () => {
|
|
895
|
+
const psbt = new Psbt();
|
|
896
|
+
psbt.addInput({
|
|
897
|
+
hash: '0000000000000000000000000000000000000000000000000000000000000000',
|
|
898
|
+
index: 0,
|
|
899
|
+
}).addOutput({
|
|
900
|
+
script: payments.p2sh({
|
|
901
|
+
redeem: { output: Buffer.from([0x51]) },
|
|
902
|
+
}).output!,
|
|
903
|
+
value: 1337,
|
|
904
|
+
});
|
|
905
|
+
|
|
906
|
+
assert.throws(() => {
|
|
907
|
+
psbt.outputHasPubkey(0, Buffer.from([]));
|
|
908
|
+
}, new RegExp('scriptPubkey is P2SH but redeemScript missing'));
|
|
909
|
+
|
|
910
|
+
(psbt as any).__CACHE.__TX.outs[0].script = payments.p2wsh({
|
|
911
|
+
redeem: { output: Buffer.from([0x51]) },
|
|
912
|
+
}).output!;
|
|
913
|
+
|
|
914
|
+
assert.throws(() => {
|
|
915
|
+
psbt.outputHasPubkey(0, Buffer.from([]));
|
|
916
|
+
}, new RegExp('scriptPubkey or redeemScript is P2WSH but witnessScript missing'));
|
|
917
|
+
|
|
918
|
+
(psbt as any).__CACHE.__TX.outs[0].script = payments.p2sh({
|
|
919
|
+
redeem: payments.p2wsh({
|
|
920
|
+
redeem: { output: Buffer.from([0x51]) },
|
|
921
|
+
}),
|
|
922
|
+
}).output!;
|
|
923
|
+
|
|
924
|
+
psbt.updateOutput(0, {
|
|
925
|
+
redeemScript: payments.p2wsh({
|
|
926
|
+
redeem: { output: Buffer.from([0x51]) },
|
|
927
|
+
}).output!,
|
|
928
|
+
});
|
|
929
|
+
|
|
930
|
+
assert.throws(() => {
|
|
931
|
+
psbt.outputHasPubkey(0, Buffer.from([]));
|
|
932
|
+
}, new RegExp('scriptPubkey or redeemScript is P2WSH but witnessScript missing'));
|
|
933
|
+
|
|
934
|
+
delete psbt.data.outputs[0].redeemScript;
|
|
935
|
+
|
|
936
|
+
psbt.updateOutput(0, {
|
|
937
|
+
witnessScript: Buffer.from([0x51]),
|
|
938
|
+
});
|
|
939
|
+
|
|
940
|
+
assert.throws(() => {
|
|
941
|
+
psbt.outputHasPubkey(0, Buffer.from([]));
|
|
942
|
+
}, new RegExp('scriptPubkey is P2SH but redeemScript missing'));
|
|
943
|
+
|
|
944
|
+
psbt.updateOutput(0, {
|
|
945
|
+
redeemScript: payments.p2wsh({
|
|
946
|
+
redeem: { output: Buffer.from([0x51]) },
|
|
947
|
+
}).output!,
|
|
948
|
+
});
|
|
949
|
+
|
|
950
|
+
assert.doesNotThrow(() => {
|
|
951
|
+
psbt.outputHasPubkey(0, Buffer.from([0x51]));
|
|
952
|
+
});
|
|
953
|
+
});
|
|
954
|
+
});
|
|
955
|
+
|
|
956
|
+
describe('clone', () => {
|
|
957
|
+
it('Should clone a psbt exactly with no reference', () => {
|
|
958
|
+
const f = fixtures.clone;
|
|
959
|
+
const psbt = Psbt.fromBase64(f.psbt);
|
|
960
|
+
const notAClone = Object.assign(new Psbt(), psbt); // references still active
|
|
961
|
+
const clone = psbt.clone();
|
|
962
|
+
|
|
963
|
+
assert.strictEqual(psbt.validateSignaturesOfAllInputs(validator), true);
|
|
964
|
+
|
|
965
|
+
assert.strictEqual(clone.toBase64(), psbt.toBase64());
|
|
966
|
+
assert.strictEqual(clone.toBase64(), notAClone.toBase64());
|
|
967
|
+
assert.strictEqual(psbt.toBase64(), notAClone.toBase64());
|
|
968
|
+
(psbt as any).__CACHE.__TX.version |= 0xff0000;
|
|
969
|
+
assert.notStrictEqual(clone.toBase64(), psbt.toBase64());
|
|
970
|
+
assert.notStrictEqual(clone.toBase64(), notAClone.toBase64());
|
|
971
|
+
assert.strictEqual(psbt.toBase64(), notAClone.toBase64());
|
|
972
|
+
});
|
|
973
|
+
});
|
|
974
|
+
|
|
975
|
+
describe('setMaximumFeeRate', () => {
|
|
976
|
+
it('Sets the maximumFeeRate value', () => {
|
|
977
|
+
const psbt = new Psbt();
|
|
978
|
+
|
|
979
|
+
assert.strictEqual((psbt as any).opts.maximumFeeRate, 5000);
|
|
980
|
+
psbt.setMaximumFeeRate(6000);
|
|
981
|
+
assert.strictEqual((psbt as any).opts.maximumFeeRate, 6000);
|
|
982
|
+
});
|
|
983
|
+
});
|
|
984
|
+
|
|
985
|
+
describe('validateSignaturesOfInput', () => {
|
|
986
|
+
const f = fixtures.validateSignaturesOfInput;
|
|
987
|
+
it('Correctly validates a signature', () => {
|
|
988
|
+
const psbt = Psbt.fromBase64(f.psbt);
|
|
989
|
+
|
|
990
|
+
assert.strictEqual(psbt.validateSignaturesOfInput(f.index, validator), true);
|
|
991
|
+
assert.throws(() => {
|
|
992
|
+
psbt.validateSignaturesOfInput(f.nonExistantIndex, validator);
|
|
993
|
+
}, new RegExp('No signatures to validate'));
|
|
994
|
+
});
|
|
995
|
+
|
|
996
|
+
it('Correctly validates a signature against a pubkey', () => {
|
|
997
|
+
const psbt = Psbt.fromBase64(f.psbt);
|
|
998
|
+
assert.strictEqual(
|
|
999
|
+
psbt.validateSignaturesOfInput(f.index, validator, f.pubkey as any),
|
|
1000
|
+
true,
|
|
1001
|
+
);
|
|
1002
|
+
assert.throws(() => {
|
|
1003
|
+
psbt.validateSignaturesOfInput(f.index, validator, f.incorrectPubkey as any);
|
|
1004
|
+
}, new RegExp('No signatures for this pubkey'));
|
|
1005
|
+
});
|
|
1006
|
+
});
|
|
1007
|
+
|
|
1008
|
+
describe('validateSignaturesOfTapKeyInput', () => {
|
|
1009
|
+
const f = fixtures.validateSignaturesOfTapKeyInput;
|
|
1010
|
+
it('Correctly validates all signatures', () => {
|
|
1011
|
+
initEccLib(ecc);
|
|
1012
|
+
const psbt = Psbt.fromBase64(f.psbt);
|
|
1013
|
+
assert.strictEqual(psbt.validateSignaturesOfInput(f.index, schnorrValidator), true);
|
|
1014
|
+
});
|
|
1015
|
+
|
|
1016
|
+
it('Correctly validates a signature against a pubkey', () => {
|
|
1017
|
+
initEccLib(ecc);
|
|
1018
|
+
const psbt = Psbt.fromBase64(f.psbt);
|
|
1019
|
+
assert.strictEqual(
|
|
1020
|
+
psbt.validateSignaturesOfInput(f.index, schnorrValidator, f.pubkey as any),
|
|
1021
|
+
true,
|
|
1022
|
+
);
|
|
1023
|
+
assert.throws(() => {
|
|
1024
|
+
psbt.validateSignaturesOfInput(f.index, schnorrValidator, f.incorrectPubkey as any);
|
|
1025
|
+
}, new RegExp('No signatures for this pubkey'));
|
|
1026
|
+
});
|
|
1027
|
+
});
|
|
1028
|
+
|
|
1029
|
+
describe('validateSignaturesOfTapScriptInput', () => {
|
|
1030
|
+
const f = fixtures.validateSignaturesOfTapScriptInput;
|
|
1031
|
+
it('Correctly validates all signatures', () => {
|
|
1032
|
+
initEccLib(ecc);
|
|
1033
|
+
const psbt = Psbt.fromBase64(f.psbt);
|
|
1034
|
+
assert.strictEqual(psbt.validateSignaturesOfInput(f.index, schnorrValidator), true);
|
|
1035
|
+
});
|
|
1036
|
+
|
|
1037
|
+
it('Correctly validates a signature against a pubkey', () => {
|
|
1038
|
+
initEccLib(ecc);
|
|
1039
|
+
const psbt = Psbt.fromBase64(f.psbt);
|
|
1040
|
+
assert.strictEqual(
|
|
1041
|
+
psbt.validateSignaturesOfInput(f.index, schnorrValidator, f.pubkey as any),
|
|
1042
|
+
true,
|
|
1043
|
+
);
|
|
1044
|
+
assert.throws(() => {
|
|
1045
|
+
psbt.validateSignaturesOfInput(f.index, schnorrValidator, f.incorrectPubkey as any);
|
|
1046
|
+
}, new RegExp('No signatures for this pubkey'));
|
|
1047
|
+
});
|
|
1048
|
+
});
|
|
1049
|
+
|
|
1050
|
+
describe('tapTreeToList/tapTreeFromList', () => {
|
|
1051
|
+
it('Correctly converts a Taptree to a Tapleaf list and back', () => {
|
|
1052
|
+
taprootFixtures.valid
|
|
1053
|
+
.filter((f) => f.arguments.scriptTree)
|
|
1054
|
+
.map((f) => f.arguments.scriptTree)
|
|
1055
|
+
.forEach((scriptTree) => {
|
|
1056
|
+
const originalTree = convertScriptTree(scriptTree, LEAF_VERSION_TAPSCRIPT);
|
|
1057
|
+
const list = tapTreeToList(originalTree);
|
|
1058
|
+
const treeFromList = tapTreeFromList(list);
|
|
1059
|
+
|
|
1060
|
+
assert.deepStrictEqual(treeFromList, originalTree);
|
|
1061
|
+
});
|
|
1062
|
+
});
|
|
1063
|
+
|
|
1064
|
+
it('Throws if too many leaves on a given level', () => {
|
|
1065
|
+
const list = Array.from({ length: 5 }).map(() => ({
|
|
1066
|
+
depth: 2,
|
|
1067
|
+
leafVersion: LEAF_VERSION_TAPSCRIPT,
|
|
1068
|
+
script: Buffer.from([]),
|
|
1069
|
+
}));
|
|
1070
|
+
assert.throws(() => {
|
|
1071
|
+
tapTreeFromList(list);
|
|
1072
|
+
}, new RegExp('No room left to insert tapleaf in tree'));
|
|
1073
|
+
});
|
|
1074
|
+
|
|
1075
|
+
it('Throws if taptree depth is exceeded', () => {
|
|
1076
|
+
let tree: Taptree = [{ output: Buffer.from([]) }, { output: Buffer.from([]) }];
|
|
1077
|
+
Array.from({ length: 129 }).forEach(() => (tree = [tree, { output: Buffer.from([]) }]));
|
|
1078
|
+
assert.throws(() => {
|
|
1079
|
+
tapTreeToList(tree as Taptree);
|
|
1080
|
+
}, new RegExp('Max taptree depth exceeded.'));
|
|
1081
|
+
});
|
|
1082
|
+
|
|
1083
|
+
it('Throws if tapleaf depth is to high', () => {
|
|
1084
|
+
const list = [
|
|
1085
|
+
{
|
|
1086
|
+
depth: 129,
|
|
1087
|
+
leafVersion: LEAF_VERSION_TAPSCRIPT,
|
|
1088
|
+
script: Buffer.from([]),
|
|
1089
|
+
},
|
|
1090
|
+
];
|
|
1091
|
+
assert.throws(() => {
|
|
1092
|
+
tapTreeFromList(list);
|
|
1093
|
+
}, new RegExp('Max taptree depth exceeded.'));
|
|
1094
|
+
});
|
|
1095
|
+
|
|
1096
|
+
it('Throws if not a valid taptree structure', () => {
|
|
1097
|
+
const tree = Array.from({ length: 3 }).map(() => ({
|
|
1098
|
+
output: Buffer.from([]),
|
|
1099
|
+
}));
|
|
1100
|
+
|
|
1101
|
+
assert.throws(() => {
|
|
1102
|
+
tapTreeToList(tree as Taptree);
|
|
1103
|
+
}, new RegExp('Cannot convert taptree to tapleaf list. Expecting a tapree structure.'));
|
|
1104
|
+
});
|
|
1105
|
+
});
|
|
1106
|
+
|
|
1107
|
+
describe('getFeeRate', () => {
|
|
1108
|
+
it('Throws error if called before inputs are finalized', () => {
|
|
1109
|
+
const f = fixtures.getFeeRate;
|
|
1110
|
+
const psbt = Psbt.fromBase64(f.psbt);
|
|
1111
|
+
|
|
1112
|
+
assert.throws(() => {
|
|
1113
|
+
psbt.getFeeRate();
|
|
1114
|
+
}, new RegExp('PSBT must be finalized to calculate fee rate'));
|
|
1115
|
+
|
|
1116
|
+
psbt.finalizeAllInputs();
|
|
1117
|
+
|
|
1118
|
+
assert.strictEqual(psbt.getFeeRate(), f.fee);
|
|
1119
|
+
(psbt as any).__CACHE.__FEE_RATE = undefined;
|
|
1120
|
+
assert.strictEqual(psbt.getFeeRate(), f.fee);
|
|
1121
|
+
});
|
|
1122
|
+
});
|
|
1123
|
+
|
|
1124
|
+
describe('create 1-to-1 transaction', () => {
|
|
1125
|
+
const alice = ECPair.fromWIF('L2uPYXe17xSTqbCjZvL2DsyXPCbXspvcu5mHLDYUgzdUbZGSKrSr');
|
|
1126
|
+
const psbt = new Psbt();
|
|
1127
|
+
psbt.addInput({
|
|
1128
|
+
hash: '7d067b4a697a09d2c3cff7d4d9506c9955e93bff41bf82d439da7d030382bc3e',
|
|
1129
|
+
index: 0,
|
|
1130
|
+
nonWitnessUtxo: Buffer.from(
|
|
1131
|
+
'0200000001f9f34e95b9d5c8abcd20fc5bd4a825d1517be62f0f775e5f36da944d9' +
|
|
1132
|
+
'452e550000000006b483045022100c86e9a111afc90f64b4904bd609e9eaed80d48' +
|
|
1133
|
+
'ca17c162b1aca0a788ac3526f002207bb79b60d4fc6526329bf18a77135dc566020' +
|
|
1134
|
+
'9e761da46e1c2f1152ec013215801210211755115eabf846720f5cb18f248666fec' +
|
|
1135
|
+
'631e5e1e66009ce3710ceea5b1ad13ffffffff01905f0100000000001976a9148bb' +
|
|
1136
|
+
'c95d2709c71607c60ee3f097c1217482f518d88ac00000000',
|
|
1137
|
+
'hex',
|
|
1138
|
+
),
|
|
1139
|
+
sighashType: 1,
|
|
1140
|
+
});
|
|
1141
|
+
psbt.addOutput({
|
|
1142
|
+
address: '1KRMKfeZcmosxALVYESdPNez1AP1mEtywp',
|
|
1143
|
+
value: 80000,
|
|
1144
|
+
});
|
|
1145
|
+
psbt.signInput(0, alice);
|
|
1146
|
+
assert.throws(() => {
|
|
1147
|
+
psbt.setVersion(3);
|
|
1148
|
+
}, new RegExp('Can not modify transaction, signatures exist.'));
|
|
1149
|
+
psbt.validateSignaturesOfInput(0, validator);
|
|
1150
|
+
psbt.finalizeAllInputs();
|
|
1151
|
+
assert.throws(() => {
|
|
1152
|
+
psbt.setVersion(3);
|
|
1153
|
+
}, new RegExp('Can not modify transaction, signatures exist.'));
|
|
1154
|
+
assert.strictEqual(psbt.inputHasPubkey(0, alice.publicKey), true);
|
|
1155
|
+
assert.strictEqual(psbt.outputHasPubkey(0, alice.publicKey), false);
|
|
1156
|
+
assert.strictEqual(
|
|
1157
|
+
psbt.extractTransaction().toHex(),
|
|
1158
|
+
'02000000013ebc8203037dda39d482bf41ff3be955996c50d9d4f7cfc3d2097a694a7' +
|
|
1159
|
+
'b067d000000006b483045022100931b6db94aed25d5486884d83fc37160f37f3368c0' +
|
|
1160
|
+
'd7f48c757112abefec983802205fda64cff98c849577026eb2ce916a50ea70626a766' +
|
|
1161
|
+
'9f8596dd89b720a26b4d501210365db9da3f8a260078a7e8f8b708a1161468fb2323f' +
|
|
1162
|
+
'fda5ec16b261ec1056f455ffffffff0180380100000000001976a914ca0d36044e0dc' +
|
|
1163
|
+
'08a22724efa6f6a07b0ec4c79aa88ac00000000',
|
|
1164
|
+
);
|
|
1165
|
+
});
|
|
1166
|
+
|
|
1167
|
+
describe('Method return types', () => {
|
|
1168
|
+
it('fromBuffer returns Psbt type (not base class)', () => {
|
|
1169
|
+
const psbt = Psbt.fromBuffer(
|
|
1170
|
+
Buffer.from(
|
|
1171
|
+
'70736274ff01000a01000000000000000000000000',
|
|
1172
|
+
'hex', // cHNidP8BAAoBAAAAAAAAAAAAAAAA
|
|
1173
|
+
),
|
|
1174
|
+
);
|
|
1175
|
+
assert.strictEqual(psbt instanceof Psbt, true);
|
|
1176
|
+
assert.ok((psbt as any).__CACHE.__TX);
|
|
1177
|
+
});
|
|
1178
|
+
it('fromBase64 returns Psbt type (not base class)', () => {
|
|
1179
|
+
const psbt = Psbt.fromBase64('cHNidP8BAAoBAAAAAAAAAAAAAAAA');
|
|
1180
|
+
assert.strictEqual(psbt instanceof Psbt, true);
|
|
1181
|
+
assert.ok((psbt as any).__CACHE.__TX);
|
|
1182
|
+
});
|
|
1183
|
+
it('fromHex returns Psbt type (not base class)', () => {
|
|
1184
|
+
const psbt = Psbt.fromHex('70736274ff01000a01000000000000000000000000');
|
|
1185
|
+
assert.strictEqual(psbt instanceof Psbt, true);
|
|
1186
|
+
assert.ok((psbt as any).__CACHE.__TX);
|
|
1187
|
+
});
|
|
1188
|
+
});
|
|
1189
|
+
|
|
1190
|
+
describe('Cache', () => {
|
|
1191
|
+
it('non-witness UTXOs are cached', () => {
|
|
1192
|
+
const f = fixtures.cache.nonWitnessUtxo;
|
|
1193
|
+
const psbt = Psbt.fromBase64(f.psbt);
|
|
1194
|
+
const index = f.inputIndex;
|
|
1195
|
+
|
|
1196
|
+
// Cache is empty
|
|
1197
|
+
assert.strictEqual(
|
|
1198
|
+
(psbt as any).__CACHE.__NON_WITNESS_UTXO_BUF_CACHE[index],
|
|
1199
|
+
undefined,
|
|
1200
|
+
);
|
|
1201
|
+
|
|
1202
|
+
// Cache is populated
|
|
1203
|
+
psbt.updateInput(index, {
|
|
1204
|
+
nonWitnessUtxo: f.nonWitnessUtxo as any,
|
|
1205
|
+
});
|
|
1206
|
+
const value = psbt.data.inputs[index].nonWitnessUtxo;
|
|
1207
|
+
assert.ok((psbt as any).__CACHE.__NON_WITNESS_UTXO_BUF_CACHE[index].equals(value));
|
|
1208
|
+
assert.ok(
|
|
1209
|
+
(psbt as any).__CACHE.__NON_WITNESS_UTXO_BUF_CACHE[index].equals(f.nonWitnessUtxo),
|
|
1210
|
+
);
|
|
1211
|
+
|
|
1212
|
+
// Cache is rebuilt from internal transaction object when cleared
|
|
1213
|
+
psbt.data.inputs[index].nonWitnessUtxo = Buffer.from([1, 2, 3]);
|
|
1214
|
+
(psbt as any).__CACHE.__NON_WITNESS_UTXO_BUF_CACHE[index] = undefined;
|
|
1215
|
+
assert.ok((psbt as any).data.inputs[index].nonWitnessUtxo.equals(value));
|
|
1216
|
+
});
|
|
1217
|
+
});
|
|
1218
|
+
|
|
1219
|
+
describe('Transaction properties', () => {
|
|
1220
|
+
it('.version is exposed and is settable', () => {
|
|
1221
|
+
const psbt = new Psbt();
|
|
1222
|
+
|
|
1223
|
+
assert.strictEqual(psbt.version, 2);
|
|
1224
|
+
assert.strictEqual(psbt.version, (psbt as any).__CACHE.__TX.version);
|
|
1225
|
+
|
|
1226
|
+
psbt.version = 1;
|
|
1227
|
+
assert.strictEqual(psbt.version, 1);
|
|
1228
|
+
assert.strictEqual(psbt.version, (psbt as any).__CACHE.__TX.version);
|
|
1229
|
+
});
|
|
1230
|
+
|
|
1231
|
+
it('.locktime is exposed and is settable', () => {
|
|
1232
|
+
const psbt = new Psbt();
|
|
1233
|
+
|
|
1234
|
+
assert.strictEqual(psbt.locktime, 0);
|
|
1235
|
+
assert.strictEqual(psbt.locktime, (psbt as any).__CACHE.__TX.locktime);
|
|
1236
|
+
|
|
1237
|
+
psbt.locktime = 123;
|
|
1238
|
+
assert.strictEqual(psbt.locktime, 123);
|
|
1239
|
+
assert.strictEqual(psbt.locktime, (psbt as any).__CACHE.__TX.locktime);
|
|
1240
|
+
});
|
|
1241
|
+
|
|
1242
|
+
it('.txInputs is exposed as a readonly clone', () => {
|
|
1243
|
+
const psbt = new Psbt();
|
|
1244
|
+
const hash = Buffer.alloc(32);
|
|
1245
|
+
const index = 0;
|
|
1246
|
+
psbt.addInput({ hash, index });
|
|
1247
|
+
|
|
1248
|
+
const input = psbt.txInputs[0];
|
|
1249
|
+
const internalInput = (psbt as any).__CACHE.__TX.ins[0];
|
|
1250
|
+
|
|
1251
|
+
assert.ok(input.hash.equals(internalInput.hash));
|
|
1252
|
+
assert.strictEqual(input.index, internalInput.index);
|
|
1253
|
+
assert.strictEqual(input.sequence, internalInput.sequence);
|
|
1254
|
+
|
|
1255
|
+
input.hash[0] = 123;
|
|
1256
|
+
input.index = 123;
|
|
1257
|
+
input.sequence = 123;
|
|
1258
|
+
|
|
1259
|
+
assert.ok(!input.hash.equals(internalInput.hash));
|
|
1260
|
+
assert.notEqual(input.index, internalInput.index);
|
|
1261
|
+
assert.notEqual(input.sequence, internalInput.sequence);
|
|
1262
|
+
});
|
|
1263
|
+
|
|
1264
|
+
it('.txOutputs is exposed as a readonly clone', () => {
|
|
1265
|
+
const psbt = new Psbt();
|
|
1266
|
+
const address = '1LukeQU5jwebXbMLDVydeH4vFSobRV9rkj';
|
|
1267
|
+
const value = 100000;
|
|
1268
|
+
psbt.addOutput({ address, value });
|
|
1269
|
+
|
|
1270
|
+
const output = psbt.txOutputs[0];
|
|
1271
|
+
const internalInput = (psbt as any).__CACHE.__TX.outs[0];
|
|
1272
|
+
|
|
1273
|
+
assert.strictEqual(output.address, address);
|
|
1274
|
+
|
|
1275
|
+
assert.ok(output.script.equals(internalInput.script));
|
|
1276
|
+
assert.strictEqual(output.value, internalInput.value);
|
|
1277
|
+
|
|
1278
|
+
output.script[0] = 123;
|
|
1279
|
+
output.value = 123;
|
|
1280
|
+
|
|
1281
|
+
assert.ok(!output.script.equals(internalInput.script));
|
|
1282
|
+
assert.notEqual(output.value, internalInput.value);
|
|
1283
|
+
});
|
|
1284
|
+
});
|
|
1285
|
+
});
|