Skip to content

Commit

Permalink
Modularize items files
Browse files Browse the repository at this point in the history
  • Loading branch information
0xPxt committed Aug 16, 2024
1 parent 86ccf9e commit 268ef65
Show file tree
Hide file tree
Showing 6 changed files with 321 additions and 258 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ file(GLOB_RECURSE LIB_SRC
####
${CMAKE_CURRENT_SOURCE_DIR}/app/src/parser.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/items.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/items_format.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/parser_impl.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/crypto_helper.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/json/json_parser.c
Expand Down
248 changes: 11 additions & 237 deletions app/src/items.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,10 @@
********************************************************************************/
#include "crypto_helper.h"
#include "items.h"
#include "items_format.h"
#include "parser_impl.h"
#include <base64.h>

static items_error_t items_stdToDisplayString(item_t item, char *outVal, uint16_t *outValLen);
static items_error_t items_nothingToDisplayString(__Z_UNUSED item_t item, char *outVal, uint16_t *outValLen);
static items_error_t items_warningToDisplayString(__Z_UNUSED item_t item, char *outVal, uint16_t *outValLen);
static items_error_t items_cautionToDisplayString(__Z_UNUSED item_t item, char *outVal, uint16_t *outValLen);
static items_error_t items_txTooLargeToDisplayString(__Z_UNUSED item_t item, char *outVal, uint16_t *outValLen);
static items_error_t items_signingToDisplayString(item_t item, char *outVal, uint16_t *outValLen);
static items_error_t items_requiringToDisplayString(item_t item, char *outVal, uint16_t *outValLen);
static items_error_t items_transferToDisplayString(item_t item, char *outVal, uint16_t *outValLen);
static items_error_t items_crossTransferToDisplayString(item_t item, char *outVal, uint16_t *outValLen);
static items_error_t items_rotateToDisplayString(item_t item, char *outVal, uint16_t *outValLen);
static items_error_t items_gasToDisplayString(item_t item, char *outVal, uint16_t *outValLen);
static items_error_t items_hashToDisplayString(item_t item, char *outVal, uint16_t *outValLen);
static items_error_t items_unknownCapabilityToDisplayString(item_t item, char *outVal, uint16_t *outValLen);
static items_error_t items_storeSigningTransaction(uint8_t *items_idx);
static items_error_t items_storeNetwork(uint8_t *items_idx);
static items_error_t items_storeRequiringCapabilities(uint8_t *items_idx);
Expand All @@ -51,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 CURR_ITEM_TOKEN item_array.items[items_idx].json_token_index
#define MAX_ITEM_LENGTH_TO_DISPLAY 1000 // TODO : Check other apps to find this number

item_array_t item_array;

Expand All @@ -62,7 +50,7 @@ void items_initItems() {
MEMZERO(&item_array, sizeof(item_array_t));

for (uint8_t i = 0; i < sizeof(item_array.items) / sizeof(item_array.items[0]); i++) {
item_array.items[i].can_display = true;
item_array.items[i].can_display = bool_true;
}
}

Expand Down Expand Up @@ -387,8 +375,8 @@ static items_error_t items_storeTransferItem(parsed_json_t *json_all, uint16_t t
(*unknown_capabitilies)++;
item_array.toString[items_idx] = items_unknownCapabilityToDisplayString;

if (num_of_args > 5) {
item_array.items[items_idx].can_display = false;
if (num_of_args > 5 || json_all->tokens[token_index].end - json_all->tokens[token_index].start > MAX_ITEM_LENGTH_TO_DISPLAY) {
item_array.items[items_idx].can_display = bool_false;
}
}

Expand All @@ -412,8 +400,8 @@ static items_error_t items_storeCrossTransferItem(parsed_json_t *json_all, uint1
(*unknown_capabitilies)++;
item_array.toString[items_idx] = items_unknownCapabilityToDisplayString;

if (num_of_args > 5) {
item_array.items[items_idx].can_display = false;
if (num_of_args > 5 || json_all->tokens[token_index].end - json_all->tokens[token_index].start > MAX_ITEM_LENGTH_TO_DISPLAY) {
item_array.items[items_idx].can_display = bool_false;
}
}

Expand All @@ -436,8 +424,8 @@ static items_error_t items_storeRotateItem(parsed_json_t *json_all, uint16_t tra
(*unknown_capabitilies)++;
item_array.toString[items_idx] = items_unknownCapabilityToDisplayString;

if (num_of_args > 5) {
item_array.items[items_idx].can_display = false;
if (num_of_args > 5 || json_all->tokens[token_index].end - json_all->tokens[token_index].start > MAX_ITEM_LENGTH_TO_DISPLAY) {
item_array.items[items_idx].can_display = bool_false;
}
}

Expand All @@ -456,223 +444,9 @@ static items_error_t items_storeUnknownItem(parsed_json_t *json_all, uint16_t tr
(*unknown_capabitilies)++;
item_array.toString[items_idx] = items_unknownCapabilityToDisplayString;

if (num_of_args > 5) {
item_array.items[items_idx].can_display = false;
if (num_of_args > 5 || json_all->tokens[token_index].end - json_all->tokens[token_index].start > MAX_ITEM_LENGTH_TO_DISPLAY) {
item_array.items[items_idx].can_display = bool_false;
}

return items_ok;
}

static items_error_t items_stdToDisplayString(item_t item, char *outVal, uint16_t *outValLen) {
parsed_json_t json_all = parser_getParserTxObj()->tx_json.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;
snprintf(outVal, *outValLen, "%s", json_all.buffer + json_all.tokens[item_token_index].start);

return parser_ok;
}

static items_error_t items_nothingToDisplayString(__Z_UNUSED item_t item, char *outVal, uint16_t *outValLen) {
*outValLen = 1;
snprintf(outVal, *outValLen, " ");
return parser_ok;
}

static items_error_t items_warningToDisplayString(__Z_UNUSED item_t item, char *outVal, uint16_t *outValLen) {
*outValLen = sizeof(WARNING_TEXT);
snprintf(outVal, *outValLen, WARNING_TEXT);
return parser_ok;
}

static items_error_t items_cautionToDisplayString(__Z_UNUSED item_t item, char *outVal, uint16_t *outValLen) {
*outValLen = sizeof(CAUTION_TEXT);
snprintf(outVal, *outValLen, CAUTION_TEXT);
return parser_ok;
}

static items_error_t items_txTooLargeToDisplayString(__Z_UNUSED item_t item, char *outVal, uint16_t *outValLen) {
*outValLen = sizeof(TX_TOO_LARGE_TEXT);
snprintf(outVal, *outValLen, TX_TOO_LARGE_TEXT);
return parser_ok;
}

static items_error_t items_signingToDisplayString(__Z_UNUSED item_t item, char *outVal, uint16_t *outValLen) {
*outValLen = sizeof("Transaction");
snprintf(outVal, *outValLen, "Transaction");
return parser_ok;
}

static items_error_t items_requiringToDisplayString(__Z_UNUSED item_t item, char *outVal, uint16_t *outValLen) {
*outValLen = sizeof("Capabilities");
snprintf(outVal, *outValLen, "Capabilities");
return parser_ok;
}

static items_error_t items_transferToDisplayString(item_t item, char *outVal, uint16_t *outValLen) {
char amount[50];
uint8_t amount_len = 0;
char to[65];
uint8_t to_len = 0;
char from[65];
uint8_t from_len = 0;
uint16_t token_index = 0;
parsed_json_t json_all = parser_getParserTxObj()->tx_json.json;
uint16_t item_token_index = item.json_token_index;

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

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

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

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);

return parser_ok;
}

static items_error_t items_crossTransferToDisplayString(item_t item, char *outVal, uint16_t *outValLen) {
char amount[50];
uint8_t amount_len = 0;
char to[65];
uint8_t to_len = 0;
char from[65];
uint8_t from_len = 0;
char chain[3];
uint8_t chain_len = 0;
uint16_t token_index = 0;
parsed_json_t json_all = parser_getParserTxObj()->tx_json.json;
uint16_t item_token_index = item.json_token_index;

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

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

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

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

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);

return parser_ok;
}

static 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;

object_get_value(&json_all, item_token_index, "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 items_error_t items_gasToDisplayString(__Z_UNUSED item_t item, char *outVal, uint16_t *outValLen) {
char gasLimit[10];
uint8_t gasLimit_len = 0;
char gasPrice[64];
uint8_t gasPrice_len = 0;
parsed_json_t json_all = parser_getParserTxObj()->tx_json.json;
uint16_t item_token_index = item.json_token_index;
uint16_t meta_token_index = item_token_index;

parser_getJsonValue(&item_token_index, JSON_GAS_LIMIT);
gasLimit_len = json_all.tokens[item_token_index].end - json_all.tokens[item_token_index].start + 1;
snprintf(gasLimit, gasLimit_len, "%s", json_all.buffer + json_all.tokens[item_token_index].start);

item_token_index = meta_token_index;
parser_getJsonValue(&item_token_index, JSON_GAS_PRICE);
gasPrice_len = json_all.tokens[item_token_index].end - json_all.tokens[item_token_index].start + 1;
snprintf(gasPrice, gasPrice_len, "%s", json_all.buffer + json_all.tokens[item_token_index].start);

*outValLen = gasLimit_len + gasPrice_len + sizeof("at most ") + sizeof(" at price ");
snprintf(outVal, *outValLen, "at most %s at price %s", gasLimit, gasPrice);

return parser_ok;
}

static items_error_t items_hashToDisplayString(item_t item, char *outVal, uint16_t *outValLen) {
*outValLen = sizeof(base64_hash);
snprintf(outVal, *outValLen, "%s", base64_hash);
return parser_ok;
}

static items_error_t items_unknownCapabilityToDisplayString(item_t item, char *outVal, uint16_t *outValLen) {
uint16_t token_index = 0;
uint16_t args_count = 0;
uint8_t len = 0;
uint8_t outVal_idx= 0;
parsed_json_t json_all = parser_getParserTxObj()->tx_json.json;
uint16_t item_token_index = item.json_token_index;

object_get_value(&json_all, item_token_index, "name", &token_index);
len = json_all.tokens[token_index].end - json_all.tokens[token_index].start + sizeof("name: ");
snprintf(outVal, len, "name: %s", json_all.buffer + json_all.tokens[token_index].start);
outVal_idx += len;

// Remove null terminator
outVal[outVal_idx - 1] = ',';

// Add space
outVal[outVal_idx] = ' ';
outVal_idx++;

if (!item.can_display) {
len = sizeof("args cannot be displayed on Ledger");
snprintf(outVal + outVal_idx, len, "args cannot be displayed on Ledger");
outVal_idx += len;
return parser_ok;
}

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


if (args_count) {
uint16_t args_token_index = 0;
for (uint8_t i = 0; i < args_count - 1; i++) {
array_get_nth_element(&json_all, token_index, i, &args_token_index);
if (json_all.tokens[args_token_index].type == JSMN_STRING) {
// Strings go in between double quotes
len = json_all.tokens[args_token_index].end - json_all.tokens[args_token_index].start + sizeof("arg X: \"\",");
snprintf(outVal + outVal_idx, len, "arg %d: \"%s\",", i + 1, json_all.buffer + json_all.tokens[args_token_index].start);
} else {
len = json_all.tokens[args_token_index].end - json_all.tokens[args_token_index].start + sizeof("arg X: ,");
snprintf(outVal + outVal_idx, len, "arg %d: %s,", i + 1, json_all.buffer + json_all.tokens[args_token_index].start);
}
outVal_idx += len;

// Remove null terminator
outVal[outVal_idx - 1] = ' ';
}

// Last arg (without comma)
array_get_nth_element(&json_all, token_index, args_count - 1, &args_token_index);
if (json_all.tokens[args_token_index].type == JSMN_STRING) {
len = json_all.tokens[args_token_index].end - json_all.tokens[args_token_index].start + sizeof("arg X: \"\"");
snprintf(outVal + outVal_idx, len, "arg %d: \"%s\"", args_count, json_all.buffer + json_all.tokens[args_token_index].start);
} else {
len = json_all.tokens[args_token_index].end - json_all.tokens[args_token_index].start + sizeof("arg X: ");
snprintf(outVal + outVal_idx, len, "arg %d: %s", args_count, json_all.buffer + json_all.tokens[args_token_index].start);
}
outVal_idx += len;
} else {
len = sizeof("no args");
snprintf(outVal + outVal_idx, len, "no args");
outVal_idx += len;
}

*outValLen = outVal_idx;

return parser_ok;
}
22 changes: 1 addition & 21 deletions app/src/items.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,7 @@
#include "zxtypes.h"
#include "parser_common.h"
#include "json_parser.h"

#define WARNING_TEXT "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."
#define CAUTION_TEXT "'meta' field of transaction not recognized"
#define TX_TOO_LARGE_TEXT "Transaction too large for Ledger to display. PROCEED WITH GREAT CAUTION. Do you want to continue?"

typedef struct {
char key[25];
uint16_t json_token_index;
bool can_display;
} item_t;

typedef enum {
items_ok,
items_error,
} items_error_t;

typedef struct {
item_t items[20];
uint8_t numOfItems;
items_error_t (*toString[20])(item_t item, char *outVal, uint16_t *outValLen);
} item_array_t;
#include "items_defs.h"

void items_initItems();
void items_storeItems();
Expand Down
36 changes: 36 additions & 0 deletions app/src/items_defs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*******************************************************************************

Check notice on line 1 in app/src/items_defs.h

View workflow job for this annotation

GitHub Actions / lint

Run clang-format on app/src/items_defs.h

File app/src/items_defs.h does not conform to Custom style guidelines. (lines 18)
* (c) 2018 - 2024 Zondax AG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
********************************************************************************/
#pragma once

#include <zxmacros.h>
#include "zxtypes.h"

typedef struct {
char key[25];
uint16_t json_token_index;
bool_t can_display;
} item_t;

typedef enum {
items_ok,
items_error,
} items_error_t;

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

1 comment on commit 268ef65

@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.