Skip to content

Commit

Permalink
Merge pull request #22 from LedgerHQ/fix/apa/plugin_sdk_update
Browse files Browse the repository at this point in the history
Updated to use the latest plugin SDK
  • Loading branch information
apaillier-ledger authored Oct 25, 2023
2 parents d51ed69 + 17309ec commit dce974e
Show file tree
Hide file tree
Showing 11 changed files with 67 additions and 183 deletions.
2 changes: 1 addition & 1 deletion ethereum-plugin-sdk
117 changes: 0 additions & 117 deletions src/main.c → src/contract.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,7 @@
* limitations under the License.
********************************************************************************/

#include <stdbool.h>
#include <stdint.h>

#include "os.h"
#include "cx.h"

#include "glyphs.h"

#include "paraswap_plugin.h"

// ---------- Paraswap V5 -------------
Expand Down Expand Up @@ -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;
}
3 changes: 1 addition & 2 deletions src/handle_finalize.c
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 1 addition & 3 deletions src/handle_init_contract.c
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
3 changes: 1 addition & 2 deletions src/handle_provide_parameter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions src/handle_provide_token.c
Original file line number Diff line number Diff line change
@@ -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);

Expand Down
3 changes: 1 addition & 2 deletions src/handle_query_contract_id.c
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
64 changes: 31 additions & 33 deletions src/handle_query_contract_ui.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include <stdbool.h>
#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:
Expand All @@ -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:
Expand All @@ -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.
Expand Down Expand Up @@ -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;
}
7 changes: 0 additions & 7 deletions src/paraswap_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
2 changes: 1 addition & 1 deletion tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading

0 comments on commit dce974e

Please sign in to comment.