Skip to content

Commit

Permalink
Merge pull request #9 from blooo-io/feat/LAPP-9-add-transfer-ownershi…
Browse files Browse the repository at this point in the history
…p-method-in-c-files

Feat/lapp 9 add transfer ownership method in c files
  • Loading branch information
n4l5u0r authored Nov 26, 2021
2 parents 0856a47 + 61a4a8c commit b196707
Show file tree
Hide file tree
Showing 28 changed files with 424 additions and 12 deletions.
6 changes: 6 additions & 0 deletions src/handle_finalize.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

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

msg->uiType = ETH_UI_TYPE_GENERIC;

// 2 additional screens are required to display the `token and `beneficiary` fields
msg->numScreens = 2;
msg->result = ETH_PLUGIN_RESULT_OK;

if (context->selectorIndex == SAFE_TRANSFER) {
// An additional screen is required to display the `token` field for safe_transfer method.
msg->numScreens += 1;
}
}
8 changes: 7 additions & 1 deletion src/handle_init_contract.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ void handle_init_contract(void *parameters) {
}

if (msg->pluginContextLength < sizeof(context_t)) {
PRINTF("LOU: %d\n", sizeof(context_t));
PRINTF("LOU: %d\n", msg->pluginContextLength);
PRINTF("Plugin parameters structure is bigger than allowed size\n");
msg->result = ETH_PLUGIN_RESULT_ERROR;
// msg->result = ETH_PLUGIN_RESULT_ERROR;
return;
}

Expand All @@ -22,6 +24,7 @@ void handle_init_contract(void *parameters) {
uint8_t i;
for (i = 0; i < NUM_SELECTORS; i++) {
if (memcmp((uint8_t *) PIC(POAP_SELECTORS[i]), msg->selector, SELECTOR_SIZE) == 0) {
PRINTF("LOU: %d\n", context->selectorIndex);
context->selectorIndex = i;
break;
}
Expand All @@ -35,6 +38,9 @@ void handle_init_contract(void *parameters) {
case MINT_TOKEN:
context->next_param = EVENT_ID;
break;
case SAFE_TRANSFER:
context->next_param = FROM_ADDRESS;
break;
default:
PRINTF("Missing selectorIndex: %d\n", context->selectorIndex);
msg->result = ETH_PLUGIN_RESULT_ERROR;
Expand Down
32 changes: 32 additions & 0 deletions src/handle_provide_parameter.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ static void handle_beneficiary(const ethPluginProvideParameter_t *msg, context_t
PRINTF("BENEFICIARY: %.*H\n", ADDRESS_LENGTH, context->beneficiary);
}

static void handle_from_address(const ethPluginProvideParameter_t *msg, context_t *context) {
memset(context->from_address, 0, sizeof(context->from_address));
memcpy(context->from_address,
&msg->parameter[PARAMETER_LENGTH - ADDRESS_LENGTH],
sizeof(context->from_address));
PRINTF("FROM_ADDRESS: %.*H\n", ADDRESS_LENGTH, context->from_address);
}
static void handle_mint_token(ethPluginProvideParameter_t *msg, context_t *context) {
switch (context->next_param) {
case EVENT_ID:
Expand All @@ -40,6 +47,28 @@ static void handle_mint_token(ethPluginProvideParameter_t *msg, context_t *conte
break;
}
}
static void handle_safe_transfer(ethPluginProvideParameter_t *msg, context_t *context) {
switch (context->next_param) {
case FROM_ADDRESS: // from_address
handle_from_address(msg, context);
context->next_param = BENEFICIARY;
break;
case BENEFICIARY: // to
handle_beneficiary(msg, context);
context->next_param = TOKEN;
break;
case TOKEN: // id of the token received
handle_token(msg, context);
context->next_param = NONE;
break;
case NONE:
break;
default:
PRINTF("Param not supported\n");
msg->result = ETH_PLUGIN_RESULT_ERROR;
break;
}
}

void handle_provide_parameter(void *parameters) {
ethPluginProvideParameter_t *msg = (ethPluginProvideParameter_t *) parameters;
Expand All @@ -63,6 +92,9 @@ void handle_provide_parameter(void *parameters) {
case MINT_TOKEN:
handle_mint_token(msg, context);
break;
case SAFE_TRANSFER:
handle_safe_transfer(msg, context);
break;
default:
PRINTF("Selector Index not supported: %d\n", context->selectorIndex);
msg->result = ETH_PLUGIN_RESULT_ERROR;
Expand Down
4 changes: 3 additions & 1 deletion src/handle_query_contract_id.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ void handle_query_contract_id(void *parameters) {
strlcpy(msg->name, PLUGIN_NAME, msg->nameLength);

switch (context->selectorIndex) {
case SAFE_TRANSFER:
strlcpy(msg->version, "Safe Transfer", msg->versionLength);
break;
case MINT_TOKEN:
strlcpy(msg->version, "Mint", msg->versionLength);
break;
Expand All @@ -15,6 +18,5 @@ void handle_query_contract_id(void *parameters) {
msg->result = ETH_PLUGIN_RESULT_ERROR;
return;
}

msg->result = ETH_PLUGIN_RESULT_OK;
}
26 changes: 25 additions & 1 deletion src/handle_query_contract_ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ static void set_beneficiary_ui(ethQueryContractUI_t *msg, context_t *context) {
getEthAddressStringFromBinary(context->beneficiary, msg->msg + 2, msg->pluginSharedRW->sha3, 0);
}

// Set UI for "From Address" screen.
static void set_from_address_ui(ethQueryContractUI_t *msg, context_t *context) {
strlcpy(msg->title, "From Address", msg->titleLength);

msg->msg[0] = '0';
msg->msg[1] = 'x';

getEthAddressStringFromBinary(context->from_address,
msg->msg + 2,
msg->pluginSharedRW->sha3,
0);
}

// Set UI for "Warning" screen.
static void set_warning_ui(ethQueryContractUI_t *msg,
const context_t *context __attribute__((unused))) {
Expand All @@ -38,12 +51,20 @@ static screens_t get_screen(const ethQueryContractUI_t *msg, const context_t *co
}
break;
case 1:
if (!token_not_found) {
if (!token_not_found && context->selectorIndex == MINT_TOKEN) {
return BENEFICIARY_SCREEN;
} else if (!token_not_found) {
return FROM_ADDRESS_SCREEN;
} else if (token_not_found) {
return WARN_SCREEN;
}
break;
case 2:
if (!token_not_found) {
return BENEFICIARY_SCREEN;
} else if (token_not_found) {
return WARN_SCREEN;
}
default:
return ERROR;
break;
Expand All @@ -61,6 +82,9 @@ void handle_query_contract_ui(void *parameters) {
screens_t screen = get_screen(msg, context);

switch (screen) {
case FROM_ADDRESS_SCREEN:
set_from_address_ui(msg, context);
break;
case TOKEN_SCREEN:
set_token_ui(msg, context);
break;
Expand Down
3 changes: 2 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@
// Function: mintToken(uint256 eventId, uint256 tokenId, address receiver, bytes signedMessage)
// Selector: 0x3da5b8f0
static const uint8_t MINT_TOKEN_SELECTOR[SELECTOR_SIZE] = {0x3d, 0xa5, 0xb8, 0xf0};
static const uint8_t SAFE_TRANSFER_SELECTOR[SELECTOR_SIZE] = {0x42, 0x84, 0x2e, 0x0e};

// Array of all the different poap selectors.
const uint8_t *const POAP_SELECTORS[NUM_SELECTORS] = {MINT_TOKEN_SELECTOR};
const uint8_t *const POAP_SELECTORS[NUM_SELECTORS] = {MINT_TOKEN_SELECTOR, SAFE_TRANSFER_SELECTOR};

// Function to dispatch calls from the ethereum app.
void dispatch_plugin_calls(int message, void *parameters) {
Expand Down
6 changes: 5 additions & 1 deletion src/poap_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "eth_plugin_interface.h"
#include <string.h>

#define NUM_SELECTORS 1
#define NUM_SELECTORS 2
#define PLUGIN_NAME "Poap"
#define TOKEN_FOUND 1 << 1
#define SELECTOR_SIZE 4
Expand All @@ -13,19 +13,22 @@

typedef enum {
MINT_TOKEN,
SAFE_TRANSFER,
} selector_t;

// Enumeration used to parse the smart contract data.
typedef enum {
EVENT_ID,
TOKEN,
BENEFICIARY,
FROM_ADDRESS,
NONE,
} parameter;

typedef enum {
TOKEN_SCREEN,
BENEFICIARY_SCREEN,
FROM_ADDRESS_SCREEN,
WARN_SCREEN,
ERROR,
} screens_t;
Expand All @@ -36,6 +39,7 @@ extern const uint8_t *const POAP_SELECTORS[NUM_SELECTORS];
typedef struct context_t {
// For display.
uint8_t beneficiary[ADDRESS_LENGTH];
uint8_t from_address[ADDRESS_LENGTH];
uint8_t token_id[PARAMETER_LENGTH]; // not crypto token dedicated poap token value int number
char ticker[MAX_TICKER_LEN];

Expand Down
Binary file modified tests/elfs/poap_nanos.elf
Binary file not shown.
Binary file modified tests/elfs/poap_nanox.elf
Binary file not shown.
2 changes: 2 additions & 0 deletions tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"bignumber.js": "^9.0.0",
"bip32-path": "^0.4.2",
"core-js": "^3.7.0",
"ethers": "^5.4.7",
"fs-extra": "^10.0.0",
"ethereum-tx-decoder": "^3.0.0",
"google-protobuf": "^3.11.0",
"jest-serial-runner": "^1.1.0",
Expand Down
Loading

0 comments on commit b196707

Please sign in to comment.