Can now read a chain ID from the swap sub coin config

This commit is contained in:
Alexandre Paillier
2024-02-08 15:10:59 +01:00
parent c84794b257
commit 62d8b327d6
4 changed files with 46 additions and 22 deletions

View File

@@ -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,

View File

@@ -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
@@ -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;
}
@@ -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,

View File

@@ -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;
}

View File

@@ -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);