@btc-vision/bitcoin 6.5.6 → 7.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/HOW_TO_WRITE_GOOD_CODE.md +2436 -0
- package/benchmark/psbt-2000-inputs.bench.ts +178 -0
- package/benchmark/signing.bench.ts +147 -0
- package/browser/address.d.ts +57 -10
- 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 +12477 -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 +58 -11
- package/build/address.d.ts.map +1 -0
- package/build/address.js +82 -25
- 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 +202 -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 +35 -9
- package/build/payments/bip341.d.ts.map +1 -0
- package/build/payments/bip341.js +34 -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 -150
- 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 +428 -103
- 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 +465 -143
- 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 +117 -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 +406 -413
- 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 +36 -12
- 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 +101 -37
- 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 +169 -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 +68 -9
- package/scripts/bundle-ecc.ts +111 -0
- package/src/address.ts +91 -45
- package/src/bech32utils.ts +3 -3
- package/src/bip66.ts +34 -24
- package/src/block.ts +205 -86
- package/src/branded.ts +18 -0
- package/src/crypto.ts +64 -26
- package/src/ecc/context.ts +280 -0
- package/src/ecc/index.ts +14 -0
- package/src/ecc/types.ts +147 -0
- package/src/ecpair.d.ts +99 -0
- package/src/errors.ts +163 -0
- package/src/index.ts +112 -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 +32 -33
- package/src/payments/embed.ts +244 -41
- package/src/payments/index.ts +12 -10
- package/src/payments/p2ms.ts +497 -118
- package/src/payments/p2op.ts +432 -134
- package/src/payments/p2pk.ts +370 -72
- package/src/payments/p2pkh.ts +524 -130
- package/src/payments/p2sh.ts +572 -169
- package/src/payments/p2tr.ts +686 -194
- package/src/payments/p2wpkh.ts +482 -105
- package/src/payments/p2wsh.ts +524 -162
- package/src/payments/types.ts +80 -66
- package/src/psbt/bip371.ts +72 -51
- package/src/psbt/psbtutils.ts +39 -40
- package/src/psbt/types.ts +324 -0
- package/src/psbt/utils.ts +188 -0
- package/src/psbt/validation.ts +185 -0
- package/src/psbt.ts +608 -827
- package/src/pubkey.ts +22 -23
- package/src/push_data.ts +18 -16
- package/src/script.ts +81 -66
- package/src/script_number.ts +6 -6
- package/src/script_signature.ts +33 -36
- package/src/transaction.ts +462 -239
- package/src/types.ts +229 -100
- package/src/workers/WorkerSigningPool.node.ts +887 -0
- package/src/workers/WorkerSigningPool.ts +666 -0
- package/src/workers/ecc-bundle.ts +26 -0
- package/src/workers/index.ts +165 -0
- package/src/workers/psbt-parallel.ts +327 -0
- package/src/workers/signing-worker.ts +353 -0
- package/src/workers/types.ts +417 -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/typedoc.json +29 -0
- package/vite.config.browser.ts +3 -42
- package/vitest.config.integration.ts +2 -0
- package/browser/bufferutils.d.ts +0 -34
- package/browser/chunks/crypto-BhCpKpek.js +0 -2033
- package/browser/chunks/payments-B1wlSccx.js +0 -1089
- package/browser/chunks/psbt-BCNk7JUx.js +0 -4055
- 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/src/block.ts
CHANGED
|
@@ -1,49 +1,91 @@
|
|
|
1
|
-
import { BufferReader, BufferWriter, reverseBuffer, varuint } from './bufferutils.js';
|
|
2
1
|
import * as bcrypto from './crypto.js';
|
|
2
|
+
import {
|
|
3
|
+
alloc,
|
|
4
|
+
BinaryReader,
|
|
5
|
+
BinaryWriter,
|
|
6
|
+
compare,
|
|
7
|
+
equals,
|
|
8
|
+
fromHex,
|
|
9
|
+
reverse,
|
|
10
|
+
toHex,
|
|
11
|
+
varuint,
|
|
12
|
+
} from './io/index.js';
|
|
3
13
|
import { fastMerkleRoot } from './merkle.js';
|
|
4
14
|
import { Transaction } from './transaction.js';
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
const { typeforce } = types;
|
|
15
|
+
import type { Bytes32 } from './types.js';
|
|
8
16
|
|
|
9
17
|
const errorMerkleNoTxes = new TypeError('Cannot compute merkle root for zero transactions');
|
|
10
18
|
const errorWitnessNotSegwit = new TypeError('Cannot compute witness commit for non-segwit block');
|
|
11
19
|
|
|
20
|
+
const WITNESS_COMMIT_PREFIX = fromHex('6a24aa21a9ed');
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Represents a Bitcoin block with header fields and optional transactions.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* import { Block, fromHex } from '@btc-vision/bitcoin';
|
|
28
|
+
*
|
|
29
|
+
* // Parse a block from hex
|
|
30
|
+
* const block = Block.fromHex('0100000000000000...');
|
|
31
|
+
*
|
|
32
|
+
* // Access block properties
|
|
33
|
+
* console.log(block.version);
|
|
34
|
+
* console.log(block.getId());
|
|
35
|
+
* console.log(block.timestamp);
|
|
36
|
+
*
|
|
37
|
+
* // Check proof of work
|
|
38
|
+
* if (block.checkProofOfWork()) {
|
|
39
|
+
* console.log('Valid PoW');
|
|
40
|
+
* }
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
12
43
|
export class Block {
|
|
44
|
+
/** Block version number */
|
|
13
45
|
version: number = 1;
|
|
14
|
-
|
|
15
|
-
|
|
46
|
+
/** Hash of the previous block (32 bytes) */
|
|
47
|
+
prevHash?: Bytes32 = undefined;
|
|
48
|
+
/** Merkle root of the transactions (32 bytes) */
|
|
49
|
+
merkleRoot?: Bytes32 = undefined;
|
|
50
|
+
/** Block timestamp (Unix time) */
|
|
16
51
|
timestamp: number = 0;
|
|
17
|
-
|
|
52
|
+
/** Witness commitment for SegWit blocks (32 bytes) */
|
|
53
|
+
witnessCommit?: Bytes32 = undefined;
|
|
54
|
+
/** Compact representation of the target threshold */
|
|
18
55
|
bits: number = 0;
|
|
56
|
+
/** Nonce used for proof of work */
|
|
19
57
|
nonce: number = 0;
|
|
58
|
+
/** Array of transactions included in the block */
|
|
20
59
|
transactions?: Transaction[] = undefined;
|
|
21
60
|
|
|
22
|
-
|
|
61
|
+
/**
|
|
62
|
+
* Parses a Block from a Uint8Array.
|
|
63
|
+
* @param buffer - The raw block data (minimum 80 bytes for header only)
|
|
64
|
+
* @returns Parsed Block instance
|
|
65
|
+
* @throws Error if buffer is too small
|
|
66
|
+
*/
|
|
67
|
+
static fromBuffer(buffer: Uint8Array): Block {
|
|
23
68
|
if (buffer.length < 80) throw new Error('Buffer too small (< 80 bytes)');
|
|
24
69
|
|
|
25
|
-
const
|
|
70
|
+
const reader = new BinaryReader(buffer);
|
|
26
71
|
|
|
27
72
|
const block = new Block();
|
|
28
|
-
block.version =
|
|
29
|
-
block.prevHash =
|
|
30
|
-
block.merkleRoot =
|
|
31
|
-
block.timestamp =
|
|
32
|
-
block.bits =
|
|
33
|
-
block.nonce =
|
|
73
|
+
block.version = reader.readInt32LE();
|
|
74
|
+
block.prevHash = reader.readBytes(32) as Bytes32;
|
|
75
|
+
block.merkleRoot = reader.readBytes(32) as Bytes32;
|
|
76
|
+
block.timestamp = reader.readUInt32LE();
|
|
77
|
+
block.bits = reader.readUInt32LE();
|
|
78
|
+
block.nonce = reader.readUInt32LE();
|
|
34
79
|
|
|
35
80
|
if (buffer.length === 80) return block;
|
|
36
81
|
|
|
37
82
|
const readTransaction = (): Transaction => {
|
|
38
|
-
const tx = Transaction.fromBuffer(
|
|
39
|
-
|
|
40
|
-
true,
|
|
41
|
-
);
|
|
42
|
-
bufferReader.offset += tx.byteLength();
|
|
83
|
+
const tx = Transaction.fromBuffer(reader.data.subarray(reader.offset), true);
|
|
84
|
+
reader.offset += tx.byteLength();
|
|
43
85
|
return tx;
|
|
44
86
|
};
|
|
45
87
|
|
|
46
|
-
const nTransactions =
|
|
88
|
+
const nTransactions = reader.readVarInt();
|
|
47
89
|
block.transactions = [];
|
|
48
90
|
|
|
49
91
|
for (let i = 0; i < nTransactions; ++i) {
|
|
@@ -58,20 +100,43 @@ export class Block {
|
|
|
58
100
|
return block;
|
|
59
101
|
}
|
|
60
102
|
|
|
103
|
+
/**
|
|
104
|
+
* Parses a Block from a hex string.
|
|
105
|
+
* @param hex - Hexadecimal representation of the block
|
|
106
|
+
* @returns Parsed Block instance
|
|
107
|
+
*/
|
|
61
108
|
static fromHex(hex: string): Block {
|
|
62
|
-
return Block.fromBuffer(
|
|
109
|
+
return Block.fromBuffer(fromHex(hex));
|
|
63
110
|
}
|
|
64
111
|
|
|
65
|
-
|
|
112
|
+
/**
|
|
113
|
+
* Calculates the target threshold from the compact bits representation.
|
|
114
|
+
* @param bits - Compact bits value from block header
|
|
115
|
+
* @returns 32-byte target threshold
|
|
116
|
+
*/
|
|
117
|
+
static calculateTarget(bits: number): Bytes32 {
|
|
66
118
|
const exponent = ((bits & 0xff000000) >> 24) - 3;
|
|
67
119
|
const mantissa = bits & 0x007fffff;
|
|
68
|
-
const target =
|
|
69
|
-
|
|
70
|
-
|
|
120
|
+
const target = alloc(32);
|
|
121
|
+
// Write mantissa as big-endian at the computed offset
|
|
122
|
+
const offset = 29 - exponent;
|
|
123
|
+
target[offset] = (mantissa >> 16) & 0xff;
|
|
124
|
+
target[offset + 1] = (mantissa >> 8) & 0xff;
|
|
125
|
+
target[offset + 2] = mantissa & 0xff;
|
|
126
|
+
return target as Bytes32;
|
|
71
127
|
}
|
|
72
128
|
|
|
73
|
-
|
|
74
|
-
|
|
129
|
+
/**
|
|
130
|
+
* Calculates the merkle root for a list of transactions.
|
|
131
|
+
* @param transactions - Array of transactions
|
|
132
|
+
* @param forWitness - If true, calculate witness merkle root (for SegWit)
|
|
133
|
+
* @returns 32-byte merkle root hash
|
|
134
|
+
* @throws TypeError if transactions is empty or not an array
|
|
135
|
+
*/
|
|
136
|
+
static calculateMerkleRoot(transactions: Transaction[], forWitness?: boolean): Bytes32 {
|
|
137
|
+
if (!Array.isArray(transactions)) {
|
|
138
|
+
throw new TypeError('Expected an array of transactions');
|
|
139
|
+
}
|
|
75
140
|
if (transactions.length === 0) throw errorMerkleNoTxes;
|
|
76
141
|
if (forWitness && !txesHaveWitnessCommit(transactions)) throw errorWitnessNotSegwit;
|
|
77
142
|
|
|
@@ -79,45 +144,74 @@ export class Block {
|
|
|
79
144
|
|
|
80
145
|
const rootHash = fastMerkleRoot(hashes, bcrypto.hash256);
|
|
81
146
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
147
|
+
if (forWitness) {
|
|
148
|
+
const witness = transactions[0]!.ins[0]!.witness![0]!;
|
|
149
|
+
const combined = new Uint8Array(rootHash.length + witness.length);
|
|
150
|
+
combined.set(rootHash);
|
|
151
|
+
combined.set(witness, rootHash.length);
|
|
152
|
+
return bcrypto.hash256(combined) as Bytes32;
|
|
153
|
+
}
|
|
154
|
+
return rootHash as Bytes32;
|
|
85
155
|
}
|
|
86
156
|
|
|
87
|
-
|
|
88
|
-
|
|
157
|
+
/**
|
|
158
|
+
* Extracts the witness commitment from the coinbase transaction.
|
|
159
|
+
* @returns 32-byte witness commitment or null if not found
|
|
160
|
+
*/
|
|
161
|
+
getWitnessCommit(): Bytes32 | null {
|
|
162
|
+
if (!this.transactions || !txesHaveWitnessCommit(this.transactions)) return null;
|
|
89
163
|
|
|
90
164
|
// The merkle root for the witness data is in an OP_RETURN output.
|
|
91
165
|
// There is no rule for the index of the output, so use filter to find it.
|
|
92
166
|
// The root is prepended with 0xaa21a9ed so check for 0x6a24aa21a9ed
|
|
93
167
|
// If multiple commits are found, the output with highest index is assumed.
|
|
94
|
-
const witnessCommits = this.transactions
|
|
95
|
-
out.script.subarray(0, 6)
|
|
168
|
+
const witnessCommits = this.transactions[0]!.outs.filter((out) =>
|
|
169
|
+
equals(out.script.subarray(0, 6), WITNESS_COMMIT_PREFIX),
|
|
96
170
|
).map((out) => out.script.subarray(6, 38));
|
|
97
171
|
if (witnessCommits.length === 0) return null;
|
|
98
172
|
// Use the commit with the highest output (should only be one though)
|
|
99
173
|
const result = witnessCommits[witnessCommits.length - 1];
|
|
100
174
|
|
|
101
|
-
if (!(result instanceof
|
|
102
|
-
return result;
|
|
175
|
+
if (!(result instanceof Uint8Array && result.length === 32)) return null;
|
|
176
|
+
return result as Bytes32;
|
|
103
177
|
}
|
|
104
178
|
|
|
179
|
+
/**
|
|
180
|
+
* Checks if this block has a witness commitment.
|
|
181
|
+
* @returns True if the block has a witness commitment
|
|
182
|
+
*/
|
|
105
183
|
hasWitnessCommit(): boolean {
|
|
106
|
-
if (this.witnessCommit instanceof
|
|
184
|
+
if (this.witnessCommit instanceof Uint8Array && this.witnessCommit.length === 32)
|
|
185
|
+
return true;
|
|
107
186
|
if (this.getWitnessCommit() !== null) return true;
|
|
108
187
|
return false;
|
|
109
188
|
}
|
|
110
189
|
|
|
190
|
+
/**
|
|
191
|
+
* Checks if any transaction in this block has witness data.
|
|
192
|
+
* @returns True if any transaction has witness data
|
|
193
|
+
*/
|
|
111
194
|
hasWitness(): boolean {
|
|
112
|
-
return anyTxHasWitness(this.transactions
|
|
195
|
+
return this.transactions ? anyTxHasWitness(this.transactions) : false;
|
|
113
196
|
}
|
|
114
197
|
|
|
198
|
+
/**
|
|
199
|
+
* Calculates the weight of this block.
|
|
200
|
+
* Weight = (base size * 3) + total size
|
|
201
|
+
* @returns Block weight in weight units
|
|
202
|
+
*/
|
|
115
203
|
weight(): number {
|
|
116
204
|
const base = this.byteLength(false, false);
|
|
117
205
|
const total = this.byteLength(false, true);
|
|
118
206
|
return base * 3 + total;
|
|
119
207
|
}
|
|
120
208
|
|
|
209
|
+
/**
|
|
210
|
+
* Calculates the serialized byte length of this block.
|
|
211
|
+
* @param headersOnly - If true, return only header size (80 bytes)
|
|
212
|
+
* @param allowWitness - If true, include witness data in calculation
|
|
213
|
+
* @returns Byte length of the serialized block
|
|
214
|
+
*/
|
|
121
215
|
byteLength(headersOnly?: boolean, allowWitness: boolean = true): number {
|
|
122
216
|
if (headersOnly || !this.transactions) return 80;
|
|
123
217
|
|
|
@@ -128,14 +222,26 @@ export class Block {
|
|
|
128
222
|
);
|
|
129
223
|
}
|
|
130
224
|
|
|
131
|
-
|
|
225
|
+
/**
|
|
226
|
+
* Computes the double-SHA256 hash of the block header.
|
|
227
|
+
* @returns 32-byte block hash
|
|
228
|
+
*/
|
|
229
|
+
getHash(): Bytes32 {
|
|
132
230
|
return bcrypto.hash256(this.toBuffer(true));
|
|
133
231
|
}
|
|
134
232
|
|
|
233
|
+
/**
|
|
234
|
+
* Returns the block ID (hash in reversed hex format, as displayed in block explorers).
|
|
235
|
+
* @returns Block ID as hex string
|
|
236
|
+
*/
|
|
135
237
|
getId(): string {
|
|
136
|
-
return
|
|
238
|
+
return toHex(reverse(this.getHash()));
|
|
137
239
|
}
|
|
138
240
|
|
|
241
|
+
/**
|
|
242
|
+
* Converts the block timestamp to a Date object.
|
|
243
|
+
* @returns UTC date of the block
|
|
244
|
+
*/
|
|
139
245
|
getUTCDate(): Date {
|
|
140
246
|
const date = new Date(0); // epoch
|
|
141
247
|
date.setUTCSeconds(this.timestamp);
|
|
@@ -143,94 +249,107 @@ export class Block {
|
|
|
143
249
|
return date;
|
|
144
250
|
}
|
|
145
251
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
252
|
+
/**
|
|
253
|
+
* Serializes the block to a Uint8Array.
|
|
254
|
+
* @param headersOnly - If true, only serialize the 80-byte header
|
|
255
|
+
* @returns Serialized block data
|
|
256
|
+
*/
|
|
257
|
+
toBuffer(headersOnly?: boolean): Uint8Array {
|
|
258
|
+
if (!this.prevHash) throw new TypeError('Block prevHash is required');
|
|
259
|
+
if (!this.merkleRoot) throw new TypeError('Block merkleRoot is required');
|
|
149
260
|
|
|
150
|
-
const
|
|
261
|
+
const buffer = new Uint8Array(this.byteLength(headersOnly));
|
|
151
262
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
263
|
+
const writer = new BinaryWriter(buffer);
|
|
264
|
+
|
|
265
|
+
writer.writeInt32LE(this.version);
|
|
266
|
+
writer.writeBytes(this.prevHash);
|
|
267
|
+
writer.writeBytes(this.merkleRoot);
|
|
268
|
+
writer.writeUInt32LE(this.timestamp);
|
|
269
|
+
writer.writeUInt32LE(this.bits);
|
|
270
|
+
writer.writeUInt32LE(this.nonce);
|
|
158
271
|
|
|
159
272
|
if (headersOnly || !this.transactions) return buffer;
|
|
160
273
|
|
|
161
|
-
|
|
162
|
-
bufferWriter.offset += encoded.bytes;
|
|
274
|
+
writer.writeVarInt(this.transactions.length);
|
|
163
275
|
|
|
164
276
|
this.transactions.forEach((tx) => {
|
|
165
277
|
const txSize = tx.byteLength(); // TODO: extract from toBuffer?
|
|
166
|
-
tx.toBuffer(buffer
|
|
167
|
-
|
|
278
|
+
tx.toBuffer(buffer.subarray(writer.offset));
|
|
279
|
+
writer.offset += txSize;
|
|
168
280
|
});
|
|
169
281
|
|
|
170
282
|
return buffer;
|
|
171
283
|
}
|
|
172
284
|
|
|
285
|
+
/**
|
|
286
|
+
* Serializes the block to a hex string.
|
|
287
|
+
* @param headersOnly - If true, only serialize the 80-byte header
|
|
288
|
+
* @returns Hex string representation of the block
|
|
289
|
+
*/
|
|
173
290
|
toHex(headersOnly?: boolean): string {
|
|
174
|
-
return this.toBuffer(headersOnly)
|
|
291
|
+
return toHex(this.toBuffer(headersOnly));
|
|
175
292
|
}
|
|
176
293
|
|
|
294
|
+
/**
|
|
295
|
+
* Validates the merkle root and witness commitment (if present).
|
|
296
|
+
* @returns True if the transaction roots are valid
|
|
297
|
+
*/
|
|
177
298
|
checkTxRoots(): boolean {
|
|
178
299
|
// If the Block has segwit transactions but no witness commit,
|
|
179
300
|
// there's no way it can be valid, so fail the check.
|
|
180
301
|
const hasWitnessCommit = this.hasWitnessCommit();
|
|
181
302
|
if (!hasWitnessCommit && this.hasWitness()) return false;
|
|
182
|
-
return this
|
|
303
|
+
return this.#checkMerkleRoot() && (hasWitnessCommit ? this.#checkWitnessCommit() : true);
|
|
183
304
|
}
|
|
184
305
|
|
|
306
|
+
/**
|
|
307
|
+
* Validates that the block hash meets the target threshold (proof of work).
|
|
308
|
+
* @returns True if the block's proof of work is valid
|
|
309
|
+
*/
|
|
185
310
|
checkProofOfWork(): boolean {
|
|
186
|
-
const hash
|
|
311
|
+
const hash = reverse(this.getHash());
|
|
187
312
|
const target = Block.calculateTarget(this.bits);
|
|
188
313
|
|
|
189
|
-
return
|
|
314
|
+
return compare(hash, target) <= 0;
|
|
190
315
|
}
|
|
191
316
|
|
|
192
|
-
|
|
317
|
+
#checkMerkleRoot(): boolean {
|
|
193
318
|
if (!this.transactions) throw errorMerkleNoTxes;
|
|
319
|
+
if (!this.merkleRoot) throw new TypeError('Block merkleRoot is required');
|
|
194
320
|
|
|
195
321
|
const actualMerkleRoot = Block.calculateMerkleRoot(this.transactions);
|
|
196
|
-
return this.merkleRoot
|
|
322
|
+
return compare(this.merkleRoot, actualMerkleRoot) === 0;
|
|
197
323
|
}
|
|
198
324
|
|
|
199
|
-
|
|
325
|
+
#checkWitnessCommit(): boolean {
|
|
200
326
|
if (!this.transactions) throw errorMerkleNoTxes;
|
|
201
327
|
if (!this.hasWitnessCommit()) throw errorWitnessNotSegwit;
|
|
328
|
+
if (!this.witnessCommit) throw errorWitnessNotSegwit;
|
|
202
329
|
|
|
203
330
|
const actualWitnessCommit = Block.calculateMerkleRoot(this.transactions, true);
|
|
204
|
-
return this.witnessCommit
|
|
331
|
+
return compare(this.witnessCommit, actualWitnessCommit) === 0;
|
|
205
332
|
}
|
|
206
333
|
}
|
|
207
334
|
|
|
335
|
+
/**
|
|
336
|
+
* Checks if the coinbase transaction has witness data (required for witness commitment).
|
|
337
|
+
* @param transactions - Array of transactions
|
|
338
|
+
* @returns True if coinbase has witness data
|
|
339
|
+
*/
|
|
208
340
|
function txesHaveWitnessCommit(transactions: Transaction[]): boolean {
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
transactions[0].ins &&
|
|
213
|
-
transactions[0].ins instanceof Array &&
|
|
214
|
-
transactions[0].ins[0] &&
|
|
215
|
-
transactions[0].ins[0].witness &&
|
|
216
|
-
transactions[0].ins[0].witness instanceof Array &&
|
|
217
|
-
transactions[0].ins[0].witness.length > 0
|
|
218
|
-
);
|
|
341
|
+
const coinbase = transactions[0];
|
|
342
|
+
if (!coinbase?.ins?.[0]?.witness) return false;
|
|
343
|
+
return coinbase.ins[0].witness.length > 0;
|
|
219
344
|
}
|
|
220
345
|
|
|
346
|
+
/**
|
|
347
|
+
* Checks if any transaction in the array has witness data.
|
|
348
|
+
* @param transactions - Array of transactions
|
|
349
|
+
* @returns True if any transaction has witness data
|
|
350
|
+
*/
|
|
221
351
|
function anyTxHasWitness(transactions: Transaction[]): boolean {
|
|
222
|
-
return (
|
|
223
|
-
|
|
224
|
-
transactions.some(
|
|
225
|
-
(tx) =>
|
|
226
|
-
typeof tx === 'object' &&
|
|
227
|
-
tx.ins instanceof Array &&
|
|
228
|
-
tx.ins.some(
|
|
229
|
-
(input) =>
|
|
230
|
-
typeof input === 'object' &&
|
|
231
|
-
input.witness instanceof Array &&
|
|
232
|
-
input.witness.length > 0,
|
|
233
|
-
),
|
|
234
|
-
)
|
|
352
|
+
return transactions.some((tx) =>
|
|
353
|
+
tx.ins?.some((input) => input.witness && input.witness.length > 0),
|
|
235
354
|
);
|
|
236
355
|
}
|
package/src/branded.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Branded type definitions for type-safe primitives.
|
|
3
|
+
*
|
|
4
|
+
* @packageDocumentation
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
declare const __brand: unique symbol;
|
|
8
|
+
type Brand<T, B extends string> = T & { readonly [__brand]: B };
|
|
9
|
+
|
|
10
|
+
export type Bytes32 = Brand<Uint8Array, 'Bytes32'>;
|
|
11
|
+
export type Bytes20 = Brand<Uint8Array, 'Bytes20'>;
|
|
12
|
+
export type PublicKey = Brand<Uint8Array, 'PublicKey'>;
|
|
13
|
+
export type XOnlyPublicKey = Brand<Uint8Array, 'XOnlyPublicKey'>;
|
|
14
|
+
export type Satoshi = Brand<bigint, 'Satoshi'>;
|
|
15
|
+
export type PrivateKey = Brand<Uint8Array, 'PrivateKey'>;
|
|
16
|
+
export type Signature = Brand<Uint8Array, 'Signature'>;
|
|
17
|
+
export type SchnorrSignature = Brand<Uint8Array, 'SchnorrSignature'>;
|
|
18
|
+
export type Script = Brand<Uint8Array, 'Script'>;
|
package/src/crypto.ts
CHANGED
|
@@ -1,32 +1,62 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
2
|
+
* Cryptographic hashing functions for Bitcoin.
|
|
3
|
+
* Includes ripemd160, sha1, sha256, hash160, hash256, and taggedHash.
|
|
4
4
|
*
|
|
5
5
|
* @packageDocumentation
|
|
6
6
|
*/
|
|
7
7
|
import { ripemd160 as _ripemd160, sha1 as _sha1 } from '@noble/hashes/legacy.js';
|
|
8
8
|
import { sha256 as _sha256 } from '@noble/hashes/sha2.js';
|
|
9
|
+
import { concat } from './io/index.js';
|
|
10
|
+
import type { Bytes20, Bytes32 } from './types.js';
|
|
9
11
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
+
/**
|
|
13
|
+
* Computes RIPEMD-160 hash of the input.
|
|
14
|
+
* @param data - Input data
|
|
15
|
+
* @returns 20-byte hash (Bytes20)
|
|
16
|
+
*/
|
|
17
|
+
export function ripemd160(data: Uint8Array): Bytes20 {
|
|
18
|
+
return _ripemd160(data) as Bytes20;
|
|
12
19
|
}
|
|
13
20
|
|
|
14
|
-
|
|
15
|
-
|
|
21
|
+
/**
|
|
22
|
+
* Computes SHA-1 hash of the input.
|
|
23
|
+
* @param data - Input data
|
|
24
|
+
* @returns 20-byte hash (Bytes20)
|
|
25
|
+
*/
|
|
26
|
+
export function sha1(data: Uint8Array): Bytes20 {
|
|
27
|
+
return _sha1(data) as Bytes20;
|
|
16
28
|
}
|
|
17
29
|
|
|
18
|
-
|
|
19
|
-
|
|
30
|
+
/**
|
|
31
|
+
* Computes SHA-256 hash of the input.
|
|
32
|
+
* @param data - Input data
|
|
33
|
+
* @returns 32-byte hash (Bytes32)
|
|
34
|
+
*/
|
|
35
|
+
export function sha256(data: Uint8Array): Bytes32 {
|
|
36
|
+
return _sha256(data) as Bytes32;
|
|
20
37
|
}
|
|
21
38
|
|
|
22
|
-
|
|
23
|
-
|
|
39
|
+
/**
|
|
40
|
+
* Computes HASH160 (RIPEMD160(SHA256(data))) of the input.
|
|
41
|
+
* @param data - Input data
|
|
42
|
+
* @returns 20-byte hash (Bytes20)
|
|
43
|
+
*/
|
|
44
|
+
export function hash160(data: Uint8Array): Bytes20 {
|
|
45
|
+
return _ripemd160(_sha256(data)) as Bytes20;
|
|
24
46
|
}
|
|
25
47
|
|
|
26
|
-
|
|
27
|
-
|
|
48
|
+
/**
|
|
49
|
+
* Computes double SHA-256 hash of the input.
|
|
50
|
+
* @param data - Input data
|
|
51
|
+
* @returns 32-byte hash (Bytes32)
|
|
52
|
+
*/
|
|
53
|
+
export function hash256(data: Uint8Array): Bytes32 {
|
|
54
|
+
return _sha256(_sha256(data)) as Bytes32;
|
|
28
55
|
}
|
|
29
56
|
|
|
57
|
+
/**
|
|
58
|
+
* BIP340/Taproot tag names for tagged hashing.
|
|
59
|
+
*/
|
|
30
60
|
export const TAGS = [
|
|
31
61
|
'BIP0340/challenge',
|
|
32
62
|
'BIP0340/aux',
|
|
@@ -38,63 +68,65 @@ export const TAGS = [
|
|
|
38
68
|
'KeyAgg list',
|
|
39
69
|
'KeyAgg coefficient',
|
|
40
70
|
] as const;
|
|
71
|
+
|
|
41
72
|
export type TaggedHashPrefix = (typeof TAGS)[number];
|
|
73
|
+
|
|
42
74
|
type TaggedHashPrefixes = {
|
|
43
|
-
[key in TaggedHashPrefix]:
|
|
75
|
+
[key in TaggedHashPrefix]: Uint8Array;
|
|
44
76
|
};
|
|
45
|
-
|
|
77
|
+
|
|
46
78
|
/**
|
|
47
|
-
*
|
|
79
|
+
* Precomputed tagged hash prefixes: SHA256(tag) || SHA256(tag)
|
|
48
80
|
*/
|
|
49
81
|
export const TAGGED_HASH_PREFIXES: TaggedHashPrefixes = {
|
|
50
|
-
'BIP0340/challenge':
|
|
82
|
+
'BIP0340/challenge': new Uint8Array([
|
|
51
83
|
123, 181, 45, 122, 159, 239, 88, 50, 62, 177, 191, 122, 64, 125, 179, 130, 210, 243, 242,
|
|
52
84
|
216, 27, 177, 34, 79, 73, 254, 81, 143, 109, 72, 211, 124, 123, 181, 45, 122, 159, 239, 88,
|
|
53
85
|
50, 62, 177, 191, 122, 64, 125, 179, 130, 210, 243, 242, 216, 27, 177, 34, 79, 73, 254, 81,
|
|
54
86
|
143, 109, 72, 211, 124,
|
|
55
87
|
]),
|
|
56
|
-
'BIP0340/aux':
|
|
88
|
+
'BIP0340/aux': new Uint8Array([
|
|
57
89
|
241, 239, 78, 94, 192, 99, 202, 218, 109, 148, 202, 250, 157, 152, 126, 160, 105, 38, 88,
|
|
58
90
|
57, 236, 193, 31, 151, 45, 119, 165, 46, 216, 193, 204, 144, 241, 239, 78, 94, 192, 99, 202,
|
|
59
91
|
218, 109, 148, 202, 250, 157, 152, 126, 160, 105, 38, 88, 57, 236, 193, 31, 151, 45, 119,
|
|
60
92
|
165, 46, 216, 193, 204, 144,
|
|
61
93
|
]),
|
|
62
|
-
'BIP0340/nonce':
|
|
94
|
+
'BIP0340/nonce': new Uint8Array([
|
|
63
95
|
7, 73, 119, 52, 167, 155, 203, 53, 91, 155, 140, 125, 3, 79, 18, 28, 244, 52, 215, 62, 247,
|
|
64
96
|
45, 218, 25, 135, 0, 97, 251, 82, 191, 235, 47, 7, 73, 119, 52, 167, 155, 203, 53, 91, 155,
|
|
65
97
|
140, 125, 3, 79, 18, 28, 244, 52, 215, 62, 247, 45, 218, 25, 135, 0, 97, 251, 82, 191, 235,
|
|
66
98
|
47,
|
|
67
99
|
]),
|
|
68
|
-
TapLeaf:
|
|
100
|
+
TapLeaf: new Uint8Array([
|
|
69
101
|
174, 234, 143, 220, 66, 8, 152, 49, 5, 115, 75, 88, 8, 29, 30, 38, 56, 211, 95, 28, 181, 64,
|
|
70
102
|
8, 212, 211, 87, 202, 3, 190, 120, 233, 238, 174, 234, 143, 220, 66, 8, 152, 49, 5, 115, 75,
|
|
71
103
|
88, 8, 29, 30, 38, 56, 211, 95, 28, 181, 64, 8, 212, 211, 87, 202, 3, 190, 120, 233, 238,
|
|
72
104
|
]),
|
|
73
|
-
TapBranch:
|
|
105
|
+
TapBranch: new Uint8Array([
|
|
74
106
|
25, 65, 161, 242, 229, 110, 185, 95, 162, 169, 241, 148, 190, 92, 1, 247, 33, 111, 51, 237,
|
|
75
107
|
130, 176, 145, 70, 52, 144, 208, 91, 245, 22, 160, 21, 25, 65, 161, 242, 229, 110, 185, 95,
|
|
76
108
|
162, 169, 241, 148, 190, 92, 1, 247, 33, 111, 51, 237, 130, 176, 145, 70, 52, 144, 208, 91,
|
|
77
109
|
245, 22, 160, 21,
|
|
78
110
|
]),
|
|
79
|
-
TapSighash:
|
|
111
|
+
TapSighash: new Uint8Array([
|
|
80
112
|
244, 10, 72, 223, 75, 42, 112, 200, 180, 146, 75, 242, 101, 70, 97, 237, 61, 149, 253, 102,
|
|
81
113
|
163, 19, 235, 135, 35, 117, 151, 198, 40, 228, 160, 49, 244, 10, 72, 223, 75, 42, 112, 200,
|
|
82
114
|
180, 146, 75, 242, 101, 70, 97, 237, 61, 149, 253, 102, 163, 19, 235, 135, 35, 117, 151,
|
|
83
115
|
198, 40, 228, 160, 49,
|
|
84
116
|
]),
|
|
85
|
-
TapTweak:
|
|
117
|
+
TapTweak: new Uint8Array([
|
|
86
118
|
232, 15, 225, 99, 156, 156, 160, 80, 227, 175, 27, 57, 193, 67, 198, 62, 66, 156, 188, 235,
|
|
87
119
|
21, 217, 64, 251, 181, 197, 161, 244, 175, 87, 197, 233, 232, 15, 225, 99, 156, 156, 160,
|
|
88
120
|
80, 227, 175, 27, 57, 193, 67, 198, 62, 66, 156, 188, 235, 21, 217, 64, 251, 181, 197, 161,
|
|
89
121
|
244, 175, 87, 197, 233,
|
|
90
122
|
]),
|
|
91
|
-
'KeyAgg list':
|
|
123
|
+
'KeyAgg list': new Uint8Array([
|
|
92
124
|
72, 28, 151, 28, 60, 11, 70, 215, 240, 178, 117, 174, 89, 141, 78, 44, 126, 215, 49, 156,
|
|
93
125
|
89, 74, 92, 110, 199, 158, 160, 212, 153, 2, 148, 240, 72, 28, 151, 28, 60, 11, 70, 215,
|
|
94
126
|
240, 178, 117, 174, 89, 141, 78, 44, 126, 215, 49, 156, 89, 74, 92, 110, 199, 158, 160, 212,
|
|
95
127
|
153, 2, 148, 240,
|
|
96
128
|
]),
|
|
97
|
-
'KeyAgg coefficient':
|
|
129
|
+
'KeyAgg coefficient': new Uint8Array([
|
|
98
130
|
191, 201, 4, 3, 77, 28, 136, 232, 200, 14, 34, 229, 61, 36, 86, 109, 100, 130, 78, 214, 66,
|
|
99
131
|
114, 129, 192, 145, 0, 249, 77, 205, 82, 201, 129, 191, 201, 4, 3, 77, 28, 136, 232, 200,
|
|
100
132
|
14, 34, 229, 61, 36, 86, 109, 100, 130, 78, 214, 66, 114, 129, 192, 145, 0, 249, 77, 205,
|
|
@@ -102,6 +134,12 @@ export const TAGGED_HASH_PREFIXES: TaggedHashPrefixes = {
|
|
|
102
134
|
]),
|
|
103
135
|
};
|
|
104
136
|
|
|
105
|
-
|
|
106
|
-
|
|
137
|
+
/**
|
|
138
|
+
* Computes a BIP340-style tagged hash.
|
|
139
|
+
* @param prefix - The tag name
|
|
140
|
+
* @param data - Input data
|
|
141
|
+
* @returns 32-byte hash (Bytes32)
|
|
142
|
+
*/
|
|
143
|
+
export function taggedHash(prefix: TaggedHashPrefix, data: Uint8Array): Bytes32 {
|
|
144
|
+
return sha256(concat([TAGGED_HASH_PREFIXES[prefix], data]));
|
|
107
145
|
}
|