Skip to content

Commit

Permalink
Add tx matedata handling to check for asset id in transaction and com…
Browse files Browse the repository at this point in the history
…plete UI for that case
  • Loading branch information
neithanmo committed Dec 3, 2024
1 parent 36dacc7 commit 6b6a5a1
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 14 deletions.
7 changes: 5 additions & 2 deletions app/src/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@
* limitations under the License.
********************************************************************************/
#define U128_STR_MAX_LEN 40
// plus null terminator
#define MAX_DENOM_LEN 120 + 1

// Constant to use to allocate a buffer on the stack
// to hold the formatting of a value_t type, following
// provided documentatio, we choose the worst case, where
// provided documentation, we choose the worst case, where
// a value contains an unknown token and we use a custom denom
// U128 + space + denom + null terminator
#define VALUE_DISPLAY_MAX_LEN (U128_STR_MAX_LEN + 1 + 120 + 1) // = 162
#define VALUE_DISPLAY_MAX_LEN (U128_STR_MAX_LEN + 1 + MAX_DENOM_LEN) // = 162

// raw address len before encoding
#define ADDRESS_LEN_BYTES 80
Expand Down
7 changes: 3 additions & 4 deletions app/src/parser_txdef.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ extern "C" {
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include "constants.h"

#define MEMO_KEY_SIZE 32
#define MEMO_ADDRESS_INNER_SIZE 80
Expand All @@ -38,8 +39,6 @@ extern "C" {

#define MAX_SYMBOL_LEN 40
#define MAX_ASSET_NAME_LEN 120
// plus null terminator
#define MAX_DENOM_LEN 120 + 1

typedef struct {
const uint8_t *ptr;
Expand Down Expand Up @@ -273,8 +272,8 @@ typedef struct {
// that are not listed in our internal table
// but that the user provide when signing a transaction
typedef struct {
char denoms[MAX_DENOM_LEN];
uint8_t denom_len;
char denom[MAX_DENOM_LEN];
uint8_t len;
} tx_metadata_t;

#ifdef __cplusplus
Expand Down
40 changes: 36 additions & 4 deletions app/src/tx_metadata.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "parser_txdef.h"
#include "tx_metadata.h"
#include "rslib.h"

#include "constants.h"

parser_error_t metadata_parse(const uint8_t *data, size_t dataLen, tx_metadata_t *metadata, uint8_t metadataLen) {
if (metadata == NULL) {
Expand Down Expand Up @@ -66,8 +66,8 @@ parser_error_t metadata_parse(const uint8_t *data, size_t dataLen, tx_metadata_t
}

// Copy the string data into the metadata array
MEMCPY(metadata[i].denoms, &data[data_offset], len);
metadata[i].denom_len = len;
MEMCPY(metadata[i].denom, &data[data_offset], len);
metadata[i].len = len;

data_offset += len;
}
Expand All @@ -81,7 +81,39 @@ parser_error_t metadata_parse(const uint8_t *data, size_t dataLen, tx_metadata_t
return parser_ok;
}

parser_error_t metadata_toAssetId(const tx_metadata_t *metadata, uint8_t *assetId, uint16_t assetIdLen) {
parser_error_t metadata_toAssetId(const tx_metadata_t *metadata, uint8_t *asset, uint16_t asset_len) {
bytes_t data = {0};
data.ptr = (uint8_t *)&metadata->denom[0];
data.len = metadata->len;

CHECK_ERROR(rs_get_asset_id_from_metadata(&data, asset, asset_len));

return parser_ok;
}

uint8_t metadata_getDenom(const tx_metadata_t *metadata,
uint8_t metadataLen,
const asset_id_t *asset,
char *denom,
uint8_t len) {
if (metadataLen == 0 || metadata == NULL ||
asset == NULL || denom == NULL || len == 0) {
return 0;
}

uint8_t computed_asset[ASSET_ID_LEN] = {0};
const tx_metadata_t *found = NULL;

for(uint8_t i = 0; i < metadataLen; ++i) {
found = &metadata[i];
CHECK_ERROR(metadata_toAssetId(found, computed_asset, ASSET_ID_LEN));

if (MEMCMP(computed_asset, asset->inner.ptr, ASSET_ID_LEN) == 0) {
if (found->len <= len) {
MEMCPY(denom, found->denom, found->len);
return found->len;
}
}
}
return 0; // Asset not found
}
15 changes: 15 additions & 0 deletions app/src/tx_metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ parser_error_t metadata_parse(const uint8_t *data, size_t dataLen, tx_metadata_t

parser_error_t metadata_toAssetId(const tx_metadata_t *metadata, uint8_t *assetId, uint16_t assetIdLen);

/**
* @brief Retrieves denomination string for a given asset from metadata array
* @param metadata Array of transaction metadata entries
* @param metadataLen Length of the metadata array
* @param asset Target asset ID to search for
* @param denom Buffer to store the denomination string
* @param len Length of the denomination buffer
* @return Length of denomination copied, 0 if not found or error
*/
uint8_t metadata_getDenom(const tx_metadata_t *metadata,
uint8_t metadataLen,
const asset_id_t *asset,
char *denom,
uint8_t len);


#ifdef __cplusplus
}
Expand Down
32 changes: 28 additions & 4 deletions app/src/ui/note.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "note.h"
#include "constants.h"
#include "ui_utils.h"
#include "tx_metadata.h"

parser_error_t printValue(const parser_context_t *ctx, const value_t *value,
char *outVal, uint16_t outValLen) {
Expand Down Expand Up @@ -50,12 +51,34 @@ parser_error_t printValue(const parser_context_t *ctx, const value_t *value,
return parser_ok;
}

// TODO: Case 2: Base denom (integer + space + denom) taken from transaction
// Case 2: Base denom (integer + space + denom) taken from transaction
// for this we use the parser_context_t to access the transaction metadata
// here it is likely we need to call rust to compute the asset id
// and check it matches spend.note.asset_id, otherwise a fallback(case3) is used
// if not found, we default to case 3
char denom[MAX_DENOM_LEN + 1] = {0};

uint8_t trace_len = metadata_getDenom(&ctx->tx_metadata[0], MAX_TX_METADATA_LEN, &value->asset_id, denom, MAX_DENOM_LEN + 1);

if (trace_len != 0) {
// We found denom trace in provided transaction metadata
int written = snprintf(outVal, outValLen - 1, "%s", amount_str);
if (written < 0 || written >= outValLen - 1) {
return parser_unexpected_buffer_end;
}

// Space
outVal[written] = ' ';
written += 1;
// check we have space for denomination path
if (written + trace_len >= outValLen - 1) {
return parser_unexpected_buffer_end;
}

MEMCPY(&outVal[written], denom, trace_len);
written += trace_len;
outVal[written] = '\0';

return parser_ok;
}

// Case 3: Bech32 fallback (integer + space + bech32 of asset_id)
int written = snprintf(outVal, outValLen - 1, "%s", amount_str);
Expand All @@ -64,8 +87,9 @@ parser_error_t printValue(const parser_context_t *ctx, const value_t *value,
}
// Space
outVal[written] = ' ';
written += 1;

CHECK_ERROR(printAssetId(value->asset_id.inner.ptr, value->asset_id.inner.len, outVal + written + 1, outValLen - written - 1));
CHECK_ERROR(printAssetId(value->asset_id.inner.ptr, value->asset_id.inner.len, outVal + written, outValLen - written - 1));

return err;
}

0 comments on commit 6b6a5a1

Please sign in to comment.