Skip to content

Commit 8a1ce2f

Browse files
Merge pull request #534 from LedgerHQ/feat/apa/evm_swap
EVM swap
2 parents df74ace + 62d8b32 commit 8a1ce2f

File tree

8 files changed

+55
-28
lines changed

8 files changed

+55
-28
lines changed

src/eth_plugin_handler.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,9 @@ void eth_plugin_prepare_query_contract_UI(ethQueryContractUI_t *queryContractUI,
6767

6868
queryContractUI->screenIndex = screenIndex;
6969
chain_id = get_tx_chain_id();
70-
strlcpy(queryContractUI->network_ticker, get_displayable_ticker(&chain_id), MAX_TICKER_LEN);
70+
strlcpy(queryContractUI->network_ticker,
71+
get_displayable_ticker(&chain_id, chainConfig),
72+
sizeof(queryContractUI->network_ticker));
7173
queryContractUI->title = title;
7274
queryContractUI->titleLength = titleLength;
7375
queryContractUI->msg = msg;

src/handle_get_printable_amount.c

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,32 @@
77
#include "common_utils.h"
88
#include "uint256.h"
99
#include "string.h"
10+
#include "network.h"
1011

1112
void handle_get_printable_amount(get_printable_amount_parameters_t* params,
1213
chain_config_t* config) {
13-
uint8_t decimals;
1414
char ticker[MAX_TICKER_LEN];
15+
uint8_t decimals;
16+
uint64_t chain_id = 0;
17+
1518
memset(params->printable_amount, 0, sizeof(params->printable_amount));
1619
if (params->amount_length > 32) {
1720
PRINTF("Amount is too big, 32 bytes max but buffer has %u bytes", params->amount_length);
1821
return;
1922
}
2023

21-
// If the amount is a fee, its value is nominated in ETH even if we're doing an ERC20 swap
24+
if (!parse_swap_config(params->coin_configuration,
25+
params->coin_configuration_length,
26+
ticker,
27+
&decimals,
28+
&chain_id)) {
29+
PRINTF("Error while parsing config\n");
30+
return;
31+
}
32+
// If the amount is a fee, the ticker should be the chain's native currency
2233
if (params->is_fee) {
23-
uint8_t ticker_len = strnlen(config->coinName, sizeof(config->coinName));
24-
memcpy(ticker, config->coinName, ticker_len);
25-
ticker[ticker_len] = '\0';
34+
strlcpy(ticker, get_displayable_ticker(&chain_id, config), sizeof(ticker));
2635
decimals = WEI_TO_ETHER;
27-
} else {
28-
// If the amount is *not* a fee, decimals and ticker are built from the given config
29-
if (!parse_swap_config(params->coin_configuration,
30-
params->coin_configuration_length,
31-
ticker,
32-
&decimals)) {
33-
PRINTF("Error while parsing config\n");
34-
return;
35-
}
3636
}
3737

3838
if (!amountToString(params->amount,

src/handle_swap_sign_transaction.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "handle_swap_sign_transaction.h"
66
#include "shared_context.h"
77
#include "common_utils.h"
8+
#include "network.h"
89
#ifdef HAVE_NBGL
910
#include "nbgl_use_case.h"
1011
#endif // HAVE_NBGL
@@ -27,12 +28,15 @@ bool copy_transaction_parameters(create_transaction_parameters_t* sign_transacti
2728
return false;
2829
}
2930

30-
uint8_t decimals;
3131
char ticker[MAX_TICKER_LEN];
32+
uint8_t decimals;
33+
uint64_t chain_id = 0;
34+
3235
if (!parse_swap_config(sign_transaction_params->coin_configuration,
3336
sign_transaction_params->coin_configuration_length,
3437
ticker,
35-
&decimals)) {
38+
&decimals,
39+
&chain_id)) {
3640
PRINTF("Error while parsing config\n");
3741
return false;
3842
}
@@ -46,7 +50,7 @@ bool copy_transaction_parameters(create_transaction_parameters_t* sign_transacti
4650
}
4751

4852
// If the amount is a fee, its value is nominated in ETH even if we're doing an ERC20 swap
49-
strlcpy(ticker, config->coinName, MAX_TICKER_LEN);
53+
strlcpy(ticker, get_displayable_ticker(&chain_id, config), sizeof(ticker));
5054
decimals = WEI_TO_ETHER;
5155
if (!amountToString(sign_transaction_params->fee_amount,
5256
sign_transaction_params->fee_amount_length,

src/network.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,11 @@ uint64_t get_tx_chain_id(void) {
136136
return chain_id;
137137
}
138138

139-
const char *get_displayable_ticker(const uint64_t *chain_id) {
139+
const char *get_displayable_ticker(const uint64_t *chain_id, const chain_config_t *chain_cfg) {
140140
const char *ticker = get_network_ticker_from_chain_id(chain_id);
141141

142142
if (ticker == NULL) {
143-
ticker = chainConfig->coinName;
143+
ticker = chain_cfg->coinName;
144144
}
145145
return ticker;
146146
}

src/network.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <stdint.h>
44
#include <stdbool.h>
5+
#include "chainConfig.h"
56

67
#define UNSUPPORTED_CHAIN_ID_MSG(id) \
78
do { \
@@ -16,4 +17,4 @@ bool app_compatible_with_chain_id(const uint64_t *chain_id);
1617

1718
uint64_t get_tx_chain_id(void);
1819

19-
const char *get_displayable_ticker(const uint64_t *chain_id);
20+
const char *get_displayable_ticker(const uint64_t *chain_id, const chain_config_t *chain_cfg);

src/swap_utils.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,38 @@
2222
#include "asset_info.h"
2323
#include "swap_utils.h"
2424

25-
bool parse_swap_config(const uint8_t *config, uint8_t config_len, char *ticker, uint8_t *decimals) {
25+
bool parse_swap_config(const uint8_t *config,
26+
uint8_t config_len,
27+
char *ticker,
28+
uint8_t *decimals,
29+
uint64_t *chain_id) {
2630
uint8_t ticker_len, offset = 0;
31+
2732
if (config_len == 0) {
2833
return false;
2934
}
30-
ticker_len = config[offset++];
31-
if (ticker_len == 0 || ticker_len > MAX_TICKER_LEN - 2 || config_len - offset < ticker_len) {
35+
ticker_len = config[offset];
36+
offset += sizeof(ticker_len);
37+
if ((ticker_len == 0) || (ticker_len > (MAX_TICKER_LEN - 2)) ||
38+
((config_len - offset) < (ticker_len))) {
3239
return false;
3340
}
3441
memcpy(ticker, config + offset, ticker_len);
3542
offset += ticker_len;
3643
ticker[ticker_len] = '\0';
3744

38-
if (config_len - offset < 1) {
45+
if ((config_len - offset) < 1) {
3946
return false;
4047
}
4148
*decimals = config[offset];
49+
offset += sizeof(*decimals);
50+
51+
// the chain ID was adder later to the CAL swap subconfig
52+
// so it is optional for retro-compatibility (as it might not be present)
53+
if ((config_len - offset) >= sizeof(*chain_id)) {
54+
PRINTF("Chain ID from the swap subconfig = 0x%.*h\n", sizeof(*chain_id), &config[offset]);
55+
*chain_id = u64_from_BE(config + offset, sizeof(*chain_id));
56+
offset += sizeof(*chain_id);
57+
}
4258
return true;
4359
}

src/swap_utils.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,8 @@
1919

2020
#include <stdint.h>
2121

22-
bool parse_swap_config(const uint8_t* config, uint8_t config_len, char* ticker, uint8_t* decimals);
22+
bool parse_swap_config(const uint8_t* config,
23+
uint8_t config_len,
24+
char* ticker,
25+
uint8_t* decimals,
26+
uint64_t* chain_id);

src_features/signTx/logic_signTx.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ static void address_to_string(uint8_t *in,
200200

201201
static void raw_fee_to_string(uint256_t *rawFee, char *displayBuffer, uint32_t displayBufferSize) {
202202
uint64_t chain_id = get_tx_chain_id();
203-
const char *feeTicker = get_displayable_ticker(&chain_id);
203+
const char *feeTicker = get_displayable_ticker(&chain_id, chainConfig);
204204
uint8_t tickerOffset = 0;
205205
uint32_t i;
206206

@@ -323,7 +323,7 @@ __attribute__((noinline)) static void finalize_parsing_helper(bool direct, bool
323323
char displayBuffer[50];
324324
uint8_t decimals = WEI_TO_ETHER;
325325
uint64_t chain_id = get_tx_chain_id();
326-
const char *ticker = get_displayable_ticker(&chain_id);
326+
const char *ticker = get_displayable_ticker(&chain_id, chainConfig);
327327
ethPluginFinalize_t pluginFinalize;
328328

329329
*use_standard_UI = true;

0 commit comments

Comments
 (0)