Files
app-ethereum/src_features/signTx/ui_common_signTx.c

133 lines
4.4 KiB
C
Raw Normal View History

#include "os_io_seproxyhal.h"
2020-06-27 13:24:04 +02:00
#include "shared_context.h"
#include "utils.h"
#include "common_ui.h"
#include "handle_swap_sign_transaction.h"
2020-06-27 13:24:04 +02:00
unsigned int io_seproxyhal_touch_tx_ok(__attribute__((unused)) const bagl_element_t *e) {
uint8_t privateKeyData[INT256_LENGTH];
2020-06-27 13:24:04 +02:00
uint8_t signature[100];
cx_ecfp_private_key_t privateKey;
uint32_t tx = 0;
int err;
2020-06-27 13:24:04 +02:00
io_seproxyhal_io_heartbeat();
2020-12-01 16:20:13 +01:00
os_perso_derive_node_bip32(CX_CURVE_256K1,
2022-07-08 11:12:50 +02:00
tmpCtx.transactionContext.bip32.path,
tmpCtx.transactionContext.bip32.length,
2020-12-01 16:20:13 +01:00
privateKeyData,
NULL);
cx_ecfp_init_private_key(CX_CURVE_256K1, privateKeyData, 32, &privateKey);
explicit_bzero(privateKeyData, sizeof(privateKeyData));
2020-06-27 13:24:04 +02:00
unsigned int info = 0;
io_seproxyhal_io_heartbeat();
cx_ecdsa_sign(&privateKey,
CX_RND_RFC6979 | CX_LAST,
CX_SHA256,
tmpCtx.transactionContext.hash,
sizeof(tmpCtx.transactionContext.hash),
signature,
sizeof(signature),
&info);
explicit_bzero(&privateKey, sizeof(privateKey));
if (txContext.txType == EIP1559 || txContext.txType == EIP2930) {
if (info & CX_ECCINFO_PARITY_ODD) {
G_io_apdu_buffer[0] = 1;
} else {
G_io_apdu_buffer[0] = 0;
}
2020-12-01 16:20:13 +01:00
} else {
// Parity is present in the sequence tag in the legacy API
if (tmpContent.txContent.vLength == 0) {
// Legacy API
G_io_apdu_buffer[0] = 27;
} else {
// New API
2021-08-27 17:28:24 +02:00
// Note that this is wrong for a large v, but ledgerjs will recover.
// Taking only the 4 highest bytes to not introduce breaking changes. In the future,
// this should be updated.
uint32_t v = (uint32_t) u64_from_BE(tmpContent.txContent.v,
MIN(4, tmpContent.txContent.vLength));
G_io_apdu_buffer[0] = (v * 2) + 35;
}
if (info & CX_ECCINFO_PARITY_ODD) {
G_io_apdu_buffer[0]++;
}
if (info & CX_ECCINFO_xGTn) {
G_io_apdu_buffer[0] += 2;
}
2020-06-27 13:24:04 +02:00
}
format_signature_out(signature);
tx = 65;
G_io_apdu_buffer[tx++] = 0x90;
G_io_apdu_buffer[tx++] = 0x00;
2020-06-27 13:24:04 +02:00
// Send back the response, do not restart the event loop
err = io_exchange(CHANNEL_APDU | IO_RETURN_AFTER_TX, tx);
if (G_called_from_swap) {
PRINTF("G_called_from_swap\n");
// If we are in swap mode and have validated a TX, we send it and immediately quit
if (err == 0) {
finalize_exchange_sign_transaction(true);
} else {
PRINTF("Unrecoverable\n");
os_sched_exit(-1);
}
2020-06-29 15:43:02 +02:00
}
reset_app_context();
2020-06-27 13:24:04 +02:00
// Display back the original UX
ui_idle();
2020-12-01 16:20:13 +01:00
return 0; // do not redraw the widget
2020-06-27 13:24:04 +02:00
}
unsigned int io_seproxyhal_touch_tx_cancel(__attribute__((unused)) const bagl_element_t *e) {
2020-06-27 13:24:04 +02:00
reset_app_context();
G_io_apdu_buffer[0] = 0x69;
G_io_apdu_buffer[1] = 0x85;
// Send back the response, do not restart the event loop
io_exchange(CHANNEL_APDU | IO_RETURN_AFTER_TX, 2);
// Display back the original UX
ui_idle();
2020-12-01 16:20:13 +01:00
return 0; // do not redraw the widget
2020-06-27 13:24:04 +02:00
}
unsigned int io_seproxyhal_touch_data_ok(__attribute__((unused)) const bagl_element_t *e) {
2020-06-27 13:24:04 +02:00
parserStatus_e txResult = USTREAM_FINISHED;
txResult = continueTx(&txContext);
switch (txResult) {
2020-12-01 16:20:13 +01:00
case USTREAM_SUSPENDED:
break;
case USTREAM_FINISHED:
break;
case USTREAM_PROCESSING:
io_seproxyhal_send_status(0x9000);
ui_idle();
break;
case USTREAM_FAULT:
reset_app_context();
io_seproxyhal_send_status(0x6A80);
ui_idle();
break;
default:
PRINTF("Unexpected parser status\n");
reset_app_context();
io_seproxyhal_send_status(0x6A80);
ui_idle();
2020-06-27 13:24:04 +02:00
}
if (txResult == USTREAM_FINISHED) {
finalizeParsing(false);
}
return 0;
}
unsigned int io_seproxyhal_touch_data_cancel(__attribute__((unused)) const bagl_element_t *e) {
2020-06-27 13:24:04 +02:00
reset_app_context();
io_seproxyhal_send_status(0x6985);
// Display back the original UX
ui_idle();
2020-12-01 16:20:13 +01:00
return 0; // do not redraw the widget
2020-06-27 13:24:04 +02:00
}