Skip to content

Commit

Permalink
Move bec32 constants to own file and add defs for asset encoding
Browse files Browse the repository at this point in the history
import bool

Add asset encoding api
  • Loading branch information
neithanmo committed Nov 25, 2024
1 parent bc830ba commit 4735ad5
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 24 deletions.
24 changes: 24 additions & 0 deletions app/src/constants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Common BECH32m constants
#define CHECKSUM_LENGTH 8
#define BECH32_BITS_PER_CHAR 5
#define BITS_PER_BYTE 8
#define BECH32_SEPARATOR "1"
#define SEPARATOR_LENGTH 1

// Some defines for address and asset encoding
#define ADDR_BECH32_PREFIX "penumbra" BECH32_SEPARATOR
// #define FIXED_ADDR_PREFIX ADDR_BECH32_PREFIX BECH32_SEPARATOR
#define ASSET_BECH32_PREFIX "passet" BECH32_SEPARATOR
#define ASSET_ID_LEN 32
// HRP length + 1 (separator) + 52 (data) + 6 (checksum) + 1 (null terminator)
// 6 + 1 + 52 + 6 + 1 = 66
#define ENCODED_ASSET_SIZE (strlen(ASSET_BECH32_PREFIX) + ((ASSET_ID_LEN * BITS_PER_BYTE + BECH32_BITS_PER_CHAR - 1) / BECH32_BITS_PER_CHAR) + CHECKSUM_LENGTH + 1)

#define ADDR_HRP_LENGTH (sizeof(ADDR_BECH32_PREFIX) - 1)

#define ENCODED_DATA_LENGTH \
(((ADDRESS_LEN_BYTES + CHECKSUM_LENGTH) * BITS_PER_BYTE + BECH32_BITS_PER_CHAR - 1) / BECH32_BITS_PER_CHAR)

#define ENCODED_ADDR_LEN (ADDR_HRP_LENGTH + SEPARATOR_LENGTH + ENCODED_DATA_LENGTH)

#define ENCODED_ADDR_BUFFER_SIZE (ENCODED_ADDR_LEN + 2)
18 changes: 1 addition & 17 deletions app/src/keys_def.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ extern "C" {

#include <stddef.h>
#include <stdint.h>
#include "constants.h"

#define KEY_LEN 32
#define DIVERSIFIER_KEY_LEN 16
Expand All @@ -42,26 +43,9 @@ extern "C" {

#define ADDR_RANDOMIZER_LEN 12

#define BECH32_PREFIX "penumbra"

#define FIXED_PREFIX BECH32_PREFIX "1"
#define ADDRESS_NUM_CHARS_SHORT_FORM 24
#define NUM_CHARS_TO_DISPLAY 33

#define HRP_LENGTH (sizeof(FIXED_PREFIX) - 1)
#define CHECKSUM_LENGTH 8
#define SEPARATOR_LENGTH 1

#define BECH32_BITS_PER_CHAR 5
#define BITS_PER_BYTE 8

#define ENCODED_DATA_LENGTH \
(((ADDRESS_LEN_BYTES + CHECKSUM_LENGTH) * BITS_PER_BYTE + BECH32_BITS_PER_CHAR - 1) / BECH32_BITS_PER_CHAR)

#define ENCODED_ADDR_LEN (HRP_LENGTH + SEPARATOR_LENGTH + ENCODED_DATA_LENGTH)

#define ENCODED_ADDR_BUFFER_SIZE (ENCODED_ADDR_LEN + 2)

// raw keys in bytes

/** The spending key consists of spend_key_bytes and ask.
Expand Down
1 change: 1 addition & 0 deletions app/src/parser_txdef.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ extern "C" {

#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>

#define MEMO_KEY_SIZE 32
#define MEMO_ADDRESS_INNER_SIZE 80
Expand Down
33 changes: 26 additions & 7 deletions app/src/ui_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,49 @@
#include <stdio.h>

#include "parser_common.h"
#include "keys_def.h"
#include "rslib.h"
#include "zxerror.h"
#include "zxformat.h"
#include "zxmacros.h"
#include "constants.h"

parser_error_t printAddress(uint8_t *address, uint16_t address_len, char *out, uint16_t out_len) {
if (address_len != ADDRESS_LEN_BYTES) {

parser_error_t printBech32Encoded(const char *prefix, uint16_t prefix_len, uint8_t *data,
uint16_t data_len, uint16_t expected_len,
char *out, uint16_t out_len) {
if (data == NULL) {
return parser_unexpected_error;
}
if (data_len != expected_len) {
return parser_invalid_address;
}

// check we have space for the null terminator
if (out_len < ENCODED_ADDR_BUFFER_SIZE + 1) {
// Check we have space for the null terminator
if (out_len < prefix_len + ((data_len * 8 + 4) / 5) + CHECKSUM_LENGTH + 1) {
return parser_display_idx_out_of_range;
}

MEMZERO(out, out_len);

int32_t ret = rs_bech32_encode((const uint8_t *)BECH32_PREFIX, sizeof(BECH32_PREFIX) - 1, address,
address_len, (uint8_t *)out, out_len);
int32_t ret = rs_bech32_encode((const uint8_t *)prefix, prefix_len, data,
data_len, (uint8_t *)out, out_len);

if (ret < 0) {
return parser_unexpected_error;
}

return parser_ok;
}


parser_error_t printAddress(uint8_t *address, uint16_t address_len, char *out, uint16_t out_len) {
return printBech32Encoded(ADDR_BECH32_PREFIX, sizeof(ADDR_BECH32_PREFIX) - 1,
address, address_len, ADDRESS_LEN_BYTES,
out, out_len);
}

parser_error_t printAssetId(uint8_t *asset, uint16_t asset_len, char *out, uint16_t out_len) {
return printBech32Encoded(ASSET_BECH32_PREFIX, sizeof(ASSET_BECH32_PREFIX) - 1,
asset, asset_len, ASSET_ID_LEN,
out, out_len);
}
24 changes: 24 additions & 0 deletions app/src/ui_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,29 @@
********************************************************************************/
#pragma once

/**
* @brief Encodes binary data into a Bech32 format string with a specified prefix.
*
* This function generates a Bech32-encoded string from the input data, appending a checksum.
* It verifies that the input data has the expected length and that the output buffer
* is large enough to accommodate the resulting string.
*
* @param[in] prefix The human-readable part (HRP) of the Bech32 string.
* @param[in] prefix_len The length of the prefix, excluding the null terminator.
* @param[in] data Pointer to the binary data to encode.
* @param[in] data_len Length of the binary data to encode.
* @param[in] expected_len Expected length of the binary data (for validation).
* @param[out] out Buffer to store the resulting Bech32-encoded string.
* @param[in] out_len Length of the output buffer.
*
* @return parser_ok on success, or an appropriate error code:
* - `parser_unexpected_error` if encoding fails or the input data is NULL.
* - `parser_invalid_address` if the length of the input data does not match the expected length.
* - `parser_display_idx_out_of_range` if the output buffer is too small.
*/
parser_error_t printBech32Encoded(const char *prefix, uint16_t prefix_len, uint8_t *data,
uint16_t data_len, uint16_t expected_len,
char *out, uint16_t out_len);

parser_error_t printAddress(uint8_t *address, uint16_t address_len, char *out, uint16_t out_len);
parser_error_t printAssetId(uint8_t *asset, uint16_t asset_len, char *out, uint16_t out_len);

0 comments on commit 4735ad5

Please sign in to comment.