From 77f5c9389ba9797ee1902656473b267fcd98487a Mon Sep 17 00:00:00 2001 From: Alexandre Paillier Date: Mon, 29 Jan 2024 17:28:08 +0100 Subject: [PATCH] EIP-712 addresses can now be displayed as a token ticker or a trusted domain name --- src_features/signMessageEIP712/ui_logic.c | 60 ++++++++++++++++++++--- 1 file changed, 54 insertions(+), 6 deletions(-) diff --git a/src_features/signMessageEIP712/ui_logic.c b/src_features/signMessageEIP712/ui_logic.c index 96d8bdd..5a169e9 100644 --- a/src_features/signMessageEIP712/ui_logic.c +++ b/src_features/signMessageEIP712/ui_logic.c @@ -16,6 +16,7 @@ #include "typed_data.h" #include "commands_712.h" #include "common_ui.h" +#include "domain_name.h" static t_ui_context *ui_ctx = NULL; @@ -184,6 +185,46 @@ static void ui_712_format_str(const uint8_t *const data, uint8_t length) { } } +/** + * Find a substitute token ticker for a given address + * + * @param[in] addr the given address + * @return the ticker name if found, \ref NULL otherwise + */ +static const char *get_address_token_ticker(const uint8_t *addr) { + tokenDefinition_t *token; + + // Loop over the received token informations + for (uint8_t token_idx = 0; token_idx < MAX_ITEMS; ++token_idx) { + if (tmpCtx.transactionContext.tokenSet[token_idx] == 1) { + token = &tmpCtx.transactionContext.extraInfo[token_idx].token; + if (memcmp(token->address, addr, ADDRESS_LENGTH) == 0) { + return token->ticker; + } + } + } + return NULL; +} + +/** + * Find a substitute (token ticker or domain name) for a given address + * + * @param[in] addr the given address + * @return the substitute if found, \ref NULL otherwise + */ +static const char *get_address_substitute(const uint8_t *addr) { + const char *str = NULL; + + str = get_address_token_ticker(addr); + if (str == NULL) { + if (has_domain_name(&eip712_context->chain_id, addr)) { + // No handling of the verbose domains setting + str = g_domain_name; + } + } + return str; +} + /** * Format a given data as a string representation of an address * @@ -196,13 +237,20 @@ static bool ui_712_format_addr(const uint8_t *const data, uint8_t length) { apdu_response_code = APDU_RESPONSE_INVALID_DATA; return false; } + if (ui_712_field_shown()) { - if (!getEthDisplayableAddress((uint8_t *) data, - strings.tmp.tmp, - sizeof(strings.tmp.tmp), - &global_sha3, - chainConfig->chainId)) { - THROW(APDU_RESPONSE_ERROR_NO_INFO); + const char *sub; + + if (!N_storage.verbose_eip712 && ((sub = get_address_substitute(data)) != NULL)) { + ui_712_set_value(sub, strlen(sub)); + } else { + if (!getEthDisplayableAddress((uint8_t *) data, + strings.tmp.tmp, + sizeof(strings.tmp.tmp), + &global_sha3, + chainConfig->chainId)) { + THROW(APDU_RESPONSE_ERROR_NO_INFO); + } } } return true;