diff --git a/extmod/modtrezorcrypto/modtrezorcrypto-nist256p1.h b/extmod/modtrezorcrypto/modtrezorcrypto-nist256p1.h index 02b90bbf4..1a25e39a0 100644 --- a/extmod/modtrezorcrypto/modtrezorcrypto-nist256p1.h +++ b/extmod/modtrezorcrypto/modtrezorcrypto-nist256p1.h @@ -70,7 +70,7 @@ STATIC mp_obj_t mod_TrezorCrypto_Nist256p1_sign(size_t n_args, const mp_obj_t *a mp_buffer_info_t sk, dig; mp_get_buffer_raise(args[1], &sk, MP_BUFFER_READ); mp_get_buffer_raise(args[2], &dig, MP_BUFFER_READ); - bool compressed = n_args > 3 && args[3] == mp_const_true; + bool compressed = n_args < 4 || args[3] == mp_const_true; if (sk.len != 32) { mp_raise_ValueError("Invalid length of secret key"); } diff --git a/extmod/modtrezorcrypto/modtrezorcrypto-secp256k1.h b/extmod/modtrezorcrypto/modtrezorcrypto-secp256k1.h index 3f8ed2412..5205c7dc0 100644 --- a/extmod/modtrezorcrypto/modtrezorcrypto-secp256k1.h +++ b/extmod/modtrezorcrypto/modtrezorcrypto-secp256k1.h @@ -70,7 +70,7 @@ STATIC mp_obj_t mod_TrezorCrypto_Secp256k1_sign(size_t n_args, const mp_obj_t *a mp_buffer_info_t sk, dig; mp_get_buffer_raise(args[1], &sk, MP_BUFFER_READ); mp_get_buffer_raise(args[2], &dig, MP_BUFFER_READ); - bool compressed = n_args > 3 && args[3] == mp_const_true; + bool compressed = n_args < 4 || args[3] == mp_const_true; if (sk.len != 32) { mp_raise_ValueError("Invalid length of secret key"); } diff --git a/src/apps/wallet/layout_sign_message.py b/src/apps/wallet/layout_sign_message.py index 3ea036009..b641cc525 100644 --- a/src/apps/wallet/layout_sign_message.py +++ b/src/apps/wallet/layout_sign_message.py @@ -3,14 +3,37 @@ from trezor.utils import unimport @unimport -async def layout_sign_message(message, session_id): - from trezor.messages.Success import Success +async def layout_sign_message(msg, session_id): + from trezor.messages.MessageSignature import MessageSignature + from trezor.crypto.hashlib import sha256 + from trezor.crypto.curve import secp256k1 + from ..common.signtx import node_derive, HashWriter, write_varint + from ..common.seed import get_root_node + from ..common import coins ui.display.clear() ui.display.text(10, 30, 'Signing message', ui.BOLD, ui.LIGHT_GREEN, ui.BLACK) - ui.display.text(10, 60, message.message, ui.MONO, ui.WHITE, ui.BLACK) + ui.display.text(10, 60, msg.message, ui.MONO, ui.WHITE, ui.BLACK) - # TODO + address_n = msg.address_n + message = msg.message + coin_name = getattr(msg, 'coin_name', 'Bitcoin') + coin = coins.by_name(coin_name) - return Success(message='Signed') + root = await get_root_node(session_id) + node = node_derive(root, address_n) + seckey = node.private_key() + address = node.address(coin.address_type) + + h = HashWriter(sha256) + write_varint(h, len(coin.signed_message_header)) + h.extend(coin.signed_message_header) + write_varint(h, len(message)) + h.extend(message) + + digest = sha256(h.getvalue()).digest() + + signature = secp256k1.sign(seckey, digest) + + return MessageSignature(address=address, signature=signature)