Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve app flow #14

Merged
merged 9 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion app/Makefile.version
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ APPVERSION_M=1
# This is the minor version
APPVERSION_N=0
# This is the patch version
APPVERSION_P=2
APPVERSION_P=3
105 changes: 40 additions & 65 deletions app/src/items.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <base64.h>

#include "app_mode.h"
#include "crypto_helper.h"
#include "items_format.h"
#include "parser_impl.h"
Expand All @@ -34,7 +35,6 @@ static items_error_t items_storeNetwork();
static items_error_t items_storeRequiringCapabilities();
static items_error_t items_storeKey();
static items_error_t items_validateSigners();
static items_error_t items_storePayingGas();
static items_error_t items_storeAllTransfers();
static items_error_t items_storeCaution();
static items_error_t items_storeHashWarning();
Expand All @@ -44,7 +44,6 @@ static items_error_t items_checkTxLengths();
static items_error_t items_computeHash(tx_type_t tx_type);
static items_error_t items_storeHash();
static items_error_t items_storeSignForAddr();
static items_error_t items_storeGasItem(uint16_t json_token_index);
static items_error_t items_storeTxItem(uint16_t transfer_token_index, uint8_t *num_of_transfers);
static items_error_t items_storeTxCrossItem(uint16_t transfer_token_index, uint8_t *num_of_transfers);
static items_error_t items_storeTxRotateItem(uint16_t transfer_token_index);
Expand Down Expand Up @@ -84,8 +83,6 @@ items_error_t items_storeItems(tx_type_t tx_type) {

CHECK_ITEMS_ERROR(items_validateSigners());

CHECK_ITEMS_ERROR(items_storePayingGas());

CHECK_ITEMS_ERROR(items_storeAllTransfers());

if (parser_validateMetaField() != parser_ok) {
Expand All @@ -103,9 +100,11 @@ items_error_t items_storeItems(tx_type_t tx_type) {

CHECK_ITEMS_ERROR(items_computeHash(tx_type));

CHECK_ITEMS_ERROR(items_storeHash());
if (app_mode_expert()) {
CHECK_ITEMS_ERROR(items_storeHash());

CHECK_ITEMS_ERROR(items_storeSignForAddr());
CHECK_ITEMS_ERROR(items_storeSignForAddr());
}

return items_ok;
}
Expand Down Expand Up @@ -204,45 +203,6 @@ static items_error_t items_validateSigners() {
return items_ok;
}

static items_error_t items_storePayingGas() {
parsed_json_t *json_all = &(parser_getParserJsonObj()->json);
uint16_t *curr_token_idx = &item_array.items[item_array.numOfItems].json_token_index;
uint16_t token_index = 0;
uint16_t name_token_index = 0;
jsmntok_t *token;

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

if (!items_isNullField(*curr_token_idx)) {
PARSER_TO_ITEMS_ERROR(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_no_data) {
return items_ok;
}

if (!items_isNullField(*curr_token_idx)) {
uint16_t clist_element_count = 0;
PARSER_TO_ITEMS_ERROR(array_get_element_count(json_all, *curr_token_idx, &clist_element_count));

for (uint16_t i = 0; i < (uint8_t)clist_element_count; i++) {
PARSER_TO_ITEMS_ERROR(array_get_nth_element(json_all, *curr_token_idx, i, &token_index));

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

if (MEMCMP("coin.GAS", json_all->buffer + token->start, token->end - token->start) == 0) {
*curr_token_idx = token_index;
items_storeGasItem(*curr_token_idx);
return items_ok;
}
}
}
}

*curr_token_idx = 0;
return items_ok;
}

static items_error_t items_storeAllTransfers() {
parsed_json_t *json_all = &(parser_getParserJsonObj()->json);
uint16_t *curr_token_idx = &item_array.items[item_array.numOfItems].json_token_index;
Expand Down Expand Up @@ -411,26 +371,6 @@ static items_error_t items_storeSignForAddr() {
return items_ok;
}

static items_error_t items_storeGasItem(uint16_t json_token_index) {
uint16_t token_index = 0;
uint16_t args_count = 0;
parsed_json_t *json_all = &(parser_getParserJsonObj()->json);
item_t *item = &item_array.items[item_array.numOfItems];

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

if (args_count > 0) {
items_storeUnknownItem(args_count, token_index);
} else {
item->key = key_paying_gas;
item_array.toString[item_array.numOfItems] = items_nothingToDisplayString;
INCREMENT_NUM_ITEMS()
}

return items_ok;
}

static items_error_t items_storeTxItem(uint16_t transfer_token_index, uint8_t *num_of_transfers) {
uint16_t token_index = 0;
uint16_t num_of_args = 0;
Expand All @@ -446,6 +386,21 @@ static items_error_t items_storeTxItem(uint16_t transfer_token_index, uint8_t *n
(*num_of_transfers)++;
item_array.toString[item_array.numOfItems] = items_transferToDisplayString;
INCREMENT_NUM_ITEMS()
item = &item_array.items[item_array.numOfItems];
item->key = key_from;
PARSER_TO_ITEMS_ERROR(array_get_nth_element(json_all, token_index, 0, &item->json_token_index));
item_array.toString[item_array.numOfItems] = items_stdToDisplayString;
INCREMENT_NUM_ITEMS()
item = &item_array.items[item_array.numOfItems];
item->key = key_to;
PARSER_TO_ITEMS_ERROR(array_get_nth_element(json_all, token_index, 1, &item->json_token_index));
item_array.toString[item_array.numOfItems] = items_stdToDisplayString;
INCREMENT_NUM_ITEMS()
item = &item_array.items[item_array.numOfItems];
item->key = key_amount;
PARSER_TO_ITEMS_ERROR(array_get_nth_element(json_all, token_index, 2, &item->json_token_index));
item_array.toString[item_array.numOfItems] = items_amountToDisplayString;
INCREMENT_NUM_ITEMS()
} else {
items_storeUnknownItem(num_of_args, token_index);
}
Expand All @@ -468,6 +423,26 @@ static items_error_t items_storeTxCrossItem(uint16_t transfer_token_index, uint8
(*num_of_transfers)++;
item_array.toString[item_array.numOfItems] = items_crossTransferToDisplayString;
INCREMENT_NUM_ITEMS()
item = &item_array.items[item_array.numOfItems];
item->key = key_from;
PARSER_TO_ITEMS_ERROR(array_get_nth_element(json_all, token_index, 0, &item->json_token_index));
item_array.toString[item_array.numOfItems] = items_stdToDisplayString;
INCREMENT_NUM_ITEMS()
item = &item_array.items[item_array.numOfItems];
item->key = key_to;
PARSER_TO_ITEMS_ERROR(array_get_nth_element(json_all, token_index, 1, &item->json_token_index));
item_array.toString[item_array.numOfItems] = items_stdToDisplayString;
INCREMENT_NUM_ITEMS()
item = &item_array.items[item_array.numOfItems];
item->key = key_amount;
PARSER_TO_ITEMS_ERROR(array_get_nth_element(json_all, token_index, 2, &item->json_token_index));
item_array.toString[item_array.numOfItems] = items_amountToDisplayString;
INCREMENT_NUM_ITEMS()
item = &item_array.items[item_array.numOfItems];
item->key = key_to_chain;
PARSER_TO_ITEMS_ERROR(array_get_nth_element(json_all, token_index, 3, &item->json_token_index));
item_array.toString[item_array.numOfItems] = items_stdToDisplayString;
INCREMENT_NUM_ITEMS()
} else {
items_storeUnknownItem(num_of_args, token_index);
}
Expand Down
6 changes: 5 additions & 1 deletion app/src/items_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include "zxtypes.h"

#define MAX_NUMBER_OF_ITEMS 25
#define MAX_NUMBER_OF_ITEMS 65

#define CHECK_ITEMS_ERROR(__CALL) \
{ \
Expand Down Expand Up @@ -55,6 +55,10 @@ typedef enum {
key_chain_id,
key_paying_gas,
key_transfer,
key_from,
key_to,
key_amount,
key_to_chain,
key_rotate,
key_unknown_capability,
key_transaction_hash,
Expand Down
59 changes: 18 additions & 41 deletions app/src/items_format.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,60 +103,37 @@ items_error_t items_requiringToDisplayString(__Z_UNUSED item_t item, char *outVa
return items_ok;
}

items_error_t items_transferToDisplayString(item_t item, char *outVal, uint16_t outValLen) {
const char *amount;
const char *to;
const char *from;
uint8_t amount_len = 0;
uint8_t to_len = 0;
uint8_t from_len = 0;
uint16_t token_index = 0;
parsed_json_t *json_all = &(parser_getParserJsonObj()->json);
uint16_t item_token_index = item.json_token_index;
items_error_t items_amountToDisplayString(item_t item, char *outVal, uint16_t outValLen) {
const parsed_json_t *json_all = &(parser_getParserJsonObj()->json);
const jsmntok_t *token = &(json_all->tokens[item.json_token_index]);
const uint16_t len = token->end - token->start;

PARSER_TO_ITEMS_ERROR(object_get_value(json_all, item_token_index, "args", &token_index));
PARSER_TO_ITEMS_ERROR(parser_arrayElementToString(token_index, 0, &from, &from_len));
PARSER_TO_ITEMS_ERROR(parser_arrayElementToString(token_index, 1, &to, &to_len));
PARSER_TO_ITEMS_ERROR(parser_arrayElementToString(token_index, 2, &amount, &amount_len));
if (len == 0) return items_length_zero;

uint16_t required_len = amount_len + from_len + to_len + strlen(" from ") + strlen(" to ") + 4 * sizeof("\"");
if (required_len > outValLen) {
if (len >= outValLen) {
return items_data_too_large;
}

snprintf(outVal, outValLen, "%.*s from \"%.*s\" to \"%.*s\"", amount_len, amount, from_len, from, to_len, to);
snprintf(outVal, outValLen, "KDA %.*s", len, json_all->buffer + token->start);

return items_ok;
}

items_error_t items_crossTransferToDisplayString(item_t item, char *outVal, uint16_t outValLen) {
const char *amount;
const char *to;
const char *from;
const char *chain;
uint8_t amount_len = 0;
uint8_t to_len = 0;
uint8_t from_len = 0;
uint8_t chain_len = 0;
uint16_t token_index = 0;
parsed_json_t *json_all = &(parser_getParserJsonObj()->json);
uint16_t item_token_index = item.json_token_index;
items_error_t items_transferToDisplayString(__Z_UNUSED item_t item, char *outVal, uint16_t outValLen) {
uint16_t len = sizeof("Normal Transfer");

PARSER_TO_ITEMS_ERROR(object_get_value(json_all, item_token_index, "args", &token_index));
PARSER_TO_ITEMS_ERROR(parser_arrayElementToString(token_index, 0, &from, &from_len));
PARSER_TO_ITEMS_ERROR(parser_arrayElementToString(token_index, 1, &to, &to_len));
PARSER_TO_ITEMS_ERROR(parser_arrayElementToString(token_index, 2, &amount, &amount_len));
PARSER_TO_ITEMS_ERROR(parser_arrayElementToString(token_index, 3, &chain, &chain_len));
if (len > outValLen) return items_data_too_large;

uint16_t required_len = amount_len + from_len + to_len + chain_len + strlen("Cross-chain ") + strlen(" from ") +
strlen(" to ") + 6 * strlen("\"") + strlen(" to chain ");
if (required_len > outValLen) {
return items_data_too_large;
}
snprintf(outVal, len, "Normal Transfer");
return items_ok;
}

snprintf(outVal, outValLen, "Cross-chain %.*s from \"%.*s\" to \"%.*s\" to chain \"%.*s\"", amount_len, amount, from_len,
from, to_len, to, chain_len, chain);
items_error_t items_crossTransferToDisplayString(__Z_UNUSED item_t item, char *outVal, uint16_t outValLen) {
uint16_t len = sizeof("Cross-chain Transfer");

if (len > outValLen) return items_data_too_large;

snprintf(outVal, len, "Cross-chain Transfer");
return items_ok;
}

Expand Down
1 change: 1 addition & 0 deletions app/src/items_format.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ items_error_t items_cautionToDisplayString(__Z_UNUSED item_t item, char *outVal,
items_error_t items_txTooLargeToDisplayString(__Z_UNUSED item_t item, char *outVal, uint16_t outValLen);
items_error_t items_signingToDisplayString(item_t item, char *outVal, uint16_t outValLen);
items_error_t items_requiringToDisplayString(item_t item, char *outVal, uint16_t outValLen);
items_error_t items_amountToDisplayString(item_t item, char *outVal, uint16_t outValLen);
items_error_t items_transferToDisplayString(item_t item, char *outVal, uint16_t outValLen);
items_error_t items_crossTransferToDisplayString(item_t item, char *outVal, uint16_t outValLen);
items_error_t items_rotateToDisplayString(item_t item, char *outVal, uint16_t outValLen);
Expand Down
12 changes: 12 additions & 0 deletions app/src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,18 @@ static parser_error_t parser_getItemKey(uint8_t displayIdx, char *outKey, uint16
case key_paying_gas:
strncpy(outKey, "Paying Gas", outKeyLen);
break;
case key_from:
strncpy(outKey, "From", outKeyLen);
break;
case key_to:
strncpy(outKey, "To", outKeyLen);
break;
case key_amount:
strncpy(outKey, "Amount", outKeyLen);
break;
case key_to_chain:
strncpy(outKey, "To Chain", outKeyLen);
break;
case key_transfer:
if (update_counts) {
transfer_count++;
Expand Down
2 changes: 1 addition & 1 deletion deps/ledger-zxlib
Submodule ledger-zxlib updated 57 files
+34 −39 app/common/app_main.c
+31 −30 app/common/app_main.h
+20 −27 app/ui/view.c
+1 −2 app/ui/view.h
+35 −37 app/ui/view_inspect_s.c
+20 −1 app/ui/view_internal.h
+97 −89 app/ui/view_nano.c
+1 −1 app/ui/view_nano.h
+15 −14 app/ui/view_nano_inspect.h
+12 −12 app/ui/view_nbgl.c
+215 −140 app/ui/view_x.c
+32 −33 include/apdu_codes.h
+19 −16 include/app_mode.h
+19 −21 include/base58.h
+1 −1 include/base64.h
+1 −1 include/bech32.h
+1 −1 include/bignum.h
+1 −6 include/bittools.h
+17 −20 include/buffering.h
+15 −15 include/hexutils.h
+8 −35 include/segwit_addr.h
+15 −18 include/sigutils.h
+17 −16 include/timeutils.h
+318 −415 include/utf8.h
+145 −149 include/view_templates.h
+14 −14 include/zxcanary.h
+51 −49 include/zxerror.h
+74 −79 include/zxformat.h
+14 −14 include/zxtypes.h
+2 −3 include/zxutils_ledger.h
+3 −3 include/zxversion.h
+1 −1 scripts/install_deps.sh
+1 −1 scripts/template.sh
+27 −3 src/app_mode.c
+40 −48 src/base58.c
+20 −20 src/base64.c
+20 −23 src/bech32.c
+24 −27 src/bignum.c
+20 −26 src/buffering.c
+24 −25 src/hexutils.c
+14 −22 src/segwit_addr.c
+17 −21 src/sigutils.c
+76 −686 src/timeutils.c
+19 −17 src/zxcanary.c
+30 −30 src/zxformat.c
+133 −134 src/zxutils_ledger.c
+1 −1 templates/Makefile.root
+1 −1 tests/asciify.cpp
+1 −1 tests/base64.cpp
+1 −1 tests/bech32.cpp
+1 −1 tests/bip44path.cpp
+1 −1 tests/buffering_tests.cpp
+1 −1 tests/doubledabble.cpp
+1 −1 tests/hexutils.cpp
+1 −1 tests/macros.cpp
+1 −1 tests/sigutils.cpp
+1 −1 tests/zxformat.cpp
Loading
Loading