diff --git a/src/contract.c b/src/boilerplate_plugin.c similarity index 57% rename from src/contract.c rename to src/boilerplate_plugin.c index 48560b37..14d80cbf 100644 --- a/src/contract.c +++ b/src/boilerplate_plugin.c @@ -18,15 +18,6 @@ #include #include "boilerplate_plugin.h" -// List of selectors supported by this plugin. -// EDIT THIS: Adapt the variable names and change the `0x` values to match your selectors. -static const uint32_t SWAP_EXACT_ETH_FOR_TOKENS_SELECTOR = 0x7ff36ab5; -static const uint32_t BOILERPLATE_DUMMY_SELECTOR_2 = 0x13374242; - -// Array of all the different boilerplate selectors. Make sure this follows the same order as the -// enum defined in `boilerplate_plugin.h` -// EDIT THIS: Use the names of the array declared above. -const uint32_t BOILERPLATE_SELECTORS[NUM_SELECTORS] = { - SWAP_EXACT_ETH_FOR_TOKENS_SELECTOR, - BOILERPLATE_DUMMY_SELECTOR_2, -}; +// This array will be automatically expanded to map all selector_t names with the correct value. +// Do not modify ! +const uint32_t SELECTORS[SELECTOR_COUNT] = {SELECTORS_LIST(TO_VALUE)}; diff --git a/src/boilerplate_plugin.h b/src/boilerplate_plugin.h index eb329b6e..62c22624 100644 --- a/src/boilerplate_plugin.h +++ b/src/boilerplate_plugin.h @@ -1,25 +1,55 @@ +/******************************************************************************* + * Plugin Boilerplate + * (c) 2023 Ledger + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + ********************************************************************************/ + #pragma once +#include #include "eth_internals.h" #include "eth_plugin_interface.h" -#include - -// Number of selectors defined in this plugin. Should match the enum `selector_t`. -// EDIT THIS: Put in the number of selectors your plugin is going to support. -#define NUM_SELECTORS 2 // Name of the plugin. // EDIT THIS: Replace with your plugin name. #define PLUGIN_NAME "PluginBoilerplate" -// Enumeration of the different selectors possible. -// Should follow the exact same order as the array declared in main.c -// EDIT THIS: Change the naming (`selector_t`), and add your selector names. -typedef enum { - SWAP_EXACT_ETH_FOR_TOKENS = 0, - BOILERPLATE_DUMMY_2, +// All possible selectors of your plugin. +// EDIT THIS: Enter your selectors here, in the format X(NAME, value) +// A Xmacro below will create for you: +// - an enum named selector_t with every NAME +// - a map named SELECTORS associating each NAME with it's value +#define SELECTORS_LIST(X) \ + X(SWAP_EXACT_ETH_FOR_TOKENS, 0x7ff36ab5) \ + X(BOILERPLATE_DUMMY_2, 0x13374242) + +// Xmacro helpers to define the enum and map +// Do not modify ! +#define TO_ENUM(selector_name, selector_id) selector_name, +#define TO_VALUE(selector_name, selector_id) selector_id, + +// This enum will be automatically expanded to hold all selector names. +// The value SELECTOR_COUNT can be used to get the number of defined selectors +// Do not modify ! +typedef enum selector_e { + SELECTORS_LIST(TO_ENUM) SELECTOR_COUNT, } selector_t; +// This array will be automatically expanded to map all selector_t names with the correct value. +// Do not modify ! +extern const uint32_t SELECTORS[SELECTOR_COUNT]; + // Enumeration used to parse the smart contract data. // EDIT THIS: Adapt the parameter names here. typedef enum { @@ -31,9 +61,6 @@ typedef enum { UNEXPECTED_PARAMETER, } parameter; -// EDIT THIS: Rename `BOILERPLATE` to be the same as the one initialized in `main.c`. -extern const uint32_t BOILERPLATE_SELECTORS[NUM_SELECTORS]; - // Shared global memory with Ethereum app. Must be at most 5 * 32 bytes. // EDIT THIS: This struct is used by your plugin to save the parameters you parse. You // will need to adapt this struct to your plugin. diff --git a/src/handle_init_contract.c b/src/handle_init_contract.c index 3154cc1e..77728ab8 100644 --- a/src/handle_init_contract.c +++ b/src/handle_init_contract.c @@ -24,7 +24,7 @@ void handle_init_contract(ethPluginInitContract_t *msg) { memset(context, 0, sizeof(*context)); size_t index; - if (!find_selector(U4BE(msg->selector, 0), BOILERPLATE_SELECTORS, NUM_SELECTORS, &index)) { + if (!find_selector(U4BE(msg->selector, 0), SELECTORS, SELECTOR_COUNT, &index)) { PRINTF("Error: selector not found!\n"); msg->result = ETH_PLUGIN_RESULT_UNAVAILABLE; return;