Fix nft transactions (#229)

* Removed pluginType "hack"

* Fix some ERC 721 & 1155 function signature hashes

* Fix UI for ERC721 operations

* Explicit Batch Transfer UI with ERC1155

* Unified some ERC721 & 1155 non-static functions naming

* Fix UI for ERC1155 operations

* Added missing pin-lock check when signing transactions

* Fix the shell script that builds the elf files for testing

* Add tests dependency ethers

* Removed the space in the test filename

* Tests build script refactoring

* Now works when called from anywhere (not just the script's directory)
* Now handles LNS & LNX builds together (less duplicated code)

* Temporarily disable Nano X tests

Until Zemu supports Nano X 2.0 SDK

* Tests now start with blind signing disabled

Makes it closer to reality & very few of them requires it

* Update to the latest sdk version

* make eth_plugin_perform_init() readable

Introduce 2 functions.

* Now properly parses the apdu and displays the total quantity of NFT IDs transferred in ERC1155 batch transfer

* Add NFT prod public keys

* Added extra checks for the chain ID handling

Following the security review

* NFTs now only supported by LNS

* Version bump

Co-authored-by: Alexandre Paillier <alexandre.paillier@ledger.fr>
Co-authored-by: greenknot <greenknot@users.noreply.github.com>
This commit is contained in:
Jean P
2021-12-17 12:04:51 +01:00
committed by GitHub
parent 586155c0d4
commit a53a2428cc
40 changed files with 735 additions and 208 deletions

View File

@@ -1,9 +1,11 @@
#ifdef HAVE_NFT_SUPPORT
#include "erc1155_plugin.h"
#include "eth_plugin_internal.h"
static const uint8_t ERC1155_APPROVE_FOR_ALL_SELECTOR[SELECTOR_SIZE] = {0xa2, 0x2c, 0xb4, 0x65};
static const uint8_t ERC1155_SAFE_TRANSFER_SELECTOR[SELECTOR_SIZE] = {0xf2, 0x42, 0x43, 0x2a};
static const uint8_t ERC1155_SAFE_BATCH_TRANSFER[SELECTOR_SIZE] = {0xf2, 0x42, 0x43, 0x2a};
static const uint8_t ERC1155_SAFE_BATCH_TRANSFER[SELECTOR_SIZE] = {0x2e, 0xb2, 0xc2, 0xd6};
const uint8_t *const ERC1155_SELECTORS[NUM_ERC1155_SELECTORS] = {
ERC1155_APPROVE_FOR_ALL_SELECTOR,
@@ -59,11 +61,11 @@ static void handle_finalize(void *parameters) {
msg->tokenLookup2 = NULL;
switch (context->selectorIndex) {
case SAFE_TRANSFER:
msg->numScreens = 4;
msg->numScreens = 5;
break;
case SET_APPROVAL_FOR_ALL:
case SAFE_BATCH_TRANSFER:
msg->numScreens = 3;
msg->numScreens = 4;
break;
default:
msg->result = ETH_PLUGIN_RESULT_ERROR;
@@ -99,9 +101,11 @@ static void handle_query_contract_id(void *parameters) {
strlcpy(msg->version, "Allowance", msg->versionLength);
break;
case SAFE_TRANSFER:
case SAFE_BATCH_TRANSFER:
strlcpy(msg->version, "Transfer", msg->versionLength);
break;
case SAFE_BATCH_TRANSFER:
strlcpy(msg->version, "Batch Transfer", msg->versionLength);
break;
default:
PRINTF("Unsupported selector %d\n", context->selectorIndex);
msg->result = ETH_PLUGIN_RESULT_ERROR;
@@ -134,3 +138,5 @@ void erc1155_plugin_call(int message, void *parameters) {
break;
}
}
#endif // HAVE_NFT_SUPPORT

View File

@@ -1,3 +1,5 @@
#ifdef HAVE_NFT_SUPPORT
#pragma once
#include <string.h>
@@ -5,6 +7,7 @@
#include "shared_context.h"
#include "ethUtils.h"
#include "utils.h"
#include "uint256.h"
// Internal plugin for EIP 1155: https://eips.ethereum.org/EIPS/eip-1155
@@ -31,19 +34,22 @@ typedef enum {
} erc1155_selector_field;
typedef struct erc1155_context_t {
uint8_t address[ADDRESS_LENGTH];
uint8_t tokenId[INT256_LENGTH];
uint8_t value[INT256_LENGTH];
uint8_t address[ADDRESS_LENGTH];
uint8_t tokenId[INT256_LENGTH];
uint256_t value;
uint32_t valueOffset;
uint32_t tokenIdsOffset;
uint32_t targetOffset;
uint16_t ids_array_len;
uint32_t ids_offset;
uint16_t values_array_len;
uint32_t values_offset;
uint16_t array_index;
bool approved;
erc1155_selector_field next_param;
uint8_t selectorIndex;
} erc1155_context_t;
// TODO: Find out why there is a duplicate if we remove 1155 suffix
void handle_provide_parameter_1155(void *parameters);
void handle_query_contract_ui_1155(void *parameters);
void handle_query_contract_ui_1155(void *parameters);
#endif // HAVE_NFT_SUPPORT

View File

@@ -1,7 +1,11 @@
#ifdef HAVE_NFT_SUPPORT
#include "erc1155_plugin.h"
#include "eth_plugin_internal.h"
static void handle_safe_transfer(ethPluginProvideParameter_t *msg, erc1155_context_t *context) {
uint8_t new_value[INT256_LENGTH];
switch (context->next_param) {
case FROM:
context->next_param = TO;
@@ -15,7 +19,8 @@ static void handle_safe_transfer(ethPluginProvideParameter_t *msg, erc1155_conte
context->next_param = VALUE;
break;
case VALUE:
copy_parameter(context->value, msg->parameter, sizeof(context->value));
copy_parameter(new_value, msg->parameter, sizeof(new_value));
convertUint256BE(new_value, INT256_LENGTH, &context->value);
context->next_param = NONE;
break;
default:
@@ -25,31 +30,74 @@ static void handle_safe_transfer(ethPluginProvideParameter_t *msg, erc1155_conte
}
static void handle_batch_transfer(ethPluginProvideParameter_t *msg, erc1155_context_t *context) {
uint256_t new_value;
switch (context->next_param) {
case FROM:
context->next_param = TO;
break;
case TO:
copy_address(context->address, msg->parameter, sizeof(context->address));
context->next_param = TOKEN_ID;
context->next_param = TOKEN_IDS_OFFSET;
break;
case TOKEN_IDS_OFFSET:
context->tokenIdsOffset = U4BE(msg->parameter, PARAMETER_LENGTH - 4);
context->ids_offset = \
U4BE(msg->parameter,
PARAMETER_LENGTH - sizeof(context->ids_offset)) + 4;
context->next_param = VALUE_OFFSET;
break;
case VALUE_OFFSET:
context->targetOffset = context->tokenIdsOffset;
context->next_param = TOKEN_ID;
context->values_offset = \
U4BE(msg->parameter,
PARAMETER_LENGTH - sizeof(context->values_offset)) + 4;
context->next_param = TOKEN_IDS_LENGTH;
break;
case TOKEN_IDS_LENGTH:
if ((msg->parameterOffset + PARAMETER_LENGTH) > context->ids_offset)
{
context->ids_array_len = \
U2BE(msg->parameter,
PARAMETER_LENGTH - sizeof(context->ids_array_len));
context->next_param = TOKEN_ID;
// set to zero for next step
context->array_index = 0;
}
break;
case TOKEN_ID:
copy_parameter(context->tokenId, msg->parameter, sizeof(context->tokenId));
context->targetOffset = context->valueOffset;
context->next_param = VALUE;
// don't copy anything since we won't display it
if (--context->ids_array_len == 0)
{
context->next_param = VALUE_LENGTH;
}
context->array_index++;
break;
case VALUE_LENGTH:
if ((msg->parameterOffset + PARAMETER_LENGTH) > context->values_offset)
{
context->values_array_len = \
U2BE(msg->parameter,
PARAMETER_LENGTH - sizeof(context->values_array_len));
if (context->values_array_len != context->array_index)
{
PRINTF("Token ids and values array sizes mismatch!");
}
context->next_param = VALUE;
// set to zero for next step
context->array_index = 0;
explicit_bzero(&context->value, sizeof(context->value));
}
break;
case VALUE:
copy_parameter(context->value, msg->parameter, sizeof(context->value));
context->targetOffset = 0;
context->next_param = NONE;
// put it temporarily in token id since we don't use it in batch transfer
copy_parameter(context->tokenId, msg->parameter, sizeof(context->value));
convertUint256BE(context->tokenId, sizeof(context->tokenId), &new_value);
add256(&context->value, &new_value, &context->value);
if (--context->values_array_len == 0)
{
context->next_param = NONE;
}
context->array_index++;
break;
default:
// Some extra data might be present so don't error.
break;
@@ -84,10 +132,10 @@ void handle_provide_parameter_1155(void *parameters) {
msg->result = ETH_PLUGIN_RESULT_SUCCESSFUL;
if (context->targetOffset > SELECTOR_SIZE &&
context->targetOffset != msg->parameterOffset - SELECTOR_SIZE) {
return;
}
//if (context->targetOffset > SELECTOR_SIZE &&
// context->targetOffset != msg->parameterOffset - SELECTOR_SIZE) {
// return;
//}
switch (context->selectorIndex) {
case SAFE_TRANSFER:
handle_safe_transfer(msg, context);
@@ -103,4 +151,6 @@ void handle_provide_parameter_1155(void *parameters) {
msg->result = ETH_PLUGIN_RESULT_ERROR;
break;
}
}
}
#endif // HAVE_NFT_SUPPORT

View File

@@ -1,3 +1,5 @@
#ifdef HAVE_NFT_SUPPORT
#include "erc1155_plugin.h"
static void set_approval_for_all_ui(ethQueryContractUI_t *msg, erc1155_context_t *context) {
@@ -40,20 +42,6 @@ static void set_approval_for_all_ui(ethQueryContractUI_t *msg, erc1155_context_t
static void set_transfer_ui(ethQueryContractUI_t *msg, erc1155_context_t *context) {
switch (msg->screenIndex) {
case 0:
// What will be displayed on the screen is:
// | Send |
// | `X` `COLLECTION_NAME` |
// where `X` is `value`
strlcpy(msg->title, "Send", msg->titleLength);
uint256_to_decimal(context->value, sizeof(context->value), msg->msg, msg->msgLength);
strlcat(msg->msg, " ", msg->msgLength);
if (msg->item1) {
strlcat(msg->msg, (const char *) &msg->item1->nft.collectionName, msg->msgLength);
} else {
strlcat(msg->msg, "Items", msg->msgLength);
}
break;
case 1:
strlcpy(msg->title, "To", msg->titleLength);
getEthDisplayableAddress(context->address,
msg->msg,
@@ -61,9 +49,17 @@ static void set_transfer_ui(ethQueryContractUI_t *msg, erc1155_context_t *contex
&global_sha3,
chainConfig->chainId);
break;
case 1:
strlcpy(msg->title, "Collection Name", msg->titleLength);
if (msg->item1) {
strlcpy(msg->msg, (const char *) &msg->item1->nft.collectionName, msg->msgLength);
} else {
strlcpy(msg->msg, "Not Found", msg->msgLength);
}
break;
case 2:
strlcpy(msg->title, "NFT Address", msg->titleLength);
getEthDisplayableAddress(msg->pluginSharedRO->txContent->destination,
getEthDisplayableAddress((uint8_t *)msg->item1->nft.contractAddress,
msg->msg,
msg->msgLength,
&global_sha3,
@@ -76,6 +72,10 @@ static void set_transfer_ui(ethQueryContractUI_t *msg, erc1155_context_t *contex
msg->msg,
msg->msgLength);
break;
case 4:
strlcpy(msg->title, "Quantity", msg->titleLength);
tostring256(&context->value, 10, msg->msg, msg->msgLength);
break;
default:
PRINTF("Unsupported screen index %d\n", msg->screenIndex);
msg->result = ETH_PLUGIN_RESULT_ERROR;
@@ -84,27 +84,42 @@ static void set_transfer_ui(ethQueryContractUI_t *msg, erc1155_context_t *contex
}
static void set_batch_transfer_ui(ethQueryContractUI_t *msg, erc1155_context_t *context) {
char quantity_str[48];
switch (msg->screenIndex) {
case 0:
strlcpy(msg->title, "Send NFTs From", msg->titleLength);
uint256_to_decimal(context->value, sizeof(context->value), msg->msg, msg->msgLength);
strlcat(msg->msg, " Different Collections", msg->msgLength);
break;
case 1:
strlcpy(msg->title, "To", msg->titleLength);
getEthDisplayableAddress(context->address,
msg->msg,
msg->msgLength,
&global_sha3,
chainConfig->chainId);
break;
case 1:
strlcpy(msg->title, "Collection Name", msg->titleLength);
if (msg->item1) {
strlcpy(msg->msg, (const char *) &msg->item1->nft.collectionName, msg->msgLength);
} else {
strlcpy(msg->msg, "Not Found", msg->msgLength);
}
break;
case 2:
strlcpy(msg->title, "NFT Address", msg->titleLength);
getEthDisplayableAddress(msg->pluginSharedRO->txContent->destination,
getEthDisplayableAddress((uint8_t *)msg->item1->nft.contractAddress,
msg->msg,
msg->msgLength,
&global_sha3,
chainConfig->chainId);
break;
case 3:
strlcpy(msg->title, "Total Quantity", msg->titleLength);
tostring256(&context->value, 10, &quantity_str[0], sizeof(quantity_str));
snprintf(msg->msg,
msg->msgLength,
"%s from %d NFT IDs",
quantity_str,
context->array_index);
break;
default:
PRINTF("Unsupported screen index %d\n", msg->screenIndex);
msg->result = ETH_PLUGIN_RESULT_ERROR;
@@ -132,4 +147,6 @@ void handle_query_contract_ui_1155(void *parameters) {
PRINTF("Unsupported selector index %d\n", context->selectorIndex);
break;
}
}
}
#endif // HAVE_NFT_SUPPORT

View File

@@ -1,11 +1,13 @@
#ifdef HAVE_NFT_SUPPORT
#include "erc721_plugin.h"
#include "eth_plugin_internal.h"
static const uint8_t ERC721_APPROVE_SELECTOR[SELECTOR_SIZE] = {0x13, 0x37, 0x42, 0x42};
static const uint8_t ERC721_APPROVE_SELECTOR[SELECTOR_SIZE] = {0x09, 0x5e, 0xa7, 0xb3};
static const uint8_t ERC721_APPROVE_FOR_ALL_SELECTOR[SELECTOR_SIZE] = {0xa2, 0x2c, 0xb4, 0x65};
static const uint8_t ERC721_TRANSFER_SELECTOR[SELECTOR_SIZE] = {0x23, 0xb8, 0x72, 0xdd};
static const uint8_t ERC721_SAFE_TRANSFER_SELECTOR[SELECTOR_SIZE] = {0x42, 0x84, 0x2e, 0x0e};
static const uint8_t ERC721_SAFE_TRANSFER_DATA_SELECTOR[SELECTOR_SIZE] = {0xf2, 0x42, 0x43, 0x2a};
static const uint8_t ERC721_SAFE_TRANSFER_DATA_SELECTOR[SELECTOR_SIZE] = {0xb8, 0x8d, 0x4f, 0xde};
const uint8_t *const ERC721_SELECTORS[NUM_ERC721_SELECTORS] = {
ERC721_APPROVE_SELECTOR,
@@ -62,12 +64,12 @@ static void handle_finalize(void *parameters) {
case TRANSFER:
case SAFE_TRANSFER:
case SAFE_TRANSFER_DATA:
case SET_APPROVAL_FOR_ALL:
msg->numScreens = 3;
break;
case APPROVE:
msg->numScreens = 4;
break;
case SET_APPROVAL_FOR_ALL:
msg->numScreens = 3;
break;
default:
msg->result = ETH_PLUGIN_RESULT_ERROR;
return;
@@ -125,7 +127,7 @@ void erc721_plugin_call(int message, void *parameters) {
handle_init_contract(parameters);
} break;
case ETH_PLUGIN_PROVIDE_PARAMETER: {
handle_provide_parameter(parameters);
handle_provide_parameter_721(parameters);
} break;
case ETH_PLUGIN_FINALIZE: {
handle_finalize(parameters);
@@ -137,10 +139,12 @@ void erc721_plugin_call(int message, void *parameters) {
handle_query_contract_id(parameters);
} break;
case ETH_PLUGIN_QUERY_CONTRACT_UI: {
handle_query_contract_ui(parameters);
handle_query_contract_ui_721(parameters);
} break;
default:
PRINTF("Unhandled message %d\n", message);
break;
}
}
#endif // HAVE_NFT_SUPPORT

View File

@@ -1,3 +1,5 @@
#ifdef HAVE_NFT_SUPPORT
#pragma once
#include <string.h>
@@ -38,5 +40,7 @@ typedef struct erc721_context_t {
uint8_t selectorIndex;
} erc721_context_t;
void handle_provide_parameter(void *parameters);
void handle_query_contract_ui(void *parameters);
void handle_provide_parameter_721(void *parameters);
void handle_query_contract_ui_721(void *parameters);
#endif // HAVE_NFT_SUPPORT

View File

@@ -1,3 +1,5 @@
#ifdef HAVE_NFT_SUPPORT
#include "erc721_plugin.h"
#include "eth_plugin_internal.h"
@@ -60,7 +62,7 @@ static void handle_approval_for_all(ethPluginProvideParameter_t *msg, erc721_con
}
}
void handle_provide_parameter(void *parameters) {
void handle_provide_parameter_721(void *parameters) {
ethPluginProvideParameter_t *msg = (ethPluginProvideParameter_t *) parameters;
erc721_context_t *context = (erc721_context_t *) msg->pluginContext;
@@ -90,4 +92,6 @@ void handle_provide_parameter(void *parameters) {
msg->result = ETH_PLUGIN_RESULT_ERROR;
break;
}
}
}
#endif // HAVE_NFT_SUPPORT

View File

@@ -1,3 +1,5 @@
#ifdef HAVE_NFT_SUPPORT
#include "erc721_plugin.h"
static void set_approval_ui(ethQueryContractUI_t *msg, erc721_context_t *context) {
@@ -113,7 +115,7 @@ static void set_transfer_ui(ethQueryContractUI_t *msg, erc721_context_t *context
break;
case 2:
strlcpy(msg->title, "NFT Address", msg->titleLength);
getEthDisplayableAddress(msg->pluginSharedRO->txContent->destination,
getEthDisplayableAddress((uint8_t *)msg->item1->nft.contractAddress,
msg->msg,
msg->msgLength,
&global_sha3,
@@ -141,7 +143,7 @@ static void set_transfer_ui(ethQueryContractUI_t *msg, erc721_context_t *context
}
}
void handle_query_contract_ui(void *parameters) {
void handle_query_contract_ui_721(void *parameters) {
ethQueryContractUI_t *msg = (ethQueryContractUI_t *) parameters;
erc721_context_t *context = (erc721_context_t *) msg->pluginContext;
@@ -163,4 +165,6 @@ void handle_query_contract_ui(void *parameters) {
PRINTF("Unsupported selector index %d\n", context->selectorIndex);
break;
}
}
}
#endif // HAVE_NFT_SUPPORT