From 3e25f04d0523d006c0fcbb707b34acbc30aa1a37 Mon Sep 17 00:00:00 2001 From: TamtamHero <10632523+TamtamHero@users.noreply.github.com> Date: Wed, 25 Aug 2021 13:43:27 +0200 Subject: [PATCH] Refactoring helper to get printable eth address from hex --- src_common/ethUtils.c | 18 ++++++++++ src_common/ethUtils.h | 6 ++++ src_features/signTx/logic_signTx.c | 11 +++--- src_features/stark_sign/ui_flow_stark_sign.c | 16 +++------ src_plugins/erc20/erc20_plugin.c | 11 +++--- src_plugins/erc721/erc721_plugin.c | 21 ++++++++--- src_plugins/eth2/eth2_plugin.c | 21 +++-------- src_plugins/starkware/starkware_plugin.c | 38 ++++++++++---------- 8 files changed, 80 insertions(+), 62 deletions(-) diff --git a/src_common/ethUtils.c b/src_common/ethUtils.c index aedcff1..e5596c0 100644 --- a/src_common/ethUtils.c +++ b/src_common/ethUtils.c @@ -219,6 +219,24 @@ void getEthAddressStringFromBinary(uint8_t *address, out[40] = '\0'; } +// Fills the `out` buffer with the lowercase string representation of the pubkey passed in as binary +// format by `in`. (eg: uint8_t*:0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB -> +// char*:"0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB\0" ) `sha3` context doesn't have have to be +// initialized prior to call. `chain_config` must be initialized. +void getEthDisplayableAddress(uint8_t *in, + char *out, + size_t out_len, + cx_sha3_t *sha3, + chain_config_t *chain_config) { + if (out_len < 43) { + strlcpy(out, "ERROR", out_len); + return; + } + out[0] = '0'; + out[1] = 'x'; + getEthAddressStringFromBinary(in, out + 2, sha3, chain_config); +} + bool adjustDecimals(char *src, uint32_t srcLength, char *target, diff --git a/src_common/ethUtils.h b/src_common/ethUtils.h index 501a2b7..386c8fd 100644 --- a/src_common/ethUtils.h +++ b/src_common/ethUtils.h @@ -51,6 +51,12 @@ void getEthAddressStringFromBinary(uint8_t *address, cx_sha3_t *sha3Context, chain_config_t *chain_config); +void getEthDisplayableAddress(uint8_t *in, + char *out, + size_t out_len, + cx_sha3_t *sha3, + chain_config_t *chain_config); + bool adjustDecimals(char *src, uint32_t srcLength, char *target, diff --git a/src_features/signTx/logic_signTx.c b/src_features/signTx/logic_signTx.c index ece60ed..0b7377a 100644 --- a/src_features/signTx/logic_signTx.c +++ b/src_features/signTx/logic_signTx.c @@ -415,12 +415,11 @@ void finalizeParsing(bool direct) { // Prepare destination address to display if (genericUI) { if (tmpContent.txContent.destinationLength != 0) { - displayBuffer[0] = '0'; - displayBuffer[1] = 'x'; - getEthAddressStringFromBinary(tmpContent.txContent.destination, - displayBuffer + 2, - &global_sha3, - chainConfig); + getEthDisplayableAddress(tmpContent.txContent.destination, + displayBuffer, + sizeof(displayBuffer), + &global_sha3, + chainConfig); compareOrCopy(strings.common.fullAddress, sizeof(strings.common.fullAddress), displayBuffer, diff --git a/src_features/stark_sign/ui_flow_stark_sign.c b/src_features/stark_sign/ui_flow_stark_sign.c index 6f1b247..854b334 100644 --- a/src_features/stark_sign/ui_flow_stark_sign.c +++ b/src_features/stark_sign/ui_flow_stark_sign.c @@ -13,16 +13,6 @@ void stark_sign_display_master_account() { dataContext.starkContext.transferDestination); } -void stark_sign_display_condition_address() { - strings.tmp.tmp[0] = '0'; - strings.tmp.tmp[1] = 'x'; - getEthAddressStringFromBinary(dataContext.starkContext.conditionAddress, - strings.tmp.tmp + 2, - &global_sha3, - chainConfig); - strings.tmp.tmp[42] = '\0'; -} - void stark_sign_display_condition_fact() { snprintf(strings.tmp.tmp, sizeof(strings.tmp.tmp), "0x%.*H", 32, dataContext.starkContext.fact); } @@ -189,7 +179,11 @@ UX_STEP_NOCB_INIT( UX_STEP_NOCB_INIT( ux_stark_conditional_transfer_8_step, bnnn_paging, - stark_sign_display_condition_address(), + getEthDisplayableAddress(dataContext.starkContext.conditionAddress, + strings.tmp.tmp, + sizeof(strings.tmp.tmp), + &global_sha3, + chainConfig), { .title = "Cond. Address", .text = strings.tmp.tmp diff --git a/src_plugins/erc20/erc20_plugin.c b/src_plugins/erc20/erc20_plugin.c index 156ca07..b17a741 100644 --- a/src_plugins/erc20/erc20_plugin.c +++ b/src_plugins/erc20/erc20_plugin.c @@ -212,12 +212,11 @@ void erc20_plugin_call(int message, void *parameters) { strlcpy(msg->msg, context->contract_name, msg->msgLength); } else { strlcpy(msg->title, "Address", msg->titleLength); - msg->msg[0] = '0'; - msg->msg[1] = 'x'; - getEthAddressStringFromBinary(context->destinationAddress, - msg->msg + 2, - msg->pluginSharedRW->sha3, - chainConfig); + getEthDisplayableAddress(context->destinationAddress, + msg->msg, + msg->msgLength, + msg->pluginSharedRW->sha3, + chainConfig); } msg->result = ETH_PLUGIN_RESULT_OK; diff --git a/src_plugins/erc721/erc721_plugin.c b/src_plugins/erc721/erc721_plugin.c index 584ac5c..a7dddb4 100644 --- a/src_plugins/erc721/erc721_plugin.c +++ b/src_plugins/erc721/erc721_plugin.c @@ -5,8 +5,11 @@ #include "ethUtils.h" #include "utils.h" -void starkware_print_stark_key(uint8_t *starkKey, char *destination); -void starkware_print_eth_address(uint8_t *address, char *destination); +void getEthDisplayableAddress(uint8_t *in, + char *out, + size_t out_len, + cx_sha3_t *sha3, + chain_config_t *chain_config); typedef struct erc721_parameters_t { uint8_t selectorIndex; @@ -119,19 +122,27 @@ void erc721_plugin_call(int message, void *parameters) { switch (msg->screenIndex) { case 0: strlcpy(msg->title, "Contract Name", msg->titleLength); - starkware_print_eth_address(tmpContent.txContent.destination, msg->msg); + getEthDisplayableAddress(tmpContent.txContent.destination, + msg->msg, + msg->msgLength, + &global_sha3, + chainConfig); msg->result = ETH_PLUGIN_RESULT_OK; break; case 1: strlcpy(msg->title, "NFT Contract", msg->titleLength); - starkware_print_eth_address(context->address, msg->msg); + getEthDisplayableAddress(context->address, + msg->msg, + msg->msgLength, + &global_sha3, + chainConfig); msg->result = ETH_PLUGIN_RESULT_OK; break; case 2: strlcpy(msg->title, "TokenID", msg->titleLength); - starkware_print_stark_key(context->tokenId, msg->msg); + snprintf(msg->msg, 70, "0x%.*H", 32, context->tokenId); msg->result = ETH_PLUGIN_RESULT_OK; break; diff --git a/src_plugins/eth2/eth2_plugin.c b/src_plugins/eth2/eth2_plugin.c index 7a43eaf..4b4dd3a 100644 --- a/src_plugins/eth2/eth2_plugin.c +++ b/src_plugins/eth2/eth2_plugin.c @@ -32,19 +32,6 @@ typedef struct eth2_deposit_parameters_t { char deposit_address[ETH2_DEPOSIT_PUBKEY_LENGTH]; } eth2_deposit_parameters_t; -// Fills the `out` buffer with the lowercase string representation of the pubkey passed in as binary -// format by `in`. Does not check the size, so expects `out` to be big enough to hold the string -// representation. Returns the length of string (counting the null terminating character). -static int getEthDisplayableAddress(char *out, uint8_t *in, cx_sha3_t *sha3) { - out[0] = '0'; - out[1] = 'x'; - getEthAddressStringFromBinary(in, out + 2, sha3, chainConfig); - - uint8_t destinationLen = strlen(out) + 1; // Adding one to account for \0. - - return destinationLen; -} - void eth2_plugin_call(int message, void *parameters) { switch (message) { case ETH_PLUGIN_INIT_CONTRACT: { @@ -130,9 +117,11 @@ void eth2_plugin_call(int message, void *parameters) { // Use a temporary buffer to store the string representation. char tmp[ETH2_DEPOSIT_PUBKEY_LENGTH]; - getEthDisplayableAddress(tmp, - (uint8_t *) context->deposit_address, - msg->pluginSharedRW->sha3); + getEthDisplayableAddress((uint8_t *) context->deposit_address, + tmp, + sizeof(tmp), + msg->pluginSharedRW->sha3, + chainConfig); // Copy back the string to the global variable. strlcpy(context->deposit_address, tmp, ETH2_DEPOSIT_PUBKEY_LENGTH); diff --git a/src_plugins/starkware/starkware_plugin.c b/src_plugins/starkware/starkware_plugin.c index c2ae91e..0b2e980 100644 --- a/src_plugins/starkware/starkware_plugin.c +++ b/src_plugins/starkware/starkware_plugin.c @@ -279,18 +279,6 @@ void starkware_print_stark_key(uint8_t *starkKey, char *destination) { snprintf(destination, 70, "0x%.*H", 32, starkKey); } -// TODO : rewrite as independant code -void starkware_print_eth_address(uint8_t *address, char *destination, size_t destinationLength) { - if (destinationLength < 43) { - strlcpy(destination, "ERROR", destinationLength); - return; - } - destination[0] = '0'; - destination[1] = 'x'; - getEthAddressStringFromBinary(address, destination + 2, &global_sha3, chainConfig); - destination[42] = '\0'; -} - // TODO : rewrite as independant code void starkware_print_amount(uint8_t *amountData, char *destination, @@ -348,7 +336,11 @@ void starkware_print_asset_contract(char *destination, size_t destinationLength) if (dataContext.tokenContext.quantumIndex != MAX_TOKEN) { tokenDefinition_t *token = &tmpCtx.transactionContext.tokens[dataContext.tokenContext.quantumIndex]; - starkware_print_eth_address(token->address, destination, destinationLength); + getEthDisplayableAddress(token->address, + destination, + destinationLength, + &global_sha3, + chainConfig); } else { strlcpy(destination, "UNKNOWN", destinationLength); } @@ -708,9 +700,11 @@ void starkware_plugin_call(int message, void *parameters) { if (is_deversify_contract(tmpContent.txContent.destination)) { strlcpy(msg->msg, "DeversiFi", msg->msgLength); } else { - starkware_print_eth_address(tmpContent.txContent.destination, - msg->msg, - msg->msgLength); + getEthDisplayableAddress(tmpContent.txContent.destination, + msg->msg, + msg->msgLength, + &global_sha3, + chainConfig); } msg->result = ETH_PLUGIN_RESULT_OK; break; @@ -720,7 +714,11 @@ void starkware_plugin_call(int message, void *parameters) { case STARKWARE_REGISTER_AND_DEPOSIT_TOKEN: case STARKWARE_REGISTER_AND_DEPOSIT_ETH: strlcpy(msg->title, "From ETH Address", msg->titleLength); - starkware_print_eth_address(context->amount, msg->msg, msg->msgLength); + getEthDisplayableAddress(context->amount, + msg->msg, + msg->msgLength, + &global_sha3, + chainConfig); break; case STARKWARE_ESCAPE: strlcpy(msg->title, "Amount", msg->titleLength); @@ -784,7 +782,11 @@ void starkware_plugin_call(int message, void *parameters) { case STARKWARE_WITHDRAW_TO: case STARKWARE_WITHDRAW_NFT_TO: strlcpy(msg->title, "To ETH Address", msg->titleLength); - starkware_print_eth_address(context->amount, msg->msg, msg->msgLength); + getEthDisplayableAddress(context->amount, + msg->msg, + msg->msgLength, + &global_sha3, + chainConfig); break; case STARKWARE_WITHDRAW_AND_MINT: strlcpy(msg->title, "Asset Contract", msg->titleLength);