Skip to content

Commit

Permalink
feat: migration including v5 methods
Browse files Browse the repository at this point in the history
  • Loading branch information
n4l5u0r committed Oct 19, 2021
1 parent d385d64 commit 7f2bc43
Show file tree
Hide file tree
Showing 327 changed files with 4,495 additions and 574 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ __pycache__/

# JS
tests/node_modules
tests/lib
tests/lib
tests/snapshots-tmp
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,42 @@ The flow processed in [GitHub Actions](https://github.com/features/actions) is t

- Code formatting with [clang-format](http://clang.llvm.org/docs/ClangFormat.html)
- Compilation of the application for Ledger Nano S in [ledger-app-builder](https://github.com/LedgerHQ/ledger-app-builder)


## RDP tests execution

You can test the plugin using a remote development host, if you deal with an unsupported CPU architecture (Ex.: Apple M1). Your server must have a GUI Desktop installed.

1. Install first xrdp on remote linux VM (Ex: Ubuntu 20.04.1)
```
sudo apt install xrdp
````
2. Set access control to none :
```
xhost +
```
> ```access control disabled, clients can connect from any host```
3. Connect to the VM using Remote Desktop Client using port forwarding through ssh connection on port 3389. This will keep the security at maximum and avoid exposing the VM to the web on RDP port.
```
ssh -i PRIVATEKEY USERNAME@PUBLICIP -L 3389:localhost:3389
```
4. Identify the Display index:
```
echo $DISPLAY
```
>```:10.0```
5. In the terminal where are executed the tests set Display to the RDP previous value, here ``:10.0``:
```
export DISPLAY=:10.0
```
6. After this setup you could run ``yarn test`` and see the emulator in the RDP display going through the test sequence.
50 changes: 50 additions & 0 deletions src/handle_finalize.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "paraswap_plugin.h"

void handle_finalize(void *parameters) {
ethPluginFinalize_t *msg = (ethPluginFinalize_t *) parameters;
paraswap_parameters_t *context = (paraswap_parameters_t *) msg->pluginContext;
if (context->valid) {
msg->numScreens = 2;
if ((context->selectorIndex == SIMPLE_SWAP || context->selectorIndex == SIMPLE_BUY ||
context->selectorIndex == SIMPLE_SWAP_V4) &&
(strncmp(context->beneficiary,
(const unsigned char *) NULL_ETH_ADDRESS,
ADDRESS_LENGTH) != 0)) {
// An addiitonal screen is required to display the `beneficiary` field.
msg->numScreens += 1;
}
if (!ADDRESS_IS_ETH(context->contract_address_sent)) {
// Address is not ETH so we will need to look up the token in the CAL.
msg->tokenLookup1 = context->contract_address_sent;
PRINTF("Setting address sent to: %.*H\n",
ADDRESS_LENGTH,
context->contract_address_sent);

// The user is not swapping ETH, so make sure there's no ETH being sent in this tx.
if (!allzeroes(msg->pluginSharedRO->txContent->value.value,
msg->pluginSharedRO->txContent->value.length)) {
PRINTF("ETH attached to tx when token being swapped is %.*H\n",
sizeof(context->contract_address_sent),
context->contract_address_sent);
msg->result = ETH_PLUGIN_RESULT_ERROR;
}
} else {
msg->tokenLookup1 = NULL;
}
if (!ADDRESS_IS_ETH(context->contract_address_received)) {
// Address is not ETH so we will need to look up the token in the CAL.
PRINTF("Setting address receiving to: %.*H\n",
ADDRESS_LENGTH,
context->contract_address_received);
msg->tokenLookup2 = context->contract_address_received;
} else {
msg->tokenLookup2 = NULL;
}

msg->uiType = ETH_UI_TYPE_GENERIC;
msg->result = ETH_PLUGIN_RESULT_OK;
} else {
PRINTF("Context not valid\n");
msg->result = ETH_PLUGIN_RESULT_FALLBACK;
}
}
75 changes: 75 additions & 0 deletions src/handle_init_contract.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include "paraswap_plugin.h"

// Called once to init.
void handle_init_contract(void *parameters) {
ethPluginInitContract_t *msg = (ethPluginInitContract_t *) parameters;

if (msg->interfaceVersion != ETH_PLUGIN_INTERFACE_VERSION_LATEST) {
PRINTF("Wrong interface version: expected %d got %d\n",
ETH_PLUGIN_INTERFACE_VERSION_LATEST,
msg->interfaceVersion);
msg->result = ETH_PLUGIN_RESULT_UNAVAILABLE;
return;
}

if (msg->pluginContextLength < sizeof(paraswap_parameters_t)) {
PRINTF("Paraswap context size too big: expected %d got %d\n",
sizeof(paraswap_parameters_t),
msg->pluginContextLength);
msg->result = ETH_PLUGIN_RESULT_ERROR;
return;
}

paraswap_parameters_t *context = (paraswap_parameters_t *) msg->pluginContext;
memset(context, 0, sizeof(*context));
context->valid = 1;

for (uint8_t i = 0; i < NUM_PARASWAP_SELECTORS; i++) {
if (memcmp((uint8_t *) PIC(PARASWAP_SELECTORS[i]), msg->selector, SELECTOR_SIZE) == 0) {
context->selectorIndex = i;
break;
}
}

// Set `next_param` to be the first field we expect to parse.
switch (context->selectorIndex) {
case BUY_ON_UNI_FORK:
case SWAP_ON_UNI_FORK:
case BUY_ON_UNI:
case SWAP_ON_UNI:
case SWAP_ON_UNI_V4:
case SWAP_ON_UNI_FORK_V4:
case BUY_ON_UNI_V4:
case BUY_ON_UNI_FORK_V4:
if (context->selectorIndex == SWAP_ON_UNI_FORK ||
context->selectorIndex == BUY_ON_UNI_FORK ||
context->selectorIndex == SWAP_ON_UNI_FORK_V4 ||
context->selectorIndex == BUY_ON_UNI_FORK_V4) {
context->skip =
2; // Skip the first two parameters (factory and initCode) for uni forks.
}
context->next_param = AMOUNT_SENT;
break;
case SWAP_ON_ZERO_V4:
case SWAP_ON_ZERO_V2:
context->next_param = TOKEN_SENT;
break;
case MEGA_SWAP:
case BUY:
case MULTI_SWAP:
case SIMPLE_BUY:
case SIMPLE_SWAP:
case SIMPLE_SWAP_V4:
case MULTI_SWAP_V4:
case MEGA_SWAP_V4:
context->next_param = TOKEN_SENT;
if (context->selectorIndex != SIMPLE_SWAP_V4)
context->skip = 1; // Skipping 0x20 (offset of structure)
break;
default:
PRINTF("Missing selectorIndex\n");
msg->result = ETH_PLUGIN_RESULT_ERROR;
return;
}
msg->result = ETH_PLUGIN_RESULT_OK;
}
93 changes: 70 additions & 23 deletions src/provide_parameters.c → src/handle_provide_parameter.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

// Store the amount sent in the form of a string, without any ticker or decimals. These will be
// added when displaying.
static void handle_amount_sent(ethPluginProvideParameter_t *msg, paraswap_parameters_t *context) {
static void handle_amount_sent(const ethPluginProvideParameter_t *msg,
paraswap_parameters_t *context) {
memset(context->amount_sent, 0, sizeof(context->amount_sent));

// Convert to string.
Expand All @@ -17,7 +18,7 @@ static void handle_amount_sent(ethPluginProvideParameter_t *msg, paraswap_parame

// Store the amount received in the form of a string, without any ticker or decimals. These will be
// added when displaying.
static void handle_amount_received(ethPluginProvideParameter_t *msg,
static void handle_amount_received(const ethPluginProvideParameter_t *msg,
paraswap_parameters_t *context) {
memset(context->amount_received, 0, sizeof(context->amount_received));

Expand All @@ -31,34 +32,37 @@ static void handle_amount_received(ethPluginProvideParameter_t *msg,
PRINTF("AMOUNT RECEIVED: %s\n", context->amount_received);
}

static void handle_beneficiary(ethPluginProvideParameter_t *msg, paraswap_parameters_t *context) {
static void handle_beneficiary(const ethPluginProvideParameter_t *msg,
paraswap_parameters_t *context) {
memset(context->beneficiary, 0, sizeof(context->beneficiary));
memcpy(context->beneficiary,
&msg->parameter[PARAMETER_LENGTH - ADDRESS_LENGTH],
sizeof(context->beneficiary));
PRINTF("BENEFICIARY: %.*H\n", ADDRESS_LENGTH, context->beneficiary);
}

static void handle_array_len(ethPluginProvideParameter_t *msg, paraswap_parameters_t *context) {
static void handle_array_len(const ethPluginProvideParameter_t *msg,
paraswap_parameters_t *context) {
context->array_len = msg->parameter[PARAMETER_LENGTH - 1];
PRINTF("LIST LEN: %d\n", context->array_len);
}

static void handle_token_sent(ethPluginProvideParameter_t *msg, paraswap_parameters_t *context) {
static void handle_token_sent(const ethPluginProvideParameter_t *msg,
paraswap_parameters_t *context) {
memset(context->contract_address_sent, 0, sizeof(context->contract_address_sent));
memcpy(context->contract_address_sent,
&msg->parameter[PARAMETER_LENGTH - ADDRESS_LENGTH],
sizeof(context->contract_address_sent));
PRINTF("TOKEN SENT: %.*H\n", ADDRESS_LENGTH, context->contract_address_sent);
}

static void handle_token_received(ethPluginProvideParameter_t *msg,
static void handle_token_received(const ethPluginProvideParameter_t *msg,
paraswap_parameters_t *context) {
memset(context->contract_address_received, 0, sizeof(context->contract_address_received));
memcpy(context->contract_address_received,
&msg->parameter[PARAMETER_LENGTH - ADDRESS_LENGTH],
sizeof(context->contract_address_received));
PRINTF("TOKEN RECIEVED: %.*H\n", ADDRESS_LENGTH, context->contract_address_received);
PRINTF("TOKEN RECEIVED: %.*H\n", ADDRESS_LENGTH, context->contract_address_received);
}

static void handle_uniswap_and_forks(ethPluginProvideParameter_t *msg,
Expand All @@ -69,7 +73,9 @@ static void handle_uniswap_and_forks(ethPluginProvideParameter_t *msg,
context->next_param = AMOUNT_RECEIVED;
context->checkpoint = msg->parameterOffset;
if (context->selectorIndex == BUY_ON_UNI_FORK ||
context->selectorIndex == SWAP_ON_UNI_FORK) {
context->selectorIndex == SWAP_ON_UNI_FORK ||
context->selectorIndex == SWAP_ON_UNI_FORK_V4 ||
context->selectorIndex == BUY_ON_UNI_FORK_V4) {
// Substract two chunks because we've skipped the first two parameters.
// No underflow possible because we've skipped the first two chunks, so
// msg->parameterOffset >= 2 * PARAMETER_LENGTH.
Expand All @@ -81,7 +87,7 @@ static void handle_uniswap_and_forks(ethPluginProvideParameter_t *msg,
context->next_param = PATHS_OFFSET;
break;
case PATHS_OFFSET:
context->offset = U2BE(msg->parameter, PARAMETER_LENGTH - 2);
context->offset = U2BE(msg->parameter, PARAMETER_LENGTH - sizeof(context->offset));
context->next_param = PATH;
break;
case PATH: // len(path)
Expand Down Expand Up @@ -125,7 +131,7 @@ static void handle_simple_calls(ethPluginProvideParameter_t *msg, paraswap_param
handle_amount_received(msg, context);
context->next_param = BENEFICIARY;
context->skip = 4; // callees, exchangeData, startIndexes, values.
if (context->selectorIndex == SIMPLE_SWAP) {
if (context->selectorIndex == SIMPLE_SWAP || context->selectorIndex == SIMPLE_SWAP_V4) {
context->skip++; // skip field expectedAmount for simple swap.
}
break;
Expand Down Expand Up @@ -161,7 +167,9 @@ static void handle_multiswap(ethPluginProvideParameter_t *msg, paraswap_paramete
case BENEFICIARY: // beneficiary
handle_beneficiary(msg, context);
context->next_param = PATHS_OFFSET;
context->skip = 2; // Skip referrer and useReduxtoken
if (context->selectorIndex == MULTI_SWAP_V4) {
context->skip = 2; // Skip referrer and useReduxtoken
}
break;
case PATHS_OFFSET:
context->offset = U2BE(msg->parameter, PARAMETER_LENGTH - 2);
Expand Down Expand Up @@ -222,6 +230,37 @@ static void handle_buy(ethPluginProvideParameter_t *msg, paraswap_parameters_t *
}
}

static void handle_swap_on_zero(ethPluginProvideParameter_t *msg, paraswap_parameters_t *context) {
switch (context->next_param) {
case TOKEN_SENT: // fromToken
handle_token_sent(msg, context);
context->next_param = TOKEN_RECEIVED;
break;
case TOKEN_RECEIVED: // toToken
handle_token_received(msg, context);
context->next_param = AMOUNT_SENT;
break;
case AMOUNT_SENT: // fromAmount
handle_amount_sent(msg, context);
context->next_param = AMOUNT_RECEIVED;
break;
case AMOUNT_RECEIVED: // toAmount
handle_amount_received(msg, context);
context->next_param = NONE;
break;
case BENEFICIARY: // beneficiary
handle_beneficiary(msg, context);
context->next_param = NONE;
break;
case NONE:
break;
default:
PRINTF("Param not supported\n");
msg->result = ETH_PLUGIN_RESULT_ERROR;
break;
}
}

static void handle_megaswap(ethPluginProvideParameter_t *msg, paraswap_parameters_t *context) {
switch (context->next_param) {
case TOKEN_SENT: // fromToken
Expand All @@ -241,7 +280,9 @@ static void handle_megaswap(ethPluginProvideParameter_t *msg, paraswap_parameter
case BENEFICIARY:
handle_beneficiary(msg, context);
context->next_param = MEGA_PATHS_OFFSET;
context->skip = 2; // Skip referrer and useReduxToken.
if (context->selectorIndex == MEGA_SWAP_V4) {
context->skip = 2; // Skip referrer and useReduxToken.
}
break;
case MEGA_PATHS_OFFSET:
context->offset = U2BE(msg->parameter, PARAMETER_LENGTH - 2);
Expand Down Expand Up @@ -303,37 +344,43 @@ void handle_provide_parameter(void *parameters) {
msg->parameterOffset);
return;
}

context->offset = 0; // Reset offset
switch (context->selectorIndex) {
case BUY_ON_UNI_FORK:
case SWAP_ON_UNI_FORK:
case BUY_ON_UNI:
case SWAP_ON_UNI: {
case SWAP_ON_UNI:
case SWAP_ON_UNI_V4:
case SWAP_ON_UNI_FORK_V4:
case BUY_ON_UNI_V4:
case BUY_ON_UNI_FORK_V4:
handle_uniswap_and_forks(msg, context);
break;
}

case SIMPLE_BUY:
case SIMPLE_SWAP: {
case SIMPLE_SWAP:
case SIMPLE_SWAP_V4:
handle_simple_calls(msg, context);
break;
}

case MULTI_SWAP: {
case MULTI_SWAP:
case MULTI_SWAP_V4:
handle_multiswap(msg, context);
break;
}

case BUY: {
case BUY:
handle_buy(msg, context);
break;
}

case MEGA_SWAP: {
case MEGA_SWAP:
case MEGA_SWAP_V4:
handle_megaswap(msg, context);
break;
}

case SWAP_ON_ZERO_V2:
case SWAP_ON_ZERO_V4:
handle_swap_on_zero(msg, context);
break;

default:
PRINTF("Selector Index %d not supported\n", context->selectorIndex);
Expand Down
Loading

0 comments on commit 7f2bc43

Please sign in to comment.