Constr Plutus Data

typedef struct cardano_constr_plutus_data_t cardano_constr_plutus_data_t

The main datatype Constr represents the nth constructor along with its arguments.

Remark: We don’t directly serialize the alternative in the tag, instead the scheme is:

  • Alternatives 0-6 -> tags 121-127, followed by the arguments in a list.

  • Alternatives 7-127 -> tags 1280-1400, followed by the arguments in a list.

  • Any alternatives, including those that don’t fit in the above -> tag 102 followed by a list containing an unsigned integer for the actual alternative, and then the arguments in a (nested!) list.


cardano_error_t cardano_constr_plutus_data_new(uint64_t alternative, cardano_plutus_list_t *data, cardano_constr_plutus_data_t **constr_plutus_data)

Creates and initializes a new instance of a plutus data constructor.

This function allocates and initializes a new instance of cardano_constr_plutus_data_t, using the provided Constr alternative number and list of arguments as a PlutusList. It returns an error code to indicate success or failure of the operation.

Usage Example:

uint64_t alternative = 1; // Example alternative number
cardano_plutus_list_t* data = { ... }; // Assume data is initialized here
cardano_constr_plutus_data_t* constr_plutus_data = NULL;

// Attempt to create a new constr_plutus_data
cardano_error_t result = cardano_constr_plutus_data_new(alternative, data, &constr_plutus_data);

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_plutus_data);
}

cardano_plutus_list_unref(&data);

Parameters:
uint64_t alternative

[in] The Constr alternative number, representing the nth constructor of a ‘Sum Type’.

cardano_plutus_list_t *data

[in] A pointer to a cardano_plutus_list_t object representing the list of arguments of the ‘Sum Type’ as a ‘PlutusList’.

cardano_constr_plutus_data_t **constr_plutus_data

[out] On successful initialization, 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 constr_plutus_data was successfully created, or an appropriate error code indicating the failure reason.


cardano_error_t cardano_constr_plutus_data_from_cbor(cardano_cbor_reader_t *reader, cardano_constr_plutus_data_t **constr_plutus_data)

Creates a cardano_cbor_reader_t from a CBOR reader.

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

Usage Example:

cardano_cbor_reader_t* reader = cardano_cbor_reader_new(cbor_data, data_size);
cardano_constr_plutus_data_t* constr_plutus_data = NULL;

cardano_error_t result = cardano_constr_plutus_data_from_cbor(reader, &constr_plutus_data);

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_plutus_data);
}
else
{
  const char* error = cardano_cbor_reader_get_last_error(reader);
  printf("Failed to decode constr_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_constr_plutus_data_from_cbor, it caches the original CBOR representation internally. When cardano_constr_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_constr_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_constr_plutus_data_t object by calling cardano_constr_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 constr_plutus_data data.

cardano_constr_plutus_data_t **constr_plutus_data

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

Returns:

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


cardano_error_t cardano_constr_plutus_data_to_cbor(const cardano_constr_plutus_data_t *constr_plutus_data, cardano_cbor_writer_t *writer)

Serializes a cardano_cbor_reader_t into CBOR format using a CBOR writer.

This function serializes the given cardano_constr_plutus_data_t object using a cardano_cbor_writer_t.

Usage Example:

cardano_constr_plutus_data_t* constr_plutus_data = ...;
cardano_cbor_writer_t* writer = cardano_cbor_writer_new();

if (writer)
{
  cardano_error_t result = cardano_constr_plutus_data_to_cbor(constr_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_constr_plutus_data_unref(&constr_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_constr_plutus_data_from_cbor, it caches the original CBOR representation internally. When cardano_constr_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_constr_plutus_data_clear_cbor_cache after the object has been created.

Parameters:
const cardano_constr_plutus_data_t *constr_plutus_data

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


cardano_error_t cardano_constr_plutus_data_to_cip116_json(const cardano_constr_plutus_data_t *constr_data, cardano_json_writer_t *writer)

Serializes a Plutus constructor data object to CIP-116 JSON.

This function serializes the Plutus constructor data structure, including its tag, alternative, and data list.

Parameters:
const cardano_constr_plutus_data_t *constr_data

[in] Pointer to a valid cardano_constr_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 constr_data or writer is NULL. Other Any error propagated from nested writers.


cardano_error_t cardano_constr_plutus_data_get_data(cardano_constr_plutus_data_t *constr_plutus_data, cardano_plutus_list_t **data)

Retrieves the data associated with a cardano_cbor_reader_t.

This function provides access to the data part of a cardano_constr_plutus_data_t object. It returns a new reference to a cardano_plutus_list_t object representing the data. This allows the data to be used independently of the original cardano_cbor_reader_t object. The reference count of the data object is increased by one, making it the caller’s responsibility to release it by calling cardano_plutus_list_unref when it is no longer needed.

Usage Example:

cardano_constr_plutus_data_t* original_constr_plutus_data = cardano_constr_plutus_data_new(...);
cardano_plutus_list_t* data_constr_plutus_data = NULL;

// Attempt to retrieve the data associated with the constr_plutus_data
cardano_error_t result = cardano_constr_plutus_data_get_data(original_constr_plutus_data, &data_constr_plutus_data);

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

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

// Release the original constr_plutus_data after use
cardano_constr_plutus_data_unref(&original_constr_plutus_data);

Parameters:
cardano_constr_plutus_data_t *constr_plutus_data

[in] A constant pointer to the cardano_constr_plutus_data_t object from which the data is to be retrieved.

cardano_plutus_list_t **data

[out] A pointer to store the reference to the cardano_plutus_list_t object containing the data. If the input constr_plutus_data is NULL, returns NULL. The caller is responsible for managing the lifecycle of this object, including releasing it with cardano_plutus_list_unref.

Returns:

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


cardano_error_t cardano_constr_plutus_data_set_data(cardano_constr_plutus_data_t *constr_plutus_data, cardano_plutus_list_t *data)

Sets the data associated with a cardano_constr_plutus_data_t.

This function sets the data part of a cardano_constr_plutus_data_t object to the provided cardano_plutus_list_t object. It replaces any existing data associated with the cardano_constr_plutus_data_t. The reference count of the provided data object is increased by one, making it the caller’s responsibility to release it when it is no longer needed.

Usage Example:

cardano_constr_plutus_data_t* original_constr_plutus_data = cardano_constr_plutus_data_new(...);
cardano_plutus_list_t* new_data = { ... }; // Assume new_data is initialized here

// Attempt to set the new data associated with the constr_plutus_data
cardano_error_t result = cardano_constr_plutus_data_set_data(original_constr_plutus_data, new_data);

if (result == CARDANO_SUCCESS)
{
  // Data successfully set
  printf("Data set successfully.\n");
}
else
{
  // Handle error
  fprintf(stderr, "Failed to set data: %s.\n", cardano_error_to_string(result));
}

// Release the original constr_plutus_data after use
cardano_constr_plutus_data_unref(&original_constr_plutus_data);

Parameters:
cardano_constr_plutus_data_t *constr_plutus_data

[in] A constant pointer to the cardano_constr_plutus_data_t object to which the data is to be set.

cardano_plutus_list_t *data

[in] A pointer to the cardano_plutus_list_t object containing the new data to be set.

Returns:

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


cardano_error_t cardano_constr_plutus_data_get_alternative(const cardano_constr_plutus_data_t *constr_plutus_data, uint64_t *alternative)

Retrieves the alternative of the cardano_constr_plutus_data_t.

This function retrieves the alternative of a given cardano_constr_plutus_data_t object and stores it in the provided output parameter. The alternative represents the nth constructor of a ‘Sum Type’.

Usage Example:

cardano_constr_plutus_data_t* constr_plutus_data = cardano_constr_plutus_data_new(...);
uint64_t alternative;
cardano_error_t result = cardano_constr_plutus_data_get_alternative(constr_plutus_data, &alternative);

if (result == CARDANO_SUCCESS)
{
  // Use the alternative number
  printf("Alternative number: %llu\n", (unsigned long long)alternative);
}
else
{
  // Handle error
  fprintf(stderr, "Failed to retrieve alternative: %s.\n", cardano_error_to_string(result));
}

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

Parameters:
const cardano_constr_plutus_data_t *constr_plutus_data

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

uint64_t *alternative

[out] Pointer to a variable where the alternative will be stored. This variable will be set to the alternative number.

Returns:

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


cardano_error_t cardano_constr_plutus_data_set_alternative(cardano_constr_plutus_data_t *constr_plutus_data, uint64_t alternative)

Sets the alternative of the cardano_constr_plutus_data_t.

This function sets the alternative of a given cardano_constr_plutus_data_t object to the provided alternative number.

Usage Example:

cardano_constr_plutus_data_t* constr_plutus_data = cardano_constr_plutus_data_new(...);
uint64_t new_alternative = 2; // Example alternative number

cardano_error_t result = cardano_constr_plutus_data_set_alternative(constr_plutus_data, new_alternative);

if (result == CARDANO_SUCCESS)
{
  // Alternative successfully set
  printf("Alternative set successfully.\n");
}
else
{
  // Handle error
  fprintf(stderr, "Failed to set alternative: %s.\n", cardano_error_to_string(result));
}

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

Parameters:
cardano_constr_plutus_data_t *constr_plutus_data

[in] A constant pointer to the cardano_constr_plutus_data_t object to which the alternative is to be set.

uint64_t alternative

[in] The alternative number to set.

Returns:

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


bool cardano_constr_plutus_equals(const cardano_constr_plutus_data_t *lhs, const cardano_constr_plutus_data_t *rhs)

Checks whether two cardano_constr_plutus_data_t objects are equal.

This function compares two cardano_constr_plutus_data_t objects, lhs and rhs, to determine if they represent the same data. It returns true if the contents of the two objects are equal, and false otherwise.

Usage Example:

cardano_constr_plutus_data_t* data1 = cardano_constr_plutus_data_new(...);
cardano_constr_plutus_data_t* data2 = cardano_constr_plutus_data_new(...);

bool are_equal = cardano_constr_plutus_equals(data1, data2);

if (are_equal)
{
  // The two constr_plutus_data objects are equal
  printf("The two constr_plutus_data objects are equal.\n");
}
else
{
  // The two constr_plutus_data objects are not equal
  printf("The two constr_plutus_data objects are not equal.\n");
}

// Release the constr_plutus_data objects after use
cardano_constr_plutus_data_unref(&data1);
cardano_constr_plutus_data_unref(&data2);

Parameters:
const cardano_constr_plutus_data_t *lhs

[in] A constant pointer to the first cardano_constr_plutus_data_t object to compare.

const cardano_constr_plutus_data_t *rhs

[in] A constant pointer to the second cardano_constr_plutus_data_t object to compare.

Returns:

True if the contents of the two constr_plutus_data objects are equal, false otherwise.


void cardano_constr_plutus_data_clear_cbor_cache(cardano_constr_plutus_data_t *constr_plutus_data)

Clears the cached CBOR representation from a constr plutus data.

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

Usage Example:

// Assume constr_plutus_data was created using cardano_constr_plutus_data_from_cbor
cardano_constr_plutus_data_t* constr_plutus_data = ...;

// Modify the constr_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 constr_plutus_data
cardano_constr_plutus_data_clear_cbor_cache(constr_plutus_data);

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

result = cardano_constr_plutus_data_to_cbor(constr_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_constr_plutus_data_unref(&constr_plutus_data);

Warning

Clearing the CBOR cache may change the binary representation of the constr plutus data when serialized, which can alter the constr 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_constr_plutus_data_t *constr_plutus_data

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


void cardano_constr_plutus_data_unref(cardano_constr_plutus_data_t **constr_plutus_data)

Decrements the reference count of a cardano_constr_plutus_data_t object.

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

Usage Example:

cardano_constr_plutus_data_t* constr_plutus_data = ...; // Assume constr_plutus_data is initialized here

// Perform operations with the constr_plutus_data...

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

Note

After calling cardano_constr_plutus_data_unref, the pointer to the cardano_constr_plutus_data_t object will be set to NULL to prevent its reuse.

Parameters:
cardano_constr_plutus_data_t **constr_plutus_data

[inout] A pointer to the pointer of the constr_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_constr_plutus_data_ref(cardano_constr_plutus_data_t *constr_plutus_data)

Increases the reference count of the cardano_constr_plutus_data_t object.

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

Usage Example:

// Assuming constr_plutus_data is a previously created constr_plutus_data object

cardano_constr_plutus_data_ref(constr_plutus_data);

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

Note

Always ensure that for every call to cardano_constr_plutus_data_ref there is a corresponding call to cardano_constr_plutus_data_unref to prevent memory leaks.

Parameters:
cardano_constr_plutus_data_t *constr_plutus_data

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


size_t cardano_constr_plutus_data_refcount(const cardano_constr_plutus_data_t *constr_plutus_data)

Retrieves the current reference count of the cardano_constr_plutus_data_t object.

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

Usage Example:

// Assuming constr_plutus_data is a previously created constr_plutus_data object

size_t ref_count = cardano_constr_plutus_data_refcount(constr_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_constr_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_constr_plutus_data_t *constr_plutus_data

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

Returns:

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


void cardano_constr_plutus_data_set_last_error(cardano_constr_plutus_data_t *constr_plutus_data, const char *message)

Sets the last error message for a given cardano_constr_plutus_data_t object.

Records an error message in the constr_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_constr_plutus_data_t *constr_plutus_data

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


const char *cardano_constr_plutus_data_get_last_error(const cardano_constr_plutus_data_t *constr_plutus_data)

Retrieves the last error message recorded for a specific cardano_constr_plutus_data_t.

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

Parameters:
const cardano_constr_plutus_data_t *constr_plutus_data

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

Returns:

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