From 27f9ad52a8d18fc8dcb995a3f1d7576eee32d157 Mon Sep 17 00:00:00 2001 From: Francois Beutin Date: Wed, 17 Apr 2024 14:18:26 +0200 Subject: [PATCH 1/5] Remove token management from main --- src/main.c | 25 +++++------------------ src/manage_asset_info.c | 20 ++++++++++++++++++ src/manage_asset_info.h | 7 +++++++ src/ui_callbacks.h | 6 +----- src_features/signMessageEIP712/ui_logic.c | 14 ++++--------- src_features/signTx/logic_signTx.c | 5 +++-- 6 files changed, 40 insertions(+), 37 deletions(-) create mode 100644 src/manage_asset_info.c create mode 100644 src/manage_asset_info.h diff --git a/src/main.c b/src/main.c index 74e661078..c566b5d2d 100644 --- a/src/main.c +++ b/src/main.c @@ -32,6 +32,7 @@ #include "challenge.h" #include "domain_name.h" #include "crypto_helpers.h" +#include "manage_asset_info.h" unsigned char G_io_seproxyhal_spi_buffer[IO_SEPROXYHAL_BUFFER_SIZE_B]; @@ -114,22 +115,6 @@ unsigned short io_exchange_al(unsigned char channel, unsigned short tx_len) { return 0; } -extraInfo_t *getKnownToken(const uint8_t *contractAddress) { - union extraInfo_t *currentItem = NULL; - // Works for ERC-20 & NFT tokens since both structs in the union have the - // contract address aligned - for (uint8_t i = 0; i < MAX_ITEMS; i++) { - currentItem = (union extraInfo_t *) &tmpCtx.transactionContext.extraInfo[i].token; - if (tmpCtx.transactionContext.tokenSet[i] && - (memcmp(currentItem->token.address, contractAddress, ADDRESS_LENGTH) == 0)) { - PRINTF("Token found at index %d\n", i); - return currentItem; - } - } - - return NULL; -} - const uint8_t *parseBip32(const uint8_t *dataBuffer, uint8_t *dataLength, bip32_path_t *bip32) { if (*dataLength < 1) { PRINTF("Invalid data\n"); @@ -171,7 +156,7 @@ void handleApdu(unsigned int *flags, unsigned int *tx) { switch (G_io_apdu_buffer[OFFSET_INS]) { case INS_GET_PUBLIC_KEY: - memset(tmpCtx.transactionContext.tokenSet, 0, MAX_ITEMS); + reset_known_tokens(); handleGetPublicKey(G_io_apdu_buffer[OFFSET_P1], G_io_apdu_buffer[OFFSET_P2], G_io_apdu_buffer + OFFSET_CDATA, @@ -246,7 +231,7 @@ void handleApdu(unsigned int *flags, unsigned int *tx) { break; case INS_SIGN_PERSONAL_MESSAGE: - memset(tmpCtx.transactionContext.tokenSet, 0, MAX_ITEMS); + reset_known_tokens(); *flags |= IO_ASYNCH_REPLY; if (!handleSignPersonalMessage(G_io_apdu_buffer[OFFSET_P1], G_io_apdu_buffer[OFFSET_P2], @@ -259,7 +244,7 @@ void handleApdu(unsigned int *flags, unsigned int *tx) { case INS_SIGN_EIP_712_MESSAGE: switch (G_io_apdu_buffer[OFFSET_P2]) { case P2_EIP712_LEGACY_IMPLEM: - memset(tmpCtx.transactionContext.tokenSet, 0, MAX_ITEMS); + reset_known_tokens(); handleSignEIP712Message_v0(G_io_apdu_buffer[OFFSET_P1], G_io_apdu_buffer[OFFSET_P2], G_io_apdu_buffer + OFFSET_CDATA, @@ -281,7 +266,7 @@ void handleApdu(unsigned int *flags, unsigned int *tx) { #ifdef HAVE_ETH2 case INS_GET_ETH2_PUBLIC_KEY: - memset(tmpCtx.transactionContext.tokenSet, 0, MAX_ITEMS); + reset_known_tokens(); handleGetEth2PublicKey(G_io_apdu_buffer[OFFSET_P1], G_io_apdu_buffer[OFFSET_P2], G_io_apdu_buffer + OFFSET_CDATA, diff --git a/src/manage_asset_info.c b/src/manage_asset_info.c new file mode 100644 index 000000000..616953bbd --- /dev/null +++ b/src/manage_asset_info.c @@ -0,0 +1,20 @@ +#include "manage_asset_info.h" + +void reset_known_tokens(void) { + memset(tmpCtx.transactionContext.tokenSet, 0, MAX_ITEMS); +} + +extraInfo_t *get_asset_info(const uint8_t *contractAddress) { + // Works for ERC-20 & NFT tokens since both structs in the union have the + // contract address aligned + for (uint8_t i = 0; i < MAX_ITEMS; i++) { + extraInfo_t *currentItem = &tmpCtx.transactionContext.extraInfo[i]; + if (tmpCtx.transactionContext.tokenSet[i] && + (memcmp(currentItem->token.address, contractAddress, ADDRESS_LENGTH) == 0)) { + PRINTF("Token found at index %d\n", i); + return currentItem; + } + } + + return NULL; +} diff --git a/src/manage_asset_info.h b/src/manage_asset_info.h new file mode 100644 index 000000000..401598c06 --- /dev/null +++ b/src/manage_asset_info.h @@ -0,0 +1,7 @@ +#include "shared_context.h" +#include "common_utils.h" +#include "asset_info.h" + +void reset_known_tokens(void); + +extraInfo_t *get_asset_info(const uint8_t *contractAddress); diff --git a/src/ui_callbacks.h b/src/ui_callbacks.h index 0513c8694..90984f03c 100644 --- a/src/ui_callbacks.h +++ b/src/ui_callbacks.h @@ -1,5 +1,4 @@ -#ifndef _UI_CALLBACKS_H_ -#define _UI_CALLBACKS_H_ +#pragma once #include "shared_context.h" #include "ux.h" @@ -26,6 +25,3 @@ void ui_warning_contract_data(void); void io_seproxyhal_send_status(uint32_t sw); void finalizeParsing(bool direct); -extraInfo_t *getKnownToken(const uint8_t *contractAddress); - -#endif // _UI_CALLBACKS_H_ diff --git a/src_features/signMessageEIP712/ui_logic.c b/src_features/signMessageEIP712/ui_logic.c index 54598f162..943415846 100644 --- a/src_features/signMessageEIP712/ui_logic.c +++ b/src_features/signMessageEIP712/ui_logic.c @@ -15,6 +15,7 @@ #include "apdu_constants.h" // APDU response codes #include "typed_data.h" #include "commands_712.h" +#include "manage_asset_info.h" #include "common_ui.h" #include "domain_name.h" #include "uint_common.h" @@ -192,16 +193,9 @@ static void ui_712_format_str(const uint8_t *const data, uint8_t length) { * @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 information - 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; - } - } + extraInfo_t *extra_info = get_asset_info(addr); + if (extra_info != NULL) { + return extra_info->token.ticker; } return NULL; } diff --git a/src_features/signTx/logic_signTx.c b/src_features/signTx/logic_signTx.c index c16c857f1..8e348f809 100644 --- a/src_features/signTx/logic_signTx.c +++ b/src_features/signTx/logic_signTx.c @@ -10,6 +10,7 @@ #include "apdu_constants.h" #include "crypto_helpers.h" #include "format.h" +#include "manage_asset_info.h" #define ERR_SILENT_MODE_CHECK_FAILED 0x6001 @@ -366,14 +367,14 @@ __attribute__((noinline)) static bool finalize_parsing_helper(bool direct, bool if ((pluginFinalize.tokenLookup1 != NULL) || (pluginFinalize.tokenLookup2 != NULL)) { if (pluginFinalize.tokenLookup1 != NULL) { PRINTF("Lookup1: %.*H\n", ADDRESS_LENGTH, pluginFinalize.tokenLookup1); - pluginProvideInfo.item1 = getKnownToken(pluginFinalize.tokenLookup1); + pluginProvideInfo.item1 = get_asset_info(pluginFinalize.tokenLookup1); if (pluginProvideInfo.item1 != NULL) { PRINTF("Token1 ticker: %s\n", pluginProvideInfo.item1->token.ticker); } } if (pluginFinalize.tokenLookup2 != NULL) { PRINTF("Lookup2: %.*H\n", ADDRESS_LENGTH, pluginFinalize.tokenLookup2); - pluginProvideInfo.item2 = getKnownToken(pluginFinalize.tokenLookup2); + pluginProvideInfo.item2 = get_asset_info(pluginFinalize.tokenLookup2); if (pluginProvideInfo.item2 != NULL) { PRINTF("Token2 ticker: %s\n", pluginProvideInfo.item2->token.ticker); } From c8c4b6cccc12d68727b970b25987a574b2076070 Mon Sep 17 00:00:00 2001 From: Alexandre Paillier Date: Tue, 7 May 2024 14:27:00 +0200 Subject: [PATCH 2/5] Renamed get_asset_info to get_asset_info_by_addr --- src/manage_asset_info.c | 2 +- src/manage_asset_info.h | 3 +-- src_features/signMessageEIP712/ui_logic.c | 2 +- src_features/signTx/logic_signTx.c | 4 ++-- 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/manage_asset_info.c b/src/manage_asset_info.c index 616953bbd..fd25099c9 100644 --- a/src/manage_asset_info.c +++ b/src/manage_asset_info.c @@ -4,7 +4,7 @@ void reset_known_tokens(void) { memset(tmpCtx.transactionContext.tokenSet, 0, MAX_ITEMS); } -extraInfo_t *get_asset_info(const uint8_t *contractAddress) { +extraInfo_t *get_asset_info_by_addr(const uint8_t *contractAddress) { // Works for ERC-20 & NFT tokens since both structs in the union have the // contract address aligned for (uint8_t i = 0; i < MAX_ITEMS; i++) { diff --git a/src/manage_asset_info.h b/src/manage_asset_info.h index 401598c06..9926801ec 100644 --- a/src/manage_asset_info.h +++ b/src/manage_asset_info.h @@ -3,5 +3,4 @@ #include "asset_info.h" void reset_known_tokens(void); - -extraInfo_t *get_asset_info(const uint8_t *contractAddress); +extraInfo_t *get_asset_info_by_addr(const uint8_t *contractAddress); diff --git a/src_features/signMessageEIP712/ui_logic.c b/src_features/signMessageEIP712/ui_logic.c index 943415846..1629c7f95 100644 --- a/src_features/signMessageEIP712/ui_logic.c +++ b/src_features/signMessageEIP712/ui_logic.c @@ -193,7 +193,7 @@ static void ui_712_format_str(const uint8_t *const data, uint8_t length) { * @return the ticker name if found, \ref NULL otherwise */ static const char *get_address_token_ticker(const uint8_t *addr) { - extraInfo_t *extra_info = get_asset_info(addr); + extraInfo_t *extra_info = get_asset_info_by_addr(addr); if (extra_info != NULL) { return extra_info->token.ticker; } diff --git a/src_features/signTx/logic_signTx.c b/src_features/signTx/logic_signTx.c index 8e348f809..1101bbdd8 100644 --- a/src_features/signTx/logic_signTx.c +++ b/src_features/signTx/logic_signTx.c @@ -367,14 +367,14 @@ __attribute__((noinline)) static bool finalize_parsing_helper(bool direct, bool if ((pluginFinalize.tokenLookup1 != NULL) || (pluginFinalize.tokenLookup2 != NULL)) { if (pluginFinalize.tokenLookup1 != NULL) { PRINTF("Lookup1: %.*H\n", ADDRESS_LENGTH, pluginFinalize.tokenLookup1); - pluginProvideInfo.item1 = get_asset_info(pluginFinalize.tokenLookup1); + pluginProvideInfo.item1 = get_asset_info_by_addr(pluginFinalize.tokenLookup1); if (pluginProvideInfo.item1 != NULL) { PRINTF("Token1 ticker: %s\n", pluginProvideInfo.item1->token.ticker); } } if (pluginFinalize.tokenLookup2 != NULL) { PRINTF("Lookup2: %.*H\n", ADDRESS_LENGTH, pluginFinalize.tokenLookup2); - pluginProvideInfo.item2 = get_asset_info(pluginFinalize.tokenLookup2); + pluginProvideInfo.item2 = get_asset_info_by_addr(pluginFinalize.tokenLookup2); if (pluginProvideInfo.item2 != NULL) { PRINTF("Token2 ticker: %s\n", pluginProvideInfo.item2->token.ticker); } From 18a02dcacbcd9580db4c035cd1fbda352296b872 Mon Sep 17 00:00:00 2001 From: Alexandre Paillier Date: Tue, 7 May 2024 17:17:26 +0200 Subject: [PATCH 3/5] Added some asset info util functions --- src/manage_asset_info.c | 27 ++++++++++++++++++++++++++- src/manage_asset_info.h | 2 ++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/manage_asset_info.c b/src/manage_asset_info.c index fd25099c9..bfaa58938 100644 --- a/src/manage_asset_info.c +++ b/src/manage_asset_info.c @@ -4,12 +4,26 @@ void reset_known_tokens(void) { memset(tmpCtx.transactionContext.tokenSet, 0, MAX_ITEMS); } +static extraInfo_t *get_asset_info(uint8_t index) { + if (index >= MAX_ITEMS) { + return NULL; + } + return &tmpCtx.transactionContext.extraInfo[index]; +} + +static bool asset_info_is_set(uint8_t index) { + if (index >= MAX_ITEMS) { + return false; + } + return tmpCtx.transactionContext.tokenSet[index] != 0; +} + extraInfo_t *get_asset_info_by_addr(const uint8_t *contractAddress) { // Works for ERC-20 & NFT tokens since both structs in the union have the // contract address aligned for (uint8_t i = 0; i < MAX_ITEMS; i++) { extraInfo_t *currentItem = &tmpCtx.transactionContext.extraInfo[i]; - if (tmpCtx.transactionContext.tokenSet[i] && + if (asset_info_is_set(i) && (memcmp(currentItem->token.address, contractAddress, ADDRESS_LENGTH) == 0)) { PRINTF("Token found at index %d\n", i); return currentItem; @@ -18,3 +32,14 @@ extraInfo_t *get_asset_info_by_addr(const uint8_t *contractAddress) { return NULL; } + +extraInfo_t *get_current_asset_info(void) { + return get_asset_info(tmpCtx.transactionContext.currentItemIndex); +} + +void validate_current_asset_info(void) { + // mark it as set + tmpCtx.transactionContext.tokenSet[tmpCtx.transactionContext.currentItemIndex] = 1; + // increment index + tmpCtx.transactionContext.currentItemIndex = (tmpCtx.transactionContext.currentItemIndex + 1) % MAX_ITEMS; +} diff --git a/src/manage_asset_info.h b/src/manage_asset_info.h index 9926801ec..fa478b469 100644 --- a/src/manage_asset_info.h +++ b/src/manage_asset_info.h @@ -4,3 +4,5 @@ void reset_known_tokens(void); extraInfo_t *get_asset_info_by_addr(const uint8_t *contractAddress); +extraInfo_t *get_current_asset_info(void); +void validate_current_asset_info(void); From c2e0f7ca6a6682e379577cb50fac2e926177d1e0 Mon Sep 17 00:00:00 2001 From: Alexandre Paillier Date: Tue, 7 May 2024 17:20:20 +0200 Subject: [PATCH 4/5] Now uses the new asset util functions Now the first asset is properly stored at index 0 instead of 1 --- src/eth_plugin_handler.c | 8 ++++---- src/eth_plugin_handler.h | 2 +- src/main.c | 1 - src/manage_asset_info.c | 1 + .../cmd_provideTokenInfo.c | 15 +++++---------- .../provideNFTInformation/cmd_provideNFTInfo.c | 8 +++----- 6 files changed, 14 insertions(+), 21 deletions(-) diff --git a/src/eth_plugin_handler.c b/src/eth_plugin_handler.c index ddeeabcf3..43431506c 100644 --- a/src/eth_plugin_handler.c +++ b/src/eth_plugin_handler.c @@ -52,17 +52,17 @@ void eth_plugin_prepare_query_contract_UI(ethQueryContractUI_t *queryContractUI, memset((uint8_t *) queryContractUI, 0, sizeof(ethQueryContractUI_t)); // If no extra information was found, set the pointer to NULL - if (NO_EXTRA_INFO(tmpCtx, 1)) { + if (NO_EXTRA_INFO(tmpCtx, 0)) { queryContractUI->item1 = NULL; } else { - queryContractUI->item1 = &tmpCtx.transactionContext.extraInfo[1]; + queryContractUI->item1 = &tmpCtx.transactionContext.extraInfo[0]; } // If no extra information was found, set the pointer to NULL - if (NO_EXTRA_INFO(tmpCtx, 0)) { + if (NO_EXTRA_INFO(tmpCtx, 1)) { queryContractUI->item2 = NULL; } else { - queryContractUI->item2 = &tmpCtx.transactionContext.extraInfo[0]; + queryContractUI->item2 = &tmpCtx.transactionContext.extraInfo[1]; } queryContractUI->screenIndex = screenIndex; diff --git a/src/eth_plugin_handler.h b/src/eth_plugin_handler.h index dab8ea46d..1acaea018 100644 --- a/src/eth_plugin_handler.h +++ b/src/eth_plugin_handler.h @@ -6,7 +6,7 @@ #define NO_EXTRA_INFO(ctx, idx) \ (allzeroes(&(ctx.transactionContext.extraInfo[idx]), sizeof(extraInfo_t))) -#define NO_NFT_METADATA (NO_EXTRA_INFO(tmpCtx, 1)) +#define NO_NFT_METADATA (NO_EXTRA_INFO(tmpCtx, 0)) void eth_plugin_prepare_init(ethPluginInitContract_t *init, const uint8_t *selector, diff --git a/src/main.c b/src/main.c index c566b5d2d..f76cae87f 100644 --- a/src/main.c +++ b/src/main.c @@ -533,7 +533,6 @@ __attribute__((noreturn)) void coin_main(libargs_t *args) { } reset_app_context(); - tmpCtx.transactionContext.currentItemIndex = 0; for (;;) { UX_INIT(); diff --git a/src/manage_asset_info.c b/src/manage_asset_info.c index bfaa58938..6c915e236 100644 --- a/src/manage_asset_info.c +++ b/src/manage_asset_info.c @@ -2,6 +2,7 @@ void reset_known_tokens(void) { memset(tmpCtx.transactionContext.tokenSet, 0, MAX_ITEMS); + tmpCtx.transactionContext.currentItemIndex = 0; } static extraInfo_t *get_asset_info(uint8_t index) { diff --git a/src_features/provideErc20TokenInformation/cmd_provideTokenInfo.c b/src_features/provideErc20TokenInformation/cmd_provideTokenInfo.c index 94bf4e241..f9b3bf5be 100644 --- a/src_features/provideErc20TokenInformation/cmd_provideTokenInfo.c +++ b/src_features/provideErc20TokenInformation/cmd_provideTokenInfo.c @@ -5,6 +5,7 @@ #include "os_io_seproxyhal.h" #include "extra_tokens.h" #include "network.h" +#include "manage_asset_info.h" #ifdef HAVE_CONTRACT_NAME_IN_DESCRIPTOR @@ -26,10 +27,7 @@ void handleProvideErc20TokenInformation(uint8_t p1, cx_sha256_init(&sha256); - tmpCtx.transactionContext.currentItemIndex = - (tmpCtx.transactionContext.currentItemIndex + 1) % MAX_ITEMS; - tokenDefinition_t *token = - &tmpCtx.transactionContext.tokens[tmpCtx.transactionContext.currentItemIndex]; + tokenDefinition_t *token = &get_current_asset_info()->token; if (dataLength < 1) { THROW(0x6A80); @@ -95,7 +93,7 @@ void handleProvideErc20TokenInformation(uint8_t p1, THROW(0x6A80); #endif } - tmpCtx.transactionContext.tokenSet[tmpCtx.transactionContext.currentItemIndex] = 1; + validate_current_asset_info(); THROW(0x9000); } @@ -117,10 +115,7 @@ void handleProvideErc20TokenInformation(uint8_t p1, uint8_t hash[INT256_LENGTH]; cx_ecfp_public_key_t tokenKey; - tmpCtx.transactionContext.currentItemIndex = - (tmpCtx.transactionContext.currentItemIndex + 1) % MAX_ITEMS; - tokenDefinition_t *token = - &tmpCtx.transactionContext.extraInfo[tmpCtx.transactionContext.currentItemIndex].token; + tokenDefinition_t *token = &get_current_asset_info()->token; PRINTF("Provisioning currentItemIndex %d\n", tmpCtx.transactionContext.currentItemIndex); @@ -183,7 +178,7 @@ void handleProvideErc20TokenInformation(uint8_t p1, } } - tmpCtx.transactionContext.tokenSet[tmpCtx.transactionContext.currentItemIndex] = 1; + validate_current_asset_info(); THROW(0x9000); } diff --git a/src_features/provideNFTInformation/cmd_provideNFTInfo.c b/src_features/provideNFTInformation/cmd_provideNFTInfo.c index 61889d63c..147887c5f 100644 --- a/src_features/provideNFTInformation/cmd_provideNFTInfo.c +++ b/src_features/provideNFTInformation/cmd_provideNFTInfo.c @@ -8,6 +8,7 @@ #include "os_io_seproxyhal.h" #include "network.h" #include "public_keys.h" +#include "manage_asset_info.h" #define TYPE_SIZE 1 #define VERSION_SIZE 1 @@ -56,10 +57,7 @@ void handleProvideNFTInformation(uint8_t p1, PRINTF("NFT metadata provided without proper plugin loaded!\n"); THROW(0x6985); } - tmpCtx.transactionContext.currentItemIndex = - (tmpCtx.transactionContext.currentItemIndex + 1) % MAX_ITEMS; - nftInfo_t *nft = - &tmpCtx.transactionContext.extraInfo[tmpCtx.transactionContext.currentItemIndex].nft; + nftInfo_t *nft = &get_current_asset_info()->nft; PRINTF("Provisioning currentItemIndex %d\n", tmpCtx.transactionContext.currentItemIndex); @@ -201,7 +199,7 @@ void handleProvideNFTInformation(uint8_t p1, #endif } - tmpCtx.transactionContext.tokenSet[tmpCtx.transactionContext.currentItemIndex] = 1; + validate_current_asset_info(); THROW(0x9000); } From c2011f5d423edd32c3d245edf20eeb1492aec84f Mon Sep 17 00:00:00 2001 From: Alexandre Paillier Date: Tue, 7 May 2024 17:37:27 +0200 Subject: [PATCH 5/5] Renamed Item to Asset & changed the set marker type to boolean Also renamed the reset assets function --- src/main.c | 9 +++---- src/manage_asset_info.c | 24 ++++++++++--------- src/manage_asset_info.h | 2 +- src/shared_context.h | 8 ++++--- .../cmd_provideTokenInfo.c | 5 ++-- .../cmd_provideNFTInfo.c | 2 +- 6 files changed, 28 insertions(+), 22 deletions(-) diff --git a/src/main.c b/src/main.c index f76cae87f..a1507b572 100644 --- a/src/main.c +++ b/src/main.c @@ -80,6 +80,7 @@ void reset_app_context() { eth2WithdrawalIndex = 0; #endif memset((uint8_t *) &tmpCtx, 0, sizeof(tmpCtx)); + forget_known_assets(); memset((uint8_t *) &txContext, 0, sizeof(txContext)); memset((uint8_t *) &tmpContent, 0, sizeof(tmpContent)); } @@ -156,7 +157,7 @@ void handleApdu(unsigned int *flags, unsigned int *tx) { switch (G_io_apdu_buffer[OFFSET_INS]) { case INS_GET_PUBLIC_KEY: - reset_known_tokens(); + forget_known_assets(); handleGetPublicKey(G_io_apdu_buffer[OFFSET_P1], G_io_apdu_buffer[OFFSET_P2], G_io_apdu_buffer + OFFSET_CDATA, @@ -231,7 +232,7 @@ void handleApdu(unsigned int *flags, unsigned int *tx) { break; case INS_SIGN_PERSONAL_MESSAGE: - reset_known_tokens(); + forget_known_assets(); *flags |= IO_ASYNCH_REPLY; if (!handleSignPersonalMessage(G_io_apdu_buffer[OFFSET_P1], G_io_apdu_buffer[OFFSET_P2], @@ -244,7 +245,7 @@ void handleApdu(unsigned int *flags, unsigned int *tx) { case INS_SIGN_EIP_712_MESSAGE: switch (G_io_apdu_buffer[OFFSET_P2]) { case P2_EIP712_LEGACY_IMPLEM: - reset_known_tokens(); + forget_known_assets(); handleSignEIP712Message_v0(G_io_apdu_buffer[OFFSET_P1], G_io_apdu_buffer[OFFSET_P2], G_io_apdu_buffer + OFFSET_CDATA, @@ -266,7 +267,7 @@ void handleApdu(unsigned int *flags, unsigned int *tx) { #ifdef HAVE_ETH2 case INS_GET_ETH2_PUBLIC_KEY: - reset_known_tokens(); + forget_known_assets(); handleGetEth2PublicKey(G_io_apdu_buffer[OFFSET_P1], G_io_apdu_buffer[OFFSET_P2], G_io_apdu_buffer + OFFSET_CDATA, diff --git a/src/manage_asset_info.c b/src/manage_asset_info.c index 6c915e236..d24b2a4c2 100644 --- a/src/manage_asset_info.c +++ b/src/manage_asset_info.c @@ -1,29 +1,30 @@ #include "manage_asset_info.h" +#include "shared_context.h" -void reset_known_tokens(void) { - memset(tmpCtx.transactionContext.tokenSet, 0, MAX_ITEMS); - tmpCtx.transactionContext.currentItemIndex = 0; +void forget_known_assets(void) { + memset(tmpCtx.transactionContext.assetSet, false, MAX_ASSETS); + tmpCtx.transactionContext.currentAssetIndex = 0; } static extraInfo_t *get_asset_info(uint8_t index) { - if (index >= MAX_ITEMS) { + if (index >= MAX_ASSETS) { return NULL; } return &tmpCtx.transactionContext.extraInfo[index]; } static bool asset_info_is_set(uint8_t index) { - if (index >= MAX_ITEMS) { + if (index >= MAX_ASSETS) { return false; } - return tmpCtx.transactionContext.tokenSet[index] != 0; + return tmpCtx.transactionContext.assetSet[index]; } extraInfo_t *get_asset_info_by_addr(const uint8_t *contractAddress) { // Works for ERC-20 & NFT tokens since both structs in the union have the // contract address aligned - for (uint8_t i = 0; i < MAX_ITEMS; i++) { - extraInfo_t *currentItem = &tmpCtx.transactionContext.extraInfo[i]; + for (uint8_t i = 0; i < MAX_ASSETS; i++) { + extraInfo_t *currentItem = get_asset_info(i); if (asset_info_is_set(i) && (memcmp(currentItem->token.address, contractAddress, ADDRESS_LENGTH) == 0)) { PRINTF("Token found at index %d\n", i); @@ -35,12 +36,13 @@ extraInfo_t *get_asset_info_by_addr(const uint8_t *contractAddress) { } extraInfo_t *get_current_asset_info(void) { - return get_asset_info(tmpCtx.transactionContext.currentItemIndex); + return get_asset_info(tmpCtx.transactionContext.currentAssetIndex); } void validate_current_asset_info(void) { // mark it as set - tmpCtx.transactionContext.tokenSet[tmpCtx.transactionContext.currentItemIndex] = 1; + tmpCtx.transactionContext.assetSet[tmpCtx.transactionContext.currentAssetIndex] = true; // increment index - tmpCtx.transactionContext.currentItemIndex = (tmpCtx.transactionContext.currentItemIndex + 1) % MAX_ITEMS; + tmpCtx.transactionContext.currentAssetIndex = + (tmpCtx.transactionContext.currentAssetIndex + 1) % MAX_ASSETS; } diff --git a/src/manage_asset_info.h b/src/manage_asset_info.h index fa478b469..9c9da5cd1 100644 --- a/src/manage_asset_info.h +++ b/src/manage_asset_info.h @@ -2,7 +2,7 @@ #include "common_utils.h" #include "asset_info.h" -void reset_known_tokens(void); +void forget_known_assets(void); extraInfo_t *get_asset_info_by_addr(const uint8_t *contractAddress); extraInfo_t *get_current_asset_info(void); void validate_current_asset_info(void); diff --git a/src/shared_context.h b/src/shared_context.h index 51097081c..08dc70c82 100644 --- a/src/shared_context.h +++ b/src/shared_context.h @@ -22,6 +22,8 @@ #define N_storage (*(volatile internalStorage_t *) PIC(&N_storage_real)) +#define MAX_ASSETS MAX_ITEMS // TODO: Temporary, remove once plugin SDK is updated + typedef struct bip32_path_t { uint8_t length; uint32_t path[MAX_BIP32_PATH]; @@ -77,9 +79,9 @@ typedef struct publicKeyContext_t { typedef struct transactionContext_t { bip32_path_t bip32; uint8_t hash[INT256_LENGTH]; - union extraInfo_t extraInfo[MAX_ITEMS]; - uint8_t tokenSet[MAX_ITEMS]; - uint8_t currentItemIndex; + union extraInfo_t extraInfo[MAX_ASSETS]; + bool assetSet[MAX_ASSETS]; + uint8_t currentAssetIndex; } transactionContext_t; typedef struct messageSigningContext_t { diff --git a/src_features/provideErc20TokenInformation/cmd_provideTokenInfo.c b/src_features/provideErc20TokenInformation/cmd_provideTokenInfo.c index f9b3bf5be..06e266020 100644 --- a/src_features/provideErc20TokenInformation/cmd_provideTokenInfo.c +++ b/src_features/provideErc20TokenInformation/cmd_provideTokenInfo.c @@ -117,7 +117,7 @@ void handleProvideErc20TokenInformation(uint8_t p1, tokenDefinition_t *token = &get_current_asset_info()->token; - PRINTF("Provisioning currentItemIndex %d\n", tmpCtx.transactionContext.currentItemIndex); + PRINTF("Provisioning currentAssetIndex %d\n", tmpCtx.transactionContext.currentAssetIndex); if (dataLength < 1) { THROW(0x6A80); @@ -138,10 +138,11 @@ void handleProvideErc20TokenInformation(uint8_t p1, memmove(token->address, workBuffer + offset, 20); offset += 20; dataLength -= 20; - // TODO: Handle 64-bit long chain IDs + // TODO: 4 bytes for this is overkill token->decimals = U4BE(workBuffer, offset); offset += 4; dataLength -= 4; + // TODO: Handle 64-bit long chain IDs chain_id = U4BE(workBuffer, offset); if (!app_compatible_with_chain_id(&chain_id)) { UNSUPPORTED_CHAIN_ID_MSG(chain_id); diff --git a/src_features/provideNFTInformation/cmd_provideNFTInfo.c b/src_features/provideNFTInformation/cmd_provideNFTInfo.c index 147887c5f..106ce7700 100644 --- a/src_features/provideNFTInformation/cmd_provideNFTInfo.c +++ b/src_features/provideNFTInformation/cmd_provideNFTInfo.c @@ -59,7 +59,7 @@ void handleProvideNFTInformation(uint8_t p1, } nftInfo_t *nft = &get_current_asset_info()->nft; - PRINTF("Provisioning currentItemIndex %d\n", tmpCtx.transactionContext.currentItemIndex); + PRINTF("Provisioning currentAssetIndex %d\n", tmpCtx.transactionContext.currentAssetIndex); size_t offset = 0;