Native Script - Require Time After

typedef struct cardano_script_invalid_before_t cardano_script_invalid_before_t

This script evaluates to true if the lower bound of the transaction validity interval is a slot number Y, and Y >= X.

This condition guarantees that the actual slot number in which the transaction is included is greater than or equal to slot number X.


cardano_error_t cardano_script_invalid_before_new(uint64_t slot, cardano_script_invalid_before_t **script_invalid_before)

Creates and initializes a new instance of a script_invalid_before.

This function allocates and initializes a new instance of cardano_script_invalid_before_t with a provided slot number. It returns an error code to indicate success or failure of the operation.

Usage Example:

uint64_t slot = 1000;
cardano_script_invalid_before_t* script_invalid_before = NULL;

// Attempt to create a new script_invalid_before
cardano_error_t result = cardano_script_invalid_before_new(slot, &script_invalid_before);

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

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

Parameters:
uint64_t slot

[in] The slot number representing the upper bound of the transaction validity interval.

cardano_script_invalid_before_t **script_invalid_before

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

Returns:

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


cardano_error_t cardano_script_invalid_before_from_cbor(cardano_cbor_reader_t *reader, cardano_script_invalid_before_t **script_invalid_before)

Creates a script_invalid_before from a CBOR reader.

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

Usage Example:

cardano_cbor_reader_t* reader = cardano_cbor_reader_new(cbor_data, data_size);
cardano_script_invalid_before_t* script_invalid_before = NULL;

cardano_error_t result = cardano_script_invalid_before_from_cbor(reader, &script_invalid_before);

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

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

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

Note

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

Parameters:
cardano_cbor_reader_t *reader

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

cardano_script_invalid_before_t **script_invalid_before

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

Returns:

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


cardano_error_t cardano_script_invalid_before_to_cbor(const cardano_script_invalid_before_t *script_invalid_before, cardano_cbor_writer_t *writer)

Serializes a script_invalid_before into CBOR format using a CBOR writer.

This function serializes the given cardano_script_invalid_before_t object using a cardano_cbor_writer_t.

Usage Example:

cardano_script_invalid_before_t* script_invalid_before = ...;
cardano_cbor_writer_t* writer = cardano_cbor_writer_new();

if (writer)
{
  cardano_error_t result = cardano_script_invalid_before_to_cbor(script_invalid_before, writer);

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

  cardano_cbor_writer_unref(&writer);
}

cardano_script_invalid_before_unref(&script_invalid_before);

Parameters:
const cardano_script_invalid_before_t *script_invalid_before

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

cardano_cbor_writer_t *writer

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

Returns:

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


cardano_error_t cardano_script_invalid_before_to_cip116_json(const cardano_script_invalid_before_t *script_invalid_before, cardano_json_writer_t *writer)

Serializes a native script “timelock_start” to CIP-116 JSON.

Writes a single JSON object representing a timelock that is invalid before a given slot. The function emits the surrounding braces {} and the object contains the fields shown below.

JSON shape:

{
  "tag":  "timelock_start",
  "slot": "<uint>"
}

Note

Keys are emitted in a stable order: first "tag", then "slot".

Parameters:
const cardano_script_invalid_before_t *script_invalid_before

[in] A valid pointer to the timelock object to serialize.

cardano_json_writer_t *writer

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

Returns:

CARDANO_SUCCESS on success; CARDANO_ERROR_POINTER_IS_NULL if either parameter is NULL.


cardano_error_t cardano_script_invalid_before_from_json(const char *json, size_t json_size, cardano_script_invalid_before_t **native_script)

Creates a script_invalid_before from a JSON string.

This function parses JSON data using a provided JSON string and constructs a cardano_script_invalid_before_t object. It assumes that the JSON data corresponds to the structure expected for a script_invalid_before.

Usage Example:

const char* json_string = "{\"type\": \"after\", \"slot\": 500}"; // Example JSON string
size_t json_size = strlen(json_string); // Calculate the size of the JSON string
cardano_script_invalid_before_t* script_invalid_before = NULL;

// Attempt to create a new script_invalid_before from a JSON string
cardano_error_t result = cardano_script_invalid_before_from_json(json_string, json_size, &script_invalid_before);

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

  // Once done, ensure to clean up and release the script_invalid_before
  cardano_script_invalid_before_unref(&script_invalid_before);
}
else
{
  printf("Failed to create script_invalid_before from JSON. Error code: %d\n", result);
}

Parameters:
const char *json

[in] A pointer to a null-terminated string containing the JSON-encoded script_invalid_before data. This string must not be NULL.

size_t json_size

[in] The size of the JSON string, excluding the null terminator.

cardano_script_invalid_before_t **native_script

[out] A pointer to a pointer of cardano_script_invalid_before_t that will be set to the address of the newly created script_invalid_before object upon successful decoding. The caller is responsible for managing the lifecycle of this object by calling cardano_script_invalid_before_unref when it is no longer needed.

Returns:

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


cardano_error_t cardano_script_invalid_before_get_slot(const cardano_script_invalid_before_t *script_invalid_before, uint64_t *slot)

Retrieves the slot number from a script_invalid_before object.

This function retrieves the slot number from the provided cardano_script_invalid_before_t object and stores it in the provided output parameter.

Usage Example:

cardano_script_invalid_before_t* script_invalid_before = ...; // Assume script_invalid_before is initialized
uint64_t slot;
cardano_error_t result = cardano_script_invalid_before_get_slot(script_invalid_before, &slot);

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

// Clean up the script_invalid_before object once done
cardano_script_invalid_before_unref(&script_invalid_before);

Parameters:
const cardano_script_invalid_before_t *script_invalid_before

[in] A constant pointer to the cardano_script_invalid_before_t object from which the slot number is to be retrieved. The object must not be NULL.

uint64_t *slot

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

Returns:

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


cardano_error_t cardano_script_invalid_before_set_slot(cardano_script_invalid_before_t *script_invalid_before, uint64_t slot)

Sets the slot number for a script_invalid_before object.

This function sets the slot number for the provided cardano_script_invalid_before_t object.

Usage Example:

cardano_script_invalid_before_t* script_invalid_before = ...; // Assume script_invalid_before is initialized
uint64_t slot = 1000;

// Set the slot number for the script_invalid_before
cardano_error_t result = cardano_script_invalid_before_set_slot(script_invalid_before, slot);

if (result == CARDANO_SUCCESS)
{
  // Slot number set successfully
}

// Clean up the script_invalid_before object once done
cardano_script_invalid_before_unref(&script_invalid_before);

Parameters:
cardano_script_invalid_before_t *script_invalid_before

[in] A pointer to the cardano_script_invalid_before_t object for which the slot number is to be set.

uint64_t slot

[in] The slot number to set.

Returns:

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


bool cardano_script_invalid_before_equals(const cardano_script_invalid_before_t *lhs, const cardano_script_invalid_before_t *rhs)

Checks if two script_invalid_before objects are equal.

This function compares two cardano_script_invalid_before_t objects to determine if they are equal. Two script_invalid_before objects are considered equal if they have the same slot number.

Usage Example:

uint64_t slot1 = 1000;
uint64_t slot2 = 1000;
cardano_script_invalid_before_t* script_invalid_before1 = NULL;
cardano_script_invalid_before_t* script_invalid_before2 = NULL;

cardano_script_invalid_before_new(slot1, &script_invalid_before1);
cardano_script_invalid_before_new(slot2, &script_invalid_before2);

if (cardano_script_invalid_before_equals(script_invalid_before1, script_invalid_before2))
{
  // The two script_invalid_before objects are equal
}
else
{
  // The two script_invalid_before objects are not equal
}

// Clean up the script_invalid_before objects once done
cardano_script_invalid_before_unref(&script_invalid_before1);
cardano_script_invalid_before_unref(&script_invalid_before2);

Parameters:
const cardano_script_invalid_before_t *lhs

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

const cardano_script_invalid_before_t *rhs

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

Returns:

true if the two script_invalid_before objects are equal, false otherwise.


void cardano_script_invalid_before_unref(cardano_script_invalid_before_t **script_invalid_before)

Decrements the reference count of a script_invalid_before object.

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

Usage Example:

cardano_script_invalid_before_t* script_invalid_before = cardano_script_invalid_before_new();

// Perform operations with the script_invalid_before...

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

Note

After calling cardano_script_invalid_before_unref, the pointer to the cardano_script_invalid_before_t object will be set to NULL to prevent its reuse.

Parameters:
cardano_script_invalid_before_t **script_invalid_before

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


void cardano_script_invalid_before_ref(cardano_script_invalid_before_t *script_invalid_before)

Increases the reference count of the cardano_script_invalid_before_t object.

This function is used to manun_ofy increment the reference count of a script_invalid_before object, indicating that another part of the code has taken ownership of it. This ensures the object remains n_ofocated and valid until n_of owners have released their reference by calling cardano_script_invalid_before_unref.

Usage Example:

// Assuming script_invalid_before is a previously created script_invalid_before object

cardano_script_invalid_before_ref(script_invalid_before);

// Now script_invalid_before can be safely used elsewhere without worrying about premature den_ofocation

Note

Always ensure that for every call to cardano_script_invalid_before_ref there is a corresponding call to cardano_script_invalid_before_unref to prevent memory leaks.

Parameters:
cardano_script_invalid_before_t *script_invalid_before

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


size_t cardano_script_invalid_before_refcount(const cardano_script_invalid_before_t *script_invalid_before)

Retrieves the current reference count of the cardano_script_invalid_before_t object.

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

Usage Example:

// Assuming script_invalid_before is a previously created script_invalid_before object

size_t ref_count = cardano_script_invalid_before_refcount(script_invalid_before);

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

Warning

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

Parameters:
const cardano_script_invalid_before_t *script_invalid_before

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

Returns:

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


void cardano_script_invalid_before_set_last_error(cardano_script_invalid_before_t *script_invalid_before, const char *message)

Sets the last error message for a given script_invalid_before object.

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

Note

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

Parameters:
cardano_script_invalid_before_t *script_invalid_before

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

const char *message

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


const char *cardano_script_invalid_before_get_last_error(const cardano_script_invalid_before_t *script_invalid_before)

Retrieves the last error message recorded for a specific script_invalid_before.

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

Note

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

Parameters:
const cardano_script_invalid_before_t *script_invalid_before

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

Returns:

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