Skip to content

Commit

Permalink
Return to Exchange after a swap transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
fbeutin-ledger committed Nov 17, 2023
1 parent c1de5cf commit 675b525
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 41 deletions.
17 changes: 3 additions & 14 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,18 +252,7 @@ void coin_main() {
app_exit();
}

struct libargs_s {
unsigned int id;
unsigned int command;
unsigned int unused;
union {
check_address_parameters_t *check_address;
create_transaction_parameters_t *create_transaction;
get_printable_amount_parameters_t *get_printable_amount;
};
};

static void library_main_helper(struct libargs_s *args) {
static void library_main_helper(libargs_t *args) {
check_api_level(CX_COMPAT_APILEVEL);
PRINTF("Inside a library \n");
switch (args->command) {
Expand All @@ -290,7 +279,7 @@ static void library_main_helper(struct libargs_s *args) {
}
}

void library_main(struct libargs_s *args) {
void library_main(libargs_t *args) {
bool end = false;
/* This loop ensures that library_main_helper and os_lib_end are called
* within a try context, even if an exception is thrown */
Expand Down Expand Up @@ -322,7 +311,7 @@ __attribute__((section(".boot"))) int main(int arg0) {
coin_main();
} else {
// Called as library from another app
struct libargs_s *args = (struct libargs_s *) arg0;
libargs_t *args = (libargs_t *) arg0;
if (args->id == 0x100) {
library_main(args);
} else {
Expand Down
1 change: 1 addition & 0 deletions src/swap/handle_get_printable_amount.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <stdint.h>

#include "handle_get_printable_amount.h"
#include "swap_utils.h"
#include "../xrp/xrp_helpers.h"

/* return 0 on error, 1 otherwise */
Expand Down
13 changes: 13 additions & 0 deletions src/swap/handle_swap_sign_transaction.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
#include "os_io_seproxyhal.h"
#include "../apdu/global.h"
#include "swap_lib_calls.h"
#include "swap_utils.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* params) {
// first copy parameters to stack, and then to global data.
Expand All @@ -27,12 +31,21 @@ bool copy_transaction_parameters(create_transaction_parameters_t* params) {
return false;
}

// Full reset the global variables
os_explicit_zero_BSS_segment();
// Keep the address at wich we'll reply the signing status
G_swap_sign_return_value_address = &params->result;
// Commit the values read from exchange to the clean global space
memcpy(&approval_strings.swap, &stack_data, sizeof(stack_data));

return true;
}

void __attribute__((noreturn)) finalize_exchange_sign_transaction(bool is_success) {
*G_swap_sign_return_value_address = is_success;
os_lib_end();
}

void handle_swap_sign_transaction(void) {
called_from_swap = true;
reset_transaction_context();
Expand Down
1 change: 1 addition & 0 deletions src/swap/handle_swap_sign_transaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "swap_lib_calls.h"

bool copy_transaction_parameters(create_transaction_parameters_t* sign_transaction_params);
void __attribute__((noreturn)) finalize_exchange_sign_transaction(bool is_success);
void handle_swap_sign_transaction(void);

#endif // _HANDLE_SWAP_SIGN_TRANSACTION_H_
76 changes: 52 additions & 24 deletions src/swap/swap_lib_calls.h
Original file line number Diff line number Diff line change
@@ -1,52 +1,80 @@
#pragma once

#include <stddef.h>
#include <stdbool.h>
#include <stdint.h>
/* This file is the shared API between Exchange and the apps started in Library mode for Exchange
*
* DO NOT MODIFY THIS FILE IN APPLICATIONS OTHER THAN EXCHANGE
* On modification in Exchange, forward the changes to all applications supporting Exchange
*/

#include "stdbool.h"
#include "stdint.h"

#define RUN_APPLICATION 1

#define SIGN_TRANSACTION 2

#define CHECK_ADDRESS 3

#define GET_PRINTABLE_AMOUNT 4

/*
* Amounts are stored as bytes, with a max size of 16 (see protobuf
* specifications). Max 16B integer is 340282366920938463463374607431768211455
* in decimal, which is a 32-long char string.
* The printable amount also contains spaces, the ticker symbol (with variable
* size, up to 12 in Ethereum for instance) and a terminating null byte, so 50
* bytes total should be a fair maximum.
*/
#define MAX_PRINTABLE_AMOUNT_SIZE 50

// structure that should be send to specific coin application to get address
typedef struct check_address_parameters_s {
// IN
unsigned char* coin_configuration;
unsigned char coin_configuration_length;
uint8_t *coin_configuration;
uint8_t coin_configuration_length;
// serialized path, segwit, version prefix, hash used, dictionary etc.
// fields and serialization format depends on spesific coin app
unsigned char* address_parameters;
unsigned char address_parameters_length;
char* address_to_check;
char* extra_id_to_check;
uint8_t *address_parameters;
uint8_t address_parameters_length;
char *address_to_check;
char *extra_id_to_check;
// OUT
int result;
} check_address_parameters_t;

// structure that should be send to specific coin application to get printable amount
typedef struct get_printable_amount_parameters_s {
// IN
unsigned char* coin_configuration;
unsigned char coin_configuration_length;
unsigned char* amount;
unsigned char amount_length;
uint8_t *coin_configuration;
uint8_t coin_configuration_length;
uint8_t *amount;
uint8_t amount_length;
bool is_fee;
// OUT
char printable_amount[30];
// int result;
char printable_amount[MAX_PRINTABLE_AMOUNT_SIZE];
} get_printable_amount_parameters_t;

typedef struct create_transaction_parameters_s {
unsigned char* coin_configuration;
unsigned char coin_configuration_length;
unsigned char* amount;
unsigned char amount_length;
unsigned char* fee_amount;
unsigned char fee_amount_length;
char* destination_address;
char* destination_address_extra_id;
// IN
uint8_t *coin_configuration;
uint8_t coin_configuration_length;
uint8_t *amount;
uint8_t amount_length;
uint8_t *fee_amount;
uint8_t fee_amount_length;
char *destination_address;
char *destination_address_extra_id;
// OUT
uint8_t result;
} create_transaction_parameters_t;

bool swap_str_to_u64(const uint8_t* src, size_t length, uint64_t* result);
typedef struct libargs_s {
unsigned int id;
unsigned int command;
unsigned int unused;
union {
check_address_parameters_t *check_address;
create_transaction_parameters_t *create_transaction;
get_printable_amount_parameters_t *get_printable_amount;
};
} libargs_t;
2 changes: 1 addition & 1 deletion src/swap/swap_lib_calls.c → src/swap/swap_utils.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <string.h>

#include "swap_lib_calls.h"
#include "swap_utils.h"

bool swap_str_to_u64(const uint8_t* src, size_t length, uint64_t* result) {
const size_t num_bytes = 8;
Expand Down
7 changes: 7 additions & 0 deletions src/swap/swap_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include <stddef.h>
#include <stdbool.h>
#include <stdint.h>

bool swap_str_to_u64(const uint8_t* src, size_t length, uint64_t* result);
5 changes: 3 additions & 2 deletions src/transaction/transaction.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "../xrp/format.h"
#include "../xrp/readers.h"
#include "../xrp/xrp_helpers.h"
#include "handle_swap_sign_transaction.h"
#include <string.h>

static action_t approval_action;
Expand Down Expand Up @@ -172,11 +173,11 @@ void review_transaction(parseResult_t *transaction, action_t on_approve, action_
if (called_from_swap) {
if (check_swap_conditions_and_sign(transaction)) {
approval_action();
finalize_exchange_sign_transaction(true);
} else {
rejection_action();
finalize_exchange_sign_transaction(false);
}
called_from_swap = false;
os_sched_exit(0);
} else {
display_review_menu(transaction, on_approval_menu_result);
}
Expand Down
1 change: 1 addition & 0 deletions tests/src/test_swap.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "cx.h"
#include "../src/swap/handle_check_address.h"
#include "../src/swap/swap_utils.h"
#include "../src/xrp/xrp_helpers.h"

void test_check_address(void **state) {
Expand Down

0 comments on commit 675b525

Please sign in to comment.