Asset Name

typedef struct cardano_asset_name_t cardano_asset_name_t

Represents an asset name.


cardano_error_t cardano_asset_name_from_bytes(const byte_t *data, size_t size, cardano_asset_name_t **asset_name)

Creates and initializes a new instance of a Cardano asset name from byte data.

This function allocates and initializes a new instance of a cardano_asset_name_t object, which represents the name of a Cardano native asset. Native asset names in Cardano are arbitrary byte strings, typically used to uniquely identify assets within a policy.

Usage Example:

const byte_t* name_data = (const byte_t*)"example_asset";
size_t name_size = strlen("example_asset");
cardano_asset_name_t* asset_name = NULL;

cardano_error_t result = cardano_asset_name_from_bytes(name_data, name_size, &asset_name);
if (result == CARDANO_SUCCESS)
{
  // Use the asset_name
  // Once done, ensure to clean up and release the asset_name
  cardano_asset_name_unref(&asset_name);
}
else
{
  printf("Failed to create asset name: %s\n", cardano_error_to_string(result));
}

Parameters:
const byte_t *data

[in] A pointer to an array of bytes representing the asset name. This array should contain the raw byte data of the asset name and must not be NULL.

size_t size

[in] The size of the byte array. This value specifies how many bytes from the data array should be used for the asset name. The size must not exceed the maximum allowed length for asset names in Cardano.

cardano_asset_name_t **asset_name

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

Returns:

cardano_error_t indicating the outcome of the operation. Returns CARDANO_SUCCESS if the asset name was successfully created from the provided bytes, or an appropriate error code indicating the failure reason, such as CARDANO_ERROR_POINTER_IS_NULL if the input data pointer is NULL, or CARDANO_ERROR_INVALID_ARGUMENT if the size exceeds the maximum length or other constraints.


cardano_error_t cardano_asset_name_from_hex(const char *hex, size_t size, cardano_asset_name_t **asset_name)

Creates and initializes a new instance of a Cardano asset name from a hexadecimal string.

This function allocates and initializes a new instance of a cardano_asset_name_t object, representing an asset name using a hexadecimal string. Asset names in Cardano are arbitrary byte strings, and this function specifically interprets the input as a hex-encoded representation of those bytes.

Usage Example:

const char* hex_string = "6578616d706c65"; // Hex for "example"
size_t hex_size = strlen(hex_string); // Should be an even number
cardano_asset_name_t* asset_name = NULL;

cardano_error_t result = cardano_asset_name_from_hex(hex_string, hex_size, &asset_name);
if (result == CARDANO_SUCCESS)
{
  // Use the asset_name
  // Once done, ensure to clean up and release the asset_name
  cardano_asset_name_unref(&asset_name);
}
else
{
  printf("Failed to create asset name from hex: %s\n", cardano_error_to_string(result));
}

Parameters:
const char *hex

[in] A pointer to a null-terminated string containing the hexadecimal representation of the asset name. The string must not be NULL, must be properly hex-encoded, and must not contain non-hex characters.

size_t size

[in] The number of characters in the hex string. This value must be even, as each pair of characters represents one byte.

cardano_asset_name_t **asset_name

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

Returns:

cardano_error_t indicating the outcome of the operation. Returns CARDANO_SUCCESS if the asset name was successfully created from the provided hex string, or an appropriate error code indicating the failure reason, such as CARDANO_ERROR_POINTER_IS_NULL if the input hex pointer is NULL, or CARDANO_ERROR_INVALID_ARGUMENT if the size is incorrect or the hex string is not valid.


cardano_error_t cardano_asset_name_from_string(const char *string, size_t size, cardano_asset_name_t **asset_name)

Creates and initializes a new instance of a Cardano asset name from a UTF-8 string.

This function allocates and initializes a new instance of a cardano_asset_name_t object, which represents the name of a Cardano native asset. Asset names in Cardano are arbitrary byte strings, but this function specifically interprets the input as a UTF-8 encoded string, allowing for easier creation of asset names from human-readable text.

Usage Example:

const char* name_string = "example_asset";
size_t name_length = strlen(name_string); // Not including the null terminator
cardano_asset_name_t* asset_name = NULL;

cardano_error_t result = cardano_asset_name_from_string(name_string, name_length, &asset_name);
if (result == CARDANO_SUCCESS)
{
  // Use the asset_name
  // Once done, ensure to clean up and release the asset_name
  cardano_asset_name_unref(&asset_name);
}
else
{
  printf("Failed to create asset name: %s\n", cardano_error_to_string(result));
}

Parameters:
const char *string

[in] A pointer to a null-terminated UTF-8 encoded string representing the asset name. This string must not be NULL and should be properly encoded in UTF-8.

size_t size

[in] The number of bytes in the string, not including the null terminator. This allows for strings containing null bytes to be handled correctly.

cardano_asset_name_t **asset_name

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

Returns:

cardano_error_t indicating the outcome of the operation. Returns CARDANO_SUCCESS if the asset name was successfully created from the provided string, or an appropriate error code indicating the failure reason, such as CARDANO_ERROR_POINTER_IS_NULL if the input string pointer is NULL, or CARDANO_ERROR_INVALID_ARGUMENT if the size is incorrect or other constraints are not met.


cardano_error_t cardano_asset_name_from_cbor(cardano_cbor_reader_t *reader, cardano_asset_name_t **asset_name)

Creates a cardano_asset_name_t from a CBOR reader.

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

Usage Example:

cardano_cbor_reader_t* reader = cardano_cbor_reader_new(cbor_data, data_size);
cardano_asset_name_t* asset_name = NULL;

cardano_error_t result = cardano_asset_name_from_cbor(reader, &asset_name);

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

  // Once done, ensure to clean up and release the asset_name
  cardano_asset_name_unref(&asset_name);
}
else
{
  const char* error = cardano_cbor_reader_get_last_error(reader);
  printf("Failed to decode asset_name: %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_asset_name_t object by calling cardano_asset_name_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 data.

cardano_asset_name_t **asset_name

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

Returns:

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


cardano_error_t cardano_asset_name_to_cip116_json(const cardano_asset_name_t *asset_name, cardano_json_writer_t *writer)

Serializes an asset name to its CIP-116 JSON string representation.

The function writes the hexadecimal representation of the asset name as a JSON string (e.g., “4d794173736574”).

Parameters:
const cardano_asset_name_t *asset_name

[in] Pointer to a valid cardano_asset_name_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 asset_name or writer is NULL. CARDANO_ERROR_MEMORY_ALLOCATION_FAILED If memory allocation fails. Other Any error propagated from nested writers.


cardano_error_t cardano_asset_name_to_cbor(const cardano_asset_name_t *asset_name, cardano_cbor_writer_t *writer)

Serializes asset name into CBOR format using a CBOR writer.

This function serializes the given cardano_asset_name_t object using a cardano_cbor_writer_t.

Usage Example:

cardano_asset_name_t* asset_name = ...;
cardano_cbor_writer_t* writer = cardano_cbor_writer_new();

if (writer)
{
  cardano_error_t result = cardano_asset_name_to_cbor(asset_name, 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_asset_name_unref(&asset_name);

Parameters:
const cardano_asset_name_t *asset_name

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


const char *cardano_asset_name_get_string(const cardano_asset_name_t *asset_name)

Retrieves the string representation of a Cardano asset name.

This function provides access to the string representation of a cardano_asset_name_t object. The asset name is returned as a UTF-8 encoded null-terminated string. This string points to an internal buffer of the cardano_asset_name_t object and must not be modified or freed by the caller. The string remains valid as long as the asset name object is not modified or freed.

Usage Example:

cardano_asset_name_t* asset_name = ...; // Assume asset_name is already initialized
const char* name_str = cardano_asset_name_get_string(asset_name);

if (name_str != NULL)
{
  printf("Asset Name: %s\n", name_str);
}
else
{
  printf("Failed to retrieve the asset name string.\n");
}
// No need to free name_str, it is managed internally

Parameters:
const cardano_asset_name_t *asset_name

[in] A pointer an initialized cardano_asset_name_t object. The pointer must not be NULL and should point to a valid asset name object.

Returns:

A pointer to a null-terminated UTF-8 string representing the asset name. If the input is NULL or an error occurs, NULL is returned instead. The caller must not free this string as it is managed internally by the asset name object.


size_t cardano_asset_name_get_string_size(const cardano_asset_name_t *asset_name)

Retrieves the size of the string representation of a Cardano asset name, including the null terminator.

This function calculates the length of the string representation of a cardano_asset_name_t object, including the null terminator. This is useful for understanding the buffer size required to store the string representation of the asset name.

Usage Example:

cardano_asset_name_t* asset_name = ...; // Assume asset_name is already initialized
size_t name_size = cardano_asset_name_get_string_size(asset_name);

if (name_size > 0)
{
  printf("Size of the asset name string including null terminator: %zu\n", name_size);
}
else
{
  printf("Failed to retrieve the size of the asset name string.\n");
}

Parameters:
const cardano_asset_name_t *asset_name

[in] A pointer to an initialized cardano_asset_name_t object. The pointer must not be NULL and should point to a valid asset name object.

Returns:

The size of the string in bytes, including the null terminator. If the input is NULL or an error occurs, the function returns 0.


const byte_t *cardano_asset_name_get_bytes(const cardano_asset_name_t *asset_name)

Retrieves the byte representation of a Cardano asset name.

This function provides access to the underlying byte array of a cardano_asset_name_t object. The byte array represents the asset name as it is stored and used within the Cardano ecosystem.

Usage Example:

cardano_asset_name_t* asset_name = ...; // Assume asset_name is already initialized
const byte_t* name_bytes = cardano_asset_name_get_bytes(asset_name);

if (name_bytes != NULL)
{
  // Process the bytes of the asset name
  // Note: The length of the byte array should be known or determined separately
}
else
{
  printf("Failed to retrieve the bytes of the asset name.\n");
}

Parameters:
const cardano_asset_name_t *asset_name

[in] A pointer of an initialized cardano_asset_name_t object. The pointer must not be NULL and should point to a valid asset name object.

Returns:

A pointer to the constant byte array representing the asset name. This pointer points to an internal buffer and must not be modified or freed by the caller. If the input is NULL or an error occurs, the function returns NULL.


size_t cardano_asset_name_get_bytes_size(const cardano_asset_name_t *asset_name)

Retrieves the size of the byte array for a Cardano asset name.

This function returns the size of the byte array representing the asset name contained within a cardano_asset_name_t object.

Usage Example:

cardano_asset_name_t* asset_name = ...; // Assume asset_name is already initialized
size_t name_size = cardano_asset_name_get_bytes_size(asset_name);

if (name_size > 0)
{
  const byte_t* name_bytes = cardano_asset_name_get_bytes(asset_name);
  // Process the bytes knowing the exact size of the asset name
}
else
{
  printf("Failed to retrieve the size of the asset name or asset name is empty.\n");
}

Parameters:
const cardano_asset_name_t *asset_name

[in] A pointer of an initialized cardano_asset_name_t object. The pointer must not be NULL and should point to a valid asset name object.

Returns:

The size of the byte array representing the asset name. If the input is NULL or an error occurs, the function returns 0. This size does not include a null terminator, as asset names can contain arbitrary binary data.


const char *cardano_asset_name_get_hex(const cardano_asset_name_t *asset_name)

Retrieves the hexadecimal string representation of a Cardano asset name.

This function returns the hexadecimal string representation of a cardano_asset_name_t object. The string encodes the asset name, which consists of the concatenated bytes of the policy ID and asset name.

Usage Example:

const cardano_asset_name_t* asset_name = ...; // Assume asset_name is already initialized
const char* hex_string = cardano_asset_name_get_hex(asset_name);

if (hex_string != NULL)
{
  // Process the hexadecimal string of the asset name
  printf("Asset ID in hex: %s\n", hex_string);
}
else
{
  printf("Failed to retrieve the hexadecimal string of the asset name.\n");
}

Parameters:
const cardano_asset_name_t *asset_name

[in] A pointer to an initialized cardano_asset_name_t object. The pointer must not be NULL and should point to a valid asset name object.

Returns:

A pointer to a null-terminated string representing the asset name in hexadecimal format. This string is owned by the cardano_asset_name_t object and must not be modified or freed by the caller. If the input is NULL or an error occurs, the function returns NULL.


size_t cardano_asset_name_get_hex_size(const cardano_asset_name_t *asset_name)

Retrieves the size of the hexadecimal string for a Cardano asset name.

This function returns the size of the hexadecimal string representation of a cardano_asset_name_t object. The size includes the number of characters in the hexadecimal string, not including the null terminator.

Usage Example:

const cardano_asset_name_t* asset_name = ...; // Assume asset_name is already initialized
size_t hex_size = cardano_asset_name_get_hex_size(asset_name);

if (hex_size > 0)
{
  const char* hex_string = cardano_asset_name_get_hex(asset_name);
  // Process the hex string knowing the exact size of the asset name
  printf("Hexadecimal string size: %zu\n", hex_size);
}
else
{
  printf("Failed to retrieve the size of the asset name in hex or asset name is empty.\n");
}

Parameters:
const cardano_asset_name_t *asset_name

[in] A pointer to an initialized cardano_asset_name_t object. The pointer must not be NULL and should point to a valid asset name object.

Returns:

The size of the hexadecimal string representing the asset name. If the input is NULL or an error occurs, the function returns 0. The size does not include a null terminator.


void cardano_asset_name_unref(cardano_asset_name_t **asset_name)

Decrements the reference count of a cardano_asset_name_t object.

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

Usage Example:

cardano_asset_name_t* asset_name = cardano_asset_name_new(major, minor);

// Perform operations with the asset_name...

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

Note

After calling cardano_asset_name_unref, the pointer to the cardano_asset_name_t object will be set to NULL to prevent its reuse.

Parameters:
cardano_asset_name_t **asset_name

[inout] A pointer to the pointer of the asset_name 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_asset_name_ref(cardano_asset_name_t *asset_name)

Increases the reference count of the cardano_asset_name_t object.

This function is used to manually increment the reference count of an cardano_asset_name_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_asset_name_unref.

Usage Example:

// Assuming asset_name is a previously created asset_name object

cardano_asset_name_ref(asset_name);

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

Note

Always ensure that for every call to cardano_asset_name_ref there is a corresponding call to cardano_asset_name_unref to prevent memory leaks.

Parameters:
cardano_asset_name_t *asset_name

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


size_t cardano_asset_name_refcount(const cardano_asset_name_t *asset_name)

Retrieves the current reference count of the cardano_asset_name_t object.

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

Usage Example:

// Assuming asset_name is a previously created asset_name object

size_t ref_count = cardano_asset_name_refcount(asset_name);

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_asset_name_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_asset_name_t *asset_name

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

Returns:

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


void cardano_asset_name_set_last_error(cardano_asset_name_t *asset_name, const char *message)

Sets the last error message for a given cardano_asset_name_t object.

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

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


const char *cardano_asset_name_get_last_error(const cardano_asset_name_t *asset_name)

Retrieves the last error message recorded for a specific asset_name.

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

Parameters:
const cardano_asset_name_t *asset_name

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

Returns:

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