Skip to content

Commit

Permalink
NAAPS-1102 Add fees to Validate Swap.
Browse files Browse the repository at this point in the history
  • Loading branch information
joaoccmartins committed Feb 27, 2025
1 parent 02d07f3 commit b224f4d
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 14 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 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 b224f4d

Please sign in to comment.