@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/address.ts
CHANGED
|
@@ -9,9 +9,10 @@
|
|
|
9
9
|
*/
|
|
10
10
|
import { bech32, bech32m } from 'bech32';
|
|
11
11
|
import * as bs58check from 'bs58check';
|
|
12
|
-
import {
|
|
12
|
+
import { type Bech32Result, fromBech32 } from './bech32utils.js';
|
|
13
|
+
import { alloc } from './io/index.js';
|
|
14
|
+
import type { Network } from './networks.js';
|
|
13
15
|
import * as networks from './networks.js';
|
|
14
|
-
import { Network } from './networks.js';
|
|
15
16
|
import { p2op } from './payments/p2op.js';
|
|
16
17
|
import { p2pkh } from './payments/p2pkh.js';
|
|
17
18
|
import { p2sh } from './payments/p2sh.js';
|
|
@@ -20,16 +21,24 @@ import { p2wpkh } from './payments/p2wpkh.js';
|
|
|
20
21
|
import { p2wsh } from './payments/p2wsh.js';
|
|
21
22
|
import * as bscript from './script.js';
|
|
22
23
|
import { opcodes } from './script.js';
|
|
23
|
-
import {
|
|
24
|
+
import {
|
|
25
|
+
type Bytes20,
|
|
26
|
+
isBytes20,
|
|
27
|
+
isUInt8,
|
|
28
|
+
type Script,
|
|
29
|
+
toBytes20,
|
|
30
|
+
toBytes32,
|
|
31
|
+
type XOnlyPublicKey,
|
|
32
|
+
} from './types.js';
|
|
24
33
|
|
|
25
34
|
export { fromBech32, type Bech32Result };
|
|
26
35
|
|
|
27
36
|
/** base58check decode result */
|
|
28
37
|
export interface Base58CheckResult {
|
|
29
38
|
/** address hash */
|
|
30
|
-
hash:
|
|
39
|
+
readonly hash: Bytes20;
|
|
31
40
|
/** address version: 0x00 for P2PKH, 0x05 for P2SH */
|
|
32
|
-
version: number;
|
|
41
|
+
readonly version: number;
|
|
33
42
|
}
|
|
34
43
|
|
|
35
44
|
export const FUTURE_SEGWIT_MAX_SIZE: number = 40;
|
|
@@ -45,14 +54,14 @@ const FUTURE_SEGWIT_VERSION_WARNING: string =
|
|
|
45
54
|
'with caution. Wallets should verify the segwit version from the output of fromBech32, ' +
|
|
46
55
|
'then decide when it is safe to use which version of segwit.';
|
|
47
56
|
|
|
48
|
-
export const isUnknownSegwitVersion = (output:
|
|
57
|
+
export const isUnknownSegwitVersion = (output: Uint8Array): boolean => {
|
|
49
58
|
try {
|
|
50
|
-
const data =
|
|
59
|
+
const data = output.subarray(2);
|
|
51
60
|
if (data.length < FUTURE_SEGWIT_MIN_SIZE || data.length > FUTURE_SEGWIT_MAX_SIZE) {
|
|
52
61
|
throw new TypeError('Invalid program length for segwit address');
|
|
53
62
|
}
|
|
54
63
|
|
|
55
|
-
const version = output[0] - FUTURE_SEGWIT_VERSION_DIFF;
|
|
64
|
+
const version = output[0]! - FUTURE_SEGWIT_VERSION_DIFF;
|
|
56
65
|
if (version < FUTURE_SEGWIT_MIN_VERSION || version > FUTURE_SEGWIT_MAX_VERSION + 1) {
|
|
57
66
|
throw new TypeError('Invalid version for segwit address');
|
|
58
67
|
}
|
|
@@ -73,8 +82,8 @@ export const isUnknownSegwitVersion = (output: Buffer): boolean => {
|
|
|
73
82
|
* @param network - Network object containing bech32 and optional bech32Opnet prefix
|
|
74
83
|
* @returns Bech32m-encoded future Taproot-style address
|
|
75
84
|
*/
|
|
76
|
-
export function toFutureOPNetAddress(output:
|
|
77
|
-
if (!
|
|
85
|
+
export function toFutureOPNetAddress(output: Uint8Array, network: Network): string {
|
|
86
|
+
if (!(output instanceof Uint8Array)) throw new TypeError('output must be a Uint8Array');
|
|
78
87
|
if (!network.bech32Opnet) throw new Error('Network does not support opnet');
|
|
79
88
|
|
|
80
89
|
const opcode = output[0];
|
|
@@ -82,17 +91,17 @@ export function toFutureOPNetAddress(output: Buffer, network: Network): string {
|
|
|
82
91
|
// work out where the push-data really starts
|
|
83
92
|
let pushPos = 1,
|
|
84
93
|
progLen: number;
|
|
85
|
-
if (output[1] < 0x4c) {
|
|
86
|
-
progLen = output[1]
|
|
94
|
+
if (output[1]! < 0x4c) {
|
|
95
|
+
progLen = output[1]!;
|
|
87
96
|
pushPos = 2;
|
|
88
97
|
} else if (output[1] === 0x4c) {
|
|
89
|
-
progLen = output[2]
|
|
98
|
+
progLen = output[2]!;
|
|
90
99
|
pushPos = 3;
|
|
91
100
|
} else {
|
|
92
101
|
throw new TypeError('Unsupported push opcode in script');
|
|
93
102
|
}
|
|
94
103
|
|
|
95
|
-
const program =
|
|
104
|
+
const program = output.subarray(pushPos, pushPos + progLen);
|
|
96
105
|
|
|
97
106
|
if (program.length < FUTURE_SEGWIT_MIN_SIZE || program.length > FUTURE_SEGWIT_MAX_SIZE)
|
|
98
107
|
throw new TypeError('Invalid program length for segwit address');
|
|
@@ -100,8 +109,8 @@ export function toFutureOPNetAddress(output: Buffer, network: Network): string {
|
|
|
100
109
|
const version =
|
|
101
110
|
opcode === opcodes.OP_0
|
|
102
111
|
? 0
|
|
103
|
-
: opcode >= opcodes.OP_1 && opcode <= opcodes.OP_16
|
|
104
|
-
? opcode - (opcodes.OP_1 - 1)
|
|
112
|
+
: opcode! >= opcodes.OP_1 && opcode! <= opcodes.OP_16
|
|
113
|
+
? opcode! - (opcodes.OP_1 - 1)
|
|
105
114
|
: -1;
|
|
106
115
|
|
|
107
116
|
if (version < FUTURE_SEGWIT_MAX_VERSION || version > FUTURE_MAX_VERSION)
|
|
@@ -111,13 +120,13 @@ export function toFutureOPNetAddress(output: Buffer, network: Network): string {
|
|
|
111
120
|
return bech32m.encode(network.bech32Opnet, words);
|
|
112
121
|
}
|
|
113
122
|
|
|
114
|
-
export function _toFutureSegwitAddress(output:
|
|
115
|
-
const data =
|
|
123
|
+
export function _toFutureSegwitAddress(output: Uint8Array, network: Network): string {
|
|
124
|
+
const data = output.subarray(2);
|
|
116
125
|
if (data.length < FUTURE_SEGWIT_MIN_SIZE || data.length > FUTURE_SEGWIT_MAX_SIZE) {
|
|
117
126
|
throw new TypeError('Invalid program length for segwit address');
|
|
118
127
|
}
|
|
119
128
|
|
|
120
|
-
const version = output[0] - FUTURE_SEGWIT_VERSION_DIFF;
|
|
129
|
+
const version = output[0]! - FUTURE_SEGWIT_VERSION_DIFF;
|
|
121
130
|
if (version < FUTURE_SEGWIT_MIN_VERSION || version > FUTURE_SEGWIT_MAX_VERSION) {
|
|
122
131
|
throw new TypeError('Invalid version for segwit address');
|
|
123
132
|
}
|
|
@@ -133,14 +142,14 @@ export function _toFutureSegwitAddress(output: Buffer, network: Network): string
|
|
|
133
142
|
* decode address with base58 specification, return address version and address hash if valid
|
|
134
143
|
*/
|
|
135
144
|
export function fromBase58Check(address: string): Base58CheckResult {
|
|
136
|
-
const payload =
|
|
145
|
+
const payload = new Uint8Array(bs58check.default.decode(address));
|
|
137
146
|
|
|
138
147
|
// TODO: 4.0.0, move to "toOutputScript"
|
|
139
148
|
if (payload.length < 21) throw new TypeError(address + ' is too short');
|
|
140
149
|
if (payload.length > 21) throw new TypeError(address + ' is too long');
|
|
141
150
|
|
|
142
|
-
const version = payload
|
|
143
|
-
const hash =
|
|
151
|
+
const version = payload[0]!;
|
|
152
|
+
const hash = payload.subarray(1) as Bytes20;
|
|
144
153
|
|
|
145
154
|
return { version, hash };
|
|
146
155
|
}
|
|
@@ -148,12 +157,13 @@ export function fromBase58Check(address: string): Base58CheckResult {
|
|
|
148
157
|
/**
|
|
149
158
|
* encode address hash to base58 address with version
|
|
150
159
|
*/
|
|
151
|
-
export function toBase58Check(hash:
|
|
152
|
-
|
|
160
|
+
export function toBase58Check(hash: Bytes20, version: number): string {
|
|
161
|
+
if (!isBytes20(hash)) throw new TypeError('Expected 20 bytes hash');
|
|
162
|
+
if (!isUInt8(version)) throw new TypeError('Expected UInt8 version');
|
|
153
163
|
|
|
154
|
-
const payload =
|
|
155
|
-
payload
|
|
156
|
-
|
|
164
|
+
const payload = alloc(21);
|
|
165
|
+
payload[0] = version;
|
|
166
|
+
payload.set(hash, 1);
|
|
157
167
|
|
|
158
168
|
return bs58check.default.encode(payload);
|
|
159
169
|
}
|
|
@@ -162,7 +172,7 @@ export function toBase58Check(hash: Buffer, version: number): string {
|
|
|
162
172
|
* encode address hash to bech32 address with version and prefix
|
|
163
173
|
*/
|
|
164
174
|
export function toBech32(
|
|
165
|
-
data:
|
|
175
|
+
data: Uint8Array,
|
|
166
176
|
version: number,
|
|
167
177
|
prefix: string,
|
|
168
178
|
prefixOpnet?: string,
|
|
@@ -180,24 +190,24 @@ export function toBech32(
|
|
|
180
190
|
/**
|
|
181
191
|
* decode address from output script with network, return address if matched
|
|
182
192
|
*/
|
|
183
|
-
export function fromOutputScript(output:
|
|
193
|
+
export function fromOutputScript(output: Uint8Array, network?: Network): string {
|
|
184
194
|
// TODO: Network
|
|
185
195
|
network = network || networks.bitcoin;
|
|
186
196
|
|
|
187
197
|
try {
|
|
188
|
-
return p2pkh({ output, network }).address as string;
|
|
198
|
+
return p2pkh({ output: output as Script, network }).address as string;
|
|
189
199
|
} catch (e) {}
|
|
190
200
|
try {
|
|
191
|
-
return p2sh({ output, network }).address as string;
|
|
201
|
+
return p2sh({ output: output as Script, network }).address as string;
|
|
192
202
|
} catch (e) {}
|
|
193
203
|
try {
|
|
194
|
-
return p2wpkh({ output, network }).address as string;
|
|
204
|
+
return p2wpkh({ output: output as Script, network }).address as string;
|
|
195
205
|
} catch (e) {}
|
|
196
206
|
try {
|
|
197
|
-
return p2wsh({ output, network }).address as string;
|
|
207
|
+
return p2wsh({ output: output as Script, network }).address as string;
|
|
198
208
|
} catch (e) {}
|
|
199
209
|
try {
|
|
200
|
-
return p2tr({ output, network }).address as string;
|
|
210
|
+
return p2tr({ output: output as Script, network }).address as string;
|
|
201
211
|
} catch (e) {}
|
|
202
212
|
try {
|
|
203
213
|
return toFutureOPNetAddress(output, network);
|
|
@@ -210,10 +220,45 @@ export function fromOutputScript(output: Buffer, network?: Network): string {
|
|
|
210
220
|
}
|
|
211
221
|
|
|
212
222
|
/**
|
|
213
|
-
*
|
|
223
|
+
* Options for toOutputScript function.
|
|
214
224
|
*/
|
|
215
|
-
export
|
|
216
|
-
|
|
225
|
+
export interface ToOutputScriptOptions {
|
|
226
|
+
/**
|
|
227
|
+
* Network to use for encoding. Defaults to bitcoin mainnet.
|
|
228
|
+
*/
|
|
229
|
+
readonly network?: Network;
|
|
230
|
+
/**
|
|
231
|
+
* Optional callback for future segwit version warnings.
|
|
232
|
+
* If provided, called with FUTURE_SEGWIT_VERSION_WARNING when encoding
|
|
233
|
+
* to a future segwit version (v2-v15) address.
|
|
234
|
+
* If not provided, no warning is emitted.
|
|
235
|
+
*/
|
|
236
|
+
readonly onFutureSegwitWarning?: (warning: string) => void;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Encodes address to output script with network, return output script if address matched.
|
|
241
|
+
* @param address - The address to encode
|
|
242
|
+
* @param networkOrOptions - Network or options object
|
|
243
|
+
* @returns The output script as Uint8Array
|
|
244
|
+
*/
|
|
245
|
+
export function toOutputScript(
|
|
246
|
+
address: string,
|
|
247
|
+
networkOrOptions?: Network | ToOutputScriptOptions,
|
|
248
|
+
): Uint8Array {
|
|
249
|
+
let network: Network;
|
|
250
|
+
let onFutureSegwitWarning: ((warning: string) => void) | undefined;
|
|
251
|
+
|
|
252
|
+
if (networkOrOptions && 'bech32' in networkOrOptions) {
|
|
253
|
+
// It's a Network object
|
|
254
|
+
network = networkOrOptions;
|
|
255
|
+
} else if (networkOrOptions && typeof networkOrOptions === 'object') {
|
|
256
|
+
// It's an options object
|
|
257
|
+
network = networkOrOptions.network || networks.bitcoin;
|
|
258
|
+
onFutureSegwitWarning = networkOrOptions.onFutureSegwitWarning;
|
|
259
|
+
} else {
|
|
260
|
+
network = networks.bitcoin;
|
|
261
|
+
}
|
|
217
262
|
|
|
218
263
|
let decodeBase58: Base58CheckResult | undefined;
|
|
219
264
|
let decodeBech32: Bech32Result | undefined;
|
|
@@ -223,9 +268,9 @@ export function toOutputScript(address: string, network?: Network): Buffer {
|
|
|
223
268
|
|
|
224
269
|
if (decodeBase58) {
|
|
225
270
|
if (decodeBase58.version === network.pubKeyHash)
|
|
226
|
-
return p2pkh({ hash: decodeBase58.hash }).output as
|
|
271
|
+
return p2pkh({ hash: decodeBase58.hash }).output as Uint8Array;
|
|
227
272
|
if (decodeBase58.version === network.scriptHash)
|
|
228
|
-
return p2sh({ hash: decodeBase58.hash }).output as
|
|
273
|
+
return p2sh({ hash: decodeBase58.hash }).output as Uint8Array;
|
|
229
274
|
} else {
|
|
230
275
|
try {
|
|
231
276
|
decodeBech32 = fromBech32(address);
|
|
@@ -240,26 +285,27 @@ export function toOutputScript(address: string, network?: Network): Buffer {
|
|
|
240
285
|
throw new Error(address + ' has an invalid prefix');
|
|
241
286
|
if (decodeBech32.version === 0) {
|
|
242
287
|
if (decodeBech32.data.length === 20)
|
|
243
|
-
return p2wpkh({ hash: decodeBech32.data }).output as
|
|
288
|
+
return p2wpkh({ hash: toBytes20(decodeBech32.data) }).output as Uint8Array;
|
|
244
289
|
if (decodeBech32.data.length === 32)
|
|
245
|
-
return p2wsh({ hash: decodeBech32.data }).output as
|
|
290
|
+
return p2wsh({ hash: toBytes32(decodeBech32.data) }).output as Uint8Array;
|
|
246
291
|
} else if (decodeBech32.version === 1) {
|
|
247
292
|
if (decodeBech32.data.length === 32)
|
|
248
|
-
return p2tr({ pubkey: decodeBech32.data })
|
|
293
|
+
return p2tr({ pubkey: decodeBech32.data as XOnlyPublicKey })
|
|
294
|
+
.output as Uint8Array;
|
|
249
295
|
} else if (decodeBech32.version === FUTURE_OPNET_VERSION) {
|
|
250
296
|
if (!network.bech32Opnet) throw new Error(address + ' has an invalid prefix');
|
|
251
297
|
return p2op({
|
|
252
298
|
program: decodeBech32.data,
|
|
253
299
|
network,
|
|
254
|
-
}).output as
|
|
300
|
+
}).output as Uint8Array;
|
|
255
301
|
} else if (
|
|
256
302
|
decodeBech32.version >= FUTURE_SEGWIT_MIN_VERSION &&
|
|
257
303
|
decodeBech32.version <= FUTURE_SEGWIT_MAX_VERSION &&
|
|
258
304
|
decodeBech32.data.length >= FUTURE_SEGWIT_MIN_SIZE &&
|
|
259
305
|
decodeBech32.data.length <= FUTURE_SEGWIT_MAX_SIZE
|
|
260
306
|
) {
|
|
261
|
-
if (decodeBech32.version !== FUTURE_OPNET_VERSION) {
|
|
262
|
-
|
|
307
|
+
if (decodeBech32.version !== FUTURE_OPNET_VERSION && onFutureSegwitWarning) {
|
|
308
|
+
onFutureSegwitWarning(FUTURE_SEGWIT_VERSION_WARNING);
|
|
263
309
|
}
|
|
264
310
|
|
|
265
311
|
return bscript.compile([
|
package/src/bech32utils.ts
CHANGED
|
@@ -11,7 +11,7 @@ export interface Bech32Result {
|
|
|
11
11
|
/** address prefix: bc for P2WPKH、P2WSH、P2TR */
|
|
12
12
|
prefix: string;
|
|
13
13
|
/** address data:20 bytes for P2WPKH, 32 bytes for P2WSH、P2TR */
|
|
14
|
-
data:
|
|
14
|
+
data: Uint8Array;
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
/**
|
|
@@ -36,8 +36,8 @@ export function fromBech32(address: string): Bech32Result {
|
|
|
36
36
|
const data = bech32.fromWords(result.words.slice(1));
|
|
37
37
|
|
|
38
38
|
return {
|
|
39
|
-
version
|
|
39
|
+
version: version!,
|
|
40
40
|
prefix: result.prefix,
|
|
41
|
-
data:
|
|
41
|
+
data: new Uint8Array(data),
|
|
42
42
|
};
|
|
43
43
|
}
|
package/src/bip66.ts
CHANGED
|
@@ -2,52 +2,62 @@
|
|
|
2
2
|
// Format: 0x30 [total-length] 0x02 [R-length] [R] 0x02 [S-length] [S]
|
|
3
3
|
// NOTE: SIGHASH byte ignored AND restricted, truncate before use
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
import { alloc } from './io/index.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* DER-encoded signature components.
|
|
9
|
+
*/
|
|
10
|
+
export interface DerSignature {
|
|
11
|
+
readonly r: Uint8Array;
|
|
12
|
+
readonly s: Uint8Array;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function check(buffer: Uint8Array): boolean {
|
|
6
16
|
if (buffer.length < 8) return false;
|
|
7
17
|
if (buffer.length > 72) return false;
|
|
8
18
|
if (buffer[0] !== 0x30) return false;
|
|
9
19
|
if (buffer[1] !== buffer.length - 2) return false;
|
|
10
20
|
if (buffer[2] !== 0x02) return false;
|
|
11
21
|
|
|
12
|
-
const lenR = buffer[3]
|
|
22
|
+
const lenR = buffer[3]!;
|
|
13
23
|
if (lenR === 0) return false;
|
|
14
24
|
if (5 + lenR >= buffer.length) return false;
|
|
15
|
-
if (buffer[4 + lenR] !== 0x02) return false;
|
|
25
|
+
if (buffer[4 + lenR]! !== 0x02) return false;
|
|
16
26
|
|
|
17
|
-
const lenS = buffer[5 + lenR]
|
|
27
|
+
const lenS = buffer[5 + lenR]!;
|
|
18
28
|
if (lenS === 0) return false;
|
|
19
29
|
if (6 + lenR + lenS !== buffer.length) return false;
|
|
20
30
|
|
|
21
|
-
if (buffer[4] & 0x80) return false;
|
|
22
|
-
if (lenR > 1 && buffer[4] === 0x00 && !(buffer[5] & 0x80)) return false;
|
|
31
|
+
if (buffer[4]! & 0x80) return false;
|
|
32
|
+
if (lenR > 1 && buffer[4] === 0x00 && !(buffer[5]! & 0x80)) return false;
|
|
23
33
|
|
|
24
|
-
if (buffer[lenR + 6] & 0x80) return false;
|
|
25
|
-
if (lenS > 1 && buffer[lenR + 6] === 0x00 && !(buffer[lenR + 7] & 0x80)) return false;
|
|
34
|
+
if (buffer[lenR + 6]! & 0x80) return false;
|
|
35
|
+
if (lenS > 1 && buffer[lenR + 6] === 0x00 && !(buffer[lenR + 7]! & 0x80)) return false;
|
|
26
36
|
return true;
|
|
27
37
|
}
|
|
28
38
|
|
|
29
|
-
export function decode(buffer:
|
|
39
|
+
export function decode(buffer: Uint8Array): DerSignature {
|
|
30
40
|
if (buffer.length < 8) throw new Error('DER sequence length is too short');
|
|
31
41
|
if (buffer.length > 72) throw new Error('DER sequence length is too long');
|
|
32
42
|
if (buffer[0] !== 0x30) throw new Error('Expected DER sequence');
|
|
33
43
|
if (buffer[1] !== buffer.length - 2) throw new Error('DER sequence length is invalid');
|
|
34
44
|
if (buffer[2] !== 0x02) throw new Error('Expected DER integer');
|
|
35
45
|
|
|
36
|
-
const lenR = buffer[3]
|
|
46
|
+
const lenR = buffer[3]!;
|
|
37
47
|
if (lenR === 0) throw new Error('R length is zero');
|
|
38
48
|
if (5 + lenR >= buffer.length) throw new Error('R length is too long');
|
|
39
|
-
if (buffer[4 + lenR] !== 0x02) throw new Error('Expected DER integer (2)');
|
|
49
|
+
if (buffer[4 + lenR]! !== 0x02) throw new Error('Expected DER integer (2)');
|
|
40
50
|
|
|
41
|
-
const lenS = buffer[5 + lenR]
|
|
51
|
+
const lenS = buffer[5 + lenR]!;
|
|
42
52
|
if (lenS === 0) throw new Error('S length is zero');
|
|
43
53
|
if (6 + lenR + lenS !== buffer.length) throw new Error('S length is invalid');
|
|
44
54
|
|
|
45
|
-
if (buffer[4] & 0x80) throw new Error('R value is negative');
|
|
46
|
-
if (lenR > 1 && buffer[4] === 0x00 && !(buffer[5] & 0x80))
|
|
55
|
+
if (buffer[4]! & 0x80) throw new Error('R value is negative');
|
|
56
|
+
if (lenR > 1 && buffer[4] === 0x00 && !(buffer[5]! & 0x80))
|
|
47
57
|
throw new Error('R value excessively padded');
|
|
48
58
|
|
|
49
|
-
if (buffer[lenR + 6] & 0x80) throw new Error('S value is negative');
|
|
50
|
-
if (lenS > 1 && buffer[lenR + 6] === 0x00 && !(buffer[lenR + 7] & 0x80))
|
|
59
|
+
if (buffer[lenR + 6]! & 0x80) throw new Error('S value is negative');
|
|
60
|
+
if (lenS > 1 && buffer[lenR + 6] === 0x00 && !(buffer[lenR + 7]! & 0x80))
|
|
51
61
|
throw new Error('S value excessively padded');
|
|
52
62
|
|
|
53
63
|
// non-BIP66 - extract R, S values
|
|
@@ -79,29 +89,29 @@ export function decode(buffer: Buffer): { r: Buffer; s: Buffer } {
|
|
|
79
89
|
* 62300 => 0x00f35c
|
|
80
90
|
* -62300 => 0xff0ca4
|
|
81
91
|
*/
|
|
82
|
-
export function encode(r:
|
|
92
|
+
export function encode(r: Uint8Array, s: Uint8Array): Uint8Array {
|
|
83
93
|
const lenR = r.length;
|
|
84
94
|
const lenS = s.length;
|
|
85
95
|
if (lenR === 0) throw new Error('R length is zero');
|
|
86
96
|
if (lenS === 0) throw new Error('S length is zero');
|
|
87
97
|
if (lenR > 33) throw new Error('R length is too long');
|
|
88
98
|
if (lenS > 33) throw new Error('S length is too long');
|
|
89
|
-
if (r[0] & 0x80) throw new Error('R value is negative');
|
|
90
|
-
if (s[0] & 0x80) throw new Error('S value is negative');
|
|
91
|
-
if (lenR > 1 && r[0] === 0x00 && !(r[1] & 0x80)) throw new Error('R value excessively padded');
|
|
92
|
-
if (lenS > 1 && s[0] === 0x00 && !(s[1] & 0x80)) throw new Error('S value excessively padded');
|
|
99
|
+
if (r[0]! & 0x80) throw new Error('R value is negative');
|
|
100
|
+
if (s[0]! & 0x80) throw new Error('S value is negative');
|
|
101
|
+
if (lenR > 1 && r[0] === 0x00 && !(r[1]! & 0x80)) throw new Error('R value excessively padded');
|
|
102
|
+
if (lenS > 1 && s[0] === 0x00 && !(s[1]! & 0x80)) throw new Error('S value excessively padded');
|
|
93
103
|
|
|
94
|
-
const signature =
|
|
104
|
+
const signature = alloc(6 + lenR + lenS);
|
|
95
105
|
|
|
96
106
|
// 0x30 [total-length] 0x02 [R-length] [R] 0x02 [S-length] [S]
|
|
97
107
|
signature[0] = 0x30;
|
|
98
108
|
signature[1] = signature.length - 2;
|
|
99
109
|
signature[2] = 0x02;
|
|
100
110
|
signature[3] = r.length;
|
|
101
|
-
|
|
111
|
+
signature.set(r, 4);
|
|
102
112
|
signature[4 + lenR] = 0x02;
|
|
103
113
|
signature[5 + lenR] = s.length;
|
|
104
|
-
|
|
114
|
+
signature.set(s, 6 + lenR);
|
|
105
115
|
|
|
106
116
|
return signature;
|
|
107
117
|
}
|