Skip to content

Commit

Permalink
Merge pull request cosmos#145 from cosmos/dev
Browse files Browse the repository at this point in the history
Improve swap (cosmos#144)
  • Loading branch information
chcmedeiros authored Jan 28, 2025
2 parents 7d7e69f + 2accc6b commit c160886
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 15 deletions.
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=2
# This is the `spec_version` field of `Runtime`
APPVERSION_N=36
# This is the patch version of this release
APPVERSION_P=3
APPVERSION_P=4
2 changes: 1 addition & 1 deletion app/src/chain_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ typedef struct {

// To enable custom config for a new chain, just add a new entry in this array with path, hrp and encoding
static const chain_config_t chainConfig[] = {
// {118, cosmos, BECH32_COSMOS},
{118, "cosmos", BECH32_COSMOS},
{60, "inj", BECH32_ETH},
{60, "evmos", BECH32_ETH},
{60, "xpla", BECH32_ETH},
Expand Down
2 changes: 2 additions & 0 deletions app/src/coin.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ typedef enum {
#define COIN_DEFAULT_CHAINID "cosmoshub-4"
#define OSMOSIS_CHAINID "osmosis-1"
#define DYDX_CHAINID "dydx-mainnet-1"
#define MANTRA_CHAINID "mantra-1"
#define XION_CHAINID "xion-mainnet-1"

// In non-expert mode, the app will convert from uatom to ATOM
#define COIN_DEFAULT_DENOM_BASE "uatom"
Expand Down
21 changes: 15 additions & 6 deletions app/src/swap/handle_check_address.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@
#include "swap_utils.h"
#include "chain_config.h"


/*
Address parameters have the following structure;
- bip32 path length (1 byte) | bip32 path (4 * pathLength bytes)
Coin Configuration has the following structure;
- hrp length (1 byte) | hrp (hrp length bytes)
*/
void handle_check_address(check_address_parameters_t *params) {
if (params == NULL || params->address_to_check == NULL) {
return;
Expand All @@ -31,20 +36,24 @@ void handle_check_address(check_address_parameters_t *params) {
// Reset result
params->result = 0;

// Address parameters have the following structure
// hrp length (1 byte) | hrp (hrp length bytes) | bip32 path length (1 byte) | bip32 path (4 * pathLength bytes)
// Get HRP
uint8_t hrp_length = params->address_parameters[0];
if (params->coin_configuration == NULL) {
ZEMU_LOGF(200, "Coin configuration is NULL\n");
return;
}

uint8_t hrp_length = params->coin_configuration[0];
ZEMU_LOGF(200, "HRP length: %d\n", hrp_length);
char hrp[MAX_BECH32_HRP_LEN + 1] = {0};

if (hrp_length == 0 || hrp_length > MAX_BECH32_HRP_LEN) {
return;
}
memcpy(hrp, params->address_parameters + 1, hrp_length);
memcpy(hrp, params->coin_configuration + 1, hrp_length);
hrp[hrp_length] = 0;

// Get bip32 path
uint8_t bip32_path_length = params->address_parameters[1 + hrp_length];
uint8_t bip32_path_length = params->address_parameters[0];
uint32_t bip32_path[HDPATH_LEN_DEFAULT] = {0};

if (bip32_path_length != HDPATH_LEN_DEFAULT) {
Expand Down
27 changes: 20 additions & 7 deletions app/src/swap/handle_sign_transaction.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ swap_globals_t G_swap_state;
// Save the BSS address where we will write the return value when finished
static uint8_t *G_swap_sign_return_value_address;

static const char * chain_ids[] = { COIN_DEFAULT_CHAINID, OSMOSIS_CHAINID, DYDX_CHAINID};
static const char * chain_ids[] = {COIN_DEFAULT_CHAINID, OSMOSIS_CHAINID, DYDX_CHAINID, MANTRA_CHAINID, XION_CHAINID};

bool is_allowed_chainid(const char *chainId) {
for (uint32_t i = 0; i < array_length(chain_ids); i++) {
Expand Down Expand Up @@ -121,12 +121,12 @@ bool copy_transaction_parameters(create_transaction_parameters_t *sign_transacti
* 5 | Fee : 5 photon
*
* Verification Details:
* - The function will first confirm that the number of items in the transaction
* matches the expected count for the current mode.
* - Each item's content will be checked against the predefined values for the
* corresponding display index.
* - If any discrepancy is found (either in item count or content), the function
* will return an error.
* - The function will confirm that the number of items in the transaction
* matches the expected count for the current mode.
*/
parser_error_t check_swap_conditions(parser_context_t *ctx_parsed_tx) {
parser_error_t err = parser_unexpected_error;
Expand All @@ -140,10 +140,6 @@ parser_error_t check_swap_conditions(parser_context_t *ctx_parsed_tx) {
char tmpKey[20] = {0};
char tmpValue[65] = {0};

if ((app_mode_expert() && ctx_parsed_tx->tx_obj->tx_json.num_items != EXPERT_MODE_ITEMS) || (!app_mode_expert() && ctx_parsed_tx->tx_obj->tx_json.num_items != NORMAL_MODE_ITEMS)) {
return parser_unexpected_error;
}

// Cosmos App in normal mode requires that chain id is the default one. If not, it will print expert mode fields
// this means if we reach this point and no chain_id is printed, chain_id must be the default one
CHECK_PARSER_ERR(parser_getItem(ctx_parsed_tx, displayIdx, tmpKey, sizeof(tmpKey), tmpValue, sizeof(tmpValue), pageIdx, &pageCount))
Expand Down Expand Up @@ -182,8 +178,10 @@ parser_error_t check_swap_conditions(parser_context_t *ctx_parsed_tx) {

// Check if memo is present
displayIdx += 1;
uint8_t has_memo = 0;
CHECK_PARSER_ERR(parser_getItem(ctx_parsed_tx, displayIdx, tmpKey, sizeof(tmpKey), tmpValue, sizeof(tmpValue), pageIdx, &pageCount))
if (strcmp(tmpKey, "Memo") == 0) {
has_memo = 1;
if(strcmp(tmpValue, G_swap_state.memo) != 0) {
ZEMU_LOGF(200, "Wrong swap tx memo ('%s', should be : '%s').\n", tmpValue, G_swap_state.memo);
return parser_unexpected_error;
Expand All @@ -205,6 +203,21 @@ parser_error_t check_swap_conditions(parser_context_t *ctx_parsed_tx) {
return parser_unexpected_error;
}

switch (has_memo){
case 0:
if ((app_mode_expert() && ctx_parsed_tx->tx_obj->tx_json.num_items != EXPERT_MODE_ITEMS - 1) || (!app_mode_expert() && ctx_parsed_tx->tx_obj->tx_json.num_items != NORMAL_MODE_ITEMS - 1)) {
return parser_unexpected_error;
}
break;
case 1:
if ((app_mode_expert() && ctx_parsed_tx->tx_obj->tx_json.num_items != EXPERT_MODE_ITEMS) || (!app_mode_expert() && ctx_parsed_tx->tx_obj->tx_json.num_items != NORMAL_MODE_ITEMS)) {
return parser_unexpected_error;
}
break;
default:
break;
}

return parser_ok;
}

Expand Down
Binary file modified tests_zemu/snapshots/fl-mainmenu/00004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/s-mainmenu/00004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/s-mainmenu/00010.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/sp-mainmenu/00004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/sp-mainmenu/00010.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/st-mainmenu/00004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/x-mainmenu/00004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests_zemu/snapshots/x-mainmenu/00010.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c160886

Please sign in to comment.