Skip to content

Commit

Permalink
Error checking
Browse files Browse the repository at this point in the history
  • Loading branch information
0xPxt committed Aug 19, 2024
1 parent 9c0d73d commit 363e499
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 42 deletions.
49 changes: 25 additions & 24 deletions app/src/items.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,9 @@ item_array_t *items_getItemArray() {
}

void items_storeItems() {
parsed_json_t json_all = parser_getParserTxObj()->tx_json.json;
uint8_t items_idx = 0;
uint8_t unknown_capabitilies = 1;
uint16_t token_index = 0;
parsed_json_t json_all = parser_getParserTxObj()->tx_json.json;

items_storeSigningTransaction(&items_idx);

Expand Down Expand Up @@ -108,7 +107,9 @@ static items_error_t items_storeSigningTransaction(uint8_t *items_idx) {
}

static items_error_t items_storeNetwork(uint8_t *items_idx) {
if (parser_getJsonValue(&item_array.items[*items_idx].json_token_index, JSON_NETWORK_ID) == parser_ok) {
uint16_t *curr_token_idx = &item_array.items[*items_idx].json_token_index;

if (parser_getJsonValue(curr_token_idx, JSON_NETWORK_ID) == parser_ok) {
strcpy(item_array.items[*items_idx].key, "On Network");
item_array.toString[*items_idx] = items_stdToDisplayString;
(*items_idx)++;
Expand All @@ -126,11 +127,11 @@ 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()->tx_json.json);
uint16_t *curr_token_idx = &item_array.items[*items_idx].json_token_index;

if (parser_getJsonValue(curr_token_idx, JSON_SIGNERS) == parser_ok) {
array_get_nth_element(&json_all, *curr_token_idx, 0, curr_token_idx);
array_get_nth_element(json_all, *curr_token_idx, 0, curr_token_idx);
if (parser_getJsonValue(curr_token_idx, JSON_PUBKEY) == parser_ok) {
strcpy(item_array.items[*items_idx].key, "Of Key");
item_array.toString[*items_idx] = items_stdToDisplayString;
Expand All @@ -142,20 +143,20 @@ 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()->tx_json.json);
uint16_t *curr_token_idx = &item_array.items[*items_idx].json_token_index;
uint16_t token_index = 0;

if (parser_getJsonValue(curr_token_idx, JSON_SIGNERS) == parser_ok) {
array_get_nth_element(&json_all, *curr_token_idx, 0, curr_token_idx);
array_get_nth_element(json_all, *curr_token_idx, 0, curr_token_idx);
if (parser_getJsonValue(curr_token_idx, JSON_CLIST) == parser_ok) {
uint16_t clist_token_index = *curr_token_idx;

for (uint8_t i = 0; i < parser_getNumberOfClistElements(); i++) {
if (array_get_nth_element(&json_all, clist_token_index, i, &token_index) == parser_ok) {
if (array_get_nth_element(json_all, clist_token_index, i, &token_index) == parser_ok) {
uint16_t name_token_index = 0;
if (object_get_value(&json_all, token_index, JSON_NAME, &name_token_index) == parser_ok) {
if (MEMCMP("coin.TRANSFER", json_all.buffer + json_all.tokens[name_token_index].start,
if (object_get_value(json_all, token_index, JSON_NAME, &name_token_index) == parser_ok) {
if (MEMCMP("coin.TRANSFER", json_all->buffer + json_all->tokens[name_token_index].start,
sizeof("coin.TRANSFER") - 1) == 0) {
if (parser_findPubKeyInClist(item_array.items[*items_idx - 1].json_token_index) == parser_ok) {
item_array.items[*items_idx].json_token_index = 0;
Expand All @@ -180,11 +181,11 @@ 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()->tx_json.json);
uint16_t *curr_token_idx = &item_array.items[*items_idx].json_token_index;

if (parser_getJsonValue(curr_token_idx, JSON_SIGNERS) == parser_ok) {
array_get_nth_element(&json_all, *curr_token_idx, 0, curr_token_idx);
array_get_nth_element(json_all, *curr_token_idx, 0, curr_token_idx);
if (parser_getJsonValue(curr_token_idx, JSON_CLIST) == parser_ok) {
parser_getGasObject(curr_token_idx);
items_storeGasItem(*curr_token_idx, *items_idx, unknown_capabitilies);
Expand All @@ -198,39 +199,39 @@ 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()->tx_json.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;

if (parser_getJsonValue(curr_token_idx, JSON_SIGNERS) == parser_ok) {
array_get_nth_element(&json_all, *curr_token_idx, 0, curr_token_idx);
array_get_nth_element(json_all, *curr_token_idx, 0, curr_token_idx);
if (parser_getJsonValue(curr_token_idx, JSON_CLIST) == parser_ok) {
uint16_t clist_token_index = *curr_token_idx;
for (uint8_t i = 0; i < parser_getNumberOfClistElements(); i++) {
if (array_get_nth_element(&json_all, clist_token_index, i, &token_index) == parser_ok) {
if (array_get_nth_element(json_all, clist_token_index, i, &token_index) == parser_ok) {
uint16_t name_token_index = 0;
if (object_get_value(&json_all, token_index, JSON_NAME, &name_token_index) == parser_ok) {
if (MEMCMP("coin.TRANSFER_XCHAIN", json_all.buffer + json_all.tokens[name_token_index].start,
if (object_get_value(json_all, token_index, JSON_NAME, &name_token_index) == parser_ok) {
if (MEMCMP("coin.TRANSFER_XCHAIN", json_all->buffer + json_all->tokens[name_token_index].start,
sizeof("coin.TRANSFER_XCHAIN") - 1) == 0) {
*curr_token_idx = token_index;
items_storeCrossTransferItem(&json_all, token_index, *items_idx, &num_of_transfers, unknown_capabitilies);
items_storeCrossTransferItem(json_all, token_index, *items_idx, &num_of_transfers, unknown_capabitilies);
(*items_idx)++;
} else if (MEMCMP("coin.TRANSFER", json_all.buffer + json_all.tokens[name_token_index].start,
} else if (MEMCMP("coin.TRANSFER", json_all->buffer + json_all->tokens[name_token_index].start,
sizeof("coin.TRANSFER") - 1) == 0) {
*curr_token_idx = token_index;
items_storeTransferItem(&json_all, token_index, *items_idx, &num_of_transfers, unknown_capabitilies);
items_storeTransferItem(json_all, token_index, *items_idx, &num_of_transfers, unknown_capabitilies);
(*items_idx)++;
} else if (MEMCMP("coin.ROTATE", json_all.buffer + json_all.tokens[name_token_index].start,
} else if (MEMCMP("coin.ROTATE", json_all->buffer + json_all->tokens[name_token_index].start,
sizeof("coin.ROTATE") - 1) == 0) {
*curr_token_idx = token_index;
items_storeRotateItem(&json_all, token_index, *items_idx, unknown_capabitilies);
items_storeRotateItem(json_all, token_index, *items_idx, unknown_capabitilies);
(*items_idx)++;
} else if (MEMCMP("coin.GAS", json_all.buffer + json_all.tokens[name_token_index].start,
} else if (MEMCMP("coin.GAS", json_all->buffer + json_all->tokens[name_token_index].start,
sizeof("coin.GAS") - 1) != 0) {
// Any other case that's not coin.GAS
*curr_token_idx = token_index;
items_storeUnknownItem(&json_all, token_index, *items_idx, unknown_capabitilies);
items_storeUnknownItem(json_all, token_index, *items_idx, unknown_capabitilies);
item_array.toString[*items_idx] = items_unknownCapabilityToDisplayString;
(*items_idx)++;
}
Expand Down
20 changes: 18 additions & 2 deletions app/src/items_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@
#include <zxmacros.h>
#include "zxtypes.h"

#define MAX_NUMBER_OF_ITEMS 25

#define PARSER_TO_ITEMS_ERROR(__CALL) \
{ \
parser_error_t __err = __CALL; \
CHECK_APP_CANARY() \
if (__err != parser_ok) return items_error; \
}

#define ITEMS_TO_PARSER_ERROR(__CALL) \
{ \
items_error_t __err = __CALL; \
CHECK_APP_CANARY() \
if (__err != items_ok) return parser_unexpected_error; \
}

typedef struct {
char key[25];
uint16_t json_token_index;
Expand All @@ -30,7 +46,7 @@ typedef enum {
} items_error_t;

typedef struct {
item_t items[20];
item_t items[MAX_NUMBER_OF_ITEMS];
uint8_t numOfItems;
items_error_t (*toString[20])(item_t item, char *outVal, uint16_t *outValLen);
items_error_t (*toString[MAX_NUMBER_OF_ITEMS])(item_t item, char *outVal, uint16_t *outValLen);
} item_array_t;
16 changes: 8 additions & 8 deletions app/src/items_format.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ items_error_t items_transferToDisplayString(item_t item, char *outVal, uint16_t

object_get_value(&json_all, item_token_index, "args", &token_index);

parser_arrayElementToString(token_index, 0, from, &from_len);
PARSER_TO_ITEMS_ERROR(parser_arrayElementToString(token_index, 0, from, &from_len));

parser_arrayElementToString(token_index, 1, to, &to_len);
PARSER_TO_ITEMS_ERROR(parser_arrayElementToString(token_index, 1, to, &to_len));

parser_arrayElementToString(token_index, 2, amount, &amount_len);
PARSER_TO_ITEMS_ERROR(parser_arrayElementToString(token_index, 2, amount, &amount_len));

*outValLen = amount_len + from_len + to_len + sizeof(" from ") + sizeof(" to ") + 4 * sizeof("\"");
snprintf(outVal, *outValLen, "%s from \"%s\" to \"%s\"", amount, from, to);
Expand All @@ -105,13 +105,13 @@ items_error_t items_crossTransferToDisplayString(item_t item, char *outVal, uint

object_get_value(&json_all, item_token_index, "args", &token_index);

parser_arrayElementToString(token_index, 0, from, &from_len);
PARSER_TO_ITEMS_ERROR(parser_arrayElementToString(token_index, 0, from, &from_len));

parser_arrayElementToString(token_index, 1, to, &to_len);
PARSER_TO_ITEMS_ERROR(parser_arrayElementToString(token_index, 1, to, &to_len));

parser_arrayElementToString(token_index, 2, amount, &amount_len);
PARSER_TO_ITEMS_ERROR(parser_arrayElementToString(token_index, 2, amount, &amount_len));

parser_arrayElementToString(token_index, 3, chain, &chain_len);
PARSER_TO_ITEMS_ERROR(parser_arrayElementToString(token_index, 3, chain, &chain_len));

*outValLen = amount_len + from_len + to_len + chain_len + sizeof("Cross-chain ") + sizeof(" from ") + sizeof(" to ") + 6 * sizeof("\"") + sizeof(" to chain ");
snprintf(outVal, *outValLen, "Cross-chain %s from \"%s\" to \"%s\" to chain \"%s\"", amount, from, to, chain);
Expand Down Expand Up @@ -157,7 +157,7 @@ items_error_t items_gasToDisplayString(__Z_UNUSED item_t item, char *outVal, uin
return items_ok;
}

items_error_t items_hashToDisplayString(item_t item, char *outVal, uint16_t *outValLen) {
items_error_t items_hashToDisplayString(__Z_UNUSED item_t item, char *outVal, uint16_t *outValLen) {
*outValLen = sizeof(base64_hash);
snprintf(outVal, *outValLen, "%s", base64_hash);
return items_ok;
Expand Down
9 changes: 5 additions & 4 deletions app/src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ parser_error_t parser_parse(parser_context_t *ctx, const uint8_t *data, size_t d
}

parser_error_t parser_validate(parser_context_t *ctx) {

// TODO: validate the tx (JSON validation)

// Iterate through all items to check that all can be shown and are valid
uint8_t numItems = 0;
CHECK_ERROR(parser_getNumItems(ctx, &numItems))
Expand Down Expand Up @@ -100,14 +97,18 @@ parser_error_t parser_getItem(const parser_context_t *ctx, uint8_t displayIdx, c
*pageCount = 1;
uint8_t numItems = 0;
item_array_t *item_array = items_getItemArray();
char tempVal[300] = {0};
uint16_t tempValLen = 0;
CHECK_ERROR(parser_getNumItems(ctx, &numItems))
CHECK_APP_CANARY()

CHECK_ERROR(checkSanity(numItems, displayIdx))
cleanOutput(outKey, outKeyLen, outVal, outValLen);

snprintf(outKey, outKeyLen, "%s", item_array->items[displayIdx].key);
item_array->toString[displayIdx](item_array->items[displayIdx], outVal, &outValLen);
ITEMS_TO_PARSER_ERROR(item_array->toString[displayIdx](item_array->items[displayIdx], tempVal, &tempValLen));
pageString(outVal, outValLen, tempVal, pageIdx, pageCount);


return parser_ok;
}
10 changes: 6 additions & 4 deletions app/src/parser_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,12 @@ parser_error_t parser_arrayElementToString(uint16_t json_token_index, uint16_t e
uint16_t token_index = 0;
parsed_json_t json_all = parser_tx_obj.tx_json.json;

array_get_nth_element(&json_all, json_token_index, element_idx, &token_index);
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);
*outValLen = json_all.tokens[token_index].end - json_all.tokens[token_index].start;
outVal[*outValLen] = '\0';

return parser_ok;
}

parser_error_t parser_getGasObject(uint16_t *json_token_index) {
Expand All @@ -117,9 +119,9 @@ parser_error_t parser_getGasObject(uint16_t *json_token_index) {
uint16_t name_token_index = 0;

for (uint16_t i = 0; i < parser_getNumberOfClistElements(); i++) {
array_get_nth_element(json_all, *json_token_index, i, &token_index);
CHECK_ERROR(array_get_nth_element(json_all, *json_token_index, i, &token_index));

object_get_value(json_all, token_index, JSON_NAME, &name_token_index);
CHECK_ERROR(object_get_value(json_all, token_index, JSON_NAME, &name_token_index));
if (MEMCMP("coin.GAS", json_all->buffer + json_all->tokens[name_token_index].start,
json_all->tokens[name_token_index].end - json_all->tokens[name_token_index].start) == 0) {
*json_token_index = token_index;
Expand All @@ -131,7 +133,7 @@ parser_error_t parser_getGasObject(uint16_t *json_token_index) {
}

parser_error_t parser_validateMetaField() {
char *keywords[20] = {
const char *keywords[20] = {
JSON_CREATION_TIME,
JSON_TTL,
JSON_GAS_LIMIT,
Expand Down

1 comment on commit 363e499

@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_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
  • 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

Have any feedback or feature suggestions? Share it here.

Please sign in to comment.