Skip to content

Commit

Permalink
Merge pull request #1 from adrienlacombe-ledger/develop
Browse files Browse the repository at this point in the history
clang format and fix tests
  • Loading branch information
shahthepro authored Jan 3, 2024
2 parents d3552b4 + 2d6b8a5 commit 106da3b
Show file tree
Hide file tree
Showing 9 changed files with 1,124 additions and 1,166 deletions.
77 changes: 38 additions & 39 deletions src/handle_finalize.c
Original file line number Diff line number Diff line change
@@ -1,50 +1,49 @@
#include "plugin.h"

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

msg->uiType = ETH_UI_TYPE_GENERIC;
msg->uiType = ETH_UI_TYPE_GENERIC;

// Number of screens needed
msg->numScreens = 2;
// Number of screens needed
msg->numScreens = 2;

bool sender_is_beneficiary =
memcmp(msg->address, context->beneficiary, ADDRESS_LENGTH) == 0;
bool sender_is_beneficiary = memcmp(msg->address, context->beneficiary, ADDRESS_LENGTH) == 0;

if (sender_is_beneficiary) {
// No need to show beneficary screen when signer is same as `beneficiary`
bool wrap_tx =
(context->selectorIndex == WRAP || context->selectorIndex == UNWRAP);
bool uniswap_tx =
(context->selectorIndex == UNISWAP_V3_ROUTER_EXACT_INPUT ||
context->selectorIndex == UNISWAP_ROUTER_EXACT_INPUT_SINGLE);
if (sender_is_beneficiary) {
// No need to show beneficary screen when signer is same as `beneficiary`
bool wrap_tx = (context->selectorIndex == WRAP || context->selectorIndex == UNWRAP);
bool uniswap_tx = (context->selectorIndex == UNISWAP_V3_ROUTER_EXACT_INPUT ||
context->selectorIndex == UNISWAP_ROUTER_EXACT_INPUT_SINGLE);

if (wrap_tx) {
msg->numScreens -= 1;
} else if (uniswap_tx) {
msg->numScreens += 1;
if (wrap_tx) {
msg->numScreens -= 1;
} else if (uniswap_tx) {
msg->numScreens += 1;
}
}
if (!ADDRESS_IS_NETWORK_TOKEN(context->contract_address_sent)) {
// Address is not network token (0xeee...) so we will need to look up the
// token in the CAL.
printf_hex_array("Setting address sent to: ",
ADDRESS_LENGTH,
context->contract_address_sent);
msg->tokenLookup1 = context->contract_address_sent;
} else {
sent_network_token(context);
msg->tokenLookup1 = NULL;
}
if (!ADDRESS_IS_NETWORK_TOKEN(context->contract_address_received)) {
// Address is not network token (0xeee...) so we will need to look up the
// token in the CAL.
printf_hex_array("Setting address received to: ",
ADDRESS_LENGTH,
context->contract_address_received);
msg->tokenLookup2 = context->contract_address_received;
} else {
received_network_token(context);
msg->tokenLookup2 = NULL;
}
}
if (!ADDRESS_IS_NETWORK_TOKEN(context->contract_address_sent)) {
// Address is not network token (0xeee...) so we will need to look up the
// token in the CAL.
printf_hex_array("Setting address sent to: ", ADDRESS_LENGTH,
context->contract_address_sent);
msg->tokenLookup1 = context->contract_address_sent;
} else {
sent_network_token(context);
msg->tokenLookup1 = NULL;
}
if (!ADDRESS_IS_NETWORK_TOKEN(context->contract_address_received)) {
// Address is not network token (0xeee...) so we will need to look up the
// token in the CAL.
printf_hex_array("Setting address received to: ", ADDRESS_LENGTH,
context->contract_address_received);
msg->tokenLookup2 = context->contract_address_received;
} else {
received_network_token(context);
msg->tokenLookup2 = NULL;
}

msg->result = ETH_PLUGIN_RESULT_OK;
msg->result = ETH_PLUGIN_RESULT_OK;
}
157 changes: 78 additions & 79 deletions src/handle_init_contract.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,89 +3,88 @@

// Called once to init.
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.
msg->result = ETH_PLUGIN_RESULT_UNAVAILABLE;
return;
}
// Make sure we are running a compatible version.
if (msg->interfaceVersion != ETH_PLUGIN_INTERFACE_VERSION_LATEST) {
// If not the case, return the `UNAVAILABLE` status.
msg->result = ETH_PLUGIN_RESULT_UNAVAILABLE;
return;
}

// Double check that the `context_t` struct is not bigger than the maximum
// size (defined by `msg->pluginContextLength`).
if (msg->pluginContextLength < sizeof(context_t)) {
PRINTF("Plugin parameters structure is bigger than allowed size\n");
msg->result = ETH_PLUGIN_RESULT_ERROR;
return;
}
// Double check that the `context_t` struct is not bigger than the maximum
// size (defined by `msg->pluginContextLength`).
if (msg->pluginContextLength < sizeof(context_t)) {
PRINTF("Plugin parameters structure is bigger than allowed size\n");
msg->result = ETH_PLUGIN_RESULT_ERROR;
return;
}

context_t *context = (context_t *)msg->pluginContext;
context_t *context = (context_t *) msg->pluginContext;

// Initialize the context (to 0).
memset(context, 0, sizeof(*context));
// Initialize the context (to 0).
memset(context, 0, sizeof(*context));

size_t 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;
}
context->selectorIndex = index;
// check for overflow
if ((size_t)context->selectorIndex != index) {
PRINTF("Error: overflow detected on selector index!\n");
msg->result = ETH_PLUGIN_RESULT_ERROR;
return;
}
size_t 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;
}
context->selectorIndex = index;
// check for overflow
if ((size_t) context->selectorIndex != index) {
PRINTF("Error: overflow detected on selector index!\n");
msg->result = ETH_PLUGIN_RESULT_ERROR;
return;
}

// Set `next_param` to be the first field we expect to parse.
switch (context->selectorIndex) {
case ZAPPER_DEPOSIT_ETH:
context->next_param = NONE;
break;
case CURVE_POOL_EXCHANGE:
case CURVE_POOL_EXCHANGE_UNDERLYING:
if (memcmp(CURVE_OETH_POOL_ADDRESS,
msg->pluginSharedRO->txContent->destination,
ADDRESS_LENGTH) == 0 ||
memcmp(CURVE_OUSD_POOL_ADDRESS,
msg->pluginSharedRO->txContent->destination,
ADDRESS_LENGTH) == 0) {
context->next_param = TOKEN_SENT;
break;
}
PRINTF("Missing selectorIndex: %d\n", context->selectorIndex);
msg->result = ETH_PLUGIN_RESULT_ERROR;
return;
case UNISWAP_V3_ROUTER_EXACT_INPUT:
context->skip += 2;
context->next_param = BENEFICIARY;
break;
case UNISWAP_ROUTER_EXACT_INPUT_SINGLE:
break;
case CURVE_ROUTER_EXCHANGE_MULTIPLE:
case VAULT_MINT:
context->next_param = TOKEN_SENT;
break;
case FLIPPER_BUY_OUSD_WITH_USDT:
case FLIPPER_SELL_OUSD_FOR_USDT:
case FLIPPER_BUY_OUSD_WITH_DAI:
case FLIPPER_SELL_OUSD_FOR_DAI:
case FLIPPER_BUY_OUSD_WITH_USDC:
case FLIPPER_SELL_OUSD_FOR_USDC:
case ZAPPER_DEPOSIT_SFRXETH:
case VAULT_REDEEM:
case WRAP:
case UNWRAP:
context->next_param = AMOUNT_SENT;
break;
// Keep this
default:
PRINTF("Missing selectorIndex: %d\n", context->selectorIndex);
msg->result = ETH_PLUGIN_RESULT_ERROR;
return;
}
// Set `next_param` to be the first field we expect to parse.
switch (context->selectorIndex) {
case ZAPPER_DEPOSIT_ETH:
context->next_param = NONE;
break;
case CURVE_POOL_EXCHANGE:
case CURVE_POOL_EXCHANGE_UNDERLYING:
if (memcmp(CURVE_OETH_POOL_ADDRESS,
msg->pluginSharedRO->txContent->destination,
ADDRESS_LENGTH) == 0 ||
memcmp(CURVE_OUSD_POOL_ADDRESS,
msg->pluginSharedRO->txContent->destination,
ADDRESS_LENGTH) == 0) {
context->next_param = TOKEN_SENT;
break;
}
PRINTF("Missing selectorIndex: %d\n", context->selectorIndex);
msg->result = ETH_PLUGIN_RESULT_ERROR;
return;
case UNISWAP_V3_ROUTER_EXACT_INPUT:
context->skip += 2;
context->next_param = BENEFICIARY;
break;
case UNISWAP_ROUTER_EXACT_INPUT_SINGLE:
break;
case CURVE_ROUTER_EXCHANGE_MULTIPLE:
case VAULT_MINT:
context->next_param = TOKEN_SENT;
break;
case FLIPPER_BUY_OUSD_WITH_USDT:
case FLIPPER_SELL_OUSD_FOR_USDT:
case FLIPPER_BUY_OUSD_WITH_DAI:
case FLIPPER_SELL_OUSD_FOR_DAI:
case FLIPPER_BUY_OUSD_WITH_USDC:
case FLIPPER_SELL_OUSD_FOR_USDC:
case ZAPPER_DEPOSIT_SFRXETH:
case VAULT_REDEEM:
case WRAP:
case UNWRAP:
context->next_param = AMOUNT_SENT;
break;
// Keep this
default:
PRINTF("Missing selectorIndex: %d\n", context->selectorIndex);
msg->result = ETH_PLUGIN_RESULT_ERROR;
return;
}

// Return valid status.
msg->result = ETH_PLUGIN_RESULT_OK;
// Return valid status.
msg->result = ETH_PLUGIN_RESULT_OK;
}
Loading

0 comments on commit 106da3b

Please sign in to comment.