Skip to content

Commit

Permalink
transfer.rotate + raise warnings when no clist
Browse files Browse the repository at this point in the history
  • Loading branch information
0xPxt committed Aug 13, 2024
1 parent e033635 commit 1650152
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 33 deletions.
90 changes: 88 additions & 2 deletions app/src/items.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@

static parser_error_t items_stdToDisplayString(uint16_t token_index, char *outVal, uint16_t *outValLen);
static parser_error_t items_nothingToDisplayString(__Z_UNUSED uint16_t token_index, char *outVal, uint16_t *outValLen);
static parser_error_t items_warningToDisplayString(__Z_UNUSED uint16_t token_index, char *outVal, uint16_t *outValLen);
static parser_error_t items_signingToDisplayString(uint16_t token_index, char *outVal, uint16_t *outValLen);
static parser_error_t items_requiringToDisplayString(uint16_t token_index, char *outVal, uint16_t *outValLen);
static parser_error_t items_transferToDisplayString(uint16_t token_index, char *outVal, uint16_t *outValLen);
static parser_error_t items_rotateToDisplayString(uint16_t token_index_json, char *outVal, uint16_t *outValLen);
static parser_error_t items_gasToDisplayString(uint16_t token_index, char *outVal, uint16_t *outValLen);
static parser_error_t items_hashToDisplayString(uint16_t token_index, char *outVal, uint16_t *outValLen);
static parser_error_t items_unknownCapabilityToDisplayString(uint16_t token_index, char *outVal, uint16_t *outValLen);
static void items_storeGasItem(uint16_t json_token_index, uint8_t items_idx, uint8_t *unknown_capabitilies);
static void items_storeTransferItem(parsed_json_t *json_all, uint16_t transfer_token_index, uint8_t items_idx, uint8_t *num_of_transfers, uint8_t *unknown_capabitilies);
static void items_storeRotateItem(parsed_json_t *json_all, uint16_t transfer_token_index, uint8_t items_idx, uint8_t *unknown_capabitilies);

#define CURR_ITEM_TOKEN item_array.items[items_idx].json_token_index

Expand Down Expand Up @@ -56,6 +59,7 @@ void items_storeItems() {
uint8_t unknown_capabitilies = 1;
uint8_t num_of_transfers = 1;
uint16_t token_index = 0;
bool unscoped_signer = false;

strcpy(item_array.items[items_idx].key, "Signing");
item_array.items[items_idx].toString = items_signingToDisplayString;
Expand All @@ -80,7 +84,39 @@ void items_storeItems() {
}
}

// TODO : Cleanup
if (parser_getJsonValue(&CURR_ITEM_TOKEN, JSON_SIGNERS) == parser_ok) {
array_get_nth_element(&json_all, CURR_ITEM_TOKEN, 0, &CURR_ITEM_TOKEN);
if (parser_getJsonValue(&CURR_ITEM_TOKEN, JSON_CLIST) == parser_ok) {
uint16_t clist_token_index = CURR_ITEM_TOKEN;
for (uint8_t i = 0; i < parser_getNumberOfClistElements(); 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) {
if (MEMCMP("coin.TRANSFER", 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) {
if (parser_findKeyInClist(item_array.items[items_idx - 1].json_token_index) == parser_no_data) {
unscoped_signer = true;
break;
}
}
}
}
}
} else {
// No Clist given
unscoped_signer = true;
}
}

if (unscoped_signer) {
strcpy(item_array.items[items_idx].key, "Unscoped Signer");
CURR_ITEM_TOKEN = item_array.items[items_idx - 1].json_token_index;
item_array.items[items_idx].toString = items_stdToDisplayString;
items_idx++;
}

CURR_ITEM_TOKEN = 0;
if (parser_getJsonValue(&CURR_ITEM_TOKEN, JSON_SIGNERS) == parser_ok) {
array_get_nth_element(&json_all, CURR_ITEM_TOKEN, 0, &CURR_ITEM_TOKEN);
if (parser_getJsonValue(&CURR_ITEM_TOKEN, JSON_CLIST) == parser_ok) {
Expand All @@ -90,26 +126,38 @@ void items_storeItems() {
}
}

CURR_ITEM_TOKEN = 0;
if (parser_getJsonValue(&CURR_ITEM_TOKEN, JSON_SIGNERS) == parser_ok) {
array_get_nth_element(&json_all, CURR_ITEM_TOKEN, 0, &CURR_ITEM_TOKEN);
if (parser_getJsonValue(&CURR_ITEM_TOKEN, JSON_CLIST) == parser_ok) {
uint16_t clist_token_index = CURR_ITEM_TOKEN;
for (uint8_t i = 0; i < parser_getNumberOfClistElements(); 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, "name", &name_token_index) == parser_ok) {
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,
json_all.tokens[name_token_index].end - json_all.tokens[name_token_index].start) == 0) {
CURR_ITEM_TOKEN = token_index;
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,
json_all.tokens[name_token_index].end - json_all.tokens[name_token_index].start) == 0) {
CURR_ITEM_TOKEN = token_index;
items_storeRotateItem(&json_all, token_index, items_idx, &unknown_capabitilies);
items_idx++;
}
}
}
}
} else {
// No Clist given, Raise warning
strcpy(item_array.items[items_idx].key, "WARNING");
item_array.items[items_idx].toString = items_warningToDisplayString;
items_idx++;
}
}

CURR_ITEM_TOKEN = 0;
if (parser_getJsonValue(&CURR_ITEM_TOKEN, JSON_META) == parser_ok) {
if (parser_getJsonValue(&CURR_ITEM_TOKEN, JSON_CHAIN_ID) == parser_ok) {
strcpy(item_array.items[items_idx].key, "On Chain");
Expand All @@ -118,6 +166,7 @@ void items_storeItems() {
}
}

CURR_ITEM_TOKEN = 0;
if (parser_getJsonValue(&CURR_ITEM_TOKEN, JSON_META) == parser_ok) {
strcpy(item_array.items[items_idx].key, "Using Gas");
item_array.items[items_idx].toString = items_gasToDisplayString;
Expand Down Expand Up @@ -198,6 +247,24 @@ static void items_storeTransferItem(parsed_json_t *json_all, uint16_t transfer_t
}
}

static void items_storeRotateItem(parsed_json_t *json_all, uint16_t transfer_token_index, uint8_t items_idx, uint8_t *unknown_capabitilies) {
uint16_t token_index = 0;
uint16_t num_of_args = 0;

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

array_get_element_count(json_all, token_index, &num_of_args);

if (num_of_args == 1) {
snprintf(item_array.items[items_idx].key, sizeof(item_array.items[items_idx].key), "Rotate for account");
item_array.items[items_idx].toString = items_rotateToDisplayString;
} else {
snprintf(item_array.items[items_idx].key, sizeof(item_array.items[items_idx].key), "Unknown Capability %d", *unknown_capabitilies);
(*unknown_capabitilies)++;
item_array.items[items_idx].toString = items_unknownCapabilityToDisplayString;
}
}

static parser_error_t items_stdToDisplayString(uint16_t token_index, char *outVal, uint16_t *outValLen) {
parsed_json_t json_all = parser_getParserTxObj()->tx_json.json;

Expand All @@ -213,6 +280,12 @@ static parser_error_t items_nothingToDisplayString(__Z_UNUSED uint16_t token_ind
return parser_ok;
}

static parser_error_t items_warningToDisplayString(__Z_UNUSED uint16_t token_index, char *outVal, uint16_t *outValLen) {
*outValLen = sizeof("UNSAFE TRANSACTION. This transaction's code was not recognized and does not limit capabilities for all signers. Signing this transaction may make arbitrary actions on the chain including loss of all funds.");
snprintf(outVal, *outValLen, "UNSAFE TRANSACTION. This transaction's code was not recognized and does not limit capabilities for all signers. Signing this transaction may make arbitrary actions on the chain including loss of all funds.");
return parser_ok;
}

static parser_error_t items_signingToDisplayString(__Z_UNUSED uint16_t token_index, char *outVal, uint16_t *outValLen) {
*outValLen = sizeof("Transaction");
snprintf(outVal, *outValLen, "Transaction");
Expand All @@ -225,7 +298,7 @@ static parser_error_t items_requiringToDisplayString(__Z_UNUSED uint16_t token_i
return parser_ok;
}

static parser_error_t items_transferToDisplayString(__Z_UNUSED uint16_t token_index_json, char *outVal, uint16_t *outValLen) {
static parser_error_t items_transferToDisplayString(uint16_t token_index_json, char *outVal, uint16_t *outValLen) {
char amount[50];
uint8_t amount_len = 0;
char to[65];
Expand Down Expand Up @@ -258,6 +331,19 @@ static parser_error_t items_transferToDisplayString(__Z_UNUSED uint16_t token_in
return parser_ok;
}

static parser_error_t items_rotateToDisplayString(uint16_t token_index_json, char *outVal, uint16_t *outValLen) {
uint16_t token_index = 0;
parsed_json_t json_all = parser_getParserTxObj()->tx_json.json;

object_get_value(&json_all, token_index_json, "args", &token_index);
array_get_nth_element(&json_all, token_index, 0, &token_index);

*outValLen = json_all.tokens[token_index].end - json_all.tokens[token_index].start + sizeof("\"\"");
snprintf(outVal, *outValLen, "\"%s\"", json_all.buffer + json_all.tokens[token_index].start);

return parser_ok;
}

static parser_error_t items_gasToDisplayString(uint16_t token_index, char *outVal, uint16_t *outValLen) {
char gasLimit[10];
uint8_t gasLimit_len = 0;
Expand Down
58 changes: 31 additions & 27 deletions app/src/parser_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,6 @@ parser_tx_t *parser_getParserTxObj() {
return &parser_tx_obj;
}

parser_error_t parser_isTransfer(parsed_json_t *json_obj) {
uint16_t name_token_idx = 0;
parser_error_t ret = parser_json_not_a_transfer;

object_get_value(json_obj, 0, "name", &name_token_idx);

if (((uint16_t) strlen("coin.TRANSFER")) == (json_obj->tokens[name_token_idx].end - json_obj->tokens[name_token_idx].start)) {
if (MEMCMP("coin.TRANSFER",
json_obj->buffer + json_obj->tokens[name_token_idx].start,
json_obj->tokens[name_token_idx].end - json_obj->tokens[name_token_idx].start) == 0) {
ret = parser_ok;
}
}

return ret;
}

uint16_t parser_getNumberOfClistElements() {
uint16_t number_of_elements = 0;
parsed_json_t *json_all = &parser_tx_obj.tx_json.json;
Expand All @@ -68,11 +51,40 @@ uint16_t parser_getNumberOfClistElements() {
return number_of_elements;
}

parser_error_t parser_findKeyInClist(uint16_t key_token_index) {
parsed_json_t *json_all = &parser_tx_obj.tx_json.json;
uint16_t token_index = 0;
uint16_t clist_token_index = 0;
uint16_t args_token_index = 0;
uint16_t number_of_args = 0;

parser_getJsonValue(&clist_token_index, JSON_SIGNERS);
array_get_nth_element(&json_all, clist_token_index, 0, &clist_token_index);
parser_getJsonValue(&clist_token_index, JSON_CLIST);

for (uint16_t i = 0; i < parser_getNumberOfClistElements(); i++) {
CHECK_ERROR(array_get_nth_element(json_all, clist_token_index, i, &args_token_index));
CHECK_ERROR(parser_getJsonValue(&args_token_index, JSON_ARGS));
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);
if (MEMCMP(json_all->buffer + json_all->tokens[key_token_index].start,
json_all->buffer + json_all->tokens[token_index].start,
json_all->tokens[key_token_index].end - json_all->tokens[key_token_index].start) == 0) {
return parser_ok;
}
}
}

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;

object_get_value(&parser_tx_obj.tx_json.json, *json_token_index, key, &token_index);
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);

Expand All @@ -85,14 +97,6 @@ parser_error_t parser_getJsonValue(uint16_t *json_token_index, const char *key)
return parser_ok;
}

parser_error_t parser_getNthClistElement(parsed_json_t *json_obj, uint8_t clist_array_idx) {
uint16_t token_index = 0;
array_get_nth_element(json_obj, 0, clist_array_idx, &token_index);
json_parse(json_obj, json_obj->buffer + json_obj->tokens[token_index].start, json_obj->tokens[token_index].end - json_obj->tokens[token_index].start);

return parser_ok;
}

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;
Expand All @@ -101,7 +105,7 @@ parser_error_t parser_getGasObject(uint16_t *json_token_index) {
for (uint16_t i = 0; i < parser_getNumberOfClistElements(); i++) {
array_get_nth_element(json_all, *json_token_index, i, &token_index);

object_get_value(json_all, token_index, "name", &name_token_index);
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 Down
7 changes: 3 additions & 4 deletions app/src/parser_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ extern "C" {
#define JSON_GAS_PRICE "gasPrice"
#define JSON_SIGNERS "signers"
#define JSON_CLIST "clist"

#define JSON_ARGS "args"
#define JSON_NAME "name"
typedef struct {
const uint8_t *buffer;
uint16_t bufferLen;
Expand All @@ -44,15 +45,13 @@ typedef struct {
parser_error_t _read_json_tx(parser_context_t *c, parser_tx_t *v);
parser_tx_t *parser_getParserTxObj();
parser_error_t parser_initClistObject();
parser_error_t parser_initTransfer();
parser_error_t parser_isTransfer(parsed_json_t *json_obj);
parser_error_t parser_getTransferFrom(char **from, uint16_t *from_len);
parser_error_t parser_getTransferTo(char **to, uint16_t *to_len);
parser_error_t parser_getTransferAmount(char **amount, uint16_t *amount_len);
uint16_t parser_getNumberOfClistElements();
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_getNthClistElement(parsed_json_t *json_obj, uint8_t clist_array_idx);
parser_error_t parser_getGasObject(uint16_t *json_token_index);
parser_error_t parser_getChainId(parsed_json_t *json_obj);

Expand Down

0 comments on commit 1650152

Please sign in to comment.