Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
swap_error_code_helpers.h
Go to the documentation of this file.
1#pragma once
2#ifdef HAVE_SWAP
3
4#include "io.h"
5#include "buffer.h"
6
7// clang-format off
8/*
9# Error response for applications started by Exchange in SWAP context
10
11## RAPDU status word
12
13Each application must define a unique status word for every Exchange-related error.
14
15## RAPDU data
16
17The first 2 bytes of the RAPDU data represent the error code. Format is 16 bits integer in big endian.
18
19The upper byte is common between all applications. It must be one of the following value:
20
21| Name | Value | Description |
22| ----------------------------- | ------ | ----------------------------------------------------------------------------- |
23| ERROR_INTERNAL | 0x00 | Internal application error, forward to the firmware team for analysis. |
24| ERROR_WRONG_AMOUNT | 0x01 | The amount does not match the one validated in Exchange. |
25| ERROR_WRONG_DESTINATION | 0x02 | The destination address does not match the one validated in Exchange. |
26| ERROR_WRONG_FEES | 0x03 | The fees are different from what was validated in Exchange. |
27| ERROR_WRONG_METHOD | 0x04 | The method used is invalid in Exchange context. |
28| ERROR_CROSSCHAIN_WRONG_MODE | 0x05 | The mode used for the cross-chain hash validation is not supported. |
29| ERROR_CROSSCHAIN_WRONG_METHOD | 0x06 | The method used is invalid in cross-chain Exchange context. |
30| ERROR_CROSSCHAIN_WRONG_HASH | 0x07 | The hash for the cross-chain transaction does not match the validated value. |
31| ERROR_GENERIC | 0xFF | A generic or unspecified error not covered by the specific error codes above. |
32| | | Refer to the remaining bytes for further details on the error. |
33
34The lower byte can be set by the application to refine the error code returned
35
36So the error code for ERROR_WRONG_METHOD would be 0x04XX with XX being application specific (can be 00 if there is nothing to refine)
37
38The remaining bytes of the data are application-specific and can include, but are not limited to:
39- Debugging information (e.g., error logs or internal state).
40- Field values (e.g., expected vs actual amounts, destination, fees).
41- More specific error codes tailored to the application's context.
42 */
43// clang-format on
44
45typedef enum swap_error_common_code_e {
46 SWAP_EC_ERROR_INTERNAL = 0x00,
47 SWAP_EC_ERROR_WRONG_AMOUNT = 0x01,
48 SWAP_EC_ERROR_WRONG_DESTINATION = 0x02,
49 SWAP_EC_ERROR_WRONG_FEES = 0x03,
50 SWAP_EC_ERROR_WRONG_METHOD = 0x04,
51 SWAP_EC_ERROR_CROSSCHAIN_WRONG_MODE = 0x05,
52 SWAP_EC_ERROR_CROSSCHAIN_WRONG_METHOD = 0x06,
53 SWAP_EC_ERROR_CROSSCHAIN_WRONG_HASH = 0x07,
54 SWAP_EC_ERROR_GENERIC = 0xFF,
55} swap_error_common_code_t;
56
63__attribute__((noreturn)) void send_swap_error_simple(uint16_t status_word,
64 uint8_t common_error_code,
65 uint8_t application_specific_error_code);
66
74__attribute__((noreturn)) void send_swap_error_with_buffer(uint16_t status_word,
75 uint8_t common_error_code,
76 uint8_t application_specific_error_code,
77 const buffer_t buffer_data);
78
87#define SWAP_ERROR_HELPER_MAX_BUFFER_COUNT 8
88__attribute__((noreturn)) void send_swap_error_with_buffers(uint16_t status_word,
89 uint8_t common_error_code,
90 uint8_t application_specific_error_code,
91 const buffer_t *buffer_data,
92 size_t count);
93
103// Immediately call snprintf here (no function wrapping it cleanly in a .c file).
104// This is because we don't have a vsnprintf implementation which would be needed if
105// we were to pass the va_args to an intermediate function.
106// See https://stackoverflow.com/a/150578
107#define send_swap_error_with_string( \
108 status_word, common_error_code, application_specific_error_code, format, ...) \
109 do { \
110 /* Up to a full data apdu minus the status word and the swap error code */ \
111 char format_buffer[sizeof(G_io_apdu_buffer) - sizeof(status_word) - 2] = {0}; \
112 /* snprintf always returns 0 on our platform, don't check the return value */ \
113 /* See https://github.com/LedgerHQ/ledger-secure-sdk/issues/236 */ \
114 snprintf(format_buffer, sizeof(format_buffer), format, ##__VA_ARGS__); \
115 PRINTF("send_swap_error_with_string %s\n", format_buffer); \
116 buffer_t string_buffer; \
117 string_buffer.ptr = (uint8_t *) &format_buffer; \
118 string_buffer.size = strnlen(format_buffer, sizeof(format_buffer)); \
119 string_buffer.offset = 0; \
120 send_swap_error_with_buffers( \
121 status_word, common_error_code, application_specific_error_code, &string_buffer, 1); \
122 } while (0)
123
124#endif // HAVE_SWAP
__attribute__((section("._nbgl_fonts_"))) const
return the non-unicode font corresponding to the given font ID
Definition nbgl_fonts.c:67
unsigned short uint16_t
Definition usbd_conf.h:54
unsigned char uint8_t
Definition usbd_conf.h:53