From 052b06df54b2b3149d00dfb27afa707f857e1119 Mon Sep 17 00:00:00 2001 From: Francois Beutin Date: Wed, 17 Apr 2024 14:16:38 +0200 Subject: [PATCH] Remove pluginSharedRW and pluginSharedRO from plugin API --- src/eth_plugin_handler.c | 23 ++----- src/main.c | 2 +- src_common/eth_plugin_interface.h | 91 +++++++++++++++------------- src_plugins/erc1155/erc1155_plugin.c | 5 +- src_plugins/erc20/erc20_plugin.c | 6 +- src_plugins/erc721/erc721_plugin.c | 5 +- src_plugins/eth2/eth2_plugin.c | 2 +- 7 files changed, 63 insertions(+), 71 deletions(-) diff --git a/src/eth_plugin_handler.c b/src/eth_plugin_handler.c index 3e64e10d6d..962cd3fa95 100644 --- a/src/eth_plugin_handler.c +++ b/src/eth_plugin_handler.c @@ -179,14 +179,9 @@ eth_plugin_result_t eth_plugin_perform_init(uint8_t *contractAddress, } eth_plugin_result_t eth_plugin_call(int method, void *parameter) { - ethPluginSharedRW_t pluginRW; - ethPluginSharedRO_t pluginRO; char *alias; uint8_t i; - pluginRW.sha3 = &global_sha3; - pluginRO.txContent = &tmpContent.txContent; - if (dataContext.tokenContext.pluginStatus <= ETH_PLUGIN_RESULT_UNSUCCESSFUL) { PRINTF("Cached plugin call but no plugin available\n"); return dataContext.tokenContext.pluginStatus; @@ -201,8 +196,7 @@ eth_plugin_result_t eth_plugin_call(int method, void *parameter) { ((ethPluginInitContract_t *) parameter)->interfaceVersion = ETH_PLUGIN_INTERFACE_VERSION_LATEST; ((ethPluginInitContract_t *) parameter)->result = ETH_PLUGIN_RESULT_UNAVAILABLE; - ((ethPluginInitContract_t *) parameter)->pluginSharedRW = &pluginRW; - ((ethPluginInitContract_t *) parameter)->pluginSharedRO = &pluginRO; + ((ethPluginInitContract_t *) parameter)->txContent = &tmpContent.txContent; ((ethPluginInitContract_t *) parameter)->pluginContext = (uint8_t *) &dataContext.tokenContext.pluginContext; ((ethPluginInitContract_t *) parameter)->pluginContextLength = @@ -211,39 +205,34 @@ eth_plugin_result_t eth_plugin_call(int method, void *parameter) { case ETH_PLUGIN_PROVIDE_PARAMETER: PRINTF("-- PLUGIN PROVIDE PARAMETER --\n"); ((ethPluginProvideParameter_t *) parameter)->result = ETH_PLUGIN_RESULT_UNAVAILABLE; - ((ethPluginProvideParameter_t *) parameter)->pluginSharedRW = &pluginRW; - ((ethPluginProvideParameter_t *) parameter)->pluginSharedRO = &pluginRO; + ((ethPluginProvideParameter_t *) parameter)->txContent = &tmpContent.txContent; ((ethPluginProvideParameter_t *) parameter)->pluginContext = (uint8_t *) &dataContext.tokenContext.pluginContext; break; case ETH_PLUGIN_FINALIZE: PRINTF("-- PLUGIN FINALIZE --\n"); ((ethPluginFinalize_t *) parameter)->result = ETH_PLUGIN_RESULT_UNAVAILABLE; - ((ethPluginFinalize_t *) parameter)->pluginSharedRW = &pluginRW; - ((ethPluginFinalize_t *) parameter)->pluginSharedRO = &pluginRO; + ((ethPluginFinalize_t *) parameter)->txContent = &tmpContent.txContent; ((ethPluginFinalize_t *) parameter)->pluginContext = (uint8_t *) &dataContext.tokenContext.pluginContext; break; case ETH_PLUGIN_PROVIDE_INFO: PRINTF("-- PLUGIN PROVIDE INFO --\n"); ((ethPluginProvideInfo_t *) parameter)->result = ETH_PLUGIN_RESULT_UNAVAILABLE; - ((ethPluginProvideInfo_t *) parameter)->pluginSharedRW = &pluginRW; - ((ethPluginProvideInfo_t *) parameter)->pluginSharedRO = &pluginRO; + ((ethPluginProvideInfo_t *) parameter)->txContent = &tmpContent.txContent; ((ethPluginProvideInfo_t *) parameter)->pluginContext = (uint8_t *) &dataContext.tokenContext.pluginContext; break; case ETH_PLUGIN_QUERY_CONTRACT_ID: PRINTF("-- PLUGIN QUERY CONTRACT ID --\n"); ((ethQueryContractID_t *) parameter)->result = ETH_PLUGIN_RESULT_UNAVAILABLE; - ((ethQueryContractID_t *) parameter)->pluginSharedRW = &pluginRW; - ((ethQueryContractID_t *) parameter)->pluginSharedRO = &pluginRO; + ((ethQueryContractID_t *) parameter)->txContent = &tmpContent.txContent; ((ethQueryContractID_t *) parameter)->pluginContext = (uint8_t *) &dataContext.tokenContext.pluginContext; break; case ETH_PLUGIN_QUERY_CONTRACT_UI: PRINTF("-- PLUGIN QUERY CONTRACT UI --\n"); - ((ethQueryContractUI_t *) parameter)->pluginSharedRW = &pluginRW; - ((ethQueryContractUI_t *) parameter)->pluginSharedRO = &pluginRO; + ((ethQueryContractUI_t *) parameter)->txContent = &tmpContent.txContent; ((ethQueryContractUI_t *) parameter)->pluginContext = (uint8_t *) &dataContext.tokenContext.pluginContext; break; diff --git a/src/main.c b/src/main.c index c148a7acc0..79e9c5d547 100644 --- a/src/main.c +++ b/src/main.c @@ -114,7 +114,7 @@ unsigned short io_exchange_al(unsigned char channel, unsigned short tx_len) { return 0; } -extraInfo_t *getKnownToken(uint8_t *contractAddress) { +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 diff --git a/src_common/eth_plugin_interface.h b/src_common/eth_plugin_interface.h index faa2be5f34..44e689ebd8 100644 --- a/src_common/eth_plugin_interface.h +++ b/src_common/eth_plugin_interface.h @@ -75,18 +75,6 @@ typedef enum eth_ui_type_e { } eth_ui_type_t; -// Scratch objects and utilities available to the plugin READ-WRITE -typedef struct ethPluginSharedRW_s { - cx_sha3_t *sha3; -} ethPluginSharedRW_t; - - -// Transaction data available to the plugin READ-ONLY -typedef struct ethPluginSharedRO_s { - txContent_t *txContent; -} ethPluginSharedRO_t; - - // Plugin-only memory allocated by the Ethereum application and used by the plugin. #define PLUGIN_CONTEXT_SIZE (5 * INT256_LENGTH) // It is recommended to cast the raw uin8_t array to a structure meaningful for your plugin @@ -106,17 +94,19 @@ typedef struct ethPluginSharedRO_s { // Init Contract typedef struct ethPluginInitContract_s { + // READ ONLY // eth_plugin_interface_version_t interfaceVersion; - eth_plugin_result_t result; - - // in - ethPluginSharedRW_t *pluginSharedRW; - ethPluginSharedRO_t *pluginSharedRO; - uint8_t *pluginContext; + const txContent_t *txContent; size_t pluginContextLength; const uint8_t *selector; // 4 bytes selector size_t dataSize; + // READ WRITE // + uint8_t *pluginContext; // PLUGIN_CONTEXT_SIZE + + // WRITE ONLY // + eth_plugin_result_t result; + } ethPluginInitContract_t; // void handle_init_contract(ethPluginInitContract_t *parameters); @@ -124,12 +114,15 @@ typedef struct ethPluginInitContract_s { // Provide parameter typedef struct ethPluginProvideParameter_s { - ethPluginSharedRW_t *pluginSharedRW; - ethPluginSharedRO_t *pluginSharedRO; - uint8_t *pluginContext; // PLUGIN_CONTEXT_SIZE + // READ ONLY // + const txContent_t *txContent; const uint8_t *parameter; // 32 bytes parameter uint32_t parameterOffset; + // READ WRITE // + uint8_t *pluginContext; // PLUGIN_CONTEXT_SIZE + + // WRITE ONLY // eth_plugin_result_t result; } ethPluginProvideParameter_t; @@ -139,19 +132,22 @@ typedef struct ethPluginProvideParameter_s { // Finalize typedef struct ethPluginFinalize_s { - ethPluginSharedRW_t *pluginSharedRW; - ethPluginSharedRO_t *pluginSharedRO; - uint8_t *pluginContext; // PLUGIN_CONTEXT_SIZE + // READ ONLY // + const txContent_t *txContent; - uint8_t *tokenLookup1; // set by the plugin if a token should be looked up - uint8_t *tokenLookup2; + // READ WRITE // + uint8_t *pluginContext; // PLUGIN_CONTEXT_SIZE - const uint8_t *amount; // set an uint256 pointer if uiType is UI_AMOUNT_ADDRESS - const uint8_t *address; // set to the destination address if uiType is UI_AMOUNT_ADDRESS. Set - // to the user's address if uiType is UI_TYPE_GENERIC + // WRITE ONLY // + const uint8_t *tokenLookup1; // set by the plugin if a token should be looked up + const uint8_t *tokenLookup2; eth_ui_type_t uiType; - uint8_t numScreens; // ignored if uiType is UI_AMOUNT_ADDRESS + const uint8_t *amount; // set an uint256 pointer if uiType is UI_AMOUNT_ADDRESS + const uint8_t *address; // set to the destination address if uiType is UI_AMOUNT_ADDRESS. + // set to the user's address if uiType is UI_TYPE_GENERIC + uint8_t numScreens; // set the number of screens to display if uiType is UI_TYPE_GENERIC + eth_plugin_result_t result; } ethPluginFinalize_t; @@ -161,13 +157,16 @@ typedef struct ethPluginFinalize_s { // Provide token typedef struct ethPluginProvideInfo_s { - ethPluginSharedRW_t *pluginSharedRW; - ethPluginSharedRO_t *pluginSharedRO; - uint8_t *pluginContext; // PLUGIN_CONTEXT_SIZE - + // READ ONLY // + const txContent_t *txContent; union extraInfo_t *item1; // set by the ETH application, to be saved by the plugin union extraInfo_t *item2; + // READ WRITE // + uint8_t *pluginContext; // PLUGIN_CONTEXT_SIZE + + + // WRITE ONLY // uint8_t additionalScreens; // Used by the plugin if it needs to display additional screens // based on the information received from the token definitions. @@ -182,14 +181,17 @@ typedef struct ethPluginProvideInfo_s { // This is always called on the non aliased contract typedef struct ethQueryContractID_s { - ethPluginSharedRW_t *pluginSharedRW; - ethPluginSharedRO_t *pluginSharedRO; + // READ ONLY // + const txContent_t *txContent; + size_t nameLength; + size_t versionLength; + + // READ WRITE // uint8_t *pluginContext; // PLUGIN_CONTEXT_SIZE + // WRITE ONLY // char *name; - size_t nameLength; char *version; - size_t versionLength; eth_plugin_result_t result; @@ -200,18 +202,21 @@ typedef struct ethQueryContractID_s { // Query Contract UI typedef struct ethQueryContractUI_s { - ethPluginSharedRW_t *pluginSharedRW; - ethPluginSharedRO_t *pluginSharedRO; + // READ ONLY // + const txContent_t *txContent; union extraInfo_t *item1; union extraInfo_t *item2; char network_ticker[MAX_TICKER_LEN]; - uint8_t *pluginContext; // PLUGIN_CONTEXT_SIZE + size_t titleLength; + size_t msgLength; uint8_t screenIndex; + // READ WRITE // + uint8_t *pluginContext; // PLUGIN_CONTEXT_SIZE + + // WRITE ONLY // char *title; - size_t titleLength; char *msg; - size_t msgLength; eth_plugin_result_t result; diff --git a/src_plugins/erc1155/erc1155_plugin.c b/src_plugins/erc1155/erc1155_plugin.c index b81aa8d5c5..3dd699aa5f 100644 --- a/src_plugins/erc1155/erc1155_plugin.c +++ b/src_plugins/erc1155/erc1155_plugin.c @@ -61,7 +61,7 @@ static void handle_finalize(void *parameters) { erc1155_context_t *context = (erc1155_context_t *) msg->pluginContext; if (context->selectorIndex != SAFE_BATCH_TRANSFER) { - msg->tokenLookup1 = msg->pluginSharedRO->txContent->destination; + msg->tokenLookup1 = msg->txContent->destination; } else { msg->tokenLookup1 = NULL; } @@ -82,8 +82,7 @@ static void handle_finalize(void *parameters) { return; } // Check if some ETH is attached to this tx - if (!allzeroes((void *) &msg->pluginSharedRO->txContent->value, - sizeof(msg->pluginSharedRO->txContent->value))) { + if (!allzeroes((void *) &msg->txContent->value, sizeof(msg->txContent->value))) { // Those functions are not payable so return an error. msg->result = ETH_PLUGIN_RESULT_ERROR; return; diff --git a/src_plugins/erc20/erc20_plugin.c b/src_plugins/erc20/erc20_plugin.c index c104974005..b98c84ef8e 100644 --- a/src_plugins/erc20/erc20_plugin.c +++ b/src_plugins/erc20/erc20_plugin.c @@ -79,7 +79,7 @@ void erc20_plugin_call(int message, void *parameters) { ethPluginInitContract_t *msg = (ethPluginInitContract_t *) parameters; erc20_parameters_t *context = (erc20_parameters_t *) msg->pluginContext; // enforce that ETH amount should be 0 - if (!allzeroes(msg->pluginSharedRO->txContent->value.value, 32)) { + if (!allzeroes(msg->txContent->value.value, 32)) { PRINTF("Err: Transaction amount is not 0\n"); msg->result = ETH_PLUGIN_RESULT_ERROR; } else { @@ -129,13 +129,13 @@ void erc20_plugin_call(int message, void *parameters) { erc20_parameters_t *context = (erc20_parameters_t *) msg->pluginContext; PRINTF("erc20 plugin finalize\n"); if (context->selectorIndex == ERC20_TRANSFER) { - msg->tokenLookup1 = msg->pluginSharedRO->txContent->destination; + msg->tokenLookup1 = msg->txContent->destination; msg->amount = context->amount; msg->address = context->destinationAddress; msg->uiType = ETH_UI_TYPE_AMOUNT_ADDRESS; msg->result = ETH_PLUGIN_RESULT_OK; } else if (context->selectorIndex == ERC20_APPROVE) { - msg->tokenLookup1 = msg->pluginSharedRO->txContent->destination; + msg->tokenLookup1 = msg->txContent->destination; msg->numScreens = 2; msg->uiType = ETH_UI_TYPE_GENERIC; msg->result = ETH_PLUGIN_RESULT_OK; diff --git a/src_plugins/erc721/erc721_plugin.c b/src_plugins/erc721/erc721_plugin.c index bfe12cd9d1..c3101e1418 100644 --- a/src_plugins/erc721/erc721_plugin.c +++ b/src_plugins/erc721/erc721_plugin.c @@ -67,7 +67,7 @@ static void handle_finalize(void *parameters) { ethPluginFinalize_t *msg = (ethPluginFinalize_t *) parameters; erc721_context_t *context = (erc721_context_t *) msg->pluginContext; - msg->tokenLookup1 = msg->pluginSharedRO->txContent->destination; + msg->tokenLookup1 = msg->txContent->destination; msg->tokenLookup2 = NULL; switch (context->selectorIndex) { case TRANSFER: @@ -84,8 +84,7 @@ static void handle_finalize(void *parameters) { return; } // Check if some ETH is attached to this tx - if (!allzeroes((void *) &msg->pluginSharedRO->txContent->value, - sizeof(msg->pluginSharedRO->txContent->value))) { + if (!allzeroes((void *) &msg->txContent->value, sizeof(msg->txContent->value))) { // Set Approval for All is not payable if (context->selectorIndex == SET_APPROVAL_FOR_ALL) { msg->result = ETH_PLUGIN_RESULT_ERROR; diff --git a/src_plugins/eth2/eth2_plugin.c b/src_plugins/eth2/eth2_plugin.c index c18462a8e3..27535721f1 100644 --- a/src_plugins/eth2/eth2_plugin.c +++ b/src_plugins/eth2/eth2_plugin.c @@ -37,7 +37,7 @@ void eth2_plugin_call(int message, void *parameters) { ethPluginInitContract_t *msg = (ethPluginInitContract_t *) parameters; eth2_deposit_parameters_t *context = (eth2_deposit_parameters_t *) msg->pluginContext; if (memcmp(deposit_contract_address, - msg->pluginSharedRO->txContent->destination, + msg->txContent->destination, sizeof(deposit_contract_address)) != 0) { PRINTF("eth2plugin: failed to check deposit contract\n"); context->valid = 0;