mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-07-15 19:18:11 +00:00
style(legacy, python): fix style
This commit is contained in:
parent
6590f0eea1
commit
3793824f00
@ -315,8 +315,9 @@ static void send_signature(void) {
|
|||||||
* using standard ethereum units.
|
* using standard ethereum units.
|
||||||
* The buffer must be at least 25 bytes.
|
* The buffer must be at least 25 bytes.
|
||||||
*/
|
*/
|
||||||
static void ethereumFormatAmount(const bignum256 *amnt, const EthereumTokenInfo *token,
|
static void ethereumFormatAmount(const bignum256 *amnt,
|
||||||
char *buf, int buflen) {
|
const EthereumTokenInfo *token, char *buf,
|
||||||
|
int buflen) {
|
||||||
bignum256 bn1e9 = {0};
|
bignum256 bn1e9 = {0};
|
||||||
bn_read_uint32(1000000000, &bn1e9);
|
bn_read_uint32(1000000000, &bn1e9);
|
||||||
const char *suffix = NULL;
|
const char *suffix = NULL;
|
||||||
@ -554,7 +555,8 @@ static bool ethereum_signing_init_common(struct signing_params *params) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void ethereum_signing_handle_erc20(struct signing_params *params, const EthereumDefinitions *defs) {
|
static void ethereum_signing_handle_erc20(struct signing_params *params,
|
||||||
|
const EthereumDefinitions *defs) {
|
||||||
if (params->has_to && ethereum_parse(params->to, params->pubkeyhash)) {
|
if (params->has_to && ethereum_parse(params->to, params->pubkeyhash)) {
|
||||||
params->pubkeyhash_set = true;
|
params->pubkeyhash_set = true;
|
||||||
} else {
|
} else {
|
||||||
@ -598,10 +600,12 @@ static bool ethereum_signing_confirm_common(
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ethereum_signing_init(const EthereumSignTx *msg, const HDNode *node, const EthereumDefinitions *defs) {
|
void ethereum_signing_init(const EthereumSignTx *msg, const HDNode *node,
|
||||||
|
const EthereumDefinitions *defs) {
|
||||||
struct signing_params params = {
|
struct signing_params params = {
|
||||||
.chain_id = defs ? defs->network.chain_id : msg->chain_id,
|
.chain_id = defs ? defs->network.chain_id : msg->chain_id,
|
||||||
.chain_suffix = defs ? defs->network.shortcut : get_ethereum_suffix(msg->chain_id),
|
.chain_suffix =
|
||||||
|
defs ? defs->network.shortcut : get_ethereum_suffix(msg->chain_id),
|
||||||
.data_length = msg->data_length,
|
.data_length = msg->data_length,
|
||||||
.data_initial_chunk_size = msg->data_initial_chunk.size,
|
.data_initial_chunk_size = msg->data_initial_chunk.size,
|
||||||
.data_initial_chunk_bytes = msg->data_initial_chunk.bytes,
|
.data_initial_chunk_bytes = msg->data_initial_chunk.bytes,
|
||||||
@ -704,10 +708,12 @@ void ethereum_signing_init(const EthereumSignTx *msg, const HDNode *node, const
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ethereum_signing_init_eip1559(const EthereumSignTxEIP1559 *msg,
|
void ethereum_signing_init_eip1559(const EthereumSignTxEIP1559 *msg,
|
||||||
const HDNode *node, const EthereumDefinitions *defs) {
|
const HDNode *node,
|
||||||
|
const EthereumDefinitions *defs) {
|
||||||
struct signing_params params = {
|
struct signing_params params = {
|
||||||
.chain_id = defs ? defs->network.chain_id : msg->chain_id,
|
.chain_id = defs ? defs->network.chain_id : msg->chain_id,
|
||||||
.chain_suffix = defs ? defs->network.shortcut : get_ethereum_suffix(msg->chain_id),
|
.chain_suffix =
|
||||||
|
defs ? defs->network.shortcut : get_ethereum_suffix(msg->chain_id),
|
||||||
|
|
||||||
.data_length = msg->data_length,
|
.data_length = msg->data_length,
|
||||||
.data_initial_chunk_size = msg->data_initial_chunk.size,
|
.data_initial_chunk_size = msg->data_initial_chunk.size,
|
||||||
@ -1062,7 +1068,8 @@ bool ethereum_path_check(uint32_t address_n_count, const uint32_t *address_n,
|
|||||||
valid = valid && (path_slip44 == 60 || path_slip44 == 1);
|
valid = valid && (path_slip44 == 60 || path_slip44 == 1);
|
||||||
} else if (defs->network.slip44 != 60 && defs->network.slip44 != 1) {
|
} else if (defs->network.slip44 != 60 && defs->network.slip44 != 1) {
|
||||||
// Allow cross-signing with Ethereum unless it's testnet.
|
// Allow cross-signing with Ethereum unless it's testnet.
|
||||||
valid = valid && (path_slip44 == defs->network.slip44 || path_slip44 == 60);
|
valid =
|
||||||
|
valid && (path_slip44 == defs->network.slip44 || path_slip44 == 60);
|
||||||
} else {
|
} else {
|
||||||
valid = valid && (path_slip44 == defs->network.slip44);
|
valid = valid && (path_slip44 == defs->network.slip44);
|
||||||
}
|
}
|
||||||
|
@ -28,9 +28,11 @@
|
|||||||
|
|
||||||
#define CHAIN_ID_UNKNOWN UINT64_MAX
|
#define CHAIN_ID_UNKNOWN UINT64_MAX
|
||||||
|
|
||||||
void ethereum_signing_init(const EthereumSignTx *msg, const HDNode *node, const EthereumDefinitions *defs);
|
void ethereum_signing_init(const EthereumSignTx *msg, const HDNode *node,
|
||||||
|
const EthereumDefinitions *defs);
|
||||||
void ethereum_signing_init_eip1559(const EthereumSignTxEIP1559 *msg,
|
void ethereum_signing_init_eip1559(const EthereumSignTxEIP1559 *msg,
|
||||||
const HDNode *node, const EthereumDefinitions *defs);
|
const HDNode *node,
|
||||||
|
const EthereumDefinitions *defs);
|
||||||
void ethereum_signing_abort(void);
|
void ethereum_signing_abort(void);
|
||||||
void ethereum_signing_txack(const EthereumTxAck *msg);
|
void ethereum_signing_txack(const EthereumTxAck *msg);
|
||||||
|
|
||||||
|
@ -34,19 +34,21 @@
|
|||||||
#include "trezor.h" // because of the "VERSTR" macro used in "fsm_sendFailureDebug" function
|
#include "trezor.h" // because of the "VERSTR" macro used in "fsm_sendFailureDebug" function
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
static const uint8_t DEFINITIONS_PUBLIC_KEY[] = " ";
|
static const uint8_t DEFINITIONS_PUBLIC_KEY[] =
|
||||||
|
" ";
|
||||||
#if DEBUG_LINK
|
#if DEBUG_LINK
|
||||||
static const uint8_t DEFINITIONS_DEV_PUBLIC_KEY[] = "\xdb\x99\x5f\xe2\x51\x69\xd1\x41\xca\xb9\xbb\xba\x92\xba\xa0\x1f\x9f\x2e\x1e\xce\x7d\xf4\xcb\x2a\xc0\x51\x90\xf3\x7f\xcc\x1f\x9d";
|
static const uint8_t DEFINITIONS_DEV_PUBLIC_KEY[] =
|
||||||
|
"\xdb\x99\x5f\xe2\x51\x69\xd1\x41\xca\xb9\xbb\xba\x92\xba\xa0\x1f\x9f\x2e"
|
||||||
|
"\x1e\xce\x7d\xf4\xcb\x2a\xc0\x51\x90\xf3\x7f\xcc\x1f\x9d";
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
#define MIN_DATA_VERSION 1
|
#define MIN_DATA_VERSION 1
|
||||||
#define FORMAT_VERSION_LENGTH 8
|
#define FORMAT_VERSION_LENGTH 8
|
||||||
#define FORMAT_VERSION (const pb_byte_t *) "trzd1\x00\x00\x00"
|
#define FORMAT_VERSION (const pb_byte_t *)"trzd1\x00\x00\x00"
|
||||||
#define MERKLE_TREE_SIGNED_ROOT_SIZE 64
|
#define MERKLE_TREE_SIGNED_ROOT_SIZE 64
|
||||||
|
|
||||||
#define HASH_DATA_BUFFER_SIZE (1 + MAX(EthereumEncodedDefinitions_size / 2, 2 * SHA256_DIGEST_LENGTH))
|
#define HASH_DATA_BUFFER_SIZE \
|
||||||
|
(1 + MAX(EthereumEncodedDefinitions_size / 2, 2 * SHA256_DIGEST_LENGTH))
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
// prefix
|
// prefix
|
||||||
@ -65,12 +67,15 @@ typedef struct {
|
|||||||
ed25519_signature signed_root_hash;
|
ed25519_signature signed_root_hash;
|
||||||
} ParsedEncodedEthereumDefinitions;
|
} ParsedEncodedEthereumDefinitions;
|
||||||
|
|
||||||
const ParsedEncodedEthereumDefinitions *_parse_encoded_EthereumDefinitions(const pb_size_t size, const pb_byte_t *bytes) {
|
const ParsedEncodedEthereumDefinitions *_parse_encoded_EthereumDefinitions(
|
||||||
|
const pb_size_t size, const pb_byte_t *bytes) {
|
||||||
static ParsedEncodedEthereumDefinitions parsed;
|
static ParsedEncodedEthereumDefinitions parsed;
|
||||||
|
|
||||||
// format version + definition type + data version + payload length + payload (at least 1B) + proof length + signed Merkle tree root hash
|
// format version + definition type + data version + payload length + payload
|
||||||
if (size < (FORMAT_VERSION_LENGTH + 1 + 4 + 2 + 1 + 1 + MERKLE_TREE_SIGNED_ROOT_SIZE)) {
|
// (at least 1B) + proof length + signed Merkle tree root hash
|
||||||
return (const ParsedEncodedEthereumDefinitions * const) NULL;
|
if (size < (FORMAT_VERSION_LENGTH + 1 + 4 + 2 + 1 + 1 +
|
||||||
|
MERKLE_TREE_SIGNED_ROOT_SIZE)) {
|
||||||
|
return (const ParsedEncodedEthereumDefinitions *const)NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
pb_size_t actual_position = 0;
|
pb_size_t actual_position = 0;
|
||||||
@ -83,57 +88,66 @@ const ParsedEncodedEthereumDefinitions *_parse_encoded_EthereumDefinitions(const
|
|||||||
parsed.data_version = read_be(&bytes[actual_position]);
|
parsed.data_version = read_be(&bytes[actual_position]);
|
||||||
actual_position += 4;
|
actual_position += 4;
|
||||||
|
|
||||||
parsed.payload_length_in_bytes = (((uint16_t)bytes[actual_position]) << 8) | (((uint16_t)bytes[actual_position+1]));
|
parsed.payload_length_in_bytes = (((uint16_t)bytes[actual_position]) << 8) |
|
||||||
|
(((uint16_t)bytes[actual_position + 1]));
|
||||||
actual_position += 2;
|
actual_position += 2;
|
||||||
|
|
||||||
if (size < actual_position - 1) {
|
if (size < actual_position - 1) {
|
||||||
return (const ParsedEncodedEthereumDefinitions * const) NULL;
|
return (const ParsedEncodedEthereumDefinitions *const)NULL;
|
||||||
}
|
}
|
||||||
parsed.payload_start = actual_position;
|
parsed.payload_start = actual_position;
|
||||||
actual_position += parsed.payload_length_in_bytes;
|
actual_position += parsed.payload_length_in_bytes;
|
||||||
|
|
||||||
if (size < actual_position - 1) {
|
if (size < actual_position - 1) {
|
||||||
return (const ParsedEncodedEthereumDefinitions * const) NULL;
|
return (const ParsedEncodedEthereumDefinitions *const)NULL;
|
||||||
}
|
}
|
||||||
parsed.proof_length = bytes[actual_position];
|
parsed.proof_length = bytes[actual_position];
|
||||||
actual_position += 1;
|
actual_position += 1;
|
||||||
|
|
||||||
if (size < actual_position - 1) {
|
if (size < actual_position - 1) {
|
||||||
return (const ParsedEncodedEthereumDefinitions * const) NULL;
|
return (const ParsedEncodedEthereumDefinitions *const)NULL;
|
||||||
}
|
}
|
||||||
parsed.proof_start = actual_position;
|
parsed.proof_start = actual_position;
|
||||||
actual_position += parsed.proof_length * SHA256_DIGEST_LENGTH;
|
actual_position += parsed.proof_length * SHA256_DIGEST_LENGTH;
|
||||||
|
|
||||||
if (size < actual_position + MERKLE_TREE_SIGNED_ROOT_SIZE - 1) {
|
if (size < actual_position + MERKLE_TREE_SIGNED_ROOT_SIZE - 1) {
|
||||||
return (const ParsedEncodedEthereumDefinitions * const) NULL;
|
return (const ParsedEncodedEthereumDefinitions *const)NULL;
|
||||||
}
|
}
|
||||||
memcpy(&parsed.signed_root_hash, &bytes[actual_position], MERKLE_TREE_SIGNED_ROOT_SIZE);
|
memcpy(&parsed.signed_root_hash, &bytes[actual_position],
|
||||||
|
MERKLE_TREE_SIGNED_ROOT_SIZE);
|
||||||
|
|
||||||
return (const ParsedEncodedEthereumDefinitions * const) &parsed;
|
return (const ParsedEncodedEthereumDefinitions *const)&parsed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool _decode_definition(const pb_size_t size, const pb_byte_t *bytes,
|
||||||
bool _decode_definition(const pb_size_t size, const pb_byte_t *bytes, const EthereumDefinitionType expected_type, void *definition) {
|
const EthereumDefinitionType expected_type,
|
||||||
|
void *definition) {
|
||||||
// parse received definition
|
// parse received definition
|
||||||
const ParsedEncodedEthereumDefinitions *parsed_def = _parse_encoded_EthereumDefinitions(size, bytes);
|
const ParsedEncodedEthereumDefinitions *parsed_def =
|
||||||
|
_parse_encoded_EthereumDefinitions(size, bytes);
|
||||||
if (!parsed_def) {
|
if (!parsed_def) {
|
||||||
fsm_sendFailure(FailureType_Failure_DataError, _("Invalid Ethereum definition."));
|
fsm_sendFailure(FailureType_Failure_DataError,
|
||||||
|
_("Invalid Ethereum definition."));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// check definition fields
|
// check definition fields
|
||||||
if (memcmp(FORMAT_VERSION, &bytes[parsed_def->format_version_start], FORMAT_VERSION_LENGTH)) {
|
if (memcmp(FORMAT_VERSION, &bytes[parsed_def->format_version_start],
|
||||||
fsm_sendFailure(FailureType_Failure_DataError, _("Used different Ethereum definition format version."));
|
FORMAT_VERSION_LENGTH)) {
|
||||||
|
fsm_sendFailure(FailureType_Failure_DataError,
|
||||||
|
_("Used different Ethereum definition format version."));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (expected_type != parsed_def->definition_type) {
|
if (expected_type != parsed_def->definition_type) {
|
||||||
fsm_sendFailure(FailureType_Failure_DataError, _("Definition of invalid type for Ethereum."));
|
fsm_sendFailure(FailureType_Failure_DataError,
|
||||||
|
_("Definition of invalid type for Ethereum."));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (MIN_DATA_VERSION > parsed_def->data_version) {
|
if (MIN_DATA_VERSION > parsed_def->data_version) {
|
||||||
fsm_sendFailure(FailureType_Failure_DataError, _("Used Ethereum definition data version too low."));
|
fsm_sendFailure(FailureType_Failure_DataError,
|
||||||
|
_("Used Ethereum definition data version too low."));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -142,15 +156,20 @@ bool _decode_definition(const pb_size_t size, const pb_byte_t *bytes, const Ethe
|
|||||||
uint8_t hash_data[HASH_DATA_BUFFER_SIZE];
|
uint8_t hash_data[HASH_DATA_BUFFER_SIZE];
|
||||||
memzero(hash_data, HASH_DATA_BUFFER_SIZE);
|
memzero(hash_data, HASH_DATA_BUFFER_SIZE);
|
||||||
// leaf hash = sha256('\x00' + leaf data)
|
// leaf hash = sha256('\x00' + leaf data)
|
||||||
memcpy(&hash_data[1], bytes, parsed_def->payload_start + parsed_def->payload_length_in_bytes);
|
memcpy(&hash_data[1], bytes,
|
||||||
sha256_Raw(hash_data, 1 + parsed_def->payload_start + parsed_def->payload_length_in_bytes, hash);
|
parsed_def->payload_start + parsed_def->payload_length_in_bytes);
|
||||||
|
sha256_Raw(
|
||||||
|
hash_data,
|
||||||
|
1 + parsed_def->payload_start + parsed_def->payload_length_in_bytes,
|
||||||
|
hash);
|
||||||
|
|
||||||
pb_size_t index = parsed_def->proof_start;
|
pb_size_t index = parsed_def->proof_start;
|
||||||
int cmp = 0;
|
int cmp = 0;
|
||||||
const void *min, *max;
|
const void *min, *max;
|
||||||
for (uint8_t i = 0; i < parsed_def->proof_length; i++) {
|
for (uint8_t i = 0; i < parsed_def->proof_length; i++) {
|
||||||
memzero(hash_data, HASH_DATA_BUFFER_SIZE);
|
memzero(hash_data, HASH_DATA_BUFFER_SIZE);
|
||||||
// node hash = sha256('\x01' + min(hash, next_proof) + max(hash, next_proof))
|
// node hash = sha256('\x01' + min(hash, next_proof) + max(hash,
|
||||||
|
// next_proof))
|
||||||
hash_data[0] = '\x01';
|
hash_data[0] = '\x01';
|
||||||
cmp = memcmp(hash, &bytes[index], SHA256_DIGEST_LENGTH);
|
cmp = memcmp(hash, &bytes[index], SHA256_DIGEST_LENGTH);
|
||||||
min = cmp < 1 ? hash : &bytes[index];
|
min = cmp < 1 ? hash : &bytes[index];
|
||||||
@ -162,20 +181,27 @@ bool _decode_definition(const pb_size_t size, const pb_byte_t *bytes, const Ethe
|
|||||||
}
|
}
|
||||||
|
|
||||||
// and verify its signature
|
// and verify its signature
|
||||||
if (ed25519_sign_open(hash, SHA256_DIGEST_LENGTH, DEFINITIONS_PUBLIC_KEY, parsed_def->signed_root_hash) != 0
|
if (ed25519_sign_open(hash, SHA256_DIGEST_LENGTH, DEFINITIONS_PUBLIC_KEY,
|
||||||
|
parsed_def->signed_root_hash) != 0
|
||||||
#if DEBUG_LINK
|
#if DEBUG_LINK
|
||||||
&& ed25519_sign_open(hash, SHA256_DIGEST_LENGTH, DEFINITIONS_DEV_PUBLIC_KEY, parsed_def->signed_root_hash) != 0
|
&&
|
||||||
|
ed25519_sign_open(hash, SHA256_DIGEST_LENGTH, DEFINITIONS_DEV_PUBLIC_KEY,
|
||||||
|
parsed_def->signed_root_hash) != 0
|
||||||
#endif
|
#endif
|
||||||
) {
|
) {
|
||||||
// invalid signature
|
// invalid signature
|
||||||
fsm_sendFailure(FailureType_Failure_DataError, _("Ethereum definition signature is invalid."));
|
fsm_sendFailure(FailureType_Failure_DataError,
|
||||||
|
_("Ethereum definition signature is invalid."));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// decode message
|
// decode message
|
||||||
const pb_msgdesc_t *fields = (expected_type == EthereumDefinitionType_NETWORK ? EthereumNetworkInfo_fields : EthereumTokenInfo_fields);
|
const pb_msgdesc_t *fields = (expected_type == EthereumDefinitionType_NETWORK
|
||||||
|
? EthereumNetworkInfo_fields
|
||||||
|
: EthereumTokenInfo_fields);
|
||||||
memzero(definition, sizeof(definition));
|
memzero(definition, sizeof(definition));
|
||||||
pb_istream_t stream = pb_istream_from_buffer(&bytes[parsed_def->payload_start], parsed_def->payload_length_in_bytes);
|
pb_istream_t stream = pb_istream_from_buffer(
|
||||||
|
&bytes[parsed_def->payload_start], parsed_def->payload_length_in_bytes);
|
||||||
bool status = pb_decode(&stream, fields, definition);
|
bool status = pb_decode(&stream, fields, definition);
|
||||||
if (!status) {
|
if (!status) {
|
||||||
// invalid message
|
// invalid message
|
||||||
@ -186,31 +212,43 @@ bool _decode_definition(const pb_size_t size, const pb_byte_t *bytes, const Ethe
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void _set_EthereumNetworkInfo_to_builtin(const uint64_t ref_chain_id, EthereumNetworkInfo *network) {
|
void _set_EthereumNetworkInfo_to_builtin(const uint64_t ref_chain_id,
|
||||||
|
EthereumNetworkInfo *network) {
|
||||||
network->chain_id = ref_chain_id;
|
network->chain_id = ref_chain_id;
|
||||||
network->slip44 = ethereum_slip44_by_chain_id(ref_chain_id);
|
network->slip44 = ethereum_slip44_by_chain_id(ref_chain_id);
|
||||||
memzero(network->shortcut, sizeof(network->shortcut));
|
memzero(network->shortcut, sizeof(network->shortcut));
|
||||||
const char *sc = get_ethereum_suffix(ref_chain_id);
|
const char *sc = get_ethereum_suffix(ref_chain_id);
|
||||||
strncpy(network->shortcut, sc, sizeof(network->shortcut) - 1);
|
strncpy(network->shortcut, sc, sizeof(network->shortcut) - 1);
|
||||||
memzero(network->name, sizeof(network->name));
|
memzero(network->name, sizeof(network->name));
|
||||||
// network->rskip60 is skipped, it is resolved where it is needed (using switch with hardcoded values)
|
// network->rskip60 is skipped, it is resolved where it is needed (using
|
||||||
|
// switch with hardcoded values)
|
||||||
}
|
}
|
||||||
|
|
||||||
bool _get_EthereumNetworkInfo(const EthereumEncodedDefinitions_encoded_network_t *encoded_network, const uint64_t ref_chain_id, EthereumNetworkInfo *network) {
|
bool _get_EthereumNetworkInfo(
|
||||||
|
const EthereumEncodedDefinitions_encoded_network_t *encoded_network,
|
||||||
|
const uint64_t ref_chain_id, EthereumNetworkInfo *network) {
|
||||||
// try to get built-in definition
|
// try to get built-in definition
|
||||||
_set_EthereumNetworkInfo_to_builtin(ref_chain_id, network);
|
_set_EthereumNetworkInfo_to_builtin(ref_chain_id, network);
|
||||||
|
|
||||||
// if we still do not have any network definition try to decode the received one
|
// if we still do not have any network definition try to decode the received
|
||||||
if (strncmp(network->shortcut, UNKNOWN_NETWORK_SHORTCUT, sizeof(network->shortcut)) == 0 && encoded_network != NULL) {
|
// one
|
||||||
if (_decode_definition(encoded_network->size, encoded_network->bytes, EthereumDefinitionType_NETWORK, (void*) network)) {
|
if (strncmp(network->shortcut, UNKNOWN_NETWORK_SHORTCUT,
|
||||||
if (ref_chain_id != CHAIN_ID_UNKNOWN && network->chain_id != ref_chain_id) {
|
sizeof(network->shortcut)) == 0 &&
|
||||||
|
encoded_network != NULL) {
|
||||||
|
if (_decode_definition(encoded_network->size, encoded_network->bytes,
|
||||||
|
EthereumDefinitionType_NETWORK, (void *)network)) {
|
||||||
|
if (ref_chain_id != CHAIN_ID_UNKNOWN &&
|
||||||
|
network->chain_id != ref_chain_id) {
|
||||||
// chain_id mismatch - error and reset definition
|
// chain_id mismatch - error and reset definition
|
||||||
fsm_sendFailure(FailureType_Failure_DataError, _("Invalid network definition - chain IDs not equal."));
|
fsm_sendFailure(FailureType_Failure_DataError,
|
||||||
|
_("Invalid network definition - chain IDs not equal."));
|
||||||
_set_EthereumNetworkInfo_to_builtin(ref_chain_id, network);
|
_set_EthereumNetworkInfo_to_builtin(ref_chain_id, network);
|
||||||
} else {
|
} else {
|
||||||
// chain_id does match the reference one (if provided) so prepend one space character to symbol, terminate it
|
// chain_id does match the reference one (if provided) so prepend one
|
||||||
// (encoded definitions does not have space prefix) and return the decoded data
|
// space character to symbol, terminate it (encoded definitions does not
|
||||||
memmove(&network->shortcut[1], &network->shortcut, sizeof(network->shortcut) - 2);
|
// have space prefix) and return the decoded data
|
||||||
|
memmove(&network->shortcut[1], &network->shortcut,
|
||||||
|
sizeof(network->shortcut) - 2);
|
||||||
network->shortcut[0] = ' ';
|
network->shortcut[0] = ' ';
|
||||||
network->shortcut[sizeof(network->shortcut) - 1] = 0;
|
network->shortcut[sizeof(network->shortcut) - 1] = 0;
|
||||||
return true;
|
return true;
|
||||||
@ -224,12 +262,16 @@ bool _get_EthereumNetworkInfo(const EthereumEncodedDefinitions_encoded_network_t
|
|||||||
return network->chain_id == CHAIN_ID_UNKNOWN ? false : true;
|
return network->chain_id == CHAIN_ID_UNKNOWN ? false : true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void _get_EthereumTokenInfo(const EthereumEncodedDefinitions_encoded_token_t *encoded_token, const uint64_t ref_chain_id, const char *ref_address, EthereumTokenInfo* token) {
|
void _get_EthereumTokenInfo(
|
||||||
|
const EthereumEncodedDefinitions_encoded_token_t *encoded_token,
|
||||||
|
const uint64_t ref_chain_id, const char *ref_address,
|
||||||
|
EthereumTokenInfo *token) {
|
||||||
EthereumTokenInfo_address_t ref_address_bytes;
|
EthereumTokenInfo_address_t ref_address_bytes;
|
||||||
const EthereumTokenInfo* builtin = UnknownToken;
|
const EthereumTokenInfo *builtin = UnknownToken;
|
||||||
|
|
||||||
// convert ref_address string to bytes
|
// convert ref_address string to bytes
|
||||||
bool address_parsed = ref_address && ethereum_parse(ref_address, ref_address_bytes.bytes);
|
bool address_parsed =
|
||||||
|
ref_address && ethereum_parse(ref_address, ref_address_bytes.bytes);
|
||||||
|
|
||||||
// try to get built-in definition
|
// try to get built-in definition
|
||||||
if (address_parsed) {
|
if (address_parsed) {
|
||||||
@ -238,11 +280,16 @@ void _get_EthereumTokenInfo(const EthereumEncodedDefinitions_encoded_token_t *en
|
|||||||
|
|
||||||
// if we do not have any token definition try to decode the received one
|
// if we do not have any token definition try to decode the received one
|
||||||
if (builtin == UnknownToken && encoded_token != NULL) {
|
if (builtin == UnknownToken && encoded_token != NULL) {
|
||||||
if (_decode_definition(encoded_token->size, encoded_token->bytes, EthereumDefinitionType_TOKEN, (void*) token)) {
|
if (_decode_definition(encoded_token->size, encoded_token->bytes,
|
||||||
|
EthereumDefinitionType_TOKEN, (void *)token)) {
|
||||||
if ((ref_chain_id == CHAIN_ID_UNKNOWN || token->chain_id == ref_chain_id) && (!address_parsed || !memcmp(token->address.bytes, ref_address_bytes.bytes, sizeof(token->address.bytes)))) {
|
if ((ref_chain_id == CHAIN_ID_UNKNOWN ||
|
||||||
// chain_id and/or address does match the reference ones (if provided) so prepend one space character to symbol, terminate it
|
token->chain_id == ref_chain_id) &&
|
||||||
// (encoded definitions does not have space prefix) and return the decoded data
|
(!address_parsed ||
|
||||||
|
!memcmp(token->address.bytes, ref_address_bytes.bytes,
|
||||||
|
sizeof(token->address.bytes)))) {
|
||||||
|
// chain_id and/or address does match the reference ones (if provided)
|
||||||
|
// so prepend one space character to symbol, terminate it (encoded
|
||||||
|
// definitions does not have space prefix) and return the decoded data
|
||||||
memmove(&token->symbol[1], &token->symbol, sizeof(token->symbol) - 2);
|
memmove(&token->symbol[1], &token->symbol, sizeof(token->symbol) - 2);
|
||||||
token->symbol[0] = ' ';
|
token->symbol[0] = ' ';
|
||||||
token->symbol[sizeof(token->symbol) - 1] = 0;
|
token->symbol[sizeof(token->symbol) - 1] = 0;
|
||||||
@ -251,8 +298,8 @@ void _get_EthereumTokenInfo(const EthereumEncodedDefinitions_encoded_token_t *en
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// decoding did not happen or failed, so we have to copy the data to the result
|
// decoding did not happen or failed, so we have to copy the data to the
|
||||||
// reset token definition
|
// result reset token definition
|
||||||
memzero(token->symbol, sizeof(token->symbol));
|
memzero(token->symbol, sizeof(token->symbol));
|
||||||
token->decimals = 0;
|
token->decimals = 0;
|
||||||
memzero(token->address.bytes, sizeof(token->address.bytes));
|
memzero(token->address.bytes, sizeof(token->address.bytes));
|
||||||
@ -263,19 +310,25 @@ void _get_EthereumTokenInfo(const EthereumEncodedDefinitions_encoded_token_t *en
|
|||||||
// copy data to token definition
|
// copy data to token definition
|
||||||
strncpy(token->symbol, builtin->symbol, sizeof(token->symbol) - 1);
|
strncpy(token->symbol, builtin->symbol, sizeof(token->symbol) - 1);
|
||||||
token->decimals = builtin->decimals;
|
token->decimals = builtin->decimals;
|
||||||
memcpy(token->address.bytes, builtin->address.bytes, sizeof(token->address.bytes));
|
memcpy(token->address.bytes, builtin->address.bytes,
|
||||||
|
sizeof(token->address.bytes));
|
||||||
token->address.size = sizeof(token->address.bytes);
|
token->address.size = sizeof(token->address.bytes);
|
||||||
token->chain_id = builtin->chain_id;
|
token->chain_id = builtin->chain_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
const EthereumDefinitions *get_EthereumDefinitions(const EthereumEncodedDefinitions_encoded_network_t *encoded_network, const EthereumEncodedDefinitions_encoded_token_t *encoded_token, const uint64_t ref_chain_id, const char *ref_address) {
|
const EthereumDefinitions *get_EthereumDefinitions(
|
||||||
|
const EthereumEncodedDefinitions_encoded_network_t *encoded_network,
|
||||||
|
const EthereumEncodedDefinitions_encoded_token_t *encoded_token,
|
||||||
|
const uint64_t ref_chain_id, const char *ref_address) {
|
||||||
static EthereumDefinitions defs;
|
static EthereumDefinitions defs;
|
||||||
|
|
||||||
if (_get_EthereumNetworkInfo(encoded_network, ref_chain_id, &defs.network)) {
|
if (_get_EthereumNetworkInfo(encoded_network, ref_chain_id, &defs.network)) {
|
||||||
// we have found network definition, we can try to load token definition
|
// we have found network definition, we can try to load token definition
|
||||||
_get_EthereumTokenInfo(encoded_token, ref_chain_id, ref_address, &defs.token);
|
_get_EthereumTokenInfo(encoded_token, ref_chain_id, ref_address,
|
||||||
|
&defs.token);
|
||||||
} else {
|
} else {
|
||||||
// if we did not find any network definition, set token definition to unknown token
|
// if we did not find any network definition, set token definition to
|
||||||
|
// unknown token
|
||||||
_get_EthereumTokenInfo(NULL, CHAIN_ID_UNKNOWN, NULL, &defs.token);
|
_get_EthereumTokenInfo(NULL, CHAIN_ID_UNKNOWN, NULL, &defs.token);
|
||||||
}
|
}
|
||||||
return &defs;
|
return &defs;
|
||||||
|
@ -27,6 +27,9 @@ typedef struct {
|
|||||||
EthereumTokenInfo token;
|
EthereumTokenInfo token;
|
||||||
} EthereumDefinitions;
|
} EthereumDefinitions;
|
||||||
|
|
||||||
const EthereumDefinitions *get_EthereumDefinitions(const EthereumEncodedDefinitions_encoded_network_t *encoded_network, const EthereumEncodedDefinitions_encoded_token_t *encoded_token, const uint64_t ref_chain_id, const char *ref_address);
|
const EthereumDefinitions *get_EthereumDefinitions(
|
||||||
|
const EthereumEncodedDefinitions_encoded_network_t *encoded_network,
|
||||||
|
const EthereumEncodedDefinitions_encoded_token_t *encoded_token,
|
||||||
|
const uint64_t ref_chain_id, const char *ref_address);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -20,8 +20,7 @@
|
|||||||
static bool fsm_ethereumCheckPath(uint32_t address_n_count,
|
static bool fsm_ethereumCheckPath(uint32_t address_n_count,
|
||||||
const uint32_t *address_n, bool pubkey_export,
|
const uint32_t *address_n, bool pubkey_export,
|
||||||
const EthereumDefinitions *defs) {
|
const EthereumDefinitions *defs) {
|
||||||
if (ethereum_path_check(address_n_count, address_n, pubkey_export,
|
if (ethereum_path_check(address_n_count, address_n, pubkey_export, defs)) {
|
||||||
defs)) {
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,10 +41,12 @@ void fsm_msgEthereumGetPublicKey(const EthereumGetPublicKey *msg) {
|
|||||||
|
|
||||||
const EthereumEncodedDefinitions_encoded_network_t *encoded_network = NULL;
|
const EthereumEncodedDefinitions_encoded_network_t *encoded_network = NULL;
|
||||||
if (msg->has_encoded_network) {
|
if (msg->has_encoded_network) {
|
||||||
encoded_network = (const EthereumEncodedDefinitions_encoded_network_t *) &msg->encoded_network;
|
encoded_network = (const EthereumEncodedDefinitions_encoded_network_t *)&msg
|
||||||
|
->encoded_network;
|
||||||
}
|
}
|
||||||
|
|
||||||
const EthereumDefinitions *defs = get_EthereumDefinitions(encoded_network, NULL, CHAIN_ID_UNKNOWN, NULL);
|
const EthereumDefinitions *defs =
|
||||||
|
get_EthereumDefinitions(encoded_network, NULL, CHAIN_ID_UNKNOWN, NULL);
|
||||||
|
|
||||||
// we use Bitcoin-like format for ETH
|
// we use Bitcoin-like format for ETH
|
||||||
const CoinInfo *coin = fsm_getCoin(true, "Bitcoin");
|
const CoinInfo *coin = fsm_getCoin(true, "Bitcoin");
|
||||||
@ -104,14 +105,19 @@ void fsm_msgEthereumSignTx(const EthereumSignTx *msg) {
|
|||||||
const EthereumEncodedDefinitions_encoded_token_t *encoded_token = NULL;
|
const EthereumEncodedDefinitions_encoded_token_t *encoded_token = NULL;
|
||||||
if (msg->has_definitions) {
|
if (msg->has_definitions) {
|
||||||
if (msg->definitions.has_encoded_network) {
|
if (msg->definitions.has_encoded_network) {
|
||||||
encoded_network = (const EthereumEncodedDefinitions_encoded_network_t *) &msg->definitions.encoded_network;
|
encoded_network =
|
||||||
|
(const EthereumEncodedDefinitions_encoded_network_t *)&msg
|
||||||
|
->definitions.encoded_network;
|
||||||
}
|
}
|
||||||
if (msg->definitions.has_encoded_token) {
|
if (msg->definitions.has_encoded_token) {
|
||||||
encoded_token = (const EthereumEncodedDefinitions_encoded_token_t *) &msg->definitions.encoded_token;
|
encoded_token = (const EthereumEncodedDefinitions_encoded_token_t *)&msg
|
||||||
|
->definitions.encoded_token;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const EthereumDefinitions *defs = get_EthereumDefinitions(encoded_network, encoded_token, msg->chain_id, msg->has_to ? msg->to : NULL);
|
const EthereumDefinitions *defs =
|
||||||
|
get_EthereumDefinitions(encoded_network, encoded_token, msg->chain_id,
|
||||||
|
msg->has_to ? msg->to : NULL);
|
||||||
|
|
||||||
if (!fsm_ethereumCheckPath(msg->address_n_count, msg->address_n, false,
|
if (!fsm_ethereumCheckPath(msg->address_n_count, msg->address_n, false,
|
||||||
defs)) {
|
defs)) {
|
||||||
@ -135,14 +141,19 @@ void fsm_msgEthereumSignTxEIP1559(const EthereumSignTxEIP1559 *msg) {
|
|||||||
const EthereumEncodedDefinitions_encoded_token_t *encoded_token = NULL;
|
const EthereumEncodedDefinitions_encoded_token_t *encoded_token = NULL;
|
||||||
if (msg->has_definitions) {
|
if (msg->has_definitions) {
|
||||||
if (msg->definitions.has_encoded_network) {
|
if (msg->definitions.has_encoded_network) {
|
||||||
encoded_network = (const EthereumEncodedDefinitions_encoded_network_t *) &msg->definitions.encoded_network;
|
encoded_network =
|
||||||
|
(const EthereumEncodedDefinitions_encoded_network_t *)&msg
|
||||||
|
->definitions.encoded_network;
|
||||||
}
|
}
|
||||||
if (msg->definitions.has_encoded_token) {
|
if (msg->definitions.has_encoded_token) {
|
||||||
encoded_token = (const EthereumEncodedDefinitions_encoded_token_t *) &msg->definitions.encoded_token;
|
encoded_token = (const EthereumEncodedDefinitions_encoded_token_t *)&msg
|
||||||
|
->definitions.encoded_token;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const EthereumDefinitions *defs = get_EthereumDefinitions(encoded_network, encoded_token, msg->chain_id, msg->has_to ? msg->to : NULL);
|
const EthereumDefinitions *defs =
|
||||||
|
get_EthereumDefinitions(encoded_network, encoded_token, msg->chain_id,
|
||||||
|
msg->has_to ? msg->to : NULL);
|
||||||
|
|
||||||
if (!fsm_ethereumCheckPath(msg->address_n_count, msg->address_n, false,
|
if (!fsm_ethereumCheckPath(msg->address_n_count, msg->address_n, false,
|
||||||
defs)) {
|
defs)) {
|
||||||
@ -172,10 +183,12 @@ void fsm_msgEthereumGetAddress(const EthereumGetAddress *msg) {
|
|||||||
|
|
||||||
const EthereumEncodedDefinitions_encoded_network_t *encoded_network = NULL;
|
const EthereumEncodedDefinitions_encoded_network_t *encoded_network = NULL;
|
||||||
if (msg->has_encoded_network) {
|
if (msg->has_encoded_network) {
|
||||||
encoded_network = (const EthereumEncodedDefinitions_encoded_network_t *) &msg->encoded_network;
|
encoded_network = (const EthereumEncodedDefinitions_encoded_network_t *)&msg
|
||||||
|
->encoded_network;
|
||||||
}
|
}
|
||||||
|
|
||||||
const EthereumDefinitions *defs = get_EthereumDefinitions(encoded_network, NULL, CHAIN_ID_UNKNOWN, NULL);
|
const EthereumDefinitions *defs =
|
||||||
|
get_EthereumDefinitions(encoded_network, NULL, CHAIN_ID_UNKNOWN, NULL);
|
||||||
|
|
||||||
if (!fsm_ethereumCheckPath(msg->address_n_count, msg->address_n, false,
|
if (!fsm_ethereumCheckPath(msg->address_n_count, msg->address_n, false,
|
||||||
defs)) {
|
defs)) {
|
||||||
@ -199,7 +212,8 @@ void fsm_msgEthereumGetAddress(const EthereumGetAddress *msg) {
|
|||||||
bool rskip60 = false;
|
bool rskip60 = false;
|
||||||
uint64_t chain_id = 0;
|
uint64_t chain_id = 0;
|
||||||
|
|
||||||
if (defs->network.chain_id != CHAIN_ID_UNKNOWN && defs->network.slip44 == slip44) {
|
if (defs->network.chain_id != CHAIN_ID_UNKNOWN &&
|
||||||
|
defs->network.slip44 == slip44) {
|
||||||
chain_id = defs->network.chain_id;
|
chain_id = defs->network.chain_id;
|
||||||
} else {
|
} else {
|
||||||
// constants from trezor-common/defs/ethereum/networks.json
|
// constants from trezor-common/defs/ethereum/networks.json
|
||||||
@ -242,10 +256,12 @@ void fsm_msgEthereumSignMessage(const EthereumSignMessage *msg) {
|
|||||||
|
|
||||||
const EthereumEncodedDefinitions_encoded_network_t *encoded_network = NULL;
|
const EthereumEncodedDefinitions_encoded_network_t *encoded_network = NULL;
|
||||||
if (msg->has_encoded_network) {
|
if (msg->has_encoded_network) {
|
||||||
encoded_network = (const EthereumEncodedDefinitions_encoded_network_t *) &msg->encoded_network;
|
encoded_network = (const EthereumEncodedDefinitions_encoded_network_t *)&msg
|
||||||
|
->encoded_network;
|
||||||
}
|
}
|
||||||
|
|
||||||
const EthereumDefinitions *defs = get_EthereumDefinitions(encoded_network, NULL, CHAIN_ID_UNKNOWN, NULL);
|
const EthereumDefinitions *defs =
|
||||||
|
get_EthereumDefinitions(encoded_network, NULL, CHAIN_ID_UNKNOWN, NULL);
|
||||||
|
|
||||||
if (!fsm_ethereumCheckPath(msg->address_n_count, msg->address_n, false,
|
if (!fsm_ethereumCheckPath(msg->address_n_count, msg->address_n, false,
|
||||||
defs)) {
|
defs)) {
|
||||||
@ -336,10 +352,12 @@ void fsm_msgEthereumSignTypedHash(const EthereumSignTypedHash *msg) {
|
|||||||
|
|
||||||
const EthereumEncodedDefinitions_encoded_network_t *encoded_network = NULL;
|
const EthereumEncodedDefinitions_encoded_network_t *encoded_network = NULL;
|
||||||
if (msg->has_encoded_network) {
|
if (msg->has_encoded_network) {
|
||||||
encoded_network = (const EthereumEncodedDefinitions_encoded_network_t *) &msg->encoded_network;
|
encoded_network = (const EthereumEncodedDefinitions_encoded_network_t *)&msg
|
||||||
|
->encoded_network;
|
||||||
}
|
}
|
||||||
|
|
||||||
const EthereumDefinitions *defs = get_EthereumDefinitions(encoded_network, NULL, CHAIN_ID_UNKNOWN, NULL);
|
const EthereumDefinitions *defs =
|
||||||
|
get_EthereumDefinitions(encoded_network, NULL, CHAIN_ID_UNKNOWN, NULL);
|
||||||
|
|
||||||
if (!fsm_ethereumCheckPath(msg->address_n_count, msg->address_n, false,
|
if (!fsm_ethereumCheckPath(msg->address_n_count, msg->address_n, false,
|
||||||
defs)) {
|
defs)) {
|
||||||
|
@ -477,7 +477,9 @@ def sign_tx(
|
|||||||
)
|
)
|
||||||
|
|
||||||
address_n = tools.parse_path(address)
|
address_n = tools.parse_path(address)
|
||||||
from_address = ethereum.get_address(client, address_n, encoded_network=defs.encoded_network)
|
from_address = ethereum.get_address(
|
||||||
|
client, address_n, encoded_network=defs.encoded_network
|
||||||
|
)
|
||||||
|
|
||||||
if token:
|
if token:
|
||||||
data = _erc20_contract(token, to_address, amount)
|
data = _erc20_contract(token, to_address, amount)
|
||||||
@ -715,7 +717,10 @@ def verify_message(
|
|||||||
@download_definitions_option
|
@download_definitions_option
|
||||||
@with_client
|
@with_client
|
||||||
def sign_typed_data_hash(
|
def sign_typed_data_hash(
|
||||||
client: "TrezorClient", address: str, domain_hash_hex: str, message_hash_hex: str,
|
client: "TrezorClient",
|
||||||
|
address: str,
|
||||||
|
domain_hash_hex: str,
|
||||||
|
message_hash_hex: str,
|
||||||
definitions_dir: pathlib.Path,
|
definitions_dir: pathlib.Path,
|
||||||
network_def: BinaryIO,
|
network_def: BinaryIO,
|
||||||
download_definitions: bool,
|
download_definitions: bool,
|
||||||
@ -736,7 +741,9 @@ def sign_typed_data_hash(
|
|||||||
download_definitions=download_definitions,
|
download_definitions=download_definitions,
|
||||||
slip44_hardened=address_n[1],
|
slip44_hardened=address_n[1],
|
||||||
)
|
)
|
||||||
ret = ethereum.sign_typed_data_hash(client, address_n, domain_hash, message_hash, defs.encoded_network)
|
ret = ethereum.sign_typed_data_hash(
|
||||||
|
client, address_n, domain_hash, message_hash, defs.encoded_network
|
||||||
|
)
|
||||||
output = {
|
output = {
|
||||||
"domain_hash": domain_hash_hex,
|
"domain_hash": domain_hash_hex,
|
||||||
"message_hash": message_hash_hex,
|
"message_hash": message_hash_hex,
|
||||||
|
Loading…
Reference in New Issue
Block a user