Skip to content

Commit

Permalink
Can now read a chain ID from the swap sub coin config
Browse files Browse the repository at this point in the history
  • Loading branch information
apaillier-ledger committed Feb 8, 2024
1 parent c84794b commit 30ecc93
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
26 changes: 13 additions & 13 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 (!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, its value is nominated in ETH even if we're doing an ERC20 swap
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
14 changes: 12 additions & 2 deletions src/swap_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@
#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;
}
Expand All @@ -38,6 +43,11 @@ bool parse_swap_config(const uint8_t *config, uint8_t config_len, char *ticker,
if (config_len - offset < 1) {
return false;
}
*decimals = config[offset];
*decimals = config[offset++];
// optional for retro-compatibility
if ((config_len - offset) >= sizeof(*chain_id)) {
*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);

0 comments on commit 30ecc93

Please sign in to comment.