@helios-lang/effect 0.1.14 → 0.2.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/dist/Cardano/CoinSelection.js +130 -0
- package/dist/Cardano/CoinSelection.js.map +1 -0
- package/dist/{Ledger → Cardano/Ledger}/Address.js +30 -20
- package/dist/Cardano/Ledger/Address.js.map +1 -0
- package/dist/{Ledger → Cardano/Ledger}/AssetClass.js +16 -11
- package/dist/Cardano/Ledger/AssetClass.js.map +1 -0
- package/dist/Cardano/Ledger/Assets.js +235 -0
- package/dist/Cardano/Ledger/Assets.js.map +1 -0
- package/dist/Cardano/Ledger/Credential.js +53 -0
- package/dist/Cardano/Ledger/Credential.js.map +1 -0
- package/dist/Cardano/Ledger/DCert.js +117 -0
- package/dist/Cardano/Ledger/DCert.js.map +1 -0
- package/dist/Cardano/Ledger/DatumHash.js +25 -0
- package/dist/Cardano/Ledger/DatumHash.js.map +1 -0
- package/dist/Cardano/Ledger/MintingPolicy.js +40 -0
- package/dist/Cardano/Ledger/MintingPolicy.js.map +1 -0
- package/dist/Cardano/Ledger/NativeScript.js +140 -0
- package/dist/Cardano/Ledger/NativeScript.js.map +1 -0
- package/dist/Cardano/Ledger/PubKey.js +22 -0
- package/dist/Cardano/Ledger/PubKey.js.map +1 -0
- package/dist/{Ledger → Cardano/Ledger}/PubKeyHash.js +5 -5
- package/dist/Cardano/Ledger/PubKeyHash.js.map +1 -0
- package/dist/Cardano/Ledger/Redeemer.js +155 -0
- package/dist/Cardano/Ledger/Redeemer.js.map +1 -0
- package/dist/Cardano/Ledger/RewardAddress.js +93 -0
- package/dist/Cardano/Ledger/RewardAddress.js.map +1 -0
- package/dist/Cardano/Ledger/Signature.js +23 -0
- package/dist/Cardano/Ledger/Signature.js.map +1 -0
- package/dist/Cardano/Ledger/Tx.js +498 -0
- package/dist/Cardano/Ledger/Tx.js.map +1 -0
- package/dist/Cardano/Ledger/TxHash.js +38 -0
- package/dist/Cardano/Ledger/TxHash.js.map +1 -0
- package/dist/Cardano/Ledger/TxOutput.js +167 -0
- package/dist/Cardano/Ledger/TxOutput.js.map +1 -0
- package/dist/{Ledger → Cardano/Ledger}/TxOutputDatum.js +44 -8
- package/dist/Cardano/Ledger/TxOutputDatum.js.map +1 -0
- package/dist/Cardano/Ledger/UTxO.js +100 -0
- package/dist/Cardano/Ledger/UTxO.js.map +1 -0
- package/dist/{Ledger → Cardano/Ledger}/UTxORef.js +28 -6
- package/dist/Cardano/Ledger/UTxORef.js.map +1 -0
- package/dist/{Ledger → Cardano/Ledger}/ValidatorHash.js +5 -5
- package/dist/Cardano/Ledger/ValidatorHash.js.map +1 -0
- package/dist/{Ledger → Cardano/Ledger}/index.js +5 -0
- package/dist/Cardano/Ledger/index.js.map +1 -0
- package/dist/Cardano/Network/IsMainnet.js.map +1 -0
- package/dist/{Network → Cardano/Network}/Params.js +27 -3
- package/dist/Cardano/Network/Params.js.map +1 -0
- package/dist/Cardano/Network/UTxO.js +20 -0
- package/dist/Cardano/Network/UTxO.js.map +1 -0
- package/dist/Cardano/Network/UTxOsAt.js +4 -0
- package/dist/Cardano/Network/UTxOsAt.js.map +1 -0
- package/dist/Cardano/Network/errors.js +33 -0
- package/dist/Cardano/Network/errors.js.map +1 -0
- package/dist/{Network → Cardano/Network}/index.js +1 -0
- package/dist/Cardano/Network/index.js.map +1 -0
- package/dist/Cardano/TxBuilder.js +946 -0
- package/dist/Cardano/TxBuilder.js.map +1 -0
- package/dist/Cardano/Uplc/Builtins.js +1820 -0
- package/dist/Cardano/Uplc/Builtins.js.map +1 -0
- package/dist/Cardano/Uplc/Cek.js +716 -0
- package/dist/Cardano/Uplc/Cek.js.map +1 -0
- package/dist/Cardano/Uplc/Cost.js +956 -0
- package/dist/Cardano/Uplc/Cost.js.map +1 -0
- package/dist/{Uplc → Cardano/Uplc}/Data.js +143 -22
- package/dist/Cardano/Uplc/Data.js.map +1 -0
- package/dist/Cardano/Uplc/Script.js +118 -0
- package/dist/Cardano/Uplc/Script.js.map +1 -0
- package/dist/Cardano/Uplc/ScriptContext.js +259 -0
- package/dist/Cardano/Uplc/ScriptContext.js.map +1 -0
- package/dist/Cardano/Uplc/Term.js +384 -0
- package/dist/Cardano/Uplc/Term.js.map +1 -0
- package/dist/Cardano/Uplc/Type.js +131 -0
- package/dist/Cardano/Uplc/Type.js.map +1 -0
- package/dist/Cardano/Uplc/Value.js +315 -0
- package/dist/Cardano/Uplc/Value.js.map +1 -0
- package/dist/Cardano/Uplc/index.js +7 -0
- package/dist/Cardano/Uplc/index.js.map +1 -0
- package/dist/Cardano/index.js +6 -0
- package/dist/Cardano/index.js.map +1 -0
- package/dist/{internal → Codecs}/Base32.js +2 -2
- package/dist/Codecs/Base32.js.map +1 -0
- package/dist/{Bech32.js → Codecs/Bech32.js} +2 -2
- package/dist/Codecs/Bech32.js.map +1 -0
- package/dist/Codecs/BigEndian.js.map +1 -0
- package/dist/Codecs/Bits.js.map +1 -0
- package/dist/{internal → Codecs}/Bytes.js +23 -12
- package/dist/Codecs/Bytes.js.map +1 -0
- package/dist/{Cbor.js → Codecs/Cbor.js} +559 -328
- package/dist/Codecs/Cbor.js.map +1 -0
- package/dist/{internal → Codecs}/Flat.js +16 -0
- package/dist/Codecs/Flat.js.map +1 -0
- package/dist/Codecs/Float.js.map +1 -0
- package/dist/Codecs/LittleEndian.js +27 -0
- package/dist/Codecs/LittleEndian.js.map +1 -0
- package/dist/Codecs/Uint64.js +89 -0
- package/dist/Codecs/Uint64.js.map +1 -0
- package/dist/{internal → Codecs}/Utf8.js +7 -2
- package/dist/Codecs/Utf8.js.map +1 -0
- package/dist/Codecs/ZigZag.js +26 -0
- package/dist/Codecs/ZigZag.js.map +1 -0
- package/dist/Codecs/index.js +8 -0
- package/dist/Codecs/index.js.map +1 -0
- package/dist/Crypto/Blake2b.js +156 -0
- package/dist/Crypto/Blake2b.js.map +1 -0
- package/dist/Crypto/Curve.js +92 -0
- package/dist/Crypto/Curve.js.map +1 -0
- package/dist/Crypto/Ed25519.js +323 -0
- package/dist/Crypto/Ed25519.js.map +1 -0
- package/dist/Crypto/EdDSA.js +222 -0
- package/dist/Crypto/EdDSA.js.map +1 -0
- package/dist/Crypto/Field.js +323 -0
- package/dist/Crypto/Field.js.map +1 -0
- package/dist/Crypto/Hmac.js +51 -0
- package/dist/Crypto/Hmac.js.map +1 -0
- package/dist/Crypto/Keccak.js +167 -0
- package/dist/Crypto/Keccak.js.map +1 -0
- package/dist/Crypto/Pbkdf2.js +45 -0
- package/dist/Crypto/Pbkdf2.js.map +1 -0
- package/dist/Crypto/Sha2_256.js +200 -0
- package/dist/Crypto/Sha2_256.js.map +1 -0
- package/dist/Crypto/Sha2_512.js +264 -0
- package/dist/Crypto/Sha2_512.js.map +1 -0
- package/dist/Crypto/Sha3_256.js +10 -0
- package/dist/Crypto/Sha3_256.js.map +1 -0
- package/dist/Crypto/errors.js +9 -0
- package/dist/Crypto/errors.js.map +1 -0
- package/dist/Crypto/index.js +12 -0
- package/dist/Crypto/index.js.map +1 -0
- package/dist/index.js +3 -5
- package/dist/index.js.map +1 -1
- package/package.json +24 -6
- package/tsconfig.build.json +1 -1
- package/tsconfig.build.tsbuildinfo +1 -1
- package/tsconfig.tsbuildinfo +1 -1
- package/types/Cardano/CoinSelection.d.ts +26 -0
- package/types/Cardano/CoinSelection.d.ts.map +1 -0
- package/types/{Ledger → Cardano/Ledger}/Address.d.ts +7 -6
- package/types/Cardano/Ledger/Address.d.ts.map +1 -0
- package/types/{Ledger → Cardano/Ledger}/AssetClass.d.ts +2 -2
- package/types/Cardano/Ledger/AssetClass.d.ts.map +1 -0
- package/types/Cardano/Ledger/Assets.d.ts +114 -0
- package/types/Cardano/Ledger/Assets.d.ts.map +1 -0
- package/types/{Ledger → Cardano/Ledger}/Credential.d.ts +10 -4
- package/types/Cardano/Ledger/Credential.d.ts.map +1 -0
- package/types/Cardano/Ledger/DCert.d.ts +40 -0
- package/types/Cardano/Ledger/DCert.d.ts.map +1 -0
- package/types/{Ledger → Cardano/Ledger}/DatumHash.d.ts +6 -5
- package/types/Cardano/Ledger/DatumHash.d.ts.map +1 -0
- package/types/{Ledger → Cardano/Ledger}/MintingPolicy.d.ts +11 -8
- package/types/Cardano/Ledger/MintingPolicy.d.ts.map +1 -0
- package/types/Cardano/Ledger/NativeScript.d.ts +105 -0
- package/types/Cardano/Ledger/NativeScript.d.ts.map +1 -0
- package/types/Cardano/Ledger/PubKey.d.ts +41 -0
- package/types/Cardano/Ledger/PubKey.d.ts.map +1 -0
- package/types/{Ledger → Cardano/Ledger}/PubKeyHash.d.ts +5 -5
- package/types/Cardano/Ledger/PubKeyHash.d.ts.map +1 -0
- package/types/Cardano/Ledger/Redeemer.d.ts +303 -0
- package/types/Cardano/Ledger/Redeemer.d.ts.map +1 -0
- package/types/Cardano/Ledger/RewardAddress.d.ts +60 -0
- package/types/Cardano/Ledger/RewardAddress.d.ts.map +1 -0
- package/types/Cardano/Ledger/Signature.d.ts +13 -0
- package/types/Cardano/Ledger/Signature.d.ts.map +1 -0
- package/types/Cardano/Ledger/Tx.d.ts +375 -0
- package/types/Cardano/Ledger/Tx.d.ts.map +1 -0
- package/types/{Ledger → Cardano/Ledger}/TxHash.d.ts +35 -5
- package/types/Cardano/Ledger/TxHash.d.ts.map +1 -0
- package/types/Cardano/Ledger/TxOutput.d.ts +438 -0
- package/types/Cardano/Ledger/TxOutput.d.ts.map +1 -0
- package/types/Cardano/Ledger/TxOutputDatum.d.ts +98 -0
- package/types/Cardano/Ledger/TxOutputDatum.d.ts.map +1 -0
- package/types/Cardano/Ledger/UTxO.d.ts +1420 -0
- package/types/Cardano/Ledger/UTxO.d.ts.map +1 -0
- package/types/Cardano/Ledger/UTxORef.d.ts +222 -0
- package/types/Cardano/Ledger/UTxORef.d.ts.map +1 -0
- package/types/{Ledger → Cardano/Ledger}/ValidatorHash.d.ts +5 -5
- package/types/Cardano/Ledger/ValidatorHash.d.ts.map +1 -0
- package/types/{Ledger → Cardano/Ledger}/index.d.ts +5 -0
- package/types/Cardano/Ledger/index.d.ts.map +1 -0
- package/types/Cardano/Network/IsMainnet.d.ts.map +1 -0
- package/types/{Network → Cardano/Network}/Params.d.ts +32 -4
- package/types/Cardano/Network/Params.d.ts.map +1 -0
- package/types/Cardano/Network/UTxO.d.ts +15 -0
- package/types/Cardano/Network/UTxO.d.ts.map +1 -0
- package/types/Cardano/Network/UTxOsAt.d.ts +9 -0
- package/types/Cardano/Network/UTxOsAt.d.ts.map +1 -0
- package/types/Cardano/Network/errors.d.ts +39 -0
- package/types/Cardano/Network/errors.d.ts.map +1 -0
- package/types/{Network → Cardano/Network}/index.d.ts +1 -0
- package/types/Cardano/Network/index.d.ts.map +1 -0
- package/types/Cardano/TxBuilder.d.ts +261 -0
- package/types/Cardano/TxBuilder.d.ts.map +1 -0
- package/types/Cardano/Uplc/Builtins.d.ts +163 -0
- package/types/Cardano/Uplc/Builtins.d.ts.map +1 -0
- package/types/Cardano/Uplc/Cek.d.ts +165 -0
- package/types/Cardano/Uplc/Cek.d.ts.map +1 -0
- package/types/Cardano/Uplc/Cost.d.ts +93 -0
- package/types/Cardano/Uplc/Cost.d.ts.map +1 -0
- package/types/{Uplc → Cardano/Uplc}/Data.d.ts +149 -15
- package/types/Cardano/Uplc/Data.d.ts.map +1 -0
- package/types/Cardano/Uplc/Script.d.ts +87 -0
- package/types/Cardano/Uplc/Script.d.ts.map +1 -0
- package/types/Cardano/Uplc/ScriptContext.d.ts +21 -0
- package/types/Cardano/Uplc/ScriptContext.d.ts.map +1 -0
- package/types/Cardano/Uplc/Term.d.ts +454 -0
- package/types/Cardano/Uplc/Term.d.ts.map +1 -0
- package/types/Cardano/Uplc/Type.d.ts +29 -0
- package/types/Cardano/Uplc/Type.d.ts.map +1 -0
- package/types/Cardano/Uplc/Value.d.ts +152 -0
- package/types/Cardano/Uplc/Value.d.ts.map +1 -0
- package/types/Cardano/Uplc/index.d.ts +7 -0
- package/types/Cardano/Uplc/index.d.ts.map +1 -0
- package/types/Cardano/index.d.ts +6 -0
- package/types/Cardano/index.d.ts.map +1 -0
- package/types/Codecs/Base32.d.ts.map +1 -0
- package/types/Codecs/Bech32.d.ts.map +1 -0
- package/types/Codecs/BigEndian.d.ts.map +1 -0
- package/types/Codecs/Bits.d.ts.map +1 -0
- package/types/{internal → Codecs}/Bytes.d.ts +7 -6
- package/types/Codecs/Bytes.d.ts.map +1 -0
- package/types/{Cbor.d.ts → Codecs/Cbor.d.ts} +77 -55
- package/types/Codecs/Cbor.d.ts.map +1 -0
- package/types/{internal → Codecs}/Flat.d.ts +2 -0
- package/types/Codecs/Flat.d.ts.map +1 -0
- package/types/Codecs/Float.d.ts.map +1 -0
- package/types/Codecs/LittleEndian.d.ts +18 -0
- package/types/Codecs/LittleEndian.d.ts.map +1 -0
- package/types/Codecs/Uint64.d.ts +16 -0
- package/types/Codecs/Uint64.d.ts.map +1 -0
- package/types/{internal → Codecs}/Utf8.d.ts +2 -2
- package/types/Codecs/Utf8.d.ts.map +1 -0
- package/types/Codecs/ZigZag.d.ts +3 -0
- package/types/Codecs/ZigZag.d.ts.map +1 -0
- package/types/Codecs/index.d.ts +8 -0
- package/types/Codecs/index.d.ts.map +1 -0
- package/types/Crypto/Blake2b.d.ts +16 -0
- package/types/Crypto/Blake2b.d.ts.map +1 -0
- package/types/Crypto/Curve.d.ts +161 -0
- package/types/Crypto/Curve.d.ts.map +1 -0
- package/types/Crypto/Ed25519.d.ts +155 -0
- package/types/Crypto/Ed25519.d.ts.map +1 -0
- package/types/Crypto/EdDSA.d.ts +159 -0
- package/types/Crypto/EdDSA.d.ts.map +1 -0
- package/types/Crypto/Field.d.ts +273 -0
- package/types/Crypto/Field.d.ts.map +1 -0
- package/types/Crypto/Hmac.d.ts +20 -0
- package/types/Crypto/Hmac.d.ts.map +1 -0
- package/types/Crypto/Keccak.d.ts +16 -0
- package/types/Crypto/Keccak.d.ts.map +1 -0
- package/types/Crypto/Pbkdf2.d.ts +15 -0
- package/types/Crypto/Pbkdf2.d.ts.map +1 -0
- package/types/Crypto/Sha2_256.d.ts +36 -0
- package/types/Crypto/Sha2_256.d.ts.map +1 -0
- package/types/Crypto/Sha2_512.d.ts +31 -0
- package/types/Crypto/Sha2_512.d.ts.map +1 -0
- package/types/Crypto/Sha3_256.d.ts +8 -0
- package/types/Crypto/Sha3_256.d.ts.map +1 -0
- package/types/Crypto/errors.d.ts +10 -0
- package/types/Crypto/errors.d.ts.map +1 -0
- package/types/Crypto/index.d.ts +12 -0
- package/types/Crypto/index.d.ts.map +1 -0
- package/types/index.d.ts +3 -5
- package/types/index.d.ts.map +1 -1
- package/dist/Address.js +0 -13
- package/dist/Address.js.map +0 -1
- package/dist/Bech32.js.map +0 -1
- package/dist/Cbor.js.map +0 -1
- package/dist/Ledger/Address.js.map +0 -1
- package/dist/Ledger/AssetClass.js.map +0 -1
- package/dist/Ledger/Assets.js +0 -120
- package/dist/Ledger/Assets.js.map +0 -1
- package/dist/Ledger/Credential.js +0 -17
- package/dist/Ledger/Credential.js.map +0 -1
- package/dist/Ledger/DatumHash.js +0 -21
- package/dist/Ledger/DatumHash.js.map +0 -1
- package/dist/Ledger/IsMainnet.js +0 -4
- package/dist/Ledger/IsMainnet.js.map +0 -1
- package/dist/Ledger/MintingPolicy.js +0 -45
- package/dist/Ledger/MintingPolicy.js.map +0 -1
- package/dist/Ledger/NetworkParams.js +0 -40
- package/dist/Ledger/NetworkParams.js.map +0 -1
- package/dist/Ledger/PubKeyHash.js.map +0 -1
- package/dist/Ledger/TxHash.js +0 -23
- package/dist/Ledger/TxHash.js.map +0 -1
- package/dist/Ledger/TxId.js +0 -23
- package/dist/Ledger/TxId.js.map +0 -1
- package/dist/Ledger/TxInput.js +0 -51
- package/dist/Ledger/TxInput.js.map +0 -1
- package/dist/Ledger/TxOutput.js +0 -118
- package/dist/Ledger/TxOutput.js.map +0 -1
- package/dist/Ledger/TxOutputDatum.js.map +0 -1
- package/dist/Ledger/TxOutputId.js +0 -55
- package/dist/Ledger/TxOutputId.js.map +0 -1
- package/dist/Ledger/UTxO.js +0 -43
- package/dist/Ledger/UTxO.js.map +0 -1
- package/dist/Ledger/UTxORef.js.map +0 -1
- package/dist/Ledger/ValidatorHash.js.map +0 -1
- package/dist/Ledger/index.js.map +0 -1
- package/dist/Network/IsMainnet.js.map +0 -1
- package/dist/Network/Params.js.map +0 -1
- package/dist/Network/UTxOsAt.js +0 -4
- package/dist/Network/UTxOsAt.js.map +0 -1
- package/dist/Network/errors.js +0 -16
- package/dist/Network/errors.js.map +0 -1
- package/dist/Network/index.js.map +0 -1
- package/dist/Uplc/Cek.js +0 -3
- package/dist/Uplc/Cek.js.map +0 -1
- package/dist/Uplc/Data.js.map +0 -1
- package/dist/Uplc/DataSchema.js +0 -118
- package/dist/Uplc/DataSchema.js.map +0 -1
- package/dist/Uplc/Primitive.js +0 -23
- package/dist/Uplc/Primitive.js.map +0 -1
- package/dist/Uplc/index.js +0 -3
- package/dist/Uplc/index.js.map +0 -1
- package/dist/internal/Base32.js.map +0 -1
- package/dist/internal/BigEndian.js.map +0 -1
- package/dist/internal/Bits.js.map +0 -1
- package/dist/internal/Bytes.js.map +0 -1
- package/dist/internal/Flat.js.map +0 -1
- package/dist/internal/Float.js.map +0 -1
- package/dist/internal/Utf8.js.map +0 -1
- package/tsconfig.check.tsbuildinfo +0 -1
- package/types/Address.d.ts +0 -5
- package/types/Address.d.ts.map +0 -1
- package/types/Bech32.d.ts.map +0 -1
- package/types/Cbor.d.ts.map +0 -1
- package/types/Ledger/Address.d.ts.map +0 -1
- package/types/Ledger/AssetClass.d.ts.map +0 -1
- package/types/Ledger/Assets.d.ts +0 -70
- package/types/Ledger/Assets.d.ts.map +0 -1
- package/types/Ledger/Credential.d.ts.map +0 -1
- package/types/Ledger/DatumHash.d.ts.map +0 -1
- package/types/Ledger/IsMainnet.d.ts +0 -6
- package/types/Ledger/IsMainnet.d.ts.map +0 -1
- package/types/Ledger/MintingPolicy.d.ts.map +0 -1
- package/types/Ledger/NetworkParams.d.ts +0 -40
- package/types/Ledger/NetworkParams.d.ts.map +0 -1
- package/types/Ledger/PubKeyHash.d.ts.map +0 -1
- package/types/Ledger/TxHash.d.ts.map +0 -1
- package/types/Ledger/TxId.d.ts +0 -70
- package/types/Ledger/TxId.d.ts.map +0 -1
- package/types/Ledger/TxInput.d.ts +0 -55
- package/types/Ledger/TxInput.d.ts.map +0 -1
- package/types/Ledger/TxOutput.d.ts +0 -63
- package/types/Ledger/TxOutput.d.ts.map +0 -1
- package/types/Ledger/TxOutputDatum.d.ts +0 -41
- package/types/Ledger/TxOutputDatum.d.ts.map +0 -1
- package/types/Ledger/TxOutputId.d.ts +0 -133
- package/types/Ledger/TxOutputId.d.ts.map +0 -1
- package/types/Ledger/UTxO.d.ts +0 -55
- package/types/Ledger/UTxO.d.ts.map +0 -1
- package/types/Ledger/UTxORef.d.ts +0 -133
- package/types/Ledger/UTxORef.d.ts.map +0 -1
- package/types/Ledger/ValidatorHash.d.ts.map +0 -1
- package/types/Ledger/index.d.ts.map +0 -1
- package/types/Network/IsMainnet.d.ts.map +0 -1
- package/types/Network/Params.d.ts.map +0 -1
- package/types/Network/UTxOsAt.d.ts +0 -9
- package/types/Network/UTxOsAt.d.ts.map +0 -1
- package/types/Network/errors.d.ts +0 -18
- package/types/Network/errors.d.ts.map +0 -1
- package/types/Network/index.d.ts.map +0 -1
- package/types/Uplc/Cek.d.ts +0 -72
- package/types/Uplc/Cek.d.ts.map +0 -1
- package/types/Uplc/Data.d.ts.map +0 -1
- package/types/Uplc/DataSchema.d.ts +0 -227
- package/types/Uplc/DataSchema.d.ts.map +0 -1
- package/types/Uplc/Primitive.d.ts +0 -26
- package/types/Uplc/Primitive.d.ts.map +0 -1
- package/types/Uplc/index.d.ts +0 -3
- package/types/Uplc/index.d.ts.map +0 -1
- package/types/internal/Base32.d.ts.map +0 -1
- package/types/internal/BigEndian.d.ts.map +0 -1
- package/types/internal/Bits.d.ts.map +0 -1
- package/types/internal/Bytes.d.ts.map +0 -1
- package/types/internal/Flat.d.ts.map +0 -1
- package/types/internal/Float.d.ts.map +0 -1
- package/types/internal/Utf8.d.ts.map +0 -1
- /package/dist/{Network → Cardano/Network}/IsMainnet.js +0 -0
- /package/dist/{internal → Codecs}/BigEndian.js +0 -0
- /package/dist/{internal → Codecs}/Bits.js +0 -0
- /package/dist/{internal → Codecs}/Float.js +0 -0
- /package/types/{Network → Cardano/Network}/IsMainnet.d.ts +0 -0
- /package/types/{internal → Codecs}/Base32.d.ts +0 -0
- /package/types/{Bech32.d.ts → Codecs/Bech32.d.ts} +0 -0
- /package/types/{internal → Codecs}/BigEndian.d.ts +0 -0
- /package/types/{internal → Codecs}/Bits.d.ts +0 -0
- /package/types/{internal → Codecs}/Float.d.ts +0 -0
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { Data,
|
|
2
|
-
import * as BigEndian from "./
|
|
3
|
-
import * as Bytes from "./
|
|
4
|
-
import * as Float from "./
|
|
5
|
-
import * as Utf8 from "./
|
|
1
|
+
import { Data, Either } from "effect";
|
|
2
|
+
import * as BigEndian from "./BigEndian.js";
|
|
3
|
+
import * as Bytes from "./Bytes.js";
|
|
4
|
+
import * as Float from "./Float.js";
|
|
5
|
+
import * as Utf8 from "./Utf8.js";
|
|
6
6
|
const FALSE_BYTE = 244; // m = 7, n = 20
|
|
7
7
|
const TRUE_BYTE = 245; // m = 7, n = 21
|
|
8
8
|
export class DecodeError extends Data.TaggedError("Cbor.DecodeError") {
|
|
@@ -16,19 +16,20 @@ export class DecodeError extends Data.TaggedError("Cbor.DecodeError") {
|
|
|
16
16
|
* @param bytes
|
|
17
17
|
* @returns
|
|
18
18
|
*/
|
|
19
|
-
export const decodeBool = (bytes) =>
|
|
19
|
+
export const decodeBool = (bytes) => {
|
|
20
20
|
const stream = Bytes.makeStream(bytes);
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
});
|
|
21
|
+
return stream.shiftOne().pipe(Either.flatMap((b) => {
|
|
22
|
+
if (b == TRUE_BYTE) {
|
|
23
|
+
return Either.right(true);
|
|
24
|
+
}
|
|
25
|
+
else if (b == FALSE_BYTE) {
|
|
26
|
+
return Either.right(false);
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
return Either.left(new DecodeError(stream, "unexpected non-boolean cbor object"));
|
|
30
|
+
}
|
|
31
|
+
}));
|
|
32
|
+
};
|
|
32
33
|
/**
|
|
33
34
|
* Encodes a `boolean` into its CBOR representation.
|
|
34
35
|
* @param b
|
|
@@ -46,41 +47,54 @@ export function encodeBool(b) {
|
|
|
46
47
|
* @param bytes
|
|
47
48
|
* @returns
|
|
48
49
|
*/
|
|
49
|
-
export const isBool = (bytes) =>
|
|
50
|
-
.peekOne()
|
|
51
|
-
|
|
50
|
+
export const isBool = (bytes) => {
|
|
51
|
+
const head = Bytes.makeStream(bytes).peekOne();
|
|
52
|
+
if (Either.isLeft(head)) {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
return head.right == FALSE_BYTE || head.right == TRUE_BYTE;
|
|
56
|
+
};
|
|
57
|
+
const decodeIndefBytes = (stream) => Either.gen(function* () {
|
|
58
|
+
yield* stream.shiftOne();
|
|
59
|
+
// multiple chunks
|
|
60
|
+
let res = [];
|
|
61
|
+
let next = yield* stream.peekOne();
|
|
62
|
+
while (next != 255) {
|
|
63
|
+
const [, n] = yield* decodeDefHead(stream);
|
|
64
|
+
if (n > 64n) {
|
|
65
|
+
return yield* Either.left(new DecodeError(stream, "Bytearray chunk too large"));
|
|
66
|
+
}
|
|
67
|
+
const chunk = yield* stream.shiftMany(Number(n));
|
|
68
|
+
res = res.concat(chunk);
|
|
69
|
+
next = yield* stream.peekOne();
|
|
70
|
+
}
|
|
71
|
+
const last = yield* stream.shiftOne();
|
|
72
|
+
if (last != 255) {
|
|
73
|
+
return yield* Either.left(new DecodeError(stream, "invalid indef bytes termination byte"));
|
|
74
|
+
}
|
|
75
|
+
return res;
|
|
76
|
+
});
|
|
77
|
+
const decodeDefBytes = (stream) => decodeDefHead(stream).pipe(Either.flatMap(([m, n]) => {
|
|
78
|
+
if (m != 2) {
|
|
79
|
+
return Either.left(new DecodeError(stream, "Invalid def bytes"));
|
|
80
|
+
}
|
|
81
|
+
return stream.shiftMany(Number(n));
|
|
82
|
+
}));
|
|
52
83
|
/**
|
|
53
84
|
* Unwraps a CBOR encoded list of bytes
|
|
54
85
|
* @param bytes
|
|
55
86
|
* cborbytes, mutated to form remaining
|
|
56
87
|
* @returns byteArray
|
|
57
88
|
*/
|
|
58
|
-
export const decodeBytes = (bytes) =>
|
|
89
|
+
export const decodeBytes = (bytes) => {
|
|
59
90
|
const stream = Bytes.makeStream(bytes);
|
|
60
|
-
if (
|
|
61
|
-
|
|
62
|
-
// multiple chunks
|
|
63
|
-
let res = [];
|
|
64
|
-
while ((yield* stream.peekOne()) != 255) {
|
|
65
|
-
const [, n] = yield* decodeDefHead(stream);
|
|
66
|
-
if (n > 64n) {
|
|
67
|
-
return yield* new DecodeError(stream, "Bytearray chunk too large");
|
|
68
|
-
}
|
|
69
|
-
res = res.concat(yield* stream.shiftMany(Number(n)));
|
|
70
|
-
}
|
|
71
|
-
if ((yield* stream.shiftOne()) != 255) {
|
|
72
|
-
throw new Error("invalid indef bytes termination byte");
|
|
73
|
-
}
|
|
74
|
-
return res;
|
|
91
|
+
if (isIndefBytes(bytes)) {
|
|
92
|
+
return decodeIndefBytes(stream);
|
|
75
93
|
}
|
|
76
94
|
else {
|
|
77
|
-
|
|
78
|
-
if (m != 2) {
|
|
79
|
-
return yield* new DecodeError(stream, "Invalid def bytes");
|
|
80
|
-
}
|
|
81
|
-
return yield* stream.shiftMany(Number(n));
|
|
95
|
+
return decodeDefBytes(stream);
|
|
82
96
|
}
|
|
83
|
-
}
|
|
97
|
+
};
|
|
84
98
|
/**
|
|
85
99
|
* Wraps a list of bytes using CBOR. Optionally splits the bytes into chunks.
|
|
86
100
|
* @example
|
|
@@ -90,8 +104,8 @@ export const decodeBytes = (bytes) => Effect.gen(function* () {
|
|
|
90
104
|
* @returns
|
|
91
105
|
* cbor bytes
|
|
92
106
|
*/
|
|
93
|
-
export function encodeBytes(
|
|
94
|
-
bytes = Bytes.toArray(
|
|
107
|
+
export function encodeBytes(bs, splitIntoChunks = false) {
|
|
108
|
+
const bytes = Bytes.toArray(bs).slice();
|
|
95
109
|
if (bytes.length <= 64 || !splitIntoChunks) {
|
|
96
110
|
const head = encodeDefHead(2, BigInt(bytes.length));
|
|
97
111
|
return head.concat(bytes);
|
|
@@ -110,23 +124,40 @@ export function encodeBytes(bytes, splitIntoChunks = false) {
|
|
|
110
124
|
* @param bytes
|
|
111
125
|
* @returns
|
|
112
126
|
*/
|
|
113
|
-
export const isBytes = (bytes) =>
|
|
127
|
+
export const isBytes = (bytes) => {
|
|
128
|
+
const m = peekMajorType(bytes);
|
|
129
|
+
if (Either.isLeft(m)) {
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
return m.right == 2;
|
|
133
|
+
};
|
|
114
134
|
/**
|
|
115
135
|
* @param bytes
|
|
116
136
|
* @returns
|
|
117
137
|
*/
|
|
118
|
-
export const isDefBytes = (bytes) =>
|
|
138
|
+
export const isDefBytes = (bytes) => {
|
|
119
139
|
const stream = Bytes.makeStream(bytes);
|
|
120
|
-
const m =
|
|
121
|
-
|
|
122
|
-
|
|
140
|
+
const m = peekMajorType(stream);
|
|
141
|
+
if (Either.isLeft(m)) {
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
const n = stream.peekOne();
|
|
145
|
+
if (Either.isLeft(n)) {
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
return m.right == 2 && n.right != 2 * 32 + 31;
|
|
149
|
+
};
|
|
123
150
|
/**
|
|
124
151
|
* @param bytes
|
|
125
152
|
* @returns
|
|
126
153
|
*/
|
|
127
|
-
export const isIndefBytes = (bytes) =>
|
|
128
|
-
.peekOne()
|
|
129
|
-
|
|
154
|
+
export const isIndefBytes = (bytes) => {
|
|
155
|
+
const head = Bytes.makeStream(bytes).peekOne();
|
|
156
|
+
if (Either.isLeft(head)) {
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
159
|
+
return head.right == 2 * 32 + 31;
|
|
160
|
+
};
|
|
130
161
|
/**
|
|
131
162
|
* The homogenous field type case is used by the uplc ConstrData (undetermined number of UplcData items)
|
|
132
163
|
* @template Decoders
|
|
@@ -135,26 +166,24 @@ export const isIndefBytes = (bytes) => Bytes.makeStream(bytes)
|
|
|
135
166
|
* Array for heterogenous item types, single function for homogenous item types
|
|
136
167
|
* @returns
|
|
137
168
|
*/
|
|
138
|
-
export const decodeConstr = (fieldDecoder) => (bytes) =>
|
|
169
|
+
export const decodeConstr = (fieldDecoder) => (bytes) => Either.gen(function* () {
|
|
139
170
|
const stream = Bytes.makeStream(bytes);
|
|
140
171
|
const tag = yield* decodeConstrTag(stream);
|
|
141
|
-
const res = yield* decodeList((itemStream, i) =>
|
|
172
|
+
const res = yield* decodeList((itemStream, i) => {
|
|
142
173
|
if (Array.isArray(fieldDecoder)) {
|
|
143
174
|
const decoder = fieldDecoder[i];
|
|
144
175
|
if (decoder === undefined) {
|
|
145
|
-
return
|
|
176
|
+
return Either.left(new DecodeError(stream, `expected ${fieldDecoder.length} fields, got more than ${i}`));
|
|
146
177
|
}
|
|
147
|
-
|
|
148
|
-
return yield* decoder(itemStream);
|
|
178
|
+
return decoder(itemStream);
|
|
149
179
|
}
|
|
150
180
|
else {
|
|
151
|
-
|
|
152
|
-
return yield* fieldDecoder(itemStream);
|
|
181
|
+
return fieldDecoder(itemStream);
|
|
153
182
|
}
|
|
154
|
-
})
|
|
183
|
+
})(stream);
|
|
155
184
|
if (Array.isArray(fieldDecoder)) {
|
|
156
185
|
if (res.length < fieldDecoder.length) {
|
|
157
|
-
return yield* new DecodeError(stream, `expected ${fieldDecoder.length} fields, only got ${res.length}`);
|
|
186
|
+
return yield* Either.left(new DecodeError(stream, `expected ${fieldDecoder.length} fields, only got ${res.length}`));
|
|
158
187
|
}
|
|
159
188
|
}
|
|
160
189
|
return [tag, res];
|
|
@@ -163,49 +192,55 @@ export const decodeConstr = (fieldDecoder) => (bytes) => Effect.gen(function* ()
|
|
|
163
192
|
* @param bytes
|
|
164
193
|
* @returns
|
|
165
194
|
*/
|
|
166
|
-
export const decodeConstrLazy = (bytes) =>
|
|
195
|
+
export const decodeConstrLazy = (bytes) => {
|
|
167
196
|
const stream = Bytes.makeStream(bytes);
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
return [tag, decodeField];
|
|
171
|
-
});
|
|
197
|
+
return Either.zipWith(decodeConstrTag(stream), decodeListLazy(bytes), (tag, decodeField) => [tag, decodeField]);
|
|
198
|
+
};
|
|
172
199
|
/**
|
|
173
200
|
* @param bytes
|
|
174
201
|
* @returns
|
|
175
202
|
*/
|
|
176
|
-
const decodeConstrTag = (bytes) =>
|
|
203
|
+
const decodeConstrTag = (bytes) => {
|
|
177
204
|
const stream = Bytes.makeStream(bytes);
|
|
178
205
|
// constr
|
|
179
|
-
const
|
|
206
|
+
const head = decodeDefHead(stream);
|
|
207
|
+
if (Either.isLeft(head)) {
|
|
208
|
+
return Either.left(head.left);
|
|
209
|
+
}
|
|
210
|
+
const [m, n] = head.right;
|
|
180
211
|
if (m != 6) {
|
|
181
|
-
return
|
|
212
|
+
return Either.left(new DecodeError(stream, "Unexpected constr tag head"));
|
|
182
213
|
}
|
|
183
214
|
if (n < 102n) {
|
|
184
|
-
return
|
|
215
|
+
return Either.left(new DecodeError(stream, `unexpected encoded constr tag ${n}`));
|
|
185
216
|
}
|
|
186
217
|
else if (n == 102n) {
|
|
187
|
-
const
|
|
218
|
+
const check = decodeDefHead(stream);
|
|
219
|
+
if (Either.isLeft(check)) {
|
|
220
|
+
return Either.left(check.left);
|
|
221
|
+
}
|
|
222
|
+
const [mCheck, nCheck] = check.right;
|
|
188
223
|
if (mCheck != 4 || nCheck != 2n) {
|
|
189
|
-
return
|
|
224
|
+
return Either.left(new DecodeError(stream, "Unexpected constr tag nested head"));
|
|
190
225
|
}
|
|
191
|
-
return
|
|
226
|
+
return decodeInt(stream).pipe(Either.map(Number));
|
|
192
227
|
}
|
|
193
228
|
else if (n < 121n) {
|
|
194
|
-
return
|
|
229
|
+
return Either.left(new DecodeError(stream, `unexpected encoded constr tag ${n}`));
|
|
195
230
|
}
|
|
196
231
|
else if (n <= 127n) {
|
|
197
|
-
return Number(n - 121n);
|
|
232
|
+
return Either.right(Number(n - 121n));
|
|
198
233
|
}
|
|
199
234
|
else if (n < 1280n) {
|
|
200
|
-
return
|
|
235
|
+
return Either.left(new DecodeError(stream, `unexpected encoded constr tag ${n}`));
|
|
201
236
|
}
|
|
202
237
|
else if (n <= 1400n) {
|
|
203
|
-
return Number(n - 1280n + 7n);
|
|
238
|
+
return Either.right(Number(n - 1280n + 7n));
|
|
204
239
|
}
|
|
205
240
|
else {
|
|
206
|
-
return
|
|
241
|
+
return Either.left(new DecodeError(stream, `unexpected encoded constr tag ${n}`));
|
|
207
242
|
}
|
|
208
|
-
}
|
|
243
|
+
};
|
|
209
244
|
/**
|
|
210
245
|
* Note: internally the indef list format is used if the number of fields is > 0, if the number of fields is 0 the def list format is used
|
|
211
246
|
* see [well-typed/cborg/serialise/src/Codec/Serialise/Class.hs](https://github.com/well-typed/cborg/blob/4bdc818a1f0b35f38bc118a87944630043b58384/serialise/src/Codec/Serialise/Class.hs#L181).
|
|
@@ -243,73 +278,93 @@ function encodeConstrTag(tag) {
|
|
|
243
278
|
* @param bytes
|
|
244
279
|
* @returns
|
|
245
280
|
*/
|
|
246
|
-
export const isConstr = (bytes) =>
|
|
247
|
-
|
|
248
|
-
|
|
281
|
+
export const isConstr = (bytes) => {
|
|
282
|
+
const head = decodeDefHead(Bytes.makeStream(bytes).copy());
|
|
283
|
+
if (Either.isLeft(head)) {
|
|
284
|
+
return false;
|
|
249
285
|
}
|
|
250
|
-
|
|
286
|
+
const [m, n] = head.right;
|
|
287
|
+
if (m != 6) {
|
|
251
288
|
return false;
|
|
252
289
|
}
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
}));
|
|
290
|
+
return n == 102n || (n >= 121n && n <= 127n) || (n >= 1280n && n <= 1400n);
|
|
291
|
+
};
|
|
256
292
|
const FLOAT16_HEAD = 249;
|
|
257
293
|
const FLOAT32_HEAD = 250;
|
|
258
294
|
const FLOAT64_HEAD = 251;
|
|
295
|
+
const decodeFloat16Body = (stream) => stream.shiftMany(2).pipe(Either.flatMap(Float.decodeFloat16), Either.mapLeft((e) => {
|
|
296
|
+
return e._tag == "DecodeException"
|
|
297
|
+
? new DecodeError(stream, `failed to decode float16 (${e.message})`)
|
|
298
|
+
: e;
|
|
299
|
+
}));
|
|
300
|
+
const decodeFloat32Body = (stream) => stream.shiftMany(4).pipe(Either.flatMap(Float.decodeFloat32), Either.mapLeft((e) => {
|
|
301
|
+
return e._tag == "DecodeException"
|
|
302
|
+
? new DecodeError(stream, `failed to decode float32 (${e.message})`)
|
|
303
|
+
: e;
|
|
304
|
+
}));
|
|
305
|
+
const decodeFloat64Body = (stream) => stream.shiftMany(8).pipe(Either.flatMap(Float.decodeFloat64), Either.mapLeft((e) => {
|
|
306
|
+
return e._tag == "DecodeException"
|
|
307
|
+
? new DecodeError(stream, `failed to decode float64 (${e.message})`)
|
|
308
|
+
: e;
|
|
309
|
+
}));
|
|
259
310
|
/**
|
|
260
311
|
* @param bytes
|
|
261
312
|
* @returns
|
|
262
313
|
*/
|
|
263
|
-
export const decodeFloat = (bytes) =>
|
|
314
|
+
export const decodeFloat = (bytes) => {
|
|
264
315
|
const stream = Bytes.makeStream(bytes);
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
});
|
|
316
|
+
return stream.shiftOne().pipe(Either.flatMap((head) => {
|
|
317
|
+
switch (head) {
|
|
318
|
+
case FLOAT16_HEAD:
|
|
319
|
+
return decodeFloat16Body(stream);
|
|
320
|
+
case FLOAT32_HEAD:
|
|
321
|
+
return decodeFloat32Body(stream);
|
|
322
|
+
case FLOAT64_HEAD:
|
|
323
|
+
return decodeFloat64Body(stream);
|
|
324
|
+
default:
|
|
325
|
+
return Either.left(new DecodeError(stream, "invalid float header"));
|
|
326
|
+
}
|
|
327
|
+
}));
|
|
328
|
+
};
|
|
277
329
|
/**
|
|
278
330
|
* @param bytes
|
|
279
331
|
* @returns
|
|
280
332
|
*/
|
|
281
|
-
export const decodeFloat16 = (bytes) =>
|
|
333
|
+
export const decodeFloat16 = (bytes) => {
|
|
282
334
|
const stream = Bytes.makeStream(bytes);
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
});
|
|
335
|
+
return stream.shiftOne().pipe(Either.flatMap((head) => {
|
|
336
|
+
if (head != FLOAT16_HEAD) {
|
|
337
|
+
return Either.left(new DecodeError(stream, "invalid Float16 header"));
|
|
338
|
+
}
|
|
339
|
+
return decodeFloat16Body(stream);
|
|
340
|
+
}));
|
|
341
|
+
};
|
|
289
342
|
/**
|
|
290
343
|
* @param bytes
|
|
291
344
|
* @returns
|
|
292
345
|
*/
|
|
293
|
-
export const decodeFloat32 = (bytes) =>
|
|
346
|
+
export const decodeFloat32 = (bytes) => {
|
|
294
347
|
const stream = Bytes.makeStream(bytes);
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
});
|
|
348
|
+
return stream.shiftOne().pipe(Either.flatMap((head) => {
|
|
349
|
+
if (head != FLOAT32_HEAD) {
|
|
350
|
+
return Either.left(new DecodeError(stream, "invalid Float32 header"));
|
|
351
|
+
}
|
|
352
|
+
return decodeFloat32Body(stream);
|
|
353
|
+
}));
|
|
354
|
+
};
|
|
301
355
|
/**
|
|
302
356
|
* @param bytes
|
|
303
357
|
* @returns
|
|
304
358
|
*/
|
|
305
|
-
export const decodeFloat64 = (bytes) =>
|
|
359
|
+
export const decodeFloat64 = (bytes) => {
|
|
306
360
|
const stream = Bytes.makeStream(bytes);
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
});
|
|
361
|
+
return stream.shiftOne().pipe(Either.flatMap((head) => {
|
|
362
|
+
if (head != FLOAT64_HEAD) {
|
|
363
|
+
return Either.left(new DecodeError(stream, "invalid Float64 header"));
|
|
364
|
+
}
|
|
365
|
+
return decodeFloat64Body(stream);
|
|
366
|
+
}));
|
|
367
|
+
};
|
|
313
368
|
/**
|
|
314
369
|
* @param f
|
|
315
370
|
* @returns
|
|
@@ -335,30 +390,48 @@ export function encodeFloat64(f) {
|
|
|
335
390
|
* @param bytes
|
|
336
391
|
* @returns
|
|
337
392
|
*/
|
|
338
|
-
export const isFloat = (bytes) =>
|
|
339
|
-
.peekOne()
|
|
340
|
-
|
|
393
|
+
export const isFloat = (bytes) => {
|
|
394
|
+
const head = Bytes.makeStream(bytes).peekOne();
|
|
395
|
+
if (Either.isLeft(head)) {
|
|
396
|
+
return false;
|
|
397
|
+
}
|
|
398
|
+
return (head.right == FLOAT16_HEAD ||
|
|
399
|
+
head.right == FLOAT32_HEAD ||
|
|
400
|
+
head.right == FLOAT64_HEAD);
|
|
401
|
+
};
|
|
341
402
|
/**
|
|
342
403
|
* @param bytes
|
|
343
404
|
* @returns
|
|
344
405
|
*/
|
|
345
|
-
export const isFloat16 = (bytes) =>
|
|
346
|
-
.peekOne()
|
|
347
|
-
|
|
406
|
+
export const isFloat16 = (bytes) => {
|
|
407
|
+
const head = Bytes.makeStream(bytes).peekOne();
|
|
408
|
+
if (Either.isLeft(head)) {
|
|
409
|
+
return false;
|
|
410
|
+
}
|
|
411
|
+
return head.right == FLOAT16_HEAD;
|
|
412
|
+
};
|
|
348
413
|
/**
|
|
349
414
|
* @param bytes
|
|
350
415
|
* @returns
|
|
351
416
|
*/
|
|
352
|
-
export const isFloat32 = (bytes) =>
|
|
353
|
-
.peekOne()
|
|
354
|
-
|
|
417
|
+
export const isFloat32 = (bytes) => {
|
|
418
|
+
const head = Bytes.makeStream(bytes).peekOne();
|
|
419
|
+
if (Either.isLeft(head)) {
|
|
420
|
+
return false;
|
|
421
|
+
}
|
|
422
|
+
return head.right == FLOAT32_HEAD;
|
|
423
|
+
};
|
|
355
424
|
/**
|
|
356
425
|
* @param bytes
|
|
357
426
|
* @returns
|
|
358
427
|
*/
|
|
359
|
-
export const isFloat64 = (bytes) =>
|
|
360
|
-
.peekOne()
|
|
361
|
-
|
|
428
|
+
export const isFloat64 = (bytes) => {
|
|
429
|
+
const head = Bytes.makeStream(bytes).peekOne();
|
|
430
|
+
if (Either.isLeft(head)) {
|
|
431
|
+
return false;
|
|
432
|
+
}
|
|
433
|
+
return head.right == FLOAT64_HEAD;
|
|
434
|
+
};
|
|
362
435
|
/**
|
|
363
436
|
* @param b0
|
|
364
437
|
* @returns
|
|
@@ -373,47 +446,64 @@ function decodeFirstHeadByte(b0) {
|
|
|
373
446
|
* @returns
|
|
374
447
|
* [majorType, n]
|
|
375
448
|
*/
|
|
376
|
-
export const decodeDefHead = (bytes) =>
|
|
449
|
+
export const decodeDefHead = (bytes) => {
|
|
377
450
|
const stream = Bytes.makeStream(bytes);
|
|
378
451
|
if (stream.isAtEnd()) {
|
|
379
|
-
return
|
|
452
|
+
return Either.left(new DecodeError(stream, "Empty CBOR head"));
|
|
453
|
+
}
|
|
454
|
+
const first = stream.shiftOne();
|
|
455
|
+
if (Either.isLeft(first)) {
|
|
456
|
+
return Either.left(first.left);
|
|
380
457
|
}
|
|
381
|
-
const
|
|
382
|
-
const [m, n0] = decodeFirstHeadByte(first);
|
|
458
|
+
const [m, n0] = decodeFirstHeadByte(first.right);
|
|
383
459
|
if (n0 <= 23) {
|
|
384
|
-
return [m, BigInt(n0)];
|
|
460
|
+
return Either.right([m, BigInt(n0)]);
|
|
385
461
|
}
|
|
386
462
|
else if (n0 == 24) {
|
|
387
|
-
const l =
|
|
388
|
-
|
|
463
|
+
const l = decodeIntInternal(stream, 1);
|
|
464
|
+
if (Either.isLeft(l)) {
|
|
465
|
+
return Either.left(l.left);
|
|
466
|
+
}
|
|
467
|
+
return Either.right([m, l.right]);
|
|
389
468
|
}
|
|
390
469
|
else if (n0 == 25) {
|
|
391
470
|
if (m == 7) {
|
|
392
|
-
return
|
|
471
|
+
return Either.left(new DecodeError(stream, "Unexpected float16 (hint: decode float16 by calling decodeFloat16 directly)"));
|
|
472
|
+
}
|
|
473
|
+
const n = decodeIntInternal(stream, 2);
|
|
474
|
+
if (Either.isLeft(n)) {
|
|
475
|
+
return Either.left(n.left);
|
|
393
476
|
}
|
|
394
|
-
|
|
395
|
-
return [m, n];
|
|
477
|
+
return Either.right([m, n.right]);
|
|
396
478
|
}
|
|
397
479
|
else if (n0 == 26) {
|
|
398
480
|
if (m == 7) {
|
|
399
|
-
return
|
|
481
|
+
return Either.left(new DecodeError(stream, "Unexpected float32 (hint: decode float32 by calling decodeFloat32 directly)"));
|
|
482
|
+
}
|
|
483
|
+
const n = decodeIntInternal(stream, 4);
|
|
484
|
+
if (Either.isLeft(n)) {
|
|
485
|
+
return Either.left(n.left);
|
|
400
486
|
}
|
|
401
|
-
return [m,
|
|
487
|
+
return Either.right([m, n.right]);
|
|
402
488
|
}
|
|
403
489
|
else if (n0 == 27) {
|
|
404
490
|
if (m == 7) {
|
|
405
|
-
return
|
|
491
|
+
return Either.left(new DecodeError(stream, "Unexpected float64 (hint: decode float64 by calling decodeFloat64 directly)"));
|
|
406
492
|
}
|
|
407
|
-
|
|
493
|
+
const n = decodeIntInternal(stream, 8);
|
|
494
|
+
if (Either.isLeft(n)) {
|
|
495
|
+
return Either.left(n.left);
|
|
496
|
+
}
|
|
497
|
+
return Either.right([m, n.right]);
|
|
408
498
|
}
|
|
409
499
|
else if ((m == 2 || m == 3 || m == 4 || m == 5 || m == 7) && n0 == 31) {
|
|
410
500
|
// head value 31 is used an indefinite length marker for 2,3,4,5,7 (never for 0,1,6)
|
|
411
|
-
return
|
|
501
|
+
return Either.left(new DecodeError(stream, `Unexpected header m=${m} n0=${n0} (expected def instead of indef)`));
|
|
412
502
|
}
|
|
413
503
|
else {
|
|
414
|
-
return
|
|
504
|
+
return Either.left(new DecodeError(stream, "Bad CBOR header"));
|
|
415
505
|
}
|
|
416
|
-
}
|
|
506
|
+
};
|
|
417
507
|
/**
|
|
418
508
|
* @param m major type
|
|
419
509
|
* @param n size parameter
|
|
@@ -467,51 +557,48 @@ export function encodeIndefHead(m) {
|
|
|
467
557
|
*/
|
|
468
558
|
export const peekMajorType = (bytes) => Bytes.makeStream(bytes)
|
|
469
559
|
.peekOne()
|
|
470
|
-
.pipe(
|
|
560
|
+
.pipe(Either.map((head) => Math.trunc(head / 32)));
|
|
471
561
|
/**
|
|
472
562
|
* @param bytes
|
|
473
563
|
* @returns
|
|
474
564
|
*/
|
|
475
|
-
export const peekMajorAndSimpleMinorType = (bytes) => Bytes.makeStream(bytes).peekOne().pipe(
|
|
565
|
+
export const peekMajorAndSimpleMinorType = (bytes) => Bytes.makeStream(bytes).peekOne().pipe(Either.map(decodeFirstHeadByte));
|
|
476
566
|
/**
|
|
477
567
|
* Decodes a CBOR encoded bigint integer.
|
|
478
568
|
* @param bytes
|
|
479
569
|
* @returns
|
|
480
570
|
*/
|
|
481
|
-
export const decodeInt = (bytes) =>
|
|
571
|
+
export const decodeInt = (bytes) => {
|
|
482
572
|
const stream = Bytes.makeStream(bytes);
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
}
|
|
487
|
-
else if (m == 1) {
|
|
488
|
-
return -n - 1n;
|
|
489
|
-
}
|
|
490
|
-
else if (m == 6) {
|
|
491
|
-
if (n == 2n) {
|
|
492
|
-
return yield* decodeIntInternal(stream);
|
|
493
|
-
}
|
|
494
|
-
else if (n == 3n) {
|
|
495
|
-
return -(yield* decodeIntInternal(stream)) - 1n;
|
|
573
|
+
return decodeDefHead(stream).pipe(Either.flatMap(([m, n]) => {
|
|
574
|
+
if (m == 0) {
|
|
575
|
+
return Either.right(n);
|
|
496
576
|
}
|
|
497
|
-
else {
|
|
498
|
-
return
|
|
577
|
+
else if (m == 1) {
|
|
578
|
+
return Either.right(-n - 1n);
|
|
499
579
|
}
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
580
|
+
else if (m == 6) {
|
|
581
|
+
if (n == 2n) {
|
|
582
|
+
return decodeIntInternal(stream);
|
|
583
|
+
}
|
|
584
|
+
else if (n == 3n) {
|
|
585
|
+
return decodeIntInternal(stream).pipe(Either.map((x) => -x - 1n));
|
|
586
|
+
}
|
|
587
|
+
else {
|
|
588
|
+
return Either.left(new DecodeError(stream, `Unexpected tag m:${m}`));
|
|
589
|
+
}
|
|
509
590
|
}
|
|
510
591
|
else {
|
|
511
|
-
return
|
|
592
|
+
return Either.left(new DecodeError(stream, `Unexpected tag m:${m}`));
|
|
512
593
|
}
|
|
513
594
|
}));
|
|
514
595
|
};
|
|
596
|
+
export const decodeIntAsNumber = (bytes) => decodeInt(bytes).pipe(Either.map(Number));
|
|
597
|
+
const decodeIntInternal = (stream, nBytes = undefined) => {
|
|
598
|
+
return (nBytes === undefined ? decodeBytes(stream) : stream.shiftMany(nBytes)).pipe(Either.flatMap(BigEndian.decode), Either.mapLeft((e) => e._tag == "DecodeException"
|
|
599
|
+
? new DecodeError(stream, `failed to decode BigEndian int (${e.message})`)
|
|
600
|
+
: e));
|
|
601
|
+
};
|
|
515
602
|
/**
|
|
516
603
|
* Encodes a bigint integer using CBOR.
|
|
517
604
|
* @param n
|
|
@@ -538,7 +625,12 @@ export function encodeInt(n) {
|
|
|
538
625
|
* @param bytes
|
|
539
626
|
* @returns
|
|
540
627
|
*/
|
|
541
|
-
export const isInt = (bytes) =>
|
|
628
|
+
export const isInt = (bytes) => {
|
|
629
|
+
const mn0 = peekMajorAndSimpleMinorType(bytes);
|
|
630
|
+
if (Either.isLeft(mn0)) {
|
|
631
|
+
return false;
|
|
632
|
+
}
|
|
633
|
+
const [m, n0] = mn0.right;
|
|
542
634
|
if (m == 0 || m == 1) {
|
|
543
635
|
return true;
|
|
544
636
|
}
|
|
@@ -548,7 +640,32 @@ export const isInt = (bytes) => peekMajorAndSimpleMinorType(bytes).pipe(Effect.m
|
|
|
548
640
|
else {
|
|
549
641
|
return false;
|
|
550
642
|
}
|
|
551
|
-
}
|
|
643
|
+
};
|
|
644
|
+
const decodeIndefList = (stream, itemDecoder) => Either.gen(function* () {
|
|
645
|
+
const res = [];
|
|
646
|
+
yield* stream.shiftOne();
|
|
647
|
+
let i = 0;
|
|
648
|
+
while ((yield* stream.peekOne()) != 255) {
|
|
649
|
+
res.push(yield* itemDecoder(stream, i));
|
|
650
|
+
i++;
|
|
651
|
+
}
|
|
652
|
+
const last = yield* stream.shiftOne();
|
|
653
|
+
if (last != 255) {
|
|
654
|
+
return yield* Either.left(new DecodeError(stream, "Invalid def list head byte"));
|
|
655
|
+
}
|
|
656
|
+
return res;
|
|
657
|
+
});
|
|
658
|
+
const decodeDefList = (stream, itemDecoder) => Either.gen(function* () {
|
|
659
|
+
const res = [];
|
|
660
|
+
const [m, n] = yield* decodeDefHead(stream);
|
|
661
|
+
if (m != 4) {
|
|
662
|
+
return yield* Either.left(new DecodeError(stream, "invalid def list head byte"));
|
|
663
|
+
}
|
|
664
|
+
for (let i = 0; i < Number(n); i++) {
|
|
665
|
+
res.push(yield* itemDecoder(stream, i));
|
|
666
|
+
}
|
|
667
|
+
return res;
|
|
668
|
+
});
|
|
552
669
|
/**
|
|
553
670
|
* Decodes a CBOR encoded list.
|
|
554
671
|
* A decoder function is called with the bytes of every contained item (nothing is returning directly).
|
|
@@ -556,72 +673,56 @@ export const isInt = (bytes) => peekMajorAndSimpleMinorType(bytes).pipe(Effect.m
|
|
|
556
673
|
* @param itemDecoder
|
|
557
674
|
* @returns
|
|
558
675
|
*/
|
|
559
|
-
export const decodeList = (itemDecoder) => (bytes) =>
|
|
676
|
+
export const decodeList = (itemDecoder) => (bytes) => {
|
|
560
677
|
const stream = Bytes.makeStream(bytes);
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
yield* stream.shiftOne();
|
|
564
|
-
let i = 0;
|
|
565
|
-
while ((yield* stream.peekOne()) != 255) {
|
|
566
|
-
res.push(yield* itemDecoder(stream, i));
|
|
567
|
-
i++;
|
|
568
|
-
}
|
|
569
|
-
const last = yield* stream.shiftOne();
|
|
570
|
-
if (last != 255) {
|
|
571
|
-
return yield* new DecodeError(stream, "Invalid def list head byte");
|
|
572
|
-
}
|
|
678
|
+
if (isIndefList(stream)) {
|
|
679
|
+
return decodeIndefList(stream, itemDecoder);
|
|
573
680
|
}
|
|
574
681
|
else {
|
|
575
|
-
|
|
576
|
-
if (m != 4) {
|
|
577
|
-
return yield* new DecodeError(stream, "invalid def list head byte");
|
|
578
|
-
}
|
|
579
|
-
for (let i = 0; i < Number(n); i++) {
|
|
580
|
-
res.push(yield* itemDecoder(stream, i));
|
|
581
|
-
}
|
|
682
|
+
return decodeDefList(stream, itemDecoder);
|
|
582
683
|
}
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
/**
|
|
586
|
-
* @param bytes
|
|
587
|
-
* @returns
|
|
588
|
-
*
|
|
589
|
-
*
|
|
590
|
-
*/
|
|
591
|
-
export const decodeListLazy = (bytes) => Effect.gen(function* () {
|
|
592
|
-
const stream = Bytes.makeStream(bytes);
|
|
684
|
+
};
|
|
685
|
+
const decodeIndefListLazy = (stream) => Either.gen(function* () {
|
|
593
686
|
let i = 0;
|
|
594
687
|
let done = false;
|
|
595
|
-
|
|
596
|
-
if (yield*
|
|
688
|
+
yield* stream.shiftOne();
|
|
689
|
+
if ((yield* stream.peekOne()) == 255) {
|
|
597
690
|
yield* stream.shiftOne();
|
|
598
|
-
|
|
599
|
-
if ((yield* stream.peekOne()) == 255) {
|
|
600
|
-
yield* stream.shiftOne();
|
|
601
|
-
done = true;
|
|
602
|
-
}
|
|
603
|
-
});
|
|
691
|
+
done = true;
|
|
604
692
|
}
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
return yield* new DecodeError(stream, "Unexpected header major type");
|
|
693
|
+
const decodeItem = (itemDecoder) => Either.gen(function* () {
|
|
694
|
+
if (done) {
|
|
695
|
+
return yield* Either.left(new DecodeError(stream, "end-of-list"));
|
|
609
696
|
}
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
}
|
|
697
|
+
const res = yield* itemDecoder(stream, i);
|
|
698
|
+
i++;
|
|
699
|
+
if ((yield* stream.peekOne()) == 255) {
|
|
700
|
+
yield* stream.shiftOne();
|
|
701
|
+
done = true;
|
|
702
|
+
}
|
|
703
|
+
return res;
|
|
704
|
+
});
|
|
705
|
+
return decodeItem;
|
|
706
|
+
});
|
|
707
|
+
const decodeDefListLazy = (stream) => Either.gen(function* () {
|
|
708
|
+
let i = 0;
|
|
709
|
+
let done = false;
|
|
710
|
+
const [m, n] = yield* decodeDefHead(stream);
|
|
711
|
+
if (m != 4) {
|
|
712
|
+
return yield* Either.left(new DecodeError(stream, "Unexpected header major type"));
|
|
713
|
+
}
|
|
714
|
+
if (i >= n) {
|
|
715
|
+
done = true;
|
|
616
716
|
}
|
|
617
|
-
|
|
618
|
-
const decodeItem = (itemDecoder) => Effect.gen(function* () {
|
|
717
|
+
const decodeItem = (itemDecoder) => Either.gen(function* () {
|
|
619
718
|
if (done) {
|
|
620
|
-
return yield* new DecodeError(stream, "end-of-list");
|
|
719
|
+
return yield* Either.left(new DecodeError(stream, "end-of-list"));
|
|
621
720
|
}
|
|
622
721
|
const res = yield* itemDecoder(stream, i);
|
|
623
722
|
i++;
|
|
624
|
-
|
|
723
|
+
if (i >= n) {
|
|
724
|
+
done = true;
|
|
725
|
+
}
|
|
625
726
|
return res;
|
|
626
727
|
});
|
|
627
728
|
return decodeItem;
|
|
@@ -630,44 +731,73 @@ export const decodeListLazy = (bytes) => Effect.gen(function* () {
|
|
|
630
731
|
* @param bytes
|
|
631
732
|
* @returns
|
|
632
733
|
*/
|
|
633
|
-
export const
|
|
734
|
+
export const decodeListLazy = (bytes) => {
|
|
634
735
|
const stream = Bytes.makeStream(bytes);
|
|
736
|
+
if (isIndefList(stream)) {
|
|
737
|
+
return decodeIndefListLazy(stream);
|
|
738
|
+
}
|
|
739
|
+
else {
|
|
740
|
+
return decodeDefListLazy(stream);
|
|
741
|
+
}
|
|
742
|
+
};
|
|
743
|
+
const decodeIndefListLazyOption = (stream) => Either.gen(function* () {
|
|
635
744
|
let i = 0;
|
|
636
745
|
let done = false;
|
|
637
|
-
|
|
638
|
-
if (yield*
|
|
746
|
+
yield* stream.shiftOne();
|
|
747
|
+
if ((yield* stream.peekOne()) == 255) {
|
|
639
748
|
yield* stream.shiftOne();
|
|
640
|
-
|
|
641
|
-
if ((yield* stream.peekOne()) == 255) {
|
|
642
|
-
yield* stream.shiftOne();
|
|
643
|
-
done = true;
|
|
644
|
-
}
|
|
645
|
-
});
|
|
749
|
+
done = true;
|
|
646
750
|
}
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
return yield* new DecodeError(stream, "Unexpected major type for list");
|
|
751
|
+
const decodeItem = (itemDecoder) => Either.gen(function* () {
|
|
752
|
+
if (done) {
|
|
753
|
+
return undefined;
|
|
651
754
|
}
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
}
|
|
755
|
+
const res = yield* itemDecoder(stream, i);
|
|
756
|
+
i++;
|
|
757
|
+
if ((yield* stream.peekOne()) == 255) {
|
|
758
|
+
yield* stream.shiftOne();
|
|
759
|
+
done = true;
|
|
760
|
+
}
|
|
761
|
+
return res;
|
|
762
|
+
});
|
|
763
|
+
return decodeItem;
|
|
764
|
+
});
|
|
765
|
+
const decodeDefListLazyOption = (stream) => Either.gen(function* () {
|
|
766
|
+
let i = 0;
|
|
767
|
+
let done = false;
|
|
768
|
+
const [m, n] = yield* decodeDefHead(stream);
|
|
769
|
+
if (m != 4) {
|
|
770
|
+
return yield* Either.left(new DecodeError(stream, "Unexpected major type for list"));
|
|
658
771
|
}
|
|
659
|
-
|
|
660
|
-
|
|
772
|
+
if (i >= n) {
|
|
773
|
+
done = true;
|
|
774
|
+
}
|
|
775
|
+
const decodeItem = (itemDecoder) => Either.gen(function* () {
|
|
661
776
|
if (done) {
|
|
662
777
|
return undefined;
|
|
663
778
|
}
|
|
664
779
|
const res = yield* itemDecoder(stream, i);
|
|
665
780
|
i++;
|
|
666
|
-
|
|
781
|
+
if (i >= n) {
|
|
782
|
+
done = true;
|
|
783
|
+
}
|
|
667
784
|
return res;
|
|
668
785
|
});
|
|
669
786
|
return decodeItem;
|
|
670
787
|
});
|
|
788
|
+
/**
|
|
789
|
+
* @param bytes
|
|
790
|
+
* @returns
|
|
791
|
+
*/
|
|
792
|
+
export const decodeListLazyOption = (bytes) => {
|
|
793
|
+
const stream = Bytes.makeStream(bytes);
|
|
794
|
+
if (isIndefList(stream)) {
|
|
795
|
+
return decodeIndefListLazyOption(stream);
|
|
796
|
+
}
|
|
797
|
+
else {
|
|
798
|
+
return decodeDefListLazyOption(stream);
|
|
799
|
+
}
|
|
800
|
+
};
|
|
671
801
|
/**
|
|
672
802
|
* This follows the serialization format that the Haskell input-output-hk/plutus UPLC evaluator (i.e. empty lists use `encodeDefList`, non-empty lists use `encodeIndefList`).
|
|
673
803
|
* See [well-typed/cborg/serialise/src/Codec/Serialise/Class.hs](https://github.com/well-typed/cborg/blob/4bdc818a1f0b35f38bc118a87944630043b58384/serialise/src/Codec/Serialise/Class.hs#L181).
|
|
@@ -728,23 +858,40 @@ export function encodeDefList(items) {
|
|
|
728
858
|
* @param bytes
|
|
729
859
|
* @returns
|
|
730
860
|
*/
|
|
731
|
-
export const isList = (bytes) =>
|
|
861
|
+
export const isList = (bytes) => {
|
|
862
|
+
const m = peekMajorType(bytes);
|
|
863
|
+
if (Either.isLeft(m)) {
|
|
864
|
+
return false;
|
|
865
|
+
}
|
|
866
|
+
return m.right == 4;
|
|
867
|
+
};
|
|
732
868
|
/**
|
|
733
869
|
* @param bytes
|
|
734
870
|
* @returns
|
|
735
871
|
*/
|
|
736
|
-
export const isDefList = (bytes) =>
|
|
872
|
+
export const isDefList = (bytes) => {
|
|
737
873
|
const stream = Bytes.makeStream(bytes);
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
874
|
+
const m = peekMajorType(stream);
|
|
875
|
+
if (Either.isLeft(m)) {
|
|
876
|
+
return false;
|
|
877
|
+
}
|
|
878
|
+
const n = stream.peekOne();
|
|
879
|
+
if (Either.isLeft(n)) {
|
|
880
|
+
return false;
|
|
881
|
+
}
|
|
882
|
+
return m.right == 4 && n.right != 4 * 32 + 31;
|
|
883
|
+
};
|
|
741
884
|
/**
|
|
742
885
|
* @param bytes
|
|
743
886
|
* @returns
|
|
744
887
|
*/
|
|
745
|
-
export const isIndefList = (bytes) =>
|
|
746
|
-
.peekOne()
|
|
747
|
-
|
|
888
|
+
export const isIndefList = (bytes) => {
|
|
889
|
+
const head = Bytes.makeStream(bytes).peekOne();
|
|
890
|
+
if (Either.isLeft(head)) {
|
|
891
|
+
return false;
|
|
892
|
+
}
|
|
893
|
+
return head.right == 4 * 32 + 31;
|
|
894
|
+
};
|
|
748
895
|
/**
|
|
749
896
|
* Decodes a CBOR encoded map.
|
|
750
897
|
* Calls a decoder function for each key-value pair (nothing is returned directly).
|
|
@@ -755,16 +902,16 @@ export const isIndefList = (bytes) => Bytes.makeStream(bytes)
|
|
|
755
902
|
* @param valueDecoder
|
|
756
903
|
* @returns
|
|
757
904
|
*/
|
|
758
|
-
export const decodeMap = (keyDecoder, valueDecoder) => (bytes) =>
|
|
905
|
+
export const decodeMap = (keyDecoder, valueDecoder) => (bytes) => Either.gen(function* () {
|
|
759
906
|
const stream = Bytes.makeStream(bytes);
|
|
760
|
-
if (
|
|
907
|
+
if (isIndefMap(stream)) {
|
|
761
908
|
yield* stream.shiftOne();
|
|
762
909
|
return yield* decodeIndefMap(stream, keyDecoder, valueDecoder);
|
|
763
910
|
}
|
|
764
911
|
else {
|
|
765
912
|
const [m, n] = yield* decodeDefHead(stream);
|
|
766
913
|
if (m != 5) {
|
|
767
|
-
return yield* new DecodeError(stream, "invalid def map");
|
|
914
|
+
return yield* Either.left(new DecodeError(stream, "invalid def map"));
|
|
768
915
|
}
|
|
769
916
|
return yield* decodeDefMap(stream, Number(n), keyDecoder, valueDecoder);
|
|
770
917
|
}
|
|
@@ -777,7 +924,7 @@ export const decodeMap = (keyDecoder, valueDecoder) => (bytes) => Effect.gen(fun
|
|
|
777
924
|
* @param valueDecoder
|
|
778
925
|
* @returns
|
|
779
926
|
*/
|
|
780
|
-
const decodeDefMap = (stream, n, keyDecoder, valueDecoder) =>
|
|
927
|
+
const decodeDefMap = (stream, n, keyDecoder, valueDecoder) => Either.gen(function* () {
|
|
781
928
|
const res = [];
|
|
782
929
|
for (let i = 0; i < n; i++) {
|
|
783
930
|
res.push([yield* keyDecoder(stream), yield* valueDecoder(stream)]);
|
|
@@ -793,7 +940,7 @@ const decodeDefMap = (stream, n, keyDecoder, valueDecoder) => Effect.gen(functio
|
|
|
793
940
|
* @param valueDecoder
|
|
794
941
|
* @returns
|
|
795
942
|
*/
|
|
796
|
-
const decodeIndefMap = (stream, keyDecoder, valueDecoder) =>
|
|
943
|
+
const decodeIndefMap = (stream, keyDecoder, valueDecoder) => Either.gen(function* () {
|
|
797
944
|
const res = [];
|
|
798
945
|
while ((yield* stream.peekOne()) != 255) {
|
|
799
946
|
res.push([yield* keyDecoder(stream), yield* valueDecoder(stream)]);
|
|
@@ -845,14 +992,24 @@ function encodeMapInternal(pairs) {
|
|
|
845
992
|
* @param bytes
|
|
846
993
|
* @returns
|
|
847
994
|
*/
|
|
848
|
-
export const isMap = (bytes) =>
|
|
995
|
+
export const isMap = (bytes) => {
|
|
996
|
+
const m = peekMajorType(bytes);
|
|
997
|
+
if (Either.isLeft(m)) {
|
|
998
|
+
return false;
|
|
999
|
+
}
|
|
1000
|
+
return m.right == 5;
|
|
1001
|
+
};
|
|
849
1002
|
/**
|
|
850
1003
|
* @param bytes
|
|
851
1004
|
* @returns
|
|
852
1005
|
*/
|
|
853
|
-
const isIndefMap = (bytes) =>
|
|
854
|
-
.peekOne()
|
|
855
|
-
|
|
1006
|
+
const isIndefMap = (bytes) => {
|
|
1007
|
+
const head = Bytes.makeStream(bytes).peekOne();
|
|
1008
|
+
if (Either.isLeft(head)) {
|
|
1009
|
+
return false;
|
|
1010
|
+
}
|
|
1011
|
+
return head.right == 5 * 32 + 31;
|
|
1012
|
+
};
|
|
856
1013
|
const NULL_BYTE = 246; // m = 7, n = 22
|
|
857
1014
|
/**
|
|
858
1015
|
* Checks if next element in `bytes` is a `null`.
|
|
@@ -860,14 +1017,15 @@ const NULL_BYTE = 246; // m = 7, n = 22
|
|
|
860
1017
|
* @param bytes
|
|
861
1018
|
* @returns
|
|
862
1019
|
*/
|
|
863
|
-
export const decodeNull = (bytes) =>
|
|
1020
|
+
export const decodeNull = (bytes) => {
|
|
864
1021
|
const stream = Bytes.makeStream(bytes);
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
});
|
|
1022
|
+
return stream.shiftOne().pipe(Either.flatMap((b) => {
|
|
1023
|
+
if (b != NULL_BYTE) {
|
|
1024
|
+
return Either.left(new DecodeError(stream, "not null"));
|
|
1025
|
+
}
|
|
1026
|
+
return Either.right(null);
|
|
1027
|
+
}));
|
|
1028
|
+
};
|
|
871
1029
|
/**
|
|
872
1030
|
* Encode `null` into its CBOR representation.
|
|
873
1031
|
* @param _null ignored
|
|
@@ -880,9 +1038,48 @@ export function encodeNull(_null = null) {
|
|
|
880
1038
|
* @param bytes
|
|
881
1039
|
* @returns
|
|
882
1040
|
*/
|
|
883
|
-
export const isNull = (bytes) =>
|
|
884
|
-
.peekOne()
|
|
885
|
-
|
|
1041
|
+
export const isNull = (bytes) => {
|
|
1042
|
+
const head = Bytes.makeStream(bytes).peekOne();
|
|
1043
|
+
if (Either.isLeft(head)) {
|
|
1044
|
+
return false;
|
|
1045
|
+
}
|
|
1046
|
+
return head.right == NULL_BYTE;
|
|
1047
|
+
};
|
|
1048
|
+
/**
|
|
1049
|
+
* @template T
|
|
1050
|
+
* @param decodeSome
|
|
1051
|
+
* @returns
|
|
1052
|
+
*/
|
|
1053
|
+
export const decodeNullOption = (decodeSome) => (bytes) => {
|
|
1054
|
+
const stream = Bytes.makeStream(bytes);
|
|
1055
|
+
if (isNull(stream)) {
|
|
1056
|
+
return decodeNull(stream).pipe(Either.map(() => undefined));
|
|
1057
|
+
}
|
|
1058
|
+
else {
|
|
1059
|
+
return decodeSome(stream);
|
|
1060
|
+
}
|
|
1061
|
+
};
|
|
1062
|
+
/**
|
|
1063
|
+
* @param option
|
|
1064
|
+
* @returns
|
|
1065
|
+
*/
|
|
1066
|
+
export function encodeNullOption(option) {
|
|
1067
|
+
return option ?? encodeNull();
|
|
1068
|
+
}
|
|
1069
|
+
/**
|
|
1070
|
+
* @template T
|
|
1071
|
+
* @param bytes
|
|
1072
|
+
* @param decodeSome
|
|
1073
|
+
* @returns
|
|
1074
|
+
*/
|
|
1075
|
+
export const decodeListOption = (decodeSome) => (bytes) => decodeListLazyOption(bytes).pipe(Either.flatMap((decodeItem) => decodeItem(decodeSome)));
|
|
1076
|
+
/**
|
|
1077
|
+
* @param option
|
|
1078
|
+
* @returns
|
|
1079
|
+
*/
|
|
1080
|
+
export function encodeListOption(option) {
|
|
1081
|
+
return encodeList(option ? [option] : []);
|
|
1082
|
+
}
|
|
886
1083
|
/**
|
|
887
1084
|
* Decodes a CBOR encoded object with integer keys.
|
|
888
1085
|
* For each field a decoder is called which takes the field index and the field bytes as arguments.
|
|
@@ -893,16 +1090,15 @@ export const isNull = (bytes) => Bytes.makeStream(bytes)
|
|
|
893
1090
|
export const decodeObjectIKey = (fieldDecoders) => (bytes) => {
|
|
894
1091
|
const stream = Bytes.makeStream(bytes);
|
|
895
1092
|
const res = {};
|
|
896
|
-
return decodeMap(() =>
|
|
1093
|
+
return decodeMap(() => Either.right(null), (pairStream) => Either.gen(function* () {
|
|
897
1094
|
const key = Number(yield* decodeInt(pairStream));
|
|
898
1095
|
const decoder = fieldDecoders[key];
|
|
899
1096
|
if (decoder === undefined) {
|
|
900
|
-
return yield* new DecodeError(pairStream, `unhandled object field ${key}`);
|
|
1097
|
+
return yield* Either.left(new DecodeError(pairStream, `unhandled object field ${key}`));
|
|
901
1098
|
}
|
|
902
1099
|
/* eslint-disable-next-line @typescript-eslint/no-unsafe-assignment */
|
|
903
1100
|
res[key] = yield* decoder(pairStream);
|
|
904
|
-
|
|
905
|
-
}))(stream).pipe(Effect.map(() => {
|
|
1101
|
+
}))(stream).pipe(Either.map(() => {
|
|
906
1102
|
return res;
|
|
907
1103
|
}));
|
|
908
1104
|
};
|
|
@@ -916,16 +1112,15 @@ export const decodeObjectIKey = (fieldDecoders) => (bytes) => {
|
|
|
916
1112
|
export const decodeObjectSKey = (fieldDecoders) => (bytes) => {
|
|
917
1113
|
const stream = Bytes.makeStream(bytes);
|
|
918
1114
|
const res = {};
|
|
919
|
-
return decodeMap(() =>
|
|
1115
|
+
return decodeMap(() => Either.right(null), (pairStream) => Either.gen(function* () {
|
|
920
1116
|
const key = yield* decodeString(pairStream);
|
|
921
1117
|
const decoder = fieldDecoders[key];
|
|
922
1118
|
if (decoder === undefined) {
|
|
923
|
-
return yield* new DecodeError(pairStream, `unhandled object field ${key}`);
|
|
1119
|
+
return yield* Either.left(new DecodeError(pairStream, `unhandled object field ${key}`));
|
|
924
1120
|
}
|
|
925
1121
|
/* eslint-disable-next-line @typescript-eslint/no-unsafe-assignment */
|
|
926
1122
|
res[key] = yield* decoder(pairStream);
|
|
927
|
-
|
|
928
|
-
}))(stream).pipe(Effect.map(() => {
|
|
1123
|
+
}))(stream).pipe(Either.map(() => {
|
|
929
1124
|
return res;
|
|
930
1125
|
}));
|
|
931
1126
|
};
|
|
@@ -975,16 +1170,19 @@ const SET_TAG = 258n;
|
|
|
975
1170
|
* @param itemDecoder
|
|
976
1171
|
* @returns
|
|
977
1172
|
*/
|
|
978
|
-
export const decodeSet = (itemDecoder) => (bytes) =>
|
|
1173
|
+
export const decodeSet = (itemDecoder) => (bytes) => {
|
|
979
1174
|
const stream = Bytes.makeStream(bytes);
|
|
980
|
-
if (
|
|
981
|
-
const tag =
|
|
982
|
-
if (tag
|
|
983
|
-
return
|
|
1175
|
+
if (isTag(stream)) {
|
|
1176
|
+
const tag = decodeTag(stream);
|
|
1177
|
+
if (Either.isLeft(tag)) {
|
|
1178
|
+
return Either.left(tag.left);
|
|
1179
|
+
}
|
|
1180
|
+
if (tag.right != SET_TAG) {
|
|
1181
|
+
return Either.left(new DecodeError(stream, `expected tag ${SET_TAG} for set, got tag ${tag.right}`));
|
|
984
1182
|
}
|
|
985
1183
|
}
|
|
986
|
-
return
|
|
987
|
-
}
|
|
1184
|
+
return decodeList(itemDecoder)(stream);
|
|
1185
|
+
};
|
|
988
1186
|
/**
|
|
989
1187
|
* A tagged def list (tag 258n)
|
|
990
1188
|
* @param items
|
|
@@ -997,36 +1195,45 @@ export function encodeSet(items) {
|
|
|
997
1195
|
* @param bytes
|
|
998
1196
|
* @returns
|
|
999
1197
|
*/
|
|
1000
|
-
export const isSet = (bytes) =>
|
|
1198
|
+
export const isSet = (bytes) => {
|
|
1199
|
+
const t = peekTag(bytes);
|
|
1200
|
+
if (Either.isLeft(t)) {
|
|
1201
|
+
return false;
|
|
1202
|
+
}
|
|
1203
|
+
return t.right == SET_TAG;
|
|
1204
|
+
};
|
|
1205
|
+
const decodeSplitString = (stream) => decodeList((itemBytes) => decodeStringInternal(itemBytes))(stream).pipe(Either.map((parts) => parts.join("")));
|
|
1001
1206
|
/**
|
|
1002
1207
|
* @param bytes
|
|
1003
1208
|
* @returns
|
|
1004
1209
|
*/
|
|
1005
|
-
export const decodeString = (bytes) =>
|
|
1210
|
+
export const decodeString = (bytes) => {
|
|
1006
1211
|
const stream = Bytes.makeStream(bytes);
|
|
1007
|
-
if (
|
|
1008
|
-
|
|
1009
|
-
yield* decodeList((itemBytes) => decodeStringInternal(itemBytes).pipe(Effect.tap((s) => {
|
|
1010
|
-
result += s;
|
|
1011
|
-
})))(stream);
|
|
1012
|
-
return result;
|
|
1212
|
+
if (isDefList(stream)) {
|
|
1213
|
+
return decodeSplitString(stream);
|
|
1013
1214
|
}
|
|
1014
1215
|
else {
|
|
1015
|
-
return
|
|
1216
|
+
return decodeStringInternal(stream);
|
|
1016
1217
|
}
|
|
1017
|
-
}
|
|
1218
|
+
};
|
|
1018
1219
|
/**
|
|
1019
1220
|
* @param bytes
|
|
1020
1221
|
* @returns
|
|
1021
1222
|
*/
|
|
1022
|
-
const decodeStringInternal = (bytes) =>
|
|
1223
|
+
const decodeStringInternal = (bytes) => {
|
|
1023
1224
|
const stream = Bytes.makeStream(bytes);
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
})
|
|
1225
|
+
return decodeDefHead(stream).pipe(Either.flatMap(([m, n]) => {
|
|
1226
|
+
if (m != 3) {
|
|
1227
|
+
return Either.left(new DecodeError(stream, "unexpected"));
|
|
1228
|
+
}
|
|
1229
|
+
return stream.shiftMany(Number(n));
|
|
1230
|
+
}), Either.flatMap(Utf8.decode), Either.mapLeft((e) => {
|
|
1231
|
+
if (e._tag == "DecodeException") {
|
|
1232
|
+
return new DecodeError(stream, `invalid utf8 (${e.message})`);
|
|
1233
|
+
}
|
|
1234
|
+
return e;
|
|
1235
|
+
}));
|
|
1236
|
+
};
|
|
1030
1237
|
/**
|
|
1031
1238
|
* Encodes a Utf8 string into Cbor bytes.
|
|
1032
1239
|
* Strings can be split into lists with chunks of up to 64 bytes
|
|
@@ -1065,19 +1272,26 @@ export function encodeString(str, split = false) {
|
|
|
1065
1272
|
* @param bytes
|
|
1066
1273
|
* @returns
|
|
1067
1274
|
*/
|
|
1068
|
-
export const isString = (bytes) =>
|
|
1275
|
+
export const isString = (bytes) => {
|
|
1276
|
+
const m = peekMajorType(bytes);
|
|
1277
|
+
if (Either.isLeft(m)) {
|
|
1278
|
+
return false;
|
|
1279
|
+
}
|
|
1280
|
+
return m.right == 3;
|
|
1281
|
+
};
|
|
1069
1282
|
/**
|
|
1070
1283
|
* @param bytes
|
|
1071
1284
|
* @returns
|
|
1072
1285
|
*/
|
|
1073
|
-
export const decodeTag = (bytes) =>
|
|
1286
|
+
export const decodeTag = (bytes) => {
|
|
1074
1287
|
const stream = Bytes.makeStream(bytes);
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
});
|
|
1288
|
+
return decodeDefHead(stream).pipe(Either.flatMap(([m, n]) => {
|
|
1289
|
+
if (m != 6) {
|
|
1290
|
+
return Either.left(new DecodeError(stream, "unexpected major type for tag"));
|
|
1291
|
+
}
|
|
1292
|
+
return Either.right(n);
|
|
1293
|
+
}));
|
|
1294
|
+
};
|
|
1081
1295
|
/**
|
|
1082
1296
|
* Unrelated to constructor
|
|
1083
1297
|
* @param tag
|
|
@@ -1096,19 +1310,36 @@ export function encodeTag(tag) {
|
|
|
1096
1310
|
* @param bytes
|
|
1097
1311
|
* @returns
|
|
1098
1312
|
*/
|
|
1099
|
-
export const isTag = (bytes) =>
|
|
1313
|
+
export const isTag = (bytes) => {
|
|
1314
|
+
const m = peekMajorType(bytes);
|
|
1315
|
+
if (Either.isLeft(m)) {
|
|
1316
|
+
return false;
|
|
1317
|
+
}
|
|
1318
|
+
return m.right == 6;
|
|
1319
|
+
};
|
|
1100
1320
|
/**
|
|
1101
1321
|
* @param bytes
|
|
1102
1322
|
* @returns
|
|
1103
1323
|
*/
|
|
1104
|
-
export const peekTag = (bytes) =>
|
|
1324
|
+
export const peekTag = (bytes) => {
|
|
1325
|
+
const t = decodeTag(Bytes.makeStream(bytes).copy());
|
|
1326
|
+
if (Either.isLeft(t)) {
|
|
1327
|
+
if (t.left._tag == "Cbor.DecodeError") {
|
|
1328
|
+
return Either.right(undefined);
|
|
1329
|
+
}
|
|
1330
|
+
else {
|
|
1331
|
+
return Either.left(t.left);
|
|
1332
|
+
}
|
|
1333
|
+
}
|
|
1334
|
+
return Either.right(t.right);
|
|
1335
|
+
};
|
|
1105
1336
|
/**
|
|
1106
1337
|
* @param bytes
|
|
1107
1338
|
* @returns
|
|
1108
1339
|
*/
|
|
1109
|
-
export const decodeTagged = (bytes) =>
|
|
1340
|
+
export const decodeTagged = (bytes) => Either.gen(function* () {
|
|
1110
1341
|
const stream = Bytes.makeStream(bytes);
|
|
1111
|
-
if (
|
|
1342
|
+
if (isList(stream)) {
|
|
1112
1343
|
const decodeItem = yield* decodeListLazy(stream);
|
|
1113
1344
|
const tag = Number(yield* decodeItem(decodeInt));
|
|
1114
1345
|
return [tag, decodeItem];
|
|
@@ -1125,25 +1356,25 @@ export const decodeTagged = (bytes) => Effect.gen(function* () {
|
|
|
1125
1356
|
* Defaults to empty tuple
|
|
1126
1357
|
* @returns
|
|
1127
1358
|
*/
|
|
1128
|
-
export const decodeTuple = (itemDecoders, optionalDecoders = []) => (bytes) =>
|
|
1359
|
+
export const decodeTuple = (itemDecoders, optionalDecoders = []) => (bytes) => Either.gen(function* () {
|
|
1129
1360
|
const stream = Bytes.makeStream(bytes);
|
|
1130
1361
|
/**
|
|
1131
1362
|
* decodeList is the right decoder, but has the wrong type interface
|
|
1132
1363
|
* Cast the result to `any` to avoid type errors
|
|
1133
1364
|
*/
|
|
1134
|
-
const res = yield* decodeList((itemStream, i) =>
|
|
1365
|
+
const res = yield* decodeList((itemStream, i) => Either.gen(function* () {
|
|
1135
1366
|
let decoder = itemDecoders[i];
|
|
1136
1367
|
if (decoder === undefined) {
|
|
1137
1368
|
decoder = optionalDecoders[i - itemDecoders.length];
|
|
1138
1369
|
if (decoder === undefined) {
|
|
1139
|
-
return yield* new DecodeError(itemStream, `expected at most ${itemDecoders.length + optionalDecoders.length} items, got more than ${i}`);
|
|
1370
|
+
return yield* Either.left(new DecodeError(itemStream, `expected at most ${itemDecoders.length + optionalDecoders.length} items, got more than ${i}`));
|
|
1140
1371
|
}
|
|
1141
1372
|
}
|
|
1142
1373
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
1143
1374
|
return yield* decoder(itemStream);
|
|
1144
1375
|
}))(stream);
|
|
1145
1376
|
if (res.length < itemDecoders.length) {
|
|
1146
|
-
return yield* new DecodeError(stream, `expected at least ${itemDecoders.length} items, only got ${res.length}`);
|
|
1377
|
+
return yield* Either.left(new DecodeError(stream, `expected at least ${itemDecoders.length} items, only got ${res.length}`));
|
|
1147
1378
|
}
|
|
1148
1379
|
return res;
|
|
1149
1380
|
});
|