network.c functions cleanup
This commit is contained in:
@@ -47,6 +47,8 @@ void eth_plugin_prepare_query_contract_UI(ethQueryContractUI_t *queryContractUI,
|
||||
uint32_t titleLength,
|
||||
char *msg,
|
||||
uint32_t msgLength) {
|
||||
uint64_t chain_id;
|
||||
|
||||
memset((uint8_t *) queryContractUI, 0, sizeof(ethQueryContractUI_t));
|
||||
|
||||
// If no extra information was found, set the pointer to NULL
|
||||
@@ -64,7 +66,8 @@ void eth_plugin_prepare_query_contract_UI(ethQueryContractUI_t *queryContractUI,
|
||||
}
|
||||
|
||||
queryContractUI->screenIndex = screenIndex;
|
||||
strlcpy(queryContractUI->network_ticker, get_tx_network_ticker(), MAX_TICKER_LEN);
|
||||
chain_id = get_tx_chain_id();
|
||||
strlcpy(queryContractUI->network_ticker, get_displayable_ticker(&chain_id), MAX_TICKER_LEN);
|
||||
queryContractUI->title = title;
|
||||
queryContractUI->titleLength = titleLength;
|
||||
queryContractUI->msg = msg;
|
||||
|
||||
@@ -27,9 +27,12 @@
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "shared_context.h"
|
||||
#include "utils.h"
|
||||
#include "ethUtils.h"
|
||||
#include "chainConfig.h"
|
||||
#include "ethUstream.h"
|
||||
#include "network.h"
|
||||
|
||||
bool rlpCanDecode(uint8_t *buffer, uint32_t bufferLength, bool *valid) {
|
||||
if (*buffer <= 0x7f) {
|
||||
@@ -301,3 +304,32 @@ bool adjustDecimals(const char *src,
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Returns the chain ID. Defaults to 0 if txType was not found (For TX).
|
||||
uint64_t get_tx_chain_id(void) {
|
||||
uint64_t chain_id = 0;
|
||||
|
||||
switch (txContext.txType) {
|
||||
case LEGACY:
|
||||
chain_id = u64_from_BE(txContext.content->v, txContext.content->vLength);
|
||||
break;
|
||||
case EIP2930:
|
||||
case EIP1559:
|
||||
chain_id = u64_from_BE(tmpContent.txContent.chainID.value,
|
||||
tmpContent.txContent.chainID.length);
|
||||
break;
|
||||
default:
|
||||
PRINTF("Txtype `%d` not supported while generating chainID\n", txContext.txType);
|
||||
break;
|
||||
}
|
||||
return chain_id;
|
||||
}
|
||||
|
||||
const char *get_displayable_ticker(const uint64_t *chain_id) {
|
||||
const char *ticker = get_network_ticker_from_chain_id(chain_id);
|
||||
|
||||
if (ticker == NULL) {
|
||||
ticker = chainConfig->coinName;
|
||||
}
|
||||
return ticker;
|
||||
}
|
||||
|
||||
@@ -84,6 +84,10 @@ static __attribute__((no_instrument_function)) inline int ismaxint(uint8_t *buf,
|
||||
return 1;
|
||||
}
|
||||
|
||||
uint64_t get_tx_chain_id(void);
|
||||
|
||||
const char *get_displayable_ticker(const uint64_t *chain_id);
|
||||
|
||||
static const char HEXDIGITS[] = "0123456789abcdef";
|
||||
|
||||
#endif // _ETHUTILS_H_
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include "os_utils.h"
|
||||
#include "os_pic.h"
|
||||
#include "network.h"
|
||||
#include "os.h"
|
||||
#include "shared_context.h"
|
||||
#include "utils.h"
|
||||
|
||||
typedef enum { APP, TX } e_net_type;
|
||||
typedef struct network_info_s {
|
||||
const char *name;
|
||||
const char *ticker;
|
||||
uint64_t chain_id;
|
||||
} network_info_t;
|
||||
|
||||
// Mappping of chain ids to networks.
|
||||
static const network_info_t NETWORK_MAPPING[] = {
|
||||
@@ -66,73 +66,29 @@ static const network_info_t NETWORK_MAPPING[] = {
|
||||
{.chain_id = 39797, .name = "Energi", .ticker = "NRG"},
|
||||
{.chain_id = 248, .name = "Oasys", .ticker = "OAS"}};
|
||||
|
||||
uint64_t get_tx_chain_id(void) {
|
||||
uint64_t chain_id = 0;
|
||||
|
||||
switch (txContext.txType) {
|
||||
case LEGACY:
|
||||
chain_id = u64_from_BE(txContext.content->v, txContext.content->vLength);
|
||||
break;
|
||||
case EIP2930:
|
||||
case EIP1559:
|
||||
chain_id = u64_from_BE(tmpContent.txContent.chainID.value,
|
||||
tmpContent.txContent.chainID.length);
|
||||
break;
|
||||
default:
|
||||
PRINTF("Txtype `%d` not supported while generating chainID\n", txContext.txType);
|
||||
break;
|
||||
}
|
||||
return 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]);
|
||||
static const network_info_t *get_network_from_chain_id(const uint64_t *chain_id) {
|
||||
for (size_t i = 0; i < ARRAYLEN(NETWORK_MAPPING); i++) {
|
||||
if (NETWORK_MAPPING[i].chain_id == *chain_id) {
|
||||
return (const network_info_t *) &NETWORK_MAPPING[i];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static const char *get_network_name(e_net_type type) {
|
||||
const network_info_t *network = get_network(type);
|
||||
if (network == NULL) {
|
||||
const char *get_network_name_from_chain_id(const uint64_t *chain_id) {
|
||||
const network_info_t *net = get_network_from_chain_id(chain_id);
|
||||
|
||||
if (net == NULL) {
|
||||
return NULL;
|
||||
} else {
|
||||
return (const char *) PIC(network->name);
|
||||
}
|
||||
return PIC(net->name);
|
||||
}
|
||||
|
||||
const char *get_app_network_name(void) {
|
||||
return get_network_name(APP);
|
||||
}
|
||||
const char *get_network_ticker_from_chain_id(const uint64_t *chain_id) {
|
||||
const network_info_t *net = get_network_from_chain_id(chain_id);
|
||||
|
||||
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);
|
||||
if (net == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
const char *get_app_network_ticker(void) {
|
||||
return get_network_ticker(APP);
|
||||
}
|
||||
|
||||
const char *get_tx_network_ticker(void) {
|
||||
return get_network_ticker(TX);
|
||||
return PIC(net->ticker);
|
||||
}
|
||||
|
||||
@@ -2,24 +2,9 @@
|
||||
#define _NETWORK_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include "tokens.h"
|
||||
#include "shared_context.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef struct network_info_s {
|
||||
const char *name;
|
||||
const char *ticker;
|
||||
uint64_t chain_id;
|
||||
} network_info_t;
|
||||
|
||||
// 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);
|
||||
// Returns a pointer to the network name, or NULL if there is none.
|
||||
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_tx_network_ticker(void);
|
||||
const char *get_app_network_ticker(void);
|
||||
const char *get_network_name_from_chain_id(const uint64_t *chain_id);
|
||||
const char *get_network_ticker_from_chain_id(const uint64_t *chain_id);
|
||||
|
||||
#endif // _NETWORK_H_
|
||||
|
||||
@@ -196,7 +196,8 @@ 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_tx_network_ticker();
|
||||
uint64_t chain_id = get_tx_chain_id();
|
||||
const char *feeTicker = get_displayable_ticker(&chain_id);
|
||||
uint8_t tickerOffset = 0;
|
||||
uint32_t i;
|
||||
|
||||
@@ -263,10 +264,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_tx_network_name();
|
||||
uint64_t chain_id = get_tx_chain_id();
|
||||
const char *name = get_network_name_from_chain_id(&chain_id);
|
||||
if (name == NULL) {
|
||||
// No network name found so simply copy the chain ID as the network name.
|
||||
uint64_t chain_id = get_tx_chain_id();
|
||||
u64_to_string(chain_id, out, out_size);
|
||||
} else {
|
||||
// Network name found, simply copy it.
|
||||
@@ -313,7 +314,8 @@ 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_tx_network_ticker();
|
||||
uint64_t chain_id = get_tx_chain_id();
|
||||
const char *ticker = get_displayable_ticker(&chain_id);
|
||||
ethPluginFinalize_t pluginFinalize;
|
||||
bool use_standard_UI = true;
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#include "shared_context.h"
|
||||
#include "ui_callbacks.h"
|
||||
#include "ui_nbgl.h"
|
||||
#include "network.h"
|
||||
#include "ethUtils.h"
|
||||
#include "plugins.h"
|
||||
#include "domain_name.h"
|
||||
|
||||
|
||||
@@ -47,7 +47,8 @@ void ui_idle(void) {
|
||||
tagline = staxSharedBuffer;
|
||||
}
|
||||
} else { // Ethereum app
|
||||
app_name = get_app_network_name();
|
||||
uint64_t mainnet_chain_id = ETHEREUM_MAINNET_CHAINID;
|
||||
app_name = get_network_name_from_chain_id(&mainnet_chain_id);
|
||||
}
|
||||
|
||||
nbgl_useCaseHome((char *) app_name,
|
||||
|
||||
Reference in New Issue
Block a user