Skip to content

Commit

Permalink
Merge pull request #135 from cosmos/dev
Browse files Browse the repository at this point in the history
Improve swap (#134)
  • Loading branch information
chcmedeiros authored Jan 15, 2025
2 parents 8b29ff0 + b45932b commit a265f78
Show file tree
Hide file tree
Showing 22 changed files with 55 additions and 11 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=0
APPVERSION_P=1
2 changes: 0 additions & 2 deletions app/src/common/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ __attribute__((section(".boot"))) int main(int arg0) {
}
#endif
} else {
// The app has been launched from the dashboard
// G_swap_state.called_from_swap = false;
BEGIN_TRY
{
TRY
Expand Down
5 changes: 4 additions & 1 deletion app/src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ parser_error_t parser_getNumItems(const parser_context_t *ctx, uint8_t *num_item
return parser_ok;
}

return tx_display_numItems(num_items);
parser_error_t ret = tx_display_numItems(num_items);
ctx->tx_obj->tx_json.num_items = *num_items;

return ret;
}

__Z_INLINE bool parser_areEqual(uint16_t tokenIdx, const char *expected) {
Expand Down
1 change: 1 addition & 0 deletions app/src/parser_txdef.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ typedef struct

// current tx query
tx_query_t query;
uint8_t num_items;
}tx_json_t;


Expand Down
8 changes: 4 additions & 4 deletions app/src/swap/handle_check_address.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,25 @@


void handle_check_address(check_address_parameters_t *params) {
if (params == NULL || params->address_to_check == 0) {
if (params == NULL || params->address_to_check == NULL) {
return;
}

// Reset result
params->result = 0;

// Address parameters have the following structure
// 00 byte |path length (1 byte) | bip32 path (4 * pathLength bytes)
// path length (1 byte) | bip32 path (4 * pathLength bytes)
// Get the path
uint32_t bip32_path[HDPATH_LEN_DEFAULT] = {0};
uint8_t bip32_path_length = params->address_parameters[1];
uint8_t bip32_path_length = params->address_parameters[0];

if (bip32_path_length != HDPATH_LEN_DEFAULT) {
return;
}

for (uint32_t i = 0; i < HDPATH_LEN_DEFAULT; i++) {
readU32BE(params->address_parameters + 2 + (i * 4), &bip32_path[i]);
readU32BE(params->address_parameters + 1 + (i * 4), &bip32_path[i]);
}

char address_computed[100] = {0};
Expand Down
44 changes: 42 additions & 2 deletions app/src/swap/handle_sign_transaction.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "swap.h"
#include "swap_utils.h"
#include "zxformat.h"
#include "app_mode.h"

swap_globals_t G_swap_state;

Expand Down Expand Up @@ -79,8 +80,43 @@ bool copy_transaction_parameters(create_transaction_parameters_t *sign_transacti
return true;
}

// Ensure the received transaction matches what was validated in the Exchange app UI
// at this point, transaction was parsed by the app, so we need to compare what we parsed with what is saved in the global state
/*
* This function verifies that a received transaction follows the expected format
* based on the current application mode (expert or normal). The verification
* process includes checking the number of items in the transaction and validating
* that the items at its respective display index matches the expected content.
* If any item does not meet the expected criteria, the function will return an error.
*
* Expected transaction format:
*
* Expert Mode:
* 0 | Chain ID : cosmoshub-4
* 1 | Account : 0
* 2 | Sequence : 1
* 3 | Source Address : cosmosaccaddr1d9h8qat5e4ehc5
* 4 | Source Coins : 10 atom
* 5 | Dest Address : cosmosaccaddr1da6hgur4wse3jx32
* 6 | Dest Coins : 10 atom
* 7 | Memo : testmemo
* 8 | Fee : 5 photon
* 9 | Gas : 10000
*
* Normal Mode:
* 0 | Source Address : cosmosaccaddr1d9h8qat5e4ehc5
* 1 | Source Coins : 10 atom
* 2 | Dest Address : cosmosaccaddr1da6hgur4wse3jx32
* 3 | Dest Coins : 10 atom
* 4 | Memo : testmemo
* 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.
*/
parser_error_t check_swap_conditions(parser_context_t *ctx_parsed_tx) {
parser_error_t err = parser_unexpected_error;
if (ctx_parsed_tx == NULL) {
Expand All @@ -93,6 +129,10 @@ 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
const char *default_chain_id = "cosmoshub-4";
Expand Down
2 changes: 2 additions & 0 deletions app/src/swap/swap.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

#define ADDRESS_MAXSIZE 50
#define MEMO_MAXSIZE 50
#define EXPERT_MODE_ITEMS 10
#define NORMAL_MODE_ITEMS 6
typedef struct {
uint8_t amount[COIN_AMOUNT_MAXSIZE];
uint8_t amount_length;
Expand Down
2 changes: 1 addition & 1 deletion deps/ledger-zxlib
Binary file modified tests_zemu/snapshots/fl-mainmenu/00001.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/fl-mainmenu/00002.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/fl-mainmenu/00003.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/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/00001.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/00002.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/00003.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 a265f78

Please sign in to comment.