Skip to content

Commit

Permalink
Improve code readability
Browse files Browse the repository at this point in the history
  • Loading branch information
0xPxt committed Aug 26, 2024
1 parent e7e6e96 commit f3f2f8c
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 126 deletions.
4 changes: 4 additions & 0 deletions app/src/common/parser_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ typedef enum {
parser_json_not_a_transfer,
parser_invalid_meta_field,
parser_json_unexpected_error,
parser_name_tx_transfer,
parser_name_tx_transfer_xchain,
parser_name_rotate,
parser_name_gas,
} parser_error_t;

#ifdef __cplusplus
Expand Down
169 changes: 64 additions & 105 deletions app/src/items.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,52 +164,34 @@ static items_error_t items_validateSigners() {
item_t *item = &item_array.items[item_array.numOfItems];
item_t *ofKey_item = &item_array.items[item_array.numOfItems - 1];
uint16_t token_index = 0;
jsmntok_t *token;
uint16_t clist_element_count = 0;

PARSER_TO_ITEMS_ERROR(object_get_value(json_all, *curr_token_idx, JSON_SIGNERS, curr_token_idx));
if (parser_getValidClist(curr_token_idx, &clist_element_count) != parser_ok) {
strcpy(item->key, "Unscoped Signer");
*curr_token_idx = ofKey_item->json_token_index;
item_array.toString[item_array.numOfItems] = items_stdToDisplayString;
INCREMENT_NUM_ITEMS()
return items_ok;
}

if (!items_isNullField(*curr_token_idx)) {
array_get_nth_element(json_all, *curr_token_idx, 0, curr_token_idx);
if (object_get_value(json_all, *curr_token_idx, JSON_CLIST, curr_token_idx) == parser_ok) {
if (!items_isNullField(*curr_token_idx)) {
uint16_t clist_token_index = *curr_token_idx;
uint16_t clist_element_count = 0;
PARSER_TO_ITEMS_ERROR(array_get_element_count(json_all, clist_token_index, &clist_element_count));

for (uint8_t i = 0; i < (uint8_t)clist_element_count; i++) {
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) {
uint16_t len = 0;

token = &(json_all->tokens[name_token_index]);

len = token->end - token->start;

if (len == 0 || strlen("coin.TRANSFER") != len) continue;

if (MEMCMP("coin.TRANSFER", json_all->buffer + token->start, len) == 0) {
if (parser_findPubKeyInClist(ofKey_item->json_token_index) == parser_ok) {
*curr_token_idx = 0;
return items_ok;
}
break;
}
}
}
uint16_t clist_token_index = *curr_token_idx;
PARSER_TO_ITEMS_ERROR(array_get_element_count(json_all, clist_token_index, &clist_element_count));

for (uint8_t i = 0; i < (uint8_t)clist_element_count; i++) {
if (array_get_nth_element(json_all, clist_token_index, i, &token_index) == parser_ok) {
if (parser_getTxName(token_index) == parser_name_tx_transfer) {
if (parser_findPubKeyInClist(ofKey_item->json_token_index) != parser_ok) {
strcpy(item->key, "Unscoped Signer");
*curr_token_idx = ofKey_item->json_token_index;
item_array.toString[item_array.numOfItems] = items_stdToDisplayString;
INCREMENT_NUM_ITEMS()
return items_ok;
}
*curr_token_idx = 0;
return items_ok;
}
}
}

strcpy(item->key, "Unscoped Signer");
*curr_token_idx = ofKey_item->json_token_index;
item_array.toString[item_array.numOfItems] = items_stdToDisplayString;
INCREMENT_NUM_ITEMS()

// No transfer found
*curr_token_idx = 0;
return items_ok;
}

Expand Down Expand Up @@ -242,7 +224,6 @@ static items_error_t items_storePayingGas() {
if (MEMCMP("coin.GAS", json_all->buffer + token->start, token->end - token->start) == 0) {
*curr_token_idx = token_index;
items_storeGasItem(*curr_token_idx);
INCREMENT_NUM_ITEMS()
return items_ok;
}
}
Expand All @@ -258,74 +239,47 @@ static items_error_t items_storeAllTransfers() {
uint16_t *curr_token_idx = &item_array.items[item_array.numOfItems].json_token_index;
uint16_t token_index = 0;
uint8_t num_of_transfers = 1;
jsmntok_t *token;
item_t *item = &item_array.items[item_array.numOfItems];

PARSER_TO_ITEMS_ERROR(object_get_value(json_all, 0, JSON_SIGNERS, curr_token_idx));

if (!items_isNullField(*curr_token_idx)) {
array_get_nth_element(json_all, *curr_token_idx, 0, curr_token_idx);

if (object_get_value(json_all, *curr_token_idx, JSON_CLIST, curr_token_idx) == parser_ok) {
if (!items_isNullField(*curr_token_idx)) {
uint16_t clist_token_index = *curr_token_idx;
uint16_t clist_element_count = 0;

array_get_element_count(json_all, clist_token_index, &clist_element_count);

for (uint16_t i = 0; i < clist_element_count; i++) {
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) {
uint16_t len = 0;

token = &(json_all->tokens[name_token_index]);

len = token->end - token->start;

if (len == 0) continue;

if (strlen("coin.TRANSFER") == len &&
MEMCMP("coin.TRANSFER", json_all->buffer + token->start, len) == 0) {
*curr_token_idx = token_index;
items_storeTxItem(token_index, &num_of_transfers);
INCREMENT_NUM_ITEMS()
} else if (strlen("coin.TRANSFER_XCHAIN") == len &&
MEMCMP("coin.TRANSFER_XCHAIN", json_all->buffer + token->start, len) == 0) {
*curr_token_idx = token_index;
items_storeTxCrossItem(token_index, &num_of_transfers);
INCREMENT_NUM_ITEMS()
} else if (strlen("coin.ROTATE") == len &&
MEMCMP("coin.ROTATE", json_all->buffer + token->start, len) == 0) {
*curr_token_idx = token_index;
items_storeTxRotateItem(token_index);
INCREMENT_NUM_ITEMS()
} else if (strlen("coin.GAS") != len ||
MEMCMP("coin.GAS", json_all->buffer + token->start, len) != 0) {
// Any other case that's not coin.GAS
uint16_t args_token_index = 0;
uint16_t num_of_args = 0;
PARSER_TO_ITEMS_ERROR(object_get_value(json_all, token_index, "args", &args_token_index));
PARSER_TO_ITEMS_ERROR(array_get_element_count(json_all, args_token_index, &num_of_args));
*curr_token_idx = token_index;
items_storeUnknownItem(num_of_args, token_index);
INCREMENT_NUM_ITEMS()
}
curr_token_idx = &item_array.items[item_array.numOfItems].json_token_index;
}
}
uint16_t clist_token_index = 0;
uint16_t clist_element_count = 0;
uint16_t args_element_count = 0;

if (parser_getValidClist(&clist_token_index, &clist_element_count) == parser_ok) {
for (uint16_t i = 0; i < clist_element_count; i++) {
if (array_get_nth_element(json_all, clist_token_index, i, &token_index) == parser_ok) {
switch (parser_getTxName(token_index)) {
case parser_name_tx_transfer:
*curr_token_idx = token_index;
items_storeTxItem(token_index, &num_of_transfers);
break;
case parser_name_tx_transfer_xchain:
*curr_token_idx = token_index;
items_storeTxCrossItem(token_index, &num_of_transfers);
break;
case parser_name_rotate:
*curr_token_idx = token_index;
items_storeTxRotateItem(token_index);
break;
case parser_name_gas:
break;
default:
*curr_token_idx = token_index;
PARSER_TO_ITEMS_ERROR(object_get_value(json_all, token_index, JSON_ARGS, &token_index));
PARSER_TO_ITEMS_ERROR(array_get_element_count(json_all, token_index, &args_element_count));
items_storeUnknownItem(args_element_count, token_index);
break;
}
return items_ok;
}
curr_token_idx = &item_array.items[item_array.numOfItems].json_token_index;
}
} else {
// Non-existing/Null Signers or Clist
item_t *item = &item_array.items[item_array.numOfItems];
strcpy(item->key, "WARNING");
item_array.toString[item_array.numOfItems] = items_warningToDisplayString;
INCREMENT_NUM_ITEMS()
*curr_token_idx = 0;
}

// Non-existing/Null Signers or Clist
strcpy(item->key, "WARNING");
item_array.toString[item_array.numOfItems] = items_warningToDisplayString;
INCREMENT_NUM_ITEMS()
*curr_token_idx = 0;

return items_ok;
}

Expand Down Expand Up @@ -440,10 +394,10 @@ static items_error_t items_storeGasItem(uint16_t json_token_index) {

if (args_count > 0) {
items_storeUnknownItem(args_count, token_index);

} else {
strcpy(item->key, "Paying Gas");
item_array.toString[item_array.numOfItems] = items_nothingToDisplayString;
INCREMENT_NUM_ITEMS()
}

return items_ok;
Expand All @@ -463,6 +417,7 @@ static items_error_t items_storeTxItem(uint16_t transfer_token_index, uint8_t *n
snprintf(item->key, sizeof(item->key), "Transfer %d", *num_of_transfers);
(*num_of_transfers)++;
item_array.toString[item_array.numOfItems] = items_transferToDisplayString;
INCREMENT_NUM_ITEMS()
} else {
items_storeUnknownItem(num_of_args, token_index);
}
Expand All @@ -484,6 +439,7 @@ static items_error_t items_storeTxCrossItem(uint16_t transfer_token_index, uint8
snprintf(item->key, sizeof(item->key), "Transfer %d", *num_of_transfers);
(*num_of_transfers)++;
item_array.toString[item_array.numOfItems] = items_crossTransferToDisplayString;
INCREMENT_NUM_ITEMS()
} else {
items_storeUnknownItem(num_of_args, token_index);
}
Expand All @@ -504,6 +460,7 @@ static items_error_t items_storeTxRotateItem(uint16_t transfer_token_index) {
if (num_of_args == 1) {
snprintf(item->key, sizeof(item->key), "Rotate for account");
item_array.toString[item_array.numOfItems] = items_rotateToDisplayString;
INCREMENT_NUM_ITEMS()
} else {
items_storeUnknownItem(num_of_args, token_index);
}
Expand All @@ -524,5 +481,7 @@ static items_error_t items_storeUnknownItem(uint16_t num_of_args, uint16_t trans
item->can_display = bool_false;
}

INCREMENT_NUM_ITEMS()

return items_ok;
}
2 changes: 1 addition & 1 deletion app/src/items_format.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ items_error_t items_unknownCapabilityToDisplayString(item_t item, char *outVal,
jsmntok_t *token;
uint16_t len = 0;

PARSER_TO_ITEMS_ERROR(object_get_value(json_all, item_token_index, "name", &token_index));
PARSER_TO_ITEMS_ERROR(object_get_value(json_all, item_token_index, JSON_NAME, &token_index));
token = &(json_all->tokens[token_index]);

len = token->end - token->start;
Expand Down
77 changes: 57 additions & 20 deletions app/src/parser_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,7 @@ parser_error_t parser_findPubKeyInClist(uint16_t key_token_index) {
jsmntok_t *value_token;
jsmntok_t *key_token;

CHECK_ERROR(object_get_value(json_all, 0, JSON_SIGNERS, &clist_token_index));

if (items_isNullField(clist_token_index)) {
return parser_no_data;
}

array_get_nth_element(json_all, clist_token_index, 0, &clist_token_index);

CHECK_ERROR(object_get_value(json_all, clist_token_index, JSON_CLIST, &clist_token_index));

if (items_isNullField(clist_token_index)) {
if (parser_getValidClist(&clist_token_index, &clist_element_count) != parser_ok) {
return parser_no_data;
}

Expand Down Expand Up @@ -142,6 +132,62 @@ parser_error_t parser_validateMetaField() {
return parser_ok;
}

parser_error_t parser_getTxName(uint16_t token_index) {
parsed_json_t *json_all = &(parser_tx_obj->json);

if (object_get_value(json_all, token_index, JSON_NAME, &token_index) == parser_ok) {
uint16_t len = 0;
jsmntok_t *token;

token = &(json_all->tokens[token_index]);

len = token->end - token->start;

if (len == 0) return parser_no_data;

if (strlen("coin.TRANSFER") == len && MEMCMP("coin.TRANSFER", json_all->buffer + token->start, len) == 0) {
return parser_name_tx_transfer;
} else if (strlen("coin.TRANSFER_XCHAIN") == len &&
MEMCMP("coin.TRANSFER_XCHAIN", json_all->buffer + token->start, len) == 0) {
return parser_name_tx_transfer_xchain;
} else if (strlen("coin.ROTATE") == len && MEMCMP("coin.ROTATE", json_all->buffer + token->start, len) == 0) {
return parser_name_rotate;
} else if (strlen("coin.GAS") == len || MEMCMP("coin.GAS", json_all->buffer + token->start, len) == 0) {
return parser_name_gas;
}
}

return parser_no_data;
}

parser_error_t parser_getValidClist(uint16_t *clist_token_index, uint16_t *num_args) {
parsed_json_t *json_all = &(parser_tx_obj->json);

CHECK_ERROR(object_get_value(json_all, 0, JSON_SIGNERS, clist_token_index));

if (!items_isNullField(*clist_token_index)) {
CHECK_ERROR(array_get_nth_element(json_all, *clist_token_index, 0, clist_token_index));

if (object_get_value(json_all, *clist_token_index, JSON_CLIST, clist_token_index) == parser_ok) {
if (!items_isNullField(*clist_token_index)) {
CHECK_ERROR(array_get_element_count(json_all, *clist_token_index, num_args));
return parser_ok;
}
}
}

return parser_no_data;
}

bool items_isNullField(uint16_t json_token_index) {
parsed_json_t *json_all = &(parser_getParserTxObj()->json);
jsmntok_t *token = &(json_all->tokens[json_token_index]);

if (token->end - token->start != sizeof("null") - 1) return false;

return (MEMCMP("null", json_all->buffer + token->start, token->end - token->start) == 0);
}

const char *parser_getErrorDescription(parser_error_t err) {
switch (err) {
case parser_ok:
Expand Down Expand Up @@ -180,12 +226,3 @@ const char *parser_getErrorDescription(parser_error_t err) {
return "Unrecognized error code";
}
}

bool items_isNullField(uint16_t json_token_index) {
parsed_json_t *json_all = &(parser_getParserTxObj()->json);
jsmntok_t *token = &(json_all->tokens[json_token_index]);

if (token->end - token->start != sizeof("null") - 1) return false;

return (MEMCMP("null", json_all->buffer + token->start, token->end - token->start) == 0);
}
2 changes: 2 additions & 0 deletions app/src/parser_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ parser_error_t parser_findPubKeyInClist(uint16_t key_token_index);
parser_error_t parser_arrayElementToString(uint16_t json_token_index, uint16_t element_idx, const char **outVal,
uint8_t *outValLen);
parser_error_t parser_validateMetaField();
parser_error_t parser_getTxName(uint16_t token_index);
parser_error_t parser_getValidClist(uint16_t *clist_token_index, uint16_t *num_args);
bool items_isNullField(uint16_t json_token_index);

#ifdef __cplusplus
Expand Down

0 comments on commit f3f2f8c

Please sign in to comment.