From 17309ec8d8ec3654902a55cd652b087068370a87 Mon Sep 17 00:00:00 2001 From: Alexandre Paillier Date: Mon, 16 Oct 2023 17:24:12 +0200 Subject: [PATCH] Updated to use the latest plugin SDK --- ethereum-plugin-sdk | 2 +- src/{main.c => contract.c} | 117 --------------------------------- src/handle_finalize.c | 3 +- src/handle_init_contract.c | 4 +- src/handle_provide_parameter.c | 3 +- src/handle_provide_token.c | 3 +- src/handle_query_contract_id.c | 3 +- src/handle_query_contract_ui.c | 64 +++++++++--------- src/paraswap_plugin.h | 7 -- tests/package.json | 2 +- tests/yarn.lock | 42 ++++++++---- 11 files changed, 67 insertions(+), 183 deletions(-) rename src/{main.c => contract.c} (72%) diff --git a/ethereum-plugin-sdk b/ethereum-plugin-sdk index b9777e7..1fe4085 160000 --- a/ethereum-plugin-sdk +++ b/ethereum-plugin-sdk @@ -1 +1 @@ -Subproject commit b9777e7a81a9c33b3f8997ffa23396fa2b3f861d +Subproject commit 1fe4085d04a88f4238103a4ed3db1484fdb69c63 diff --git a/src/main.c b/src/contract.c similarity index 72% rename from src/main.c rename to src/contract.c index 1128167..3c6e1c0 100644 --- a/src/main.c +++ b/src/contract.c @@ -15,14 +15,7 @@ * limitations under the License. ********************************************************************************/ -#include #include - -#include "os.h" -#include "cx.h" - -#include "glyphs.h" - #include "paraswap_plugin.h" // ---------- Paraswap V5 ------------- @@ -175,113 +168,3 @@ const uint8_t PARASWAP_ETH_ADDRESS[ADDRESS_LENGTH] = {0xee, 0xee, 0xee, 0xee, 0x const uint8_t NULL_ETH_ADDRESS[ADDRESS_LENGTH] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; - -void paraswap_plugin_call(int message, void *parameters) { - switch (message) { - case ETH_PLUGIN_INIT_CONTRACT: - handle_init_contract(parameters); - break; - case ETH_PLUGIN_PROVIDE_PARAMETER: - handle_provide_parameter(parameters); - break; - case ETH_PLUGIN_FINALIZE: - handle_finalize(parameters); - break; - case ETH_PLUGIN_PROVIDE_INFO: - handle_provide_token(parameters); - break; - case ETH_PLUGIN_QUERY_CONTRACT_ID: - handle_query_contract_id(parameters); - break; - case ETH_PLUGIN_QUERY_CONTRACT_UI: - handle_query_contract_ui(parameters); - break; - default: - PRINTF("Unhandled message %d\n", message); - break; - } -} - -void handle_query_ui_exception(unsigned int *args) { - switch (args[0]) { - case ETH_PLUGIN_QUERY_CONTRACT_UI: - ((ethQueryContractUI_t *) args[1])->result = ETH_PLUGIN_RESULT_ERROR; - break; - default: - break; - } -} - -void call_app_ethereum() { - unsigned int libcall_params[5]; - - libcall_params[0] = (unsigned int) "Ethereum"; - libcall_params[1] = 0x100; - libcall_params[2] = RUN_APPLICATION; - libcall_params[3] = (unsigned int) NULL; -#ifdef HAVE_NBGL - caller_app_t capp; - const char name[] = APPNAME; - nbgl_icon_details_t icon_details; - uint8_t bitmap[sizeof(ICONBITMAP)]; - - memcpy(&icon_details, &ICONGLYPH, sizeof(ICONGLYPH)); - memcpy(&bitmap, &ICONBITMAP, sizeof(bitmap)); - icon_details.bitmap = (const uint8_t *) bitmap; - capp.name = (const char *) name; - capp.icon = &icon_details; - libcall_params[4] = (unsigned int) &capp; -#else - libcall_params[4] = (unsigned int) NULL; -#endif - os_lib_call((unsigned int *) &libcall_params); -} - -__attribute__((section(".boot"))) int main(int arg0) { - // exit critical section - __asm volatile("cpsie i"); - - // ensure exception will work as planned - os_boot(); - - // Try catch block. Please read the docs for more information on how to use those! - BEGIN_TRY { - TRY { - // Low-level black magic. - check_api_level(CX_COMPAT_APILEVEL); - // Check if we are called from the dashboard. - if (!arg0) { - // called from dashboard, launch Ethereum app - call_app_ethereum(); - return 0; - } else { - // Not called from dashboard: called from the ethereum app! - const unsigned int *args = (unsigned int *) arg0; - - // If `ETH_PLUGIN_CHECK_PRESENCE` is set, this means the caller is just trying to - // know whether this app exists or not. We can skip `paraswap_plugin_call`. - if (args[0] != ETH_PLUGIN_CHECK_PRESENCE) { - paraswap_plugin_call(args[0], (void *) args[1]); - } - } - } - CATCH_OTHER(e) { - switch (e) { - // These exceptions are only generated on handle_query_contract_ui() - case 0x6502: - case EXCEPTION_OVERFLOW: - handle_query_ui_exception((unsigned int *) arg0); - break; - default: - break; - } - PRINTF("Exception 0x%x caught\n", e); - } - FINALLY { - os_lib_end(); - } - } - END_TRY; - - return 0; -} diff --git a/src/handle_finalize.c b/src/handle_finalize.c index b4a01ae..3049c1b 100644 --- a/src/handle_finalize.c +++ b/src/handle_finalize.c @@ -1,7 +1,6 @@ #include "paraswap_plugin.h" -void handle_finalize(void *parameters) { - ethPluginFinalize_t *msg = (ethPluginFinalize_t *) parameters; +void handle_finalize(ethPluginFinalize_t *msg) { paraswap_parameters_t *context = (paraswap_parameters_t *) msg->pluginContext; if (context->valid) { msg->numScreens = 2; diff --git a/src/handle_init_contract.c b/src/handle_init_contract.c index b48ff45..a247b67 100644 --- a/src/handle_init_contract.c +++ b/src/handle_init_contract.c @@ -1,9 +1,7 @@ #include "paraswap_plugin.h" // Called once to init. -void handle_init_contract(void *parameters) { - ethPluginInitContract_t *msg = (ethPluginInitContract_t *) parameters; - +void handle_init_contract(ethPluginInitContract_t *msg) { if (msg->interfaceVersion != ETH_PLUGIN_INTERFACE_VERSION_LATEST) { PRINTF("Wrong interface version: expected %d got %d\n", ETH_PLUGIN_INTERFACE_VERSION_LATEST, diff --git a/src/handle_provide_parameter.c b/src/handle_provide_parameter.c index 771291d..d626713 100644 --- a/src/handle_provide_parameter.c +++ b/src/handle_provide_parameter.c @@ -347,8 +347,7 @@ static void handle_swap_uni_v2(ethPluginProvideParameter_t *msg, paraswap_parame } } -void handle_provide_parameter(void *parameters) { - ethPluginProvideParameter_t *msg = (ethPluginProvideParameter_t *) parameters; +void handle_provide_parameter(ethPluginProvideParameter_t *msg) { paraswap_parameters_t *context = (paraswap_parameters_t *) msg->pluginContext; msg->result = ETH_PLUGIN_RESULT_OK; diff --git a/src/handle_provide_token.c b/src/handle_provide_token.c index ef6737d..ed6f893 100644 --- a/src/handle_provide_token.c +++ b/src/handle_provide_token.c @@ -1,7 +1,6 @@ #include "paraswap_plugin.h" -void handle_provide_token(void *parameters) { - ethPluginProvideInfo_t *msg = (ethPluginProvideInfo_t *) parameters; +void handle_provide_token(ethPluginProvideInfo_t *msg) { paraswap_parameters_t *context = (paraswap_parameters_t *) msg->pluginContext; PRINTF("PARASWAP plugin provide token: 0x%p, 0x%p\n", msg->item1, msg->item2); diff --git a/src/handle_query_contract_id.c b/src/handle_query_contract_id.c index e4edb9d..ca0d09a 100644 --- a/src/handle_query_contract_id.c +++ b/src/handle_query_contract_id.c @@ -1,7 +1,6 @@ #include "paraswap_plugin.h" -void handle_query_contract_id(void *parameters) { - ethQueryContractID_t *msg = (ethQueryContractID_t *) parameters; +void handle_query_contract_id(ethQueryContractID_t *msg) { const paraswap_parameters_t *context = (paraswap_parameters_t *) msg->pluginContext; strlcpy(msg->name, PLUGIN_NAME, msg->nameLength); diff --git a/src/handle_query_contract_ui.c b/src/handle_query_contract_ui.c index 4024bb7..ed69a5a 100644 --- a/src/handle_query_contract_ui.c +++ b/src/handle_query_contract_ui.c @@ -1,7 +1,8 @@ +#include #include "paraswap_plugin.h" // Set UI for the "Send" screen. -static void set_send_ui(ethQueryContractUI_t *msg, paraswap_parameters_t *context) { +static bool set_send_ui(ethQueryContractUI_t *msg, paraswap_parameters_t *context) { switch (context->selectorIndex) { case SWAP_ON_UNI_FORK: case SWAP_ON_UNI_V2_FORK: @@ -28,23 +29,22 @@ static void set_send_ui(ethQueryContractUI_t *msg, paraswap_parameters_t *contex break; default: PRINTF("Unhandled selector Index: %d\n", context->selectorIndex); - msg->result = ETH_PLUGIN_RESULT_ERROR; - return; + return false; } if (ADDRESS_IS_NETWORK_TOKEN(context->contract_address_sent)) { strlcpy(context->ticker_sent, msg->network_ticker, sizeof(context->ticker_sent)); } - amountToString(context->amount_sent, - sizeof(context->amount_sent), - context->decimals_sent, - context->ticker_sent, - msg->msg, - msg->msgLength); + return amountToString(context->amount_sent, + sizeof(context->amount_sent), + context->decimals_sent, + context->ticker_sent, + msg->msg, + msg->msgLength); } // Set UI for "Receive" screen. -static void set_receive_ui(ethQueryContractUI_t *msg, paraswap_parameters_t *context) { +static bool set_receive_ui(ethQueryContractUI_t *msg, paraswap_parameters_t *context) { switch (context->selectorIndex) { case SWAP_ON_UNI_FORK: case SWAP_ON_UNI_V2_FORK: @@ -71,40 +71,40 @@ static void set_receive_ui(ethQueryContractUI_t *msg, paraswap_parameters_t *con break; default: PRINTF("Unhandled selector Index: %d\n", context->selectorIndex); - msg->result = ETH_PLUGIN_RESULT_ERROR; - return; + return false; } if (ADDRESS_IS_NETWORK_TOKEN(context->contract_address_received)) { strlcpy(context->ticker_received, msg->network_ticker, sizeof(context->ticker_received)); } - amountToString(context->amount_received, - sizeof(context->amount_received), - context->decimals_received, - context->ticker_received, - msg->msg, - msg->msgLength); + return amountToString(context->amount_received, + sizeof(context->amount_received), + context->decimals_received, + context->ticker_received, + msg->msg, + msg->msgLength); } // Set UI for "Beneficiary" screen. -static void set_beneficiary_ui(ethQueryContractUI_t *msg, paraswap_parameters_t *context) { +static bool set_beneficiary_ui(ethQueryContractUI_t *msg, paraswap_parameters_t *context) { strlcpy(msg->title, "Beneficiary", msg->titleLength); msg->msg[0] = '0'; msg->msg[1] = 'x'; - getEthAddressStringFromBinary((uint8_t *) context->beneficiary, - msg->msg + 2, - msg->pluginSharedRW->sha3, - 0); + return getEthAddressStringFromBinary((uint8_t *) context->beneficiary, + msg->msg + 2, + msg->pluginSharedRW->sha3, + 0); } // Set UI for "Warning" screen. -static void set_warning_ui(ethQueryContractUI_t *msg, +static bool set_warning_ui(ethQueryContractUI_t *msg, const paraswap_parameters_t *context __attribute__((unused))) { strlcpy(msg->title, "WARNING", msg->titleLength); strlcpy(msg->msg, "Unknown token", msg->msgLength); + return true; } // Helper function that returns the enum corresponding to the screen that should be displayed. @@ -173,31 +173,29 @@ static screens_t get_screen(const ethQueryContractUI_t *msg, const paraswap_para return ERROR; } -void handle_query_contract_ui(void *parameters) { - ethQueryContractUI_t *msg = (ethQueryContractUI_t *) parameters; +void handle_query_contract_ui(ethQueryContractUI_t *msg) { paraswap_parameters_t *context = (paraswap_parameters_t *) msg->pluginContext; + bool ret = false; memset(msg->title, 0, msg->titleLength); memset(msg->msg, 0, msg->msgLength); - msg->result = ETH_PLUGIN_RESULT_OK; screens_t screen = get_screen(msg, context); switch (screen) { case SEND_SCREEN: - set_send_ui(msg, context); + ret = set_send_ui(msg, context); break; case RECEIVE_SCREEN: - set_receive_ui(msg, context); + ret = set_receive_ui(msg, context); break; case BENEFICIARY_SCREEN: - set_beneficiary_ui(msg, context); + ret = set_beneficiary_ui(msg, context); break; case WARN_SCREEN: - set_warning_ui(msg, context); + ret = set_warning_ui(msg, context); break; default: PRINTF("Received an invalid screenIndex\n"); - msg->result = ETH_PLUGIN_RESULT_ERROR; - return; } + msg->result = ret ? ETH_PLUGIN_RESULT_OK : ETH_PLUGIN_RESULT_ERROR; } diff --git a/src/paraswap_plugin.h b/src/paraswap_plugin.h index ed16067..92ce717 100644 --- a/src/paraswap_plugin.h +++ b/src/paraswap_plugin.h @@ -109,10 +109,3 @@ typedef struct paraswap_parameters_t { uint8_t skip; // 4 * 1 + 2 * 2 + 7 * 1 == 8 + 7 == 15 bytes. There are 16 - 15 == 1 byte left. } paraswap_parameters_t; - -void handle_init_contract(void *parameters); -void handle_provide_parameter(void *parameters); -void handle_query_contract_ui(void *parameters); -void handle_finalize(void *parameters); -void handle_provide_token(void *parameters); -void handle_query_contract_id(void *parameters); \ No newline at end of file diff --git a/tests/package.json b/tests/package.json index 13995cc..6fb547e 100644 --- a/tests/package.json +++ b/tests/package.json @@ -15,7 +15,7 @@ "@ledgerhq/hw-app-eth": "^6.9.0", "@ledgerhq/hw-transport-http": "^4.74.2", "@ledgerhq/logs": "^5.50.0", - "@zondax/zemu": "^0.27.4", + "@zondax/zemu": "^0.32.0", "bignumber.js": "^9.0.0", "bip32-path": "^0.4.2", "core-js": "^3.7.0", diff --git a/tests/yarn.lock b/tests/yarn.lock index b7a1ab7..db98702 100644 --- a/tests/yarn.lock +++ b/tests/yarn.lock @@ -1881,16 +1881,16 @@ dependencies: "@types/yargs-parser" "*" -"@zondax/zemu@^0.27.4": - version "0.27.4" - resolved "https://registry.yarnpkg.com/@zondax/zemu/-/zemu-0.27.4.tgz#4e306ba76f5c718d901c7948516668e944a6aa1e" - integrity sha512-bbYAW9JJUx+hVBdwkTonu+0q9WkeFQpcGMRUrzS/Gfc6rvhAQ+X9KrGhwGrnxmBuzajt0oXZ50QvoEJHBR9PDA== +"@zondax/zemu@^0.32.0": + version "0.32.0" + resolved "https://registry.yarnpkg.com/@zondax/zemu/-/zemu-0.32.0.tgz#733ae016c464d813721bcaef273c759bde3be4a9" + integrity sha512-xbcTu/G3bRz1ubf3fDjUtNnZTSnwv5MPFaKWpKQFGRp+iIS0FdPBFGLRtilN/JqsGiF2DebI9DFkFNFIV+Dh9g== dependencies: "@grpc/grpc-js" "^1.5.5" "@grpc/proto-loader" "^0.6.9" "@ledgerhq/hw-transport" "^6.24.1" "@ledgerhq/hw-transport-http" "^6.24.1" - axios "^0.26.0" + axios "^0.27.2" axios-retry "^3.2.0" dockerode "^3.3.1" elfy "^1.0.0" @@ -1899,7 +1899,6 @@ path "^0.12.7" pngjs "^6.0.0" randomstring "^1.2.1" - rfb2 "^0.2.2" sleep "^6.3.0" abab@^2.0.0, abab@^2.0.3: @@ -2116,13 +2115,21 @@ axios@^0.23.0: dependencies: follow-redirects "^1.14.4" -axios@^0.26.0, axios@^0.26.1: +axios@^0.26.1: version "0.26.1" resolved "https://registry.yarnpkg.com/axios/-/axios-0.26.1.tgz#1ede41c51fcf51bbbd6fd43669caaa4f0495aaa9" integrity sha512-fPwcX4EvnSHuInCMItEhAGnaSEXRBjtzh9fOtsE6E1G6p7vl7edEeZe11QHf18+6+9gR5PbKV/sGKNaD8YaMeA== dependencies: follow-redirects "^1.14.8" +axios@^0.27.2: + version "0.27.2" + resolved "https://registry.yarnpkg.com/axios/-/axios-0.27.2.tgz#207658cc8621606e586c85db4b41a750e756d972" + integrity sha512-t+yRIyySRTp/wua5xEr+z1q60QmLq8ABsS5O9Me1AsE5dfKqgnCFzwiCZZ/cGNd1lq4/7akDWMxdhVlucjmnOQ== + dependencies: + follow-redirects "^1.14.9" + form-data "^4.0.0" + babel-jest@^24.9.0: version "24.9.0" resolved "https://registry.yarnpkg.com/babel-jest/-/babel-jest-24.9.0.tgz#3fc327cb8467b89d14d7bc70e315104a783ccd54" @@ -2612,7 +2619,7 @@ colorette@^1.2.1: resolved "https://registry.yarnpkg.com/colorette/-/colorette-1.2.1.tgz#4d0b921325c14faf92633086a536db6e89564b1b" integrity sha512-puCDz0CzydiSYOrnXpz/PKd69zRrribezjtE9yd4zvytoRc8+RY/KJPvtPFKZS3E3wP6neGyMe0vOTlHO5L3Pw== -combined-stream@^1.0.6, combined-stream@~1.0.6: +combined-stream@^1.0.6, combined-stream@^1.0.8, combined-stream@~1.0.6: version "1.0.8" resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f" integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg== @@ -3353,6 +3360,11 @@ follow-redirects@^1.14.8: resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.9.tgz#dd4ea157de7bfaf9ea9b3fbd85aa16951f78d8d7" integrity sha512-MQDfihBQYMcyy5dhRDJUHcw7lb2Pv/TuE6xP1vyraLukNDHKbDxDNaOE3NbCAdKQApno+GPRyo1YAp89yCjK4w== +follow-redirects@^1.14.9: + version "1.15.3" + resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.15.3.tgz#fe2f3ef2690afce7e82ed0b44db08165b207123a" + integrity sha512-1VzOtuEM8pC9SFU1E+8KfTjZyMztRsgEfwQl44z8A25uy13jSzTj6dyK2Df52iV0vgHCfBwLhDWevLn95w5v6Q== + for-in@^1.0.2: version "1.0.2" resolved "https://registry.yarnpkg.com/for-in/-/for-in-1.0.2.tgz#81068d295a8142ec0ac726c6e2200c30fb6d5e80" @@ -3363,6 +3375,15 @@ forever-agent@~0.6.1: resolved "https://registry.yarnpkg.com/forever-agent/-/forever-agent-0.6.1.tgz#fbc71f0c41adeb37f96c577ad1ed42d8fdacca91" integrity sha1-+8cfDEGt6zf5bFd60e1C2P2sypE= +form-data@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/form-data/-/form-data-4.0.0.tgz#93919daeaf361ee529584b9b31664dc12c9fa452" + integrity sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww== + dependencies: + asynckit "^0.4.0" + combined-stream "^1.0.8" + mime-types "^2.1.12" + form-data@~2.3.2: version "2.3.3" resolved "https://registry.yarnpkg.com/form-data/-/form-data-2.3.3.tgz#dcce52c05f644f298c6a7ab936bd724ceffbf3a6" @@ -5821,11 +5842,6 @@ ret@~0.1.10: resolved "https://registry.yarnpkg.com/ret/-/ret-0.1.15.tgz#b8a4825d5bdb1fc3f6f53c2bc33f81388681c7bc" integrity sha512-TTlYpa+OL+vMMNG24xSlQGEJ3B/RzEfUlLct7b5G/ytav+wPrplCpVMFuwzXbkecJrb6IYo1iFb0S9v37754mg== -rfb2@^0.2.2: - version "0.2.2" - resolved "https://registry.yarnpkg.com/rfb2/-/rfb2-0.2.2.tgz#f9444b8803e6a31848e57911ace562ce0fee5598" - integrity sha512-+Aw0oED0zsoNQYFE3FUsD+a/lm9y8YwdQaERlWHm7G5hey3tiSQGq7tfe5sFAw5fbN7Zms38bVEDxIUAOq3mRw== - rimraf@^3.0.0: version "3.0.2" resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-3.0.2.tgz#f1a5402ba6220ad52cc1282bac1ae3aa49fd061a"