Remove THROW from lib mode handlers and remove associated TRY CATCH context
This commit is contained in:
@@ -4,18 +4,18 @@
|
|||||||
#include "ethUtils.h"
|
#include "ethUtils.h"
|
||||||
#include "string.h"
|
#include "string.h"
|
||||||
|
|
||||||
#define ZERO(x) memset(&x, 0, sizeof(x))
|
#define ZERO(x) explicit_bzero(&x, sizeof(x))
|
||||||
|
|
||||||
int handle_check_address(check_address_parameters_t* params, chain_config_t* chain_config) {
|
void handle_check_address(check_address_parameters_t* params, chain_config_t* chain_config) {
|
||||||
|
params->result = 0;
|
||||||
PRINTF("Params on the address %d\n", (unsigned int) params);
|
PRINTF("Params on the address %d\n", (unsigned int) params);
|
||||||
PRINTF("Address to check %s\n", params->address_to_check);
|
PRINTF("Address to check %s\n", params->address_to_check);
|
||||||
PRINTF("Inside handle_check_address\n");
|
PRINTF("Inside handle_check_address\n");
|
||||||
if (params->address_to_check == 0) {
|
if (params->address_to_check == 0) {
|
||||||
PRINTF("Address to check == 0\n");
|
PRINTF("Address to check == 0\n");
|
||||||
return 0;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t i;
|
|
||||||
const uint8_t* bip32_path_ptr = params->address_parameters;
|
const uint8_t* bip32_path_ptr = params->address_parameters;
|
||||||
uint8_t bip32PathLength = *(bip32_path_ptr++);
|
uint8_t bip32PathLength = *(bip32_path_ptr++);
|
||||||
cx_sha3_t local_sha3;
|
cx_sha3_t local_sha3;
|
||||||
@@ -27,37 +27,55 @@ int handle_check_address(check_address_parameters_t* params, chain_config_t* cha
|
|||||||
char address[51];
|
char address[51];
|
||||||
} locals_union1;
|
} locals_union1;
|
||||||
union group2 {
|
union group2 {
|
||||||
uint8_t privateKeyData[32];
|
uint8_t privateKeyData[64];
|
||||||
cx_ecfp_public_key_t publicKey;
|
cx_ecfp_public_key_t publicKey;
|
||||||
} locals_union2;
|
} locals_union2;
|
||||||
|
|
||||||
if ((bip32PathLength < 0x01) || (bip32PathLength > MAX_BIP32_PATH) ||
|
if ((bip32PathLength < 0x01) || (bip32PathLength > MAX_BIP32_PATH) ||
|
||||||
(bip32PathLength * 4 != params->address_parameters_length - 1)) {
|
(bip32PathLength * 4 != params->address_parameters_length - 1)) {
|
||||||
PRINTF("Invalid path\n");
|
PRINTF("Invalid path\n");
|
||||||
return 0;
|
return;
|
||||||
}
|
}
|
||||||
for (i = 0; i < bip32PathLength; i++) {
|
for (uint8_t i = 0; i < bip32PathLength; i++) {
|
||||||
locals_union1.bip32Path[i] = U4BE(bip32_path_ptr, 0);
|
locals_union1.bip32Path[i] = U4BE(bip32_path_ptr, 0);
|
||||||
bip32_path_ptr += 4;
|
bip32_path_ptr += 4;
|
||||||
}
|
}
|
||||||
os_perso_derive_node_bip32(CX_CURVE_256K1,
|
if (os_derive_bip32_no_throw(CX_CURVE_256K1,
|
||||||
locals_union1.bip32Path,
|
locals_union1.bip32Path,
|
||||||
bip32PathLength,
|
bip32PathLength,
|
||||||
locals_union2.privateKeyData,
|
locals_union2.privateKeyData,
|
||||||
NULL);
|
NULL) != CX_OK) {
|
||||||
|
ZERO(locals_union1);
|
||||||
|
ZERO(locals_union2);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
ZERO(locals_union1);
|
ZERO(locals_union1);
|
||||||
cx_ecfp_init_private_key(CX_CURVE_256K1,
|
if (cx_ecfp_init_private_key_no_throw(CX_CURVE_256K1,
|
||||||
locals_union2.privateKeyData,
|
locals_union2.privateKeyData,
|
||||||
32,
|
32,
|
||||||
&locals_union1.privateKey);
|
&locals_union1.privateKey) != CX_OK) {
|
||||||
|
ZERO(locals_union1);
|
||||||
|
ZERO(locals_union2);
|
||||||
|
return;
|
||||||
|
}
|
||||||
ZERO(locals_union2);
|
ZERO(locals_union2);
|
||||||
cx_ecfp_generate_pair(CX_CURVE_256K1, &locals_union2.publicKey, &locals_union1.privateKey, 1);
|
if (cx_ecfp_generate_pair_no_throw(CX_CURVE_256K1,
|
||||||
|
&locals_union2.publicKey,
|
||||||
|
&locals_union1.privateKey,
|
||||||
|
1) != CX_OK) {
|
||||||
|
ZERO(locals_union1);
|
||||||
|
ZERO(locals_union2);
|
||||||
|
return;
|
||||||
|
}
|
||||||
ZERO(locals_union1);
|
ZERO(locals_union1);
|
||||||
if (!getEthAddressStringFromKey(&locals_union2.publicKey,
|
if (!getEthAddressStringFromKey(&locals_union2.publicKey,
|
||||||
locals_union1.address,
|
locals_union1.address,
|
||||||
&local_sha3,
|
&local_sha3,
|
||||||
chain_config->chainId)) {
|
chain_config->chainId)) {
|
||||||
THROW(CX_INVALID_PARAMETER);
|
ZERO(locals_union1);
|
||||||
|
ZERO(locals_union2);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
ZERO(locals_union2);
|
ZERO(locals_union2);
|
||||||
|
|
||||||
@@ -68,8 +86,9 @@ int handle_check_address(check_address_parameters_t* params, chain_config_t* cha
|
|||||||
|
|
||||||
if (strcmp(locals_union1.address, params->address_to_check + offset_0x) != 0) {
|
if (strcmp(locals_union1.address, params->address_to_check + offset_0x) != 0) {
|
||||||
PRINTF("Addresses don't match\n");
|
PRINTF("Addresses don't match\n");
|
||||||
return 0;
|
} else {
|
||||||
|
PRINTF("Addresses match\n");
|
||||||
|
params->result = 1;
|
||||||
}
|
}
|
||||||
PRINTF("Addresses match\n");
|
ZERO(locals_union1);
|
||||||
return 1;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
#include "swap_lib_calls.h"
|
#include "swap_lib_calls.h"
|
||||||
#include "chainConfig.h"
|
#include "chainConfig.h"
|
||||||
|
|
||||||
int handle_check_address(check_address_parameters_t* check_address_params,
|
void handle_check_address(check_address_parameters_t* check_address_params,
|
||||||
chain_config_t* chain_config);
|
chain_config_t* chain_config);
|
||||||
|
|
||||||
#endif // _HANDLE_CHECK_ADDRESS_H_
|
#endif // _HANDLE_CHECK_ADDRESS_H_
|
||||||
|
|||||||
@@ -7,13 +7,14 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <os.h>
|
#include <os.h>
|
||||||
|
|
||||||
int handle_get_printable_amount(get_printable_amount_parameters_t* params, chain_config_t* config) {
|
void handle_get_printable_amount(get_printable_amount_parameters_t* params,
|
||||||
|
chain_config_t* config) {
|
||||||
uint8_t decimals;
|
uint8_t decimals;
|
||||||
char ticker[MAX_TICKER_LEN];
|
char ticker[MAX_TICKER_LEN];
|
||||||
memset(params->printable_amount, 0, sizeof(params->printable_amount));
|
memset(params->printable_amount, 0, sizeof(params->printable_amount));
|
||||||
if (params->amount_length > 32) {
|
if (params->amount_length > 32) {
|
||||||
PRINTF("Amount is too big, 32 bytes max but buffer has %u bytes", params->amount_length);
|
PRINTF("Amount is too big, 32 bytes max but buffer has %u bytes", params->amount_length);
|
||||||
return 0;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the amount is a fee, its value is nominated in ETH even if we're doing an ERC20 swap
|
// If the amount is a fee, its value is nominated in ETH even if we're doing an ERC20 swap
|
||||||
@@ -29,7 +30,7 @@ int handle_get_printable_amount(get_printable_amount_parameters_t* params, chain
|
|||||||
ticker,
|
ticker,
|
||||||
&decimals)) {
|
&decimals)) {
|
||||||
PRINTF("Error while parsing config\n");
|
PRINTF("Error while parsing config\n");
|
||||||
return 0;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,7 +40,7 @@ int handle_get_printable_amount(get_printable_amount_parameters_t* params, chain
|
|||||||
ticker,
|
ticker,
|
||||||
params->printable_amount,
|
params->printable_amount,
|
||||||
sizeof(params->printable_amount))) {
|
sizeof(params->printable_amount))) {
|
||||||
THROW(EXCEPTION_OVERFLOW);
|
memset(params->printable_amount, 0, sizeof(params->printable_amount));
|
||||||
}
|
}
|
||||||
return 1;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
#include "swap_lib_calls.h"
|
#include "swap_lib_calls.h"
|
||||||
#include "chainConfig.h"
|
#include "chainConfig.h"
|
||||||
|
|
||||||
int handle_get_printable_amount(get_printable_amount_parameters_t* get_printable_amount_params,
|
void handle_get_printable_amount(get_printable_amount_parameters_t* get_printable_amount_params,
|
||||||
chain_config_t* config);
|
chain_config_t* config);
|
||||||
|
|
||||||
#endif // _HANDLE_GET_PRINTABLE_AMOUNT_H_
|
#endif // _HANDLE_GET_PRINTABLE_AMOUNT_H_
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ bool copy_transaction_parameters(create_transaction_parameters_t* sign_transacti
|
|||||||
ticker,
|
ticker,
|
||||||
stack_data.fullAmount,
|
stack_data.fullAmount,
|
||||||
sizeof(stack_data.fullAmount))) {
|
sizeof(stack_data.fullAmount))) {
|
||||||
THROW(EXCEPTION_OVERFLOW);
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the amount is a fee, its value is nominated in ETH even if we're doing an ERC20 swap
|
// If the amount is a fee, its value is nominated in ETH even if we're doing an ERC20 swap
|
||||||
@@ -53,7 +53,7 @@ bool copy_transaction_parameters(create_transaction_parameters_t* sign_transacti
|
|||||||
ticker,
|
ticker,
|
||||||
stack_data.maxFee,
|
stack_data.maxFee,
|
||||||
sizeof(stack_data.maxFee))) {
|
sizeof(stack_data.maxFee))) {
|
||||||
THROW(EXCEPTION_OVERFLOW);
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Full reset the global variables
|
// Full reset the global variables
|
||||||
@@ -71,9 +71,10 @@ void __attribute__((noreturn)) finalize_exchange_sign_transaction(bool is_succes
|
|||||||
os_lib_end();
|
os_lib_end();
|
||||||
}
|
}
|
||||||
|
|
||||||
void handle_swap_sign_transaction(chain_config_t* config) {
|
void __attribute__((noreturn)) handle_swap_sign_transaction(chain_config_t* config) {
|
||||||
UX_INIT();
|
|
||||||
#ifdef HAVE_NBGL
|
#ifdef HAVE_NBGL
|
||||||
|
// On Stax, display a spinner at startup
|
||||||
|
UX_INIT();
|
||||||
nbgl_useCaseSpinner("Signing");
|
nbgl_useCaseSpinner("Signing");
|
||||||
#endif // HAVE_NBGL
|
#endif // HAVE_NBGL
|
||||||
|
|
||||||
@@ -93,10 +94,9 @@ void handle_swap_sign_transaction(chain_config_t* config) {
|
|||||||
nvm_write((void*) &N_storage, (void*) &storage, sizeof(internalStorage_t));
|
nvm_write((void*) &N_storage, (void*) &storage, sizeof(internalStorage_t));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PRINTF("USB power ON/OFF\n");
|
||||||
USB_power(0);
|
USB_power(0);
|
||||||
USB_power(1);
|
USB_power(1);
|
||||||
// ui_idle();
|
|
||||||
PRINTF("USB power ON/OFF\n");
|
|
||||||
#ifdef HAVE_BLE
|
#ifdef HAVE_BLE
|
||||||
// grab the current plane mode setting
|
// grab the current plane mode setting
|
||||||
G_io_app.plane_mode = os_setting_get(OS_SETTING_PLANEMODE, NULL, 0);
|
G_io_app.plane_mode = os_setting_get(OS_SETTING_PLANEMODE, NULL, 0);
|
||||||
|
|||||||
@@ -6,6 +6,6 @@
|
|||||||
bool copy_transaction_parameters(create_transaction_parameters_t* sign_transaction_params,
|
bool copy_transaction_parameters(create_transaction_parameters_t* sign_transaction_params,
|
||||||
chain_config_t* config);
|
chain_config_t* config);
|
||||||
|
|
||||||
void handle_swap_sign_transaction(chain_config_t* config);
|
void __attribute__((noreturn)) handle_swap_sign_transaction(chain_config_t* config);
|
||||||
|
|
||||||
void __attribute__((noreturn)) finalize_exchange_sign_transaction(bool is_success);
|
void __attribute__((noreturn)) finalize_exchange_sign_transaction(bool is_success);
|
||||||
|
|||||||
48
src/main.c
48
src/main.c
@@ -731,57 +731,35 @@ void coin_main(libargs_t *args) {
|
|||||||
app_exit();
|
app_exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void library_main_helper(libargs_t *args) {
|
void library_main(libargs_t *args) {
|
||||||
check_api_level(CX_COMPAT_APILEVEL);
|
chain_config_t coin_config;
|
||||||
|
if (args->chain_config == NULL) {
|
||||||
|
// We have been started directly by Exchange, not by a Clone. Init default chain
|
||||||
|
init_coin_config(&coin_config);
|
||||||
|
args->chain_config = &coin_config;
|
||||||
|
}
|
||||||
|
|
||||||
PRINTF("Inside a library \n");
|
PRINTF("Inside a library \n");
|
||||||
switch (args->command) {
|
switch (args->command) {
|
||||||
case CHECK_ADDRESS:
|
case CHECK_ADDRESS:
|
||||||
// ensure result is zero if an exception is thrown
|
handle_check_address(args->check_address, args->chain_config);
|
||||||
args->check_address->result = 0;
|
|
||||||
args->check_address->result =
|
|
||||||
handle_check_address(args->check_address, args->chain_config);
|
|
||||||
break;
|
break;
|
||||||
case SIGN_TRANSACTION:
|
case SIGN_TRANSACTION:
|
||||||
if (copy_transaction_parameters(args->create_transaction, args->chain_config)) {
|
if (copy_transaction_parameters(args->create_transaction, args->chain_config)) {
|
||||||
// never returns
|
// never returns
|
||||||
handle_swap_sign_transaction(args->chain_config);
|
handle_swap_sign_transaction(args->chain_config);
|
||||||
|
} else {
|
||||||
|
// Failed to copy, non recoverable
|
||||||
|
os_sched_exit(-1);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case GET_PRINTABLE_AMOUNT:
|
case GET_PRINTABLE_AMOUNT:
|
||||||
// ensure result is zero if an exception is thrown (compatibility breaking, disabled
|
|
||||||
// until LL is ready)
|
|
||||||
// args->get_printable_amount->result = 0;
|
|
||||||
// args->get_printable_amount->result =
|
|
||||||
handle_get_printable_amount(args->get_printable_amount, args->chain_config);
|
handle_get_printable_amount(args->get_printable_amount, args->chain_config);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
os_lib_end();
|
||||||
|
|
||||||
void library_main(libargs_t *args) {
|
|
||||||
chain_config_t coin_config;
|
|
||||||
if (args->chain_config == NULL) {
|
|
||||||
init_coin_config(&coin_config);
|
|
||||||
args->chain_config = &coin_config;
|
|
||||||
}
|
|
||||||
volatile 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 */
|
|
||||||
while (1) {
|
|
||||||
BEGIN_TRY {
|
|
||||||
TRY {
|
|
||||||
if (!end) {
|
|
||||||
library_main_helper(args);
|
|
||||||
}
|
|
||||||
os_lib_end();
|
|
||||||
}
|
|
||||||
FINALLY {
|
|
||||||
end = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
END_TRY;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
__attribute__((section(".boot"))) int main(int arg0) {
|
__attribute__((section(".boot"))) int main(int arg0) {
|
||||||
|
|||||||
Reference in New Issue
Block a user