@opcat-labs/scrypt-ts-opcat 1.0.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/assets/.templates/smart-contract/builtin-libs/TxHashPreimageUtils.scrypt.map +1 -0
- package/assets/.templates/smart-contract/builtin-libs/TxHashPreimageUtils.scrypt.tpl +18 -0
- package/assets/.templates/smart-contract/builtin-libs/TxHashPreimageUtils.transformer.json +7 -0
- package/assets/.templates/smart-contract/builtin-libs/backtrace.scrypt.map +1 -0
- package/assets/.templates/smart-contract/builtin-libs/backtrace.scrypt.tpl +36 -0
- package/assets/.templates/smart-contract/builtin-libs/backtrace.transformer.json +7 -0
- package/assets/.templates/smart-contract/builtin-libs/byteStringReader.scrypt.map +1 -0
- package/assets/.templates/smart-contract/builtin-libs/byteStringReader.scrypt.tpl +64 -0
- package/assets/.templates/smart-contract/builtin-libs/byteStringReader.transformer.json +7 -0
- package/assets/.templates/smart-contract/builtin-libs/byteStringWriter.scrypt.map +1 -0
- package/assets/.templates/smart-contract/builtin-libs/byteStringWriter.scrypt.tpl +48 -0
- package/assets/.templates/smart-contract/builtin-libs/byteStringWriter.transformer.json +7 -0
- package/assets/.templates/smart-contract/builtin-libs/contextUtils.scrypt.map +1 -0
- package/assets/.templates/smart-contract/builtin-libs/contextUtils.scrypt.tpl +101 -0
- package/assets/.templates/smart-contract/builtin-libs/contextUtils.transformer.json +7 -0
- package/assets/.templates/smart-contract/builtin-libs/p2pk.scrypt.map +1 -0
- package/assets/.templates/smart-contract/builtin-libs/p2pk.scrypt.tpl +7 -0
- package/assets/.templates/smart-contract/builtin-libs/p2pk.transformer.json +7 -0
- package/assets/.templates/smart-contract/builtin-libs/p2pkh.scrypt.map +1 -0
- package/assets/.templates/smart-contract/builtin-libs/p2pkh.scrypt.tpl +8 -0
- package/assets/.templates/smart-contract/builtin-libs/p2pkh.transformer.json +7 -0
- package/assets/.templates/smart-contract/builtin-libs/stateLib.scrypt.map +1 -0
- package/assets/.templates/smart-contract/builtin-libs/stateLib.scrypt.tpl +3 -0
- package/assets/.templates/smart-contract/builtin-libs/stateLib.transformer.json +7 -0
- package/assets/.templates/smart-contract/builtin-libs/stateUtils.scrypt.map +1 -0
- package/assets/.templates/smart-contract/builtin-libs/stateUtils.scrypt.tpl +7 -0
- package/assets/.templates/smart-contract/builtin-libs/stateUtils.transformer.json +7 -0
- package/assets/.templates/smart-contract/builtin-libs/stdUtils.scrypt.map +1 -0
- package/assets/.templates/smart-contract/builtin-libs/stdUtils.scrypt.tpl +89 -0
- package/assets/.templates/smart-contract/builtin-libs/stdUtils.transformer.json +7 -0
- package/assets/.templates/smart-contract/builtin-libs/txUtils.scrypt.map +1 -0
- package/assets/.templates/smart-contract/builtin-libs/txUtils.scrypt.tpl +42 -0
- package/assets/.templates/smart-contract/builtin-libs/txUtils.transformer.json +7 -0
- package/assets/.templates/smart-contract/types/structs.scrypt.map +1 -0
- package/assets/.templates/smart-contract/types/structs.scrypt.tpl +48 -0
- package/assets/.templates/smart-contract/types/structs.transformer.json +7 -0
- package/assets/smart-contract/builtin-libs/TxHashPreimageUtils.scrypt +20 -0
- package/assets/smart-contract/builtin-libs/backtrace.scrypt +38 -0
- package/assets/smart-contract/builtin-libs/byteStringReader.scrypt +66 -0
- package/assets/smart-contract/builtin-libs/byteStringWriter.scrypt +50 -0
- package/assets/smart-contract/builtin-libs/contextUtils.scrypt +103 -0
- package/assets/smart-contract/builtin-libs/p2pk.scrypt +9 -0
- package/assets/smart-contract/builtin-libs/p2pkh.scrypt +10 -0
- package/assets/smart-contract/builtin-libs/stateLib.scrypt +5 -0
- package/assets/smart-contract/builtin-libs/stateUtils.scrypt +9 -0
- package/assets/smart-contract/builtin-libs/stdUtils.scrypt +91 -0
- package/assets/smart-contract/builtin-libs/txUtils.scrypt +44 -0
- package/assets/smart-contract/types/structs.scrypt +50 -0
- package/dist/cjs/features/call.cjs +38 -0
- package/dist/cjs/features/call.js.map +1 -0
- package/dist/cjs/features/deploy.cjs +33 -0
- package/dist/cjs/features/deploy.js.map +1 -0
- package/dist/cjs/features/index.cjs +8 -0
- package/dist/cjs/features/index.js.map +1 -0
- package/dist/cjs/globalTypes.cjs +3 -0
- package/dist/cjs/globalTypes.js.map +1 -0
- package/dist/cjs/index.cjs +55 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/networks.cjs +69 -0
- package/dist/cjs/networks.js.map +1 -0
- package/dist/cjs/providers/chainProvider.cjs +3 -0
- package/dist/cjs/providers/chainProvider.js.map +1 -0
- package/dist/cjs/providers/dummyProvider.cjs +87 -0
- package/dist/cjs/providers/dummyProvider.js.map +1 -0
- package/dist/cjs/providers/index.cjs +13 -0
- package/dist/cjs/providers/index.js.map +1 -0
- package/dist/cjs/providers/mempoolProvider.cjs +207 -0
- package/dist/cjs/providers/mempoolProvider.js.map +1 -0
- package/dist/cjs/providers/rpcProvider.cjs +296 -0
- package/dist/cjs/providers/rpcProvider.js.map +1 -0
- package/dist/cjs/providers/utxoProvider.cjs +18 -0
- package/dist/cjs/providers/utxoProvider.js.map +1 -0
- package/dist/cjs/psbt/bip66.cjs +182 -0
- package/dist/cjs/psbt/bip66.js.map +1 -0
- package/dist/cjs/psbt/bufferutils.cjs +186 -0
- package/dist/cjs/psbt/bufferutils.js.map +1 -0
- package/dist/cjs/psbt/contextProvider.cjs +111 -0
- package/dist/cjs/psbt/contextProvider.js.map +1 -0
- package/dist/cjs/psbt/extPsbt.cjs +589 -0
- package/dist/cjs/psbt/extPsbt.js.map +1 -0
- package/dist/cjs/psbt/psbt.cjs +973 -0
- package/dist/cjs/psbt/psbt.js.map +1 -0
- package/dist/cjs/psbt/psbtutils.cjs +140 -0
- package/dist/cjs/psbt/psbtutils.js.map +1 -0
- package/dist/cjs/psbt/signatureutils.cjs +230 -0
- package/dist/cjs/psbt/signatureutils.js.map +1 -0
- package/dist/cjs/psbt/types.cjs +3 -0
- package/dist/cjs/psbt/types.js.map +1 -0
- package/dist/cjs/signer.cjs +3 -0
- package/dist/cjs/signer.js.map +1 -0
- package/dist/cjs/signers/defaultSigner.cjs +68 -0
- package/dist/cjs/signers/defaultSigner.js.map +1 -0
- package/dist/cjs/signers/index.cjs +8 -0
- package/dist/cjs/signers/index.js.map +1 -0
- package/dist/cjs/signers/unisatSigner.cjs +38 -0
- package/dist/cjs/signers/unisatSigner.js.map +1 -0
- package/dist/cjs/smart-contract/abi.cjs +380 -0
- package/dist/cjs/smart-contract/abi.js.map +1 -0
- package/dist/cjs/smart-contract/abiutils.cjs +183 -0
- package/dist/cjs/smart-contract/abiutils.js.map +1 -0
- package/dist/cjs/smart-contract/abstractContract.cjs +25 -0
- package/dist/cjs/smart-contract/abstractContract.js.map +1 -0
- package/dist/cjs/smart-contract/builtin-libs/backtrace.cjs +102 -0
- package/dist/cjs/smart-contract/builtin-libs/backtrace.js.map +1 -0
- package/dist/cjs/smart-contract/builtin-libs/byteStringReader.cjs +138 -0
- package/dist/cjs/smart-contract/builtin-libs/byteStringReader.js.map +1 -0
- package/dist/cjs/smart-contract/builtin-libs/byteStringWriter.cjs +109 -0
- package/dist/cjs/smart-contract/builtin-libs/byteStringWriter.js.map +1 -0
- package/dist/cjs/smart-contract/builtin-libs/contextUtils.cjs +304 -0
- package/dist/cjs/smart-contract/builtin-libs/contextUtils.js.map +1 -0
- package/dist/cjs/smart-contract/builtin-libs/index.cjs +18 -0
- package/dist/cjs/smart-contract/builtin-libs/index.js.map +1 -0
- package/dist/cjs/smart-contract/builtin-libs/p2pk.cjs +74 -0
- package/dist/cjs/smart-contract/builtin-libs/p2pk.js.map +1 -0
- package/dist/cjs/smart-contract/builtin-libs/p2pkh.cjs +80 -0
- package/dist/cjs/smart-contract/builtin-libs/p2pkh.js.map +1 -0
- package/dist/cjs/smart-contract/builtin-libs/stateLib.cjs +65 -0
- package/dist/cjs/smart-contract/builtin-libs/stateLib.js.map +1 -0
- package/dist/cjs/smart-contract/builtin-libs/stateUtils.cjs +42 -0
- package/dist/cjs/smart-contract/builtin-libs/stateUtils.js.map +1 -0
- package/dist/cjs/smart-contract/builtin-libs/stdUtils.cjs +192 -0
- package/dist/cjs/smart-contract/builtin-libs/stdUtils.js.map +1 -0
- package/dist/cjs/smart-contract/builtin-libs/txHashPreimageUtils.cjs +56 -0
- package/dist/cjs/smart-contract/builtin-libs/txHashPreimageUtils.js.map +1 -0
- package/dist/cjs/smart-contract/builtin-libs/txUtils.cjs +183 -0
- package/dist/cjs/smart-contract/builtin-libs/txUtils.js.map +1 -0
- package/dist/cjs/smart-contract/consts.cjs +100 -0
- package/dist/cjs/smart-contract/consts.js.map +1 -0
- package/dist/cjs/smart-contract/decorators.cjs +83 -0
- package/dist/cjs/smart-contract/decorators.js.map +1 -0
- package/dist/cjs/smart-contract/fns/assert.cjs +17 -0
- package/dist/cjs/smart-contract/fns/assert.js.map +1 -0
- package/dist/cjs/smart-contract/fns/bitwise.cjs +141 -0
- package/dist/cjs/smart-contract/fns/bitwise.js.map +1 -0
- package/dist/cjs/smart-contract/fns/byteString.cjs +106 -0
- package/dist/cjs/smart-contract/fns/byteString.js.map +1 -0
- package/dist/cjs/smart-contract/fns/equals.cjs +66 -0
- package/dist/cjs/smart-contract/fns/equals.js.map +1 -0
- package/dist/cjs/smart-contract/fns/fill.cjs +19 -0
- package/dist/cjs/smart-contract/fns/fill.js.map +1 -0
- package/dist/cjs/smart-contract/fns/hashes.cjs +81 -0
- package/dist/cjs/smart-contract/fns/hashes.js.map +1 -0
- package/dist/cjs/smart-contract/fns/index.cjs +37 -0
- package/dist/cjs/smart-contract/fns/index.js.map +1 -0
- package/dist/cjs/smart-contract/fns/math.cjs +52 -0
- package/dist/cjs/smart-contract/fns/math.js.map +1 -0
- package/dist/cjs/smart-contract/fns/pubKey2Addr.cjs +17 -0
- package/dist/cjs/smart-contract/fns/pubKey2Addr.js.map +1 -0
- package/dist/cjs/smart-contract/methods/backtraceToGenensis.cjs +41 -0
- package/dist/cjs/smart-contract/methods/backtraceToGenensis.js.map +1 -0
- package/dist/cjs/smart-contract/methods/buildOutput.cjs +41 -0
- package/dist/cjs/smart-contract/methods/buildOutput.js.map +1 -0
- package/dist/cjs/smart-contract/methods/checkCtx.cjs +58 -0
- package/dist/cjs/smart-contract/methods/checkCtx.js.map +1 -0
- package/dist/cjs/smart-contract/methods/checkInputState.cjs +16 -0
- package/dist/cjs/smart-contract/methods/checkInputState.js.map +1 -0
- package/dist/cjs/smart-contract/methods/checkInputStateHashes.cjs +16 -0
- package/dist/cjs/smart-contract/methods/checkInputStateHashes.js.map +1 -0
- package/dist/cjs/smart-contract/methods/checkSHPreimage.cjs +17 -0
- package/dist/cjs/smart-contract/methods/checkSHPreimage.js.map +1 -0
- package/dist/cjs/smart-contract/methods/checkSig.cjs +77 -0
- package/dist/cjs/smart-contract/methods/checkSig.js.map +1 -0
- package/dist/cjs/smart-contract/serializer.cjs +145 -0
- package/dist/cjs/smart-contract/serializer.js.map +1 -0
- package/dist/cjs/smart-contract/smartContract.cjs +568 -0
- package/dist/cjs/smart-contract/smartContract.js.map +1 -0
- package/dist/cjs/smart-contract/smartContractLib.cjs +46 -0
- package/dist/cjs/smart-contract/smartContractLib.js.map +1 -0
- package/dist/cjs/smart-contract/stateSerializer.cjs +134 -0
- package/dist/cjs/smart-contract/stateSerializer.js.map +1 -0
- package/dist/cjs/smart-contract/types/abi.cjs +69 -0
- package/dist/cjs/smart-contract/types/abi.js.map +1 -0
- package/dist/cjs/smart-contract/types/artifact.cjs +16 -0
- package/dist/cjs/smart-contract/types/artifact.js.map +1 -0
- package/dist/cjs/smart-contract/types/bn.cjs +117 -0
- package/dist/cjs/smart-contract/types/bn.js.map +1 -0
- package/dist/cjs/smart-contract/types/context.cjs +3 -0
- package/dist/cjs/smart-contract/types/context.js.map +1 -0
- package/dist/cjs/smart-contract/types/index.cjs +16 -0
- package/dist/cjs/smart-contract/types/index.js.map +1 -0
- package/dist/cjs/smart-contract/types/opCode.cjs +754 -0
- package/dist/cjs/smart-contract/types/opCode.js.map +1 -0
- package/dist/cjs/smart-contract/types/primitives.cjs +108 -0
- package/dist/cjs/smart-contract/types/primitives.js.map +1 -0
- package/dist/cjs/smart-contract/types/script.cjs +43 -0
- package/dist/cjs/smart-contract/types/script.js.map +1 -0
- package/dist/cjs/smart-contract/types/structs.cjs +3 -0
- package/dist/cjs/smart-contract/types/structs.js.map +1 -0
- package/dist/cjs/smart-contract/types/utils.cjs +21 -0
- package/dist/cjs/smart-contract/types/utils.js.map +1 -0
- package/dist/cjs/utils/bvm.cjs +30 -0
- package/dist/cjs/utils/bvm.js.map +1 -0
- package/dist/cjs/utils/checkIntegrity.cjs +49 -0
- package/dist/cjs/utils/checkIntegrity.js.map +1 -0
- package/dist/cjs/utils/common.cjs +228 -0
- package/dist/cjs/utils/common.js.map +1 -0
- package/dist/cjs/utils/constants.cjs +5 -0
- package/dist/cjs/utils/constants.js.map +1 -0
- package/dist/cjs/utils/index.cjs +45 -0
- package/dist/cjs/utils/index.js.map +1 -0
- package/dist/cjs/utils/preimage.cjs +93 -0
- package/dist/cjs/utils/preimage.js.map +1 -0
- package/dist/cjs/utils/proof.cjs +110 -0
- package/dist/cjs/utils/proof.js.map +1 -0
- package/dist/cjs/utils/script_number.cjs +107 -0
- package/dist/cjs/utils/script_number.js.map +1 -0
- package/dist/esm/features/call.js +34 -0
- package/dist/esm/features/call.js.map +1 -0
- package/dist/esm/features/deploy.js +29 -0
- package/dist/esm/features/deploy.js.map +1 -0
- package/dist/esm/features/index.js +3 -0
- package/dist/esm/features/index.js.map +1 -0
- package/dist/esm/globalTypes.js +2 -0
- package/dist/esm/globalTypes.js.map +1 -0
- package/dist/esm/index.js +23 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/networks.js +41 -0
- package/dist/esm/networks.js.map +1 -0
- package/dist/esm/providers/chainProvider.js +2 -0
- package/dist/esm/providers/chainProvider.js.map +1 -0
- package/dist/esm/providers/dummyProvider.js +60 -0
- package/dist/esm/providers/dummyProvider.js.map +1 -0
- package/dist/esm/providers/index.js +5 -0
- package/dist/esm/providers/index.js.map +1 -0
- package/dist/esm/providers/mempoolProvider.js +200 -0
- package/dist/esm/providers/mempoolProvider.js.map +1 -0
- package/dist/esm/providers/rpcProvider.js +266 -0
- package/dist/esm/providers/rpcProvider.js.map +1 -0
- package/dist/esm/providers/utxoProvider.js +13 -0
- package/dist/esm/providers/utxoProvider.js.map +1 -0
- package/dist/esm/psbt/bip66.js +175 -0
- package/dist/esm/psbt/bip66.js.map +1 -0
- package/dist/esm/psbt/bufferutils.js +156 -0
- package/dist/esm/psbt/bufferutils.js.map +1 -0
- package/dist/esm/psbt/contextProvider.js +84 -0
- package/dist/esm/psbt/contextProvider.js.map +1 -0
- package/dist/esm/psbt/extPsbt.js +562 -0
- package/dist/esm/psbt/extPsbt.js.map +1 -0
- package/dist/esm/psbt/psbt.js +945 -0
- package/dist/esm/psbt/psbt.js.map +1 -0
- package/dist/esm/psbt/psbtutils.js +110 -0
- package/dist/esm/psbt/psbtutils.js.map +1 -0
- package/dist/esm/psbt/signatureutils.js +199 -0
- package/dist/esm/psbt/signatureutils.js.map +1 -0
- package/dist/esm/psbt/types.js +2 -0
- package/dist/esm/psbt/types.js.map +1 -0
- package/dist/esm/signer.js +2 -0
- package/dist/esm/signer.js.map +1 -0
- package/dist/esm/signers/defaultSigner.js +63 -0
- package/dist/esm/signers/defaultSigner.js.map +1 -0
- package/dist/esm/signers/index.js +3 -0
- package/dist/esm/signers/index.js.map +1 -0
- package/dist/esm/signers/unisatSigner.js +34 -0
- package/dist/esm/signers/unisatSigner.js.map +1 -0
- package/dist/esm/smart-contract/abi.js +373 -0
- package/dist/esm/smart-contract/abi.js.map +1 -0
- package/dist/esm/smart-contract/abiutils.js +171 -0
- package/dist/esm/smart-contract/abiutils.js.map +1 -0
- package/dist/esm/smart-contract/abstractContract.js +21 -0
- package/dist/esm/smart-contract/abstractContract.js.map +1 -0
- package/dist/esm/smart-contract/builtin-libs/backtrace.js +98 -0
- package/dist/esm/smart-contract/builtin-libs/backtrace.js.map +1 -0
- package/dist/esm/smart-contract/builtin-libs/byteStringReader.js +134 -0
- package/dist/esm/smart-contract/builtin-libs/byteStringReader.js.map +1 -0
- package/dist/esm/smart-contract/builtin-libs/byteStringWriter.js +105 -0
- package/dist/esm/smart-contract/builtin-libs/byteStringWriter.js.map +1 -0
- package/dist/esm/smart-contract/builtin-libs/contextUtils.js +300 -0
- package/dist/esm/smart-contract/builtin-libs/contextUtils.js.map +1 -0
- package/dist/esm/smart-contract/builtin-libs/index.js +8 -0
- package/dist/esm/smart-contract/builtin-libs/index.js.map +1 -0
- package/dist/esm/smart-contract/builtin-libs/p2pk.js +70 -0
- package/dist/esm/smart-contract/builtin-libs/p2pk.js.map +1 -0
- package/dist/esm/smart-contract/builtin-libs/p2pkh.js +76 -0
- package/dist/esm/smart-contract/builtin-libs/p2pkh.js.map +1 -0
- package/dist/esm/smart-contract/builtin-libs/stateLib.js +61 -0
- package/dist/esm/smart-contract/builtin-libs/stateLib.js.map +1 -0
- package/dist/esm/smart-contract/builtin-libs/stateUtils.js +38 -0
- package/dist/esm/smart-contract/builtin-libs/stateUtils.js.map +1 -0
- package/dist/esm/smart-contract/builtin-libs/stdUtils.js +188 -0
- package/dist/esm/smart-contract/builtin-libs/stdUtils.js.map +1 -0
- package/dist/esm/smart-contract/builtin-libs/txHashPreimageUtils.js +52 -0
- package/dist/esm/smart-contract/builtin-libs/txHashPreimageUtils.js.map +1 -0
- package/dist/esm/smart-contract/builtin-libs/txUtils.js +179 -0
- package/dist/esm/smart-contract/builtin-libs/txUtils.js.map +1 -0
- package/dist/esm/smart-contract/consts.js +97 -0
- package/dist/esm/smart-contract/consts.js.map +1 -0
- package/dist/esm/smart-contract/decorators.js +78 -0
- package/dist/esm/smart-contract/decorators.js.map +1 -0
- package/dist/esm/smart-contract/fns/assert.js +13 -0
- package/dist/esm/smart-contract/fns/assert.js.map +1 -0
- package/dist/esm/smart-contract/fns/bitwise.js +109 -0
- package/dist/esm/smart-contract/fns/bitwise.js.map +1 -0
- package/dist/esm/smart-contract/fns/byteString.js +97 -0
- package/dist/esm/smart-contract/fns/byteString.js.map +1 -0
- package/dist/esm/smart-contract/fns/equals.js +62 -0
- package/dist/esm/smart-contract/fns/equals.js.map +1 -0
- package/dist/esm/smart-contract/fns/fill.js +15 -0
- package/dist/esm/smart-contract/fns/fill.js.map +1 -0
- package/dist/esm/smart-contract/fns/hashes.js +73 -0
- package/dist/esm/smart-contract/fns/hashes.js.map +1 -0
- package/dist/esm/smart-contract/fns/index.js +9 -0
- package/dist/esm/smart-contract/fns/index.js.map +1 -0
- package/dist/esm/smart-contract/fns/math.js +45 -0
- package/dist/esm/smart-contract/fns/math.js.map +1 -0
- package/dist/esm/smart-contract/fns/pubKey2Addr.js +13 -0
- package/dist/esm/smart-contract/fns/pubKey2Addr.js.map +1 -0
- package/dist/esm/smart-contract/methods/backtraceToGenensis.js +36 -0
- package/dist/esm/smart-contract/methods/backtraceToGenensis.js.map +1 -0
- package/dist/esm/smart-contract/methods/buildOutput.js +36 -0
- package/dist/esm/smart-contract/methods/buildOutput.js.map +1 -0
- package/dist/esm/smart-contract/methods/checkCtx.js +31 -0
- package/dist/esm/smart-contract/methods/checkCtx.js.map +1 -0
- package/dist/esm/smart-contract/methods/checkInputState.js +12 -0
- package/dist/esm/smart-contract/methods/checkInputState.js.map +1 -0
- package/dist/esm/smart-contract/methods/checkInputStateHashes.js +12 -0
- package/dist/esm/smart-contract/methods/checkInputStateHashes.js.map +1 -0
- package/dist/esm/smart-contract/methods/checkSHPreimage.js +13 -0
- package/dist/esm/smart-contract/methods/checkSHPreimage.js.map +1 -0
- package/dist/esm/smart-contract/methods/checkSig.js +73 -0
- package/dist/esm/smart-contract/methods/checkSig.js.map +1 -0
- package/dist/esm/smart-contract/serializer.js +112 -0
- package/dist/esm/smart-contract/serializer.js.map +1 -0
- package/dist/esm/smart-contract/smartContract.js +564 -0
- package/dist/esm/smart-contract/smartContract.js.map +1 -0
- package/dist/esm/smart-contract/smartContractLib.js +42 -0
- package/dist/esm/smart-contract/smartContractLib.js.map +1 -0
- package/dist/esm/smart-contract/stateSerializer.js +129 -0
- package/dist/esm/smart-contract/stateSerializer.js.map +1 -0
- package/dist/esm/smart-contract/types/abi.js +63 -0
- package/dist/esm/smart-contract/types/abi.js.map +1 -0
- package/dist/esm/smart-contract/types/artifact.js +13 -0
- package/dist/esm/smart-contract/types/artifact.js.map +1 -0
- package/dist/esm/smart-contract/types/bn.js +89 -0
- package/dist/esm/smart-contract/types/bn.js.map +1 -0
- package/dist/esm/smart-contract/types/context.js +2 -0
- package/dist/esm/smart-contract/types/context.js.map +1 -0
- package/dist/esm/smart-contract/types/index.js +3 -0
- package/dist/esm/smart-contract/types/index.js.map +1 -0
- package/dist/esm/smart-contract/types/opCode.js +750 -0
- package/dist/esm/smart-contract/types/opCode.js.map +1 -0
- package/dist/esm/smart-contract/types/primitives.js +95 -0
- package/dist/esm/smart-contract/types/primitives.js.map +1 -0
- package/dist/esm/smart-contract/types/script.js +16 -0
- package/dist/esm/smart-contract/types/script.js.map +1 -0
- package/dist/esm/smart-contract/types/structs.js +2 -0
- package/dist/esm/smart-contract/types/structs.js.map +1 -0
- package/dist/esm/smart-contract/types/utils.js +17 -0
- package/dist/esm/smart-contract/types/utils.js.map +1 -0
- package/dist/esm/utils/bvm.js +26 -0
- package/dist/esm/utils/bvm.js.map +1 -0
- package/dist/esm/utils/checkIntegrity.js +41 -0
- package/dist/esm/utils/checkIntegrity.js.map +1 -0
- package/dist/esm/utils/common.js +188 -0
- package/dist/esm/utils/common.js.map +1 -0
- package/dist/esm/utils/constants.js +2 -0
- package/dist/esm/utils/constants.js.map +1 -0
- package/dist/esm/utils/index.js +7 -0
- package/dist/esm/utils/index.js.map +1 -0
- package/dist/esm/utils/preimage.js +65 -0
- package/dist/esm/utils/preimage.js.map +1 -0
- package/dist/esm/utils/proof.js +98 -0
- package/dist/esm/utils/proof.js.map +1 -0
- package/dist/esm/utils/script_number.js +79 -0
- package/dist/esm/utils/script_number.js.map +1 -0
- package/dist/types/features/call.d.ts +21 -0
- package/dist/types/features/call.d.ts.map +1 -0
- package/dist/types/features/deploy.d.ts +16 -0
- package/dist/types/features/deploy.d.ts.map +1 -0
- package/dist/types/features/index.d.ts +3 -0
- package/dist/types/features/index.d.ts.map +1 -0
- package/dist/types/globalTypes.d.ts +37 -0
- package/dist/types/globalTypes.d.ts.map +1 -0
- package/dist/types/index.d.ts +18 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/networks.d.ts +19 -0
- package/dist/types/networks.d.ts.map +1 -0
- package/dist/types/providers/chainProvider.d.ts +31 -0
- package/dist/types/providers/chainProvider.d.ts.map +1 -0
- package/dist/types/providers/dummyProvider.d.ts +23 -0
- package/dist/types/providers/dummyProvider.d.ts.map +1 -0
- package/dist/types/providers/index.d.ts +6 -0
- package/dist/types/providers/index.d.ts.map +1 -0
- package/dist/types/providers/mempoolProvider.d.ts +28 -0
- package/dist/types/providers/mempoolProvider.d.ts.map +1 -0
- package/dist/types/providers/rpcProvider.d.ts +34 -0
- package/dist/types/providers/rpcProvider.d.ts.map +1 -0
- package/dist/types/providers/utxoProvider.d.ts +43 -0
- package/dist/types/providers/utxoProvider.d.ts.map +1 -0
- package/dist/types/psbt/bip66.d.ts +28 -0
- package/dist/types/psbt/bip66.d.ts.map +1 -0
- package/dist/types/psbt/bufferutils.d.ts +47 -0
- package/dist/types/psbt/bufferutils.d.ts.map +1 -0
- package/dist/types/psbt/contextProvider.d.ts +15 -0
- package/dist/types/psbt/contextProvider.d.ts.map +1 -0
- package/dist/types/psbt/extPsbt.d.ts +131 -0
- package/dist/types/psbt/extPsbt.d.ts.map +1 -0
- package/dist/types/psbt/psbt.d.ts +194 -0
- package/dist/types/psbt/psbt.d.ts.map +1 -0
- package/dist/types/psbt/psbtutils.d.ts +37 -0
- package/dist/types/psbt/psbtutils.d.ts.map +1 -0
- package/dist/types/psbt/signatureutils.d.ts +52 -0
- package/dist/types/psbt/signatureutils.d.ts.map +1 -0
- package/dist/types/psbt/types.d.ts +63 -0
- package/dist/types/psbt/types.d.ts.map +1 -0
- package/dist/types/signer.d.ts +48 -0
- package/dist/types/signer.d.ts.map +1 -0
- package/dist/types/signers/defaultSigner.d.ts +30 -0
- package/dist/types/signers/defaultSigner.d.ts.map +1 -0
- package/dist/types/signers/index.d.ts +4 -0
- package/dist/types/signers/index.d.ts.map +1 -0
- package/dist/types/signers/unisatSigner.d.ts +32 -0
- package/dist/types/signers/unisatSigner.d.ts.map +1 -0
- package/dist/types/smart-contract/abi.d.ts +43 -0
- package/dist/types/smart-contract/abi.d.ts.map +1 -0
- package/dist/types/smart-contract/abiutils.d.ts +51 -0
- package/dist/types/smart-contract/abiutils.d.ts.map +1 -0
- package/dist/types/smart-contract/abstractContract.d.ts +115 -0
- package/dist/types/smart-contract/abstractContract.d.ts.map +1 -0
- package/dist/types/smart-contract/builtin-libs/backtrace.d.ts +43 -0
- package/dist/types/smart-contract/builtin-libs/backtrace.d.ts.map +1 -0
- package/dist/types/smart-contract/builtin-libs/byteStringReader.d.ts +32 -0
- package/dist/types/smart-contract/builtin-libs/byteStringReader.d.ts.map +1 -0
- package/dist/types/smart-contract/builtin-libs/byteStringWriter.d.ts +29 -0
- package/dist/types/smart-contract/builtin-libs/byteStringWriter.d.ts.map +1 -0
- package/dist/types/smart-contract/builtin-libs/contextUtils.d.ts +53 -0
- package/dist/types/smart-contract/builtin-libs/contextUtils.d.ts.map +1 -0
- package/dist/types/smart-contract/builtin-libs/index.d.ts +8 -0
- package/dist/types/smart-contract/builtin-libs/index.d.ts.map +1 -0
- package/dist/types/smart-contract/builtin-libs/p2pk.d.ts +8 -0
- package/dist/types/smart-contract/builtin-libs/p2pk.d.ts.map +1 -0
- package/dist/types/smart-contract/builtin-libs/p2pkh.d.ts +8 -0
- package/dist/types/smart-contract/builtin-libs/p2pkh.d.ts.map +1 -0
- package/dist/types/smart-contract/builtin-libs/stateLib.d.ts +27 -0
- package/dist/types/smart-contract/builtin-libs/stateLib.d.ts.map +1 -0
- package/dist/types/smart-contract/builtin-libs/stateUtils.d.ts +18 -0
- package/dist/types/smart-contract/builtin-libs/stateUtils.d.ts.map +1 -0
- package/dist/types/smart-contract/builtin-libs/stdUtils.d.ts +44 -0
- package/dist/types/smart-contract/builtin-libs/stdUtils.d.ts.map +1 -0
- package/dist/types/smart-contract/builtin-libs/txHashPreimageUtils.d.ts +8 -0
- package/dist/types/smart-contract/builtin-libs/txHashPreimageUtils.d.ts.map +1 -0
- package/dist/types/smart-contract/builtin-libs/txUtils.d.ts +69 -0
- package/dist/types/smart-contract/builtin-libs/txUtils.d.ts.map +1 -0
- package/dist/types/smart-contract/consts.d.ts +97 -0
- package/dist/types/smart-contract/consts.d.ts.map +1 -0
- package/dist/types/smart-contract/decorators.d.ts +40 -0
- package/dist/types/smart-contract/decorators.d.ts.map +1 -0
- package/dist/types/smart-contract/fns/assert.d.ts +8 -0
- package/dist/types/smart-contract/fns/assert.d.ts.map +1 -0
- package/dist/types/smart-contract/fns/bitwise.d.ts +50 -0
- package/dist/types/smart-contract/fns/bitwise.d.ts.map +1 -0
- package/dist/types/smart-contract/fns/byteString.d.ts +56 -0
- package/dist/types/smart-contract/fns/byteString.d.ts.map +1 -0
- package/dist/types/smart-contract/fns/equals.d.ts +8 -0
- package/dist/types/smart-contract/fns/equals.d.ts.map +1 -0
- package/dist/types/smart-contract/fns/fill.d.ts +11 -0
- package/dist/types/smart-contract/fns/fill.d.ts.map +1 -0
- package/dist/types/smart-contract/fns/hashes.d.ts +60 -0
- package/dist/types/smart-contract/fns/hashes.d.ts.map +1 -0
- package/dist/types/smart-contract/fns/index.d.ts +9 -0
- package/dist/types/smart-contract/fns/index.d.ts.map +1 -0
- package/dist/types/smart-contract/fns/math.d.ts +38 -0
- package/dist/types/smart-contract/fns/math.d.ts.map +1 -0
- package/dist/types/smart-contract/fns/pubKey2Addr.d.ts +11 -0
- package/dist/types/smart-contract/fns/pubKey2Addr.d.ts.map +1 -0
- package/dist/types/smart-contract/methods/backtraceToGenensis.d.ts +20 -0
- package/dist/types/smart-contract/methods/backtraceToGenensis.d.ts.map +1 -0
- package/dist/types/smart-contract/methods/buildOutput.d.ts +25 -0
- package/dist/types/smart-contract/methods/buildOutput.d.ts.map +1 -0
- package/dist/types/smart-contract/methods/checkCtx.d.ts +18 -0
- package/dist/types/smart-contract/methods/checkCtx.d.ts.map +1 -0
- package/dist/types/smart-contract/methods/checkInputState.d.ts +9 -0
- package/dist/types/smart-contract/methods/checkInputState.d.ts.map +1 -0
- package/dist/types/smart-contract/methods/checkInputStateHashes.d.ts +10 -0
- package/dist/types/smart-contract/methods/checkInputStateHashes.d.ts.map +1 -0
- package/dist/types/smart-contract/methods/checkSHPreimage.d.ts +10 -0
- package/dist/types/smart-contract/methods/checkSHPreimage.d.ts.map +1 -0
- package/dist/types/smart-contract/methods/checkSig.d.ts +11 -0
- package/dist/types/smart-contract/methods/checkSig.d.ts.map +1 -0
- package/dist/types/smart-contract/serializer.d.ts +48 -0
- package/dist/types/smart-contract/serializer.d.ts.map +1 -0
- package/dist/types/smart-contract/smartContract.d.ts +274 -0
- package/dist/types/smart-contract/smartContract.d.ts.map +1 -0
- package/dist/types/smart-contract/smartContractLib.d.ts +26 -0
- package/dist/types/smart-contract/smartContractLib.d.ts.map +1 -0
- package/dist/types/smart-contract/stateSerializer.d.ts +8 -0
- package/dist/types/smart-contract/stateSerializer.d.ts.map +1 -0
- package/dist/types/smart-contract/types/abi.d.ts +55 -0
- package/dist/types/smart-contract/types/abi.d.ts.map +1 -0
- package/dist/types/smart-contract/types/artifact.d.ts +108 -0
- package/dist/types/smart-contract/types/artifact.d.ts.map +1 -0
- package/dist/types/smart-contract/types/bn.d.ts +3 -0
- package/dist/types/smart-contract/types/bn.d.ts.map +1 -0
- package/dist/types/smart-contract/types/context.d.ts +126 -0
- package/dist/types/smart-contract/types/context.d.ts.map +1 -0
- package/dist/types/smart-contract/types/index.d.ts +4 -0
- package/dist/types/smart-contract/types/index.d.ts.map +1 -0
- package/dist/types/smart-contract/types/opCode.d.ts +747 -0
- package/dist/types/smart-contract/types/opCode.d.ts.map +1 -0
- package/dist/types/smart-contract/types/primitives.d.ts +217 -0
- package/dist/types/smart-contract/types/primitives.d.ts.map +1 -0
- package/dist/types/smart-contract/types/script.d.ts +14 -0
- package/dist/types/smart-contract/types/script.d.ts.map +1 -0
- package/dist/types/smart-contract/types/structs.d.ts +206 -0
- package/dist/types/smart-contract/types/structs.d.ts.map +1 -0
- package/dist/types/smart-contract/types/utils.d.ts +5 -0
- package/dist/types/smart-contract/types/utils.d.ts.map +1 -0
- package/dist/types/utils/bvm.d.ts +9 -0
- package/dist/types/utils/bvm.d.ts.map +1 -0
- package/dist/types/utils/checkIntegrity.d.ts +46 -0
- package/dist/types/utils/checkIntegrity.d.ts.map +1 -0
- package/dist/types/utils/common.d.ts +61 -0
- package/dist/types/utils/common.d.ts.map +1 -0
- package/dist/types/utils/constants.d.ts +3 -0
- package/dist/types/utils/constants.d.ts.map +1 -0
- package/dist/types/utils/index.d.ts +7 -0
- package/dist/types/utils/index.d.ts.map +1 -0
- package/dist/types/utils/preimage.d.ts +31 -0
- package/dist/types/utils/preimage.d.ts.map +1 -0
- package/dist/types/utils/proof.d.ts +31 -0
- package/dist/types/utils/proof.d.ts.map +1 -0
- package/dist/types/utils/script_number.d.ts +19 -0
- package/dist/types/utils/script_number.d.ts.map +1 -0
- package/package.json +79 -0
- package/scrypt.index.json +85 -0
- package/src/smart-contract/builtin-libs/backtrace.ts +116 -0
- package/src/smart-contract/builtin-libs/byteStringReader.ts +111 -0
- package/src/smart-contract/builtin-libs/byteStringWriter.ts +86 -0
- package/src/smart-contract/builtin-libs/contextUtils.ts +270 -0
- package/src/smart-contract/builtin-libs/index.ts +7 -0
- package/src/smart-contract/builtin-libs/p2pk.ts +58 -0
- package/src/smart-contract/builtin-libs/p2pkh.ts +67 -0
- package/src/smart-contract/builtin-libs/stateLib.ts +82 -0
- package/src/smart-contract/builtin-libs/stateUtils.ts +37 -0
- package/src/smart-contract/builtin-libs/stdUtils.ts +156 -0
- package/src/smart-contract/builtin-libs/txHashPreimageUtils.ts +34 -0
- package/src/smart-contract/builtin-libs/txUtils.ts +152 -0
- package/src/smart-contract/types/structs.ts +244 -0
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import { prop, method } from '../decorators.js';
|
|
2
|
+
import { assert } from '../fns/assert.js';
|
|
3
|
+
import { byteStringToInt, intToByteString, len, reverseByteString, slice, toByteString } from '../fns/byteString.js';
|
|
4
|
+
import { hash256 } from '../fns/hashes.js';
|
|
5
|
+
import { SmartContractLib } from '../smartContractLib.js';
|
|
6
|
+
import { PubKey, ByteString, Sig, Int32, UInt32, PrivKey, SigHashPreimage } from '../types/primitives.js';
|
|
7
|
+
import { SHPreimage, SpentScriptHashes, SpentAmounts, Prevouts, Outpoint, SpentDataHashes, TxHashPreimage } from '../types/structs.js';
|
|
8
|
+
import { StdUtils } from './stdUtils.js';
|
|
9
|
+
import { TxUtils } from './txUtils.js';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Library for verifying preimage.
|
|
13
|
+
* @category Library
|
|
14
|
+
* @onchain
|
|
15
|
+
*/
|
|
16
|
+
export class ContextUtils extends SmartContractLib {
|
|
17
|
+
|
|
18
|
+
// The following arguments can be generated using sample code at
|
|
19
|
+
// https://gist.github.com/scrypt-sv/f6882be580780a88984cee75dd1564c4.js
|
|
20
|
+
@prop()
|
|
21
|
+
static readonly privKey: PrivKey = PrivKey(0x26f00fe2340a84335ebdf30f57e9bb58487117b29355718f5e46bf5168d7df97n);
|
|
22
|
+
@prop()
|
|
23
|
+
static readonly pubKey: PubKey = PubKey(toByteString('02ba79df5f8ae7604a9830f03c7933028186aede0675a16f025dc4f8be8eec0382'));
|
|
24
|
+
// invK is the modular inverse of k, the ephemeral key
|
|
25
|
+
@prop()
|
|
26
|
+
static readonly invK: bigint = 0xc8ffdbaa05d93aa4ede79ec58f06a72562048b775a3507c2bf44bde4f007c40an;
|
|
27
|
+
// r is x coordinate of R, which is kG
|
|
28
|
+
@prop()
|
|
29
|
+
static readonly r: bigint = 0x1008ce7480da41702918d1ec8e6849ba32b4d65b1e40dc669c31a1e6306b266cn;
|
|
30
|
+
// rBigEndian is the signed magnitude representation of r, in big endian
|
|
31
|
+
@prop()
|
|
32
|
+
static readonly rBigEndian: ByteString = toByteString('1008ce7480da41702918d1ec8e6849ba32b4d65b1e40dc669c31a1e6306b266c');
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
@method()
|
|
36
|
+
static normalize(k: bigint, modulus: bigint): bigint {
|
|
37
|
+
const res: bigint = k % modulus;
|
|
38
|
+
// ensure it's positive
|
|
39
|
+
return (res < 0) ? res + modulus : res;
|
|
40
|
+
}
|
|
41
|
+
@method()
|
|
42
|
+
static sign(h: bigint, privKey: PrivKey, inverseK: bigint, r: bigint, rBigEndian: ByteString, sigHashType: ByteString): Sig {
|
|
43
|
+
// TODO: r * privKey can also be precomputed
|
|
44
|
+
let s: bigint = inverseK * (h + r * (privKey as bigint));
|
|
45
|
+
const N: bigint = 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141n;
|
|
46
|
+
s = ContextUtils.normalize(s, N);
|
|
47
|
+
// lower S
|
|
48
|
+
if (s > N / 2n) {
|
|
49
|
+
s = N - s;
|
|
50
|
+
}
|
|
51
|
+
// require(s != 0); // check offchain
|
|
52
|
+
/*
|
|
53
|
+
* DER: h + l + rh + rl + r + sh + sl + s + hashtype
|
|
54
|
+
* note: r & s are at most 33 bytes, thus no need to convert endian of rl & sl
|
|
55
|
+
*/
|
|
56
|
+
const rlen: bigint = BigInt(len(rBigEndian));
|
|
57
|
+
const slen = len(intToByteString(s));
|
|
58
|
+
// we convert s to 32 bytes, otherwise reverseByteString(, 32) fails when s is strictly less than 31 bytes (note: 31 bytes works)
|
|
59
|
+
// slice it after reversing to remove extra leading zeros, otherwise strict DER rule fails it due to not minimally encoded
|
|
60
|
+
const sBigEndian: ByteString = slice(reverseByteString(intToByteString(s, 32n), BigInt(32)), 32n - slen);
|
|
61
|
+
|
|
62
|
+
const l: bigint = 4n + rlen + BigInt(slen);
|
|
63
|
+
// rBigEndian must be mininally encoded, to conform to strict DER rule
|
|
64
|
+
const rb: ByteString = toByteString('30')
|
|
65
|
+
+ intToByteString(l)
|
|
66
|
+
+ toByteString('02')
|
|
67
|
+
+ intToByteString(rlen)
|
|
68
|
+
+ rBigEndian
|
|
69
|
+
+ toByteString('02')
|
|
70
|
+
+ intToByteString(slen)
|
|
71
|
+
+ sBigEndian
|
|
72
|
+
+ toByteString(sigHashType);
|
|
73
|
+
return Sig(rb);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
@method()
|
|
77
|
+
static fromBEUnsigned(b: ByteString): Int32 {
|
|
78
|
+
// change endian first
|
|
79
|
+
// append positive sign byte. This does not hurt even when sign bit is already positive
|
|
80
|
+
return byteStringToInt(reverseByteString(b, 32n) + toByteString('00'));
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* sign the transaction preimage
|
|
85
|
+
* @param shPreimage - the transaction preimage
|
|
86
|
+
* @returns a signature
|
|
87
|
+
*/
|
|
88
|
+
@method()
|
|
89
|
+
static checkSHPreimage(shPreimage: SHPreimage, sigHashType: ByteString): Sig {
|
|
90
|
+
assert(len(shPreimage.nVersion) == 4n, 'invalid length of nVersion');
|
|
91
|
+
assert(len(shPreimage.hashPrevouts) == 32n, 'invalid length of hashPrevouts');
|
|
92
|
+
assert(len(shPreimage.spentScriptHash) == 32n, 'invalid length of spentScriptHash');
|
|
93
|
+
assert(len(shPreimage.spentDataHash) == 32n, 'invalid length of spentDataHash');
|
|
94
|
+
assert(shPreimage.value >= 0n, 'invalid value of value');
|
|
95
|
+
assert(len(shPreimage.nSequence) == 4n, 'invalid length of nSequence');
|
|
96
|
+
assert(len(shPreimage.hashSpentAmounts) == 32n, 'invalid length of hashSpentAmounts');
|
|
97
|
+
assert(len(shPreimage.hashSpentScriptHashes) == 32n, 'invalid length of hashSpentScriptHashes');
|
|
98
|
+
assert(len(shPreimage.hashSpentDataHashes) == 32n, 'invalid length of hashSpentDataHashes');
|
|
99
|
+
assert(len(shPreimage.hashSequences) == 32n, 'invalid length of hashSequences');
|
|
100
|
+
assert(len(shPreimage.hashOutputs) == 32n, 'invalid length of hashOutputs');
|
|
101
|
+
assert(shPreimage.inputIndex >= 0n, 'invalid value of inputIndex');
|
|
102
|
+
assert(shPreimage.nLockTime >= 0n, 'invalid value of nLockTime');
|
|
103
|
+
assert(shPreimage.sigHashType == 1n
|
|
104
|
+
|| shPreimage.sigHashType == 2n
|
|
105
|
+
|| shPreimage.sigHashType == 3n
|
|
106
|
+
|| shPreimage.sigHashType == 0x81n
|
|
107
|
+
|| shPreimage.sigHashType == 0x82n
|
|
108
|
+
|| shPreimage.sigHashType == 0x83n
|
|
109
|
+
, 'invalid value of sigHashType');
|
|
110
|
+
|
|
111
|
+
const preimage = shPreimage.nVersion
|
|
112
|
+
+ shPreimage.hashPrevouts
|
|
113
|
+
+ shPreimage.spentScriptHash
|
|
114
|
+
+ shPreimage.spentDataHash
|
|
115
|
+
+ TxUtils.satoshisToByteString(shPreimage.value)
|
|
116
|
+
+ shPreimage.nSequence
|
|
117
|
+
+ shPreimage.hashSpentAmounts
|
|
118
|
+
+ shPreimage.hashSpentScriptHashes
|
|
119
|
+
+ shPreimage.hashSpentDataHashes
|
|
120
|
+
+ shPreimage.hashSequences
|
|
121
|
+
+ shPreimage.hashOutputs
|
|
122
|
+
+ StdUtils.toLEUnsigned(shPreimage.inputIndex, 4n)
|
|
123
|
+
+ StdUtils.toLEUnsigned(shPreimage.nLockTime, 4n)
|
|
124
|
+
+ intToByteString(shPreimage.sigHashType, 4n);
|
|
125
|
+
|
|
126
|
+
const h: ByteString = hash256(preimage);
|
|
127
|
+
const sig: Sig = ContextUtils.sign(
|
|
128
|
+
ContextUtils.fromBEUnsigned(h),
|
|
129
|
+
ContextUtils.privKey,
|
|
130
|
+
ContextUtils.invK,
|
|
131
|
+
ContextUtils.r,
|
|
132
|
+
ContextUtils.rBigEndian,
|
|
133
|
+
sigHashType);
|
|
134
|
+
return sig;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
@method()
|
|
138
|
+
static serializeSHPreimage(shPreimage: SHPreimage): SigHashPreimage {
|
|
139
|
+
assert(len(shPreimage.nVersion) == 4n, 'invalid length of nVersion');
|
|
140
|
+
assert(len(shPreimage.hashPrevouts) == 32n, 'invalid length of hashPrevouts');
|
|
141
|
+
assert(len(shPreimage.spentScriptHash) == 32n, 'invalid length of spentScriptHash');
|
|
142
|
+
assert(len(shPreimage.spentDataHash) == 32n, 'invalid length of spentDataHash');
|
|
143
|
+
assert(shPreimage.value >= 0n, 'invalid value of value');
|
|
144
|
+
assert(len(shPreimage.nSequence) == 4n, 'invalid length of nSequence');
|
|
145
|
+
assert(len(shPreimage.hashSpentAmounts) == 32n, 'invalid length of hashSpentAmounts');
|
|
146
|
+
assert(len(shPreimage.hashSpentScriptHashes) == 32n, 'invalid length of hashSpentScriptHashes');
|
|
147
|
+
assert(len(shPreimage.hashSpentDataHashes) == 32n, 'invalid length of hashSpentDataHashes');
|
|
148
|
+
assert(len(shPreimage.hashSequences) == 32n, 'invalid length of hashSequences');
|
|
149
|
+
assert(len(shPreimage.hashOutputs) == 32n, 'invalid length of hashOutputs');
|
|
150
|
+
assert(shPreimage.inputIndex >= 0n, 'invalid value of inputIndex');
|
|
151
|
+
assert(shPreimage.nLockTime >= 0n, 'invalid value of nLockTime');
|
|
152
|
+
assert(shPreimage.sigHashType == 1n
|
|
153
|
+
|| shPreimage.sigHashType == 2n
|
|
154
|
+
|| shPreimage.sigHashType == 3n
|
|
155
|
+
|| shPreimage.sigHashType == 0x81n
|
|
156
|
+
|| shPreimage.sigHashType == 0x82n
|
|
157
|
+
|| shPreimage.sigHashType == 0x83n
|
|
158
|
+
, 'invalid value of sigHashType');
|
|
159
|
+
|
|
160
|
+
const preimage = shPreimage.nVersion
|
|
161
|
+
+ shPreimage.hashPrevouts
|
|
162
|
+
+ shPreimage.spentScriptHash
|
|
163
|
+
+ shPreimage.spentDataHash
|
|
164
|
+
+ TxUtils.satoshisToByteString(shPreimage.value)
|
|
165
|
+
+ shPreimage.nSequence
|
|
166
|
+
+ shPreimage.hashSpentAmounts
|
|
167
|
+
+ shPreimage.hashSpentScriptHashes
|
|
168
|
+
+ shPreimage.hashSpentDataHashes
|
|
169
|
+
+ shPreimage.hashSequences
|
|
170
|
+
+ shPreimage.hashOutputs
|
|
171
|
+
+ StdUtils.toLEUnsigned(shPreimage.inputIndex, 4n)
|
|
172
|
+
+ StdUtils.toLEUnsigned(shPreimage.nLockTime, 4n)
|
|
173
|
+
+ intToByteString(shPreimage.sigHashType, 4n);
|
|
174
|
+
return SigHashPreimage(preimage);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Verify that the prevouts context passed in by the user is authentic
|
|
179
|
+
* @param prevouts prevouts context passed in by the user that need to be verified
|
|
180
|
+
* @param prevout prevout context passed in by the user that need to be verified
|
|
181
|
+
* @param t_hashPrevouts hashPrevouts in preimage which is trustable
|
|
182
|
+
* @param t_inputIndex the index of the input, which is trustable
|
|
183
|
+
* @returns the number of inputs, which is trustable
|
|
184
|
+
*/
|
|
185
|
+
@method()
|
|
186
|
+
static checkPrevouts(
|
|
187
|
+
prevouts: Prevouts,
|
|
188
|
+
t_hashPrevouts: ByteString,
|
|
189
|
+
t_inputIndex: UInt32,
|
|
190
|
+
t_inputCount: Int32,
|
|
191
|
+
): Outpoint {
|
|
192
|
+
// check prevouts
|
|
193
|
+
assert(hash256(prevouts) == t_hashPrevouts, 'hashPrevouts mismatch');
|
|
194
|
+
assert(t_inputIndex < t_inputCount, 'invalid prevouts');
|
|
195
|
+
assert(t_inputCount == StdUtils.checkLenDivisibleBy(prevouts, 36n), 'invalid prevouts');
|
|
196
|
+
|
|
197
|
+
const b = slice(prevouts, t_inputIndex * 36n, (t_inputIndex + 1n) * 36n);
|
|
198
|
+
return {
|
|
199
|
+
txHash: slice(b, 0n, 32n),
|
|
200
|
+
outputIndex: StdUtils.byteStringToUInt32(slice(b, 32n, 36n)),
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Check if the spent scripts array passed in matches the shaSpentScripts
|
|
206
|
+
* @param spentScriptHashes array of spent scripts passed in that need to be verified
|
|
207
|
+
* @param t_hashSpentScripts the hash of the merged spent scripts, which comes from preimage and is trustable
|
|
208
|
+
* @param t_inputCount must be trustable, the number of inputs
|
|
209
|
+
*/
|
|
210
|
+
@method()
|
|
211
|
+
static checkSpentScripts(
|
|
212
|
+
spentScriptHashes: SpentScriptHashes,
|
|
213
|
+
t_hashSpentScripts: ByteString,
|
|
214
|
+
t_inputCount: bigint,
|
|
215
|
+
): void {
|
|
216
|
+
assert(hash256(spentScriptHashes) == t_hashSpentScripts, 'hashSpentScripts mismatch');
|
|
217
|
+
assert(t_inputCount == StdUtils.checkLenDivisibleBy(spentScriptHashes, 32n), 'invalid spentScriptHashes');
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Check if the spent amounts array passed in matches the shaSpentAmounts
|
|
222
|
+
* @param spentAmounts array of spent amounts passed in that need to be verified
|
|
223
|
+
* @param t_hashSpentAmounts the hash of the merged spent amounts, which comes from preimage and is trustable
|
|
224
|
+
* @param t_inputCount must be trustable, the number of inputs
|
|
225
|
+
*/
|
|
226
|
+
@method()
|
|
227
|
+
static checkSpentAmounts(
|
|
228
|
+
spentAmounts: SpentAmounts,
|
|
229
|
+
t_hashSpentAmounts: ByteString,
|
|
230
|
+
): Int32 {
|
|
231
|
+
assert(hash256(spentAmounts) == t_hashSpentAmounts, 'hashSpentAmounts mismatch');
|
|
232
|
+
return StdUtils.checkLenDivisibleBy(spentAmounts, 8n);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
@method()
|
|
236
|
+
static checkSpentDataHashes(
|
|
237
|
+
spentDataHashes: SpentDataHashes,
|
|
238
|
+
t_hashSpentDataHashes: ByteString,
|
|
239
|
+
t_inputCount: bigint,
|
|
240
|
+
): void {
|
|
241
|
+
assert(hash256(spentDataHashes) == t_hashSpentDataHashes, 'hashSpentDataHashes mismatch');
|
|
242
|
+
assert(t_inputCount == StdUtils.checkLenDivisibleBy(spentDataHashes, 32n), 'invalid spentDataHashes');
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
@method()
|
|
247
|
+
static getSpentScriptHash(
|
|
248
|
+
spentScriptHashes: SpentScriptHashes,
|
|
249
|
+
inputIndex: Int32,
|
|
250
|
+
): ByteString {
|
|
251
|
+
return slice(spentScriptHashes, inputIndex * 32n, (inputIndex + 1n) * 32n);
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
@method()
|
|
255
|
+
static getSpentAmount(
|
|
256
|
+
spentAmounts: SpentAmounts,
|
|
257
|
+
inputIndex: Int32,
|
|
258
|
+
): Int32 {
|
|
259
|
+
return StdUtils.fromLEUnsigned(slice(spentAmounts, inputIndex * 8n, (inputIndex + 1n) * 8n));
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
@method()
|
|
263
|
+
static getSpentDataHash(
|
|
264
|
+
spentDataHashes: SpentDataHashes,
|
|
265
|
+
inputIndex: Int32,
|
|
266
|
+
): ByteString {
|
|
267
|
+
return slice(spentDataHashes, inputIndex * 32n, (inputIndex + 1n) * 32n);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { ContextUtils } from './contextUtils.js';
|
|
2
|
+
export { TxUtils } from './txUtils.js';
|
|
3
|
+
export { StateUtils } from './stateUtils.js';
|
|
4
|
+
export { StdUtils } from './stdUtils.js';
|
|
5
|
+
export { Backtrace, type ChainTxVerifyResponse } from './backtrace.js';
|
|
6
|
+
export { StateLib } from './stateLib.js';
|
|
7
|
+
export { TxHashPreimageUtils } from './txHashPreimageUtils.js';
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { method, prop } from "../decorators.js"
|
|
2
|
+
import { assert } from "../fns/assert.js"
|
|
3
|
+
import { PubKey, Sig } from "../types/primitives.js"
|
|
4
|
+
import { SmartContract } from "../smartContract.js"
|
|
5
|
+
|
|
6
|
+
export class P2PK extends SmartContract {
|
|
7
|
+
@prop()
|
|
8
|
+
readonly pubKey: PubKey
|
|
9
|
+
|
|
10
|
+
constructor(pubKey: PubKey) {
|
|
11
|
+
super(...arguments)
|
|
12
|
+
this.pubKey = pubKey
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
@method()
|
|
16
|
+
public unlock(sig: Sig) {
|
|
17
|
+
assert(this.checkSig(sig, this.pubKey), 'signature check failed')
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const desc = {
|
|
22
|
+
version: 9,
|
|
23
|
+
compilerVersion: '1.19.0+commit.72eaeba',
|
|
24
|
+
contract: 'P2PK',
|
|
25
|
+
md5: '0c046dfb1f1a91cf72b9a852537bdfe1',
|
|
26
|
+
structs: [],
|
|
27
|
+
library: [],
|
|
28
|
+
alias: [],
|
|
29
|
+
abi: [
|
|
30
|
+
{
|
|
31
|
+
type: 'function',
|
|
32
|
+
name: 'unlock',
|
|
33
|
+
index: 0,
|
|
34
|
+
params: [
|
|
35
|
+
{
|
|
36
|
+
name: 'sig',
|
|
37
|
+
type: 'Sig',
|
|
38
|
+
},
|
|
39
|
+
],
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
type: 'constructor',
|
|
43
|
+
params: [
|
|
44
|
+
{
|
|
45
|
+
name: 'pubKey',
|
|
46
|
+
type: 'PubKey',
|
|
47
|
+
},
|
|
48
|
+
],
|
|
49
|
+
},
|
|
50
|
+
],
|
|
51
|
+
stateProps: [],
|
|
52
|
+
buildType: 'release',
|
|
53
|
+
file: '',
|
|
54
|
+
hex: '<pubKey>ac',
|
|
55
|
+
sourceMapFile: '',
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
P2PK.loadArtifact(desc)
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { method, prop } from "../decorators.js"
|
|
2
|
+
import { assert } from "../fns/assert.js"
|
|
3
|
+
import { Addr, PubKey, Sig } from "../types/primitives.js"
|
|
4
|
+
import { SmartContract } from "../smartContract.js"
|
|
5
|
+
import { pubKey2Addr } from "../fns/pubKey2Addr.js"
|
|
6
|
+
|
|
7
|
+
export class P2PKH extends SmartContract {
|
|
8
|
+
@prop()
|
|
9
|
+
readonly addr: Addr
|
|
10
|
+
|
|
11
|
+
constructor(addr: Addr) {
|
|
12
|
+
super(...arguments)
|
|
13
|
+
this.addr = addr
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
@method()
|
|
17
|
+
public unlock(sig: Sig, pubkey: PubKey) {
|
|
18
|
+
assert(
|
|
19
|
+
pubKey2Addr(pubkey) == this.addr,
|
|
20
|
+
'public key hashes are not equal'
|
|
21
|
+
)
|
|
22
|
+
assert(this.checkSig(sig, pubkey), 'signature check failed')
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const desc = {
|
|
27
|
+
version: 9,
|
|
28
|
+
compilerVersion: '1.19.0+commit.72eaeba',
|
|
29
|
+
contract: 'P2PKH',
|
|
30
|
+
md5: '0c046dfb1f1a91cf72b9a852537bdfe1',
|
|
31
|
+
structs: [],
|
|
32
|
+
library: [],
|
|
33
|
+
alias: [],
|
|
34
|
+
abi: [
|
|
35
|
+
{
|
|
36
|
+
type: 'function',
|
|
37
|
+
name: 'unlock',
|
|
38
|
+
index: 0,
|
|
39
|
+
params: [
|
|
40
|
+
{
|
|
41
|
+
name: 'sig',
|
|
42
|
+
type: 'Sig',
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
name: 'pubkey',
|
|
46
|
+
type: 'PubKey',
|
|
47
|
+
},
|
|
48
|
+
],
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
type: 'constructor',
|
|
52
|
+
params: [
|
|
53
|
+
{
|
|
54
|
+
name: 'addr',
|
|
55
|
+
type: 'Ripemd160',
|
|
56
|
+
},
|
|
57
|
+
],
|
|
58
|
+
},
|
|
59
|
+
],
|
|
60
|
+
stateProps: [],
|
|
61
|
+
buildType: 'release',
|
|
62
|
+
file: '',
|
|
63
|
+
hex: '76a9<addr>88ac',
|
|
64
|
+
sourceMapFile: '',
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
P2PKH.loadArtifact(desc)
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { SmartContractLib } from '../smartContractLib.js';
|
|
2
|
+
import { ByteString, OpcatState, StructObject } from '../types/primitives.js';
|
|
3
|
+
import { ABICoder } from '../abi.js';
|
|
4
|
+
import { deserializeState, serializeState } from '../stateSerializer.js';
|
|
5
|
+
import { getUnRenamedSymbol } from '../abiutils.js';
|
|
6
|
+
import { sha256 } from '../fns/index.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Library for computing the hash of a state.
|
|
10
|
+
* @category Library
|
|
11
|
+
* @onchain
|
|
12
|
+
*/
|
|
13
|
+
export class StateLib<ST extends OpcatState> extends SmartContractLib {
|
|
14
|
+
/**
|
|
15
|
+
* Calculate the hash of the state object
|
|
16
|
+
* @param state the state object
|
|
17
|
+
* @returns the hash byte string of the state object
|
|
18
|
+
* @onchain
|
|
19
|
+
* @category State
|
|
20
|
+
*/
|
|
21
|
+
static serializeState<T extends OpcatState>(
|
|
22
|
+
this: { new (...args: any[]): StateLib<T> },
|
|
23
|
+
state: T,
|
|
24
|
+
): ByteString {
|
|
25
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
26
|
+
const libraryClazz = this as any as typeof SmartContractLib;
|
|
27
|
+
const artifact = libraryClazz.artifact;
|
|
28
|
+
if (!artifact) {
|
|
29
|
+
throw new Error(`Artifact is not loaded for the library: ${this.name}`);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const abiCoder = new ABICoder(artifact);
|
|
33
|
+
const library = abiCoder.artifact.library.find(
|
|
34
|
+
(lib) => getUnRenamedSymbol(lib.name) === getUnRenamedSymbol(this.name),
|
|
35
|
+
);
|
|
36
|
+
if (!library) {
|
|
37
|
+
throw new Error(`Library ${this.name} is not found in the artifact`);
|
|
38
|
+
}
|
|
39
|
+
if (!library.stateType) {
|
|
40
|
+
throw new Error(`State type is not defined for the library: ${this.name}`);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return serializeState(artifact, libraryClazz.stateType, state);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
static deserializeState<T extends OpcatState>(
|
|
47
|
+
this: { new (...args: any[]): StateLib<T> },
|
|
48
|
+
serializedState: ByteString,
|
|
49
|
+
): T {
|
|
50
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
51
|
+
const libraryClazz = this as any as typeof SmartContractLib;
|
|
52
|
+
const artifact = libraryClazz.artifact;
|
|
53
|
+
if (!artifact) {
|
|
54
|
+
throw new Error(`Artifact is not loaded for the library: ${this.name}`);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const abiCoder = new ABICoder(artifact);
|
|
58
|
+
const library = abiCoder.artifact.library.find(
|
|
59
|
+
(lib) => getUnRenamedSymbol(lib.name) === getUnRenamedSymbol(this.name),
|
|
60
|
+
);
|
|
61
|
+
if (!library) {
|
|
62
|
+
throw new Error(`Library ${this.name} is not found in the artifact`);
|
|
63
|
+
}
|
|
64
|
+
if (!library.stateType) {
|
|
65
|
+
throw new Error(`State type is not defined for the library: ${this.name}`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
return deserializeState(artifact, libraryClazz.stateType, serializedState);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
static stateHash<T extends OpcatState>(
|
|
72
|
+
this: { new (...args: any[]): StateLib<T> },
|
|
73
|
+
state: T,
|
|
74
|
+
): ByteString {
|
|
75
|
+
return sha256((this as any).serializeState(state));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// keep this method to enable the typecheck for stateHash method in ts
|
|
79
|
+
private _id(_state: ST): ST {
|
|
80
|
+
return _state;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { assert } from '../fns/assert.js';
|
|
2
|
+
import { method } from '../decorators.js';
|
|
3
|
+
import { slice } from '../fns/byteString.js';
|
|
4
|
+
import { SmartContractLib } from '../smartContractLib.js';
|
|
5
|
+
import { ByteString, Int32, UInt32 } from '../types/primitives.js';
|
|
6
|
+
import { SpentDataHashes } from '../types/structs.js';
|
|
7
|
+
import { TX_OUTPUT_DATA_HASH_LEN } from '../consts.js';
|
|
8
|
+
import { sha256 } from '../fns/hashes.js';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* spentDatas library
|
|
12
|
+
* @category Library
|
|
13
|
+
* @onchain
|
|
14
|
+
*/
|
|
15
|
+
export class StateUtils extends SmartContractLib {
|
|
16
|
+
/**
|
|
17
|
+
* Check if the dataHash of the passed-in raw state matches the spent data hash
|
|
18
|
+
* @param rawState passed-in raw state after serialization
|
|
19
|
+
* @param t_spentDataHashes trustable spent data hashes
|
|
20
|
+
* @param t_inputIndex trustable input index
|
|
21
|
+
*/
|
|
22
|
+
@method()
|
|
23
|
+
static checkInputState(
|
|
24
|
+
t_inputIndex: UInt32,
|
|
25
|
+
rawState: ByteString,
|
|
26
|
+
t_spentDataHashes: SpentDataHashes,
|
|
27
|
+
): void {
|
|
28
|
+
assert(
|
|
29
|
+
slice(
|
|
30
|
+
t_spentDataHashes,
|
|
31
|
+
t_inputIndex * TX_OUTPUT_DATA_HASH_LEN,
|
|
32
|
+
(t_inputIndex + 1n) * TX_OUTPUT_DATA_HASH_LEN
|
|
33
|
+
) == sha256(rawState),
|
|
34
|
+
'dataHash of state mismatch'
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { method } from "../decorators.js";
|
|
2
|
+
import { assert } from "../fns/assert.js";
|
|
3
|
+
import { byteStringToInt, intToByteString, len, slice, toByteString } from "../fns/byteString.js";
|
|
4
|
+
import { SmartContractLib } from "../smartContractLib.js";
|
|
5
|
+
import { ByteString, PrivKey, UInt32, UInt64 } from "../types/primitives.js";
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
export const UINT64_MAX = 0xffffffffffffffffn;
|
|
9
|
+
export const UINT64_MIN = 0n;
|
|
10
|
+
export const UINT32_MAX = 0xffffffffn;
|
|
11
|
+
export const UINT32_MIN = 0n;
|
|
12
|
+
|
|
13
|
+
type ReadVarintResult = {
|
|
14
|
+
data: ByteString;
|
|
15
|
+
nextPos: bigint;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export class StdUtils extends SmartContractLib {
|
|
19
|
+
@method()
|
|
20
|
+
static checkLenDivisibleBy(b: ByteString, n: bigint): bigint {
|
|
21
|
+
const l = len(b);
|
|
22
|
+
assert(l % n == 0n, 'length of b is not divisible by n');
|
|
23
|
+
return l / n;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@method()
|
|
28
|
+
static uint64ToByteString(n: UInt64): ByteString {
|
|
29
|
+
assert(n >= UINT64_MIN && n <= UINT64_MAX, 'uint64 out of range');
|
|
30
|
+
return StdUtils.toLEUnsigned(n, 8n);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
@method()
|
|
34
|
+
static uint32ToByteString(n: UInt32): ByteString {
|
|
35
|
+
assert(n >= UINT32_MIN && n <= UINT32_MAX, 'uint32 out of range');
|
|
36
|
+
return StdUtils.toLEUnsigned(n, 4n)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
@method()
|
|
40
|
+
static byteStringToUInt32(b: ByteString): UInt32 {
|
|
41
|
+
assert(len(b) == 4n, 'byteStringToUInt32: byteString length is not 4');
|
|
42
|
+
return StdUtils.fromLEUnsigned(b);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* convert signed integer `n` to unsigned integer of `l` string, in little endian
|
|
47
|
+
* @param {bigint} n the number to be converted
|
|
48
|
+
* @param {bigint} l expected length
|
|
49
|
+
* @returns {ByteString} returns a `ByteString`
|
|
50
|
+
*/
|
|
51
|
+
@method()
|
|
52
|
+
static toLEUnsigned(n: bigint, l: bigint): ByteString {
|
|
53
|
+
const m = intToByteString(n, l + 1n);
|
|
54
|
+
// remove sign byte
|
|
55
|
+
return slice(m, 0n, l);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* convert `ByteString` to unsigned integer, in sign-magnitude little endian
|
|
60
|
+
* @param {ByteString} bytes the `ByteString` to be converted
|
|
61
|
+
* @returns {bigint} returns a number
|
|
62
|
+
*/
|
|
63
|
+
@method()
|
|
64
|
+
static fromLEUnsigned(b: ByteString): bigint {
|
|
65
|
+
return byteStringToInt(b + toByteString('00'));
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
@method()
|
|
69
|
+
static writeVarInt(n: bigint): ByteString {
|
|
70
|
+
let b: ByteString = toByteString('');
|
|
71
|
+
let size = 0n;
|
|
72
|
+
if (n < 0xfdn) {
|
|
73
|
+
size = 1n;
|
|
74
|
+
}
|
|
75
|
+
else if (n < 0x10000n) {
|
|
76
|
+
b = toByteString('fd')
|
|
77
|
+
size = 2n;
|
|
78
|
+
}
|
|
79
|
+
else if (n < 0x100000000n) {
|
|
80
|
+
b = toByteString('fe')
|
|
81
|
+
size = 4n;
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
b = toByteString('ff')
|
|
85
|
+
size = 8n;
|
|
86
|
+
}
|
|
87
|
+
return b + StdUtils.toLEUnsigned(n, size);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* serializes `ByteString` with `VarInt` encoding
|
|
93
|
+
* @param buf a `ByteString`
|
|
94
|
+
* @returns serialized `ByteString`
|
|
95
|
+
*/
|
|
96
|
+
@method()
|
|
97
|
+
static pushData(buf: ByteString): ByteString {
|
|
98
|
+
let n = len(buf);
|
|
99
|
+
|
|
100
|
+
let header: ByteString = toByteString('');
|
|
101
|
+
|
|
102
|
+
if (n < 0x4c) {
|
|
103
|
+
header = StdUtils.toLEUnsigned(n, 1n);
|
|
104
|
+
}
|
|
105
|
+
else if (n < 0x100) {
|
|
106
|
+
header = toByteString('4c') + StdUtils.toLEUnsigned(n, 1n);
|
|
107
|
+
}
|
|
108
|
+
else if (n < 0x10000) {
|
|
109
|
+
header = toByteString('4d') + StdUtils.toLEUnsigned(n, 2n);
|
|
110
|
+
}
|
|
111
|
+
else if (n < 0x100000000) {
|
|
112
|
+
header = toByteString('4e') + StdUtils.toLEUnsigned(n, 4n);
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
// shall not reach here
|
|
116
|
+
assert(false);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
return header + buf;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* read [VarInt (variable integer)]{@link https://learnmeabitcoin.com/technical/general/compact-size/}-encoded data from the beginning of 'buf'
|
|
124
|
+
* @param {ByteString} buf a buffer `ByteString` of format: [prefix FD/FE/FF +] length + data
|
|
125
|
+
* @returns return data
|
|
126
|
+
*/
|
|
127
|
+
@method()
|
|
128
|
+
static readVarint(buf: ByteString, pos: bigint): ReadVarintResult {
|
|
129
|
+
let l: bigint = 0n;
|
|
130
|
+
let ret: ByteString = toByteString('');
|
|
131
|
+
let nextPos: bigint = pos;
|
|
132
|
+
const header: ByteString = slice(buf, pos, pos + 1n);
|
|
133
|
+
|
|
134
|
+
if (header == toByteString('fd')) {
|
|
135
|
+
l = StdUtils.fromLEUnsigned(slice(buf, pos + 1n, pos + 3n));
|
|
136
|
+
ret = slice(buf, 3n, 3n + l);
|
|
137
|
+
nextPos = pos + 3n + l;
|
|
138
|
+
}
|
|
139
|
+
else if (header == toByteString('fe')) {
|
|
140
|
+
l = StdUtils.fromLEUnsigned(slice(buf, pos + 1n, pos + 5n));
|
|
141
|
+
ret = slice(buf, pos + 5n, pos + 5n + l);
|
|
142
|
+
nextPos = pos + 5n + l;
|
|
143
|
+
}
|
|
144
|
+
else if (header == toByteString('ff')) {
|
|
145
|
+
l = StdUtils.fromLEUnsigned(slice(buf, pos + 1n, pos + 9n));
|
|
146
|
+
ret = slice(buf, pos + 9n, pos + 9n + l);
|
|
147
|
+
nextPos = pos + 9n + l;
|
|
148
|
+
} else {
|
|
149
|
+
l = StdUtils.fromLEUnsigned(slice(buf, pos, pos + 1n));
|
|
150
|
+
ret = slice(buf, pos + 1n, pos + 1n + l);
|
|
151
|
+
nextPos = pos + 1n + l;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return { data: ret, nextPos: nextPos };
|
|
155
|
+
}
|
|
156
|
+
}
|