Apply clang-format
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
#include <string.h>
|
||||
#include "eth_plugin_interface.h"
|
||||
#include "shared_context.h" // TODO : rewrite as independant code
|
||||
#include "eth_plugin_internal.h" // TODO : rewrite as independant code
|
||||
#include "shared_context.h" // TODO : rewrite as independant code
|
||||
#include "eth_plugin_internal.h" // TODO : rewrite as independant code
|
||||
#include "utils.h"
|
||||
|
||||
typedef enum {
|
||||
@@ -39,11 +39,9 @@ typedef struct underlying_asset_decimals_t {
|
||||
uint8_t decimals;
|
||||
} underlying_asset_decimals_t;
|
||||
|
||||
|
||||
|
||||
/* Sadly, we don't have the information about the underlying asset's decimals, which can differ from the cToken decimals.
|
||||
Therefore, we hardcode a binding table. If Compound adds a lot of token in the future, we will have to move to a CAL
|
||||
based architecture instead, as this one doesn't scale well.*/
|
||||
/* Sadly, we don't have the information about the underlying asset's decimals, which can differ from
|
||||
the cToken decimals. Therefore, we hardcode a binding table. If Compound adds a lot of token in the
|
||||
future, we will have to move to a CAL based architecture instead, as this one doesn't scale well.*/
|
||||
#define NUM_COMPOUND_BINDINGS 9
|
||||
const underlying_asset_decimals_t const UNDERLYING_ASSET_DECIMALS[NUM_COMPOUND_BINDINGS] = {
|
||||
{"cDAI", 18},
|
||||
@@ -57,10 +55,13 @@ const underlying_asset_decimals_t const UNDERLYING_ASSET_DECIMALS[NUM_COMPOUND_B
|
||||
{"cSAI", 18},
|
||||
};
|
||||
|
||||
bool get_underlying_asset_decimals(char* compound_ticker, uint8_t* out_decimals){
|
||||
for(size_t i = 0; i < NUM_COMPOUND_BINDINGS; i++){
|
||||
underlying_asset_decimals_t* binding = (underlying_asset_decimals_t *)PIC(&UNDERLYING_ASSET_DECIMALS[i]);
|
||||
if (strncmp(binding->c_ticker, compound_ticker, strnlen(binding->c_ticker, MAX_TICKER_LEN)) == 0){
|
||||
bool get_underlying_asset_decimals(char *compound_ticker, uint8_t *out_decimals) {
|
||||
for (size_t i = 0; i < NUM_COMPOUND_BINDINGS; i++) {
|
||||
underlying_asset_decimals_t *binding =
|
||||
(underlying_asset_decimals_t *) PIC(&UNDERLYING_ASSET_DECIMALS[i]);
|
||||
if (strncmp(binding->c_ticker,
|
||||
compound_ticker,
|
||||
strnlen(binding->c_ticker, MAX_TICKER_LEN)) == 0) {
|
||||
*out_decimals = binding->decimals;
|
||||
return true;
|
||||
}
|
||||
@@ -69,21 +70,21 @@ bool get_underlying_asset_decimals(char* compound_ticker, uint8_t* out_decimals)
|
||||
}
|
||||
|
||||
void compound_plugin_call(int message, void *parameters) {
|
||||
|
||||
switch(message) {
|
||||
switch (message) {
|
||||
case ETH_PLUGIN_INIT_CONTRACT: {
|
||||
ethPluginInitContract_t *msg = (ethPluginInitContract_t*)parameters;
|
||||
compound_parameters_t *context = (compound_parameters_t*)msg->pluginContext;
|
||||
ethPluginInitContract_t *msg = (ethPluginInitContract_t *) parameters;
|
||||
compound_parameters_t *context = (compound_parameters_t *) msg->pluginContext;
|
||||
size_t i;
|
||||
for (i=0; i<NUM_COMPOUND_SELECTORS; i++) {
|
||||
if (memcmp((uint8_t *)PIC(COMPOUND_SELECTORS[i]), msg->selector, SELECTOR_SIZE) == 0) {
|
||||
for (i = 0; i < NUM_COMPOUND_SELECTORS; i++) {
|
||||
if (memcmp((uint8_t *) PIC(COMPOUND_SELECTORS[i]), msg->selector, SELECTOR_SIZE) ==
|
||||
0) {
|
||||
context->selectorIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// enforce that ETH amount should be 0, except in ceth.mint case
|
||||
if (!allzeroes(msg->pluginSharedRO->txContent->value.value, 32)){
|
||||
if(context->selectorIndex != CETH_MINT){
|
||||
if (!allzeroes(msg->pluginSharedRO->txContent->value.value, 32)) {
|
||||
if (context->selectorIndex != CETH_MINT) {
|
||||
msg->result = ETH_PLUGIN_RESULT_ERROR;
|
||||
break;
|
||||
}
|
||||
@@ -94,27 +95,35 @@ void compound_plugin_call(int message, void *parameters) {
|
||||
break;
|
||||
}
|
||||
if (msg->dataSize != COMPOUND_EXPECTED_DATA_SIZE[context->selectorIndex]) {
|
||||
PRINTF("Unexpected data size for command %d expected %d got %d\n", context->selectorIndex,
|
||||
COMPOUND_EXPECTED_DATA_SIZE[context->selectorIndex], msg->dataSize);
|
||||
PRINTF("Unexpected data size for command %d expected %d got %d\n",
|
||||
context->selectorIndex,
|
||||
COMPOUND_EXPECTED_DATA_SIZE[context->selectorIndex],
|
||||
msg->dataSize);
|
||||
msg->result = ETH_PLUGIN_RESULT_ERROR;
|
||||
break;
|
||||
}
|
||||
if(context->selectorIndex == CETH_MINT){
|
||||
// ETH amount 0x1234 is stored 0x12340000...000 instead of 0x00....001234, so we strip the following zeroes when copying
|
||||
if (context->selectorIndex == CETH_MINT) {
|
||||
// ETH amount 0x1234 is stored 0x12340000...000 instead of 0x00....001234, so we
|
||||
// strip the following zeroes when copying
|
||||
memset(context->amount, 0, sizeof(context->amount));
|
||||
memmove(context->amount + sizeof(context->amount) - msg->pluginSharedRO->txContent->value.length, msg->pluginSharedRO->txContent->value.value, msg->pluginSharedRO->txContent->value.length);
|
||||
memmove(context->amount + sizeof(context->amount) -
|
||||
msg->pluginSharedRO->txContent->value.length,
|
||||
msg->pluginSharedRO->txContent->value.value,
|
||||
msg->pluginSharedRO->txContent->value.length);
|
||||
}
|
||||
PRINTF("compound plugin inititialized\n");
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
}
|
||||
break;
|
||||
} break;
|
||||
|
||||
case ETH_PLUGIN_PROVIDE_PARAMETER : {
|
||||
ethPluginProvideParameter_t *msg = (ethPluginProvideParameter_t*)parameters;
|
||||
compound_parameters_t *context = (compound_parameters_t*)msg->pluginContext;
|
||||
PRINTF("compound plugin provide parameter %d %.*H\n", msg->parameterOffset, 32, msg->parameter);
|
||||
if (context->selectorIndex != CETH_MINT){
|
||||
switch(msg->parameterOffset) {
|
||||
case ETH_PLUGIN_PROVIDE_PARAMETER: {
|
||||
ethPluginProvideParameter_t *msg = (ethPluginProvideParameter_t *) parameters;
|
||||
compound_parameters_t *context = (compound_parameters_t *) msg->pluginContext;
|
||||
PRINTF("compound plugin provide parameter %d %.*H\n",
|
||||
msg->parameterOffset,
|
||||
32,
|
||||
msg->parameter);
|
||||
if (context->selectorIndex != CETH_MINT) {
|
||||
switch (msg->parameterOffset) {
|
||||
case 4:
|
||||
memmove(context->amount, msg->parameter, 32);
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
@@ -124,36 +133,35 @@ void compound_plugin_call(int message, void *parameters) {
|
||||
msg->result = ETH_PLUGIN_RESULT_ERROR;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
PRINTF("CETH contract expects no parameters\n");
|
||||
msg->result = ETH_PLUGIN_RESULT_ERROR;
|
||||
}
|
||||
}
|
||||
break;
|
||||
} break;
|
||||
|
||||
case ETH_PLUGIN_FINALIZE: {
|
||||
ethPluginFinalize_t *msg = (ethPluginFinalize_t*)parameters;
|
||||
ethPluginFinalize_t *msg = (ethPluginFinalize_t *) parameters;
|
||||
PRINTF("compound plugin finalize\n");
|
||||
msg->tokenLookup1 = msg->pluginSharedRO->txContent->destination;
|
||||
msg->numScreens = 2;
|
||||
msg->uiType = ETH_UI_TYPE_GENERIC;
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
}
|
||||
break;
|
||||
} break;
|
||||
|
||||
case ETH_PLUGIN_PROVIDE_TOKEN: {
|
||||
ethPluginProvideToken_t *msg = (ethPluginProvideToken_t*)parameters;
|
||||
compound_parameters_t *context = (compound_parameters_t*)msg->pluginContext;
|
||||
ethPluginProvideToken_t *msg = (ethPluginProvideToken_t *) parameters;
|
||||
compound_parameters_t *context = (compound_parameters_t *) msg->pluginContext;
|
||||
PRINTF("compound plugin provide token: %d\n", (msg->token1 != NULL));
|
||||
if (msg->token1 != NULL) {
|
||||
strcpy((char *)context->ticker_1, (char *)msg->token1->ticker);
|
||||
switch (context->selectorIndex)
|
||||
{
|
||||
strcpy((char *) context->ticker_1, (char *) msg->token1->ticker);
|
||||
switch (context->selectorIndex) {
|
||||
case COMPOUND_REDEEM_UNDERLYING:
|
||||
case COMPOUND_MINT:
|
||||
case CETH_MINT:
|
||||
msg->result = get_underlying_asset_decimals(context->ticker_1, &context->decimals) ? ETH_PLUGIN_RESULT_OK : ETH_PLUGIN_RESULT_FALLBACK;
|
||||
msg->result =
|
||||
get_underlying_asset_decimals(context->ticker_1, &context->decimals)
|
||||
? ETH_PLUGIN_RESULT_OK
|
||||
: ETH_PLUGIN_RESULT_FALLBACK;
|
||||
break;
|
||||
|
||||
// Only case where we use the compound contract decimals
|
||||
@@ -166,19 +174,16 @@ void compound_plugin_call(int message, void *parameters) {
|
||||
msg->result = ETH_PLUGIN_RESULT_FALLBACK;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
msg->result = ETH_PLUGIN_RESULT_FALLBACK;
|
||||
}
|
||||
}
|
||||
break;
|
||||
} break;
|
||||
|
||||
case ETH_PLUGIN_QUERY_CONTRACT_ID: {
|
||||
ethQueryContractID_t *msg = (ethQueryContractID_t*)parameters;
|
||||
compound_parameters_t *context = (compound_parameters_t*)msg->pluginContext;
|
||||
ethQueryContractID_t *msg = (ethQueryContractID_t *) parameters;
|
||||
compound_parameters_t *context = (compound_parameters_t *) msg->pluginContext;
|
||||
strcpy(msg->name, "Type");
|
||||
switch (context->selectorIndex)
|
||||
{
|
||||
switch (context->selectorIndex) {
|
||||
case COMPOUND_REDEEM_UNDERLYING:
|
||||
case COMPOUND_REDEEM:
|
||||
strcpy(msg->version, "Redeem");
|
||||
@@ -194,37 +199,41 @@ void compound_plugin_call(int message, void *parameters) {
|
||||
}
|
||||
strcat(msg->version, " Assets");
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
}
|
||||
break;
|
||||
} break;
|
||||
|
||||
case ETH_PLUGIN_QUERY_CONTRACT_UI: {
|
||||
ethQueryContractUI_t *msg = (ethQueryContractUI_t*)parameters;
|
||||
compound_parameters_t *context = (compound_parameters_t*)msg->pluginContext;
|
||||
switch(msg->screenIndex) {
|
||||
ethQueryContractUI_t *msg = (ethQueryContractUI_t *) parameters;
|
||||
compound_parameters_t *context = (compound_parameters_t *) msg->pluginContext;
|
||||
switch (msg->screenIndex) {
|
||||
case 0: {
|
||||
strcpy(msg->title, "Amount");
|
||||
char * ticker_ptr = (char *)context->ticker_1;
|
||||
char *ticker_ptr = (char *) context->ticker_1;
|
||||
/* skip "c" in front of cToken unless we use "redeem", as
|
||||
redeem is the only operation dealing with a cToken amount */
|
||||
if(context->selectorIndex != COMPOUND_REDEEM){
|
||||
if (context->selectorIndex != COMPOUND_REDEEM) {
|
||||
ticker_ptr++;
|
||||
}
|
||||
amountToString(context->amount, sizeof(context->amount), context->decimals, ticker_ptr, msg->msg, 100);
|
||||
amountToString(context->amount,
|
||||
sizeof(context->amount),
|
||||
context->decimals,
|
||||
ticker_ptr,
|
||||
msg->msg,
|
||||
100);
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
}
|
||||
break;
|
||||
} break;
|
||||
|
||||
case 1:
|
||||
strcpy(msg->title, "Contract");
|
||||
strcpy(msg->msg, "Compound ");
|
||||
strcat(msg->msg, (char *)context->ticker_1 + 1); // remove the 'c' char at beginning of compound ticker
|
||||
strcat(msg->msg,
|
||||
(char *) context->ticker_1 +
|
||||
1); // remove the 'c' char at beginning of compound ticker
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
} break;
|
||||
|
||||
default:
|
||||
PRINTF("Unhandled message %d\n", message);
|
||||
|
||||
@@ -5,221 +5,223 @@
|
||||
#include "ethUtils.h"
|
||||
#include "utils.h"
|
||||
|
||||
typedef enum {
|
||||
ERC20_TRANSFER = 0,
|
||||
ERC20_APPROVE
|
||||
} erc20Selector_t;
|
||||
typedef enum { ERC20_TRANSFER = 0, ERC20_APPROVE } erc20Selector_t;
|
||||
|
||||
typedef enum {
|
||||
TARGET_ADDRESS = 0,
|
||||
TARGET_CONTRACT,
|
||||
TARGET_COMPOUND
|
||||
} targetType_t;
|
||||
typedef enum { TARGET_ADDRESS = 0, TARGET_CONTRACT, TARGET_COMPOUND } targetType_t;
|
||||
|
||||
typedef struct erc20_parameters_t {
|
||||
uint8_t selectorIndex;
|
||||
uint8_t destinationAddress[21];
|
||||
uint8_t amount[32];
|
||||
uint8_t ticker_1[MAX_TICKER_LEN];
|
||||
uint8_t ticker_2[MAX_TICKER_LEN];
|
||||
uint8_t decimals;
|
||||
uint8_t target;
|
||||
uint8_t selectorIndex;
|
||||
uint8_t destinationAddress[21];
|
||||
uint8_t amount[32];
|
||||
uint8_t ticker_1[MAX_TICKER_LEN];
|
||||
uint8_t ticker_2[MAX_TICKER_LEN];
|
||||
uint8_t decimals;
|
||||
uint8_t target;
|
||||
} erc20_parameters_t;
|
||||
|
||||
typedef struct ticker_binding_t {
|
||||
char ticker1[MAX_TICKER_LEN];
|
||||
char ticker2[MAX_TICKER_LEN];
|
||||
char ticker1[MAX_TICKER_LEN];
|
||||
char ticker2[MAX_TICKER_LEN];
|
||||
} ticker_binding_t;
|
||||
|
||||
#define NUM_COMPOUND_BINDINGS 9
|
||||
const ticker_binding_t const COMPOUND_BINDINGS[NUM_COMPOUND_BINDINGS] = {
|
||||
{"DAI", "CDAI"},
|
||||
{"WETH", "CETH"},
|
||||
{"USDC", "CUSDC"},
|
||||
{"ZRX", "CZRX"},
|
||||
{"USDT", "CUSDT"},
|
||||
{"WBTC", "CBTC"},
|
||||
{"BAT", "CBAT"},
|
||||
{"REPv2", "CREP"},
|
||||
{"SAI", "CSAI"},
|
||||
{"DAI", "CDAI"},
|
||||
{"WETH", "CETH"},
|
||||
{"USDC", "CUSDC"},
|
||||
{"ZRX", "CZRX"},
|
||||
{"USDT", "CUSDT"},
|
||||
{"WBTC", "CBTC"},
|
||||
{"BAT", "CBAT"},
|
||||
{"REPv2", "CREP"},
|
||||
{"SAI", "CSAI"},
|
||||
};
|
||||
|
||||
bool check_token_binding(char* ticker1, char* ticker2, const ticker_binding_t* bindings, size_t num_bindings){
|
||||
for(size_t i = 0; i < num_bindings; i++){
|
||||
ticker_binding_t* binding = (ticker_binding_t *)PIC(&bindings[i]);
|
||||
if (strncmp(binding->ticker1, ticker1, strnlen(binding->ticker1, MAX_TICKER_LEN)) == 0 &&
|
||||
strncmp(binding->ticker2, ticker2, strnlen(binding->ticker2, MAX_TICKER_LEN)) == 0){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
bool check_token_binding(char *ticker1,
|
||||
char *ticker2,
|
||||
const ticker_binding_t *bindings,
|
||||
size_t num_bindings) {
|
||||
for (size_t i = 0; i < num_bindings; i++) {
|
||||
ticker_binding_t *binding = (ticker_binding_t *) PIC(&bindings[i]);
|
||||
if (strncmp(binding->ticker1, ticker1, strnlen(binding->ticker1, MAX_TICKER_LEN)) == 0 &&
|
||||
strncmp(binding->ticker2, ticker2, strnlen(binding->ticker2, MAX_TICKER_LEN)) == 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool erc20_plugin_available_check() {
|
||||
#ifdef HAVE_STARKWARE
|
||||
if (quantumSet) {
|
||||
switch(dataContext.tokenContext.quantumType) {
|
||||
case STARK_QUANTUM_LEGACY:
|
||||
case STARK_QUANTUM_ETH:
|
||||
case STARK_QUANTUM_ERC20:
|
||||
case STARK_QUANTUM_MINTABLE_ERC20:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if (quantumSet) {
|
||||
switch (dataContext.tokenContext.quantumType) {
|
||||
case STARK_QUANTUM_LEGACY:
|
||||
case STARK_QUANTUM_ETH:
|
||||
case STARK_QUANTUM_ERC20:
|
||||
case STARK_QUANTUM_MINTABLE_ERC20:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void erc20_plugin_call(int message, void *parameters) {
|
||||
switch (message) {
|
||||
case ETH_PLUGIN_INIT_CONTRACT: {
|
||||
ethPluginInitContract_t *msg = (ethPluginInitContract_t *) parameters;
|
||||
erc20_parameters_t *context = (erc20_parameters_t *) msg->pluginContext;
|
||||
// enforce that ETH amount should be 0
|
||||
if (!allzeroes(msg->pluginSharedRO->txContent->value.value, 32)) {
|
||||
PRINTF("Err: Transaction amount is not 0\n");
|
||||
msg->result = ETH_PLUGIN_RESULT_ERROR;
|
||||
} else {
|
||||
size_t i;
|
||||
for (i = 0; i < NUM_ERC20_SELECTORS; i++) {
|
||||
if (memcmp((uint8_t *) PIC(ERC20_SELECTORS[i]), msg->selector, SELECTOR_SIZE) ==
|
||||
0) {
|
||||
context->selectorIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i == NUM_ERC20_SELECTORS) {
|
||||
PRINTF("Unknown selector %.*H\n", SELECTOR_SIZE, msg->selector);
|
||||
msg->result = ETH_PLUGIN_RESULT_ERROR;
|
||||
break;
|
||||
}
|
||||
PRINTF("erc20 plugin init\n");
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
}
|
||||
} break;
|
||||
|
||||
switch(message) {
|
||||
case ETH_PLUGIN_INIT_CONTRACT: {
|
||||
ethPluginInitContract_t *msg = (ethPluginInitContract_t*)parameters;
|
||||
erc20_parameters_t *context = (erc20_parameters_t*)msg->pluginContext;
|
||||
// enforce that ETH amount should be 0
|
||||
if (!allzeroes(msg->pluginSharedRO->txContent->value.value, 32)){
|
||||
PRINTF("Err: Transaction amount is not 0\n");
|
||||
msg->result = ETH_PLUGIN_RESULT_ERROR;
|
||||
}
|
||||
else {
|
||||
size_t i;
|
||||
for (i=0; i<NUM_ERC20_SELECTORS; i++) {
|
||||
if (memcmp((uint8_t *)PIC(ERC20_SELECTORS[i]), msg->selector, SELECTOR_SIZE) == 0) {
|
||||
context->selectorIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i == NUM_ERC20_SELECTORS) {
|
||||
PRINTF("Unknown selector %.*H\n", SELECTOR_SIZE, msg->selector);
|
||||
msg->result = ETH_PLUGIN_RESULT_ERROR;
|
||||
break;
|
||||
}
|
||||
PRINTF("erc20 plugin init\n");
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case ETH_PLUGIN_PROVIDE_PARAMETER: {
|
||||
ethPluginProvideParameter_t *msg = (ethPluginProvideParameter_t *) parameters;
|
||||
erc20_parameters_t *context = (erc20_parameters_t *) msg->pluginContext;
|
||||
PRINTF("erc20 plugin provide parameter %d %.*H\n",
|
||||
msg->parameterOffset,
|
||||
32,
|
||||
msg->parameter);
|
||||
switch (msg->parameterOffset) {
|
||||
case 4:
|
||||
memmove(context->destinationAddress, msg->parameter + 12, 20);
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
break;
|
||||
case 4 + 32:
|
||||
memmove(context->amount, msg->parameter, 32);
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
break;
|
||||
default:
|
||||
PRINTF("Unhandled parameter offset\n");
|
||||
msg->result = ETH_PLUGIN_RESULT_ERROR;
|
||||
break;
|
||||
}
|
||||
} break;
|
||||
|
||||
case ETH_PLUGIN_PROVIDE_PARAMETER : {
|
||||
ethPluginProvideParameter_t *msg = (ethPluginProvideParameter_t*)parameters;
|
||||
erc20_parameters_t *context = (erc20_parameters_t*)msg->pluginContext;
|
||||
PRINTF("erc20 plugin provide parameter %d %.*H\n", msg->parameterOffset, 32, msg->parameter);
|
||||
switch(msg->parameterOffset) {
|
||||
case 4:
|
||||
memmove(context->destinationAddress, msg->parameter + 12, 20);
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
break;
|
||||
case 4 + 32:
|
||||
memmove(context->amount, msg->parameter, 32);
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
break;
|
||||
default:
|
||||
PRINTF("Unhandled parameter offset\n");
|
||||
msg->result = ETH_PLUGIN_RESULT_ERROR;
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case ETH_PLUGIN_FINALIZE: {
|
||||
ethPluginFinalize_t *msg = (ethPluginFinalize_t *) parameters;
|
||||
erc20_parameters_t *context = (erc20_parameters_t *) msg->pluginContext;
|
||||
PRINTF("erc20 plugin finalize\n");
|
||||
if (context->selectorIndex == ERC20_TRANSFER) {
|
||||
msg->tokenLookup1 = msg->pluginSharedRO->txContent->destination;
|
||||
msg->amount = context->amount;
|
||||
msg->address = context->destinationAddress;
|
||||
msg->uiType = ETH_UI_TYPE_AMOUNT_ADDRESS;
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
} else if (context->selectorIndex == ERC20_APPROVE) {
|
||||
msg->tokenLookup1 = msg->pluginSharedRO->txContent->destination;
|
||||
msg->tokenLookup2 = context->destinationAddress;
|
||||
msg->numScreens = 2;
|
||||
msg->uiType = ETH_UI_TYPE_GENERIC;
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
}
|
||||
} break;
|
||||
|
||||
case ETH_PLUGIN_FINALIZE: {
|
||||
ethPluginFinalize_t *msg = (ethPluginFinalize_t*)parameters;
|
||||
erc20_parameters_t *context = (erc20_parameters_t*)msg->pluginContext;
|
||||
PRINTF("erc20 plugin finalize\n");
|
||||
if (context->selectorIndex == ERC20_TRANSFER){
|
||||
msg->tokenLookup1 = msg->pluginSharedRO->txContent->destination;
|
||||
msg->amount = context->amount;
|
||||
msg->address = context->destinationAddress;
|
||||
msg->uiType = ETH_UI_TYPE_AMOUNT_ADDRESS;
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
}
|
||||
else if (context->selectorIndex == ERC20_APPROVE){
|
||||
msg->tokenLookup1 = msg->pluginSharedRO->txContent->destination;
|
||||
msg->tokenLookup2 = context->destinationAddress;
|
||||
msg->numScreens = 2;
|
||||
msg->uiType = ETH_UI_TYPE_GENERIC;
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case ETH_PLUGIN_PROVIDE_TOKEN: {
|
||||
ethPluginProvideToken_t *msg = (ethPluginProvideToken_t *) parameters;
|
||||
erc20_parameters_t *context = (erc20_parameters_t *) msg->pluginContext;
|
||||
PRINTF("erc20 plugin provide token 1: %d - 2: %d\n",
|
||||
(msg->token1 != NULL),
|
||||
(msg->token2 != NULL));
|
||||
if (msg->token1 != NULL) {
|
||||
context->target = TARGET_ADDRESS;
|
||||
strcpy((char *) context->ticker_1, (char *) msg->token1->ticker);
|
||||
context->decimals = msg->token1->decimals;
|
||||
if (context->selectorIndex == ERC20_APPROVE) {
|
||||
if (msg->token2 != NULL) {
|
||||
context->target = TARGET_CONTRACT;
|
||||
strcpy((char *) context->ticker_2, (char *) msg->token2->ticker);
|
||||
// test if we're doing a Compound allowance
|
||||
if (check_token_binding((char *) msg->token1->ticker,
|
||||
(char *) msg->token2->ticker,
|
||||
COMPOUND_BINDINGS,
|
||||
NUM_COMPOUND_BINDINGS)) {
|
||||
context->target = TARGET_COMPOUND;
|
||||
}
|
||||
}
|
||||
}
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
} else {
|
||||
msg->result = ETH_PLUGIN_RESULT_FALLBACK;
|
||||
}
|
||||
} break;
|
||||
|
||||
case ETH_PLUGIN_PROVIDE_TOKEN: {
|
||||
ethPluginProvideToken_t *msg = (ethPluginProvideToken_t*)parameters;
|
||||
erc20_parameters_t *context = (erc20_parameters_t*)msg->pluginContext;
|
||||
PRINTF("erc20 plugin provide token 1: %d - 2: %d\n", (msg->token1 != NULL), (msg->token2 != NULL));
|
||||
if (msg->token1 != NULL) {
|
||||
context->target = TARGET_ADDRESS;
|
||||
strcpy((char *)context->ticker_1, (char *)msg->token1->ticker);
|
||||
context->decimals = msg->token1->decimals;
|
||||
if (context->selectorIndex == ERC20_APPROVE){
|
||||
if(msg->token2 != NULL){
|
||||
context->target = TARGET_CONTRACT;
|
||||
strcpy((char *)context->ticker_2, (char *)msg->token2->ticker);
|
||||
// test if we're doing a Compound allowance
|
||||
if (check_token_binding((char *)msg->token1->ticker, (char *)msg->token2->ticker, COMPOUND_BINDINGS, NUM_COMPOUND_BINDINGS)){
|
||||
context->target = TARGET_COMPOUND;
|
||||
}
|
||||
}
|
||||
}
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
}
|
||||
else {
|
||||
msg->result = ETH_PLUGIN_RESULT_FALLBACK;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case ETH_PLUGIN_QUERY_CONTRACT_ID: {
|
||||
ethQueryContractID_t *msg = (ethQueryContractID_t *) parameters;
|
||||
strcpy(msg->name, "Type");
|
||||
strcpy(msg->version, "Approve");
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
} break;
|
||||
|
||||
case ETH_PLUGIN_QUERY_CONTRACT_ID: {
|
||||
ethQueryContractID_t *msg = (ethQueryContractID_t*)parameters;
|
||||
strcpy(msg->name, "Type");
|
||||
strcpy(msg->version, "Approve");
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
}
|
||||
break;
|
||||
case ETH_PLUGIN_QUERY_CONTRACT_UI: {
|
||||
ethQueryContractUI_t *msg = (ethQueryContractUI_t *) parameters;
|
||||
erc20_parameters_t *context = (erc20_parameters_t *) msg->pluginContext;
|
||||
switch (msg->screenIndex) {
|
||||
case 0:
|
||||
strcpy(msg->title, "Amount");
|
||||
if (ismaxint(context->amount, sizeof(context->amount))) {
|
||||
strcpy(msg->msg, "Unlimited ");
|
||||
strcat(msg->msg, (char *) context->ticker_1);
|
||||
} else {
|
||||
amountToString(context->amount,
|
||||
sizeof(context->amount),
|
||||
context->decimals,
|
||||
(char *) context->ticker_1,
|
||||
msg->msg,
|
||||
100);
|
||||
}
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
break;
|
||||
case 1:
|
||||
if (context->target >= TARGET_CONTRACT) {
|
||||
strcpy(msg->title, "Contract");
|
||||
if (context->target == TARGET_COMPOUND) {
|
||||
strcpy(msg->msg, "Compound ");
|
||||
strcat(msg->msg,
|
||||
(char *) context->ticker_2 +
|
||||
1); // remove the 'c' char at beginning of compound ticker
|
||||
} else {
|
||||
strcpy(msg->msg, (char *) context->ticker_2);
|
||||
}
|
||||
} else {
|
||||
strcpy(msg->title, "Address");
|
||||
msg->msg[0] = '0';
|
||||
msg->msg[1] = 'x';
|
||||
getEthAddressStringFromBinary(context->destinationAddress,
|
||||
(uint8_t *) msg->msg + 2,
|
||||
msg->pluginSharedRW->sha3,
|
||||
chainConfig);
|
||||
}
|
||||
|
||||
case ETH_PLUGIN_QUERY_CONTRACT_UI: {
|
||||
ethQueryContractUI_t *msg = (ethQueryContractUI_t*)parameters;
|
||||
erc20_parameters_t *context = (erc20_parameters_t*)msg->pluginContext;
|
||||
switch(msg->screenIndex) {
|
||||
case 0:
|
||||
strcpy(msg->title, "Amount");
|
||||
if(ismaxint(context->amount, sizeof(context->amount))){
|
||||
strcpy(msg->msg, "Unlimited ");
|
||||
strcat(msg->msg, (char *)context->ticker_1);
|
||||
}
|
||||
else{
|
||||
amountToString(context->amount, sizeof(context->amount), context->decimals, (char *)context->ticker_1, msg->msg, 100);
|
||||
}
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
break;
|
||||
case 1:
|
||||
if(context->target >= TARGET_CONTRACT){
|
||||
strcpy(msg->title, "Contract");
|
||||
if (context->target == TARGET_COMPOUND){
|
||||
strcpy(msg->msg, "Compound ");
|
||||
strcat(msg->msg, (char *)context->ticker_2 + 1); // remove the 'c' char at beginning of compound ticker
|
||||
}
|
||||
else {
|
||||
strcpy(msg->msg, (char *)context->ticker_2);
|
||||
}
|
||||
}
|
||||
else{
|
||||
strcpy(msg->title, "Address");
|
||||
msg->msg[0] = '0';
|
||||
msg->msg[1] = 'x';
|
||||
getEthAddressStringFromBinary(context->destinationAddress, (uint8_t *)msg->msg + 2, msg->pluginSharedRW->sha3, chainConfig);
|
||||
}
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} break;
|
||||
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
PRINTF("Unhandled message %d\n", message);
|
||||
}
|
||||
default:
|
||||
PRINTF("Unhandled message %d\n", message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,140 +9,139 @@ void starkware_print_stark_key(uint8_t *starkKey, char *destination);
|
||||
void starkware_print_eth_address(uint8_t *address, char *destination);
|
||||
|
||||
typedef struct erc721_parameters_t {
|
||||
uint8_t selectorIndex;
|
||||
uint8_t address[20];
|
||||
uint8_t tokenId[32];
|
||||
//tokenDefinition_t *tokenSelf;
|
||||
//tokenDefinition_t *tokenAddress;
|
||||
uint8_t selectorIndex;
|
||||
uint8_t address[20];
|
||||
uint8_t tokenId[32];
|
||||
// tokenDefinition_t *tokenSelf;
|
||||
// tokenDefinition_t *tokenAddress;
|
||||
} erc721_parameters_t;
|
||||
|
||||
bool erc721_plugin_available_check() {
|
||||
#ifdef HAVE_STARKWARE
|
||||
if (quantumSet) {
|
||||
switch(dataContext.tokenContext.quantumType) {
|
||||
case STARK_QUANTUM_ERC721:
|
||||
case STARK_QUANTUM_MINTABLE_ERC721:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
if (quantumSet) {
|
||||
switch (dataContext.tokenContext.quantumType) {
|
||||
case STARK_QUANTUM_ERC721:
|
||||
case STARK_QUANTUM_MINTABLE_ERC721:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
void erc721_plugin_call(int message, void *parameters) {
|
||||
switch (message) {
|
||||
case ETH_PLUGIN_INIT_CONTRACT: {
|
||||
ethPluginInitContract_t *msg = (ethPluginInitContract_t *) parameters;
|
||||
erc721_parameters_t *context = (erc721_parameters_t *) msg->pluginContext;
|
||||
// enforce that ETH amount should be 0
|
||||
if (!allzeroes(msg->pluginSharedRO->txContent->value.value, 32)) {
|
||||
PRINTF("Err: Transaction amount is not 0 for erc721 approval\n");
|
||||
msg->result = ETH_PLUGIN_RESULT_ERROR;
|
||||
} else {
|
||||
size_t i;
|
||||
for (i = 0; i < NUM_ERC721_SELECTORS; i++) {
|
||||
if (memcmp((uint8_t *) PIC(ERC721_SELECTORS[i]),
|
||||
msg->selector,
|
||||
SELECTOR_SIZE) == 0) {
|
||||
context->selectorIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i == NUM_ERC721_SELECTORS) {
|
||||
PRINTF("Unknown erc721 selector %.*H\n", SELECTOR_SIZE, msg->selector);
|
||||
break;
|
||||
}
|
||||
if (msg->dataSize != 4 + 32 + 32) {
|
||||
PRINTF("Invalid erc721 approval data size %d\n", msg->dataSize);
|
||||
break;
|
||||
}
|
||||
PRINTF("erc721 plugin init\n");
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
}
|
||||
} break;
|
||||
|
||||
switch(message) {
|
||||
case ETH_PLUGIN_INIT_CONTRACT: {
|
||||
ethPluginInitContract_t *msg = (ethPluginInitContract_t*)parameters;
|
||||
erc721_parameters_t *context = (erc721_parameters_t*)msg->pluginContext;
|
||||
// enforce that ETH amount should be 0
|
||||
if (!allzeroes(msg->pluginSharedRO->txContent->value.value, 32)){
|
||||
PRINTF("Err: Transaction amount is not 0 for erc721 approval\n");
|
||||
msg->result = ETH_PLUGIN_RESULT_ERROR;
|
||||
}
|
||||
else {
|
||||
size_t i;
|
||||
for (i=0; i<NUM_ERC721_SELECTORS; i++) {
|
||||
if (memcmp((uint8_t *)PIC(ERC721_SELECTORS[i]), msg->selector, SELECTOR_SIZE) == 0) {
|
||||
context->selectorIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i == NUM_ERC721_SELECTORS) {
|
||||
PRINTF("Unknown erc721 selector %.*H\n", SELECTOR_SIZE, msg->selector);
|
||||
break;
|
||||
}
|
||||
if (msg->dataSize != 4 + 32 + 32) {
|
||||
PRINTF("Invalid erc721 approval data size %d\n", msg->dataSize);
|
||||
break;
|
||||
}
|
||||
PRINTF("erc721 plugin init\n");
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case ETH_PLUGIN_PROVIDE_PARAMETER: {
|
||||
ethPluginProvideParameter_t *msg = (ethPluginProvideParameter_t *) parameters;
|
||||
erc721_parameters_t *context = (erc721_parameters_t *) msg->pluginContext;
|
||||
PRINTF("erc721 plugin provide parameter %d %.*H\n",
|
||||
msg->parameterOffset,
|
||||
32,
|
||||
msg->parameter);
|
||||
switch (msg->parameterOffset) {
|
||||
case 4:
|
||||
memmove(context->address, msg->parameter + 32 - 20, 20);
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
break;
|
||||
case 4 + 32:
|
||||
memmove(context->tokenId, msg->parameter, 32);
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
break;
|
||||
default:
|
||||
PRINTF("Unhandled parameter offset\n");
|
||||
break;
|
||||
}
|
||||
} break;
|
||||
|
||||
case ETH_PLUGIN_PROVIDE_PARAMETER : {
|
||||
ethPluginProvideParameter_t *msg = (ethPluginProvideParameter_t*)parameters;
|
||||
erc721_parameters_t *context = (erc721_parameters_t*)msg->pluginContext;
|
||||
PRINTF("erc721 plugin provide parameter %d %.*H\n", msg->parameterOffset, 32, msg->parameter);
|
||||
switch(msg->parameterOffset) {
|
||||
case 4:
|
||||
memmove(context->address, msg->parameter + 32 - 20, 20);
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
break;
|
||||
case 4 + 32:
|
||||
memmove(context->tokenId, msg->parameter, 32);
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
break;
|
||||
default:
|
||||
PRINTF("Unhandled parameter offset\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case ETH_PLUGIN_FINALIZE: {
|
||||
ethPluginFinalize_t *msg = (ethPluginFinalize_t *) parameters;
|
||||
erc721_parameters_t *context = (erc721_parameters_t *) msg->pluginContext;
|
||||
PRINTF("erc721 plugin finalize\n");
|
||||
msg->tokenLookup1 = msg->pluginSharedRO->txContent->destination;
|
||||
msg->tokenLookup2 = context->address;
|
||||
msg->numScreens = 3;
|
||||
msg->uiType = ETH_UI_TYPE_GENERIC;
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
} break;
|
||||
|
||||
case ETH_PLUGIN_FINALIZE: {
|
||||
ethPluginFinalize_t *msg = (ethPluginFinalize_t*)parameters;
|
||||
erc721_parameters_t *context = (erc721_parameters_t*)msg->pluginContext;
|
||||
PRINTF("erc721 plugin finalize\n");
|
||||
msg->tokenLookup1 = msg->pluginSharedRO->txContent->destination;
|
||||
msg->tokenLookup2 = context->address;
|
||||
msg->numScreens = 3;
|
||||
msg->uiType = ETH_UI_TYPE_GENERIC;
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
}
|
||||
break;
|
||||
case ETH_PLUGIN_PROVIDE_TOKEN: {
|
||||
ethPluginProvideToken_t *msg = (ethPluginProvideToken_t *) parameters;
|
||||
erc721_parameters_t *context = (erc721_parameters_t *) msg->pluginContext;
|
||||
PRINTF("erc721 plugin provide token dest: %d - address: %d\n",
|
||||
(msg->token1 != NULL),
|
||||
(msg->token2 != NULL));
|
||||
// context->tokenSelf = msg->token1;
|
||||
// context->tokenAddress = msg->token2;
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
} break;
|
||||
|
||||
case ETH_PLUGIN_PROVIDE_TOKEN: {
|
||||
ethPluginProvideToken_t *msg = (ethPluginProvideToken_t*)parameters;
|
||||
erc721_parameters_t *context = (erc721_parameters_t*)msg->pluginContext;
|
||||
PRINTF("erc721 plugin provide token dest: %d - address: %d\n", (msg->token1 != NULL), (msg->token2 != NULL));
|
||||
//context->tokenSelf = msg->token1;
|
||||
//context->tokenAddress = msg->token2;
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
}
|
||||
break;
|
||||
case ETH_PLUGIN_QUERY_CONTRACT_ID: {
|
||||
ethQueryContractID_t *msg = (ethQueryContractID_t *) parameters;
|
||||
strcpy(msg->name, "Allowance");
|
||||
strcpy(msg->version, "");
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
} break;
|
||||
|
||||
case ETH_PLUGIN_QUERY_CONTRACT_ID: {
|
||||
ethQueryContractID_t *msg = (ethQueryContractID_t*)parameters;
|
||||
strcpy(msg->name, "Allowance");
|
||||
strcpy(msg->version, "");
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
}
|
||||
break;
|
||||
case ETH_PLUGIN_QUERY_CONTRACT_UI: {
|
||||
ethQueryContractUI_t *msg = (ethQueryContractUI_t *) parameters;
|
||||
erc721_parameters_t *context = (erc721_parameters_t *) msg->pluginContext;
|
||||
switch (msg->screenIndex) {
|
||||
case 0:
|
||||
strcpy(msg->title, "Contract Name");
|
||||
starkware_print_eth_address(tmpContent.txContent.destination, msg->msg);
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
break;
|
||||
|
||||
case ETH_PLUGIN_QUERY_CONTRACT_UI: {
|
||||
ethQueryContractUI_t *msg = (ethQueryContractUI_t*)parameters;
|
||||
erc721_parameters_t *context = (erc721_parameters_t*)msg->pluginContext;
|
||||
switch(msg->screenIndex) {
|
||||
case 0:
|
||||
strcpy(msg->title, "Contract Name");
|
||||
starkware_print_eth_address(tmpContent.txContent.destination, msg->msg);
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
break;
|
||||
case 1:
|
||||
strcpy(msg->title, "NFT Contract");
|
||||
starkware_print_eth_address(context->address, msg->msg);
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
break;
|
||||
|
||||
case 1:
|
||||
strcpy(msg->title, "NFT Contract");
|
||||
starkware_print_eth_address(context->address, msg->msg);
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
break;
|
||||
case 2:
|
||||
strcpy(msg->title, "TokenID");
|
||||
starkware_print_stark_key(context->tokenId, msg->msg);
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
break;
|
||||
|
||||
case 2:
|
||||
strcpy(msg->title, "TokenID");
|
||||
starkware_print_stark_key(context->tokenId, msg->msg);
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
PRINTF("Unhandled message %d\n", message);
|
||||
}
|
||||
default:
|
||||
PRINTF("Unhandled message %d\n", message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,155 +13,156 @@ void getEth2PublicKey(uint32_t *bip32Path, uint8_t bip32PathLength, uint8_t *out
|
||||
#define WITHDRAWAL_KEY_PATH_2 3600
|
||||
#define WITHDRAWAL_KEY_PATH_4 0
|
||||
|
||||
#define ETH2_DEPOSIT_PUBKEY_OFFSET 0x80
|
||||
#define ETH2_DEPOSIT_PUBKEY_OFFSET 0x80
|
||||
#define ETH2_WITHDRAWAL_CREDENTIALS_OFFSET 0xE0
|
||||
#define ETH2_SIGNATURE_OFFSET 0x120
|
||||
#define ETH2_DEPOSIT_PUBKEY_LENGTH 0x30
|
||||
#define ETH2_SIGNATURE_OFFSET 0x120
|
||||
#define ETH2_DEPOSIT_PUBKEY_LENGTH 0x30
|
||||
#define ETH2_WITHDRAWAL_CREDENTIALS_LENGTH 0x20
|
||||
#define ETH2_SIGNATURE_LENGTH 0x60
|
||||
#define ETH2_SIGNATURE_LENGTH 0x60
|
||||
|
||||
typedef struct eth2_deposit_parameters_t {
|
||||
uint8_t valid;
|
||||
uint8_t valid;
|
||||
} eth2_deposit_parameters_t;
|
||||
|
||||
void eth2_plugin_call(int message, void *parameters) {
|
||||
switch (message) {
|
||||
case ETH_PLUGIN_INIT_CONTRACT: {
|
||||
ethPluginInitContract_t *msg = (ethPluginInitContract_t *) parameters;
|
||||
eth2_deposit_parameters_t *context = (eth2_deposit_parameters_t *) msg->pluginContext;
|
||||
context->valid = 1;
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
} break;
|
||||
|
||||
switch(message) {
|
||||
case ETH_PLUGIN_INIT_CONTRACT: {
|
||||
ethPluginInitContract_t *msg = (ethPluginInitContract_t*)parameters;
|
||||
eth2_deposit_parameters_t *context = (eth2_deposit_parameters_t*)msg->pluginContext;
|
||||
context->valid = 1;
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
}
|
||||
break;
|
||||
case ETH_PLUGIN_PROVIDE_PARAMETER: {
|
||||
ethPluginProvideParameter_t *msg = (ethPluginProvideParameter_t *) parameters;
|
||||
eth2_deposit_parameters_t *context = (eth2_deposit_parameters_t *) msg->pluginContext;
|
||||
uint32_t index;
|
||||
PRINTF("eth2 plugin provide parameter %d %.*H\n",
|
||||
msg->parameterOffset,
|
||||
32,
|
||||
msg->parameter);
|
||||
switch (msg->parameterOffset) {
|
||||
case 4 + (32 * 0): // pubkey offset
|
||||
case 4 + (32 * 1): // withdrawal credentials offset
|
||||
case 4 + (32 * 2): // signature offset
|
||||
case 4 + (32 * 4): // deposit pubkey length
|
||||
case 4 + (32 * 7): // withdrawal credentials length
|
||||
case 4 + (32 * 9): // signature length
|
||||
{
|
||||
uint32_t check = 0;
|
||||
switch (msg->parameterOffset) {
|
||||
case 4 + (32 * 0):
|
||||
check = ETH2_DEPOSIT_PUBKEY_OFFSET;
|
||||
break;
|
||||
case 4 + (32 * 1):
|
||||
check = ETH2_WITHDRAWAL_CREDENTIALS_OFFSET;
|
||||
break;
|
||||
case 4 + (32 * 2):
|
||||
check = ETH2_SIGNATURE_OFFSET;
|
||||
break;
|
||||
case 4 + (32 * 4):
|
||||
check = ETH2_DEPOSIT_PUBKEY_LENGTH;
|
||||
break;
|
||||
case 4 + (32 * 7):
|
||||
check = ETH2_WITHDRAWAL_CREDENTIALS_LENGTH;
|
||||
break;
|
||||
case 4 + (32 * 9):
|
||||
check = ETH2_SIGNATURE_LENGTH;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
index = U4BE(msg->parameter, 32 - 4);
|
||||
if (index != check) {
|
||||
PRINTF("eth2 plugin parameter check %d failed, expected %d got %d\n",
|
||||
msg->parameterOffset,
|
||||
check,
|
||||
index);
|
||||
context->valid = 0;
|
||||
}
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
} break;
|
||||
|
||||
case ETH_PLUGIN_PROVIDE_PARAMETER : {
|
||||
ethPluginProvideParameter_t *msg = (ethPluginProvideParameter_t*)parameters;
|
||||
eth2_deposit_parameters_t *context = (eth2_deposit_parameters_t*)msg->pluginContext;
|
||||
uint32_t index;
|
||||
PRINTF("eth2 plugin provide parameter %d %.*H\n", msg->parameterOffset, 32, msg->parameter);
|
||||
switch(msg->parameterOffset) {
|
||||
case 4 + (32 * 0): // pubkey offset
|
||||
case 4 + (32 * 1): // withdrawal credentials offset
|
||||
case 4 + (32 * 2): // signature offset
|
||||
case 4 + (32 * 4): // deposit pubkey length
|
||||
case 4 + (32 * 7): // withdrawal credentials length
|
||||
case 4 + (32 * 9): // signature length
|
||||
{
|
||||
uint32_t check = 0;
|
||||
switch(msg->parameterOffset) {
|
||||
case 4 + (32 * 0):
|
||||
check = ETH2_DEPOSIT_PUBKEY_OFFSET;
|
||||
break;
|
||||
case 4 + (32 * 1):
|
||||
check = ETH2_WITHDRAWAL_CREDENTIALS_OFFSET;
|
||||
break;
|
||||
case 4 + (32 * 2):
|
||||
check = ETH2_SIGNATURE_OFFSET;
|
||||
break;
|
||||
case 4 + (32 * 4):
|
||||
check = ETH2_DEPOSIT_PUBKEY_LENGTH;
|
||||
break;
|
||||
case 4 + (32 * 7):
|
||||
check = ETH2_WITHDRAWAL_CREDENTIALS_LENGTH;
|
||||
break;
|
||||
case 4 + (32 * 9):
|
||||
check = ETH2_SIGNATURE_LENGTH;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
index = U4BE(msg->parameter, 32 - 4);
|
||||
if (index != check) {
|
||||
PRINTF("eth2 plugin parameter check %d failed, expected %d got %d\n", msg->parameterOffset, check, index);
|
||||
context->valid = 0;
|
||||
}
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
}
|
||||
break;
|
||||
case 4 + (32 * 3): // deposit data root
|
||||
case 4 + (32 * 5): // deposit pubkey
|
||||
case 4 + (32 * 6):
|
||||
case 4 + (32 * 10): // signature
|
||||
case 4 + (32 * 11):
|
||||
case 4 + (32 * 12):
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
break;
|
||||
|
||||
case 4 + (32 * 3): // deposit data root
|
||||
case 4 + (32 * 5): // deposit pubkey
|
||||
case 4 + (32 * 6):
|
||||
case 4 + (32 * 10): // signature
|
||||
case 4 + (32 * 11):
|
||||
case 4 + (32 * 12):
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
break;
|
||||
case 4 + (32 * 8): // withdrawal credentials
|
||||
{
|
||||
uint8_t tmp[48];
|
||||
uint32_t withdrawalKeyPath[4];
|
||||
withdrawalKeyPath[0] = WITHDRAWAL_KEY_PATH_1;
|
||||
withdrawalKeyPath[1] = WITHDRAWAL_KEY_PATH_2;
|
||||
withdrawalKeyPath[2] = eth2WithdrawalIndex;
|
||||
withdrawalKeyPath[3] = WITHDRAWAL_KEY_PATH_4;
|
||||
getEth2PublicKey(withdrawalKeyPath, 4, tmp);
|
||||
PRINTF("eth2 plugin computed withdrawal public key %.*H\n", 48, tmp);
|
||||
cx_hash_sha256(tmp, 48, tmp, 32);
|
||||
tmp[0] = 0;
|
||||
if (memcmp(tmp, msg->parameter, 32) != 0) {
|
||||
PRINTF("eth2 plugin invalid withdrawal credentials\n");
|
||||
PRINTF("Got %.*H\n", 32, msg->parameter);
|
||||
PRINTF("Expected %.*H\n", 32, tmp);
|
||||
context->valid = 0;
|
||||
}
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
} break;
|
||||
|
||||
case 4 + (32 * 8): // withdrawal credentials
|
||||
{
|
||||
uint8_t tmp[48];
|
||||
uint32_t withdrawalKeyPath[4];
|
||||
withdrawalKeyPath[0] = WITHDRAWAL_KEY_PATH_1;
|
||||
withdrawalKeyPath[1] = WITHDRAWAL_KEY_PATH_2;
|
||||
withdrawalKeyPath[2] = eth2WithdrawalIndex;
|
||||
withdrawalKeyPath[3] = WITHDRAWAL_KEY_PATH_4;
|
||||
getEth2PublicKey(withdrawalKeyPath, 4, tmp);
|
||||
PRINTF("eth2 plugin computed withdrawal public key %.*H\n", 48, tmp);
|
||||
cx_hash_sha256(tmp, 48, tmp, 32);
|
||||
tmp[0] = 0;
|
||||
if (memcmp(tmp, msg->parameter, 32) != 0) {
|
||||
PRINTF("eth2 plugin invalid withdrawal credentials\n");
|
||||
PRINTF("Got %.*H\n", 32, msg->parameter);
|
||||
PRINTF("Expected %.*H\n", 32, tmp);
|
||||
context->valid = 0;
|
||||
}
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
PRINTF("Unhandled parameter offset\n");
|
||||
break;
|
||||
}
|
||||
} break;
|
||||
|
||||
default:
|
||||
PRINTF("Unhandled parameter offset\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case ETH_PLUGIN_FINALIZE: {
|
||||
ethPluginFinalize_t *msg = (ethPluginFinalize_t *) parameters;
|
||||
eth2_deposit_parameters_t *context = (eth2_deposit_parameters_t *) msg->pluginContext;
|
||||
PRINTF("eth2 plugin finalize\n");
|
||||
if (context->valid) {
|
||||
msg->numScreens = 1;
|
||||
msg->uiType = ETH_UI_TYPE_GENERIC;
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
} else {
|
||||
msg->result = ETH_PLUGIN_RESULT_FALLBACK;
|
||||
}
|
||||
} break;
|
||||
|
||||
case ETH_PLUGIN_FINALIZE: {
|
||||
ethPluginFinalize_t *msg = (ethPluginFinalize_t*)parameters;
|
||||
eth2_deposit_parameters_t *context = (eth2_deposit_parameters_t*)msg->pluginContext;
|
||||
PRINTF("eth2 plugin finalize\n");
|
||||
if (context->valid) {
|
||||
msg->numScreens = 1;
|
||||
msg->uiType = ETH_UI_TYPE_GENERIC;
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
}
|
||||
else {
|
||||
msg->result = ETH_PLUGIN_RESULT_FALLBACK;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case ETH_PLUGIN_QUERY_CONTRACT_ID: {
|
||||
ethQueryContractID_t *msg = (ethQueryContractID_t *) parameters;
|
||||
strcpy(msg->name, "ETH2");
|
||||
strcpy(msg->version, "Deposit");
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
} break;
|
||||
|
||||
case ETH_PLUGIN_QUERY_CONTRACT_ID: {
|
||||
ethQueryContractID_t *msg = (ethQueryContractID_t*)parameters;
|
||||
strcpy(msg->name, "ETH2");
|
||||
strcpy(msg->version, "Deposit");
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
}
|
||||
break;
|
||||
case ETH_PLUGIN_QUERY_CONTRACT_UI: {
|
||||
ethQueryContractUI_t *msg = (ethQueryContractUI_t *) parameters;
|
||||
// eth2_deposit_parameters_t *context = (eth2_deposit_parameters_t*)msg->pluginContext;
|
||||
switch (msg->screenIndex) {
|
||||
case 0: {
|
||||
uint8_t decimals = WEI_TO_ETHER;
|
||||
uint8_t *ticker = (uint8_t *) PIC(chainConfig->coinName);
|
||||
strcpy(msg->title, "Amount");
|
||||
amountToString(tmpContent.txContent.value.value,
|
||||
tmpContent.txContent.value.length,
|
||||
decimals,
|
||||
(char *) ticker,
|
||||
msg->msg,
|
||||
100);
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
} break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} break;
|
||||
|
||||
case ETH_PLUGIN_QUERY_CONTRACT_UI: {
|
||||
ethQueryContractUI_t *msg = (ethQueryContractUI_t*)parameters;
|
||||
//eth2_deposit_parameters_t *context = (eth2_deposit_parameters_t*)msg->pluginContext;
|
||||
switch(msg->screenIndex) {
|
||||
case 0: {
|
||||
uint8_t decimals = WEI_TO_ETHER;
|
||||
uint8_t *ticker = (uint8_t *)PIC(chainConfig->coinName);
|
||||
strcpy(msg->title, "Amount");
|
||||
amountToString(tmpContent.txContent.value.value, tmpContent.txContent.value.length, decimals, (char*)ticker, msg->msg, 100);
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
PRINTF("Unhandled message %d\n", message);
|
||||
}
|
||||
default:
|
||||
PRINTF("Unhandled message %d\n", message);
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user