1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-07-15 11:08:08 +00:00

style(legacy, python): fix style

This commit is contained in:
Martin Novak 2022-10-05 15:01:39 +02:00
parent 6590f0eea1
commit 3793824f00
6 changed files with 197 additions and 107 deletions

View File

@ -315,8 +315,9 @@ static void send_signature(void) {
* using standard ethereum units.
* The buffer must be at least 25 bytes.
*/
static void ethereumFormatAmount(const bignum256 *amnt, const EthereumTokenInfo *token,
char *buf, int buflen) {
static void ethereumFormatAmount(const bignum256 *amnt,
const EthereumTokenInfo *token, char *buf,
int buflen) {
bignum256 bn1e9 = {0};
bn_read_uint32(1000000000, &bn1e9);
const char *suffix = NULL;
@ -554,7 +555,8 @@ static bool ethereum_signing_init_common(struct signing_params *params) {
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)) {
params->pubkeyhash_set = true;
} else {
@ -568,7 +570,7 @@ static void ethereum_signing_handle_erc20(struct signing_params *params, const E
memcmp(params->data_initial_chunk_bytes,
"\xa9\x05\x9c\xbb\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
16) == 0) {
params->token = defs ? &defs->token : UnknownToken;
params->token = defs ? &defs->token : UnknownToken;
}
}
@ -598,19 +600,21 @@ static bool ethereum_signing_confirm_common(
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 = {
.chain_id = defs ? defs->network.chain_id : msg->chain_id,
.chain_suffix = defs ? defs->network.shortcut : get_ethereum_suffix(msg->chain_id),
.data_length = msg->data_length,
.data_initial_chunk_size = msg->data_initial_chunk.size,
.data_initial_chunk_bytes = msg->data_initial_chunk.bytes,
.chain_id = defs ? defs->network.chain_id : msg->chain_id,
.chain_suffix =
defs ? defs->network.shortcut : get_ethereum_suffix(msg->chain_id),
.data_length = msg->data_length,
.data_initial_chunk_size = msg->data_initial_chunk.size,
.data_initial_chunk_bytes = msg->data_initial_chunk.bytes,
.has_to = msg->has_to,
.to = msg->to,
.has_to = msg->has_to,
.to = msg->to,
.value_size = msg->value.size,
.value_bytes = msg->value.bytes,
.value_size = msg->value.size,
.value_bytes = msg->value.bytes,
};
eip1559 = false;
@ -704,20 +708,22 @@ void ethereum_signing_init(const EthereumSignTx *msg, const HDNode *node, const
}
void ethereum_signing_init_eip1559(const EthereumSignTxEIP1559 *msg,
const HDNode *node, const EthereumDefinitions *defs) {
const HDNode *node,
const EthereumDefinitions *defs) {
struct signing_params params = {
.chain_id = defs ? defs->network.chain_id : msg->chain_id,
.chain_suffix = defs ? defs->network.shortcut : get_ethereum_suffix(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),
.data_length = msg->data_length,
.data_initial_chunk_size = msg->data_initial_chunk.size,
.data_initial_chunk_bytes = msg->data_initial_chunk.bytes,
.data_length = msg->data_length,
.data_initial_chunk_size = msg->data_initial_chunk.size,
.data_initial_chunk_bytes = msg->data_initial_chunk.bytes,
.has_to = msg->has_to,
.to = msg->to,
.has_to = msg->has_to,
.to = msg->to,
.value_size = msg->value.size,
.value_bytes = msg->value.bytes,
.value_size = msg->value.size,
.value_bytes = msg->value.bytes,
};
eip1559 = true;
@ -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);
} else if (defs->network.slip44 != 60 && defs->network.slip44 != 1) {
// 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 {
valid = valid && (path_slip44 == defs->network.slip44);
}

View File

@ -28,9 +28,11 @@
#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,
const HDNode *node, const EthereumDefinitions *defs);
const HDNode *node,
const EthereumDefinitions *defs);
void ethereum_signing_abort(void);
void ethereum_signing_txack(const EthereumTxAck *msg);

View File

@ -31,22 +31,24 @@
#include "messages.h"
#include "pb.h"
#include "pb_decode.h"
#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"
static const uint8_t DEFINITIONS_PUBLIC_KEY[] = " ";
static const uint8_t DEFINITIONS_PUBLIC_KEY[] =
" ";
#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
#define MIN_DATA_VERSION 1
#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 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 {
// prefix
@ -65,12 +67,15 @@ typedef struct {
ed25519_signature signed_root_hash;
} 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;
// format version + definition type + data version + payload length + payload (at least 1B) + proof length + signed Merkle tree root hash
if (size < (FORMAT_VERSION_LENGTH + 1 + 4 + 2 + 1 + 1 + MERKLE_TREE_SIGNED_ROOT_SIZE)) {
return (const ParsedEncodedEthereumDefinitions * const) NULL;
// format version + definition type + data version + payload length + payload
// (at least 1B) + proof length + signed Merkle tree root hash
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;
@ -83,57 +88,66 @@ const ParsedEncodedEthereumDefinitions *_parse_encoded_EthereumDefinitions(const
parsed.data_version = read_be(&bytes[actual_position]);
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;
if (size < actual_position - 1) {
return (const ParsedEncodedEthereumDefinitions * const) NULL;
return (const ParsedEncodedEthereumDefinitions *const)NULL;
}
parsed.payload_start = actual_position;
actual_position += parsed.payload_length_in_bytes;
if (size < actual_position - 1) {
return (const ParsedEncodedEthereumDefinitions * const) NULL;
return (const ParsedEncodedEthereumDefinitions *const)NULL;
}
parsed.proof_length = bytes[actual_position];
actual_position += 1;
if (size < actual_position - 1) {
return (const ParsedEncodedEthereumDefinitions * const) NULL;
return (const ParsedEncodedEthereumDefinitions *const)NULL;
}
parsed.proof_start = actual_position;
actual_position += parsed.proof_length * SHA256_DIGEST_LENGTH;
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, const EthereumDefinitionType expected_type, void *definition) {
bool _decode_definition(const pb_size_t size, const pb_byte_t *bytes,
const EthereumDefinitionType expected_type,
void *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) {
fsm_sendFailure(FailureType_Failure_DataError, _("Invalid Ethereum definition."));
fsm_sendFailure(FailureType_Failure_DataError,
_("Invalid Ethereum definition."));
return false;
}
// check definition fields
if (memcmp(FORMAT_VERSION, &bytes[parsed_def->format_version_start], FORMAT_VERSION_LENGTH)) {
fsm_sendFailure(FailureType_Failure_DataError, _("Used different Ethereum definition format version."));
if (memcmp(FORMAT_VERSION, &bytes[parsed_def->format_version_start],
FORMAT_VERSION_LENGTH)) {
fsm_sendFailure(FailureType_Failure_DataError,
_("Used different Ethereum definition format version."));
return false;
}
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;
}
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;
}
@ -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];
memzero(hash_data, HASH_DATA_BUFFER_SIZE);
// leaf hash = sha256('\x00' + leaf data)
memcpy(&hash_data[1], bytes, 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);
memcpy(&hash_data[1], bytes,
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;
int cmp = 0;
const void *min, *max;
for (uint8_t i = 0; i < parsed_def->proof_length; i++) {
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';
cmp = memcmp(hash, &bytes[index], SHA256_DIGEST_LENGTH);
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
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
&& 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
) {
// invalid signature
fsm_sendFailure(FailureType_Failure_DataError, _("Ethereum definition signature is invalid."));
fsm_sendFailure(FailureType_Failure_DataError,
_("Ethereum definition signature is invalid."));
return false;
}
// 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));
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);
if (!status) {
// invalid message
@ -186,31 +212,43 @@ bool _decode_definition(const pb_size_t size, const pb_byte_t *bytes, const Ethe
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->slip44 = ethereum_slip44_by_chain_id(ref_chain_id);
memzero(network->shortcut, sizeof(network->shortcut));
const char *sc = get_ethereum_suffix(ref_chain_id);
strncpy(network->shortcut, sc, sizeof(network->shortcut) - 1);
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
_set_EthereumNetworkInfo_to_builtin(ref_chain_id, network);
// if we still do not have any network definition try to decode the received one
if (strncmp(network->shortcut, UNKNOWN_NETWORK_SHORTCUT, 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) {
// if we still do not have any network definition try to decode the received
// one
if (strncmp(network->shortcut, UNKNOWN_NETWORK_SHORTCUT,
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
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);
} else {
// chain_id does match the reference one (if provided) so prepend one space character to symbol, terminate it
// (encoded definitions does not have space prefix) and return the decoded data
memmove(&network->shortcut[1], &network->shortcut, sizeof(network->shortcut) - 2);
// chain_id does match the reference one (if provided) so prepend one
// space character to symbol, terminate it (encoded definitions does not
// have space prefix) and return the decoded data
memmove(&network->shortcut[1], &network->shortcut,
sizeof(network->shortcut) - 2);
network->shortcut[0] = ' ';
network->shortcut[sizeof(network->shortcut) - 1] = 0;
return true;
@ -224,12 +262,16 @@ bool _get_EthereumNetworkInfo(const EthereumEncodedDefinitions_encoded_network_t
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;
const EthereumTokenInfo* builtin = UnknownToken;
const EthereumTokenInfo *builtin = UnknownToken;
// 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
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 (builtin == UnknownToken && encoded_token != NULL) {
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)))) {
// 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
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)))) {
// 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);
token->symbol[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
// reset token definition
// decoding did not happen or failed, so we have to copy the data to the
// result reset token definition
memzero(token->symbol, sizeof(token->symbol));
token->decimals = 0;
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
strncpy(token->symbol, builtin->symbol, sizeof(token->symbol) - 1);
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->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;
if (_get_EthereumNetworkInfo(encoded_network, ref_chain_id, &defs.network)) {
// 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 {
// 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);
}
return &defs;

View File

@ -27,6 +27,9 @@ typedef struct {
EthereumTokenInfo token;
} 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

View File

@ -20,8 +20,7 @@
static bool fsm_ethereumCheckPath(uint32_t address_n_count,
const uint32_t *address_n, bool pubkey_export,
const EthereumDefinitions *defs) {
if (ethereum_path_check(address_n_count, address_n, pubkey_export,
defs)) {
if (ethereum_path_check(address_n_count, address_n, pubkey_export, defs)) {
return true;
}
@ -42,10 +41,12 @@ void fsm_msgEthereumGetPublicKey(const EthereumGetPublicKey *msg) {
const EthereumEncodedDefinitions_encoded_network_t *encoded_network = NULL;
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
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;
if (msg->has_definitions) {
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) {
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,
defs)) {
@ -135,14 +141,19 @@ void fsm_msgEthereumSignTxEIP1559(const EthereumSignTxEIP1559 *msg) {
const EthereumEncodedDefinitions_encoded_token_t *encoded_token = NULL;
if (msg->has_definitions) {
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) {
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,
defs)) {
@ -172,10 +183,12 @@ void fsm_msgEthereumGetAddress(const EthereumGetAddress *msg) {
const EthereumEncodedDefinitions_encoded_network_t *encoded_network = NULL;
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,
defs)) {
@ -199,7 +212,8 @@ void fsm_msgEthereumGetAddress(const EthereumGetAddress *msg) {
bool rskip60 = false;
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;
} else {
// 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;
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,
defs)) {
@ -336,10 +352,12 @@ void fsm_msgEthereumSignTypedHash(const EthereumSignTypedHash *msg) {
const EthereumEncodedDefinitions_encoded_network_t *encoded_network = NULL;
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,
defs)) {

View File

@ -477,7 +477,9 @@ def sign_tx(
)
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:
data = _erc20_contract(token, to_address, amount)
@ -715,7 +717,10 @@ def verify_message(
@download_definitions_option
@with_client
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,
network_def: BinaryIO,
download_definitions: bool,
@ -736,7 +741,9 @@ def sign_typed_data_hash(
download_definitions=download_definitions,
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 = {
"domain_hash": domain_hash_hex,
"message_hash": message_hash_hex,