Refactoring helper to get printable eth address from hex

This commit is contained in:
TamtamHero
2021-08-25 13:43:27 +02:00
parent dda1e20fb3
commit 3e25f04d05
8 changed files with 80 additions and 62 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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