Skip to content

Commit

Permalink
add asset_id verification and format amount
Browse files Browse the repository at this point in the history
  • Loading branch information
chcmedeiros committed Oct 30, 2024
1 parent a87fe9a commit 6e23260
Show file tree
Hide file tree
Showing 7 changed files with 186 additions and 88 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ file(GLOB_RECURSE LIB_SRC
${CMAKE_CURRENT_SOURCE_DIR}/app/src/parser_impl.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/parser_impl_common.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/crypto_helper.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/parser_print_common.c

# ###
${CMAKE_CURRENT_SOURCE_DIR}/deps/blake2/ref/blake2b-ref.c
Expand Down
58 changes: 39 additions & 19 deletions app/src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,34 @@
#include "crypto_helper.h"
#include "parser_common.h"
#include "parser_impl.h"
#include "parser_print_common.h"
#include "rslib.h"

// lookup table for future use
static const asset_id_lookpup_t asset_id_lookups[] = {
{{0x51, 0xf3, 0x3a, 0x2f, 0x14, 0xf9, 0x27, 0x35, 0xe5, 0x62, 0xdc, 0x65, 0x8a, 0x56, 0x39, 0x27,
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) {
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;
}
}
// Temporarly set to unknown asset
*index = 1;
return parser_ok;
}

parser_error_t parser_init_context(parser_context_t *ctx, const uint8_t *buffer, uint16_t bufferSize) {
ctx->offset = 0;
ctx->buffer = NULL;
Expand Down Expand Up @@ -68,7 +94,7 @@ parser_error_t parser_getNumItems(const parser_context_t *ctx, uint8_t *num_item
UNUSED(ctx);

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

if (*num_items == 0) {
return parser_unexpected_number_items;
Expand Down Expand Up @@ -112,13 +138,10 @@ parser_error_t parser_getItem(const parser_context_t *ctx, uint8_t displayIdx, c
return parser_ok;
}

displayIdx -= 1;
if (tmp_idx > 0 && tmp_idx <= total_out_elements) {
tmp_idx = (displayIdx % ELEMENTS_PER_OUTPUT);
out_idx = (displayIdx / ELEMENTS_PER_OUTPUT);

if (tmp_idx == 1 || tmp_idx == 2) {
out_idx++;
}
out_idx = (displayIdx / ELEMENTS_PER_OUTPUT) + 1;

if (prev_decrypted_out_idx != out_idx) {
const uint8_t *output = ctx->tx_obj->outputs.data.ptr + ((out_idx - 1) * (192 + 328));
Expand All @@ -130,28 +153,25 @@ parser_error_t parser_getItem(const parser_context_t *ctx, uint8_t displayIdx, c
}

char buf[70] = {0};
uint8_t asset_id_idx = 0;
switch (tmp_idx) {
case 0:
snprintf(outKey, outKeyLen, "AssetID %d", out_idx);
array_to_hexstr(buf, sizeof(buf), ctx->tx_obj->outputs.decrypted_note.asset_id, 32);
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, "Owner %d", out_idx);
array_to_hexstr(buf, sizeof(buf), ctx->tx_obj->outputs.decrypted_note.owner, 32);
pageString(outVal, outValLen, buf, pageIdx, pageCount);
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));
return parser_ok;
case 2:
snprintf(outKey, outKeyLen, "Amount %d", out_idx);
uint64_to_str(buf, sizeof(buf), ctx->tx_obj->outputs.decrypted_note.value);
pageString(outVal, outValLen, buf, pageIdx, pageCount);
return parser_ok;
case 3:
snprintf(outKey, outKeyLen, "Fee");
uint64_to_str(buf, sizeof(buf), ctx->tx_obj->fee);
pageString(outVal, outValLen, buf, pageIdx, pageCount);
CHECK_ERROR(printAmount64(ctx->tx_obj->fee, asset_id_lookups[0].decimals, PIC(asset_id_lookups[0].name), outVal,
outValLen, pageIdx, pageCount));
return parser_ok;
case 4:
case 3:
snprintf(outKey, outKeyLen, "Expiration");
uint32_to_str(buf, sizeof(buf), ctx->tx_obj->expiration);
pageString(outVal, outValLen, buf, pageIdx, pageCount);
Expand Down
2 changes: 1 addition & 1 deletion app/src/parser_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
extern "C" {
#endif

#define ELEMENTS_PER_OUTPUT 3
#define ELEMENTS_PER_OUTPUT 2
#define OUTPUT_ELEMENT_OFFSET 1
/**
* @brief Checks that there are at least SIZE bytes available in the buffer.
Expand Down
66 changes: 66 additions & 0 deletions app/src/parser_print_common.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*******************************************************************************
* (c) 2018 - 2024 Zondax AG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
********************************************************************************/
#include "parser_print_common.h"

#include <zxformat.h>
#include <zxmacros.h>

#include "coin.h"

void remove_fraction(char *s) {
size_t len = strlen(s);

// Find the decimal point
char *decimal_point = strchr(s, '.');
if (decimal_point == NULL) {
// No decimal point found, nothing to remove
return;
}

// Find the end of the string up to the decimal point
size_t end_index = decimal_point - s;

// Find the first non-zero digit after the decimal point
size_t non_zero_index = end_index + 1;
while (s[non_zero_index] == '0') {
non_zero_index++;
}

// Check if there is a non-zero digit after the decimal point
if (non_zero_index >= len) {
// There is no non-zero digit after the decimal point
// Remove the decimal point and trailing zeros
s[end_index] = '\0';
}
}

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) {
char strAmount[33] = {0};
if (uint64_to_str(strAmount, sizeof(strAmount), amount) != NULL) {
return parser_unexpected_error;
}
if (intstr_to_fpstr_inplace(strAmount, sizeof(strAmount), amount_decimals) == 0) {
return parser_unexpected_error;
}

number_inplace_trimming(strAmount, 1);
remove_fraction(strAmount);
z_str3join(strAmount, sizeof(strAmount), "", symbol);
pageString(outVal, outValLen, strAmount, pageIdx, pageCount);

return parser_ok;
}
29 changes: 29 additions & 0 deletions app/src/parser_print_common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*******************************************************************************
* (c) 2018 - 2024 Zondax AG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
********************************************************************************/
#pragma once

#include "parser_common.h"

#ifdef __cplusplus
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
}
#endif
6 changes: 6 additions & 0 deletions app/src/parser_txdef.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ typedef struct {
bytes_t data;
} vec_mint_description_t;

typedef struct {
uint8_t identifier[32];
uint8_t decimals;
const char *name;
} asset_id_lookpup_t;

typedef struct {
transaction_version_e transactionVersion;
int64_t fee;
Expand Down
Loading

0 comments on commit 6e23260

Please sign in to comment.