Skip to content

Commit

Permalink
Use new swap_error_code_helpers interface
Browse files Browse the repository at this point in the history
  • Loading branch information
fbeutin-ledger committed Feb 10, 2025
1 parent 615643b commit 0ad26ca
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 76 deletions.
5 changes: 4 additions & 1 deletion src_features/signTx/cmd_signTx.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "feature_signTx.h"
#include "eth_plugin_interface.h"
#include "apdu_constants.h"
#include "swap_error_code_helpers.h"
#ifdef HAVE_GENERIC_TX_PARSER
#include "gtp_tx_info.h"
#endif
Expand Down Expand Up @@ -84,7 +85,9 @@ uint16_t handle_parsing_status(parserStatus_e status) {
// We have encountered an error while trying to sign a SWAP type transaction
// Return dedicated error code and flag an early exit back to Exchange
G_swap_response_ready = true;
send_swap_error(ERROR_GENERIC, APP_CODE_CALLDATA_ISSUE, NULL, NULL);
send_swap_error(APDU_RESPONSE_MODE_CHECK_FAILED,
ERROR_GENERIC,
APP_CODE_CALLDATA_ISSUE);
// unreachable
os_sched_exit(0);
}
Expand Down
11 changes: 0 additions & 11 deletions src_features/signTx/feature_signTx.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@
#include <stdbool.h>
#include "ethUstream.h"

// Error codes for swap, to be moved in SDK?
#define ERROR_WRONG_AMOUNT 0x01
#define ERROR_WRONG_DESTINATION 0x02
#define ERROR_WRONG_FEES 0x03
#define ERROR_WRONG_METHOD 0x04
#define ERROR_CROSSCHAIN_WRONG_MODE 0x05
#define ERROR_CROSSCHAIN_WRONG_METHOD 0x06
#define ERROR_GENERIC 0xFF

// App codes for detail.
typedef enum {
APP_CODE_DEFAULT = 0x00,
Expand All @@ -33,8 +24,6 @@ uint16_t finalize_parsing(const txContext_t *context);
void ux_approve_tx(bool fromPlugin);
void start_signature_flow(void);

void send_swap_error(uint8_t error_code, app_code_t app_code, const char *str1, const char *str2);

uint16_t handle_parsing_status(parserStatus_e status);

uint16_t get_public_key(uint8_t *out, uint8_t outLength);
Expand Down
89 changes: 25 additions & 64 deletions src_features/signTx/logic_signTx.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "handle_swap_sign_transaction.h"
#include "os_math.h"
#include "calldata.h"
#include "swap_error_code_helpers.h"

static bool g_use_standard_ui;

Expand Down Expand Up @@ -338,56 +339,6 @@ static int strcasecmp_workaround(const char *str1, const char *str2) {
return 0;
}

__attribute__((noreturn)) void send_swap_error(uint8_t error_code,
app_code_t app_code,
const char *str1,
const char *str2) {
uint32_t tx = 0;
size_t len = 0;
PRINTF("APDU_RESPONSE_MODE_CHECK_FAILED: 0x%x\n", error_code);
// Set RAPDU error codes
G_io_apdu_buffer[tx++] = error_code;
G_io_apdu_buffer[tx++] = app_code;
// Set RAPDU error message
if (str1 != NULL) {
PRINTF("Expected %s\n", str1);
// If the string is too long, truncate it
len = MIN(strlen((const char *) str1), sizeof(G_io_apdu_buffer) - tx - 2);
memmove(G_io_apdu_buffer + tx, str1, len);
tx += len;
if (len < strlen((const char *) str1)) {
PRINTF("Truncated %s to %d bytes\n", str1, len);
G_io_apdu_buffer[tx - 1] = '*';
}
}
if (str2 != NULL) {
PRINTF("Received %s\n", str2);
// Do we have enough space to add a separator?
if ((tx + 1 + 2) < sizeof(G_io_apdu_buffer)) {
G_io_apdu_buffer[tx++] = '#';
}
// Do we have enough space to add at least one character?
if ((tx + 1 + 2) < sizeof(G_io_apdu_buffer)) {
// If the string is too long, truncate it
len = MIN(strlen((const char *) str2), sizeof(G_io_apdu_buffer) - tx - 2);
memmove(G_io_apdu_buffer + tx, str2, len);
tx += len;
if (len < strlen((const char *) str2)) {
PRINTF("Truncated %s to %d bytes\n", str2, len);
G_io_apdu_buffer[tx - 1] = '*';
}
}
}
// Set RAPDU status word, with previous check we are sure there is at least 2 bytes left
U2BE_ENCODE(G_io_apdu_buffer, tx, APDU_RESPONSE_MODE_CHECK_FAILED);
tx += 2;
// Send RAPDU
io_exchange(CHANNEL_APDU | IO_RETURN_AFTER_TX, tx);
// In case of success, the apdu is sent immediately and eth exits
// Reaching this code means we encountered an error
finalize_exchange_sign_transaction(false);
}

__attribute__((noinline)) static uint16_t finalize_parsing_helper(const txContext_t *context) {
char displayBuffer[50];
uint8_t decimals = WEI_TO_ETHER;
Expand Down Expand Up @@ -523,14 +474,18 @@ __attribute__((noinline)) static uint16_t finalize_parsing_helper(const txContex
// User has just validated a swap but ETH received apdus about a non standard plugin /
// contract
if (!g_use_standard_ui) {
send_swap_error(ERROR_WRONG_METHOD, APP_CODE_NO_STANDARD_UI, NULL, NULL);
send_swap_error(APDU_RESPONSE_MODE_CHECK_FAILED,
ERROR_WRONG_METHOD,
APP_CODE_NO_STANDARD_UI);
// unreachable
os_sched_exit(0);
}
// Two success cases: we are in standard mode and no calldata was received
// We are in crosschain mode and the correct calldata has been received
if (G_swap_mode != SWAP_MODE_STANDARD && G_swap_mode != SWAP_MODE_CROSSCHAIN_SUCCESS) {
send_swap_error(ERROR_CROSSCHAIN_WRONG_MODE, APP_CODE_DEFAULT, NULL, NULL);
send_swap_error(APDU_RESPONSE_MODE_CHECK_FAILED,
ERROR_CROSSCHAIN_WRONG_MODE,
APP_CODE_DEFAULT);
// unreachable
os_sched_exit(0);
}
Expand Down Expand Up @@ -565,10 +520,12 @@ __attribute__((noinline)) static uint16_t finalize_parsing_helper(const txContex
if (G_called_from_swap) {
// Ensure the values are the same that the ones that have been previously validated
if (strcasecmp_workaround(strings.common.toAddress, displayBuffer) != 0) {
send_swap_error(ERROR_WRONG_DESTINATION,
APP_CODE_DEFAULT,
strings.common.toAddress,
displayBuffer);
send_swap_error_with_string(APDU_RESPONSE_MODE_CHECK_FAILED,
ERROR_WRONG_DESTINATION,
APP_CODE_DEFAULT,
"%s != %s",
strings.common.toAddress,
displayBuffer);
// unreachable
os_sched_exit(0);
}
Expand All @@ -592,10 +549,12 @@ __attribute__((noinline)) static uint16_t finalize_parsing_helper(const txContex
if (G_called_from_swap) {
// Ensure the values are the same that the ones that have been previously validated
if (strcmp(strings.common.fullAmount, displayBuffer) != 0) {
send_swap_error(ERROR_WRONG_AMOUNT,
APP_CODE_DEFAULT,
strings.common.fullAmount,
displayBuffer);
send_swap_error_with_string(APDU_RESPONSE_MODE_CHECK_FAILED,
ERROR_WRONG_AMOUNT,
APP_CODE_DEFAULT,
"%s != %s",
strings.common.fullAmount,
displayBuffer);
// unreachable
os_sched_exit(0);
}
Expand All @@ -616,10 +575,12 @@ __attribute__((noinline)) static uint16_t finalize_parsing_helper(const txContex
if (G_called_from_swap) {
// Ensure the values are the same that the ones that have been previously validated
if (strcmp(strings.common.maxFee, displayBuffer) != 0) {
send_swap_error(ERROR_WRONG_FEES,
APP_CODE_DEFAULT,
strings.common.maxFee,
displayBuffer);
send_swap_error_with_string(APDU_RESPONSE_MODE_CHECK_FAILED,
ERROR_WRONG_FEES,
APP_CODE_DEFAULT,
"%s != %s",
strings.common.maxFee,
displayBuffer);
// unreachable
os_sched_exit(0);
}
Expand Down

0 comments on commit 0ad26ca

Please sign in to comment.