mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-12-16 11:28:14 +00:00
embed/extmod/modtrezorcrypto: introduce CANONICAL_SIG_ETHEREUM constant
This commit is contained in:
parent
443539aedd
commit
03c681ec49
@ -62,13 +62,17 @@ STATIC mp_obj_t mod_trezorcrypto_secp256k1_publickey(size_t n_args, const mp_obj
|
|||||||
}
|
}
|
||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_secp256k1_publickey_obj, 1, 2, mod_trezorcrypto_secp256k1_publickey);
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorcrypto_secp256k1_publickey_obj, 1, 2, mod_trezorcrypto_secp256k1_publickey);
|
||||||
|
|
||||||
static int ethereum_is_canonic(uint8_t v, uint8_t signature[64])
|
static int ethereum_is_canonical(uint8_t v, uint8_t signature[64])
|
||||||
{
|
{
|
||||||
(void) signature;
|
(void)signature;
|
||||||
return (v & 2) == 0;
|
return (v & 2) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// def sign(secret_key: bytes, digest: bytes, compressed: bool = True, ethereum_canonical: bool = False) -> bytes:
|
enum {
|
||||||
|
CANONICAL_SIG_ETHEREUM = 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
/// def sign(secret_key: bytes, digest: bytes, compressed: bool = True, canonical: int = None) -> bytes:
|
||||||
/// '''
|
/// '''
|
||||||
/// Uses secret key to produce the signature of the digest.
|
/// Uses secret key to produce the signature of the digest.
|
||||||
/// '''
|
/// '''
|
||||||
@ -77,10 +81,12 @@ STATIC mp_obj_t mod_trezorcrypto_secp256k1_sign(size_t n_args, const mp_obj_t *a
|
|||||||
mp_get_buffer_raise(args[0], &sk, MP_BUFFER_READ);
|
mp_get_buffer_raise(args[0], &sk, MP_BUFFER_READ);
|
||||||
mp_get_buffer_raise(args[1], &dig, MP_BUFFER_READ);
|
mp_get_buffer_raise(args[1], &dig, MP_BUFFER_READ);
|
||||||
bool compressed = (n_args < 3) || (args[2] == mp_const_true);
|
bool compressed = (n_args < 3) || (args[2] == mp_const_true);
|
||||||
bool ethereum_canonical = (n_args > 3) && (args[3] == mp_const_true);
|
mp_int_t canonical = (n_args > 3) ? mp_obj_get_int(args[3]) : 0;
|
||||||
int (*is_canonical)(uint8_t by, uint8_t sig[64]) = NULL;
|
int (*is_canonical)(uint8_t by, uint8_t sig[64]) = NULL;
|
||||||
if (ethereum_canonical) {
|
switch (canonical) {
|
||||||
is_canonical = ethereum_is_canonic;
|
case CANONICAL_SIG_ETHEREUM:
|
||||||
|
is_canonical = ethereum_is_canonical;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
if (sk.len != 32) {
|
if (sk.len != 32) {
|
||||||
mp_raise_ValueError("Invalid length of secret key");
|
mp_raise_ValueError("Invalid length of secret key");
|
||||||
@ -186,6 +192,7 @@ STATIC const mp_rom_map_elem_t mod_trezorcrypto_secp256k1_globals_table[] = {
|
|||||||
{ MP_ROM_QSTR(MP_QSTR_verify), MP_ROM_PTR(&mod_trezorcrypto_secp256k1_verify_obj) },
|
{ MP_ROM_QSTR(MP_QSTR_verify), MP_ROM_PTR(&mod_trezorcrypto_secp256k1_verify_obj) },
|
||||||
{ MP_ROM_QSTR(MP_QSTR_verify_recover), MP_ROM_PTR(&mod_trezorcrypto_secp256k1_verify_recover_obj) },
|
{ MP_ROM_QSTR(MP_QSTR_verify_recover), MP_ROM_PTR(&mod_trezorcrypto_secp256k1_verify_recover_obj) },
|
||||||
{ MP_ROM_QSTR(MP_QSTR_multiply), MP_ROM_PTR(&mod_trezorcrypto_secp256k1_multiply_obj) },
|
{ MP_ROM_QSTR(MP_QSTR_multiply), MP_ROM_PTR(&mod_trezorcrypto_secp256k1_multiply_obj) },
|
||||||
|
{ MP_ROM_QSTR(MP_QSTR_CANONICAL_SIG_ETHEREUM), MP_OBJ_NEW_SMALL_INT(CANONICAL_SIG_ETHEREUM) },
|
||||||
};
|
};
|
||||||
STATIC MP_DEFINE_CONST_DICT(mod_trezorcrypto_secp256k1_globals, mod_trezorcrypto_secp256k1_globals_table);
|
STATIC MP_DEFINE_CONST_DICT(mod_trezorcrypto_secp256k1_globals, mod_trezorcrypto_secp256k1_globals_table);
|
||||||
|
|
||||||
|
@ -24,7 +24,7 @@ async def sign_message(ctx, msg):
|
|||||||
address_n = msg.address_n or ()
|
address_n = msg.address_n or ()
|
||||||
node = await seed.derive_node(ctx, address_n)
|
node = await seed.derive_node(ctx, address_n)
|
||||||
|
|
||||||
signature = secp256k1.sign(node.private_key(), message_digest(msg.message), False)
|
signature = secp256k1.sign(node.private_key(), message_digest(msg.message), False, secp256k1.CANONICAL_SIG_ETHEREUM)
|
||||||
|
|
||||||
sig = EthereumMessageSignature()
|
sig = EthereumMessageSignature()
|
||||||
sig.address = node.ethereum_pubkeyhash()
|
sig.address = node.ethereum_pubkeyhash()
|
||||||
|
@ -131,7 +131,7 @@ async def send_signature(ctx, msg: EthereumSignTx, digest):
|
|||||||
address_n = msg.address_n or ()
|
address_n = msg.address_n or ()
|
||||||
node = await seed.derive_node(ctx, address_n)
|
node = await seed.derive_node(ctx, address_n)
|
||||||
|
|
||||||
signature = secp256k1.sign(node.private_key(), digest, False, True)
|
signature = secp256k1.sign(node.private_key(), digest, False, secp256k1.CANONICAL_SIG_ETHEREUM)
|
||||||
|
|
||||||
req = EthereumTxRequest()
|
req = EthereumTxRequest()
|
||||||
req.signature_v = signature[0]
|
req.signature_v = signature[0]
|
||||||
|
Loading…
Reference in New Issue
Block a user