Script

typedef struct cardano_script_t cardano_script_t

Program that decides whether the transaction that spends the output is authorized to do so.


cardano_error_t cardano_script_new_native(cardano_native_script_t *native_script, cardano_script_t **script)

Creates and initializes a new instance of a script from a native_script.

This function creates and initializes a new instance of a cardano_script_t object from a given cardano_native_script_t object. It returns an error code to indicate the success or failure of the operation.

Usage Example:

cardano_native_script_t* native_script = cardano_native_script_new(...);
cardano_script_t* script = NULL;

// Attempt to create a new script from a native_script
cardano_error_t result = cardano_script_new_native(native_script, &script);

if (result == CARDANO_SUCCESS)
{
  // Use the script

  // Once done, ensure to clean up and release the script
  cardano_script_unref(&script);
}

// Clean up the native_script object once done
cardano_native_script_unref(&native_script);

Parameters:
cardano_native_script_t *native_script

[in] A constant pointer to the cardano_native_script_t object from which the script will be created. The object must not be NULL.

cardano_script_t **script

[out] On successful initialization, this will point to a newly created cardano_script_t object. This object represents a “strong reference” to the script, meaning that it is fully initialized and ready for use. The caller is responsible for managing the lifecycle of this object. Specifically, once the script is no longer needed, the caller must release it by calling cardano_script_unref.

Returns:

CARDANO_SUCCESS if the script was successfully created, or an appropriate error code indicating the failure reason.


cardano_error_t cardano_script_new_plutus_v1(cardano_plutus_v1_script_t *plutus_v1_script, cardano_script_t **script)

Creates and initializes a new instance of a script from a Plutus V1 script.

This function creates and initializes a new instance of a cardano_script_t object from a given cardano_plutus_v1_script_t object. It returns an error code to indicate the success or failure of the operation.

Usage Example:

cardano_plutus_v1_script_t* plutus_v1_script = cardano_plutus_v1_script_new(...);
cardano_script_t* script = NULL;

// Attempt to create a new script from a Plutus V1 script
cardano_error_t result = cardano_script_new_plutus_v1(plutus_v1_script, &script);

if (result == CARDANO_SUCCESS)
{
  // Use the script

  // Once done, ensure to clean up and release the script
  cardano_script_unref(&script);
}

// Clean up the plutus_v1_script object once done
cardano_plutus_v1_script_unref(&plutus_v1_script);

Parameters:
cardano_plutus_v1_script_t *plutus_v1_script

[in] A constant pointer to the cardano_plutus_v1_script_t object from which the script will be created. The object must not be NULL.

cardano_script_t **script

[out] On successful initialization, this will point to a newly created cardano_script_t object. This object represents a “strong reference” to the script, meaning that it is fully initialized and ready for use. The caller is responsible for managing the lifecycle of this object. Specifically, once the script is no longer needed, the caller must release it by calling cardano_script_unref.

Returns:

CARDANO_SUCCESS if the script was successfully created, or an appropriate error code indicating the failure reason.


cardano_error_t cardano_script_new_plutus_v2(cardano_plutus_v2_script_t *plutus_v2_script, cardano_script_t **script)

Creates and initializes a new instance of a script from a Plutus V2 script.

This function creates and initializes a new instance of a cardano_script_t object from a given cardano_plutus_v2_script_t object. It returns an error code to indicate the success or failure of the operation.

Usage Example:

cardano_plutus_v2_script_t* plutus_v2_script = cardano_plutus_v2_script_new(...);
cardano_script_t* script = NULL;

// Attempt to create a new script from a Plutus V2 script
cardano_error_t result = cardano_script_new_plutus_v2(plutus_v2_script, &script);

if (result == CARDANO_SUCCESS)
{
  // Use the script

  // Once done, ensure to clean up and release the script
  cardano_script_unref(&script);
}

// Clean up the plutus_v2_script object once done
cardano_plutus_v2_script_unref(&plutus_v2_script);

Parameters:
cardano_plutus_v2_script_t *plutus_v2_script

[in] A constant pointer to the cardano_plutus_v2_script_t object from which the script will be created. The object must not be NULL.

cardano_script_t **script

[out] On successful initialization, this will point to a newly created cardano_script_t object. This object represents a “strong reference” to the script, meaning that it is fully initialized and ready for use. The caller is responsible for managing the lifecycle of this object. Specifically, once the script is no longer needed, the caller must release it by calling cardano_script_unref.

Returns:

CARDANO_SUCCESS if the script was successfully created, or an appropriate error code indicating the failure reason.


cardano_error_t cardano_script_new_plutus_v3(cardano_plutus_v3_script_t *plutus_v3_script, cardano_script_t **script)

Creates and initializes a new instance of a script from a Plutus V3 script.

This function creates and initializes a new instance of a cardano_script_t object from a given cardano_plutus_v3_script_t object. It returns an error code to indicate the success or failure of the operation.

Usage Example:

cardano_plutus_v3_script_t* plutus_v3_script = cardano_plutus_v3_script_new(...);
cardano_script_t* script = NULL;

// Attempt to create a new script from a Plutus V3 script
cardano_error_t result = cardano_script_new_plutus_v3(plutus_v3_script, &script);

if (result == CARDANO_SUCCESS)
{
  // Use the script

  // Once done, ensure to clean up and release the script
  cardano_script_unref(&script);
}

// Clean up the plutus_v3_script object once done
cardano_plutus_v3_script_unref(&plutus_v3_script);

Parameters:
cardano_plutus_v3_script_t *plutus_v3_script

[in] A constant pointer to the cardano_plutus_v3_script_t object from which the script will be created. The object must not be NULL.

cardano_script_t **script

[out] On successful initialization, this will point to a newly created cardano_script_t object. This object represents a “strong reference” to the script, meaning that it is fully initialized and ready for use. The caller is responsible for managing the lifecycle of this object. Specifically, once the script is no longer needed, the caller must release it by calling cardano_script_unref.

Returns:

CARDANO_SUCCESS if the script was successfully created, or an appropriate error code indicating the failure reason.


cardano_error_t cardano_script_from_cbor(cardano_cbor_reader_t *reader, cardano_script_t **script)

Creates a script from a CBOR reader.

This function parses CBOR data using a provided cardano_cbor_reader_t and constructs a cardano_script_t object. It assumes that the CBOR reader is set up correctly and that the CBOR data corresponds to the structure expected for a script.

Usage Example:

cardano_cbor_reader_t* reader = cardano_cbor_reader_new(cbor_data, data_size);
cardano_script_t* script = NULL;

cardano_error_t result = cardano_script_from_cbor(reader, &script);

if (result == CARDANO_SUCCESS)
{
  // Use the script

  // Once done, ensure to clean up and release the script
  cardano_script_unref(&script);
}
else
{
  const char* error = cardano_cbor_reader_get_last_error(reader);
  printf("Failed to decode script: %s\n", error);
}

cardano_cbor_reader_unref(&reader); // Cleanup the CBOR reader

Note

If the function fails, the last error can be retrieved by calling cardano_cbor_reader_get_last_error with the reader. The caller is responsible for freeing the created cardano_script_t object by calling cardano_script_unref when it is no longer needed.

Parameters:
cardano_cbor_reader_t *reader

[in] A pointer to an initialized cardano_cbor_reader_t that is ready to read the CBOR-encoded script data.

cardano_script_t **script

[out] A pointer to a pointer of cardano_script_t that will be set to the address of the newly created script object upon successful decoding.

Returns:

A cardano_error_t value indicating the outcome of the operation. Returns CARDANO_SUCCESS if the script was successfully created, or an appropriate error code if an error occurred.


cardano_error_t cardano_script_to_cbor(const cardano_script_t *script, cardano_cbor_writer_t *writer)

Serializes a script into CBOR format using a CBOR writer.

This function serializes the given cardano_script_t object using a cardano_cbor_writer_t.

Usage Example:

cardano_script_t* script = ...;
cardano_cbor_writer_t* writer = cardano_cbor_writer_new();

if (writer)
{
  cardano_error_t result = cardano_script_to_cbor(script, writer);

  if (result == CARDANO_SUCCESS)
  {
    // Use the writer's buffer containing the serialized data
  }
  else
  {
    const char* error_message = cardano_cbor_writer_get_last_error(writer);
    printf("Serialization failed: %s\n", error_message);
  }

  cardano_cbor_writer_unref(&writer);
}

cardano_script_unref(&script);

Parameters:
const cardano_script_t *script

[in] A constant pointer to the cardano_script_t object that is to be serialized.

cardano_cbor_writer_t *writer

[out] A pointer to a cardano_cbor_writer_t where the CBOR serialized data will be written. The writer must already be initialized and ready to accept the data.

Returns:

Returns CARDANO_SUCCESS if the serialization is successful. If the script or writer is NULL, returns CARDANO_ERROR_POINTER_IS_NULL.


cardano_error_t cardano_script_to_cip116_json(const cardano_script_t *script, cardano_json_writer_t *writer)

Serializes a cardano_script_t as a CIP-116 ScriptRef JSON object.

This function writes a single JSON object.

The inner value is produced by the corresponding serializer:

Keys are emitted in the order: “tag”, then “value”.

Parameters:
const cardano_script_t *script

[in] A valid pointer to the script to serialize.

cardano_json_writer_t *writer

[inout] A valid JSON writer positioned where a value is expected.

Returns:

CARDANO_SUCCESS On success.

Returns:

CARDANO_ERROR_POINTER_IS_NULL If script or writer is NULL.

Returns:

CARDANO_ERROR_INVALID_SCRIPT_LANGUAGE If the script language is not supported.


cardano_error_t cardano_script_get_language(const cardano_script_t *script, cardano_script_language_t *language)

Retrieves the language of the script.

This function retrieves the language of a given cardano_script_t object and stores it in the provided output parameter. The script language is defined in the cardano_script_language_t enumeration.

Usage Example:

cardano_script_t* script = cardano_script_new_bytes_from_hex(...);
cardano_script_language_t language;
cardano_error_t result = cardano_script_get_language(script, &language);

if (result == CARDANO_SUCCESS)
{
  if (language == CARDANO_SCRIPT_LANGUAGE_NATIVE)
  {
    // Handle native script
  }
  else if (language == CARDANO_SCRIPT_LANGUAGE_PLUTUS_V1)
  {
    // Handle Plutus V1 script
  }
  // Add more cases as needed for other languages
}

// Clean up the script object once done
cardano_script_unref(&script);

Parameters:
const cardano_script_t *script

[in] A constant pointer to the cardano_script_t object from which the language is to be retrieved. The object must not be NULL.

cardano_script_language_t *language

[out] Pointer to a variable where the script language will be stored. This variable will be set to the value from the cardano_script_language_t enumeration.

Returns:

CARDANO_SUCCESS if the language was successfully retrieved, or an appropriate error code if the input is NULL or any other error occurs.


cardano_error_t cardano_script_to_native(cardano_script_t *script, cardano_native_script_t **native_script)

Converts a script object to a native script object.

This function converts a cardano_script_t object to a cardano_native_script_t object. It returns an error code to indicate the success or failure of the operation.

Usage Example:

cardano_script_t* script = ...; // Assume script is a valid script object
cardano_native_script_t* native_script = NULL;
cardano_error_t result = cardano_script_to_native(script, &native_script);

if (result == CARDANO_SUCCESS)
{
  // Use the native_script

  // Once done, ensure to clean up and release the native_script
  cardano_native_script_unref(&native_script);
}

// Clean up the script object once done
cardano_script_unref(&script);

Parameters:
cardano_script_t *script

[in] A constant pointer to the cardano_script_t object to be converted.

cardano_native_script_t **native_script

[out] On successful conversion, this will point to a newly created cardano_native_script_t object. This object represents a “strong reference” to the native script, meaning that it is fully initialized and ready for use. The caller is responsible for managing the lifecycle of this object. Specifically, once the native script is no longer needed, the caller must release it by calling cardano_native_script_unref.

Returns:

CARDANO_SUCCESS if the conversion was successful, or an appropriate error code indicating the failure reason.


cardano_error_t cardano_script_to_plutus_v1(cardano_script_t *script, cardano_plutus_v1_script_t **plutus_v1)

Converts a script object to a Plutus V1 script object.

This function converts a cardano_script_t object to a cardano_plutus_v1_script_t object. It returns an error code to indicate the success or failure of the operation.

Usage Example:

cardano_script_t* script = ...; // Assume script is a valid script object
cardano_plutus_v1_script_t* plutus_v1 = NULL;
cardano_error_t result = cardano_script_to_plutus_v1(script, &plutus_v1);

if (result == CARDANO_SUCCESS)
{
  // Use the Plutus V1 script

  // Once done, ensure to clean up and release the Plutus V1 script
  cardano_plutus_v1_script_unref(&plutus_v1);
}

// Clean up the script object once done
cardano_script_unref(&script);

Parameters:
cardano_script_t *script

[in] A constant pointer to the cardano_script_t object to be converted.

cardano_plutus_v1_script_t **plutus_v1

[out] On successful conversion, this will point to a newly created cardano_plutus_v1_script_t object. This object represents a “strong reference” to the Plutus V1 script, meaning that it is fully initialized and ready for use. The caller is responsible for managing the lifecycle of this object. Specifically, once the Plutus V1 script is no longer needed, the caller must release it by calling cardano_plutus_v1_script_unref.

Returns:

CARDANO_SUCCESS if the conversion was successful, or an appropriate error code indicating the failure reason.


cardano_error_t cardano_script_to_plutus_v2(cardano_script_t *script, cardano_plutus_v2_script_t **plutus_v2)

Converts a script object to a Plutus V2 script object.

This function converts a cardano_script_t object to a cardano_plutus_v2_script_t object. It returns an error code to indicate the success or failure of the operation.

Usage Example:

cardano_script_t* script = ...; // Assume script is a valid script object
cardano_plutus_v2_script_t* plutus_v2 = NULL;
cardano_error_t result = cardano_script_to_plutus_v2(script, &plutus_v2);

if (result == CARDANO_SUCCESS)
{
  // Use the Plutus V2 script

  // Once done, ensure to clean up and release the Plutus V2 script
  cardano_plutus_v2_script_unref(&plutus_v2);
}

// Clean up the script object once done
cardano_script_unref(&script);

Parameters:
cardano_script_t *script

[in] A constant pointer to the cardano_script_t object to be converted.

cardano_plutus_v2_script_t **plutus_v2

[out] On successful conversion, this will point to a newly created cardano_plutus_v2_script_t object. This object represents a “strong reference” to the Plutus V2 script, meaning that it is fully initialized and ready for use. The caller is responsible for managing the lifecycle of this object. Specifically, once the Plutus V2 script is no longer needed, the caller must release it by calling cardano_plutus_v2_script_unref.

Returns:

CARDANO_SUCCESS if the conversion was successful, or an appropriate error code indicating the failure reason.


cardano_error_t cardano_script_to_plutus_v3(cardano_script_t *script, cardano_plutus_v3_script_t **plutus_v3)

Converts a script object to a Plutus V3 script object.

This function converts a cardano_script_t object to a cardano_plutus_v3_script_t object. It returns an error code to indicate the success or failure of the operation.

Usage Example:

cardano_script_t* script = ...; // Assume script is a valid script object
cardano_plutus_v3_script_t* plutus_v3 = NULL;
cardano_error_t result = cardano_script_to_plutus_v3(script, &plutus_v3);

if (result == CARDANO_SUCCESS)
{
  // Use the Plutus V3 script

  // Once done, ensure to clean up and release the Plutus V3 script
  cardano_plutus_v3_script_unref(&plutus_v3);
}

// Clean up the script object once done
cardano_script_unref(&script);

Parameters:
cardano_script_t *script

[in] A constant pointer to the cardano_script_t object to be converted.

cardano_plutus_v3_script_t **plutus_v3

[out] On successful conversion, this will point to a newly created cardano_plutus_v3_script_t object. This object represents a “strong reference” to the Plutus V3 script, meaning that it is fully initialized and ready for use. The caller is responsible for managing the lifecycle of this object. Specifically, once the Plutus V3 script is no longer needed, the caller must release it by calling cardano_plutus_v3_script_unref.

Returns:

CARDANO_SUCCESS if the conversion was successful, or an appropriate error code indicating the failure reason.


cardano_blake2b_hash_t *cardano_script_get_hash(const cardano_script_t *script)

Retrieves the hash associated with a script.

This function computes the hash of a cardano_script_t object. It returns a new reference to a cardano_blake2b_hash_t object representing the hash. It the caller’s responsibility to release it by calling cardano_blake2b_hash_unref when it is no longer needed.

Usage Example:

cardano_script_t* original_script = cardano_script_new(...);
cardano_blake2b_hash_t* hash_script = cardano_script_get_hash(original_script);

if (hash_script)
{
  // Use the hash script

  // Once done, ensure to clean up and release the hash script
  cardano_blake2b_hash_unref(&hash_script);
}
// Release the original script after use
cardano_script_unref(&original_script);

Parameters:
const cardano_script_t *script

A constant pointer to the cardano_script_t object from which the hash is to be retrieved.

Returns:

A pointer to a new cardano_blake2b_hash_t object containing the hash. If the input script is NULL, returns NULL. The caller is responsible for managing the lifecycle of this object, including releasing it with cardano_blake2b_hash_unref.


bool cardano_script_equals(const cardano_script_t *lhs, const cardano_script_t *rhs)

Checks if two script objects are equal.

This function compares two cardano_script_t objects for equality. It checks if the contents of the two script objects are identical.

Usage Example:

cardano_script_t* script1 = ...; // Assume script1 is initialized
cardano_script_t* script2 = ...; // Assume script2 is initialized

if (cardano_script_equals(script1, script2))
{
  printf("script1 is equal to script2\n");
}
else
{
  printf("script1 is not equal to script2\n");
}

// Clean up the script objects once done
cardano_script_unref(&script1);
cardano_script_unref(&script2);

Parameters:
const cardano_script_t *lhs

[in] A constant pointer to the first cardano_script_t object to be compared.

const cardano_script_t *rhs

[in] A constant pointer to the second cardano_script_t object to be compared.

Returns:

true if the two script objects are equal (have the same contents), false otherwise.


void cardano_script_unref(cardano_script_t **script)

Decrements the reference count of a script object.

This function is responsible for managing the lifecycle of a cardano_script_t object by decreasing its reference count. When the reference count reaches zero, the script is finalized; its associated resources are released, and its memory is deallocated.

Usage Example:

cardano_script_t* script = cardano_script_new();

// Perform operations with the script...

cardano_script_unref(&script);
// At this point, script is NULL and cannot be used.

Note

After calling cardano_script_unref, the pointer to the cardano_script_t object will be set to NULL to prevent its reuse.

Parameters:
cardano_script_t **script

[inout] A pointer to the pointer of the script object. This double indirection allows the function to set the caller’s pointer to NULL, avoiding dangling pointer issues after the object has been freed.


void cardano_script_ref(cardano_script_t *script)

Increases the reference count of the cardano_script_t object.

This function is used to manually increment the reference count of a script object, indicating that another part of the code has taken ownership of it. This ensures the object remains allocated and valid until all owners have released their reference by calling cardano_script_unref.

Usage Example:

// Assuming script is a previously created script object

cardano_script_ref(script);

// Now script can be safely used elsewhere without worrying about premature deallocation

Note

Always ensure that for every call to cardano_script_ref there is a corresponding call to cardano_script_unref to prevent memory leaks.

Parameters:
cardano_script_t *script

A pointer to the script object whose reference count is to be incremented.


size_t cardano_script_refcount(const cardano_script_t *script)

Retrieves the current reference count of the cardano_script_t object.

This function returns the number of active references to a script object. It’s useful for debugging purposes or managing the lifecycle of the object in complex scenarios.

Usage Example:

// Assuming script is a previously created script object

size_t ref_count = cardano_script_refcount(script);

printf("Reference count: %zu\n", ref_count);

Warning

This function does not account for transitive references. A transitive reference occurs when an object holds a reference to another object, rather than directly to the cardano_script_t. As such, the reported count may not fully represent the total number of conceptual references in cases where such transitive relationships exist.

Parameters:
const cardano_script_t *script

A pointer to the script object whose reference count is queried. The object must not be NULL.

Returns:

The number of active references to the specified script object. If the object is properly managed (i.e., every cardano_script_ref call is matched with a cardano_script_unref call), this count should reach zero right before the object is deallocated.


void cardano_script_set_last_error(cardano_script_t *script, const char *message)

Sets the last error message for a given script object.

Records an error message in the script’s last_error buffer, overwriting any existing message. This is useful for storing descriptive error information that can be later retrieved. The message is truncated if it exceeds the buffer’s capacity.

Note

The error message is limited to 1023 characters, including the null terminator, due to the fixed size of the last_error buffer.

Parameters:
cardano_script_t *script

[in] A pointer to the cardano_script_t instance whose last error message is to be set. If NULL, the function does nothing.

const char *message

[in] A null-terminated string containing the error message. If NULL, the script’s last_error is set to an empty string, indicating no error.


const char *cardano_script_get_last_error(const cardano_script_t *script)

Retrieves the last error message recorded for a specific script.

This function returns a pointer to the null-terminated string containing the last error message set by cardano_script_set_last_error for the given script. If no error message has been set, or if the last_error buffer was explicitly cleared, an empty string is returned, indicating no error.

Note

The returned string points to internal storage within the object and must not be modified by the caller. The string remains valid until the next call to cardano_script_set_last_error for the same script, or until the script is deallocated.

Parameters:
const cardano_script_t *script

[in] A pointer to the cardano_script_t instance whose last error message is to be retrieved. If the script is NULL, the function returns a generic error message indicating the null script.

Returns:

A pointer to a null-terminated string containing the last error message for the specified script. If the script is NULL, “Object is NULL.” is returned to indicate the error.