Skip to content

Commit

Permalink
Swap: use standard feature
Browse files Browse the repository at this point in the history
  • Loading branch information
sgliner-ledger committed Jan 31, 2024
1 parent ec8530f commit 3169075
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 38 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/btchip_display_variables.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "os.h"
#ifndef _BTCHIP_DISPLAY_VARIABLES_H_
#define _BTCHIP_DISPLAY_VARIABLES_H_

Expand All @@ -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];
Expand Down
52 changes: 36 additions & 16 deletions src/handle_check_address.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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;
}
2 changes: 1 addition & 1 deletion src/handle_check_address.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_
6 changes: 3 additions & 3 deletions src/handle_get_printable_amount.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
#include "btchip_bcd.h"
#include <string.h>

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);
Expand All @@ -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;
}
2 changes: 1 addition & 1 deletion src/handle_get_printable_amount.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_
58 changes: 43 additions & 15 deletions src/handle_swap_sign_transaction.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 = &params->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();
Expand Down
4 changes: 2 additions & 2 deletions src/handle_swap_sign_transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 3169075

Please sign in to comment.