Skip to content

Commit

Permalink
Crosschain transfers and some corner case handling
Browse files Browse the repository at this point in the history
0xPxt committed Aug 14, 2024
1 parent 1650152 commit 6ef14c7
Showing 7 changed files with 946 additions and 113 deletions.
1 change: 1 addition & 0 deletions app/src/common/parser_common.h
Original file line number Diff line number Diff line change
@@ -60,6 +60,7 @@ typedef enum {
parser_json_too_many_tokens, // "NOMEM: JSON string contains too many tokens"
parser_json_incomplete_json, // "JSON string is not complete";
parser_json_not_a_transfer,
parser_invalid_meta_field,
parser_json_unexpected_error,
} parser_error_t;

353 changes: 257 additions & 96 deletions app/src/items.c

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion app/src/items.h
Original file line number Diff line number Diff line change
@@ -23,14 +23,20 @@
typedef struct {
char key[25];
uint16_t json_token_index;
parser_error_t (*toString)(uint16_t token_index, char *outVal, uint16_t *outValLen);
bool can_display;
} item_t;

typedef struct {
item_t items[20];
uint8_t numOfItems;
parser_error_t (*toString[20])(item_t item, char *outVal, uint16_t *outValLen);
} item_array_t;

typedef enum {
items_ok,
items_error,
} items_error_t;

void items_initItems();
void items_storeItems();
uint16_t items_getTotalItems();
2 changes: 1 addition & 1 deletion app/src/parser.c
Original file line number Diff line number Diff line change
@@ -107,7 +107,7 @@ parser_error_t parser_getItem(const parser_context_t *ctx, uint8_t displayIdx, c
cleanOutput(outKey, outKeyLen, outVal, outValLen);

snprintf(outKey, outKeyLen, "%s", item_array->items[displayIdx].key);
item_array->items[displayIdx].toString(item_array->items[displayIdx].json_token_index, outVal, &outValLen);
item_array->toString[displayIdx](item_array->items[displayIdx], outVal, &outValLen);

return parser_ok;
}
44 changes: 37 additions & 7 deletions app/src/parser_impl.c
Original file line number Diff line number Diff line change
@@ -68,8 +68,13 @@ parser_error_t parser_findKeyInClist(uint16_t key_token_index) {
CHECK_ERROR(array_get_element_count(json_all, args_token_index, &number_of_args));
for (uint16_t j = 0; j < number_of_args; j++) {
array_get_nth_element(json_all, args_token_index, j, &token_index);
uint8_t offset = 0;
// Take into account the "k:" notation for the key
if (MEMCMP("k:", json_all->buffer + json_all->tokens[token_index].start, 2) == 0) {
offset = 2;
}
if (MEMCMP(json_all->buffer + json_all->tokens[key_token_index].start,
json_all->buffer + json_all->tokens[token_index].start,
json_all->buffer + json_all->tokens[token_index].start + offset,
json_all->tokens[key_token_index].end - json_all->tokens[key_token_index].start) == 0) {
return parser_ok;
}
@@ -79,14 +84,13 @@ parser_error_t parser_findKeyInClist(uint16_t key_token_index) {
return parser_no_data;
}


parser_error_t parser_getJsonValue(uint16_t *json_token_index, const char *key) {
parsed_json_t json_obj;
uint16_t token_index = 0;

CHECK_ERROR(object_get_value(&parser_tx_obj.tx_json.json, *json_token_index, key, &token_index));

json_parse(&json_obj, parser_tx_obj.tx_json.json.buffer + parser_tx_obj.tx_json.json.tokens[token_index].start, parser_tx_obj.tx_json.json.tokens[token_index].end - parser_tx_obj.tx_json.json.tokens[token_index].start);
CHECK_ERROR(json_parse(&json_obj, parser_tx_obj.tx_json.json.buffer + parser_tx_obj.tx_json.json.tokens[token_index].start, parser_tx_obj.tx_json.json.tokens[token_index].end - parser_tx_obj.tx_json.json.tokens[token_index].start));

if (MEMCMP("null", json_obj.buffer, json_obj.bufferLen) == 0) {
return parser_no_data;
@@ -116,10 +120,36 @@ parser_error_t parser_getGasObject(uint16_t *json_token_index) {
return parser_no_data;
}

parser_error_t parser_getChainId(parsed_json_t *json_obj) {
uint16_t token_index = 0;
object_get_value(&parser_tx_obj.tx_json.json, 0, "chainId", &token_index);
json_parse(json_obj, parser_tx_obj.tx_json.json.buffer + parser_tx_obj.tx_json.json.tokens[token_index].start, parser_tx_obj.tx_json.json.tokens[token_index].end - parser_tx_obj.tx_json.json.tokens[token_index].start);
parser_error_t parser_validateMetaField() {
char *keywords[20] = {
JSON_CREATION_TIME,
JSON_TTL,
JSON_GAS_LIMIT,
JSON_CHAIN_ID,
JSON_GAS_PRICE,
JSON_SENDER
};
char meta_curr_key[20];
uint16_t meta_token_index = 0;
uint16_t meta_num_elements = 0;
uint16_t key_token_idx = 0;

if (parser_getJsonValue(&meta_token_index, JSON_META) == parser_ok) {
object_get_element_count(&parser_tx_obj.tx_json.json, meta_token_index, &meta_num_elements);
for (uint16_t i = 0; i < meta_num_elements; i++) {
object_get_nth_key(&parser_tx_obj.tx_json.json, meta_token_index, i, &key_token_idx);

MEMCPY(meta_curr_key, parser_tx_obj.tx_json.json.buffer + parser_tx_obj.tx_json.json.tokens[key_token_idx].start,
parser_tx_obj.tx_json.json.tokens[key_token_idx].end - parser_tx_obj.tx_json.json.tokens[key_token_idx].start);
meta_curr_key[parser_tx_obj.tx_json.json.tokens[key_token_idx].end - parser_tx_obj.tx_json.json.tokens[key_token_idx].start] = '\0';

if (strcmp(keywords[i], meta_curr_key) != 0) {
return parser_invalid_meta_field;
}

MEMZERO(meta_curr_key, sizeof(meta_curr_key));
}
}

return parser_ok;
}
13 changes: 8 additions & 5 deletions app/src/parser_impl.h
Original file line number Diff line number Diff line change
@@ -27,14 +27,17 @@ extern "C" {

#define JSON_NETWORK_ID "networkId"
#define JSON_META "meta"
#define JSON_SENDER "sender"
#define JSON_CHAIN_ID "chainId"
#define JSON_GAS_LIMIT "gasLimit"
#define JSON_GAS_PRICE "gasPrice"
#define JSON_SIGNERS "signers"
#define JSON_PUBKEY "pubKey"
#define JSON_CLIST "clist"
#define JSON_ARGS "args"
#define JSON_NAME "name"
#define JSON_CREATION_TIME "creationTime"
#define JSON_TTL "ttl"
#define JSON_CHAIN_ID "chainId"
#define JSON_GAS_LIMIT "gasLimit"
#define JSON_GAS_PRICE "gasPrice"
#define JSON_SENDER "sender"
typedef struct {
const uint8_t *buffer;
uint16_t bufferLen;
@@ -53,7 +56,7 @@ parser_error_t parser_findKeyInClist(uint16_t key_token_index);
uint16_t parser_getNumberOfTransfers();
parser_error_t parser_getJsonValue(uint16_t *json_token_index, const char *key);
parser_error_t parser_getGasObject(uint16_t *json_token_index);
parser_error_t parser_getChainId(parsed_json_t *json_obj);
parser_error_t parser_validateMetaField();

#ifdef __cplusplus
}
638 changes: 635 additions & 3 deletions tests/testcases.json

Large diffs are not rendered by default.

1 comment on commit 6ef14c7

@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: 12 file(s) not formatted
  • app/src/coin.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/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.