diff --git a/Makefile b/Makefile index ae419c66..14bca79c 100644 --- a/Makefile +++ b/Makefile @@ -65,6 +65,7 @@ endif ENABLE_BLUETOOTH = 1 ENABLE_NBGL_QRCODE = 1 +ENABLE_SWAP = 1 ifeq ($(TARGET_NAME),TARGET_STAX) DEFINES += COIN_ICON=C_$(COIN)_64px diff --git a/src/btchip_display_variables.h b/src/btchip_display_variables.h index f62263cb..694d3a51 100644 --- a/src/btchip_display_variables.h +++ b/src/btchip_display_variables.h @@ -1,3 +1,4 @@ +#include "os.h" #ifndef _BTCHIP_DISPLAY_VARIABLES_H_ #define _BTCHIP_DISPLAY_VARIABLES_H_ @@ -12,6 +13,7 @@ typedef struct swap_data_s { // number of already signed input in the transaction, to compare with // totalNumberOfInputs and exit properly int alreadySignedInputs; + int initialized; unsigned char amount[8]; unsigned char fees[8]; char destination_address[65]; diff --git a/src/handle_check_address.c b/src/handle_check_address.c index b7dcde7d..41a8b730 100644 --- a/src/handle_check_address.c +++ b/src/handle_check_address.c @@ -81,26 +81,42 @@ static bool get_address_from_compressed_public_key( return true; } -static int os_strcmp(const char* s1, const char* s2) { - size_t size = strlen(s1) + 1; - return memcmp(s1, s2, size); -} +void swap_handle_check_address(check_address_parameters_t* params) { + PRINTF("Inside swap_handle_check_address\n"); + params->result = 0; + + if (params->address_parameters == NULL) { + PRINTF("derivation path expected\n"); + return; + } + + if (params->address_to_check == NULL) { + PRINTF("Address to check expected\n"); + return; + } + PRINTF("Address to check %s\n", params->address_to_check); + + if (params->extra_id_to_check == NULL) { + PRINTF("extra_id_to_check expected\n"); + return; + } else if (params->extra_id_to_check[0] != '\0') { + PRINTF("extra_id_to_check expected empty, not '%s'\n", params->extra_id_to_check); + return; + } -int handle_check_address(check_address_parameters_t* params) { - unsigned char compressed_public_key[33]; - PRINTF("Params on the address %d\n",(unsigned int)params); - PRINTF("Address to check %s\n",params->address_to_check); - PRINTF("Inside handle_check_address\n"); if (params->address_to_check == 0) { PRINTF("Address to check == 0\n"); - return 0; + return; } + + unsigned char compressed_public_key[33]; if (!derive_compressed_public_key( params->address_parameters + 1, params->address_parameters_length - 1, compressed_public_key, sizeof(compressed_public_key))) { - return 0; + PRINTF("Failed to derive public key\n"); + return; } char address[51]; @@ -113,12 +129,16 @@ int handle_check_address(check_address_parameters_t* params) { address, sizeof(address))) { PRINTF("Can't create address from given public key\n"); - return 0; + return; } - if (os_strcmp(address,params->address_to_check) != 0) { - PRINTF("Addresses don't match\n"); - return 0; + + if (strcmp(params->address_to_check, address) != 0) { + PRINTF("Address %s != %s\n", params->address_to_check, address); + return; } + PRINTF("Addresses match\n"); - return 1; + + params->result = 1; + return; } \ No newline at end of file diff --git a/src/handle_check_address.h b/src/handle_check_address.h index 4d6c2a63..59806838 100644 --- a/src/handle_check_address.h +++ b/src/handle_check_address.h @@ -4,6 +4,6 @@ #include "swap_lib_calls.h" #include "btchip_context.h" -int handle_check_address(check_address_parameters_t* check_address_params); +void handle_check_address(check_address_parameters_t* check_address_params); #endif // _HANDLE_CHECK_ADDRESS_H_ \ No newline at end of file diff --git a/src/handle_get_printable_amount.c b/src/handle_get_printable_amount.c index 2be91eef..9d4e9e4d 100644 --- a/src/handle_get_printable_amount.c +++ b/src/handle_get_printable_amount.c @@ -2,11 +2,11 @@ #include "btchip_bcd.h" #include -int handle_get_printable_amount(get_printable_amount_parameters_t* params) { +void swap_handle_get_printable_amount(get_printable_amount_parameters_t* params) { params->printable_amount[0] = 0; if (params->amount_length > 8) { PRINTF("Amount is too big"); - return 0; + return; } unsigned char amount[8]; memset(amount, 0, 8); @@ -17,5 +17,5 @@ int handle_get_printable_amount(get_printable_amount_parameters_t* params) { int res_length = btchip_convert_hex_amount_to_displayable_no_globals(amount, COIN_FLAGS, (uint8_t *)params->printable_amount + coin_name_length + 1); params->printable_amount[res_length + coin_name_length + 1] = '\0'; - return 1; + return; } \ No newline at end of file diff --git a/src/handle_get_printable_amount.h b/src/handle_get_printable_amount.h index a870dbdd..5b9ba487 100644 --- a/src/handle_get_printable_amount.h +++ b/src/handle_get_printable_amount.h @@ -4,6 +4,6 @@ #include "swap_lib_calls.h" #include "btchip_context.h" -int handle_get_printable_amount(get_printable_amount_parameters_t* get_printable_amount_params); +void swap_handle_get_printable_amount(get_printable_amount_parameters_t* get_printable_amount_params); #endif // _HANDLE_GET_PRINTABLE_AMOUNT_H_ \ No newline at end of file diff --git a/src/handle_swap_sign_transaction.c b/src/handle_swap_sign_transaction.c index 92160215..6ecbdca9 100644 --- a/src/handle_swap_sign_transaction.c +++ b/src/handle_swap_sign_transaction.c @@ -10,37 +10,65 @@ #include "nbgl_use_case.h" #endif +#include "swap.h" + // Save the BSS address where we will write the return value when finished static uint8_t *G_swap_sign_return_value_address; -bool copy_transaction_parameters(create_transaction_parameters_t* sign_transaction_params) { - // first copy parameters to stack, and then to global data. - // We need this "trick" as the input data position can overlap with btc-app globals - swap_data_t stack_data; - memset(&stack_data, 0, sizeof(stack_data)); - strncpy(stack_data.destination_address, sign_transaction_params->destination_address, sizeof(stack_data.destination_address) - 1); - if ((stack_data.destination_address[sizeof(stack_data.destination_address) - 1] != '\0') || - (sign_transaction_params->amount_length > 8) || - (sign_transaction_params->fee_amount_length > 8)) { +bool swap_copy_transaction_parameters(create_transaction_parameters_t* params) { + PRINTF("Inside swap_copy_transaction_parameters\n"); + + // Ensure no extraid + if (params->destination_address_extra_id == NULL) { + PRINTF("destination_address_extra_id expected\n"); + return false; + } else if (params->destination_address_extra_id[0] != '\0') { + PRINTF("destination_address_extra_id expected empty, not '%s'\n", + params->destination_address_extra_id); + return false; + } + + // We need this "trick" as the input data position can overlap with app globals + // and also because we want to memset the whole bss segment as it is not done + // when an app is called as a lib. + // This is necessary as many part of the code expect bss variables to + // initialized at 0. + swap_data_t swap_validated; + memset(&swap_validated, 0, sizeof(swap_validated)); + + // Save recipient + strlcpy(swap_validated.destination_address, + params->destination_address, + sizeof(swap_validated.destination_address)); + if (swap_validated.destination_address[sizeof(swap_validated.destination_address) - 1] != '\0') { return false; } + // store amount as big endian in 8 bytes, so the passed data should be alligned to right // input {0xEE, 0x00, 0xFF} should be stored like {0x00, 0x00, 0x00, 0x00, 0x00, 0xEE, 0x00, 0xFF} - memcpy(stack_data.amount + 8 - sign_transaction_params->amount_length, sign_transaction_params->amount, sign_transaction_params->amount_length); - memcpy(stack_data.fees + 8 - sign_transaction_params->fee_amount_length, sign_transaction_params->fee_amount, sign_transaction_params->fee_amount_length); + memcpy(swap_validated.amount + 8 - params->amount_length, params->amount, params->amount_length); + memcpy(swap_validated.fees + 8 - params->fee_amount_length, params->fee_amount, params->fee_amount_length); - // Erase values inherited from Exchange app + // Save amount and fees +// swap_str_to_u64(params->amount, params->amount_length, &swap_validated.amount); +// swap_str_to_u64(params->fee_amount, params->fee_amount_length, &swap_validated.fees); +// + swap_validated.initialized = true; + + // Full reset the global variables os_explicit_zero_BSS_segment(); // Keep the address at which we'll reply the signing status - G_swap_sign_return_value_address = &sign_transaction_params->result; + G_swap_sign_return_value_address = ¶ms->result; + // Copy from stack back to global data segment - memcpy(&vars.swap_data, &stack_data, sizeof(stack_data)); + memcpy(&vars.swap_data, &swap_validated, sizeof(swap_validated)); + swap_validated.initialized = true; return true; } -void handle_swap_sign_transaction(void) { +void swap_handle_swap_sign_transaction(void) { btchip_context_init(); io_seproxyhal_init(); UX_INIT(); diff --git a/src/handle_swap_sign_transaction.h b/src/handle_swap_sign_transaction.h index 91978797..0f9bc0bc 100644 --- a/src/handle_swap_sign_transaction.h +++ b/src/handle_swap_sign_transaction.h @@ -4,9 +4,9 @@ #include "swap_lib_calls.h" #include "btchip_context.h" -bool copy_transaction_parameters(create_transaction_parameters_t* sign_transaction_params); +bool swap_copy_transaction_parameters(create_transaction_parameters_t* sign_transaction_params); -void handle_swap_sign_transaction(void); +void swap_handle_swap_sign_transaction(void); void __attribute__((noreturn)) finalize_exchange_sign_transaction(bool is_success);