Starkex v2 integration
This commit is contained in:
committed by
TamtamHero
parent
61424e7907
commit
9854d12753
@@ -3,6 +3,7 @@
|
||||
#define APP_FLAG_DATA_ALLOWED 0x01
|
||||
#define APP_FLAG_EXTERNAL_TOKEN_NEEDED 0x02
|
||||
#define APP_FLAG_STARKWARE 0x04
|
||||
#define APP_FLAG_STARKWARE_V2 0x08
|
||||
|
||||
#define CLA 0xE0
|
||||
#define INS_GET_PUBLIC_KEY 0x02
|
||||
@@ -29,12 +30,17 @@
|
||||
#define STARKWARE_INS_GET_PUBLIC_KEY 0x02
|
||||
#define STARKWARE_INS_SIGN_MESSAGE 0x04
|
||||
#define STARKWARE_INS_PROVIDE_QUANTUM 0x08
|
||||
#define STARKWARE_INS_UNSAFE_SIGN 0x0A
|
||||
|
||||
#define P1_STARK_ORDER 0x01
|
||||
#define P1_STARK_TRANSFER 0x02
|
||||
#define P1_STARK_ORDER_V2 0x03
|
||||
#define P1_STARK_TRANSFER_V2 0x04
|
||||
#define P1_STARK_CONDITIONAL_TRANSFER 0x05
|
||||
|
||||
#define STARK_ORDER_TYPE 0
|
||||
#define STARK_TRANSFER_TYPE 1
|
||||
#define STARK_CONDITIONAL_TRANSFER_TYPE 2
|
||||
|
||||
#endif
|
||||
|
||||
@@ -64,6 +70,7 @@ void handleSetEth2WinthdrawalIndex(uint8_t p1, uint8_t p2, uint8_t *dataBuffer,
|
||||
void handleStarkwareGetPublicKey(uint8_t p1, uint8_t p2, uint8_t *dataBuffer, uint16_t dataLength, unsigned int *flags, unsigned int *tx);
|
||||
void handleStarkwareSignMessage(uint8_t p1, uint8_t p2, uint8_t *dataBuffer, uint16_t dataLength, unsigned int *flags, unsigned int *tx);
|
||||
void handleStarkwareProvideQuantum(uint8_t p1, uint8_t p2, uint8_t *dataBuffer, uint16_t dataLength, unsigned int *flags, unsigned int *tx);
|
||||
void handleStarkwareUnsafeSign(uint8_t p1, uint8_t p2, uint8_t *dataBuffer, uint16_t dataLength, unsigned int *flags, unsigned int *tx);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -57,10 +57,13 @@ int eth_plugin_perform_init(uint8_t *contractAddress, ethPluginInitContract_t *i
|
||||
}
|
||||
for (j=0; ((j<INTERNAL_ETH_PLUGINS[i].num_selectors) && (contractAddress != NULL)); j++) {
|
||||
if (memcmp(init->selector, PIC(selectors[j]), SELECTOR_SIZE) == 0) {
|
||||
strcpy(dataContext.tokenContext.pluginName, INTERNAL_ETH_PLUGINS[i].alias);
|
||||
dataContext.tokenContext.pluginAvailable = 1;
|
||||
contractAddress = NULL;
|
||||
break;
|
||||
if ((INTERNAL_ETH_PLUGINS[i].availableCheck == NULL) ||
|
||||
((PluginAvailableCheck)PIC(INTERNAL_ETH_PLUGINS[i].availableCheck))()) {
|
||||
strcpy(dataContext.tokenContext.pluginName, INTERNAL_ETH_PLUGINS[i].alias);
|
||||
dataContext.tokenContext.pluginAvailable = 1;
|
||||
contractAddress = NULL;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
#include "eth_plugin_internal.h"
|
||||
|
||||
bool erc20_plugin_available_check(void);
|
||||
bool erc721_plugin_available_check(void);
|
||||
|
||||
void erc20_plugin_call(int message, void *parameters);
|
||||
void erc721_plugin_call(int message, void *parameters);
|
||||
void compound_plugin_call(int message, void *parameters);
|
||||
#ifdef HAVE_STARKWARE
|
||||
void starkware_plugin_call(int message, void *parameters);
|
||||
@@ -16,6 +20,13 @@ const uint8_t* const ERC20_SELECTORS[NUM_ERC20_SELECTORS] = {
|
||||
ERC20_TRANSFER_SELECTOR, ERC20_APPROVE_SELECTOR
|
||||
};
|
||||
|
||||
|
||||
static const uint8_t const ERC721_APPROVE_SELECTOR[SELECTOR_SIZE] = { 0x09, 0x5e, 0xa7, 0xb3 };
|
||||
|
||||
const uint8_t* const ERC721_SELECTORS[NUM_ERC721_SELECTORS] = {
|
||||
ERC721_APPROVE_SELECTOR
|
||||
};
|
||||
|
||||
static const uint8_t const COMPOUND_REDEEM_UNDERLYING_SELECTOR[SELECTOR_SIZE] = { 0x85, 0x2a, 0x12, 0xe3 };
|
||||
static const uint8_t const COMPOUND_REDEEM_SELECTOR[SELECTOR_SIZE] = { 0xdb, 0x00, 0x6a, 0x75 };
|
||||
static const uint8_t const COMPOUND_MINT_SELECTOR[SELECTOR_SIZE] = { 0xa0, 0x71, 0x2d, 0x68 };
|
||||
@@ -38,22 +49,32 @@ const uint8_t* const ETH2_SELECTORS[NUM_ETH2_SELECTORS] = {
|
||||
|
||||
#ifdef HAVE_STARKWARE
|
||||
|
||||
static const uint8_t const STARKWARE_REGISTER_ID[SELECTOR_SIZE] = { 0x76, 0x57, 0x18, 0xd7 };
|
||||
static const uint8_t const STARKWARE_DEPOSIT_TOKEN_ID[SELECTOR_SIZE] = { 0x00, 0xae, 0xef, 0x8a };
|
||||
static const uint8_t const STARKWARE_DEPOSIT_ETH_ID[SELECTOR_SIZE] = { 0xe2, 0xbb, 0xb1, 0x58 };
|
||||
static const uint8_t const STARKWARE_DEPOSIT_CANCEL_ID[SELECTOR_SIZE] = { 0xc7, 0xfb, 0x11, 0x7c };
|
||||
static const uint8_t const STARKWARE_DEPOSIT_RECLAIM_ID[SELECTOR_SIZE] = { 0x4e, 0xab, 0x38, 0xf4 };
|
||||
static const uint8_t const STARKWARE_WITHDRAW_ID[SELECTOR_SIZE] = { 0x2e, 0x1a, 0x7d, 0x4d };
|
||||
static const uint8_t const STARKWARE_FULL_WITHDRAWAL_ID[SELECTOR_SIZE] = { 0x27, 0x6d, 0xd1, 0xde };
|
||||
static const uint8_t const STARKWARE_FREEZE_ID[SELECTOR_SIZE] = { 0xb9, 0x10, 0x72, 0x09 };
|
||||
static const uint8_t const STARKWARE_REGISTER_ID[SELECTOR_SIZE] = { 0xdd, 0x24, 0x14, 0xd4 };
|
||||
static const uint8_t const STARKWARE_DEPOSIT_TOKEN_ID[SELECTOR_SIZE] = { 0x25, 0x05, 0xc3, 0xd9 };
|
||||
static const uint8_t const STARKWARE_DEPOSIT_ETH_ID[SELECTOR_SIZE] = { 0x00, 0xae, 0xef, 0x8a };
|
||||
static const uint8_t const STARKWARE_DEPOSIT_CANCEL_ID[SELECTOR_SIZE] = { 0x7d, 0xf7, 0xdc, 0x04 };
|
||||
static const uint8_t const STARKWARE_DEPOSIT_RECLAIM_ID[SELECTOR_SIZE] = { 0xae, 0x87, 0x38, 0x16 };
|
||||
static const uint8_t const STARKWARE_WITHDRAW_ID[SELECTOR_SIZE] = { 0x44, 0x1a, 0x3e, 0x70 };
|
||||
static const uint8_t const STARKWARE_FULL_WITHDRAWAL_ID[SELECTOR_SIZE] = { 0xa9, 0x33, 0x10, 0xc4 };
|
||||
static const uint8_t const STARKWARE_FREEZE_ID[SELECTOR_SIZE] = { 0x93, 0xc1, 0xe4, 0x66 };
|
||||
static const uint8_t const STARKWARE_ESCAPE_ID[SELECTOR_SIZE] = { 0x9e, 0x3a, 0xda, 0xc4 };
|
||||
static const uint8_t const STARKWARE_VERIFY_ESCAPE_ID[SELECTOR_SIZE] = { 0x2d, 0xd5, 0x30, 0x06 };
|
||||
|
||||
static const uint8_t const STARKWARE_WITHDRAW_TO_ID[SELECTOR_SIZE] = { 0x14, 0xcd, 0x70, 0xe4 };
|
||||
static const uint8_t const STARKWARE_DEPOSIT_NFT_ID[SELECTOR_SIZE] = { 0xae, 0x1c, 0xdd, 0xe6 };
|
||||
static const uint8_t const STARKWARE_DEPOSIT_NFT_RECLAIM_ID[SELECTOR_SIZE] = { 0xfc, 0xb0, 0x58, 0x22 };
|
||||
static const uint8_t const STARKWARE_WITHDRAW_AND_MINT_ID[SELECTOR_SIZE] = { 0xd9, 0x14, 0x43, 0xb7 };
|
||||
static const uint8_t const STARKWARE_WITHDRAW_NFT_ID[SELECTOR_SIZE] = { 0x01, 0x9b, 0x41, 0x7a };
|
||||
static const uint8_t const STARKWARE_WITHDRAW_NFT_TO_ID[SELECTOR_SIZE] = { 0xeb, 0xef, 0x0f, 0xd0 };
|
||||
|
||||
|
||||
const uint8_t* const STARKWARE_SELECTORS[NUM_STARKWARE_SELECTORS] = {
|
||||
STARKWARE_REGISTER_ID, STARKWARE_DEPOSIT_TOKEN_ID, STARKWARE_DEPOSIT_ETH_ID,
|
||||
STARKWARE_DEPOSIT_CANCEL_ID, STARKWARE_DEPOSIT_RECLAIM_ID, STARKWARE_WITHDRAW_ID,
|
||||
STARKWARE_FULL_WITHDRAWAL_ID, STARKWARE_FREEZE_ID, STARKWARE_ESCAPE_ID,
|
||||
STARKWARE_VERIFY_ESCAPE_ID
|
||||
STARKWARE_VERIFY_ESCAPE_ID, STARKWARE_WITHDRAW_TO_ID, STARKWARE_DEPOSIT_NFT_ID,
|
||||
STARKWARE_DEPOSIT_NFT_RECLAIM_ID, STARKWARE_WITHDRAW_AND_MINT_ID, STARKWARE_WITHDRAW_NFT_ID,
|
||||
STARKWARE_WITHDRAW_NFT_TO_ID
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -62,6 +83,7 @@ const uint8_t* const STARKWARE_SELECTORS[NUM_STARKWARE_SELECTORS] = {
|
||||
|
||||
const internalEthPlugin_t const INTERNAL_ETH_PLUGINS[] = {
|
||||
{
|
||||
erc20_plugin_available_check,
|
||||
ERC20_SELECTORS,
|
||||
NUM_ERC20_SELECTORS,
|
||||
"-erc20",
|
||||
@@ -69,6 +91,15 @@ const internalEthPlugin_t const INTERNAL_ETH_PLUGINS[] = {
|
||||
},
|
||||
|
||||
{
|
||||
erc721_plugin_available_check,
|
||||
ERC721_SELECTORS,
|
||||
NUM_ERC721_SELECTORS,
|
||||
"-er721",
|
||||
erc721_plugin_call
|
||||
},
|
||||
|
||||
{
|
||||
NULL,
|
||||
COMPOUND_SELECTORS,
|
||||
NUM_COMPOUND_SELECTORS,
|
||||
"-cmpd",
|
||||
@@ -78,6 +109,7 @@ const internalEthPlugin_t const INTERNAL_ETH_PLUGINS[] = {
|
||||
#ifdef HAVE_ETH2
|
||||
|
||||
{
|
||||
NULL,
|
||||
ETH2_SELECTORS,
|
||||
NUM_ETH2_SELECTORS,
|
||||
"-eth2",
|
||||
@@ -89,6 +121,7 @@ const internalEthPlugin_t const INTERNAL_ETH_PLUGINS[] = {
|
||||
#ifdef HAVE_STARKWARE
|
||||
|
||||
{
|
||||
NULL,
|
||||
STARKWARE_SELECTORS,
|
||||
10,
|
||||
"-strk",
|
||||
@@ -98,6 +131,7 @@ const internalEthPlugin_t const INTERNAL_ETH_PLUGINS[] = {
|
||||
#endif
|
||||
|
||||
{
|
||||
NULL,
|
||||
NULL,
|
||||
0,
|
||||
"",
|
||||
|
||||
@@ -4,7 +4,10 @@
|
||||
|
||||
#define SELECTOR_SIZE 4
|
||||
|
||||
typedef bool (*PluginAvailableCheck)(void);
|
||||
|
||||
typedef struct internalEthPlugin_t {
|
||||
PluginAvailableCheck availableCheck;
|
||||
const uint8_t **selectors;
|
||||
uint8_t num_selectors;
|
||||
char alias[7];
|
||||
@@ -14,6 +17,9 @@ typedef struct internalEthPlugin_t {
|
||||
#define NUM_ERC20_SELECTORS 2
|
||||
extern const uint8_t* const ERC20_SELECTORS[NUM_ERC20_SELECTORS];
|
||||
|
||||
#define NUM_ERC721_SELECTORS 1
|
||||
extern const uint8_t* const ERC721_SELECTORS[NUM_ERC721_SELECTORS];
|
||||
|
||||
#define NUM_COMPOUND_SELECTORS 4
|
||||
extern const uint8_t* const COMPOUND_SELECTORS[NUM_COMPOUND_SELECTORS];
|
||||
|
||||
@@ -26,7 +32,7 @@ extern const uint8_t* const ETH2_SELECTORS[NUM_ETH2_SELECTORS];
|
||||
|
||||
#ifdef HAVE_STARKWARE
|
||||
|
||||
#define NUM_STARKWARE_SELECTORS 10
|
||||
#define NUM_STARKWARE_SELECTORS 16
|
||||
extern const uint8_t* const STARKWARE_SELECTORS[NUM_STARKWARE_SELECTORS];
|
||||
|
||||
#endif
|
||||
|
||||
@@ -390,6 +390,9 @@ void handleApdu(unsigned int *flags, unsigned int *tx) {
|
||||
case STARKWARE_INS_PROVIDE_QUANTUM:
|
||||
handleStarkwareProvideQuantum(G_io_apdu_buffer[OFFSET_P1], G_io_apdu_buffer[OFFSET_P2], G_io_apdu_buffer + OFFSET_CDATA, G_io_apdu_buffer[OFFSET_LC], flags, tx);
|
||||
break;
|
||||
case STARKWARE_INS_UNSAFE_SIGN:
|
||||
handleStarkwareUnsafeSign(G_io_apdu_buffer[OFFSET_P1], G_io_apdu_buffer[OFFSET_P2], G_io_apdu_buffer + OFFSET_CDATA, G_io_apdu_buffer[OFFSET_LC], flags, tx);
|
||||
break;
|
||||
default:
|
||||
THROW(0x6D00);
|
||||
break;
|
||||
|
||||
@@ -30,6 +30,21 @@ typedef struct internalStorage_t {
|
||||
uint8_t initialized;
|
||||
} internalStorage_t;
|
||||
|
||||
#ifdef HAVE_STARKWARE
|
||||
|
||||
typedef enum starkQuantumType_e {
|
||||
|
||||
STARK_QUANTUM_LEGACY = 0x00,
|
||||
STARK_QUANTUM_ETH,
|
||||
STARK_QUANTUM_ERC20,
|
||||
STARK_QUANTUM_ERC721,
|
||||
STARK_QUANTUM_MINTABLE_ERC20,
|
||||
STARK_QUANTUM_MINTABLE_ERC721
|
||||
|
||||
} starkQuantumType_e;
|
||||
|
||||
#endif
|
||||
|
||||
typedef struct tokenContext_t {
|
||||
char pluginName[PLUGIN_ID_LENGTH];
|
||||
uint8_t pluginAvailable;
|
||||
@@ -46,7 +61,9 @@ typedef struct tokenContext_t {
|
||||
|
||||
#ifdef HAVE_STARKWARE
|
||||
uint8_t quantum[32];
|
||||
uint8_t mintingBlob[32];
|
||||
uint8_t quantumIndex;
|
||||
uint8_t quantumType;
|
||||
#endif
|
||||
|
||||
} tokenContext_t;
|
||||
@@ -101,6 +118,11 @@ typedef struct starkContext_t {
|
||||
uint8_t w1[32];
|
||||
uint8_t w2[32];
|
||||
uint8_t w3[32];
|
||||
uint8_t w4[32];
|
||||
uint8_t conditional;
|
||||
uint8_t transferDestination[32];
|
||||
uint8_t fact[32];
|
||||
uint8_t conditionAddress[20];
|
||||
} starkContext_t;
|
||||
|
||||
#endif
|
||||
|
||||
@@ -9,21 +9,21 @@ static unsigned char const C_cx_Stark256_n[] = {
|
||||
//n: 0x0800000000000010ffffffffffffffffb781126dcae7b2321e66a241adc64d2f
|
||||
0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xb7, 0x81, 0x12, 0x6d, 0xca, 0xe7, 0xb2, 0x32, 0x1e, 0x66, 0xa2, 0x41, 0xad, 0xc6, 0x4d, 0x2f};
|
||||
|
||||
|
||||
// C_cx_secp256k1_n - (C_cx_secp256k1_n % C_cx_Stark256_n)
|
||||
static unsigned char const STARK_DERIVE_BIAS[] = {
|
||||
0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x0e, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf7,
|
||||
0x38, 0xa1, 0x3b, 0x4b, 0x92, 0x0e, 0x94, 0x11, 0xae, 0x6d, 0xa5, 0xf4, 0x0b, 0x03, 0x58, 0xb1
|
||||
};
|
||||
};
|
||||
|
||||
void starkDerivePrivateKey(uint32_t *bip32Path, uint32_t bip32PathLength, uint8_t *privateKeyData) {
|
||||
#if 0
|
||||
// Sanity check
|
||||
void starkDerivePrivateKey(uint32_t *bip32Path, uint32_t bip32PathLength, uint8_t *privateKeyData) {
|
||||
#if 0
|
||||
// Sanity check
|
||||
if (bip32Path[0] != STARK_BIP32_PATH_0) {
|
||||
PRINTF("Invalid Stark derivation path %d\n", bip32Path[0]);
|
||||
THROW(0x6a80);
|
||||
}
|
||||
os_perso_derive_node_bip32(CX_CURVE_256K1, bip32Path, bip32PathLength, privateKeyData, NULL);
|
||||
os_perso_derive_node_bip32(CX_CURVE_256K1, bip32Path, bip32PathLength, privateKeyData, NULL);
|
||||
PRINTF("Private key before processing %.*H\n", 32, privateKeyData);
|
||||
// TODO - support additional schemes
|
||||
cx_math_modm(privateKeyData, 32, C_cx_Stark256_n, 32);
|
||||
@@ -31,30 +31,32 @@ void starkDerivePrivateKey(uint32_t *bip32Path, uint32_t bip32PathLength, uint8_
|
||||
#else
|
||||
uint8_t tmp[33];
|
||||
uint8_t index = 0;
|
||||
// Sanity check
|
||||
// Sanity check
|
||||
if ((bip32PathLength < 2) || (bip32Path[0] != STARK_BIP32_PATH_0) || (bip32Path[1] != STARK_BIP32_PATH_1)) {
|
||||
PRINTF("Invalid Stark derivation path %d %d\n", bip32Path[0], bip32Path[1]);
|
||||
THROW(0x6a80);
|
||||
}
|
||||
os_perso_derive_node_bip32(CX_CURVE_256K1, bip32Path, bip32PathLength, tmp, NULL);
|
||||
os_perso_derive_node_bip32(CX_CURVE_256K1, bip32Path, bip32PathLength, tmp, NULL);
|
||||
PRINTF("Private key before processing %.*H\n", 32, tmp);
|
||||
for(;;) {
|
||||
tmp[32] = index;
|
||||
cx_hash_sha256(tmp, 33, privateKeyData, 32);
|
||||
PRINTF("Key hash %.*H\n", 32, privateKeyData);
|
||||
if (cx_math_cmp(privateKeyData, STARK_DERIVE_BIAS, 32) < 0) {
|
||||
cx_math_modm(privateKeyData, 32, C_cx_Stark256_n, 32);
|
||||
break;
|
||||
}
|
||||
index++;
|
||||
}
|
||||
PRINTF("Key result %.*H\n", 32, privateKeyData);
|
||||
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
void stark_get_amount_string(uint8_t *contractAddress, uint8_t *quantum256, uint8_t *amount64, char *tmp100, char *target100) {
|
||||
uint256_t amountPre, quantum, amount;
|
||||
uint8_t decimals;
|
||||
char *ticker = (char*)PIC(chainConfig->coinName);
|
||||
uint8_t decimals;
|
||||
char *ticker = (char*)PIC(chainConfig->coinName);
|
||||
|
||||
PRINTF("stark_get_amount_string %.*H\n", 20, contractAddress);
|
||||
|
||||
|
||||
@@ -18,8 +18,4 @@ typedef unsigned char ECPoint[EC_POINT_SIZE];
|
||||
void pedersen(FieldElement res, /* out */
|
||||
FieldElement a, FieldElement b);
|
||||
|
||||
int stark_sign(uint8_t *signautre, /* out */
|
||||
uint8_t *privateKeyData, FieldElement token1,
|
||||
FieldElement token2, FieldElement msg);
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifdef HAVE_STARKWARE
|
||||
|
||||
#include "stark_crypto.h"
|
||||
#include "shared_context.h"
|
||||
#include "ethUtils.h"
|
||||
|
||||
#include "os_io_seproxyhal.h"
|
||||
@@ -81,7 +82,7 @@ void pedersen(FieldElement res, /* out */
|
||||
ECPoint hash;
|
||||
|
||||
memcpy(hash, PEDERSEN_SHIFT, sizeof(hash));
|
||||
|
||||
|
||||
accum_ec_mul(&hash, a, 1, 1);
|
||||
accum_ec_mul(&hash, a+1, FIELD_ELEMENT_SIZE-1, 0);
|
||||
accum_ec_mul(&hash, b, 1, 3);
|
||||
@@ -94,15 +95,23 @@ int stark_sign(uint8_t *signature, /* out */
|
||||
uint8_t *privateKeyData,
|
||||
FieldElement token1,
|
||||
FieldElement token2,
|
||||
FieldElement msg) {
|
||||
FieldElement msg,
|
||||
FieldElement condition) {
|
||||
unsigned int info = 0;
|
||||
FieldElement hash;
|
||||
cx_ecfp_private_key_t privateKey;
|
||||
PRINTF("Stark sign msg w1 %.*H\n", 32, token1);
|
||||
PRINTF("Stark sign msg w2 %.*H\n", 32, token2);
|
||||
PRINTF("Stark sign w3 %.*H\n", 32, msg);
|
||||
if (condition != NULL) {
|
||||
PRINTF("Stark sign w4 %.*H\n", 32, condition);
|
||||
}
|
||||
pedersen(hash, token1, token2);
|
||||
PRINTF("Pedersen hash 1 %.*H\n", 32, hash);
|
||||
if (condition != NULL) {
|
||||
pedersen(hash, hash, condition);
|
||||
PRINTF("Pedersen hash condition %.*H\n", 32, hash);
|
||||
}
|
||||
pedersen(hash, hash, msg);
|
||||
PRINTF("Pedersen hash 2 %.*H\n", 32, hash);
|
||||
cx_ecfp_init_private_key(CX_CURVE_Stark256, privateKeyData, 32, &privateKey);
|
||||
@@ -114,27 +123,90 @@ int stark_sign(uint8_t *signature, /* out */
|
||||
}
|
||||
|
||||
// ERC20Token(address)
|
||||
static const uint8_t ERC20_SELECTOR[] = { 0xf4, 0x72, 0x61, 0xb0 };
|
||||
static const uint8_t ERC20_SELECTOR[] = { 0xf4, 0x72, 0x61, 0xb0 };
|
||||
// ETH()
|
||||
static const uint8_t ETH_SELECTOR[] = { 0x83, 0x22, 0xff, 0xf2 };
|
||||
// ERC721Token(address, uint256)
|
||||
static const uint8_t ERC721_SELECTOR[] = { 0x02, 0x57, 0x17, 0x92 };
|
||||
// MintableERC20Token(address)
|
||||
static const uint8_t MINTABLE_ERC20_SELECTOR[] = { 0x68, 0x64, 0x6e, 0x2d };
|
||||
// MintableERC721Token(address,uint256)
|
||||
static const uint8_t MINTABLE_ERC721_SELECTOR[] = { 0xb8, 0xb8, 0x66, 0x72 };
|
||||
static const char NFT_ASSET_ID_PREFIX[] = { 'N', 'F', 'T', ':', 0 };
|
||||
static const char MINTABLE_ASSET_ID_PREFIX[] = { 'M', 'I', 'N', 'T', 'A', 'B', 'L', 'E', ':', 0 };
|
||||
|
||||
void compute_token_id(cx_sha3_t *sha3, uint8_t *contractAddress, uint8_t *quantum, uint8_t *output) {
|
||||
void compute_token_id(cx_sha3_t *sha3, uint8_t *contractAddress, uint8_t quantumType, uint8_t *quantum, uint8_t *mintingBlob, bool assetTypeOnly, uint8_t *output) {
|
||||
uint8_t tmp[36];
|
||||
cx_keccak_init(sha3, 256);
|
||||
if ((contractAddress != NULL) && (!allzeroes(contractAddress, 20))) {
|
||||
const uint8_t *selector = NULL;
|
||||
switch(quantumType) {
|
||||
case STARK_QUANTUM_ERC20:
|
||||
case STARK_QUANTUM_LEGACY:
|
||||
selector = ERC20_SELECTOR;
|
||||
break;
|
||||
case STARK_QUANTUM_ERC721:
|
||||
selector = ERC721_SELECTOR;
|
||||
break;
|
||||
case STARK_QUANTUM_MINTABLE_ERC20:
|
||||
selector = MINTABLE_ERC20_SELECTOR;
|
||||
break;
|
||||
case STARK_QUANTUM_MINTABLE_ERC721:
|
||||
selector = MINTABLE_ERC721_SELECTOR;
|
||||
break;
|
||||
default:
|
||||
PRINTF("Unsupported quantum type %d\n", quantumType);
|
||||
return;
|
||||
}
|
||||
PRINTF("compute_token_id for %.*H\n", 20, contractAddress);
|
||||
os_memset(tmp, 0, sizeof(tmp));
|
||||
os_memmove(tmp, ERC20_SELECTOR, 4);
|
||||
os_memmove(tmp + 16, contractAddress, 20);
|
||||
cx_hash((cx_hash_t*)sha3, 0, tmp, sizeof(tmp), NULL, 0);
|
||||
memset(tmp, 0, sizeof(tmp));
|
||||
memmove(tmp, selector, 4);
|
||||
memmove(tmp + 16, contractAddress, 20);
|
||||
cx_hash((cx_hash_t*)sha3, 0, tmp, sizeof(tmp), NULL, 0);
|
||||
}
|
||||
else {
|
||||
PRINTF("compute_token_id for ETH\n");
|
||||
cx_hash((cx_hash_t*)sha3, 0, ETH_SELECTOR, sizeof(ETH_SELECTOR), NULL, 0);
|
||||
cx_hash((cx_hash_t*)sha3, 0, ETH_SELECTOR, sizeof(ETH_SELECTOR), NULL, 0);
|
||||
}
|
||||
if ((quantumType == STARK_QUANTUM_ERC721) || (quantumType == STARK_QUANTUM_MINTABLE_ERC721)) {
|
||||
memset(tmp, 0, 32);
|
||||
tmp[31] = 1;
|
||||
PRINTF("compute_token_id quantum %.*H\n", 32, tmp);
|
||||
cx_hash((cx_hash_t*)sha3, CX_LAST, tmp, 32, output, 32);
|
||||
}
|
||||
else {
|
||||
PRINTF("compute_token_id quantum %.*H\n", 32, quantum);
|
||||
cx_hash((cx_hash_t*)sha3, CX_LAST, quantum, 32, output, 32);
|
||||
}
|
||||
if (!assetTypeOnly && ((quantumType != STARK_QUANTUM_LEGACY) &&
|
||||
(quantumType != STARK_QUANTUM_ETH) &&
|
||||
(quantumType != STARK_QUANTUM_ERC20))) {
|
||||
const char *prefix = NULL;
|
||||
output[0] &= 0x03;
|
||||
cx_keccak_init(sha3, 256);
|
||||
switch(quantumType) {
|
||||
case STARK_QUANTUM_ERC721:
|
||||
prefix = NFT_ASSET_ID_PREFIX;
|
||||
break;
|
||||
case STARK_QUANTUM_MINTABLE_ERC20:
|
||||
case STARK_QUANTUM_MINTABLE_ERC721:
|
||||
prefix = MINTABLE_ASSET_ID_PREFIX;
|
||||
break;
|
||||
default:
|
||||
PRINTF("Unsupported non default quantum type %d\n", quantumType);
|
||||
return;
|
||||
}
|
||||
cx_hash((cx_hash_t*)sha3, 0, (const uint8_t*)prefix, strlen(prefix), NULL, 0);
|
||||
cx_hash((cx_hash_t*)sha3, 0, output, 32, NULL, 0);
|
||||
cx_hash((cx_hash_t*)sha3, CX_LAST, mintingBlob, 32, output, 32);
|
||||
}
|
||||
if (!assetTypeOnly && ((quantumType == STARK_QUANTUM_MINTABLE_ERC20) || (quantumType == STARK_QUANTUM_MINTABLE_ERC721))) {
|
||||
output[0] = 0x04;
|
||||
output[1] = 0x00;
|
||||
}
|
||||
else {
|
||||
output[0] &= 0x03;
|
||||
}
|
||||
PRINTF("compute_token_id quantum %.*H\n", 32, quantum);
|
||||
cx_hash((cx_hash_t*)sha3, CX_LAST, quantum, 32, output, 32);
|
||||
output[0] &= 0x03;
|
||||
PRINTF("compute_token_id computed token %.*H\n", 32, output);
|
||||
}
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
#include "cx.h"
|
||||
#include "stark_crypto.h"
|
||||
|
||||
void compute_token_id(cx_sha3_t *sha3, uint8_t *contractAddress, uint8_t *quantum, uint8_t *output);
|
||||
void compute_token_id(cx_sha3_t *sha3, uint8_t *contractAddress, uint8_t quantumType, uint8_t *quantum, uint8_t *mintingBlob, bool assetTypeOnly, uint8_t *output);
|
||||
|
||||
void starkDerivePrivateKey(uint32_t *bip32Path, uint32_t bip32PathLength, uint8_t *privateKeyData);
|
||||
|
||||
@@ -19,7 +19,8 @@ int stark_sign(uint8_t *signature, /* out */
|
||||
uint8_t *privateKeyData,
|
||||
FieldElement token1,
|
||||
FieldElement token2,
|
||||
FieldElement msg);
|
||||
FieldElement msg,
|
||||
FieldElement condition);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
const tokenDefinition_t const TOKENS_EXTRA[NUM_TOKENS_EXTRA] = {
|
||||
|
||||
{{0x4c,0x5f,0x66,0x59,0x61,0x97,0xa8,0x6f,0xb3,0x0a,0x24,0x35,0xe2,0xef,0x4d,0xdc,0xb3,0x93,0x42,0xc9}, "tUSDT ", 6},
|
||||
{{0x1c,0x0f,0x17,0x43,0x67,0x40,0xbf,0xb9,0x2c,0x10,0x70,0xee,0x86,0x32,0x2d,0xe8,0x90,0x83,0x7c,0x6a}, "tUSDT ", 6},
|
||||
{{0xcd,0x07,0x7a,0xbe,0xdd,0x83,0x1a,0x34,0x43,0xff,0xbe,0x24,0xfb,0x76,0x66,0x1b,0xbb,0x17,0xeb,0x69}, "tZRX ", 18},
|
||||
{{0x40,0xd8,0x97,0x85,0x00,0xbf,0x68,0x32,0x4a,0x51,0x53,0x3c,0xd6,0xa2,0x1e,0x3e,0x59,0xbe,0x32,0x4a}, "tBTC ", 18},
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ typedef struct tokenDefinition_t {
|
||||
|
||||
#ifdef HAVE_TOKENS_EXTRA_LIST
|
||||
|
||||
#define NUM_TOKENS_EXTRA 3
|
||||
#define NUM_TOKENS_EXTRA 4
|
||||
|
||||
extern tokenDefinition_t const TOKENS_EXTRA[NUM_TOKENS_EXTRA];
|
||||
|
||||
|
||||
@@ -34,6 +34,10 @@ extern const ux_flow_step_t * const ux_stark_transfer_flow [];
|
||||
|
||||
extern const ux_flow_step_t * const ux_stark_self_transfer_flow [];
|
||||
|
||||
extern const ux_flow_step_t * const ux_stark_transfer_conditional_flow [];
|
||||
|
||||
extern const ux_flow_step_t * const ux_stark_self_transfer_conditional_flow [];
|
||||
|
||||
extern const ux_flow_step_t * const ux_approval_starkware_register_flow [];
|
||||
|
||||
extern const ux_flow_step_t * const ux_approval_starkware_deposit_flow [];
|
||||
@@ -46,5 +50,7 @@ extern const ux_flow_step_t * const ux_approval_starkware_escape_flow [];
|
||||
|
||||
extern const ux_flow_step_t * const ux_approval_starkware_verify_escape_flow [];
|
||||
|
||||
extern const ux_flow_step_t * const ux_stark_unsafe_sign_flow [];
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
Reference in New Issue
Block a user