Skip to content

Commit

Permalink
Remove pluginSharedRW and pluginSharedRO from plugin API
Browse files Browse the repository at this point in the history
  • Loading branch information
fbeutin-ledger committed Apr 17, 2024
1 parent 2e97cdf commit 052b06d
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 71 deletions.
23 changes: 6 additions & 17 deletions src/eth_plugin_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 =
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
91 changes: 48 additions & 43 deletions src_common/eth_plugin_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -106,30 +94,35 @@ 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);


// 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;
Expand All @@ -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;
Expand All @@ -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.

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

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

Expand Down
5 changes: 2 additions & 3 deletions src_plugins/erc1155/erc1155_plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src_plugins/erc20/erc20_plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions src_plugins/erc721/erc721_plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src_plugins/eth2/eth2_plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 052b06d

Please sign in to comment.