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

Fbe/sol token swap #107

Open
wants to merge 1 commit into
base: y333_241030/token_account_owner_display
Choose a base branch
from
Open
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ APPNAME = "Solana"

# Application version
APPVERSION_M = 1
APPVERSION_N = 6
APPVERSION_P = 1
APPVERSION = "$(APPVERSION_M).$(APPVERSION_N).$(APPVERSION_P)"
APPVERSION_N = 7
APPVERSION_P = 0
APPVERSION = "$(APPVERSION_M).$(APPVERSION_N).$(APPVERSION_P)-splswapv1"

# Application source files
APP_SOURCE_PATH += src
Expand Down Expand Up @@ -66,11 +66,15 @@ VARIANT_VALUES = solana
########################################
# Application custom permissions #
########################################
HAVE_APPLICATION_FLAG_LIBRARY = 1
ifeq ($(TARGET_NAME),$(filter $(TARGET_NAME),TARGET_NANOX TARGET_STAX TARGET_FLEX))
HAVE_APPLICATION_FLAG_BOLOS_SETTINGS = 1
endif

########################################
# Swap features #
########################################
ENABLE_SWAP = 1

########################################
# Application communication interfaces #
########################################
Expand All @@ -90,6 +94,8 @@ DISABLE_STANDARD_APP_FILES = 1

# Allow usage of function from lib_standard_app/crypto_helpers.c
APP_SOURCE_FILES += ${BOLOS_SDK}/lib_standard_app/crypto_helpers.c
APP_SOURCE_FILES += ${BOLOS_SDK}/lib_standard_app/swap_utils.c
CFLAGS += -I${BOLOS_SDK}/lib_standard_app/

WITH_U2F?=0
ifneq ($(WITH_U2F),0)
Expand All @@ -102,6 +108,7 @@ WITH_LIBSOL?=1
ifneq ($(WITH_LIBSOL),0)
SOURCE_FILES += $(filter-out %_test.c,$(wildcard libsol/*.c))
CFLAGS += -Ilibsol/include
CFLAGS += -Ilibsol
DEFINES += HAVE_SNPRINTF_FORMAT_U
DEFINES += NDEBUG
endif
Expand Down
1 change: 0 additions & 1 deletion libsol/printer.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ int print_token_amount(uint64_t amount,
return 0;
}

#define SOL_DECIMALS 9
int print_amount(uint64_t amount, char *out, size_t out_length) {
return print_token_amount(amount, "SOL", SOL_DECIMALS, out, out_length);
}
Expand Down
5 changes: 5 additions & 0 deletions libsol/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@
int err = x; \
if (err) return err; \
} while (0)

#ifndef MIN
#define MIN(a, b) ((a) < (b) ? (a) : (b));
#endif
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


#define assert_string_equal(actual, expected) assert(strcmp(actual, expected) == 0)

#define assert_pubkey_equal(actual, expected) assert(memcmp(actual, expected, 32) == 0)

#define SOL_DECIMALS 9

#ifndef UNUSED
#define UNUSED(x) (void) x
#endif
31 changes: 27 additions & 4 deletions src/handle_sign_message.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "io.h"
#include "io_utils.h"
#include "utils.h"
#include "handle_swap_sign_transaction.h"

Expand Down Expand Up @@ -93,24 +93,47 @@ void handle_sign_message_parse_message(volatile unsigned int *tx) {

static bool check_swap_validity(const SummaryItemKind_t kinds[MAX_TRANSACTION_SUMMARY_ITEMS],
size_t num_summary_steps) {
bool is_token_swap = is_token_transaction();
bool amount_ok = false;
bool recipient_ok = false;
if (num_summary_steps != 2 && num_summary_steps != 3) {
PRINTF("2 or 3 steps expected for transaction in swap context, not %u\n",
uint8_t expected_steps;
if (is_token_swap) {
expected_steps = 4;
} else {
expected_steps = 2;
}
// Accept base step number + optional fee step
if (num_summary_steps != expected_steps && num_summary_steps != expected_steps + 1) {
PRINTF("%d steps expected for token transaction in swap context, not %u\n",
expected_steps,
num_summary_steps);
return false;
}

for (size_t i = 0; i < num_summary_steps; ++i) {
transaction_summary_display_item(i, DisplayFlagNone | DisplayFlagLongPubkeys);
PRINTF("Item (%d) '%s', '%s'\n", kinds[i], G_transaction_summary_title, G_transaction_summary_text);
switch (kinds[i]) {
case SummaryItemTokenAmount:
amount_ok =
check_swap_amount(G_transaction_summary_title, G_transaction_summary_text);
break;
case SummaryItemAmount:
if (strcmp(G_transaction_summary_title, "Max fees") == 0) {
break; // Should we check the fees ?
break;
}
amount_ok =
check_swap_amount(G_transaction_summary_title, G_transaction_summary_text);
break;
case SummaryItemPubkey:
if (is_token_swap && strcmp(G_transaction_summary_title, "Token address") == 0) {
PRINTF("Skip %s field\n", G_transaction_summary_title);
break;
}
if (is_token_swap && strcmp(G_transaction_summary_title, "From (token account)") == 0) {
PRINTF("Skip %s field\n", G_transaction_summary_title);
break;
}
recipient_ok =
check_swap_recipient(G_transaction_summary_title, G_transaction_summary_text);
break;
Expand Down
2 changes: 1 addition & 1 deletion src/handle_sign_offchain_message.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "io.h"
#include "io_utils.h"
#include "os.h"
#include "ux.h"
#include "cx.h"
Expand Down
2 changes: 1 addition & 1 deletion src/io.c → src/io_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include "apdu.h"
#include "ui_api.h"
#include "handle_swap_sign_transaction.h"
#include "io.h"
#include "io_utils.h"

#ifdef HAVE_BAGL
// override point, but nothing more to do
Expand Down
File renamed without changes.
2 changes: 0 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@

ApduCommand G_command;
unsigned char G_io_seproxyhal_spi_buffer[IO_SEPROXYHAL_BUFFER_SIZE_B];
volatile bool G_called_from_swap;
volatile bool G_swap_response_ready;

static void reset_main_globals(void) {
MEMCLEAR(G_command);
Expand Down
8 changes: 4 additions & 4 deletions src/swap/handle_check_address.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ int handle_check_address(const check_address_parameters_t *params) {
PRINTF("Inside Solana handle_check_address\n");
PRINTF("Params on the address %d\n", (unsigned int) params);

if (params->coin_configuration != NULL || params->coin_configuration_length != 0) {
PRINTF("No coin_configuration expected\n");
return 0;
}
// if (params->coin_configuration != NULL || params->coin_configuration_length != 0) {
// PRINTF("No coin_configuration expected\n");
// return 0;
// }

if (params->address_parameters == NULL) {
PRINTF("derivation path expected\n");
Expand Down
35 changes: 24 additions & 11 deletions src/swap/handle_get_printable_amount.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,41 @@
#include "utils.h"
#include "sol/printer.h"

#define MAX_SWAP_TOKEN_LENGTH 15

/* return 0 on error, 1 otherwise */
int handle_get_printable_amount(get_printable_amount_parameters_t* params) {
PRINTF("Inside Solana handle_get_printable_amount\n");
MEMCLEAR(params->printable_amount);

// Fees are displayed normally
// params->is_fee

if (params->coin_configuration != NULL || params->coin_configuration_length != 0) {
PRINTF("No coin_configuration expected\n");
return 0;
}

uint64_t amount;
if (!swap_str_to_u64(params->amount, params->amount_length, &amount)) {
PRINTF("Amount is too big");
return 0;
}

if (print_amount(amount, params->printable_amount, sizeof(params->printable_amount)) != 0) {
PRINTF("print_amount failed");
return 0;
// Fees are displayed normally
if (params->is_fee || params->coin_configuration == NULL) {
PRINTF("Defaulting to native SOL amount\n");
if (print_amount(amount, params->printable_amount, sizeof(params->printable_amount)) != 0) {
PRINTF("print_amount failed");
return 0;
}
} else {
uint8_t decimals;
char ticker[MAX_SWAP_TOKEN_LENGTH] = {0};
if (!swap_parse_config(params->coin_configuration,
params->coin_configuration_length,
ticker,
sizeof(ticker),
&decimals)) {
PRINTF("Fail to parse coin_configuration\n");
return 0;
}
if (print_token_amount(amount, ticker, decimals, params->printable_amount, sizeof(params->printable_amount)) != 0) {
PRINTF("print_amount failed");
return 0;
}
}

PRINTF("Amount %s\n", params->printable_amount);
Expand Down
53 changes: 42 additions & 11 deletions src/swap/handle_swap_sign_transaction.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
#include "swap_lib_calls.h"
#include "swap_utils.h"
#include "sol/printer.h"
#include "util.h"

#define MAX_SWAP_TOKEN_LENGTH 15
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already defined in handle_get_printable_amount.c


typedef struct swap_validated_s {
bool initialized;
uint8_t decimals;
char ticker[MAX_SWAP_TOKEN_LENGTH];
uint64_t amount;
char recipient[BASE58_PUBKEY_LENGTH];
} swap_validated_t;
Expand All @@ -18,12 +23,6 @@ static uint8_t *G_swap_sign_return_value_address;

// Save the data validated during the Exchange app flow
bool copy_transaction_parameters(create_transaction_parameters_t *params) {
// Ensure no subcoin configuration
if (params->coin_configuration != NULL || params->coin_configuration_length != 0) {
PRINTF("No coin_configuration expected\n");
return false;
}

// Ensure no extraid
if (params->destination_address_extra_id == NULL) {
PRINTF("destination_address_extra_id expected\n");
Expand All @@ -39,6 +38,22 @@ bool copy_transaction_parameters(create_transaction_parameters_t *params) {
swap_validated_t swap_validated;
memset(&swap_validated, 0, sizeof(swap_validated));

// Parse config and save decimals and ticker
// If there is no coin_configuration, consider that we are doing a SOL swap
if (params->coin_configuration == NULL) {
memcpy(swap_validated.ticker, "SOL", sizeof("SOL"));
swap_validated.decimals = SOL_DECIMALS;
} else {
if (!swap_parse_config(params->coin_configuration,
params->coin_configuration_length,
swap_validated.ticker,
sizeof(swap_validated.ticker),
&swap_validated.decimals)) {
PRINTF("Fail to parse coin_configuration\n");
return false;
}
}

// Save recipient
strlcpy(swap_validated.recipient,
params->destination_address,
Expand Down Expand Up @@ -72,13 +87,19 @@ bool check_swap_amount(const char *title, const char *text) {
return false;
}

if (strcmp(title, "Transfer") != 0) {
PRINTF("Refused field '%s', expecting 'Transfer'\n", title);
char expected_title[MAX(sizeof("Transfer tokens"), sizeof("Transfer"))] = {'\0'};
if (is_token_transaction()) {
strcpy(expected_title, "Transfer tokens");
} else {
strcpy(expected_title, "Transfer");
}
if (strcmp(title, expected_title) != 0) {
PRINTF("Refused title '%s', expecting '%s'\n", title, expected_title);
return false;
}

char validated_amount[MAX_PRINTABLE_AMOUNT_SIZE];
if (print_amount(G_swap_validated.amount, validated_amount, sizeof(validated_amount)) != 0) {
if (print_token_amount(G_swap_validated.amount, G_swap_validated.ticker, G_swap_validated.decimals, validated_amount, sizeof(validated_amount)) != 0) {
PRINTF("Conversion failed\n");
return false;
}
Expand All @@ -98,8 +119,14 @@ bool check_swap_recipient(const char *title, const char *text) {
return false;
}

if (strcmp(title, "Recipient") != 0) {
PRINTF("Refused field '%s', expecting 'Recipient'\n", title);
char expected_title[MAX(sizeof("To (token account)"), sizeof("Recipient"))] = {'\0'};
if (is_token_transaction()) {
strcpy(expected_title, "To (token account)");
} else {
strcpy(expected_title, "Recipient");
}
if (strcmp(title, expected_title) != 0) {
PRINTF("Refused title '%s', expecting '%s'\n", title, expected_title);
return false;
}

Expand All @@ -116,3 +143,7 @@ void __attribute__((noreturn)) finalize_exchange_sign_transaction(bool is_succes
*G_swap_sign_return_value_address = is_success;
os_lib_end();
}

bool is_token_transaction() {
return (memcmp(G_swap_validated.ticker, "SOL", sizeof("SOL")) != 0);
}
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 @@ -8,4 +8,6 @@ bool check_swap_amount(const char *title, const char *text);

bool check_swap_recipient(const char *title, const char *text);

bool is_token_transaction();

void __attribute__((noreturn)) finalize_exchange_sign_transaction(bool is_success);
Loading
Loading