Skip to content

Commit

Permalink
Remove token management from main
Browse files Browse the repository at this point in the history
  • Loading branch information
fbeutin-ledger committed Apr 17, 2024
1 parent d467248 commit 62d00ab
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 38 deletions.
25 changes: 5 additions & 20 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "commands_712.h"
#include "challenge.h"
#include "domain_name.h"
#include "manage_asset_info.h"
#include "lib_standard_app/crypto_helpers.h"

unsigned char G_io_seproxyhal_spi_buffer[IO_SEPROXYHAL_BUFFER_SIZE_B];
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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],
Expand All @@ -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,
Expand All @@ -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,
Expand Down
20 changes: 20 additions & 0 deletions src/manage_asset_info.c
Original file line number Diff line number Diff line change
@@ -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;
}
7 changes: 7 additions & 0 deletions src/manage_asset_info.h
Original file line number Diff line number Diff line change
@@ -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);
6 changes: 1 addition & 5 deletions src/ui_callbacks.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef _UI_CALLBACKS_H_
#define _UI_CALLBACKS_H_
#pragma once

#include "shared_context.h"
#include "ux.h"
Expand All @@ -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_
17 changes: 6 additions & 11 deletions src_features/signMessageEIP712/ui_logic.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -192,18 +193,12 @@ 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 NULL;
} else {
return extra_info->token.ticker;
}
return NULL;
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src_features/signTx/logic_signTx.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "common_ui.h"
#include "ui_callbacks.h"
#include "apdu_constants.h"
#include "manage_asset_info.h"
#include "lib_standard_app/crypto_helpers.h"

#define ERR_SILENT_MODE_CHECK_FAILED 0x6001
Expand Down Expand Up @@ -361,14 +362,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);
}
Expand Down

0 comments on commit 62d00ab

Please sign in to comment.