Cardano.Script

Script

type Script
= Native NativeScript
| Plutus PlutusScript

Cardano script, either a native script or a plutus script.

script = [ 0, native_script // 1, plutus_v1_script // 2, plutus_v2_script ]

Babbage implementation in Pallas.

= ScriptPubkey (Bytes CredentialHash)
| ScriptAll (List NativeScript)
| ScriptAny (List NativeScript)
| ScriptNofK Int (List NativeScript)
| InvalidBefore Natural
| InvalidHereafter Natural

A plutus script.

= PlutusV1
| PlutusV2
| PlutusV3

The plutus version.

Phantom type describing the kind of bytes within a [PlutusScript] object. They are supposed to be the Flat bytes wrapped into a single CBOR byte string.

Extract all mentionned pubkeys in the Native script. Keys of the dict are the hex version of the keys.

Validate the multisig logic with the provided signers. The temporal aspect of the script is not considered (evaluates True).

Compute the script hash.

The script type tag must be prepended before hashing, but not encapsulated as a list to make a valid CBOR struct. This is not valid CBOR, just concatenation of tag|scriptBytes.

fromBech32 : String -> Maybe (Bytes CredentialHash)

Convert a script hash from its Bech32 representation.

Convert a script hash to its Bech32 representation.

Script Reference type, to be used to store a script into UTxOs. Internally, it also stores the Bytes version of the script to later have correct fees computations.

Create a Script Reference from the script bytes. Returns Nothing if the bytes are not a valid script.

Create a Script Reference from a Script (using elm-cardano encoding approach).

Extract the Bytes from a script Reference.

If the script was encoded as would elm-cardano, this would be equivalent to calling Bytes.fromBytes <| encode (toCbor script)

Extract the Script from a script Reference.

Extract the Script hash from the script Reference.

Scripts and Bytes

Decode a native script from its bytes representation.

This is just a helper function doing the following:

Cbor.Decode.decode decodeNativeScript (Bytes.toBytes bytes)

Get the bytes representation of a native script.

This is just a helper function doing the following:

nativeScriptBytes nativeScript =
    encodeNativeScript nativeScript
        |> Cbor.Encode.encode
        |> Bytes.fromBytes

Create a PlutusScript from its bytes representation.

Depending on where the bytes come from, they can have many different shapes. At the lowest level, Plutus scripts are encoded with a format called Flat. But since Cardano uses CBOR everywhere else, the Flat bytes are usually wrapped in a CBOR byte string. But that’s not the only shape they can have. Indeed, when transmitted in transactions like in a UTxO script reference, they are usually packed into a pair containing an integer tag for the Plutus version, and the CBOR bytes themselves, re-encoded as Bytes in the pair! Meaning the script Flat bytes might get a double CBOR byte wrap.

And if that wasn’t confusing enough, when computing the hash of a Plutus script, it’s not even the above CBOR tagged pair that is used. Instead it is the raw concatenation of the tag and the single-wrapped Flat bytes.

Summary, with pseudo-code:

  1. flat_bytes: the actual Plutus script bytes.
  2. cbor_bytes(flat_bytes): the thing that is provided in the plutus.json blueprint of an Aiken compilation.
  3. cbor_bytes(cbor_bytes(flat_bytes)): how encoded in the witness set and by some CLI tools.
  4. tag + cbor_bytes(flat_bytes): the input of the Blake2b-224 hash to compute the script hash.
  5. cbor_array[cbor_int(tag), cbor_bytes(cbor_bytes(flat_bytes))]: the thing transmitted in UTxO script references.

It’s confusing and error prone, so we made the PlutusScript type opaque, and you basically need to call this function to convert from bytes, which will smartly figure it out.

WARNING: This function is only for shapes (1, 2, 3). Shape (4) is never used except temporarily to compute hashes. Shape (5) is supposed to be decoded with the Script.fromCbor decoder: D.decode Script.fromCbor scriptCborBytes.

If no particular shape is recognized, this function will assume the whole bytes are the Flat bytes, because we don’t have a Flat decoder in Elm.

Extract the PlutusVersion from a PlutusScript.

Extract the script bytes, wrapped in a CBOR byte string, So basically the same thing that you would get in the plutus.json blueprint file.

This is returning: cbor_bytes(flat_bytes).

CBOR Encoders

toCbor : Script -> Encoder

Cbor Encoder for [Script]

Cbor Encoder for [NativeScript]

CBOR and JSON Decoders

fromCbor : Decoder Script

CBOR decoder for [Script].

This does not contain the double CBOR decoding of the script_ref UTxO field. That part has to be handled in the UTxO decoder.

Decode NativeScript from CBOR.