@btc-vision/bitcoin 6.5.5 → 7.0.0-alpha.0
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/AUDIT/README.md +9 -0
- package/HOW_TO_WRITE_GOOD_CODE.md +2436 -0
- package/SECURITY.md +27 -0
- package/benchmark/psbt-2000-inputs.bench.ts +178 -0
- package/benchmark/signing.bench.ts +147 -0
- package/browser/address.d.ts +56 -9
- package/browser/address.d.ts.map +1 -0
- package/browser/bech32utils.d.ts +9 -1
- package/browser/bech32utils.d.ts.map +1 -0
- package/browser/bip66.d.ts +11 -6
- package/browser/bip66.d.ts.map +1 -0
- package/browser/block.d.ts +117 -11
- package/browser/block.d.ts.map +1 -0
- package/browser/branded.d.ts +20 -0
- package/browser/branded.d.ts.map +1 -0
- package/browser/crypto/crypto.d.ts +1 -0
- package/browser/crypto/crypto.d.ts.map +1 -0
- package/browser/crypto.d.ts +46 -7
- package/browser/crypto.d.ts.map +1 -0
- package/browser/ecc/context.d.ts +129 -0
- package/browser/ecc/context.d.ts.map +1 -0
- package/browser/ecc/index.d.ts +11 -0
- package/browser/ecc/index.d.ts.map +1 -0
- package/browser/ecc/types.d.ts +128 -0
- package/browser/ecc/types.d.ts.map +1 -0
- package/browser/ecpair.d.ts +99 -0
- package/browser/errors.d.ts +124 -0
- package/browser/errors.d.ts.map +1 -0
- package/browser/index.d.ts +32 -5
- package/browser/index.d.ts.map +1 -0
- package/browser/index.js +12482 -101
- package/browser/io/BinaryReader.d.ts +276 -0
- package/browser/io/BinaryReader.d.ts.map +1 -0
- package/browser/io/BinaryWriter.d.ts +391 -0
- package/browser/io/BinaryWriter.d.ts.map +1 -0
- package/browser/io/MemoryPool.d.ts +220 -0
- package/browser/io/MemoryPool.d.ts.map +1 -0
- package/browser/io/base64.d.ts +13 -0
- package/browser/io/base64.d.ts.map +1 -0
- package/browser/io/hex.d.ts +67 -0
- package/browser/io/hex.d.ts.map +1 -0
- package/browser/io/index.d.ts +17 -0
- package/browser/io/index.d.ts.map +1 -0
- package/browser/io/utils.d.ts +199 -0
- package/browser/io/utils.d.ts.map +1 -0
- package/browser/merkle.d.ts +10 -1
- package/browser/merkle.d.ts.map +1 -0
- package/browser/networks.d.ts +70 -9
- package/browser/networks.d.ts.map +1 -0
- package/browser/opcodes.d.ts +1 -0
- package/browser/opcodes.d.ts.map +1 -0
- package/browser/payments/bip341.d.ts +35 -9
- package/browser/payments/bip341.d.ts.map +1 -0
- package/browser/payments/embed.d.ts +112 -1
- package/browser/payments/embed.d.ts.map +1 -0
- package/browser/payments/index.d.ts +17 -10
- package/browser/payments/index.d.ts.map +1 -0
- package/browser/payments/p2ms.d.ts +150 -0
- package/browser/payments/p2ms.d.ts.map +1 -0
- package/browser/payments/p2op.d.ts +150 -24
- package/browser/payments/p2op.d.ts.map +1 -0
- package/browser/payments/p2pk.d.ts +154 -1
- package/browser/payments/p2pk.d.ts.map +1 -0
- package/browser/payments/p2pkh.d.ts +176 -1
- package/browser/payments/p2pkh.d.ts.map +1 -0
- package/browser/payments/p2sh.d.ts +150 -1
- package/browser/payments/p2sh.d.ts.map +1 -0
- package/browser/payments/p2tr.d.ts +185 -1
- package/browser/payments/p2tr.d.ts.map +1 -0
- package/browser/payments/p2wpkh.d.ts +161 -1
- package/browser/payments/p2wpkh.d.ts.map +1 -0
- package/browser/payments/p2wsh.d.ts +146 -1
- package/browser/payments/p2wsh.d.ts.map +1 -0
- package/browser/payments/types.d.ts +94 -64
- package/browser/payments/types.d.ts.map +1 -0
- package/browser/psbt/bip371.d.ts +34 -8
- package/browser/psbt/bip371.d.ts.map +1 -0
- package/browser/psbt/psbtutils.d.ts +56 -16
- package/browser/psbt/psbtutils.d.ts.map +1 -0
- package/browser/psbt/types.d.ts +245 -0
- package/browser/psbt/types.d.ts.map +1 -0
- package/browser/psbt/utils.d.ts +64 -0
- package/browser/psbt/utils.d.ts.map +1 -0
- package/browser/psbt/validation.d.ts +84 -0
- package/browser/psbt/validation.d.ts.map +1 -0
- package/browser/psbt.d.ts +82 -118
- package/browser/psbt.d.ts.map +1 -0
- package/browser/pubkey.d.ts +27 -6
- package/browser/pubkey.d.ts.map +1 -0
- package/browser/push_data.d.ts +24 -2
- package/browser/push_data.d.ts.map +1 -0
- package/browser/script.d.ts +33 -8
- package/browser/script.d.ts.map +1 -0
- package/browser/script_number.d.ts +17 -0
- package/browser/script_number.d.ts.map +1 -0
- package/browser/script_signature.d.ts +23 -5
- package/browser/script_signature.d.ts.map +1 -0
- package/browser/transaction.d.ts +160 -18
- package/browser/transaction.d.ts.map +1 -0
- package/browser/types.d.ts +36 -38
- package/browser/types.d.ts.map +1 -0
- package/browser/workers/WorkerSigningPool.d.ts +143 -0
- package/browser/workers/WorkerSigningPool.d.ts.map +1 -0
- package/browser/workers/WorkerSigningPool.node.d.ts +116 -0
- package/browser/workers/WorkerSigningPool.node.d.ts.map +1 -0
- package/browser/workers/ecc-bundle.d.ts +25 -0
- package/browser/workers/ecc-bundle.d.ts.map +1 -0
- package/browser/workers/index.d.ts +91 -0
- package/browser/workers/index.d.ts.map +1 -0
- package/browser/workers/psbt-parallel.d.ts +88 -0
- package/browser/workers/psbt-parallel.d.ts.map +1 -0
- package/browser/workers/signing-worker.d.ts +37 -0
- package/browser/workers/signing-worker.d.ts.map +1 -0
- package/browser/workers/types.d.ts +365 -0
- package/browser/workers/types.d.ts.map +1 -0
- package/build/address.d.ts +57 -10
- package/build/address.d.ts.map +1 -0
- package/build/address.js +80 -24
- package/build/address.js.map +1 -0
- package/build/bech32utils.d.ts +9 -1
- package/build/bech32utils.d.ts.map +1 -0
- package/build/bech32utils.js +10 -2
- package/build/bech32utils.js.map +1 -0
- package/build/bip66.d.ts +11 -6
- package/build/bip66.d.ts.map +1 -0
- package/build/bip66.js +32 -3
- package/build/bip66.js.map +1 -0
- package/build/block.d.ts +117 -11
- package/build/block.d.ts.map +1 -0
- package/build/block.js +204 -72
- package/build/block.js.map +1 -0
- package/build/branded.d.ts +20 -0
- package/build/branded.d.ts.map +1 -0
- package/build/branded.js +7 -0
- package/build/branded.js.map +1 -0
- package/build/crypto/crypto.d.ts +1 -0
- package/build/crypto/crypto.d.ts.map +1 -0
- package/build/crypto/crypto.js +1 -0
- package/build/crypto/crypto.js.map +1 -0
- package/build/crypto.d.ts +46 -7
- package/build/crypto.d.ts.map +1 -0
- package/build/crypto.js +65 -20
- package/build/crypto.js.map +1 -0
- package/build/ecc/context.d.ts +135 -0
- package/build/ecc/context.d.ts.map +1 -0
- package/build/ecc/context.js +232 -0
- package/build/ecc/context.js.map +1 -0
- package/build/ecc/index.d.ts +11 -0
- package/build/ecc/index.d.ts.map +1 -0
- package/build/ecc/index.js +11 -0
- package/build/ecc/index.js.map +1 -0
- package/build/ecc/types.d.ts +134 -0
- package/build/ecc/types.d.ts.map +1 -0
- package/build/ecc/types.js +8 -0
- package/build/ecc/types.js.map +1 -0
- package/build/errors.d.ts +124 -0
- package/build/errors.d.ts.map +1 -0
- package/build/errors.js +155 -0
- package/build/errors.js.map +1 -0
- package/build/index.d.ts +32 -5
- package/build/index.d.ts.map +1 -0
- package/build/index.js +26 -3
- package/build/index.js.map +1 -0
- package/build/io/BinaryReader.d.ts +276 -0
- package/build/io/BinaryReader.d.ts.map +1 -0
- package/build/io/BinaryReader.js +425 -0
- package/build/io/BinaryReader.js.map +1 -0
- package/build/io/BinaryWriter.d.ts +391 -0
- package/build/io/BinaryWriter.d.ts.map +1 -0
- package/build/io/BinaryWriter.js +611 -0
- package/build/io/BinaryWriter.js.map +1 -0
- package/build/io/MemoryPool.d.ts +220 -0
- package/build/io/MemoryPool.d.ts.map +1 -0
- package/build/io/MemoryPool.js +309 -0
- package/build/io/MemoryPool.js.map +1 -0
- package/build/io/base64.d.ts +13 -0
- package/build/io/base64.d.ts.map +1 -0
- package/build/io/base64.js +20 -0
- package/build/io/base64.js.map +1 -0
- package/build/io/hex.d.ts +67 -0
- package/build/io/hex.d.ts.map +1 -0
- package/build/io/hex.js +138 -0
- package/build/io/hex.js.map +1 -0
- package/build/io/index.d.ts +17 -0
- package/build/io/index.d.ts.map +1 -0
- package/build/io/index.js +23 -0
- package/build/io/index.js.map +1 -0
- package/build/io/utils.d.ts +199 -0
- package/build/io/utils.d.ts.map +1 -0
- package/build/io/utils.js +271 -0
- package/build/io/utils.js.map +1 -0
- package/build/merkle.d.ts +10 -1
- package/build/merkle.d.ts.map +1 -0
- package/build/merkle.js +12 -1
- package/build/merkle.js.map +1 -0
- package/build/networks.d.ts +70 -9
- package/build/networks.d.ts.map +1 -0
- package/build/networks.js +90 -4
- package/build/networks.js.map +1 -0
- package/build/opcodes.d.ts +1 -0
- package/build/opcodes.d.ts.map +1 -0
- package/build/opcodes.js +1 -0
- package/build/opcodes.js.map +1 -0
- package/build/payments/bip341.d.ts +36 -9
- package/build/payments/bip341.d.ts.map +1 -0
- package/build/payments/bip341.js +35 -15
- package/build/payments/bip341.js.map +1 -0
- package/build/payments/embed.d.ts +120 -1
- package/build/payments/embed.d.ts.map +1 -0
- package/build/payments/embed.js +215 -34
- package/build/payments/embed.js.map +1 -0
- package/build/payments/index.d.ts +17 -10
- package/build/payments/index.d.ts.map +1 -0
- package/build/payments/index.js +20 -10
- package/build/payments/index.js.map +1 -0
- package/build/payments/p2ms.d.ts +159 -1
- package/build/payments/p2ms.d.ts.map +1 -0
- package/build/payments/p2ms.js +427 -108
- package/build/payments/p2ms.js.map +1 -0
- package/build/payments/p2op.d.ts +158 -24
- package/build/payments/p2op.d.ts.map +1 -0
- package/build/payments/p2op.js +379 -93
- package/build/payments/p2op.js.map +1 -0
- package/build/payments/p2pk.d.ts +162 -1
- package/build/payments/p2pk.d.ts.map +1 -0
- package/build/payments/p2pk.js +327 -58
- package/build/payments/p2pk.js.map +1 -0
- package/build/payments/p2pkh.d.ts +185 -1
- package/build/payments/p2pkh.d.ts.map +1 -0
- package/build/payments/p2pkh.js +467 -114
- package/build/payments/p2pkh.js.map +1 -0
- package/build/payments/p2sh.d.ts +159 -1
- package/build/payments/p2sh.d.ts.map +1 -0
- package/build/payments/p2sh.js +500 -152
- package/build/payments/p2sh.js.map +1 -0
- package/build/payments/p2tr.d.ts +193 -1
- package/build/payments/p2tr.d.ts.map +1 -0
- package/build/payments/p2tr.js +592 -174
- package/build/payments/p2tr.js.map +1 -0
- package/build/payments/p2wpkh.d.ts +170 -1
- package/build/payments/p2wpkh.d.ts.map +1 -0
- package/build/payments/p2wpkh.js +429 -104
- package/build/payments/p2wpkh.js.map +1 -0
- package/build/payments/p2wsh.d.ts +155 -1
- package/build/payments/p2wsh.d.ts.map +1 -0
- package/build/payments/p2wsh.js +466 -144
- package/build/payments/p2wsh.js.map +1 -0
- package/build/payments/types.d.ts +98 -64
- package/build/payments/types.d.ts.map +1 -0
- package/build/payments/types.js +17 -13
- package/build/payments/types.js.map +1 -0
- package/build/psbt/bip371.d.ts +35 -9
- package/build/psbt/bip371.d.ts.map +1 -0
- package/build/psbt/bip371.js +113 -28
- package/build/psbt/bip371.js.map +1 -0
- package/build/psbt/psbtutils.d.ts +56 -16
- package/build/psbt/psbtutils.d.ts.map +1 -0
- package/build/psbt/psbtutils.js +71 -16
- package/build/psbt/psbtutils.js.map +1 -0
- package/build/psbt/types.d.ts +249 -0
- package/build/psbt/types.d.ts.map +1 -0
- package/build/psbt/types.js +6 -0
- package/build/psbt/types.js.map +1 -0
- package/build/psbt/utils.d.ts +68 -0
- package/build/psbt/utils.d.ts.map +1 -0
- package/build/psbt/utils.js +171 -0
- package/build/psbt/utils.js.map +1 -0
- package/build/psbt/validation.d.ts +88 -0
- package/build/psbt/validation.d.ts.map +1 -0
- package/build/psbt/validation.js +149 -0
- package/build/psbt/validation.js.map +1 -0
- package/build/psbt.d.ts +84 -120
- package/build/psbt.d.ts.map +1 -0
- package/build/psbt.js +411 -412
- package/build/psbt.js.map +1 -0
- package/build/pubkey.d.ts +27 -6
- package/build/pubkey.d.ts.map +1 -0
- package/build/pubkey.js +37 -13
- package/build/pubkey.js.map +1 -0
- package/build/push_data.d.ts +24 -2
- package/build/push_data.d.ts.map +1 -0
- package/build/push_data.js +44 -12
- package/build/push_data.js.map +1 -0
- package/build/script.d.ts +33 -8
- package/build/script.d.ts.map +1 -0
- package/build/script.js +100 -36
- package/build/script.js.map +1 -0
- package/build/script_number.d.ts +17 -0
- package/build/script_number.d.ts.map +1 -0
- package/build/script_number.js +19 -0
- package/build/script_number.js.map +1 -0
- package/build/script_signature.d.ts +23 -5
- package/build/script_signature.d.ts.map +1 -0
- package/build/script_signature.js +48 -15
- package/build/script_signature.js.map +1 -0
- package/build/transaction.d.ts +160 -18
- package/build/transaction.d.ts.map +1 -0
- package/build/transaction.js +443 -176
- package/build/transaction.js.map +1 -0
- package/build/tsconfig.build.tsbuildinfo +1 -0
- package/build/types.d.ts +36 -38
- package/build/types.d.ts.map +1 -0
- package/build/types.js +175 -57
- package/build/types.js.map +1 -0
- package/build/workers/WorkerSigningPool.d.ts +174 -0
- package/build/workers/WorkerSigningPool.d.ts.map +1 -0
- package/build/workers/WorkerSigningPool.js +553 -0
- package/build/workers/WorkerSigningPool.js.map +1 -0
- package/build/workers/WorkerSigningPool.node.d.ts +124 -0
- package/build/workers/WorkerSigningPool.node.d.ts.map +1 -0
- package/build/workers/WorkerSigningPool.node.js +753 -0
- package/build/workers/WorkerSigningPool.node.js.map +1 -0
- package/build/workers/ecc-bundle.d.ts +25 -0
- package/build/workers/ecc-bundle.d.ts.map +1 -0
- package/build/workers/ecc-bundle.js +25 -0
- package/build/workers/ecc-bundle.js.map +1 -0
- package/build/workers/index.d.ts +91 -0
- package/build/workers/index.d.ts.map +1 -0
- package/build/workers/index.js +114 -0
- package/build/workers/index.js.map +1 -0
- package/build/workers/psbt-parallel.d.ts +117 -0
- package/build/workers/psbt-parallel.d.ts.map +1 -0
- package/build/workers/psbt-parallel.js +233 -0
- package/build/workers/psbt-parallel.js.map +1 -0
- package/build/workers/signing-worker.d.ts +37 -0
- package/build/workers/signing-worker.d.ts.map +1 -0
- package/build/workers/signing-worker.js +350 -0
- package/build/workers/signing-worker.js.map +1 -0
- package/build/workers/types.d.ts +365 -0
- package/build/workers/types.d.ts.map +1 -0
- package/build/workers/types.js +60 -0
- package/build/workers/types.js.map +1 -0
- package/package.json +83 -25
- package/scripts/bundle-ecc.ts +111 -0
- package/src/address.ts +81 -44
- package/src/bech32utils.ts +3 -3
- package/src/bip66.ts +34 -24
- package/src/block.ts +196 -84
- package/src/branded.ts +18 -0
- package/src/crypto.ts +64 -26
- package/src/ecc/context.ts +277 -0
- package/src/ecc/index.ts +14 -0
- package/src/ecc/types.ts +154 -0
- package/src/ecpair.d.ts +99 -0
- package/src/errors.ts +163 -0
- package/src/index.ts +113 -9
- package/src/io/BinaryReader.ts +461 -0
- package/src/io/BinaryWriter.ts +696 -0
- package/src/io/MemoryPool.ts +343 -0
- package/src/io/base64.ts +20 -0
- package/src/io/hex.ts +155 -0
- package/src/io/index.ts +41 -0
- package/src/io/utils.ts +283 -0
- package/src/merkle.ts +14 -9
- package/src/networks.ts +9 -9
- package/src/payments/bip341.ts +34 -33
- package/src/payments/embed.ts +244 -41
- package/src/payments/index.ts +12 -10
- package/src/payments/p2ms.ts +490 -118
- package/src/payments/p2op.ts +431 -133
- package/src/payments/p2pk.ts +370 -72
- package/src/payments/p2pkh.ts +524 -130
- package/src/payments/p2sh.ts +572 -172
- package/src/payments/p2tr.ts +686 -194
- package/src/payments/p2wpkh.ts +484 -107
- package/src/payments/p2wsh.ts +526 -164
- package/src/payments/types.ts +80 -66
- package/src/psbt/bip371.ts +68 -51
- package/src/psbt/psbtutils.ts +39 -40
- package/src/psbt/types.ts +331 -0
- package/src/psbt/utils.ts +188 -0
- package/src/psbt/validation.ts +192 -0
- package/src/psbt.ts +566 -809
- package/src/pubkey.ts +24 -25
- package/src/push_data.ts +18 -16
- package/src/script.ts +82 -64
- package/src/script_number.ts +6 -6
- package/src/script_signature.ts +33 -36
- package/src/transaction.ts +458 -238
- package/src/types.ts +231 -100
- package/src/workers/WorkerSigningPool.node.ts +887 -0
- package/src/workers/WorkerSigningPool.ts +670 -0
- package/src/workers/ecc-bundle.ts +26 -0
- package/src/workers/index.ts +165 -0
- package/src/workers/psbt-parallel.ts +332 -0
- package/src/workers/signing-worker.ts +353 -0
- package/src/workers/types.ts +413 -0
- package/test/address.spec.ts +9 -6
- package/test/bitcoin.core.spec.ts +16 -17
- package/test/block.spec.ts +8 -7
- package/test/bufferutils.spec.ts +228 -214
- package/test/crypto.spec.ts +19 -11
- package/test/fixtures/p2pk.json +0 -8
- package/test/fixtures/p2pkh.json +1 -1
- package/test/fixtures/p2sh.json +1 -1
- package/test/fixtures/script.json +1 -1
- package/test/fixtures/transaction.json +2 -2
- package/test/integration/_regtest.ts +25 -0
- package/test/integration/addresses.spec.ts +4 -3
- package/test/integration/bip32.spec.ts +2 -1
- package/test/integration/blocks.spec.ts +1 -1
- package/test/integration/cltv.spec.ts +18 -16
- package/test/integration/csv.spec.ts +37 -64
- package/test/integration/payments.spec.ts +5 -3
- package/test/integration/taproot.spec.ts +76 -83
- package/test/integration/transactions.spec.ts +38 -35
- package/test/payments.spec.ts +35 -13
- package/test/payments.utils.ts +17 -16
- package/test/psbt.spec.ts +111 -100
- package/test/script.spec.ts +11 -10
- package/test/script_signature.spec.ts +9 -11
- package/test/taproot-cache.spec.ts +694 -0
- package/test/transaction.spec.ts +32 -40
- package/test/types.spec.ts +74 -29
- package/test/workers-pool.spec.ts +963 -0
- package/test/workers-signing.spec.ts +635 -0
- package/test/workers.spec.ts +1390 -0
- package/tsconfig.base.json +34 -18
- package/tsconfig.browser.json +15 -0
- package/tsconfig.build.json +5 -0
- package/tsconfig.json +5 -14
- package/vite.config.browser.ts +3 -42
- package/vitest.config.integration.ts +11 -0
- package/browser/bufferutils.d.ts +0 -34
- package/browser/chunks/crypto-BhCpKpek.js +0 -2033
- package/browser/chunks/payments-yjA0Evsv.js +0 -1089
- package/browser/chunks/psbt-URK2hBFc.js +0 -4039
- package/browser/chunks/script-DyPItFEl.js +0 -318
- package/browser/chunks/transaction-C_UbhMGn.js +0 -432
- package/browser/chunks/utils-DNZi-T5W.js +0 -761
- package/browser/ecc_lib.d.ts +0 -3
- package/browser/hooks/AdvancedSignatureManager.d.ts +0 -16
- package/browser/hooks/HookedSigner.d.ts +0 -4
- package/browser/hooks/SignatureManager.d.ts +0 -13
- package/browser/payments/lazy.d.ts +0 -2
- package/browser/typeforce.d.ts +0 -38
- package/build/bufferutils.d.ts +0 -34
- package/build/bufferutils.js +0 -141
- package/build/ecc_lib.d.ts +0 -3
- package/build/ecc_lib.js +0 -61
- package/build/hooks/AdvancedSignatureManager.d.ts +0 -16
- package/build/hooks/AdvancedSignatureManager.js +0 -52
- package/build/hooks/HookedSigner.d.ts +0 -4
- package/build/hooks/HookedSigner.js +0 -64
- package/build/hooks/SignatureManager.d.ts +0 -13
- package/build/hooks/SignatureManager.js +0 -45
- package/build/payments/lazy.d.ts +0 -2
- package/build/payments/lazy.js +0 -28
- package/build/tsconfig.tsbuildinfo +0 -1
- package/src/bufferutils.ts +0 -188
- package/src/ecc_lib.ts +0 -94
- package/src/hooks/AdvancedSignatureManager.ts +0 -104
- package/src/hooks/HookedSigner.ts +0 -108
- package/src/hooks/SignatureManager.ts +0 -84
- package/src/payments/lazy.ts +0 -28
- package/src/typeforce.d.ts +0 -38
- package/tsconfig.webpack.json +0 -18
package/test/crypto.spec.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { crypto as bcrypto } from '../src/index.js';
|
|
|
4
4
|
import type { TaggedHashPrefix } from '../src/crypto.js';
|
|
5
5
|
import fixtures from './fixtures/crypto.json' with { type: 'json' };
|
|
6
6
|
import { sha256, TAGGED_HASH_PREFIXES, TAGS } from '../src/crypto.js';
|
|
7
|
+
import { toHex, fromHex, fromUtf8, concat, equals } from '../src/io/index.js';
|
|
7
8
|
|
|
8
9
|
describe('crypto', () => {
|
|
9
10
|
['hash160', 'hash256', 'ripemd160', 'sha1', 'sha256'].forEach((algorithm) => {
|
|
@@ -13,8 +14,8 @@ describe('crypto', () => {
|
|
|
13
14
|
const expected = (f as any)[algorithm];
|
|
14
15
|
|
|
15
16
|
it('returns ' + expected + ' for ' + f.hex, () => {
|
|
16
|
-
const data =
|
|
17
|
-
const actual = fn(data)
|
|
17
|
+
const data = fromHex(f.hex);
|
|
18
|
+
const actual = toHex(fn(data));
|
|
18
19
|
|
|
19
20
|
assert.strictEqual(actual, expected);
|
|
20
21
|
});
|
|
@@ -24,11 +25,11 @@ describe('crypto', () => {
|
|
|
24
25
|
|
|
25
26
|
describe('taggedHash', () => {
|
|
26
27
|
fixtures.taggedHash.forEach((f) => {
|
|
27
|
-
const bytes =
|
|
28
|
-
const expected =
|
|
28
|
+
const bytes = fromHex(f.hex);
|
|
29
|
+
const expected = fromHex(f.result);
|
|
29
30
|
it(`returns ${f.result} for taggedHash "${f.tag}" of ${f.hex}`, () => {
|
|
30
31
|
const actual = bcrypto.taggedHash(f.tag as TaggedHashPrefix, bytes);
|
|
31
|
-
assert.strictEqual(actual
|
|
32
|
+
assert.strictEqual(toHex(actual), toHex(expected));
|
|
32
33
|
});
|
|
33
34
|
});
|
|
34
35
|
});
|
|
@@ -36,15 +37,22 @@ describe('crypto', () => {
|
|
|
36
37
|
describe('TAGGED_HASH_PREFIXES', () => {
|
|
37
38
|
const taggedHashPrefixes = Object.fromEntries(
|
|
38
39
|
TAGS.map((tag: TaggedHashPrefix) => {
|
|
39
|
-
const tagHash = sha256(
|
|
40
|
-
return [tag,
|
|
40
|
+
const tagHash = sha256(fromUtf8(tag));
|
|
41
|
+
return [tag, concat([tagHash, tagHash])];
|
|
41
42
|
}),
|
|
42
43
|
);
|
|
43
44
|
it('stored the result of operation', () => {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
);
|
|
45
|
+
// Compare that all keys match and all values are equal
|
|
46
|
+
const storedKeys = Object.keys(TAGGED_HASH_PREFIXES);
|
|
47
|
+
const computedKeys = Object.keys(taggedHashPrefixes);
|
|
48
|
+
assert.deepStrictEqual(storedKeys.sort(), computedKeys.sort());
|
|
49
|
+
|
|
50
|
+
for (const key of storedKeys) {
|
|
51
|
+
assert.ok(
|
|
52
|
+
equals(TAGGED_HASH_PREFIXES[key as TaggedHashPrefix], taggedHashPrefixes[key]),
|
|
53
|
+
`Mismatch for tag ${key}`,
|
|
54
|
+
);
|
|
55
|
+
}
|
|
48
56
|
});
|
|
49
57
|
});
|
|
50
58
|
});
|
package/test/fixtures/p2pk.json
CHANGED
|
@@ -72,14 +72,6 @@
|
|
|
72
72
|
"exception": "Not enough data",
|
|
73
73
|
"arguments": {}
|
|
74
74
|
},
|
|
75
|
-
{
|
|
76
|
-
"description": "Non-canonical signature",
|
|
77
|
-
"exception": "Expected property \"signature\" of type \\?isCanonicalScriptSignature, got Buffer",
|
|
78
|
-
"arguments": {
|
|
79
|
-
"pubkey": "030000000000000000000000000000000000000000000000000000000000000001",
|
|
80
|
-
"signature": "3044"
|
|
81
|
-
}
|
|
82
|
-
},
|
|
83
75
|
{
|
|
84
76
|
"description": "Unexpected OP_RESERVED",
|
|
85
77
|
"exception": "Output is invalid",
|
package/test/fixtures/p2pkh.json
CHANGED
|
@@ -124,7 +124,7 @@
|
|
|
124
124
|
},
|
|
125
125
|
{
|
|
126
126
|
"description": "Non-minimally encoded (non BIP62 compliant)",
|
|
127
|
-
"exception": "
|
|
127
|
+
"exception": "Output is invalid",
|
|
128
128
|
"arguments": {
|
|
129
129
|
"outputHex": "76a94c14aa4d7985c57e011a8b3dd8e0e5a73aaef41629c588ac"
|
|
130
130
|
}
|
package/test/fixtures/p2sh.json
CHANGED
|
@@ -204,7 +204,7 @@
|
|
|
204
204
|
},
|
|
205
205
|
{
|
|
206
206
|
"description": "Non-minimally encoded (non BIP62 compliant)",
|
|
207
|
-
"exception": "
|
|
207
|
+
"exception": "Output is invalid",
|
|
208
208
|
"arguments": {
|
|
209
209
|
"outputHex": "a94c14c286a1af0947f58d1ad787385b1c2c4a976f9e7187"
|
|
210
210
|
}
|
|
@@ -892,12 +892,12 @@
|
|
|
892
892
|
"invalid": {
|
|
893
893
|
"addInput": [
|
|
894
894
|
{
|
|
895
|
-
"exception": "Expected
|
|
895
|
+
"exception": "Expected 32-byte hash",
|
|
896
896
|
"hash": "0aed1366a73b6057ee7800d737bff1bdf8c448e98d86bc0998f2b009816d",
|
|
897
897
|
"index": 0
|
|
898
898
|
},
|
|
899
899
|
{
|
|
900
|
-
"exception": "Expected
|
|
900
|
+
"exception": "Expected 32-byte hash",
|
|
901
901
|
"hash": "0aed1366a73b6057ee7800d737bff1bdf8c448e98d86bc0998f2b009816da9b0ffff",
|
|
902
902
|
"index": 0
|
|
903
903
|
}
|
|
@@ -4,3 +4,28 @@ const APIPASS = process.env.APIPASS || 'satoshi';
|
|
|
4
4
|
const APIURL = process.env.APIURL || 'https://regtest.bitbank.cc/1';
|
|
5
5
|
|
|
6
6
|
export const regtestUtils = new RegtestUtils({ APIPASS, APIURL });
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Broadcast with retry logic.
|
|
10
|
+
* The regtest node sometimes returns Bad Request when the faucet
|
|
11
|
+
* transaction hasn't fully propagated yet (race with indexer).
|
|
12
|
+
*/
|
|
13
|
+
export async function broadcastAndVerify(
|
|
14
|
+
txHex: string,
|
|
15
|
+
verify: () => Promise<void>,
|
|
16
|
+
maxRetries: number = 3,
|
|
17
|
+
): Promise<void> {
|
|
18
|
+
for (let attempt = 0; attempt < maxRetries; attempt++) {
|
|
19
|
+
try {
|
|
20
|
+
await regtestUtils.broadcast(txHex);
|
|
21
|
+
await verify();
|
|
22
|
+
return;
|
|
23
|
+
} catch (err: unknown) {
|
|
24
|
+
if (attempt === maxRetries - 1) throw err;
|
|
25
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
26
|
+
if (msg !== 'Bad Request') throw err;
|
|
27
|
+
console.log(`Bad Request, retry #${attempt + 1}`);
|
|
28
|
+
await new Promise((r) => setTimeout(r, 200 * (attempt + 1)));
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -3,6 +3,7 @@ import { ECPairFactory } from 'ecpair';
|
|
|
3
3
|
import * as ecc from 'tiny-secp256k1';
|
|
4
4
|
import { describe, it } from 'vitest';
|
|
5
5
|
import * as bitcoin from '../../src/index.js';
|
|
6
|
+
import type { PublicKey } from '../../src/index.js';
|
|
6
7
|
import { regtestUtils } from './_regtest.js';
|
|
7
8
|
|
|
8
9
|
const ECPair = ECPairFactory(ecc);
|
|
@@ -48,7 +49,7 @@ describe('bitcoinjs-lib (addresses)', () => {
|
|
|
48
49
|
'026477115981fe981a6918a6297d9803c4dc04f328f22041bedff886bbc2962e01',
|
|
49
50
|
'02c96db2302d19b43d4c69368babace7854cc84eb9e061cde51cfa77ca4a22b8b9',
|
|
50
51
|
'03c6103b3b83e4a24a0e33a4df246ef11772f9992663db0c35759a5e2ebf68d8e9',
|
|
51
|
-
].map((hex) => Buffer.from(hex, 'hex'));
|
|
52
|
+
].map((hex) => Buffer.from(hex, 'hex')) as unknown as PublicKey[];
|
|
52
53
|
const { address } = bitcoin.payments.p2sh({
|
|
53
54
|
redeem: bitcoin.payments.p2ms({ m: 2, pubkeys }),
|
|
54
55
|
});
|
|
@@ -80,7 +81,7 @@ describe('bitcoinjs-lib (addresses)', () => {
|
|
|
80
81
|
'02c96db2302d19b43d4c69368babace7854cc84eb9e061cde51cfa77ca4a22b8b9',
|
|
81
82
|
'023e4740d0ba639e28963f3476157b7cf2fb7c6fdf4254f97099cf8670b505ea59',
|
|
82
83
|
'03c6103b3b83e4a24a0e33a4df246ef11772f9992663db0c35759a5e2ebf68d8e9',
|
|
83
|
-
].map((hex) => Buffer.from(hex, 'hex'));
|
|
84
|
+
].map((hex) => Buffer.from(hex, 'hex')) as unknown as PublicKey[];
|
|
84
85
|
const { address } = bitcoin.payments.p2wsh({
|
|
85
86
|
redeem: bitcoin.payments.p2ms({ m: 3, pubkeys }),
|
|
86
87
|
});
|
|
@@ -95,7 +96,7 @@ describe('bitcoinjs-lib (addresses)', () => {
|
|
|
95
96
|
const pubkeys = [
|
|
96
97
|
'026477115981fe981a6918a6297d9803c4dc04f328f22041bedff886bbc2962e01',
|
|
97
98
|
'02c96db2302d19b43d4c69368babace7854cc84eb9e061cde51cfa77ca4a22b8b9',
|
|
98
|
-
].map((hex) => Buffer.from(hex, 'hex'));
|
|
99
|
+
].map((hex) => Buffer.from(hex, 'hex')) as unknown as PublicKey[];
|
|
99
100
|
const { address } = bitcoin.payments.p2sh({
|
|
100
101
|
redeem: bitcoin.payments.p2wsh({
|
|
101
102
|
redeem: bitcoin.payments.p2ms({ m: 2, pubkeys }),
|
|
@@ -4,6 +4,7 @@ import * as ecc from 'tiny-secp256k1';
|
|
|
4
4
|
import * as bip39 from 'bip39';
|
|
5
5
|
import { describe, it } from 'vitest';
|
|
6
6
|
import * as bitcoin from '../../src/index.js';
|
|
7
|
+
import type { PublicKey } from '../../src/index.js';
|
|
7
8
|
|
|
8
9
|
const bip32 = BIP32Factory(ecc);
|
|
9
10
|
|
|
@@ -90,7 +91,7 @@ describe('bitcoinjs-lib (BIP32)', () => {
|
|
|
90
91
|
|
|
91
92
|
const { address } = bitcoin.payments.p2sh({
|
|
92
93
|
redeem: bitcoin.payments.p2wpkh({
|
|
93
|
-
pubkey: child.publicKey,
|
|
94
|
+
pubkey: child.publicKey as PublicKey,
|
|
94
95
|
network: bitcoin.networks.testnet,
|
|
95
96
|
}),
|
|
96
97
|
network: bitcoin.networks.testnet,
|
|
@@ -22,7 +22,7 @@ describe('bitcoinjs-lib (blocks)', () => {
|
|
|
22
22
|
|
|
23
23
|
assert.strictEqual(script[0], 0x03);
|
|
24
24
|
const heightBuffer = script.slice(1, 4);
|
|
25
|
-
const height = bitcoin.script.number.decode(heightBuffer);
|
|
25
|
+
const height = bitcoin.script.number.decode(heightBuffer as Buffer);
|
|
26
26
|
assert.strictEqual(height, 498303);
|
|
27
27
|
});
|
|
28
28
|
});
|
|
@@ -3,6 +3,8 @@ import { ECPairFactory } from 'ecpair';
|
|
|
3
3
|
import * as ecc from 'tiny-secp256k1';
|
|
4
4
|
import { beforeAll, describe, it } from 'vitest';
|
|
5
5
|
import * as bitcoin from '../../src/index.js';
|
|
6
|
+
import { toHex, fromHex, reverseCopy } from '../../src/index.js';
|
|
7
|
+
import type { Script, Bytes32, Satoshi } from '../../src/index.js';
|
|
6
8
|
import { regtestUtils } from './_regtest.js';
|
|
7
9
|
|
|
8
10
|
// @ts-ignore
|
|
@@ -11,12 +13,12 @@ import bip65 from 'bip65';
|
|
|
11
13
|
const ECPair = ECPairFactory(ecc);
|
|
12
14
|
const regtest = regtestUtils.network;
|
|
13
15
|
|
|
14
|
-
function toOutputScript(address: string):
|
|
15
|
-
return bitcoin.address.toOutputScript(address, regtest);
|
|
16
|
+
function toOutputScript(address: string): Script {
|
|
17
|
+
return bitcoin.address.toOutputScript(address, regtest) as Script;
|
|
16
18
|
}
|
|
17
19
|
|
|
18
|
-
function idToHash(txid: string):
|
|
19
|
-
return
|
|
20
|
+
function idToHash(txid: string): Bytes32 {
|
|
21
|
+
return reverseCopy(fromHex(txid)) as Bytes32;
|
|
20
22
|
}
|
|
21
23
|
|
|
22
24
|
const alice = ECPair.fromWIF('cScfkGjbzzoeewVWmU2hYPUHeVGJRDdFt7WhmrVVGkxpmPP8BHWe', regtest);
|
|
@@ -31,10 +33,10 @@ describe('bitcoinjs-lib (transactions w/ CLTV)', () => {
|
|
|
31
33
|
const hashType = bitcoin.Transaction.SIGHASH_ALL;
|
|
32
34
|
|
|
33
35
|
interface KeyPair {
|
|
34
|
-
publicKey:
|
|
36
|
+
publicKey: Uint8Array;
|
|
35
37
|
}
|
|
36
38
|
|
|
37
|
-
function cltvCheckSigOutput(aQ: KeyPair, bQ: KeyPair, lockTime: number):
|
|
39
|
+
function cltvCheckSigOutput(aQ: KeyPair, bQ: KeyPair, lockTime: number): Script {
|
|
38
40
|
return bitcoin.script.fromASM(
|
|
39
41
|
`
|
|
40
42
|
OP_IF
|
|
@@ -42,10 +44,10 @@ describe('bitcoinjs-lib (transactions w/ CLTV)', () => {
|
|
|
42
44
|
OP_CHECKLOCKTIMEVERIFY
|
|
43
45
|
OP_DROP
|
|
44
46
|
OP_ELSE
|
|
45
|
-
${bQ.publicKey
|
|
47
|
+
${toHex(bQ.publicKey)}
|
|
46
48
|
OP_CHECKSIGVERIFY
|
|
47
49
|
OP_ENDIF
|
|
48
|
-
${aQ.publicKey
|
|
50
|
+
${toHex(aQ.publicKey)}
|
|
49
51
|
OP_CHECKSIG
|
|
50
52
|
`
|
|
51
53
|
.trim()
|
|
@@ -76,7 +78,7 @@ describe('bitcoinjs-lib (transactions w/ CLTV)', () => {
|
|
|
76
78
|
tx.locktime = lockTime;
|
|
77
79
|
// Note: nSequence MUST be <= 0xfffffffe otherwise OP_CHECKLOCKTIMEVERIFY will fail.
|
|
78
80
|
tx.addInput(idToHash(unspent.txId), unspent.vout, 0xfffffffe);
|
|
79
|
-
tx.addOutput(toOutputScript(regtestUtils.RANDOM_ADDRESS),
|
|
81
|
+
tx.addOutput(toOutputScript(regtestUtils.RANDOM_ADDRESS), 70000n as Satoshi);
|
|
80
82
|
|
|
81
83
|
// {Alice's signature} OP_TRUE
|
|
82
84
|
const signatureHash = tx.hashForSignature(0, redeemScript, hashType);
|
|
@@ -89,7 +91,7 @@ describe('bitcoinjs-lib (transactions w/ CLTV)', () => {
|
|
|
89
91
|
output: redeemScript,
|
|
90
92
|
},
|
|
91
93
|
}).input;
|
|
92
|
-
tx.setInputScript(0, redeemScriptSig!);
|
|
94
|
+
tx.setInputScript(0, redeemScriptSig! as Script);
|
|
93
95
|
|
|
94
96
|
await regtestUtils.broadcast(tx.toHex());
|
|
95
97
|
|
|
@@ -122,7 +124,7 @@ describe('bitcoinjs-lib (transactions w/ CLTV)', () => {
|
|
|
122
124
|
tx.locktime = lockTime;
|
|
123
125
|
// Note: nSequence MUST be <= 0xfffffffe otherwise OP_CHECKLOCKTIMEVERIFY will fail.
|
|
124
126
|
tx.addInput(idToHash(unspent.txId), unspent.vout, 0xfffffffe);
|
|
125
|
-
tx.addOutput(toOutputScript(regtestUtils.RANDOM_ADDRESS),
|
|
127
|
+
tx.addOutput(toOutputScript(regtestUtils.RANDOM_ADDRESS), 70000n as Satoshi);
|
|
126
128
|
|
|
127
129
|
// {Alice's signature} OP_TRUE
|
|
128
130
|
const signatureHash = tx.hashForSignature(0, redeemScript, hashType);
|
|
@@ -135,7 +137,7 @@ describe('bitcoinjs-lib (transactions w/ CLTV)', () => {
|
|
|
135
137
|
output: redeemScript,
|
|
136
138
|
},
|
|
137
139
|
}).input;
|
|
138
|
-
tx.setInputScript(0, redeemScriptSig!);
|
|
140
|
+
tx.setInputScript(0, redeemScriptSig! as Script);
|
|
139
141
|
|
|
140
142
|
// TODO: test that it failures _prior_ to expiry, unfortunately, race conditions when run concurrently
|
|
141
143
|
// ...
|
|
@@ -170,7 +172,7 @@ describe('bitcoinjs-lib (transactions w/ CLTV)', () => {
|
|
|
170
172
|
tx.locktime = lockTime;
|
|
171
173
|
// Note: nSequence MUST be <= 0xfffffffe otherwise OP_CHECKLOCKTIMEVERIFY will fail.
|
|
172
174
|
tx.addInput(idToHash(unspent.txId), unspent.vout, 0xfffffffe);
|
|
173
|
-
tx.addOutput(toOutputScript(regtestUtils.RANDOM_ADDRESS),
|
|
175
|
+
tx.addOutput(toOutputScript(regtestUtils.RANDOM_ADDRESS), 80000n as Satoshi);
|
|
174
176
|
|
|
175
177
|
// {Alice's signature} {Bob's signature} OP_FALSE
|
|
176
178
|
const signatureHash = tx.hashForSignature(0, redeemScript, hashType);
|
|
@@ -184,7 +186,7 @@ describe('bitcoinjs-lib (transactions w/ CLTV)', () => {
|
|
|
184
186
|
output: redeemScript,
|
|
185
187
|
},
|
|
186
188
|
}).input;
|
|
187
|
-
tx.setInputScript(0, redeemScriptSig!);
|
|
189
|
+
tx.setInputScript(0, redeemScriptSig! as Script);
|
|
188
190
|
|
|
189
191
|
await regtestUtils.broadcast(tx.toHex());
|
|
190
192
|
await regtestUtils.verify({
|
|
@@ -215,7 +217,7 @@ describe('bitcoinjs-lib (transactions w/ CLTV)', () => {
|
|
|
215
217
|
tx.locktime = lockTime;
|
|
216
218
|
// Note: nSequence MUST be <= 0xfffffffe otherwise OP_CHECKLOCKTIMEVERIFY will fail.
|
|
217
219
|
tx.addInput(idToHash(unspent.txId), unspent.vout, 0xfffffffe);
|
|
218
|
-
tx.addOutput(toOutputScript(regtestUtils.RANDOM_ADDRESS),
|
|
220
|
+
tx.addOutput(toOutputScript(regtestUtils.RANDOM_ADDRESS), 10000n as Satoshi);
|
|
219
221
|
|
|
220
222
|
// {Alice's signature} OP_TRUE
|
|
221
223
|
const signatureHash = tx.hashForSignature(0, redeemScript, hashType);
|
|
@@ -229,7 +231,7 @@ describe('bitcoinjs-lib (transactions w/ CLTV)', () => {
|
|
|
229
231
|
output: redeemScript,
|
|
230
232
|
},
|
|
231
233
|
}).input;
|
|
232
|
-
tx.setInputScript(0, redeemScriptSig!);
|
|
234
|
+
tx.setInputScript(0, redeemScriptSig! as Script);
|
|
233
235
|
|
|
234
236
|
await regtestUtils.broadcast(tx.toHex()).catch((err: unknown) => {
|
|
235
237
|
assert.throws(() => {
|
|
@@ -4,21 +4,24 @@ import * as ecc from 'tiny-secp256k1';
|
|
|
4
4
|
import { beforeAll, describe, it } from 'vitest';
|
|
5
5
|
import * as bitcoin from '../../src/index.js';
|
|
6
6
|
import type { PsbtInput } from '../../src/index.js';
|
|
7
|
+
import { toHex, fromHex, reverseCopy } from '../../src/index.js';
|
|
8
|
+
import type { Script, Bytes32, Satoshi } from '../../src/index.js';
|
|
7
9
|
import { regtestUtils } from './_regtest.js';
|
|
8
10
|
|
|
9
11
|
// @ts-ignore
|
|
10
12
|
import bip68 from 'bip68';
|
|
11
|
-
|
|
13
|
+
|
|
14
|
+
|
|
12
15
|
|
|
13
16
|
const ECPair = ECPairFactory(ecc);
|
|
14
17
|
const regtest = regtestUtils.network;
|
|
15
18
|
|
|
16
|
-
function toOutputScript(address: string):
|
|
17
|
-
return bitcoin.address.toOutputScript(address, regtest);
|
|
19
|
+
function toOutputScript(address: string): Script {
|
|
20
|
+
return bitcoin.address.toOutputScript(address, regtest) as Script;
|
|
18
21
|
}
|
|
19
22
|
|
|
20
|
-
function idToHash(txid: string):
|
|
21
|
-
return
|
|
23
|
+
function idToHash(txid: string): Bytes32 {
|
|
24
|
+
return reverseCopy(fromHex(txid)) as Bytes32;
|
|
22
25
|
}
|
|
23
26
|
|
|
24
27
|
const alice = ECPair.fromWIF('cScfkGjbzzoeewVWmU2hYPUHeVGJRDdFt7WhmrVVGkxpmPP8BHWe', regtest);
|
|
@@ -35,11 +38,11 @@ describe('bitcoinjs-lib (transactions w/ CSV)', () => {
|
|
|
35
38
|
const hashType = bitcoin.Transaction.SIGHASH_ALL;
|
|
36
39
|
|
|
37
40
|
interface KeyPair {
|
|
38
|
-
publicKey:
|
|
41
|
+
publicKey: Uint8Array;
|
|
39
42
|
}
|
|
40
43
|
|
|
41
44
|
// IF MTP (from when confirmed) > seconds, _alice can redeem
|
|
42
|
-
function csvCheckSigOutput(_alice: KeyPair, _bob: KeyPair, sequence: number):
|
|
45
|
+
function csvCheckSigOutput(_alice: KeyPair, _bob: KeyPair, sequence: number): Script {
|
|
43
46
|
return bitcoin.script.fromASM(
|
|
44
47
|
`
|
|
45
48
|
OP_IF
|
|
@@ -47,10 +50,10 @@ describe('bitcoinjs-lib (transactions w/ CSV)', () => {
|
|
|
47
50
|
OP_CHECKSEQUENCEVERIFY
|
|
48
51
|
OP_DROP
|
|
49
52
|
OP_ELSE
|
|
50
|
-
${_bob.publicKey
|
|
53
|
+
${toHex(_bob.publicKey)}
|
|
51
54
|
OP_CHECKSIGVERIFY
|
|
52
55
|
OP_ENDIF
|
|
53
|
-
${_alice.publicKey
|
|
56
|
+
${toHex(_alice.publicKey)}
|
|
54
57
|
OP_CHECKSIG
|
|
55
58
|
`
|
|
56
59
|
.trim()
|
|
@@ -73,7 +76,7 @@ describe('bitcoinjs-lib (transactions w/ CSV)', () => {
|
|
|
73
76
|
_dave: KeyPair,
|
|
74
77
|
sequence1: number,
|
|
75
78
|
sequence2: number,
|
|
76
|
-
):
|
|
79
|
+
): Script {
|
|
77
80
|
return bitcoin.script.fromASM(
|
|
78
81
|
`
|
|
79
82
|
OP_IF
|
|
@@ -83,20 +86,20 @@ describe('bitcoinjs-lib (transactions w/ CSV)', () => {
|
|
|
83
86
|
${bitcoin.script.number.encode(sequence1).toString('hex')}
|
|
84
87
|
OP_CHECKSEQUENCEVERIFY
|
|
85
88
|
OP_DROP
|
|
86
|
-
${_alice.publicKey
|
|
89
|
+
${toHex(_alice.publicKey)}
|
|
87
90
|
OP_CHECKSIGVERIFY
|
|
88
91
|
OP_1
|
|
89
92
|
OP_ENDIF
|
|
90
|
-
${_bob.publicKey
|
|
91
|
-
${_charles.publicKey
|
|
92
|
-
${_dave.publicKey
|
|
93
|
+
${toHex(_bob.publicKey)}
|
|
94
|
+
${toHex(_charles.publicKey)}
|
|
95
|
+
${toHex(_dave.publicKey)}
|
|
93
96
|
OP_3
|
|
94
97
|
OP_CHECKMULTISIG
|
|
95
98
|
OP_ELSE
|
|
96
99
|
${bitcoin.script.number.encode(sequence2).toString('hex')}
|
|
97
100
|
OP_CHECKSEQUENCEVERIFY
|
|
98
101
|
OP_DROP
|
|
99
|
-
${_alice.publicKey
|
|
102
|
+
${toHex(_alice.publicKey)}
|
|
100
103
|
OP_CHECKSIG
|
|
101
104
|
OP_ENDIF
|
|
102
105
|
`
|
|
@@ -123,7 +126,7 @@ describe('bitcoinjs-lib (transactions w/ CSV)', () => {
|
|
|
123
126
|
const unspent = await regtestUtils.faucet(p2sh.address!, 1e5);
|
|
124
127
|
const utx = await regtestUtils.fetch(unspent.txId);
|
|
125
128
|
// for non segwit inputs, you must pass the full transaction buffer
|
|
126
|
-
const nonWitnessUtxo =
|
|
129
|
+
const nonWitnessUtxo = fromHex(utx.txHex);
|
|
127
130
|
|
|
128
131
|
// This is an example of using the finalizeInput second parameter to
|
|
129
132
|
// define how you finalize the inputs, allowing for any type of script.
|
|
@@ -138,10 +141,10 @@ describe('bitcoinjs-lib (transactions w/ CSV)', () => {
|
|
|
138
141
|
})
|
|
139
142
|
.addOutput({
|
|
140
143
|
address: regtestUtils.RANDOM_ADDRESS,
|
|
141
|
-
value:
|
|
144
|
+
value: 70000n as Satoshi,
|
|
142
145
|
})
|
|
143
146
|
.signInput(0, alice)
|
|
144
|
-
.finalizeInput(0, csvGetFinalScripts) // See csvGetFinalScripts below
|
|
147
|
+
.finalizeInput(0, csvGetFinalScripts as any) // See csvGetFinalScripts below
|
|
145
148
|
.extractTransaction();
|
|
146
149
|
|
|
147
150
|
// TODO: test that it failures _prior_ to expiry, unfortunately, race conditions when run concurrently
|
|
@@ -180,10 +183,10 @@ describe('bitcoinjs-lib (transactions w/ CSV)', () => {
|
|
|
180
183
|
const tx = new bitcoin.Transaction();
|
|
181
184
|
tx.version = 2;
|
|
182
185
|
tx.addInput(idToHash(unspent.txId), unspent.vout, sequence);
|
|
183
|
-
tx.addOutput(toOutputScript(regtestUtils.RANDOM_ADDRESS),
|
|
186
|
+
tx.addOutput(toOutputScript(regtestUtils.RANDOM_ADDRESS), 10000n as Satoshi);
|
|
184
187
|
|
|
185
188
|
// {Alice's signature} OP_TRUE
|
|
186
|
-
const signatureHash = tx.hashForSignature(0, p2sh.redeem!.output
|
|
189
|
+
const signatureHash = tx.hashForSignature(0, p2sh.redeem!.output! as Script, hashType);
|
|
187
190
|
const redeemScriptSig = bitcoin.payments.p2sh({
|
|
188
191
|
network: regtest,
|
|
189
192
|
redeem: {
|
|
@@ -196,7 +199,7 @@ describe('bitcoinjs-lib (transactions w/ CSV)', () => {
|
|
|
196
199
|
]),
|
|
197
200
|
},
|
|
198
201
|
}).input;
|
|
199
|
-
tx.setInputScript(0, redeemScriptSig!);
|
|
202
|
+
tx.setInputScript(0, redeemScriptSig! as Script);
|
|
200
203
|
|
|
201
204
|
await regtestUtils.broadcast(tx.toHex()).catch((err: unknown) => {
|
|
202
205
|
assert.throws(() => {
|
|
@@ -228,10 +231,10 @@ describe('bitcoinjs-lib (transactions w/ CSV)', () => {
|
|
|
228
231
|
const tx = new bitcoin.Transaction();
|
|
229
232
|
tx.version = 2;
|
|
230
233
|
tx.addInput(idToHash(unspent.txId), unspent.vout);
|
|
231
|
-
tx.addOutput(toOutputScript(regtestUtils.RANDOM_ADDRESS),
|
|
234
|
+
tx.addOutput(toOutputScript(regtestUtils.RANDOM_ADDRESS), 70000n as Satoshi);
|
|
232
235
|
|
|
233
236
|
// OP_0 {Bob sig} {Charles sig} OP_TRUE OP_TRUE
|
|
234
|
-
const signatureHash = tx.hashForSignature(0, p2sh.redeem!.output
|
|
237
|
+
const signatureHash = tx.hashForSignature(0, p2sh.redeem!.output! as Script, hashType);
|
|
235
238
|
const redeemScriptSig = bitcoin.payments.p2sh({
|
|
236
239
|
network: regtest,
|
|
237
240
|
redeem: {
|
|
@@ -246,7 +249,7 @@ describe('bitcoinjs-lib (transactions w/ CSV)', () => {
|
|
|
246
249
|
]),
|
|
247
250
|
},
|
|
248
251
|
}).input;
|
|
249
|
-
tx.setInputScript(0, redeemScriptSig!);
|
|
252
|
+
tx.setInputScript(0, redeemScriptSig! as Script);
|
|
250
253
|
|
|
251
254
|
await regtestUtils.broadcast(tx.toHex());
|
|
252
255
|
|
|
@@ -281,10 +284,10 @@ describe('bitcoinjs-lib (transactions w/ CSV)', () => {
|
|
|
281
284
|
const tx = new bitcoin.Transaction();
|
|
282
285
|
tx.version = 2;
|
|
283
286
|
tx.addInput(idToHash(unspent.txId), unspent.vout, sequence1); // Set sequence1 for input
|
|
284
|
-
tx.addOutput(toOutputScript(regtestUtils.RANDOM_ADDRESS),
|
|
287
|
+
tx.addOutput(toOutputScript(regtestUtils.RANDOM_ADDRESS), 70000n as Satoshi);
|
|
285
288
|
|
|
286
289
|
// OP_0 {Bob sig} {Alice mediator sig} OP_FALSE OP_TRUE
|
|
287
|
-
const signatureHash = tx.hashForSignature(0, p2sh.redeem!.output
|
|
290
|
+
const signatureHash = tx.hashForSignature(0, p2sh.redeem!.output! as Script, hashType);
|
|
288
291
|
const redeemScriptSig = bitcoin.payments.p2sh({
|
|
289
292
|
network: regtest,
|
|
290
293
|
redeem: {
|
|
@@ -299,7 +302,7 @@ describe('bitcoinjs-lib (transactions w/ CSV)', () => {
|
|
|
299
302
|
]),
|
|
300
303
|
},
|
|
301
304
|
}).input;
|
|
302
|
-
tx.setInputScript(0, redeemScriptSig!);
|
|
305
|
+
tx.setInputScript(0, redeemScriptSig! as Script);
|
|
303
306
|
|
|
304
307
|
// Wait 2 blocks
|
|
305
308
|
await regtestUtils.mine(2);
|
|
@@ -337,10 +340,10 @@ describe('bitcoinjs-lib (transactions w/ CSV)', () => {
|
|
|
337
340
|
const tx = new bitcoin.Transaction();
|
|
338
341
|
tx.version = 2;
|
|
339
342
|
tx.addInput(idToHash(unspent.txId), unspent.vout, sequence2); // Set sequence2 for input
|
|
340
|
-
tx.addOutput(toOutputScript(regtestUtils.RANDOM_ADDRESS),
|
|
343
|
+
tx.addOutput(toOutputScript(regtestUtils.RANDOM_ADDRESS), 70000n as Satoshi);
|
|
341
344
|
|
|
342
345
|
// {Alice mediator sig} OP_FALSE
|
|
343
|
-
const signatureHash = tx.hashForSignature(0, p2sh.redeem!.output
|
|
346
|
+
const signatureHash = tx.hashForSignature(0, p2sh.redeem!.output! as Script, hashType);
|
|
344
347
|
const redeemScriptSig = bitcoin.payments.p2sh({
|
|
345
348
|
network: regtest,
|
|
346
349
|
redeem: {
|
|
@@ -352,7 +355,7 @@ describe('bitcoinjs-lib (transactions w/ CSV)', () => {
|
|
|
352
355
|
]),
|
|
353
356
|
},
|
|
354
357
|
}).input;
|
|
355
|
-
tx.setInputScript(0, redeemScriptSig!);
|
|
358
|
+
tx.setInputScript(0, redeemScriptSig! as Script);
|
|
356
359
|
|
|
357
360
|
// Wait 5 blocks
|
|
358
361
|
await regtestUtils.mine(5);
|
|
@@ -374,13 +377,13 @@ describe('bitcoinjs-lib (transactions w/ CSV)', () => {
|
|
|
374
377
|
function csvGetFinalScripts(
|
|
375
378
|
inputIndex: number,
|
|
376
379
|
input: PsbtInput,
|
|
377
|
-
script:
|
|
380
|
+
script: Script,
|
|
378
381
|
isSegwit: boolean,
|
|
379
382
|
isP2SH: boolean,
|
|
380
383
|
isP2WSH: boolean,
|
|
381
384
|
): {
|
|
382
|
-
finalScriptSig:
|
|
383
|
-
finalScriptWitness:
|
|
385
|
+
finalScriptSig: Script | undefined;
|
|
386
|
+
finalScriptWitness: Uint8Array | undefined;
|
|
384
387
|
} {
|
|
385
388
|
// Step 1: Check to make sure the meaningful script matches what you expect.
|
|
386
389
|
const decompiled = bitcoin.script.decompile(script);
|
|
@@ -412,41 +415,11 @@ function csvGetFinalScripts(
|
|
|
412
415
|
redeem: payment,
|
|
413
416
|
});
|
|
414
417
|
|
|
415
|
-
function witnessStackToScriptWitness(witness: Buffer[]): Buffer {
|
|
416
|
-
let buffer = Buffer.allocUnsafe(0);
|
|
417
|
-
|
|
418
|
-
function writeSlice(slice: Buffer): void {
|
|
419
|
-
buffer = Buffer.concat([buffer, Buffer.from(slice)]);
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
function writeVarInt(i: number): void {
|
|
423
|
-
const currentLen = buffer.length;
|
|
424
|
-
const varintLen = varuint.encodingLength(i);
|
|
425
|
-
|
|
426
|
-
buffer = Buffer.concat([buffer, Buffer.allocUnsafe(varintLen)]);
|
|
427
|
-
varuint.encode(i, buffer, currentLen);
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
function writeVarSlice(slice: Buffer): void {
|
|
431
|
-
writeVarInt(slice.length);
|
|
432
|
-
writeSlice(slice);
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
function writeVector(vector: Buffer[]): void {
|
|
436
|
-
writeVarInt(vector.length);
|
|
437
|
-
vector.forEach(writeVarSlice);
|
|
438
|
-
}
|
|
439
|
-
|
|
440
|
-
writeVector(witness);
|
|
441
|
-
|
|
442
|
-
return buffer;
|
|
443
|
-
}
|
|
444
|
-
|
|
445
418
|
return {
|
|
446
419
|
finalScriptSig: payment.input,
|
|
447
420
|
finalScriptWitness:
|
|
448
421
|
payment.witness && payment.witness.length > 0
|
|
449
|
-
? witnessStackToScriptWitness(payment.witness)
|
|
422
|
+
? bitcoin.witnessStackToScriptWitness(payment.witness)
|
|
450
423
|
: undefined,
|
|
451
424
|
};
|
|
452
425
|
}
|
|
@@ -2,6 +2,8 @@ import { ECPairFactory } from 'ecpair';
|
|
|
2
2
|
import * as ecc from 'tiny-secp256k1';
|
|
3
3
|
import { describe, it } from 'vitest';
|
|
4
4
|
import * as bitcoin from '../../src/index.js';
|
|
5
|
+
import { fromHex } from '../../src/index.js';
|
|
6
|
+
import type { Satoshi } from '../../src/index.js';
|
|
5
7
|
import { regtestUtils } from './_regtest.js';
|
|
6
8
|
import * as fs from 'node:fs';
|
|
7
9
|
|
|
@@ -15,20 +17,20 @@ async function buildAndSign(
|
|
|
15
17
|
redeemScript: any,
|
|
16
18
|
witnessScript: any,
|
|
17
19
|
): Promise<null> {
|
|
18
|
-
const unspent = await regtestUtils.faucetComplex(prevOutput, 5e4);
|
|
20
|
+
const unspent = await regtestUtils.faucetComplex(Buffer.from(prevOutput), 5e4);
|
|
19
21
|
const utx = await regtestUtils.fetch(unspent.txId);
|
|
20
22
|
|
|
21
23
|
const psbt = new bitcoin.Psbt({ network: NETWORK })
|
|
22
24
|
.addInput({
|
|
23
25
|
hash: unspent.txId,
|
|
24
26
|
index: unspent.vout,
|
|
25
|
-
nonWitnessUtxo:
|
|
27
|
+
nonWitnessUtxo: fromHex(utx.txHex),
|
|
26
28
|
...(redeemScript ? { redeemScript } : {}),
|
|
27
29
|
...(witnessScript ? { witnessScript } : {}),
|
|
28
30
|
})
|
|
29
31
|
.addOutput({
|
|
30
32
|
address: regtestUtils.RANDOM_ADDRESS,
|
|
31
|
-
value:
|
|
33
|
+
value: 20000n as Satoshi,
|
|
32
34
|
});
|
|
33
35
|
|
|
34
36
|
if (depends.signatures) {
|