Plutus Data

typedef struct cardano_plutus_data_t cardano_plutus_data_t

A type corresponding to the Plutus Core Data datatype.

The point of this type is to be opaque as to ensure that it is only used in ways that plutus scripts can handle.

Use this type to build any data structures that you want to be representable on-chain.


cardano_error_t cardano_plutus_data_new_constr(cardano_constr_plutus_data_t *constr, cardano_plutus_data_t **plutus_data)

Creates and initializes a new instance of a plutus_data from a constr_plutus_data.

This function creates and initializes a new instance of a cardano_plutus_data_t object from a given cardano_constr_plutus_data_t object. It essentially converts a constr_plutus_data into a plutus_data. It returns an error code to indicate the success or failure of the operation.

Usage Example:

cardano_constr_plutus_data_t* constr_plutus_data = cardano_constr_plutus_data_new(...);
cardano_plutus_data_t* plutus_data = NULL;

// Attempt to create a new plutus_data from a constr_plutus_data
cardano_error_t result = cardano_plutus_data_new_constr(constr_plutus_data, &plutus_data);

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

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

// Clean up the constr_plutus_data object once done
cardano_constr_plutus_data_unref(&constr_plutus_data);

Parameters:
cardano_constr_plutus_data_t *constr

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

cardano_plutus_data_t **plutus_data

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

Returns:

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


cardano_error_t cardano_plutus_data_new_map(cardano_plutus_map_t *map, cardano_plutus_data_t **plutus_data)

Creates and initializes a new instance of a plutus_data from a plutus_map.

This function creates and initializes a new instance of a cardano_plutus_data_t object from a given cardano_plutus_map_t object. It essentially converts a plutus map into a plutus data. It returns an error code to indicate the success or failure of the operation.

Usage Example:

cardano_plutus_map_t* map = cardano_plutus_map_new(...);
cardano_plutus_data_t* plutus_data = NULL;

// Attempt to create a new plutus_data from a plutus_map
cardano_error_t result = cardano_plutus_data_new_map(map, &plutus_data);

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

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

// Clean up the plutus_map object once done
cardano_plutus_map_unref(&map);

Parameters:
cardano_plutus_map_t *map

[in] A pointer to the cardano_plutus_map_t object from which the plutus data will be created.

cardano_plutus_data_t **plutus_data

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

Returns:

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


cardano_error_t cardano_plutus_data_new_list(cardano_plutus_list_t *list, cardano_plutus_data_t **plutus_data)

Creates and initializes a new instance of a plutus data from a plutus list.

This function creates and initializes a new instance of a cardano_plutus_data_t object from a given cardano_plutus_list_t object. It essentially converts a plutus list into a plutus data. It returns an error code to indicate the success or failure of the operation.

Usage Example:

cardano_plutus_list_t* list = cardano_plutus_list_new(...);
cardano_plutus_data_t* plutus_data = NULL;

// Attempt to create a new plutus_data from a plutus_list
cardano_error_t result = cardano_plutus_data_new_list(list, &plutus_data);

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

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

// Clean up the plutus_list object once done
cardano_plutus_list_unref(&list);

Parameters:
cardano_plutus_list_t *list

[in] A pointer to the cardano_plutus_list_t object from which the plutus data will be created. The object must not be NULL.

cardano_plutus_data_t **plutus_data

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

Returns:

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


cardano_error_t cardano_plutus_data_new_integer(const cardano_bigint_t *bigint, cardano_plutus_data_t **plutus_data)

Creates and initializes a new instance of plutus data from a bigint object.

This function creates and initializes a new instance of a cardano_plutus_data_t object from a given bigint object.

Usage Example:

// Assume bigint is already initialized and set to a value
cardano_bigint_t* bigint = ...;
cardano_plutus_data_t* plutus_data = NULL;

// Attempt to create a new plutus_data from a bigint
cardano_error_t result = cardano_plutus_data_new_integer(bigint, &plutus_data);

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

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

Parameters:
const cardano_bigint_t *bigint

[in] A constant pointer to the cardano_bigint_t object from which the plutus_data will be created.

cardano_plutus_data_t **plutus_data

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

Returns:

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


cardano_error_t cardano_plutus_data_new_integer_from_int(int64_t integer, cardano_plutus_data_t **plutus_data)

Creates and initializes a new instance of plutus data from an integer value.

This function creates and initializes a new instance of a cardano_plutus_data_t object from a given int64_t integer. It converts an integer into a plutus data object, suitable for transactions and other blockchain-related operations. It returns an error code to indicate the success or failure of the operation.

Usage Example:

int64_t integer_value = 42; // Example integer value
cardano_plutus_data_t* plutus_data = NULL;

// Attempt to create a new plutus_data from an integer value
cardano_error_t result = cardano_plutus_data_new_integer_from_int(integer_value, &plutus_data);

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

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

Parameters:
int64_t integer

[in] The int64_t integer value from which the plutus_data will be created.

cardano_plutus_data_t **plutus_data

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

Returns:

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


cardano_error_t cardano_plutus_data_new_integer_from_uint(uint64_t integer, cardano_plutus_data_t **plutus_data)

Creates and initializes a new instance of plutus data from an unsigned integer value.

This function creates and initializes a new instance of a cardano_plutus_data_t object from a given uint64_t integer.

Usage Example:

uint64_t integer_value = 18446744073709551615u; // Example unsigned integer value
cardano_plutus_data_t* plutus_data = NULL;

// Attempt to create a new plutus_data from an unsigned integer value
cardano_error_t result = cardano_plutus_data_new_integer_from_uint(integer_value, &plutus_data);

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

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

Parameters:
uint64_t integer

[in] The uint64_t integer value from which the plutus_data will be created.

cardano_plutus_data_t **plutus_data

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

Returns:

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


cardano_error_t cardano_plutus_data_new_integer_from_string(const char *string, size_t size, int32_t base, cardano_plutus_data_t **plutus_data)

Creates and initializes a new instance of Plutus data from a string representation of an integer in a specified base.

This function creates and initializes a new instance of a cardano_plutus_data_t object from a given string representing an integer, interpreted according to the specified base (e.g., 10 for decimal, 16 for hexadecimal).

Usage Example:

const char* integer_str = "12345678901234567890"; // Example large integer value in string form
cardano_plutus_data_t* plutus_data = NULL;
int32_t base = 10; // Decimal base

// Attempt to create a new plutus_data from a string representation of an integer in decimal
cardano_error_t result = cardano_plutus_data_new_integer_from_string(integer_str, strlen(integer_str), base, &plutus_data);

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

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

Parameters:
const char *string

[in] A pointer to the character string containing the numeric representation of the integer.

size_t size

[in] The number of characters in the string, excluding the null terminator.

int32_t base

[in] The numerical base in which the string representation is expressed. Valid values are between 2 and 36.

cardano_plutus_data_t **plutus_data

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

Returns:

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


cardano_error_t cardano_plutus_data_new_bytes(const byte_t *bytes, size_t size, cardano_plutus_data_t **plutus_data)

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

This function creates and initializes a new instance of a cardano_plutus_data_t object from a given byte array. It essentially converts a byte array into a plutus data. 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) / sizeof(byte_t); // Calculate the size of the byte array
cardano_plutus_data_t* plutus_data = NULL;

// Attempt to create a new plutus data from a byte array
cardano_error_t result = cardano_plutus_data_new_bytes(bytes, size, &plutus_data);

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

  // Once done, ensure to clean up and release the plutus data
  cardano_plutus_data_unref(&plutus_data);
}

Parameters:
const byte_t *bytes

[in] A pointer to the byte array from which the plutus_data will be created. The byte array must not be NULL.

size_t size

[in] The size of the byte array.

cardano_plutus_data_t **plutus_data

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

Returns:

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


cardano_error_t cardano_plutus_data_new_bytes_from_hex(const char *hex, size_t size, cardano_plutus_data_t **plutus_data)

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

This function creates and initializes a new instance of a cardano_plutus_data_t object from a given hexadecimal string. It essentially converts a hexadecimal string into a plutus data, treating each pair of characters as a byte. 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_data_t* plutus_data = NULL;

// Attempt to create a new plutus data from a hexadecimal string
cardano_error_t result = cardano_plutus_data_new_bytes_from_hex(hex_string, size, &plutus_data);

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

  // Once done, ensure to clean up and release the plutus data
  cardano_plutus_data_unref(&plutus_data);
}

Parameters:
const char *hex

[in] A pointer to the hexadecimal string from which the plutus data will be created. The hexadecimal string must not be NULL.

size_t size

[in] The size of the hexadecimal string.

cardano_plutus_data_t **plutus_data

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

Returns:

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


cardano_error_t cardano_plutus_data_from_cbor(cardano_cbor_reader_t *reader, cardano_plutus_data_t **plutus_data)

Creates a plutus_data from a CBOR reader.

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

Usage Example:

cardano_cbor_reader_t* reader = cardano_cbor_reader_new(cbor_data, data_size);
cardano_plutus_data_t* plutus_data = NULL;

cardano_error_t result = cardano_plutus_data_from_cbor(reader, &plutus_data);

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

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

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

Remark

In Cardano, entities are encoded in CBOR, but CBOR allows multiple valid ways to encode the same data. The Cardano blockchain does not enforce a canonical CBOR representation, meaning that if you decode a transaction from CBOR and then re-encode it, the resulting encoding could be different. This would change the data and invalidate any existing signatures. To prevent this, when a plutus data object is created using cardano_plutus_data_from_cbor, it caches the original CBOR representation internally. When cardano_plutus_data_to_cbor is called, it will output the cached CBOR. If the cached CBOR representation is not needed, the client can call cardano_plutus_data_clear_cbor_cache after the object has been created.

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_data_t object by calling cardano_plutus_data_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_data data.

cardano_plutus_data_t **plutus_data

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

Returns:

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


cardano_error_t cardano_plutus_data_to_cbor(const cardano_plutus_data_t *plutus_data, cardano_cbor_writer_t *writer)

Serializes a plutus_data into CBOR format using a CBOR writer.

This function serializes the given cardano_plutus_data_t object using a cardano_cbor_writer_t.

Usage Example:

cardano_plutus_data_t* plutus_data = ...;
cardano_cbor_writer_t* writer = cardano_cbor_writer_new();

if (writer)
{
  cardano_error_t result = cardano_plutus_data_to_cbor(plutus_data, 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_data_unref(&plutus_data);

Remark

In Cardano, entities are encoded in CBOR, but CBOR allows multiple valid ways to encode the same data. The Cardano blockchain does not enforce a canonical CBOR representation, meaning that if you decode a transaction from CBOR and then re-encode it, the resulting encoding could be different. This would change the data and invalidate any existing signatures. To prevent this, when a plutus data object is created using cardano_plutus_data_from_cbor, it caches the original CBOR representation internally. When cardano_plutus_data_to_cbor is called, it will output the cached CBOR. If the cached CBOR representation is not needed, the client can call cardano_plutus_data_clear_cbor_cache after the object has been created.

Parameters:
const cardano_plutus_data_t *plutus_data

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


cardano_error_t cardano_plutus_data_to_cip116_json(const cardano_plutus_data_t *data, cardano_json_writer_t *writer)

Serializes a Plutus data object to CIP-116 JSON.

This function recursively serializes the Plutus data structure according to the schema definitions (constr, map, list, integer, bytes).

Parameters:
const cardano_plutus_data_t *data

[in] Pointer to a valid cardano_plutus_data_t.

cardano_json_writer_t *writer

[in] Pointer to a valid cardano_json_writer_t.

Returns:

CARDANO_SUCCESS On success. CARDANO_ERROR_POINTER_IS_NULL If data or writer is NULL. CARDANO_ERROR_MEMORY_ALLOCATION_FAILED If memory allocation fails. CARDANO_ERROR_INVALID_ARGUMENT If the data type is unknown. Other Any error propagated from nested writers.


cardano_error_t cardano_plutus_data_get_kind(const cardano_plutus_data_t *plutus_data, cardano_plutus_data_kind_t *kind)

Retrieves the kind of the plutus_data.

This function retrieves the kind of a given cardano_plutus_data_t object and stores it in the provided output parameter. The plutus data kind is defined in the cardano_plutus_data_kind_t enumeration, which specifies whether the plutus data represents an integer, byte array, or other types.

Usage Example:

cardano_plutus_data_t* plutus_data = cardano_plutus_data_new_bytes_from_hex(...);
cardano_plutus_data_kind_t kind;
cardano_error_t result = cardano_plutus_data_get_kind(plutus_data, &kind);

if (result == CARDANO_SUCCESS)
{
  if (kind == CARDANO_PLUTUS_DATA_KIND_INTEGER)
  {
    // Handle integer type plutus_data
  }
  else if (kind == CARDANO_PLUTUS_DATA_KIND_BYTES)
  {
    // Handle byte array type plutus_data
  }
  // Add more cases as needed for other kinds
}

// Clean up the plutus_data object once done
cardano_plutus_data_unref(&plutus_data);

Parameters:
const cardano_plutus_data_t *plutus_data

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

cardano_plutus_data_kind_t *kind

[out] Pointer to a variable where the plutus_data kind will be stored. This variable will be set to the value from the cardano_plutus_data_kind_t enumeration.

Returns:

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


cardano_error_t cardano_plutus_data_to_constr(cardano_plutus_data_t *plutus_data, cardano_constr_plutus_data_t **constr)

Converts a plutus data object to a constr plutus data object.

This function converts a cardano_plutus_data_t object to a cardano_constr_plutus_data_t object. It essentially creates a constr_plutus_data object from the given plutus_data, allowing the conversion between different types of plutus data. It returns an error code to indicate the success or failure of the operation.

Usage Example:

cardano_plutus_data_t* plutus_data = ...; // Assume plutus_data is a valid constr plutus data object
cardano_constr_plutus_data_t* constr = NULL;
cardano_error_t result = cardano_plutus_data_to_constr(plutus_data, &constr);

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

  // Once done, ensure to clean up and release the constr_plutus_data
  cardano_constr_plutus_data_unref(&constr);
}

// Clean up the plutus_data object once done
cardano_plutus_data_unref(&plutus_data);

Parameters:
cardano_plutus_data_t *plutus_data

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

cardano_constr_plutus_data_t **constr

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

Returns:

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


cardano_error_t cardano_plutus_data_to_map(cardano_plutus_data_t *plutus_data, cardano_plutus_map_t **map)

Converts a plutus data object to a plutus map object.

This function converts a cardano_plutus_data_t object to a cardano_plutus_map_t object. It essentially creates a plutus map object from the given plutus data, allowing the conversion between different types of plutus data. It returns an error code to indicate the success or failure of the operation.

Usage Example:

cardano_plutus_data_t* plutus_data = ...; // Assume plutus_data is a valid plutus map object
cardano_plutus_map_t* map = NULL;
cardano_error_t result = cardano_plutus_data_to_map(plutus_data, &map);

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

  // Once done, ensure to clean up and release the plutus_map
  cardano_plutus_map_unref(&map);
}

// Clean up the plutus_data object once done
cardano_plutus_data_unref(&plutus_data);

Parameters:
cardano_plutus_data_t *plutus_data

[in] A constant pointer to the cardano_plutus_data_t object to be converted. The object must not be NULL.

cardano_plutus_map_t **map

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

Returns:

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


cardano_error_t cardano_plutus_data_to_list(cardano_plutus_data_t *plutus_data, cardano_plutus_list_t **list)

Converts a plutus data object to a plutus list object.

This function converts a cardano_plutus_data_t object to a cardano_plutus_list_t object. It essentially creates a plutus list object from the given plutus data, allowing the conversion between different types of plutus data. It returns an error code to indicate the success or failure of the operation.

Usage Example:

cardano_plutus_data_t* plutus_data = ...; // Assume plutus_data is a valid plutus list object
cardano_plutus_list_t* list = NULL;
cardano_error_t result = cardano_plutus_data_to_list(plutus_data, &list);

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

  // Once done, ensure to clean up and release the plutus_list
  cardano_plutus_list_unref(&list);
}

// Clean up the plutus_data object once done
cardano_plutus_data_unref(&plutus_data);

Parameters:
cardano_plutus_data_t *plutus_data

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

cardano_plutus_list_t **list

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

Returns:

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


cardano_error_t cardano_plutus_data_to_integer(const cardano_plutus_data_t *plutus_data, cardano_bigint_t **integer)

Converts a plutus data object to a bigint object.

This function converts a cardano_plutus_data_t object to a cardano_bigint_t object.

Usage Example:

cardano_plutus_data_t* plutus_data = cardano_plutus_data_new_integer_from_string("987456321478523698745");
cardano_bigint_t* bigint = NULL;
cardano_error_t result = cardano_plutus_data_to_integer(plutus_data, &bigint);

if (result == CARDANO_SUCCESS)
{
  // Use the bigint object
  const size_t size = cardano_bigint_get_string_size(bigint, 10);
  char* buffer = (char*)malloc(size);

  if (cardano_bigint_to_string(bigint, buffer, size, 10) == CARDANO_SUCCESS)
  {
    printf("Extracted bigint value: %s\n", buffer);
  }
}

// Clean up
cardano_plutus_data_unref(&plutus_data);
cardano_bigint_unref(&bigint);

Parameters:
const cardano_plutus_data_t *plutus_data

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

cardano_bigint_t **integer

[out] On successful conversion, this will point to a newly created cardano_bigint_t object representing the extracted integer value. The caller is responsible for managing the memory of this object, including releasing it when it is no longer needed.

Returns:

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


cardano_error_t cardano_plutus_data_to_bounded_bytes(cardano_plutus_data_t *plutus_data, cardano_buffer_t **bounded_bytes)

Converts a plutus data object to bounded bytes.

This function converts a cardano_plutus_data_t object to bounded bytes. It extracts the bytes from the given plutus data, allowing the conversion between different types of plutus data. The resulting bytes are stored in a bounded bytes buffer. It returns an error code to indicate the success or failure of the operation.

Usage Example:

cardano_plutus_data_t* plutus_data = cardano_plutus_data_new_bytes_from_hex("DEADBEEF", 8);
cardano_buffer_t* bounded_bytes = NULL;
cardano_error_t result = cardano_plutus_data_to_bounded_bytes(plutus_data, &bounded_bytes);

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

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

  printf("\n");

  // Once done, ensure to clean up and release the bounded bytes
  cardano_buffer_unref(&bounded_bytes);
}

// Clean up the plutus_data object once done
cardano_plutus_data_unref(&plutus_data);

Parameters:
cardano_plutus_data_t *plutus_data

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

cardano_buffer_t **bounded_bytes

[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 bounded 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.


bool cardano_plutus_data_equals(const cardano_plutus_data_t *lhs, const cardano_plutus_data_t *rhs)

Checks if two plutus_data objects are equal.

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

Usage Example:

cardano_plutus_data_t* plutus_data1 = cardano_plutus_data_new_bytes_from_hex("DEADBEEF", 8);
cardano_plutus_data_t* plutus_data2 = cardano_plutus_data_new_bytes_from_hex("DEADBEEF", 8);

if (cardano_plutus_data_equals(plutus_data1, plutus_data2))
{
  printf("plutus_data1 is equal to plutus_data2\n");
}
else
{
  printf("plutus_data1 is not equal to plutus_data2\n");
}

// Clean up the plutus_data objects once done
cardano_plutus_data_unref(&plutus_data1);
cardano_plutus_data_unref(&plutus_data2);

Parameters:
const cardano_plutus_data_t *lhs

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

const cardano_plutus_data_t *rhs

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

Returns:

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


void cardano_plutus_data_clear_cbor_cache(cardano_plutus_data_t *plutus_data)

Clears the cached CBOR representation from a plutus data.

This function removes the internally cached CBOR data from a cardano_plutus_data_t object. It is useful when you have modified the plutus_data after it was created from CBOR using cardano_plutus_data_from_cbor and you want to ensure that the next serialization reflects the current state of the plutus_data, rather than using the original cached CBOR.

Usage Example:

// Assume plutus_data was created using cardano_plutus_data_from_cbor
cardano_plutus_data_t* plutus_data = ...;

// Modify the plutus_data as needed
// For example, change the fee

if (result != CARDANO_SUCCESS)
{
  printf("Failed to set new fee: %s\n", cardano_error_to_string(result));
}

// Clear the CBOR cache to ensure serialization uses the updated plutus_data
cardano_plutus_data_clear_cbor_cache(plutus_data);

// Serialize the plutus_data to CBOR
cardano_cbor_writer_t* writer = cardano_cbor_writer_new();

result = cardano_plutus_data_to_cbor(plutus_data, writer);

if (result == CARDANO_SUCCESS)
{
  // Process the CBOR data as needed
}
else
{
  const char* error_message = cardano_cbor_writer_get_last_error(writer);
  printf("Serialization failed: %s\n", error_message);
}

// Clean up resources
cardano_cbor_writer_unref(&writer);
cardano_plutus_data_unref(&plutus_data);

Warning

Clearing the CBOR cache may change the binary representation of the plutus data when serialized, which can alter the plutus data and invalidate any existing signatures. Use this function with caution, especially if the transaction has already been signed or if preserving the exact CBOR encoding is important for your application.

Parameters:
cardano_plutus_data_t *plutus_data

[inout] A pointer to an initialized cardano_plutus_data_t object from which the CBOR cache will be cleared.


void cardano_plutus_data_unref(cardano_plutus_data_t **plutus_data)

Decrements the reference count of a plutus_data object.

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

Usage Example:

cardano_plutus_data_t* plutus_data = cardano_plutus_data_new();

// Perform operations with the plutus_data...

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

Note

After calling cardano_plutus_data_unref, the pointer to the cardano_plutus_data_t object will be set to NULL to prevent its reuse.

Parameters:
cardano_plutus_data_t **plutus_data

[inout] A pointer to the pointer of the plutus_data 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_data_ref(cardano_plutus_data_t *plutus_data)

Increases the reference count of the cardano_plutus_data_t object.

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

Usage Example:

// Assuming plutus_data is a previously created plutus_data object

cardano_plutus_data_ref(plutus_data);

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

Note

Always ensure that for every call to cardano_plutus_data_ref there is a corresponding call to cardano_plutus_data_unref to prevent memory leaks.

Parameters:
cardano_plutus_data_t *plutus_data

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


size_t cardano_plutus_data_refcount(const cardano_plutus_data_t *plutus_data)

Retrieves the current reference count of the cardano_plutus_data_t object.

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

Usage Example:

// Assuming plutus_data is a previously created plutus_data object

size_t ref_count = cardano_plutus_data_refcount(plutus_data);

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_data_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_data_t *plutus_data

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

Returns:

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


void cardano_plutus_data_set_last_error(cardano_plutus_data_t *plutus_data, const char *message)

Sets the last error message for a given plutus_data object.

Records an error message in the plutus_data’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_data_t *plutus_data

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


const char *cardano_plutus_data_get_last_error(const cardano_plutus_data_t *plutus_data)

Retrieves the last error message recorded for a specific plutus_data.

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

Parameters:
const cardano_plutus_data_t *plutus_data

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

Returns:

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