Skip to content

Commit

Permalink
Merge pull request #534 from LedgerHQ/feat/apa/evm_swap
Browse files Browse the repository at this point in the history
EVM swap
  • Loading branch information
apaillier-ledger authored Feb 13, 2024
2 parents df74ace + 62d8b32 commit 8a1ce2f
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 28 deletions.
4 changes: 3 additions & 1 deletion src/eth_plugin_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ void eth_plugin_prepare_query_contract_UI(ethQueryContractUI_t *queryContractUI,

queryContractUI->screenIndex = screenIndex;
chain_id = get_tx_chain_id();
strlcpy(queryContractUI->network_ticker, get_displayable_ticker(&chain_id), MAX_TICKER_LEN);
strlcpy(queryContractUI->network_ticker,
get_displayable_ticker(&chain_id, chainConfig),
sizeof(queryContractUI->network_ticker));
queryContractUI->title = title;
queryContractUI->titleLength = titleLength;
queryContractUI->msg = msg;
Expand Down
28 changes: 14 additions & 14 deletions src/handle_get_printable_amount.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,32 @@
#include "common_utils.h"
#include "uint256.h"
#include "string.h"
#include "network.h"

void handle_get_printable_amount(get_printable_amount_parameters_t* params,
chain_config_t* config) {
uint8_t decimals;
char ticker[MAX_TICKER_LEN];
uint8_t decimals;
uint64_t chain_id = 0;

memset(params->printable_amount, 0, sizeof(params->printable_amount));
if (params->amount_length > 32) {
PRINTF("Amount is too big, 32 bytes max but buffer has %u bytes", params->amount_length);
return;
}

// If the amount is a fee, its value is nominated in ETH even if we're doing an ERC20 swap
if (!parse_swap_config(params->coin_configuration,
params->coin_configuration_length,
ticker,
&decimals,
&chain_id)) {
PRINTF("Error while parsing config\n");
return;
}
// If the amount is a fee, the ticker should be the chain's native currency
if (params->is_fee) {
uint8_t ticker_len = strnlen(config->coinName, sizeof(config->coinName));
memcpy(ticker, config->coinName, ticker_len);
ticker[ticker_len] = '\0';
strlcpy(ticker, get_displayable_ticker(&chain_id, config), sizeof(ticker));
decimals = WEI_TO_ETHER;
} else {
// If the amount is *not* a fee, decimals and ticker are built from the given config
if (!parse_swap_config(params->coin_configuration,
params->coin_configuration_length,
ticker,
&decimals)) {
PRINTF("Error while parsing config\n");
return;
}
}

if (!amountToString(params->amount,
Expand Down
10 changes: 7 additions & 3 deletions src/handle_swap_sign_transaction.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "handle_swap_sign_transaction.h"
#include "shared_context.h"
#include "common_utils.h"
#include "network.h"
#ifdef HAVE_NBGL
#include "nbgl_use_case.h"
#endif // HAVE_NBGL
Expand All @@ -27,12 +28,15 @@ bool copy_transaction_parameters(create_transaction_parameters_t* sign_transacti
return false;
}

uint8_t decimals;
char ticker[MAX_TICKER_LEN];
uint8_t decimals;
uint64_t chain_id = 0;

if (!parse_swap_config(sign_transaction_params->coin_configuration,
sign_transaction_params->coin_configuration_length,
ticker,
&decimals)) {
&decimals,
&chain_id)) {
PRINTF("Error while parsing config\n");
return false;
}
Expand All @@ -46,7 +50,7 @@ bool copy_transaction_parameters(create_transaction_parameters_t* sign_transacti
}

// If the amount is a fee, its value is nominated in ETH even if we're doing an ERC20 swap
strlcpy(ticker, config->coinName, MAX_TICKER_LEN);
strlcpy(ticker, get_displayable_ticker(&chain_id, config), sizeof(ticker));
decimals = WEI_TO_ETHER;
if (!amountToString(sign_transaction_params->fee_amount,
sign_transaction_params->fee_amount_length,
Expand Down
4 changes: 2 additions & 2 deletions src/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ uint64_t get_tx_chain_id(void) {
return chain_id;
}

const char *get_displayable_ticker(const uint64_t *chain_id) {
const char *get_displayable_ticker(const uint64_t *chain_id, const chain_config_t *chain_cfg) {
const char *ticker = get_network_ticker_from_chain_id(chain_id);

if (ticker == NULL) {
ticker = chainConfig->coinName;
ticker = chain_cfg->coinName;
}
return ticker;
}
Expand Down
3 changes: 2 additions & 1 deletion src/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <stdint.h>
#include <stdbool.h>
#include "chainConfig.h"

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

uint64_t get_tx_chain_id(void);

const char *get_displayable_ticker(const uint64_t *chain_id);
const char *get_displayable_ticker(const uint64_t *chain_id, const chain_config_t *chain_cfg);
24 changes: 20 additions & 4 deletions src/swap_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,38 @@
#include "asset_info.h"
#include "swap_utils.h"

bool parse_swap_config(const uint8_t *config, uint8_t config_len, char *ticker, uint8_t *decimals) {
bool parse_swap_config(const uint8_t *config,
uint8_t config_len,
char *ticker,
uint8_t *decimals,
uint64_t *chain_id) {
uint8_t ticker_len, offset = 0;

if (config_len == 0) {
return false;
}
ticker_len = config[offset++];
if (ticker_len == 0 || ticker_len > MAX_TICKER_LEN - 2 || config_len - offset < ticker_len) {
ticker_len = config[offset];
offset += sizeof(ticker_len);
if ((ticker_len == 0) || (ticker_len > (MAX_TICKER_LEN - 2)) ||
((config_len - offset) < (ticker_len))) {
return false;
}
memcpy(ticker, config + offset, ticker_len);
offset += ticker_len;
ticker[ticker_len] = '\0';

if (config_len - offset < 1) {
if ((config_len - offset) < 1) {
return false;
}
*decimals = config[offset];
offset += sizeof(*decimals);

// the chain ID was adder later to the CAL swap subconfig
// so it is optional for retro-compatibility (as it might not be present)
if ((config_len - offset) >= sizeof(*chain_id)) {
PRINTF("Chain ID from the swap subconfig = 0x%.*h\n", sizeof(*chain_id), &config[offset]);
*chain_id = u64_from_BE(config + offset, sizeof(*chain_id));
offset += sizeof(*chain_id);
}
return true;
}
6 changes: 5 additions & 1 deletion src/swap_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@

#include <stdint.h>

bool parse_swap_config(const uint8_t* config, uint8_t config_len, char* ticker, uint8_t* decimals);
bool parse_swap_config(const uint8_t* config,
uint8_t config_len,
char* ticker,
uint8_t* decimals,
uint64_t* chain_id);
4 changes: 2 additions & 2 deletions src_features/signTx/logic_signTx.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ static void address_to_string(uint8_t *in,

static void raw_fee_to_string(uint256_t *rawFee, char *displayBuffer, uint32_t displayBufferSize) {
uint64_t chain_id = get_tx_chain_id();
const char *feeTicker = get_displayable_ticker(&chain_id);
const char *feeTicker = get_displayable_ticker(&chain_id, chainConfig);
uint8_t tickerOffset = 0;
uint32_t i;

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

*use_standard_UI = true;
Expand Down

0 comments on commit 8a1ce2f

Please sign in to comment.