Skip to content

Commit

Permalink
Merge pull request #24 from LedgerHQ/swap/add-fee-to-validate
Browse files Browse the repository at this point in the history
NAPPS-1102 Add fee to swap transaction validation
  • Loading branch information
joaoccmartins authored Feb 27, 2025
2 parents 02d07f3 + 0c49c79 commit f687df9
Show file tree
Hide file tree
Showing 32 changed files with 33 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ APPNAME = "Aptos"
# Application version
APPVERSION_M = 0
APPVERSION_N = 7
APPVERSION_P = 10
APPVERSION_P = 11
APPVERSION = "$(APPVERSION_M).$(APPVERSION_N).$(APPVERSION_P)"

# Application source files
Expand Down
1 change: 1 addition & 0 deletions src/handler/sign_tx.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "../transaction/types.h"
#include "../transaction/deserialize.h"
#include "../ui/action/validate.h"
#include "../swap/handle_swap_sign_transaction.h"

#ifdef HAVE_SWAP
#include "swap.h"
Expand Down
36 changes: 25 additions & 11 deletions src/swap/handle_swap_sign_transaction.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ typedef struct swap_validated_s {
// NOTE(jmartins): only needed for tokens
char ticker[MAX_SWAP_TOKEN_LENGTH];
uint64_t amount;
uint64_t fee;
uint8_t recipient[ADDRESS_LEN];
} swap_validated_t;

Expand Down Expand Up @@ -97,6 +98,11 @@ bool swap_copy_transaction_parameters(create_transaction_parameters_t* params) {
return false;
}

// Save the fee
if (!swap_str_to_u64(params->fee_amount, params->fee_amount_length, &swap_validated.fee)) {
return false;
}

swap_validated.initialized = true;

// Full reset the global variables
Expand Down Expand Up @@ -163,41 +169,49 @@ bool swap_check_validity() {
return false;
}
// Validate it's and actual coin transfer type
if (G_context.tx_info.transaction.tx_variant != TX_RAW) {
transaction_t* transaction = &G_context.tx_info.transaction;
if (transaction->tx_variant != TX_RAW) {
PRINTF("TX variant different from TX_RAW is not compatible with Swap.\n");
return false;
}

PRINTF("Payload variant: %d\n", G_context.tx_info.transaction.payload_variant);
if (G_context.tx_info.transaction.payload_variant != PAYLOAD_ENTRY_FUNCTION) {
PRINTF("Payload variant: %d\n", transaction->payload_variant);
if (transaction->payload_variant != PAYLOAD_ENTRY_FUNCTION) {
PRINTF("Payload variant incompatible with Swap.\n");
return false;
}

// Differentiate between the different types of transactions
// Differentiate between the different types of transaction->
uint64_t amount = 0;
uint8_t* receiver;
switch (G_context.tx_info.transaction.payload.entry_function.known_type) {
uint64_t gas_fee_value = transaction->gas_unit_price * transaction->max_gas_amount;
switch (transaction->payload.entry_function.known_type) {
case FUNC_APTOS_ACCOUNT_TRANSFER:
amount = G_context.tx_info.transaction.payload.entry_function.args.transfer.amount;
receiver = G_context.tx_info.transaction.payload.entry_function.args.transfer.receiver;
amount = transaction->payload.entry_function.args.transfer.amount;
receiver = transaction->payload.entry_function.args.transfer.receiver;
break;
case FUNC_COIN_TRANSFER:
case FUNC_APTOS_ACCOUNT_TRANSFER_COINS:
amount = G_context.tx_info.transaction.payload.entry_function.args.coin_transfer.amount;
receiver =
G_context.tx_info.transaction.payload.entry_function.args.coin_transfer.receiver;
amount = transaction->payload.entry_function.args.coin_transfer.amount;
receiver = transaction->payload.entry_function.args.coin_transfer.receiver;
break;
default:
PRINTF("Unknown function type\n");
return false;
}

// Validate amount
if (!validate_swap_amount(amount)) {
PRINTF("Amount on Transaction is different from validated package.\n");
return false;
}

// Validate fee
if (gas_fee_value != G_swap_validated.fee) {
PRINTF("Gas fee on Transaction is different from validated package.\n");
return false;
}

// Validate recipient
if (memcmp(receiver, G_swap_validated.recipient, ADDRESS_LEN) != 0) {
PRINTF("Recipient on Transaction is different from validated package.\n");
Expand All @@ -210,7 +224,7 @@ bool swap_check_validity() {
return true;
}

void __attribute__((noreturn)) swap_finalize_exchange_sign_transaction(bool is_success) {
void swap_finalize_exchange_sign_transaction(bool is_success) {
PRINTF("Returning to Exchange with status %d\n", is_success);
*G_swap_sign_return_value_address = is_success;
os_lib_end();
Expand Down
2 changes: 2 additions & 0 deletions src/swap/handle_swap_sign_transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
#define MAX_SWAP_TOKEN_LENGTH 15

bool swap_check_validity();

void __attribute__((noreturn)) swap_finalize_exchange_sign_transaction(bool is_success);
2 changes: 1 addition & 1 deletion src/ui/bagl_menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void ui_menu_main() {
ui_flow_display(ux_menu_main_flow);
}

UX_STEP_NOCB(ux_menu_info_step, bn, {"Aptos App", "(c) 2022 Pontem"});
UX_STEP_NOCB(ux_menu_info_step, bn, {"Aptos App", "(c) 2024 Ledger"});
UX_STEP_NOCB(ux_menu_version_step, bn, {"Version", APPVERSION});
UX_STEP_CB(ux_menu_back_step, pb, ui_menu_main(), {&C_icon_back, "Back"});

Expand Down
4 changes: 1 addition & 3 deletions src/ui/nbgl_menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ enum {
};

static const char* const INFO_TYPES[] = {"Version", "Developer", "Copyright"};
static const char* const INFO_CONTENTS[] = {APPVERSION,
"Pontem Network",
"(c) 2022 Pontem Network"};
static const char* const INFO_CONTENTS[] = {APPVERSION, "Ledger", "(c) 2024 Ledger"};

static nbgl_contentSwitch_t g_switches[SWITCHES_COUNT];

Expand Down
Binary file modified tests/snapshots/flex/test_app_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/snapshots/flex/test_app_mainmenu/00005.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/snapshots/flex/test_app_mainmenu/00006.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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/snapshots/flex/test_blind_sign_tx_long_tx/part0/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/snapshots/flex/test_blind_sign_tx_long_tx/part0/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/snapshots/flex/test_blind_sign_tx_long_tx/part1/00000.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/snapshots/flex/test_blind_sign_tx_long_tx/part1/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/snapshots/flex/test_sign_long_raw_msg/part0/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/snapshots/flex/test_sign_long_raw_msg/part0/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/snapshots/flex/test_sign_long_raw_msg/part0/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/snapshots/flex/test_sign_long_raw_msg/part1/00000.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/snapshots/flex/test_sign_long_raw_msg/part1/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/snapshots/stax/test_app_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.
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/snapshots/stax/test_blind_sign_tx_long_tx/part0/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/snapshots/stax/test_blind_sign_tx_long_tx/part0/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/snapshots/stax/test_blind_sign_tx_long_tx/part1/00000.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/snapshots/stax/test_blind_sign_tx_long_tx/part1/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/snapshots/stax/test_sign_long_raw_msg/part0/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/snapshots/stax/test_sign_long_raw_msg/part0/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/snapshots/stax/test_sign_long_raw_msg/part0/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/snapshots/stax/test_sign_long_raw_msg/part1/00000.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/snapshots/stax/test_sign_long_raw_msg/part1/00003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tests/test_name_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ def test_get_app_and_version(backend, backend_name):
app_name, version = unpack_get_app_and_version_response(response.data)

assert app_name == "Aptos"
assert version == "0.7.10"
assert version == "0.7.11"
2 changes: 1 addition & 1 deletion tests/test_version_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Taken from the Makefile, to update every time the Makefile version is bumped
MAJOR = 0
MINOR = 7
PATCH = 10
PATCH = 11


# In this test we check the behavior of the device when asked to provide the app version
Expand Down

0 comments on commit f687df9

Please sign in to comment.