Bech32¶
-
size_t cardano_encoding_bech32_get_encoded_length(const char *hrp, size_t hrp_length, const byte_t *data, size_t data_length)¶
Calculates the length of a Bech32-encoded string.
This function computes the length of a string after it has been encoded using Bech32 encoding. The length includes the human-readable part (HRP), the data part, and the separator.
Example usage:
// Define the human-readable part and the data to be encoded const char* hrp = "addr"; size_t hrp_length = strlen(hrp); byte_t data[] = {0x01, 0x02, 0x03, 0x04}; size_t data_length = sizeof(data); // Calculate the required buffer size for the Bech32-encoded string size_t encoded_length = cardano_encoding_bech32_get_encoded_length(hrp, hrp_length, data, data_length); // Allocate the buffer for the Bech32-encoded string char* encoded_string = (char*)malloc(encoded_length); if (!encoded_string) { // Handle memory allocation failure } // Now you can proceed to encode the data into Bech32 using the allocated buffer- Parameters:¶
- const char *hrp¶
[in] Pointer to the human-readable part (HRP) of the Bech32-encoded data.
- size_t hrp_length¶
[in] The length of the HRP in bytes.
- const byte_t *data¶
[in] Pointer to the binary data that will be encoded in the data part of the Bech32 string.
- size_t data_length¶
[in] The length of the binary data in bytes.
- Returns:¶
The length of the Bech32-encoded string including the null terminator. This value can be used to allocate an appropriately sized buffer for the encoded string.
-
cardano_error_t cardano_encoding_bech32_encode(const char *hrp, size_t hrp_length, const byte_t *data, size_t data_length, char *output, size_t output_length)¶
Encodes data into a Bech32-encoded string.
This function encodes binary data, along with a human-readable part (HRP), into a Bech32-encoded string.
Example usage:
// Define the human-readable part (HRP) and the data to be encoded const char* hrp = "bc"; byte_t data[] = {0x01, 0x02, 0x03, 0x04}; size_t data_length = sizeof(data); // Calculate the required buffer size for the Bech32-encoded string size_t encoded_length = cardano_encoding_bech32_get_encoded_length(hrp, strlen(hrp), data, data_length); // Allocate the buffer for the Bech32-encoded string char* encoded_string = (char*)malloc(encoded_length); if (!encoded_string) { // Handle memory allocation failure } // Encode the data into Bech32 cardano_error_t result = cardano_encoding_bech32_encode(hrp, strlen(hrp), data, data_length, encoded_string, encoded_length); if (result == CARDANO_SUCCESS) { printf("Bech32 Encoded: %s\n", encoded_string); } else { // Handle encoding failure } // Clean up free(encoded_string);Note
The function requires that the
outputbuffer is pre-allocated and its length (output_length) is sufficient to hold the entire Bech32-encoded string including the null terminator. To determine the necessary buffer size in advance, use thecardano_encoding_bech32_get_encoded_lengthfunction.- Parameters:¶
- const char *hrp¶
[in] Pointer to the human-readable part (HRP) that conveys contextual information about the encoded data.
- size_t hrp_length¶
[in] The length of the HRP in bytes.
- const byte_t *data¶
[in] Pointer to the binary data to be encoded.
- size_t data_length¶
[in] The length of the binary data in bytes.
- char *output¶
[out] Pointer to the buffer where the Bech32-encoded string will be written. The buffer must be large enough to hold the encoded string and the null terminator.
- size_t output_length¶
[in] The size of the output buffer in bytes.
- Returns:¶
CARDANO_SUCCESS if the encoding was successful, or an appropriate error code otherwise.
-
size_t cardano_encoding_bech32_get_decoded_length(const char *data, size_t data_length, size_t *hrp_length)¶
Calculates the length of the data decoded from a Bech32 string.
This function computes the length of the data that will be obtained after decoding a Bech32-encoded string. It also provides the length of the human-readable part (HRP) present in the Bech32 string (including the null terminator).
Example usage:
// Bech32 encoded string const char* encoded_str = "addr_1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4"; size_t encoded_str_length = strlen(encoded_str); // Calculate the lengths of the decoded data and the HRP size_t hrp_length = 0; size_t decoded_data_length = cardano_encoding_bech32_get_decoded_length(encoded_str, encoded_str_length, &hrp_length); // Now you can allocate memory for the decoded data and the HRP accordingly byte_t* decoded_data = (byte_t*)malloc(decoded_data_length); char* hrp = (char*)malloc(hrp_length); if (!decoded_data || !hrp) { // Handle memory allocation failure } // Proceed to decode the Bech32 stringNote
The function does not decode the data itself; it merely calculates the length of the decoded data and the HRP based on the Bech32-encoded string. To decode the actual data, use the
cardano_encoding_bech32_decodefunction.- Parameters:¶
- const char *data¶
[in] Pointer to the Bech32-encoded data from which the lengths are to be calculated.
- size_t data_length¶
[in] The length of the Bech32-encoded data in bytes.
- size_t *hrp_length¶
[out] Pointer to the size_t variable where the length of the HRP will be stored. The value is set to the length of the HRP found in the Bech32-encoded string.
- Returns:¶
The length of the decoded data in bytes. This does not include the length of the HRP.
-
cardano_error_t cardano_encoding_bech32_decode(const char *input, size_t input_length, char *hrp, size_t hrp_length, byte_t *data, size_t data_length)¶
Decodes a Bech32-encoded string into its original data and human-readable part (HRP).
This function takes a Bech32-encoded string as input and decodes it back into the original binary data along with its human-readable part (HRP). The function requires pre-allocated buffers for both the HRP and the decoded data.
Example usage:
const char* encoded_str = "addr_1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4"; size_t input_length = strlen(encoded_str); char hrp[10]; // Assume you know the HRP length or have calculated it byte_t data[32]; // Pre-allocated buffer for decoded data size_t data_length = sizeof(data); cardano_error_t result = cardano_encoding_bech32_decode(encoded_str, input_length, hrp, sizeof(hrp), data, &data_length); if (result == CARDANO_SUCCESS) { // Decoded successfully printf("HRP: %s\n", hrp); // Use the decoded data as needed } else { // Handle decoding error }Note
The function will modify the
data_lengthparameter to indicate the actual length of the decoded data. Ensure that the buffers forhrpanddataare large enough to store the decoded information. Usecardano_encoding_bech32_get_decoded_lengthto calculate the required buffer sizes.- Parameters:¶
- const char *input¶
[in] The Bech32-encoded string to be decoded.
- size_t input_length¶
[in] The length of the Bech32-encoded string.
- char *hrp¶
[out] Pre-allocated buffer where the decoded human-readable part (HRP) will be stored.
- size_t hrp_length¶
[in] The size of the pre-allocated HRP buffer. It must be large enough to store the HRP including the null terminator.
- byte_t *data¶
[out] Pre-allocated buffer where the decoded binary data will be stored.
- size_t data_length¶
[inout] On input, specifies the size of the pre-allocated data buffer. On output, it is updated to reflect the actual length of the decoded data.
- Returns:¶
CARDANO_SUCCESS if the decoding is successful, otherwise an error code indicating the failure reason.