@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
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stateful binary reader with a single DataView instance.
|
|
3
|
+
*
|
|
4
|
+
* Zero allocations during read operations. The DataView is created once
|
|
5
|
+
* in the constructor and reused for all reads.
|
|
6
|
+
*
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* High-performance binary reader for parsing binary data.
|
|
11
|
+
*
|
|
12
|
+
* Creates exactly ONE DataView instance that is reused for all read operations.
|
|
13
|
+
* This eliminates garbage collection pressure from repeated DataView allocations.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* import { BinaryReader, fromHex } from '@btc-vision/bitcoin';
|
|
18
|
+
*
|
|
19
|
+
* // Parse a Bitcoin transaction
|
|
20
|
+
* const data = fromHex('01000000...');
|
|
21
|
+
* const reader = new BinaryReader(data);
|
|
22
|
+
*
|
|
23
|
+
* const version = reader.readInt32LE();
|
|
24
|
+
* const inputCount = reader.readVarInt();
|
|
25
|
+
*
|
|
26
|
+
* for (let i = 0; i < inputCount; i++) {
|
|
27
|
+
* const txid = reader.readBytes(32);
|
|
28
|
+
* const vout = reader.readUInt32LE();
|
|
29
|
+
* const script = reader.readVarBytes();
|
|
30
|
+
* const sequence = reader.readUInt32LE();
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export declare class BinaryReader {
|
|
35
|
+
#private;
|
|
36
|
+
/**
|
|
37
|
+
* Creates a new BinaryReader.
|
|
38
|
+
*
|
|
39
|
+
* @param data - The byte array to read from
|
|
40
|
+
* @param offset - Initial read position (default 0)
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```typescript
|
|
44
|
+
* import { BinaryReader, fromHex } from '@btc-vision/bitcoin';
|
|
45
|
+
*
|
|
46
|
+
* const data = fromHex('01020304');
|
|
47
|
+
* const reader = new BinaryReader(data);
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
constructor(data: Uint8Array, offset?: number);
|
|
51
|
+
/**
|
|
52
|
+
* Creates a BinaryReader from a hex string.
|
|
53
|
+
*
|
|
54
|
+
* @param hex - Hex string (with or without 0x prefix)
|
|
55
|
+
* @returns A new BinaryReader instance
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* ```typescript
|
|
59
|
+
* import { BinaryReader } from '@btc-vision/bitcoin';
|
|
60
|
+
*
|
|
61
|
+
* const reader = BinaryReader.fromHex('01000000');
|
|
62
|
+
* const version = reader.readInt32LE(); // 1
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
static fromHex(hex: string): BinaryReader;
|
|
66
|
+
/**
|
|
67
|
+
* Current read position in the buffer.
|
|
68
|
+
*/
|
|
69
|
+
get offset(): number;
|
|
70
|
+
/**
|
|
71
|
+
* Sets the read position.
|
|
72
|
+
*
|
|
73
|
+
* @param value - New offset value
|
|
74
|
+
* @throws RangeError if offset is negative or beyond buffer length
|
|
75
|
+
*/
|
|
76
|
+
set offset(value: number);
|
|
77
|
+
/**
|
|
78
|
+
* Total length of the underlying buffer.
|
|
79
|
+
*/
|
|
80
|
+
get length(): number;
|
|
81
|
+
/**
|
|
82
|
+
* Number of bytes remaining to be read.
|
|
83
|
+
*/
|
|
84
|
+
get remaining(): number;
|
|
85
|
+
/**
|
|
86
|
+
* The underlying data buffer.
|
|
87
|
+
*/
|
|
88
|
+
get data(): Uint8Array;
|
|
89
|
+
/**
|
|
90
|
+
* Reads an 8-bit unsigned integer.
|
|
91
|
+
*
|
|
92
|
+
* @returns The value (0-255)
|
|
93
|
+
* @throws RangeError if reading past end of buffer
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```typescript
|
|
97
|
+
* const reader = BinaryReader.fromHex('ff');
|
|
98
|
+
* reader.readUInt8(); // 255
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
readUInt8(): number;
|
|
102
|
+
/**
|
|
103
|
+
* Reads an 8-bit signed integer.
|
|
104
|
+
*
|
|
105
|
+
* @returns The value (-128 to 127)
|
|
106
|
+
* @throws RangeError if reading past end of buffer
|
|
107
|
+
*/
|
|
108
|
+
readInt8(): number;
|
|
109
|
+
/**
|
|
110
|
+
* Reads a 16-bit unsigned integer in little-endian format.
|
|
111
|
+
*
|
|
112
|
+
* @returns The value (0-65535)
|
|
113
|
+
* @throws RangeError if reading past end of buffer
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* ```typescript
|
|
117
|
+
* const reader = BinaryReader.fromHex('0100'); // 1 in LE
|
|
118
|
+
* reader.readUInt16LE(); // 1
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
readUInt16LE(): number;
|
|
122
|
+
/**
|
|
123
|
+
* Reads a 16-bit signed integer in little-endian format.
|
|
124
|
+
*
|
|
125
|
+
* @returns The value (-32768 to 32767)
|
|
126
|
+
* @throws RangeError if reading past end of buffer
|
|
127
|
+
*/
|
|
128
|
+
readInt16LE(): number;
|
|
129
|
+
/**
|
|
130
|
+
* Reads a 32-bit unsigned integer in little-endian format.
|
|
131
|
+
*
|
|
132
|
+
* @returns The value (0 to 4294967295)
|
|
133
|
+
* @throws RangeError if reading past end of buffer
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* ```typescript
|
|
137
|
+
* const reader = BinaryReader.fromHex('01000000'); // 1 in LE
|
|
138
|
+
* reader.readUInt32LE(); // 1
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
readUInt32LE(): number;
|
|
142
|
+
/**
|
|
143
|
+
* Reads a 32-bit signed integer in little-endian format.
|
|
144
|
+
*
|
|
145
|
+
* @returns The value (-2147483648 to 2147483647)
|
|
146
|
+
* @throws RangeError if reading past end of buffer
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
* ```typescript
|
|
150
|
+
* const reader = BinaryReader.fromHex('ffffffff'); // -1 in LE signed
|
|
151
|
+
* reader.readInt32LE(); // -1
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
readInt32LE(): number;
|
|
155
|
+
/**
|
|
156
|
+
* Reads a 64-bit unsigned integer in little-endian format as bigint.
|
|
157
|
+
*
|
|
158
|
+
* @returns The value as bigint
|
|
159
|
+
* @throws RangeError if reading past end of buffer
|
|
160
|
+
*
|
|
161
|
+
* @example
|
|
162
|
+
* ```typescript
|
|
163
|
+
* const reader = BinaryReader.fromHex('0100000000000000'); // 1 in LE 64-bit
|
|
164
|
+
* reader.readUInt64LE(); // 1n
|
|
165
|
+
* ```
|
|
166
|
+
*/
|
|
167
|
+
readUInt64LE(): bigint;
|
|
168
|
+
/**
|
|
169
|
+
* Reads a 64-bit signed integer in little-endian format as bigint.
|
|
170
|
+
*
|
|
171
|
+
* @returns The value as bigint
|
|
172
|
+
* @throws RangeError if reading past end of buffer
|
|
173
|
+
*/
|
|
174
|
+
readInt64LE(): bigint;
|
|
175
|
+
/**
|
|
176
|
+
* Reads a specified number of bytes.
|
|
177
|
+
*
|
|
178
|
+
* Returns a subarray view (no copy) for performance.
|
|
179
|
+
* Use readBytesCopy() if you need an independent copy.
|
|
180
|
+
*
|
|
181
|
+
* @param length - Number of bytes to read
|
|
182
|
+
* @returns Uint8Array view into the buffer
|
|
183
|
+
* @throws RangeError if reading past end of buffer
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* ```typescript
|
|
187
|
+
* const reader = BinaryReader.fromHex('deadbeefcafebabe');
|
|
188
|
+
* const first4 = reader.readBytes(4); // deadbeef
|
|
189
|
+
* const next4 = reader.readBytes(4); // cafebabe
|
|
190
|
+
* ```
|
|
191
|
+
*/
|
|
192
|
+
readBytes(length: number): Uint8Array;
|
|
193
|
+
/**
|
|
194
|
+
* Reads a specified number of bytes as an independent copy.
|
|
195
|
+
*
|
|
196
|
+
* @param length - Number of bytes to read
|
|
197
|
+
* @returns New Uint8Array with copied data
|
|
198
|
+
* @throws RangeError if reading past end of buffer
|
|
199
|
+
*/
|
|
200
|
+
readBytesCopy(length: number): Uint8Array;
|
|
201
|
+
/**
|
|
202
|
+
* Reads a Bitcoin CompactSize variable-length integer.
|
|
203
|
+
*
|
|
204
|
+
* CompactSize encoding:
|
|
205
|
+
* - 0x00-0xFC: 1 byte (value as-is)
|
|
206
|
+
* - 0xFD: 3 bytes (0xFD + 2-byte LE uint16)
|
|
207
|
+
* - 0xFE: 5 bytes (0xFE + 4-byte LE uint32)
|
|
208
|
+
* - 0xFF: 9 bytes (0xFF + 8-byte LE uint64)
|
|
209
|
+
*
|
|
210
|
+
* @returns The decoded integer value
|
|
211
|
+
* @throws RangeError if reading past end of buffer
|
|
212
|
+
* @throws RangeError if value exceeds MAX_SAFE_INTEGER
|
|
213
|
+
*
|
|
214
|
+
* @example
|
|
215
|
+
* ```typescript
|
|
216
|
+
* const reader = BinaryReader.fromHex('fd0302'); // 515 encoded
|
|
217
|
+
* reader.readVarInt(); // 515
|
|
218
|
+
* ```
|
|
219
|
+
*/
|
|
220
|
+
readVarInt(): number;
|
|
221
|
+
/**
|
|
222
|
+
* Reads a Bitcoin CompactSize variable-length integer as bigint.
|
|
223
|
+
*
|
|
224
|
+
* Use this when you need the full 64-bit range.
|
|
225
|
+
*
|
|
226
|
+
* @returns The decoded integer value as bigint
|
|
227
|
+
* @throws RangeError if reading past end of buffer
|
|
228
|
+
*/
|
|
229
|
+
readVarIntBig(): bigint;
|
|
230
|
+
/**
|
|
231
|
+
* Reads a length-prefixed byte array (VarInt length + bytes).
|
|
232
|
+
*
|
|
233
|
+
* @returns Uint8Array view into the buffer
|
|
234
|
+
* @throws RangeError if reading past end of buffer
|
|
235
|
+
*
|
|
236
|
+
* @example
|
|
237
|
+
* ```typescript
|
|
238
|
+
* const reader = BinaryReader.fromHex('04deadbeef');
|
|
239
|
+
* reader.readVarBytes(); // Uint8Array [0xde, 0xad, 0xbe, 0xef]
|
|
240
|
+
* ```
|
|
241
|
+
*/
|
|
242
|
+
readVarBytes(): Uint8Array;
|
|
243
|
+
/**
|
|
244
|
+
* Reads an array of length-prefixed byte arrays.
|
|
245
|
+
*
|
|
246
|
+
* Format: VarInt count + (VarInt length + bytes) for each item.
|
|
247
|
+
*
|
|
248
|
+
* @returns Array of Uint8Array views
|
|
249
|
+
* @throws RangeError if reading past end of buffer
|
|
250
|
+
*/
|
|
251
|
+
readVector(): Uint8Array[];
|
|
252
|
+
/**
|
|
253
|
+
* Peeks at the next byte without advancing the position.
|
|
254
|
+
*
|
|
255
|
+
* @returns The next byte value, or undefined if at end
|
|
256
|
+
*/
|
|
257
|
+
peek(): number | undefined;
|
|
258
|
+
/**
|
|
259
|
+
* Skips a specified number of bytes.
|
|
260
|
+
*
|
|
261
|
+
* @param length - Number of bytes to skip
|
|
262
|
+
* @throws RangeError if skipping past end of buffer
|
|
263
|
+
*/
|
|
264
|
+
skip(length: number): void;
|
|
265
|
+
/**
|
|
266
|
+
* Resets the read position to the beginning.
|
|
267
|
+
*/
|
|
268
|
+
reset(): void;
|
|
269
|
+
/**
|
|
270
|
+
* Checks if there are more bytes to read.
|
|
271
|
+
*
|
|
272
|
+
* @returns True if there are remaining bytes
|
|
273
|
+
*/
|
|
274
|
+
hasMore(): boolean;
|
|
275
|
+
}
|
|
276
|
+
//# sourceMappingURL=BinaryReader.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BinaryReader.d.ts","sourceRoot":"","sources":["../../src/io/BinaryReader.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,qBAAa,YAAY;;IAgBrB;;;;;;;;;;;;;OAaG;gBACgB,IAAI,EAAE,UAAU,EAAE,MAAM,GAAE,MAAU;IAMvD;;;;;;;;;;;;;OAaG;WACW,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,YAAY;IAIhD;;OAEG;IACH,IAAW,MAAM,IAAI,MAAM,CAE1B;IAED;;;;;OAKG;IACH,IAAW,MAAM,CAAC,KAAK,EAAE,MAAM,EAK9B;IAED;;OAEG;IACH,IAAW,MAAM,IAAI,MAAM,CAE1B;IAED;;OAEG;IACH,IAAW,SAAS,IAAI,MAAM,CAE7B;IAED;;OAEG;IACH,IAAW,IAAI,IAAI,UAAU,CAE5B;IAED;;;;;;;;;;;OAWG;IACI,SAAS,IAAI,MAAM;IAO1B;;;;;OAKG;IACI,QAAQ,IAAI,MAAM;IAKzB;;;;;;;;;;;OAWG;IACI,YAAY,IAAI,MAAM;IAS7B;;;;;OAKG;IACI,WAAW,IAAI,MAAM;IAS5B;;;;;;;;;;;OAWG;IACI,YAAY,IAAI,MAAM;IAS7B;;;;;;;;;;;OAWG;IACI,WAAW,IAAI,MAAM;IAS5B;;;;;;;;;;;OAWG;IACI,YAAY,IAAI,MAAM;IAS7B;;;;;OAKG;IACI,WAAW,IAAI,MAAM;IAS5B;;;;;;;;;;;;;;;;OAgBG;IACI,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU;IAS5C;;;;;;OAMG;IACI,aAAa,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU;IAShD;;;;;;;;;;;;;;;;;;OAkBG;IACI,UAAU,IAAI,MAAM;IAuB3B;;;;;;;OAOG;IACI,aAAa,IAAI,MAAM;IAmB9B;;;;;;;;;;;OAWG;IACI,YAAY,IAAI,UAAU;IAKjC;;;;;;;OAOG;IACI,UAAU,IAAI,UAAU,EAAE;IASjC;;;;OAIG;IACI,IAAI,IAAI,MAAM,GAAG,SAAS;IAOjC;;;;;OAKG;IACI,IAAI,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAOjC;;OAEG;IACI,KAAK,IAAI,IAAI;IAIpB;;;;OAIG;IACI,OAAO,IAAI,OAAO;CAG5B"}
|
|
@@ -0,0 +1,425 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stateful binary reader with a single DataView instance.
|
|
3
|
+
*
|
|
4
|
+
* Zero allocations during read operations. The DataView is created once
|
|
5
|
+
* in the constructor and reused for all reads.
|
|
6
|
+
*
|
|
7
|
+
* @packageDocumentation
|
|
8
|
+
*/
|
|
9
|
+
import { fromHex } from './hex.js';
|
|
10
|
+
/**
|
|
11
|
+
* High-performance binary reader for parsing binary data.
|
|
12
|
+
*
|
|
13
|
+
* Creates exactly ONE DataView instance that is reused for all read operations.
|
|
14
|
+
* This eliminates garbage collection pressure from repeated DataView allocations.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import { BinaryReader, fromHex } from '@btc-vision/bitcoin';
|
|
19
|
+
*
|
|
20
|
+
* // Parse a Bitcoin transaction
|
|
21
|
+
* const data = fromHex('01000000...');
|
|
22
|
+
* const reader = new BinaryReader(data);
|
|
23
|
+
*
|
|
24
|
+
* const version = reader.readInt32LE();
|
|
25
|
+
* const inputCount = reader.readVarInt();
|
|
26
|
+
*
|
|
27
|
+
* for (let i = 0; i < inputCount; i++) {
|
|
28
|
+
* const txid = reader.readBytes(32);
|
|
29
|
+
* const vout = reader.readUInt32LE();
|
|
30
|
+
* const script = reader.readVarBytes();
|
|
31
|
+
* const sequence = reader.readUInt32LE();
|
|
32
|
+
* }
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export class BinaryReader {
|
|
36
|
+
/**
|
|
37
|
+
* The underlying byte array.
|
|
38
|
+
*/
|
|
39
|
+
#data;
|
|
40
|
+
/**
|
|
41
|
+
* Single DataView instance reused for all reads.
|
|
42
|
+
*/
|
|
43
|
+
#view;
|
|
44
|
+
/**
|
|
45
|
+
* Current read position.
|
|
46
|
+
*/
|
|
47
|
+
#offset;
|
|
48
|
+
/**
|
|
49
|
+
* Creates a new BinaryReader.
|
|
50
|
+
*
|
|
51
|
+
* @param data - The byte array to read from
|
|
52
|
+
* @param offset - Initial read position (default 0)
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```typescript
|
|
56
|
+
* import { BinaryReader, fromHex } from '@btc-vision/bitcoin';
|
|
57
|
+
*
|
|
58
|
+
* const data = fromHex('01020304');
|
|
59
|
+
* const reader = new BinaryReader(data);
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
constructor(data, offset = 0) {
|
|
63
|
+
this.#data = data;
|
|
64
|
+
this.#view = new DataView(data.buffer, data.byteOffset, data.byteLength);
|
|
65
|
+
this.#offset = offset;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Creates a BinaryReader from a hex string.
|
|
69
|
+
*
|
|
70
|
+
* @param hex - Hex string (with or without 0x prefix)
|
|
71
|
+
* @returns A new BinaryReader instance
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```typescript
|
|
75
|
+
* import { BinaryReader } from '@btc-vision/bitcoin';
|
|
76
|
+
*
|
|
77
|
+
* const reader = BinaryReader.fromHex('01000000');
|
|
78
|
+
* const version = reader.readInt32LE(); // 1
|
|
79
|
+
* ```
|
|
80
|
+
*/
|
|
81
|
+
static fromHex(hex) {
|
|
82
|
+
return new BinaryReader(fromHex(hex));
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Current read position in the buffer.
|
|
86
|
+
*/
|
|
87
|
+
get offset() {
|
|
88
|
+
return this.#offset;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Sets the read position.
|
|
92
|
+
*
|
|
93
|
+
* @param value - New offset value
|
|
94
|
+
* @throws RangeError if offset is negative or beyond buffer length
|
|
95
|
+
*/
|
|
96
|
+
set offset(value) {
|
|
97
|
+
if (value < 0 || value > this.#data.length) {
|
|
98
|
+
throw new RangeError(`Offset ${value} is out of bounds [0, ${this.#data.length}]`);
|
|
99
|
+
}
|
|
100
|
+
this.#offset = value;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Total length of the underlying buffer.
|
|
104
|
+
*/
|
|
105
|
+
get length() {
|
|
106
|
+
return this.#data.length;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Number of bytes remaining to be read.
|
|
110
|
+
*/
|
|
111
|
+
get remaining() {
|
|
112
|
+
return this.#data.length - this.#offset;
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* The underlying data buffer.
|
|
116
|
+
*/
|
|
117
|
+
get data() {
|
|
118
|
+
return this.#data;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Reads an 8-bit unsigned integer.
|
|
122
|
+
*
|
|
123
|
+
* @returns The value (0-255)
|
|
124
|
+
* @throws RangeError if reading past end of buffer
|
|
125
|
+
*
|
|
126
|
+
* @example
|
|
127
|
+
* ```typescript
|
|
128
|
+
* const reader = BinaryReader.fromHex('ff');
|
|
129
|
+
* reader.readUInt8(); // 255
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
readUInt8() {
|
|
133
|
+
if (this.#offset >= this.#data.length) {
|
|
134
|
+
throw new RangeError('Read past end of buffer');
|
|
135
|
+
}
|
|
136
|
+
return this.#data[this.#offset++];
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Reads an 8-bit signed integer.
|
|
140
|
+
*
|
|
141
|
+
* @returns The value (-128 to 127)
|
|
142
|
+
* @throws RangeError if reading past end of buffer
|
|
143
|
+
*/
|
|
144
|
+
readInt8() {
|
|
145
|
+
const value = this.readUInt8();
|
|
146
|
+
return value > 127 ? value - 256 : value;
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Reads a 16-bit unsigned integer in little-endian format.
|
|
150
|
+
*
|
|
151
|
+
* @returns The value (0-65535)
|
|
152
|
+
* @throws RangeError if reading past end of buffer
|
|
153
|
+
*
|
|
154
|
+
* @example
|
|
155
|
+
* ```typescript
|
|
156
|
+
* const reader = BinaryReader.fromHex('0100'); // 1 in LE
|
|
157
|
+
* reader.readUInt16LE(); // 1
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
160
|
+
readUInt16LE() {
|
|
161
|
+
if (this.#offset + 2 > this.#data.length) {
|
|
162
|
+
throw new RangeError('Read past end of buffer');
|
|
163
|
+
}
|
|
164
|
+
const value = this.#view.getUint16(this.#offset, true);
|
|
165
|
+
this.#offset += 2;
|
|
166
|
+
return value;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Reads a 16-bit signed integer in little-endian format.
|
|
170
|
+
*
|
|
171
|
+
* @returns The value (-32768 to 32767)
|
|
172
|
+
* @throws RangeError if reading past end of buffer
|
|
173
|
+
*/
|
|
174
|
+
readInt16LE() {
|
|
175
|
+
if (this.#offset + 2 > this.#data.length) {
|
|
176
|
+
throw new RangeError('Read past end of buffer');
|
|
177
|
+
}
|
|
178
|
+
const value = this.#view.getInt16(this.#offset, true);
|
|
179
|
+
this.#offset += 2;
|
|
180
|
+
return value;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Reads a 32-bit unsigned integer in little-endian format.
|
|
184
|
+
*
|
|
185
|
+
* @returns The value (0 to 4294967295)
|
|
186
|
+
* @throws RangeError if reading past end of buffer
|
|
187
|
+
*
|
|
188
|
+
* @example
|
|
189
|
+
* ```typescript
|
|
190
|
+
* const reader = BinaryReader.fromHex('01000000'); // 1 in LE
|
|
191
|
+
* reader.readUInt32LE(); // 1
|
|
192
|
+
* ```
|
|
193
|
+
*/
|
|
194
|
+
readUInt32LE() {
|
|
195
|
+
if (this.#offset + 4 > this.#data.length) {
|
|
196
|
+
throw new RangeError('Read past end of buffer');
|
|
197
|
+
}
|
|
198
|
+
const value = this.#view.getUint32(this.#offset, true);
|
|
199
|
+
this.#offset += 4;
|
|
200
|
+
return value;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Reads a 32-bit signed integer in little-endian format.
|
|
204
|
+
*
|
|
205
|
+
* @returns The value (-2147483648 to 2147483647)
|
|
206
|
+
* @throws RangeError if reading past end of buffer
|
|
207
|
+
*
|
|
208
|
+
* @example
|
|
209
|
+
* ```typescript
|
|
210
|
+
* const reader = BinaryReader.fromHex('ffffffff'); // -1 in LE signed
|
|
211
|
+
* reader.readInt32LE(); // -1
|
|
212
|
+
* ```
|
|
213
|
+
*/
|
|
214
|
+
readInt32LE() {
|
|
215
|
+
if (this.#offset + 4 > this.#data.length) {
|
|
216
|
+
throw new RangeError('Read past end of buffer');
|
|
217
|
+
}
|
|
218
|
+
const value = this.#view.getInt32(this.#offset, true);
|
|
219
|
+
this.#offset += 4;
|
|
220
|
+
return value;
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Reads a 64-bit unsigned integer in little-endian format as bigint.
|
|
224
|
+
*
|
|
225
|
+
* @returns The value as bigint
|
|
226
|
+
* @throws RangeError if reading past end of buffer
|
|
227
|
+
*
|
|
228
|
+
* @example
|
|
229
|
+
* ```typescript
|
|
230
|
+
* const reader = BinaryReader.fromHex('0100000000000000'); // 1 in LE 64-bit
|
|
231
|
+
* reader.readUInt64LE(); // 1n
|
|
232
|
+
* ```
|
|
233
|
+
*/
|
|
234
|
+
readUInt64LE() {
|
|
235
|
+
if (this.#offset + 8 > this.#data.length) {
|
|
236
|
+
throw new RangeError('Read past end of buffer');
|
|
237
|
+
}
|
|
238
|
+
const value = this.#view.getBigUint64(this.#offset, true);
|
|
239
|
+
this.#offset += 8;
|
|
240
|
+
return value;
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Reads a 64-bit signed integer in little-endian format as bigint.
|
|
244
|
+
*
|
|
245
|
+
* @returns The value as bigint
|
|
246
|
+
* @throws RangeError if reading past end of buffer
|
|
247
|
+
*/
|
|
248
|
+
readInt64LE() {
|
|
249
|
+
if (this.#offset + 8 > this.#data.length) {
|
|
250
|
+
throw new RangeError('Read past end of buffer');
|
|
251
|
+
}
|
|
252
|
+
const value = this.#view.getBigInt64(this.#offset, true);
|
|
253
|
+
this.#offset += 8;
|
|
254
|
+
return value;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Reads a specified number of bytes.
|
|
258
|
+
*
|
|
259
|
+
* Returns a subarray view (no copy) for performance.
|
|
260
|
+
* Use readBytesCopy() if you need an independent copy.
|
|
261
|
+
*
|
|
262
|
+
* @param length - Number of bytes to read
|
|
263
|
+
* @returns Uint8Array view into the buffer
|
|
264
|
+
* @throws RangeError if reading past end of buffer
|
|
265
|
+
*
|
|
266
|
+
* @example
|
|
267
|
+
* ```typescript
|
|
268
|
+
* const reader = BinaryReader.fromHex('deadbeefcafebabe');
|
|
269
|
+
* const first4 = reader.readBytes(4); // deadbeef
|
|
270
|
+
* const next4 = reader.readBytes(4); // cafebabe
|
|
271
|
+
* ```
|
|
272
|
+
*/
|
|
273
|
+
readBytes(length) {
|
|
274
|
+
if (this.#offset + length > this.#data.length) {
|
|
275
|
+
throw new RangeError('Read past end of buffer');
|
|
276
|
+
}
|
|
277
|
+
const value = this.#data.subarray(this.#offset, this.#offset + length);
|
|
278
|
+
this.#offset += length;
|
|
279
|
+
return value;
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Reads a specified number of bytes as an independent copy.
|
|
283
|
+
*
|
|
284
|
+
* @param length - Number of bytes to read
|
|
285
|
+
* @returns New Uint8Array with copied data
|
|
286
|
+
* @throws RangeError if reading past end of buffer
|
|
287
|
+
*/
|
|
288
|
+
readBytesCopy(length) {
|
|
289
|
+
if (this.#offset + length > this.#data.length) {
|
|
290
|
+
throw new RangeError('Read past end of buffer');
|
|
291
|
+
}
|
|
292
|
+
const value = this.#data.slice(this.#offset, this.#offset + length);
|
|
293
|
+
this.#offset += length;
|
|
294
|
+
return value;
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Reads a Bitcoin CompactSize variable-length integer.
|
|
298
|
+
*
|
|
299
|
+
* CompactSize encoding:
|
|
300
|
+
* - 0x00-0xFC: 1 byte (value as-is)
|
|
301
|
+
* - 0xFD: 3 bytes (0xFD + 2-byte LE uint16)
|
|
302
|
+
* - 0xFE: 5 bytes (0xFE + 4-byte LE uint32)
|
|
303
|
+
* - 0xFF: 9 bytes (0xFF + 8-byte LE uint64)
|
|
304
|
+
*
|
|
305
|
+
* @returns The decoded integer value
|
|
306
|
+
* @throws RangeError if reading past end of buffer
|
|
307
|
+
* @throws RangeError if value exceeds MAX_SAFE_INTEGER
|
|
308
|
+
*
|
|
309
|
+
* @example
|
|
310
|
+
* ```typescript
|
|
311
|
+
* const reader = BinaryReader.fromHex('fd0302'); // 515 encoded
|
|
312
|
+
* reader.readVarInt(); // 515
|
|
313
|
+
* ```
|
|
314
|
+
*/
|
|
315
|
+
readVarInt() {
|
|
316
|
+
const first = this.readUInt8();
|
|
317
|
+
if (first < 0xfd) {
|
|
318
|
+
return first;
|
|
319
|
+
}
|
|
320
|
+
if (first === 0xfd) {
|
|
321
|
+
return this.readUInt16LE();
|
|
322
|
+
}
|
|
323
|
+
if (first === 0xfe) {
|
|
324
|
+
return this.readUInt32LE();
|
|
325
|
+
}
|
|
326
|
+
// first === 0xff
|
|
327
|
+
const value = this.readUInt64LE();
|
|
328
|
+
if (value > BigInt(Number.MAX_SAFE_INTEGER)) {
|
|
329
|
+
throw new RangeError('VarInt value exceeds MAX_SAFE_INTEGER');
|
|
330
|
+
}
|
|
331
|
+
return Number(value);
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Reads a Bitcoin CompactSize variable-length integer as bigint.
|
|
335
|
+
*
|
|
336
|
+
* Use this when you need the full 64-bit range.
|
|
337
|
+
*
|
|
338
|
+
* @returns The decoded integer value as bigint
|
|
339
|
+
* @throws RangeError if reading past end of buffer
|
|
340
|
+
*/
|
|
341
|
+
readVarIntBig() {
|
|
342
|
+
const first = this.readUInt8();
|
|
343
|
+
if (first < 0xfd) {
|
|
344
|
+
return BigInt(first);
|
|
345
|
+
}
|
|
346
|
+
if (first === 0xfd) {
|
|
347
|
+
return BigInt(this.readUInt16LE());
|
|
348
|
+
}
|
|
349
|
+
if (first === 0xfe) {
|
|
350
|
+
return BigInt(this.readUInt32LE());
|
|
351
|
+
}
|
|
352
|
+
// first === 0xff
|
|
353
|
+
return this.readUInt64LE();
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* Reads a length-prefixed byte array (VarInt length + bytes).
|
|
357
|
+
*
|
|
358
|
+
* @returns Uint8Array view into the buffer
|
|
359
|
+
* @throws RangeError if reading past end of buffer
|
|
360
|
+
*
|
|
361
|
+
* @example
|
|
362
|
+
* ```typescript
|
|
363
|
+
* const reader = BinaryReader.fromHex('04deadbeef');
|
|
364
|
+
* reader.readVarBytes(); // Uint8Array [0xde, 0xad, 0xbe, 0xef]
|
|
365
|
+
* ```
|
|
366
|
+
*/
|
|
367
|
+
readVarBytes() {
|
|
368
|
+
const length = this.readVarInt();
|
|
369
|
+
return this.readBytes(length);
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Reads an array of length-prefixed byte arrays.
|
|
373
|
+
*
|
|
374
|
+
* Format: VarInt count + (VarInt length + bytes) for each item.
|
|
375
|
+
*
|
|
376
|
+
* @returns Array of Uint8Array views
|
|
377
|
+
* @throws RangeError if reading past end of buffer
|
|
378
|
+
*/
|
|
379
|
+
readVector() {
|
|
380
|
+
const count = this.readVarInt();
|
|
381
|
+
const result = [];
|
|
382
|
+
for (let i = 0; i < count; i++) {
|
|
383
|
+
result.push(this.readVarBytes());
|
|
384
|
+
}
|
|
385
|
+
return result;
|
|
386
|
+
}
|
|
387
|
+
/**
|
|
388
|
+
* Peeks at the next byte without advancing the position.
|
|
389
|
+
*
|
|
390
|
+
* @returns The next byte value, or undefined if at end
|
|
391
|
+
*/
|
|
392
|
+
peek() {
|
|
393
|
+
if (this.#offset >= this.#data.length) {
|
|
394
|
+
return undefined;
|
|
395
|
+
}
|
|
396
|
+
return this.#data[this.#offset];
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* Skips a specified number of bytes.
|
|
400
|
+
*
|
|
401
|
+
* @param length - Number of bytes to skip
|
|
402
|
+
* @throws RangeError if skipping past end of buffer
|
|
403
|
+
*/
|
|
404
|
+
skip(length) {
|
|
405
|
+
if (this.#offset + length > this.#data.length) {
|
|
406
|
+
throw new RangeError('Skip past end of buffer');
|
|
407
|
+
}
|
|
408
|
+
this.#offset += length;
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* Resets the read position to the beginning.
|
|
412
|
+
*/
|
|
413
|
+
reset() {
|
|
414
|
+
this.#offset = 0;
|
|
415
|
+
}
|
|
416
|
+
/**
|
|
417
|
+
* Checks if there are more bytes to read.
|
|
418
|
+
*
|
|
419
|
+
* @returns True if there are remaining bytes
|
|
420
|
+
*/
|
|
421
|
+
hasMore() {
|
|
422
|
+
return this.#offset < this.#data.length;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
//# sourceMappingURL=BinaryReader.js.map
|