Skip to content

Commit

Permalink
Clean Up TODOs
Browse files Browse the repository at this point in the history
  • Loading branch information
0xPxt committed Aug 21, 2024
1 parent b57455a commit eb61190
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 104 deletions.
2 changes: 0 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,6 @@ add_definitions(
)


##############################################################
##{TODO}
##############################################################
# Static Libraries

Expand Down
2 changes: 1 addition & 1 deletion app/src/common/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const char *parser_getErrorDescription(parser_error_t err);
const char *parser_getMsgPackTypeDescription(uint8_t type);

//// parses a tx buffer
parser_error_t parser_parse(parser_context_t *ctx, const uint8_t *data, size_t dataLen, parser_tx_t *tx_obj);
parser_error_t parser_parse(parser_context_t *ctx, const uint8_t *data, size_t dataLen, tx_json_t *tx_obj);

//// verifies tx fields
parser_error_t parser_validate(parser_context_t *ctx);
Expand Down
2 changes: 1 addition & 1 deletion app/src/common/tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ storage_t NV_CONST N_appdata_impl __attribute__((aligned(64)));
#define N_appdata (*(NV_VOLATILE storage_t *)PIC(&N_appdata_impl))
#endif

static parser_tx_t tx_obj;
static tx_json_t tx_obj;
static parser_context_t ctx_parsed_tx;

void tx_initialize() {
Expand Down
14 changes: 7 additions & 7 deletions app/src/items.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static items_error_t items_storeCrossTransferItem(parsed_json_t *json_all, uint1
static items_error_t items_storeRotateItem(parsed_json_t *json_all, uint16_t transfer_token_index, uint8_t items_idx, uint8_t *unknown_capabitilies);
static items_error_t items_storeUnknownItem(parsed_json_t *json_all, uint16_t transfer_token_index, uint8_t items_idx, uint8_t *unknown_capabitilies);

#define MAX_ITEM_LENGTH_TO_DISPLAY 1000 // TODO : Check other apps to find this number
#define MAX_ITEM_LENGTH_TO_DISPLAY 256

item_array_t item_array = {0};

Expand Down Expand Up @@ -126,7 +126,7 @@ static items_error_t items_storeRequiringCapabilities(uint8_t *items_idx) {
}

static items_error_t items_storeKey(uint8_t *items_idx) {
parsed_json_t *json_all = &(parser_getParserTxObj()->tx_json.json);
parsed_json_t *json_all = &(parser_getParserTxObj()->json);
uint16_t *curr_token_idx = &item_array.items[*items_idx].json_token_index;

if (parser_getJsonValue(curr_token_idx, JSON_SIGNERS) == parser_ok) {
Expand All @@ -142,7 +142,7 @@ static items_error_t items_storeKey(uint8_t *items_idx) {
}

static items_error_t items_validateSigners(uint8_t *items_idx) {
parsed_json_t *json_all = &(parser_getParserTxObj()->tx_json.json);
parsed_json_t *json_all = &(parser_getParserTxObj()->json);
uint16_t *curr_token_idx = &item_array.items[*items_idx].json_token_index;
uint16_t token_index = 0;

Expand Down Expand Up @@ -180,7 +180,7 @@ static items_error_t items_validateSigners(uint8_t *items_idx) {
}

static items_error_t items_storePayingGas(uint8_t *items_idx, uint8_t *unknown_capabitilies) {
parsed_json_t *json_all = &(parser_getParserTxObj()->tx_json.json);
parsed_json_t *json_all = &(parser_getParserTxObj()->json);
uint16_t *curr_token_idx = &item_array.items[*items_idx].json_token_index;

if (parser_getJsonValue(curr_token_idx, JSON_SIGNERS) == parser_ok) {
Expand All @@ -198,7 +198,7 @@ static items_error_t items_storePayingGas(uint8_t *items_idx, uint8_t *unknown_c
}

static items_error_t items_storeAllTransfers(uint8_t *items_idx, uint8_t *unknown_capabitilies) {
parsed_json_t *json_all = &(parser_getParserTxObj()->tx_json.json);
parsed_json_t *json_all = &(parser_getParserTxObj()->json);
uint16_t *curr_token_idx = &item_array.items[*items_idx].json_token_index;
uint16_t token_index = 0;
uint8_t num_of_transfers = 1;
Expand Down Expand Up @@ -302,7 +302,7 @@ static items_error_t items_checkTxLengths(uint8_t *items_idx) {
static items_error_t items_storeHash(uint8_t *items_idx) {
strcpy(item_array.items[*items_idx].key, "Transaction hash");

if (blake2b_hash((uint8_t *)parser_getParserTxObj()->tx_json.json.buffer, parser_getParserTxObj()->tx_json.json.bufferLen, hash) != zxerr_ok) {
if (blake2b_hash((uint8_t *)parser_getParserTxObj()->json.buffer, parser_getParserTxObj()->json.bufferLen, hash) != zxerr_ok) {
return items_error;
}

Expand Down Expand Up @@ -335,7 +335,7 @@ static items_error_t items_storeSignForAddr(uint8_t *items_idx) {
static items_error_t items_storeGasItem(uint16_t json_token_index, uint8_t items_idx, uint8_t *unknown_capabitilies) {
uint16_t token_index = 0;
uint16_t args_count = 0;
parsed_json_t *json_all = &(parser_getParserTxObj()->tx_json.json);
parsed_json_t *json_all = &(parser_getParserTxObj()->json);

object_get_value(json_all, json_token_index, "args", &token_index);
array_get_element_count(json_all, token_index, &args_count);
Expand Down
12 changes: 6 additions & 6 deletions app/src/items_format.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
extern char base64_hash[44];

items_error_t items_stdToDisplayString(item_t item, char *outVal, uint16_t *outValLen) {
parsed_json_t *json_all = &(parser_getParserTxObj()->tx_json.json);
parsed_json_t *json_all = &(parser_getParserTxObj()->json);
uint16_t item_token_index = item.json_token_index;

*outValLen = json_all->tokens[item_token_index].end - json_all->tokens[item_token_index].start + 1;
Expand Down Expand Up @@ -76,7 +76,7 @@ items_error_t items_transferToDisplayString(item_t item, char *outVal, uint16_t
char from[65];
uint8_t from_len = 0;
uint16_t token_index = 0;
parsed_json_t *json_all = &(parser_getParserTxObj()->tx_json.json);
parsed_json_t *json_all = &(parser_getParserTxObj()->json);
uint16_t item_token_index = item.json_token_index;

object_get_value(json_all, item_token_index, "args", &token_index);
Expand All @@ -103,7 +103,7 @@ items_error_t items_crossTransferToDisplayString(item_t item, char *outVal, uint
char chain[3];
uint8_t chain_len = 0;
uint16_t token_index = 0;
parsed_json_t *json_all = &(parser_getParserTxObj()->tx_json.json);
parsed_json_t *json_all = &(parser_getParserTxObj()->json);
uint16_t item_token_index = item.json_token_index;

object_get_value(json_all, item_token_index, "args", &token_index);
Expand All @@ -125,7 +125,7 @@ items_error_t items_crossTransferToDisplayString(item_t item, char *outVal, uint
items_error_t items_rotateToDisplayString(item_t item, char *outVal, uint16_t *outValLen) {
uint16_t token_index = 0;
uint16_t item_token_index = item.json_token_index;
parsed_json_t *json_all = &(parser_getParserTxObj()->tx_json.json);
parsed_json_t *json_all = &(parser_getParserTxObj()->json);

object_get_value(json_all, item_token_index, "args", &token_index);
array_get_nth_element(json_all, token_index, 0, &token_index);
Expand All @@ -141,7 +141,7 @@ items_error_t items_gasToDisplayString(__Z_UNUSED item_t item, char *outVal, uin
uint8_t gasLimit_len = 0;
char gasPrice[64];
uint8_t gasPrice_len = 0;
parsed_json_t *json_all = &(parser_getParserTxObj()->tx_json.json);
parsed_json_t *json_all = &(parser_getParserTxObj()->json);
uint16_t item_token_index = item.json_token_index;
uint16_t meta_token_index = item_token_index;

Expand Down Expand Up @@ -171,7 +171,7 @@ items_error_t items_unknownCapabilityToDisplayString(item_t item, char *outVal,
uint16_t args_count = 0;
uint8_t len = 0;
uint8_t outVal_idx= 0;
parsed_json_t *json_all = &(parser_getParserTxObj()->tx_json.json);
parsed_json_t *json_all = &(parser_getParserTxObj()->json);
uint16_t item_token_index = item.json_token_index;

object_get_value(json_all, item_token_index, "name", &token_index);
Expand Down
2 changes: 1 addition & 1 deletion app/src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ parser_error_t parser_init_context(parser_context_t *ctx, const uint8_t *buffer,
return parser_ok;
}

parser_error_t parser_parse(parser_context_t *ctx, const uint8_t *data, size_t dataLen, parser_tx_t *tx_obj) {
parser_error_t parser_parse(parser_context_t *ctx, const uint8_t *data, size_t dataLen, tx_json_t *tx_obj) {
CHECK_ERROR(parser_init_context(ctx, data, dataLen))
ctx->tx_obj = tx_obj;

Expand Down
28 changes: 14 additions & 14 deletions app/src/parser_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,29 @@
#include "crypto_helper.h"
#include <base64.h>

parser_tx_t *parser_tx_obj;
tx_json_t *parser_tx_obj;

parser_error_t _read_json_tx(parser_context_t *c, parser_tx_t *v) {
parser_error_t _read_json_tx(parser_context_t *c, tx_json_t *v) {
parser_tx_obj = v;

CHECK_ERROR(json_parse(&(parser_tx_obj->tx_json.json), (const char *) c->buffer,
CHECK_ERROR(json_parse(&(parser_tx_obj->json), (const char *) c->buffer,
c->bufferLen));

parser_tx_obj->tx_json.tx = (const char *) c->buffer;
parser_tx_obj->tx_json.flags.cache_valid = 0;
parser_tx_obj->tx_json.filter_msg_type_count = 0;
parser_tx_obj->tx_json.filter_msg_from_count = 0;
parser_tx_obj->tx = (const char *) c->buffer;
parser_tx_obj->flags.cache_valid = 0;
parser_tx_obj->filter_msg_type_count = 0;
parser_tx_obj->filter_msg_from_count = 0;

return parser_ok;
}

parser_tx_t *parser_getParserTxObj() {
tx_json_t *parser_getParserTxObj() {
return parser_tx_obj;
}

uint16_t parser_getNumberOfClistElements() {
uint16_t number_of_elements = 0;
parsed_json_t *json_all = &parser_tx_obj->tx_json.json;
parsed_json_t *json_all = &parser_tx_obj->json;
uint16_t token_index = 0;

parser_getJsonValue(&token_index, JSON_SIGNERS);
Expand All @@ -53,7 +53,7 @@ uint16_t parser_getNumberOfClistElements() {
}

parser_error_t parser_findPubKeyInClist(uint16_t key_token_index) {
parsed_json_t *json_all = &parser_tx_obj->tx_json.json;
parsed_json_t *json_all = &parser_tx_obj->json;
uint16_t token_index = 0;
uint16_t clist_token_index = 0;
uint16_t args_token_index = 0;
Expand Down Expand Up @@ -86,7 +86,7 @@ parser_error_t parser_findPubKeyInClist(uint16_t key_token_index) {
}

parser_error_t parser_getJsonValue(uint16_t *json_token_index, const char *key) {
parsed_json_t *json_all = &(parser_tx_obj->tx_json.json);
parsed_json_t *json_all = &(parser_tx_obj->json);
uint16_t token_index = 0;

CHECK_ERROR(object_get_value(json_all, *json_token_index, key, &token_index));
Expand All @@ -102,7 +102,7 @@ parser_error_t parser_getJsonValue(uint16_t *json_token_index, const char *key)

parser_error_t parser_arrayElementToString(uint16_t json_token_index, uint16_t element_idx, char *outVal, uint8_t *outValLen) {
uint16_t token_index = 0;
parsed_json_t *json_all = &(parser_tx_obj->tx_json.json);
parsed_json_t *json_all = &(parser_tx_obj->json);

CHECK_ERROR(array_get_nth_element(json_all, json_token_index, element_idx, &token_index));
strncpy(outVal, json_all->buffer + json_all->tokens[token_index].start, json_all->tokens[token_index].end - json_all->tokens[token_index].start);
Expand All @@ -114,7 +114,7 @@ parser_error_t parser_arrayElementToString(uint16_t json_token_index, uint16_t e

parser_error_t parser_getGasObject(uint16_t *json_token_index) {
uint16_t token_index = 0;
parsed_json_t *json_all = &parser_tx_obj->tx_json.json;
parsed_json_t *json_all = &parser_tx_obj->json;
uint16_t name_token_index = 0;

for (uint16_t i = 0; i < parser_getNumberOfClistElements(); i++) {
Expand Down Expand Up @@ -144,7 +144,7 @@ parser_error_t parser_validateMetaField() {
uint16_t meta_token_index = 0;
uint16_t meta_num_elements = 0;
uint16_t key_token_idx = 0;
parsed_json_t *json_all = &(parser_tx_obj->tx_json.json);
parsed_json_t *json_all = &(parser_tx_obj->json);

if (parser_getJsonValue(&meta_token_index, JSON_META) == parser_ok) {
object_get_element_count(json_all, meta_token_index, &meta_num_elements);
Expand Down
7 changes: 4 additions & 3 deletions app/src/parser_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,16 @@ extern "C" {
#define JSON_GAS_LIMIT "gasLimit"
#define JSON_GAS_PRICE "gasPrice"
#define JSON_SENDER "sender"

typedef struct {
const uint8_t *buffer;
uint16_t bufferLen;
uint16_t offset;
parser_tx_t *tx_obj;
tx_json_t *tx_obj;
} parser_context_t;

parser_error_t _read_json_tx(parser_context_t *c, parser_tx_t *v);
parser_tx_t *parser_getParserTxObj();
parser_error_t _read_json_tx(parser_context_t *c, tx_json_t *v);
tx_json_t *parser_getParserTxObj();
parser_error_t parser_initClistObject();
parser_error_t parser_getTransferFrom(char **from, uint16_t *from_len);
parser_error_t parser_getTransferTo(char **to, uint16_t *to_len);
Expand Down
5 changes: 0 additions & 5 deletions app/src/parser_txdef.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,6 @@ typedef struct
tx_query_t query;
}tx_json_t;

typedef struct {
// TODO : Remove this abstraction if no more fields are needed
tx_json_t tx_json;
} parser_tx_t;

#ifdef __cplusplus
}
#endif
2 changes: 1 addition & 1 deletion fuzz/parser_parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ char PARSER_VALUE[16384];
} // namespace

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
parser_tx_t txObj;
tx_json_t txObj;
MEMZERO(&txObj, sizeof(txObj));
parser_context_t ctx;
parser_error_t rc;
Expand Down
62 changes: 0 additions & 62 deletions tests/parser_impl.cpp

This file was deleted.

2 changes: 1 addition & 1 deletion tests/ui_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ void check_testcase(const testcase_t &tc, bool expert_mode) {
uint8_t buffer[5000];
uint16_t bufferLen = parseHexString(buffer, sizeof(buffer), tc.blob.c_str());

parser_tx_t tx_obj;
tx_json_t tx_obj;
memset(&tx_obj, 0, sizeof(tx_obj));

err = parser_parse(&ctx, buffer, bufferLen, &tx_obj);
Expand Down

1 comment on commit eb61190

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cpp-Linter Report ⚠️

Some files did not pass the configured checks!

clang-format reports: 15 file(s) not formatted
  • app/src/items_format.c
  • app/src/parser_impl.c
  • app/src/items.c
  • app/src/parser.c
  • app/src/jsmn/jsmn.c
  • app/src/json/json_parser.c
  • app/src/items_defs.h
  • app/src/coin.h
  • app/src/items_format.h
  • app/src/items.h
  • app/src/parser_txdef.h
  • app/src/parser_impl.h
  • app/src/jsmn/jsmn.h
  • app/src/common/parser_common.h
  • app/src/json/json_parser.h

Have any feedback or feature suggestions? Share it here.

Please sign in to comment.