Skip to content

Commit

Permalink
add raw amount print
Browse files Browse the repository at this point in the history
  • Loading branch information
chcmedeiros committed Oct 31, 2024
1 parent 720f6ab commit 9d6360a
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 22 deletions.
3 changes: 3 additions & 0 deletions app/src/common/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ parser_error_t parser_getNumItems(const parser_context_t *ctx, uint8_t *num_item
parser_error_t parser_getItem(const parser_context_t *ctx, uint8_t displayIdx, char *outKey, uint16_t outKeyLen,
char *outVal, uint16_t outValLen, uint8_t pageIdx, uint8_t *pageCount);

// checks outputs asset_id
parser_error_t parser_check_outputs(parser_tx_t *tx_obj);

#ifdef __cplusplus
}
#endif
5 changes: 5 additions & 0 deletions app/src/common/tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ const char *tx_parse() {
return parser_getErrorDescription(err);
}

err = parser_check_outputs(&tx_obj);
if (err != parser_ok) {
return parser_getErrorDescription(err);
}

err = parser_validate(&ctx_parsed_tx);
CHECK_APP_CANARY()

Expand Down
68 changes: 47 additions & 21 deletions app/src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,43 @@ static const asset_id_lookpup_t asset_id_lookups[] = {
0x9d, 0xdc, 0xa3, 0xd5, 0x07, 0x9a, 0x6d, 0x12, 0x42, 0xb2, 0xa5, 0x88, 0xa9, 0xcb, 0xf4, 0x4c},
8,
" IRON"},
{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
8,
" UNKNOWN"},

};

parser_error_t parser_verify_asset_id(uint8_t *asset_id, uint8_t *index) {
bool parser_verify_asset_id(uint8_t *asset_id, uint8_t *index) {
for (size_t i = 0; i < sizeof(asset_id_lookups) / sizeof(asset_id_lookups[0]); i++) {
if (MEMCMP(asset_id, PIC(asset_id_lookups[i].identifier), 32) == 0) {
*index = i;
return parser_ok;
return true;
}
}
return false;
}

parser_error_t parser_check_outputs(parser_tx_t *tx_obj) {
for (size_t i = 0; i < tx_obj->outputs.elements; i++) {
const uint8_t *output = tx_obj->outputs.data.ptr + (i * (192 + 328));
CHECK_ERROR(crypto_decrypt_merkle_note(tx_obj, output + 192, tx_obj->ovk));

bool asset_found = false; // Track if asset ID is found
for (size_t j = 0; j < sizeof(asset_id_lookups) / sizeof(asset_id_lookups[0]); j++) {
if (MEMCMP(tx_obj->outputs.decrypted_note.asset_id, PIC(asset_id_lookups[j].identifier), 32) == 0) {
asset_found = true; // Asset ID found
break;
}
}

// Handle case when asset ID is not found
if (!asset_found) {
tx_obj->n_raw_asset_id++; // Increment if asset ID is not found
#if defined(LEDGER_SPECIFIC)
// If no asset id is found, required expert mode
if (!app_mode_expert()) {
return parser_require_expert_mode;
}
#else
*index = 1;
// Check for expert mode if asset ID is not found
if (!app_mode_expert()) {
return parser_require_expert_mode;
}
#endif
return parser_ok;
}
}
return parser_ok; // Return parser_ok after processing all outputs
}

parser_error_t parser_init_context(parser_context_t *ctx, const uint8_t *buffer, uint16_t bufferSize) {
Expand Down Expand Up @@ -100,8 +114,10 @@ parser_error_t parser_validate(parser_context_t *ctx) {
parser_error_t parser_getNumItems(const parser_context_t *ctx, uint8_t *num_items) {
UNUSED(ctx);

// Txversion + (ownner + amount + asset id) * n_output + fee + expiration
*num_items = 1 + ctx->tx_obj->outputs.elements * 2 + 2;
// Txversion + (owner + amount ) * output_with_valid_asset_id + (owner + amount + asset id) * output_with_raw_asset_id +
// fee + expiration
*num_items =
1 + ((ctx->tx_obj->outputs.elements - ctx->tx_obj->n_raw_asset_id) * 2) + (ctx->tx_obj->n_raw_asset_id * 3) + 2;

if (*num_items == 0) {
return parser_unexpected_number_items;
Expand Down Expand Up @@ -138,6 +154,8 @@ parser_error_t parser_getItem(const parser_context_t *ctx, uint8_t displayIdx, c

uint64_t total_out_elements = ctx->tx_obj->outputs.elements * ELEMENTS_PER_OUTPUT;
uint8_t tmp_idx = displayIdx;
uint8_t asset_id_idx = 0;
bool known_asset_id = false;

if (displayIdx == 0) {
snprintf(outKey, outKeyLen, "Tx Version");
Expand All @@ -155,23 +173,31 @@ parser_error_t parser_getItem(const parser_context_t *ctx, uint8_t displayIdx, c
CHECK_ERROR(crypto_decrypt_merkle_note(ctx->tx_obj, output + 192, ctx->tx_obj->ovk));
prev_decrypted_out_idx = out_idx;
}

known_asset_id = parser_verify_asset_id(ctx->tx_obj->outputs.decrypted_note.asset_id, &asset_id_idx);

} else if (tmp_idx > total_out_elements) {
tmp_idx -= total_out_elements - ELEMENTS_PER_OUTPUT + 1;
}

char buf[70] = {0};
uint8_t asset_id_idx = 0;

switch (tmp_idx) {
case 0:
snprintf(outKey, outKeyLen, "To %d", out_idx - 1);
array_to_hexstr(buf, sizeof(buf), ctx->tx_obj->outputs.decrypted_note.owner, 32);
pageString(outVal, outValLen, buf, pageIdx, pageCount);
return parser_ok;
case 1:
snprintf(outKey, outKeyLen, "Amount %d", out_idx - 1);
CHECK_ERROR(parser_verify_asset_id(ctx->tx_obj->outputs.decrypted_note.asset_id, &asset_id_idx));
CHECK_ERROR(printAmount64(ctx->tx_obj->outputs.decrypted_note.value, asset_id_lookups[asset_id_idx].decimals,
PIC(asset_id_lookups[asset_id_idx].name), outVal, outValLen, pageIdx, pageCount));
if (known_asset_id) {
snprintf(outKey, outKeyLen, "Amount %d", out_idx - 1);
CHECK_ERROR(printAmount64(ctx->tx_obj->outputs.decrypted_note.value, asset_id_lookups[asset_id_idx].decimals,
PIC(asset_id_lookups[asset_id_idx].name), outVal, outValLen, pageIdx, pageCount));
} else {
snprintf(outKey, outKeyLen, "Raw amount %d", out_idx - 1);
uint64_to_str(buf, sizeof(buf), ctx->tx_obj->outputs.decrypted_note.value);
pageString(outVal, outValLen, buf, pageIdx, pageCount);
}
return parser_ok;
case 2:
snprintf(outKey, outKeyLen, "Fee");
Expand Down
1 change: 0 additions & 1 deletion app/src/parser_print_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
extern "C" {
#endif

parser_error_t parser_verify_asset_id(uint8_t *asset_id, uint8_t *index);
parser_error_t printAmount64(uint64_t amount, uint8_t amount_decimals, const char *symbol, char *outVal, uint16_t outValLen,
uint8_t pageIdx, uint8_t *pageCount);
#ifdef __cplusplus
Expand Down
2 changes: 2 additions & 0 deletions app/src/parser_txdef.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ typedef struct {

// Not part of the incoming txn but it's used to compute signatures
uint8_t transactionHash[32];

uint8_t n_raw_asset_id;
} parser_tx_t;

#ifdef __cplusplus
Expand Down

0 comments on commit 9d6360a

Please sign in to comment.