Plutus Script - V1

typedef struct cardano_plutus_v1_script_t cardano_plutus_v1_script_t

Plutus’ scripts are pieces of code that implement pure functions with True or False outputs.

These functions take several inputs such as Datum, Redeemer and the transaction context to decide whether an output can be spent or not.

V1 was the initial version of Plutus, introduced in the Alonzo hard fork.


cardano_error_t cardano_plutus_v1_script_new_bytes(const byte_t *bytes, size_t size, cardano_plutus_v1_script_t **plutus_v1_script)

Creates and initializes a new instance of a plutus script from a byte array.

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

Usage Example:

const byte_t bytes[] = {0x01, 0x02, 0x03, 0x04 ... }; // Example byte array
size_t size = sizeof(bytes); // Calculate the size of the byte array
cardano_plutus_v1_script_t* plutus_v1_script = NULL;

// Attempt to create a new plutus script from a byte array
cardano_error_t result = cardano_plutus_v1_script_new_bytes(bytes, size, &plutus_v1_script);

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

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

Parameters:
const byte_t *bytes

[in] A pointer to the byte array from which the plutus_v1_script will be created.

size_t size

[in] The size of the byte array.

cardano_plutus_v1_script_t **plutus_v1_script

[out] On successful initialization, this will point to a newly created cardano_plutus_v1_script_t object. This object represents a “strong reference” to the plutus 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 script is no longer needed, the caller must release it by calling cardano_plutus_v1_script_unref.

Returns:

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


cardano_error_t cardano_plutus_v1_script_new_bytes_from_hex(const char *hex, size_t size, cardano_plutus_v1_script_t **plutus_v1_script)

Creates and initializes a new instance of a plutus script from a hexadecimal string.

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

Usage Example:

const char* hex_string = "0123456789abcdef..."; // Example hexadecimal string
size_t size = strlen(hex_string); // Calculate the size of the string
cardano_plutus_v1_script_t* plutus_v1_script = NULL;

// Attempt to create a new plutus script from a hexadecimal string
cardano_error_t result = cardano_plutus_v1_script_new_bytes_from_hex(hex_string, size, &plutus_v1_script);

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

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

Parameters:
const char *hex

[in] A pointer to the hexadecimal string from which the plutus script will be created.

size_t size

[in] The size of the hexadecimal string.

cardano_plutus_v1_script_t **plutus_v1_script

[out] On successful initialization, this will point to a newly created cardano_plutus_v1_script_t object. This object represents a “strong reference” to the plutus 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 script is no longer needed, the caller must release it by calling cardano_plutus_v1_script_unref.

Returns:

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


cardano_error_t cardano_plutus_v1_script_from_cbor(cardano_cbor_reader_t *reader, cardano_plutus_v1_script_t **plutus_v1_script)

Creates a plutus_v1_script from a CBOR reader.

This function parses CBOR data using a provided cardano_cbor_reader_t and constructs a cardano_plutus_v1_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 plutus_v1_script.

Usage Example:

cardano_cbor_reader_t* reader = cardano_cbor_reader_new(cbor_data, data_size);
cardano_plutus_v1_script_t* plutus_v1_script = NULL;

cardano_error_t result = cardano_plutus_v1_script_from_cbor(reader, &plutus_v1_script);

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_script);
}
else
{
  const char* error = cardano_cbor_reader_get_last_error(reader);
  printf("Failed to decode plutus_v1_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_plutus_v1_script_t object by calling cardano_plutus_v1_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 plutus_v1_script data.

cardano_plutus_v1_script_t **plutus_v1_script

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

Returns:

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


cardano_error_t cardano_plutus_v1_script_to_cbor(const cardano_plutus_v1_script_t *plutus_v1_script, cardano_cbor_writer_t *writer)

Serializes a plutus_v1_script into CBOR format using a CBOR writer.

This function serializes the given cardano_plutus_v1_script_t object using a cardano_cbor_writer_t.

Usage Example:

cardano_plutus_v1_script_t* plutus_v1_script = ...;
cardano_cbor_writer_t* writer = cardano_cbor_writer_new();

if (writer)
{
  cardano_error_t result = cardano_plutus_v1_script_to_cbor(plutus_v1_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_plutus_v1_script_unref(&plutus_v1_script);

Parameters:
const cardano_plutus_v1_script_t *plutus_v1_script

[in] A constant pointer to the cardano_plutus_v1_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 plutus_v1_script or writer is NULL, returns CARDANO_ERROR_POINTER_IS_NULL.


cardano_error_t cardano_plutus_v1_script_to_cip116_json(const cardano_plutus_v1_script_t *plutus_v1_script, cardano_json_writer_t *writer)

Serializes a Plutus v1 script into a CIP-116 JSON object.

Writes a single JSON object representing the script:

{
  "language": "plutus_v1",
  "bytes": "<hex-lowercase>"
}

The function emits both the opening and closing braces. It may be called at the JSON root or anywhere a value is expected (e.g., as an array element or after a property name).

cardano_json_writer_t* w = cardano_json_writer_new(CARDANO_JSON_FORMAT_COMPACT);
cardano_error_t rc = cardano_plutus_v1_script_to_cip116_json(script, w);

if (rc == CARDANO_SUCCESS)
{
  cardano_buffer_t* out = NULL;
  rc = cardano_json_writer_encode_in_buffer(w, &out);
  // Use 'out' ...
  cardano_buffer_unref(&out);
}

cardano_json_writer_unref(&w);

Parameters:
const cardano_plutus_v1_script_t *plutus_v1_script

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

cardano_json_writer_t *writer

[inout] A valid JSON writer positioned where a value can be written.

Returns:

  • CARDANO_SUCCESS on success.

  • CARDANO_ERROR_POINTER_IS_NULL if plutus_v1_script or writer is NULL.

  • CARDANO_ERROR_MEMORY_ALLOCATION_FAILED if a temporary buffer cannot be allocated.

  • An error propagated from the writer if it is already in an error state (see cardano_json_writer_get_last_error).


cardano_error_t cardano_plutus_v1_script_to_raw_bytes(cardano_plutus_v1_script_t *plutus_v1_script, cardano_buffer_t **compiled_script)

Gets the raw bytes of a compiled Plutus v1 script.

If you need “cborBytes” for cardano-cli use cardano_plutus_v1_script_to_cbor instead.

This function converts a cardano_plutus_v1_script_t object to its raw byte representation. The resulting bytes are stored in a cardano_buffer_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 = ...; // Assume plutus_v1_script is initialized
cardano_buffer_t* compiled_script = NULL;
cardano_error_t result = cardano_plutus_v1_script_to_raw_bytes(plutus_v1_script, &compiled_script);

if (result == CARDANO_SUCCESS)
{
  // Use the bounded bytes
  printf("Extracted bounded bytes: ");

  for (size_t i = 0; i < cardano_buffer_get_size(compiled_script); ++i)
  {
    printf("%02X ", cardano_buffer_get_data(compiled_script)[i]);
  }

  printf("\n");

  // Once done, ensure to clean up and release the bounded bytes
  cardano_buffer_unref(&compiled_script);
}
else
{
  // Handle error
  printf("Failed to extract bounded bytes: %s\n", cardano_error_to_string(result));
}

// 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 to be converted.

cardano_buffer_t **compiled_script

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

Returns:

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


cardano_blake2b_hash_t *cardano_plutus_v1_script_get_hash(const cardano_plutus_v1_script_t *plutus_v1_script)

Retrieves the hash associated with a plutus_v1_script.

This function computes the hash of a cardano_plutus_v1_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_plutus_v1_script_t* original_plutus_v1_script = cardano_plutus_v1_script_new(...);
cardano_blake2b_hash_t* hash_plutus_v1_script = cardano_plutus_v1_script_get_hash(original_plutus_v1_script);

if (hash_plutus_v1_script)
{
  // Use the hash plutus_v1_script

  // Once done, ensure to clean up and release the hash plutus_v1_script
  cardano_blake2b_hash_unref(&hash_plutus_v1_script);
}
// Release the original plutus_v1_script after use
cardano_plutus_v1_script_unref(&original_plutus_v1_script);

Parameters:
const cardano_plutus_v1_script_t *plutus_v1_script

A constant pointer to the cardano_plutus_v1_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 plutus_v1_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_plutus_v1_script_equals(const cardano_plutus_v1_script_t *lhs, const cardano_plutus_v1_script_t *rhs)

Checks if two Plutus v1 script objects are equal.

This function compares two cardano_plutus_v1_script_t objects to determine if they are equal. Two Plutus v1 scripts are considered equal if they have the same content.

Usage Example:

cardano_plutus_v1_script_t* script1 = ...;  // Assume script1 is initialized here
cardano_plutus_v1_script_t* script2 = ...;  // Assume script2 is initialized here

if (cardano_plutus_v1_script_equals(script1, script2))
{
  // The two Plutus v1 scripts are equal
}
else
{
  // The two Plutus v1 scripts are not equal
}

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

Parameters:
const cardano_plutus_v1_script_t *lhs

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

const cardano_plutus_v1_script_t *rhs

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

Returns:

true if the two Plutus v1 scripts are equal, false otherwise.


void cardano_plutus_v1_script_unref(cardano_plutus_v1_script_t **plutus_v1_script)

Decrements the reference count of a plutus_v1_script object.

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

Usage Example:

cardano_plutus_v1_script_t* plutus_v1_script = cardano_plutus_v1_script_new();

// Perform operations with the plutus_v1_script...

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

Note

After calling cardano_plutus_v1_script_unref, the pointer to the cardano_plutus_v1_script_t object will be set to NULL to prevent its reuse.

Parameters:
cardano_plutus_v1_script_t **plutus_v1_script

[inout] A pointer to the pointer of the plutus_v1_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_plutus_v1_script_ref(cardano_plutus_v1_script_t *plutus_v1_script)

Increases the reference count of the cardano_plutus_v1_script_t object.

This function is used to manually increment the reference count of a plutus_v1_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_plutus_v1_script_unref.

Usage Example:

// Assuming plutus_v1_script is a previously created plutus_v1_script object

cardano_plutus_v1_script_ref(plutus_v1_script);

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

Note

Always ensure that for every call to cardano_plutus_v1_script_ref there is a corresponding call to cardano_plutus_v1_script_unref to prevent memory leaks.

Parameters:
cardano_plutus_v1_script_t *plutus_v1_script

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


size_t cardano_plutus_v1_script_refcount(const cardano_plutus_v1_script_t *plutus_v1_script)

Retrieves the current reference count of the cardano_plutus_v1_script_t object.

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

Usage Example:

// Assuming plutus_v1_script is a previously created plutus_v1_script object

size_t ref_count = cardano_plutus_v1_script_refcount(plutus_v1_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_plutus_v1_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_plutus_v1_script_t *plutus_v1_script

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

Returns:

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


void cardano_plutus_v1_script_set_last_error(cardano_plutus_v1_script_t *plutus_v1_script, const char *message)

Sets the last error message for a given plutus_v1_script object.

Records an error message in the plutus_v1_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_plutus_v1_script_t *plutus_v1_script

[in] A pointer to the cardano_plutus_v1_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 plutus_v1_script’s last_error is set to an empty string, indicating no error.


const char *cardano_plutus_v1_script_get_last_error(const cardano_plutus_v1_script_t *plutus_v1_script)

Retrieves the last error message recorded for a specific plutus_v1_script.

This function returns a pointer to the null-terminated string containing the last error message set by cardano_plutus_v1_script_set_last_error for the given plutus_v1_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_plutus_v1_script_set_last_error for the same plutus_v1_script, or until the plutus_v1_script is deallocated.

Parameters:
const cardano_plutus_v1_script_t *plutus_v1_script

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

Returns:

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