Skip to content

Commit 5af9a28

Browse files
Merge pull request #922 from LedgerHQ/fbe/swap_doc
Add more comments for swap and add markers for SWAP documentation
2 parents 0ef6fe7 + 0c72d5a commit 5af9a28

File tree

4 files changed

+152
-52
lines changed

4 files changed

+152
-52
lines changed

lib_standard_app/main.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,13 @@ WEAK void standalone_app_main(void)
114114
}
115115

116116
#ifdef HAVE_SWAP
117+
// --8<-- [start:library_app_main]
118+
/* This function is called by the main() function if this application was started by Exchange
119+
* through an os_lib_call() as opposed to being started from the Dashboard.
120+
*
121+
* We dispatch the Exchange request to the correct handler.
122+
* Handlers content are not defined in the `lib_standard_app`
123+
*/
117124
WEAK void library_app_main(libargs_t *args)
118125
{
119126
BEGIN_TRY
@@ -165,6 +172,7 @@ WEAK void library_app_main(libargs_t *args)
165172
}
166173
END_TRY;
167174
}
175+
// --8<-- [end:library_app_main]
168176
#endif // HAVE_SWAP
169177

170178
WEAK __attribute__((section(".boot"))) int main(int arg0)

lib_standard_app/swap_entrypoints.h

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,46 @@
2222
#include "swap_lib_calls.h"
2323

2424
/*
25-
* These functions must be defined by the application
26-
*/
25+
--8<-- [start:swap_entry_point_intro]
26+
The handle functions must be defined by each Coin application implementing the SWAP feature.
27+
28+
Handlers are called by Exchange through the `os_lib_call` API, and dispatched by the `main()`
29+
and `library_app_main()` functions of the lib_standard_app.
2730
28-
/* Check check_address_parameters_t.address_to_check against specified parameters.
31+
The Exchange application is responsible for handling the flow and sequencing of the SWAP.
32+
--8<-- [end:swap_entry_point_intro]
33+
*/
34+
35+
// --8<-- [start:swap_handle_check_address]
36+
/* This handle is called when the Exchange application wants to ensure that a
37+
* given address belongs to the device.
2938
*
30-
* Must set params.result to 0 on error, 1 otherwise */
39+
* If the address does belong to the device, result is set to 1. Otherwise it
40+
* is set to 0.
41+
*/
3142
void swap_handle_check_address(check_address_parameters_t *params);
43+
// --8<-- [end:swap_handle_check_address]
3244

33-
/* Format printable amount including the ticker from specified parameters.
45+
// --8<-- [start:swap_handle_get_printable_amount]
46+
/* This handle is called when the Exchange application wants to format for
47+
* display an amount + ticker of a currency known by this application
3448
*
35-
* Must set empty printable_amount on error, printable amount otherwise */
49+
* If the formatting succeeds, result is set to the formatted string. Otherwise
50+
* it is set to '\0'.
51+
*/
3652
void swap_handle_get_printable_amount(get_printable_amount_parameters_t *params);
53+
// --8<-- [end:swap_handle_get_printable_amount]
3754

38-
/* Backup up transaction parameters and wipe BSS to avoid collusion with
39-
* app-exchange BSS data.
55+
// --8<-- [start:swap_copy_transaction_parameters]
56+
/* This handle is called when the user has validated on screen the transaction
57+
* proposal sent by the partner and started the FROM Coin application to sign
58+
* the payment transaction.
59+
*
60+
* This handler needs to save in the heap the details of what has been validated
61+
* in Exchange. These elements will be checked against the received transaction
62+
* upon its reception from the Ledger Live.
4063
*
41-
* return false on error, true otherwise */
64+
* return false on error, true otherwise
65+
*/
4266
bool swap_copy_transaction_parameters(create_transaction_parameters_t *sign_transaction_params);
67+
// --8<-- [end:swap_copy_transaction_parameters]

lib_standard_app/swap_error_code_helpers.h

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,14 @@
66

77
// clang-format off
88
/*
9-
# Error response for applications started by Exchange in SWAP context
9+
--8<-- [start:intro]
10+
# Error responses for applications started by Exchange in SWAP context
11+
12+
This specification applies to the error responses returned by the Coin applications when started by
13+
Exchange for the final payment transaction of a SWAP.
14+
15+
Replying valuable data when a final payment transaction is refused eases a lot the analysis,
16+
especially if the issue happens in production context and / or is hard to reproduce.
1017
1118
## RAPDU status word
1219
@@ -18,28 +25,33 @@ The first 2 bytes of the RAPDU data represent the error code. Format is 16 bits
1825
1926
The upper byte is common between all applications. It must be one of the following value:
2027
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. |
28+
| Name | Value | Description |
29+
| ----------------------------- | ------ | ----------------------------------------------------------------------------------------------------------------------------------------------- |
30+
| ERROR_INTERNAL | 0x00 | Internal application error, forward to the Firmware team for analysis. |
31+
| ERROR_WRONG_AMOUNT | 0x01 | The amount does not match the one validated in Exchange. |
32+
| ERROR_WRONG_DESTINATION | 0x02 | The destination address does not match the one validated in Exchange. |
33+
| ERROR_WRONG_FEES | 0x03 | The fees are different from what was validated in Exchange. |
34+
| ERROR_WRONG_METHOD | 0x04 | The method used is invalid in Exchange context. |
35+
| ERROR_CROSSCHAIN_WRONG_MODE | 0x05 | The mode used for the cross-chain hash validation is not supported. |
36+
| ERROR_CROSSCHAIN_WRONG_METHOD | 0x06 | The method used is invalid in cross-chain Exchange context. |
37+
| ERROR_CROSSCHAIN_WRONG_HASH | 0x07 | The hash for the cross-chain transaction does not match the validated value. |
38+
| ERROR_GENERIC | 0xFF | A generic or unspecified error not covered by the specific error codes above.<br>Refer to the remaining bytes for further details on the error. |
3339
34-
The lower byte can be set by the application to refine the error code returned
40+
The lower byte can be set by the application to refine the error code returned.
3541
36-
So the error code for ERROR_WRONG_METHOD would be 0x04XX with XX being application specific (can be 00 if there is nothing to refine)
42+
So the error code for `ERROR_WRONG_METHOD` would be `0x04XX` with `XX` being application specific
43+
(can be `00` if there is nothing to refine).
3744
3845
The remaining bytes of the data are application-specific and can include, but are not limited to:
46+
3947
- Debugging information (e.g., error logs or internal state).
4048
- Field values (e.g., expected vs actual amounts, destination, fees).
4149
- More specific error codes tailored to the application's context.
42-
*/
50+
51+
The standard application library define several helper function to return error codes from the Coin
52+
application.
53+
--8<-- [end:intro]
54+
*/
4355
// clang-format on
4456

4557
typedef enum swap_error_common_code_e {
@@ -54,8 +66,10 @@ typedef enum swap_error_common_code_e {
5466
SWAP_EC_ERROR_GENERIC = 0xFF,
5567
} swap_error_common_code_t;
5668

69+
// --8<-- [start:helpers]
5770
/**
5871
* Sends a basic swap error with no extra data.
72+
*
5973
* @param status_word RAPDU status word.
6074
* @param common_error_code Common error code defined in swap_error_common_code_t.
6175
* @param application_specific_error_code Application-specific error code.
@@ -66,6 +80,7 @@ __attribute__((noreturn)) void send_swap_error_simple(uint16_t status_word,
6680

6781
/**
6882
* Sends a swap error with one additional buffer data.
83+
*
6984
* @param status_word RAPDU status word.
7085
* @param common_error_code Common error code.
7186
* @param application_specific_error_code Application-specific error code.
@@ -77,7 +92,8 @@ __attribute__((noreturn)) void send_swap_error_with_buffer(uint16_t status_word,
7792
const buffer_t buffer_data);
7893

7994
/**
80-
* Sends a swap error with multiple buffers containing error details.
95+
* Sends a swap error with multiple buffers containing error details as data.
96+
*
8197
* @param status_word RAPDU status word.
8298
* @param common_error_code Common error code.
8399
* @param application_specific_error_code Application-specific error code.
@@ -92,7 +108,8 @@ __attribute__((noreturn)) void send_swap_error_with_buffers(uint16_t status_word
92108
size_t count);
93109

94110
/**
95-
* Macro to send a swap error using a formatted string.
111+
* Macro to send a swap error with a formatted string as data.
112+
*
96113
* Constructs a buffer from a formatted string and passes it to send_swap_error_with_buffers.
97114
* @param status_word RAPDU status word.
98115
* @param common_error_code Common error code.
@@ -120,5 +137,6 @@ __attribute__((noreturn)) void send_swap_error_with_buffers(uint16_t status_word
120137
send_swap_error_with_buffers( \
121138
status_word, common_error_code, application_specific_error_code, &string_buffer, 1); \
122139
} while (0)
140+
// --8<-- [end:helpers]
123141

124142
#endif // HAVE_SWAP

lib_standard_app/swap_lib_calls.h

Lines changed: 74 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
#pragma once
22

3-
/* This file is the shared API between Exchange and the apps started in Library mode for Exchange
4-
*
5-
* DO NOT MODIFY THIS FILE IN APPLICATIONS OTHER THAN EXCHANGE
6-
* On modification in Exchange, forward the changes to all applications supporting Exchange
7-
*/
3+
/*
4+
--8<-- [start:swap_lib_calls_intro]
5+
This file is the shared API between the Exchange application and the coin applications started in
6+
Library mode for Exchange.
7+
8+
On Coin application side, the main() of the lib_standard_app has the logic to recognize a library
9+
start from a dashboard start and the logic to dispatch the library commands to the correct handler
10+
function.
11+
--8<-- [end:swap_lib_calls_intro]
12+
*/
813

914
#include "stdbool.h"
1015
#include "stdint.h"
1116

12-
#define RUN_APPLICATION 1
13-
14-
#define SIGN_TRANSACTION 2
15-
16-
#define CHECK_ADDRESS 3
17-
17+
// The os_lib_calls commands that can be called by the Exchange application.
18+
// They are handled by the lib_standard_app main() function.
19+
#define RUN_APPLICATION 1
20+
#define SIGN_TRANSACTION 2
21+
#define CHECK_ADDRESS 3
1822
#define GET_PRINTABLE_AMOUNT 4
1923

2024
/*
@@ -27,47 +31,91 @@
2731
*/
2832
#define MAX_PRINTABLE_AMOUNT_SIZE 50
2933

30-
// structure that should be send to specific coin application to get address
34+
// Structure parameter used by swap_handle_check_address
35+
// --8<-- [start:check_address_parameters_t]
3136
typedef struct check_address_parameters_s {
32-
// IN
37+
// INPUTS //
38+
// Additional data when dealing with tokens
39+
// Content is coin application specific
3340
uint8_t *coin_configuration;
3441
uint8_t coin_configuration_length;
42+
3543
// serialized path, segwit, version prefix, hash used, dictionary etc.
36-
// fields and serialization format depends on specific coin app
44+
// fields and serialization format are coin application specific
3745
uint8_t *address_parameters;
3846
uint8_t address_parameters_length;
39-
char *address_to_check;
40-
char *extra_id_to_check;
41-
// OUT
47+
48+
// The address to check
49+
char *address_to_check;
50+
51+
// Extra content that may be relevant depending on context: memo, calldata, ...
52+
// Content is coin application specific
53+
char *extra_id_to_check;
54+
55+
// OUTPUT //
56+
// Set to 1 if the address belongs to the device. 0 otherwise.
4257
int result;
4358
} check_address_parameters_t;
59+
// --8<-- [end:check_address_parameters_t]
4460

45-
// structure that should be send to specific coin application to get printable amount
61+
// Structure parameter used by swap_handle_get_printable_amount
62+
// --8<-- [start:get_printable_amount_parameters_t]
4663
typedef struct get_printable_amount_parameters_s {
47-
// IN
64+
// INPUTS //
65+
// Additional data when dealing with tokens
66+
// Content is coin application specific
4867
uint8_t *coin_configuration;
4968
uint8_t coin_configuration_length;
69+
70+
// Raw amount in big number format
5071
uint8_t *amount;
5172
uint8_t amount_length;
52-
bool is_fee;
53-
// OUT
73+
74+
// Set to true if the amount to format is the fee of the swap.
75+
bool is_fee;
76+
77+
// OUTPUT //
78+
// Set to the formatted string if the formatting succeeds. 0 otherwise.
5479
char printable_amount[MAX_PRINTABLE_AMOUNT_SIZE];
5580
} get_printable_amount_parameters_t;
81+
// --8<-- [end:get_printable_amount_parameters_t]
5682

83+
// Structure parameter used by swap_copy_transaction_parameters
84+
// --8<-- [start:create_transaction_parameters_t]
5785
typedef struct create_transaction_parameters_s {
58-
// IN
86+
// INPUTS //
87+
// Additional data when dealing with tokens
88+
// Content is coin application specific
5989
uint8_t *coin_configuration;
6090
uint8_t coin_configuration_length;
91+
92+
// The amount validated on the screen by the user
6193
uint8_t *amount;
6294
uint8_t amount_length;
95+
96+
// The fees amount validated on the screen by the user
6397
uint8_t *fee_amount;
6498
uint8_t fee_amount_length;
65-
char *destination_address;
66-
char *destination_address_extra_id;
67-
// OUT
99+
100+
// The partner address that will receive the funds
101+
char *destination_address;
102+
char *destination_address_extra_id;
103+
104+
// OUTPUT //
105+
// /!\ This parameter is handled by the lib_standard_app, DO NOT interact
106+
// with it in the Coin application
107+
//
108+
// After reception and signature or refusal of the transaction, the Coin
109+
// application will return to Exchange. This boolean is used to inform the
110+
// Exchange application of the result.
111+
// Set to 1 if the transaction was successfully signed, 0 otherwise.
68112
uint8_t result;
69113
} create_transaction_parameters_t;
114+
// --8<-- [end:create_transaction_parameters_t]
70115

116+
// --8<-- [start:libargs_t]
117+
// Parameter given through os_lib_call() to the Coin application by the Exchange application.
118+
// They are handled by the lib_standard_app main() function.
71119
typedef struct libargs_s {
72120
unsigned int id;
73121
unsigned int command;
@@ -78,3 +126,4 @@ typedef struct libargs_s {
78126
get_printable_amount_parameters_t *get_printable_amount;
79127
};
80128
} libargs_t;
129+
// --8<-- [end:libargs_t]

0 commit comments

Comments
 (0)