From b5c58b59cf59b0d7561bd6220cd481c5654e0698 Mon Sep 17 00:00:00 2001 From: Alexandre Paillier Date: Thu, 4 May 2023 11:52:16 +0200 Subject: [PATCH] network: cleanup --- src/eth_plugin_handler.c | 2 +- src_bagl/ui_flow_signTx.c | 4 +-- src_common/network.c | 40 ++++++++++++++++++++++++------ src_common/network.h | 17 ++++++++----- src_features/signTx/logic_signTx.c | 10 ++++---- 5 files changed, 52 insertions(+), 21 deletions(-) diff --git a/src/eth_plugin_handler.c b/src/eth_plugin_handler.c index 3aa6883..29e965f 100644 --- a/src/eth_plugin_handler.c +++ b/src/eth_plugin_handler.c @@ -64,7 +64,7 @@ void eth_plugin_prepare_query_contract_UI(ethQueryContractUI_t *queryContractUI, } queryContractUI->screenIndex = screenIndex; - strlcpy(queryContractUI->network_ticker, get_network_ticker(), MAX_TICKER_LEN); + strlcpy(queryContractUI->network_ticker, get_tx_network_ticker(), MAX_TICKER_LEN); queryContractUI->title = title; queryContractUI->titleLength = titleLength; queryContractUI->msg = msg; diff --git a/src_bagl/ui_flow_signTx.c b/src_bagl/ui_flow_signTx.c index 2b55922..c60f46d 100644 --- a/src_bagl/ui_flow_signTx.c +++ b/src_bagl/ui_flow_signTx.c @@ -220,7 +220,7 @@ void ux_approve_tx(bool fromPlugin) { // We're in a regular transaction, just show the amount and the address ux_approval_tx_flow[step++] = &ux_approval_amount_step; #ifdef HAVE_DOMAIN_NAME - uint64_t chain_id = get_chain_id(); + uint64_t chain_id = get_tx_chain_id(); if (has_domain_name(&chain_id, tmpContent.txContent.destination)) { ux_approval_tx_flow[step++] = &ux_domain_name_step; if (N_storage.verbose_domain_name) { @@ -238,7 +238,7 @@ void ux_approve_tx(bool fromPlugin) { ux_approval_tx_flow[step++] = &ux_approval_nonce_step; } - uint64_t chain_id = get_chain_id(); + uint64_t chain_id = get_tx_chain_id(); if (chainConfig->chainId == ETHEREUM_MAINNET_CHAINID && chain_id != chainConfig->chainId) { ux_approval_tx_flow[step++] = &ux_approval_network_step; } diff --git a/src_common/network.c b/src_common/network.c index f0dc4d1..1111134 100644 --- a/src_common/network.c +++ b/src_common/network.c @@ -7,6 +7,8 @@ #include "shared_context.h" #include "utils.h" +typedef enum { APP, TX } e_net_type; + // Mappping of chain ids to networks. static const network_info_t NETWORK_MAPPING[] = { {.chain_id = 1, .name = "Ethereum", .ticker = "ETH"}, @@ -64,7 +66,7 @@ static const network_info_t NETWORK_MAPPING[] = { {.chain_id = 39797, .name = "Energi", .ticker = "NRG"}, {.chain_id = 248, .name = "Oasys", .ticker = "OAS"}}; -uint64_t get_chain_id(void) { +uint64_t get_tx_chain_id(void) { uint64_t chain_id = 0; switch (txContext.txType) { @@ -83,8 +85,16 @@ uint64_t get_chain_id(void) { return chain_id; } -const network_info_t *get_network(void) { - uint64_t chain_id = get_chain_id(); +uint64_t get_app_chain_id(void) { + return chainConfig->chainId; +} + +static uint64_t get_chain_id(e_net_type type) { + return (type == APP) ? get_app_chain_id() : get_tx_chain_id(); +} + +static const network_info_t *get_network(e_net_type type) { + uint64_t chain_id = get_chain_id(type); for (size_t i = 0; i < sizeof(NETWORK_MAPPING) / sizeof(*NETWORK_MAPPING); i++) { if (NETWORK_MAPPING[i].chain_id == chain_id) { return (const network_info_t *) PIC(&NETWORK_MAPPING[i]); @@ -93,8 +103,8 @@ const network_info_t *get_network(void) { return NULL; } -const char *get_network_name(void) { - const network_info_t *network = get_network(); +static const char *get_network_name(e_net_type type) { + const network_info_t *network = get_network(type); if (network == NULL) { return NULL; } else { @@ -102,11 +112,27 @@ const char *get_network_name(void) { } } -const char *get_network_ticker(void) { - const network_info_t *network = get_network(); +const char *get_app_network_name(void) { + return get_network_name(APP); +} + +const char *get_tx_network_name(void) { + return get_network_name(TX); +} + +static const char *get_network_ticker(e_net_type type) { + const network_info_t *network = get_network(type); if (network == NULL) { return chainConfig->coinName; } else { return (char *) PIC(network->ticker); } } + +const char *get_app_network_ticker(void) { + return get_network_ticker(APP); +} + +const char *get_tx_network_ticker(void) { + return get_network_ticker(TX); +} \ No newline at end of file diff --git a/src_common/network.h b/src_common/network.h index b9ce7f3..e9069f5 100644 --- a/src_common/network.h +++ b/src_common/network.h @@ -11,13 +11,18 @@ typedef struct network_info_s { uint64_t chain_id; } network_info_t; -// Returns the current chain id. Defaults to 0 if txType was not found. -uint64_t get_chain_id(void); -// Returns a pointer to the network struct, or NULL if there is none. -const network_info_t *get_network(void); +// Returns the chain ID. Defaults to 0 if txType was not found (For TX). +uint64_t get_tx_chain_id(void); +uint64_t get_app_chain_id(void); +#ifdef HAVE_NBGL +const nbgl_icon_details_t *get_app_chain_icon(void); +#endif // HAVE_NBGL // Returns a pointer to the network name, or NULL if there is none. -const char *get_network_name(void); +const char *get_tx_network_name(void); +const char *get_app_network_name(void); + // Returns a pointer to the network ticker, or chainConfig->coinName if there is none. -const char *get_network_ticker(void); +const char *get_tx_network_ticker(void); +const char *get_app_network_ticker(void); #endif // _NETWORK_H_ diff --git a/src_features/signTx/logic_signTx.c b/src_features/signTx/logic_signTx.c index 199ccbb..c3074db 100644 --- a/src_features/signTx/logic_signTx.c +++ b/src_features/signTx/logic_signTx.c @@ -195,7 +195,7 @@ static void address_to_string(uint8_t *in, } static void raw_fee_to_string(uint256_t *rawFee, char *displayBuffer, uint32_t displayBufferSize) { - const char *feeTicker = get_network_ticker(); + const char *feeTicker = get_tx_network_ticker(); uint8_t tickerOffset = 0; uint32_t i; @@ -262,10 +262,10 @@ static void nonce_to_string(const txInt256_t *nonce, char *out, size_t out_size) } static void get_network_as_string(char *out, size_t out_size) { - const char *name = get_network_name(); + const char *name = get_tx_network_name(); if (name == NULL) { // No network name found so simply copy the chain ID as the network name. - uint64_t chain_id = get_chain_id(); + uint64_t chain_id = get_tx_chain_id(); u64_to_string(chain_id, out, out_size); } else { // Network name found, simply copy it. @@ -312,13 +312,13 @@ static int strcasecmp_workaround(const char *str1, const char *str2) { void finalizeParsing(bool direct) { char displayBuffer[50]; uint8_t decimals = WEI_TO_ETHER; - const char *ticker = get_network_ticker(); + const char *ticker = get_tx_network_ticker(); ethPluginFinalize_t pluginFinalize; bool use_standard_UI = true; // Verify the chain if (chainConfig->chainId != ETHEREUM_MAINNET_CHAINID) { - uint64_t id = get_chain_id(); + uint64_t id = get_tx_chain_id(); if (chainConfig->chainId != id) { PRINTF("Invalid chainID %u expected %u\n", id, chainConfig->chainId);