Skip to content

Commit

Permalink
Merge pull request #13 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 e370ca9 + b621e89 commit 3f09568
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 191 deletions.
2 changes: 1 addition & 1 deletion ethereum-plugin-sdk
123 changes: 0 additions & 123 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 <string.h>

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

#include "staderlabs_plugin.h"

// List of selectors supported by this plugin.
Expand Down Expand Up @@ -137,119 +130,3 @@ const uint32_t STADERLABS_SELECTORS[NUM_SELECTORS] = {
ETHX_REQUEST_WITHDRAW_SELECTOR,
ETHX_CLAIM_SELECTOR,
};

// Function to dispatch calls from the ethereum app.
void dispatch_plugin_calls(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;
}
}

// Calls the ethereum app.
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;
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 = 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);
}

// Weird low-level black magic. No need to edit this.
__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 = (const 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 `dispatch_plugin_calls`.
if (args[0] != ETH_PLUGIN_CHECK_PRESENCE) {
dispatch_plugin_calls(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 {
// Call `os_lib_end`, go back to the ethereum app.
os_lib_end();
}
}
END_TRY;

// Will not get reached.
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 "staderlabs_plugin.h"

void handle_finalize(void *parameters) {
ethPluginFinalize_t *msg = (ethPluginFinalize_t *) parameters;
void handle_finalize(ethPluginFinalize_t *msg) {
context_t *context = (context_t *) msg->pluginContext;

switch (context->selectorIndex) {
Expand Down
5 changes: 1 addition & 4 deletions src/handle_init_contract.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ static int find_selector(uint32_t selector, const uint32_t *selectors, size_t n,
}

// Called once to init.
void handle_init_contract(void *parameters) {
// Cast the msg to the type of structure we expect (here, ethPluginInitContract_t).
ethPluginInitContract_t *msg = (ethPluginInitContract_t *) parameters;

void handle_init_contract(ethPluginInitContract_t *msg) {
// Make sure we are running a compatible version.
if (msg->interfaceVersion != ETH_PLUGIN_INTERFACE_VERSION_LATEST) {
// If not the case, return the `UNAVAILABLE` status.
Expand Down
5 changes: 2 additions & 3 deletions src/handle_provide_parameter.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ static void handle_ethx_request_withdraw(ethPluginProvideParameter_t *msg, conte
}
}

void handle_provide_parameter(void *parameters) {
ethPluginProvideParameter_t *msg = (ethPluginProvideParameter_t *) parameters;
void handle_provide_parameter(ethPluginProvideParameter_t *msg) {
context_t *context = (context_t *) msg->pluginContext;
// We use `%.*H`: it's a utility function to print bytes. You first give
// the number of bytes you wish to print (in this case, `PARAMETER_LENGTH`) and then
Expand Down Expand Up @@ -125,4 +124,4 @@ void handle_provide_parameter(void *parameters) {
msg->result = ETH_PLUGIN_RESULT_ERROR;
break;
}
}
}
3 changes: 1 addition & 2 deletions src/handle_provide_token.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// EDIT THIS: Adapt this function to your needs! Remember, the information for tokens are held in
// `msg->token1` and `msg->token2`. If those pointers are `NULL`, this means the ethereum app didn't
// find any info regarding the requested tokens!
void handle_provide_token(void *parameters) {
ethPluginProvideInfo_t *msg = (ethPluginProvideInfo_t *) parameters;
void handle_provide_token(ethPluginProvideInfo_t *msg) {
msg->result = ETH_PLUGIN_RESULT_OK;
}
5 changes: 2 additions & 3 deletions src/handle_query_contract_id.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#include "staderlabs_plugin.h"

// Sets the first screen to display.
void handle_query_contract_id(void *parameters) {
ethQueryContractID_t *msg = (ethQueryContractID_t *) parameters;
void handle_query_contract_id(ethQueryContractID_t *msg) {
const context_t *context = (const context_t *) msg->pluginContext;
// msg->name will be the upper sentence displayed on the screen.
// msg->version will be the lower sentence displayed on the screen.
Expand Down Expand Up @@ -45,4 +44,4 @@ void handle_query_contract_id(void *parameters) {

strlcpy(msg->version, msgVersion, msg->versionLength);
msg->result = ETH_PLUGIN_RESULT_OK;
}
}
Loading

0 comments on commit 3f09568

Please sign in to comment.