diff --git a/src/apps/lisk/__init__.py b/src/apps/lisk/__init__.py index c82515e268..ae7e94332d 100644 --- a/src/apps/lisk/__init__.py +++ b/src/apps/lisk/__init__.py @@ -1,6 +1,6 @@ from trezor.wire import register, protobuf_workflow from trezor.messages.wire_types import \ - LiskGetAddress, LiskSignTx, LiskGetPublicKey + LiskGetAddress, LiskSignTx, LiskGetPublicKey, LiskSignMessage, LiskVerifyMessage def dispatch_LiskGetAddress(*args, **kwargs): @@ -18,7 +18,19 @@ def dispatch_LiskSignTx(*args, **kwargs): return lisk_sign_tx(*args, **kwargs) +def dispatch_LiskSignMessage(*args, **kwargs): + from .sign_message import lisk_sign_message + return lisk_sign_message(*args, **kwargs) + + +def dispatch_LiskVerifyMessage(*args, **kwargs): + from .verify_message import lisk_verify_message + return lisk_verify_message(*args, **kwargs) + + def boot(): register(LiskGetPublicKey, protobuf_workflow, dispatch_LiskGetPublicKey) register(LiskGetAddress, protobuf_workflow, dispatch_LiskGetAddress) + register(LiskSignMessage, protobuf_workflow, dispatch_LiskSignMessage) + register(LiskVerifyMessage, protobuf_workflow, dispatch_LiskVerifyMessage) register(LiskSignTx, protobuf_workflow, dispatch_LiskSignTx) diff --git a/src/apps/lisk/sign_message.py b/src/apps/lisk/sign_message.py new file mode 100644 index 0000000000..65dfaf29cc --- /dev/null +++ b/src/apps/lisk/sign_message.py @@ -0,0 +1,44 @@ +from apps.common import seed +from apps.common.confirm import require_confirm +from apps.common.signverify import split_message +from apps.wallet.sign_tx.signing import write_varint +from trezor import ui +from trezor.crypto.curve import ed25519 +from trezor.crypto.hashlib import sha256 +from trezor.messages.LiskMessageSignature import LiskMessageSignature +from trezor.ui.text import Text +from trezor.utils import HashWriter + +from .helpers import LISK_CURVE + + +def message_digest(message): + h = HashWriter(sha256) + signed_message_header = 'Lisk Signed Message:\n' + write_varint(h, len(signed_message_header)) + h.extend(signed_message_header) + write_varint(h, len(message)) + h.extend(message) + return sha256(h.get_digest()).digest() + + +async def lisk_sign_message(ctx, msg): + message = msg.message + address_n = msg.address_n or () + + await require_confirm_sign_message(ctx, message) + + node = await seed.derive_node(ctx, address_n, LISK_CURVE) + seckey = node.private_key() + pubkey = node.public_key() + pubkey = pubkey[1:] # skip ed25519 pubkey marker + + signature = ed25519.sign(seckey, message_digest(message)) + + return LiskMessageSignature(public_key=pubkey, signature=signature) + + +async def require_confirm_sign_message(ctx, message): + message = split_message(message) + content = Text('Sign Lisk message', ui.ICON_DEFAULT, max_lines=5, *message) + await require_confirm(ctx, content) diff --git a/src/apps/lisk/verify_message.py b/src/apps/lisk/verify_message.py new file mode 100644 index 0000000000..53632f0898 --- /dev/null +++ b/src/apps/lisk/verify_message.py @@ -0,0 +1,19 @@ +from apps.wallet.verify_message import require_confirm_verify_message +from trezor import wire +from trezor.crypto.curve import ed25519 +from trezor.messages.Success import Success + +from .helpers import get_address_from_public_key +from .sign_message import message_digest + + +async def lisk_verify_message(ctx, msg): + digest = message_digest(msg.message) + verified = ed25519.verify(msg.public_key, msg.signature, digest) + if not verified: + raise wire.ProcessError('Invalid signature') + + address = get_address_from_public_key(msg.public_key) + await require_confirm_verify_message(ctx, address, msg.message) + + return Success(message='Message verified') diff --git a/src/trezor/messages/LiskMessageSignature.py b/src/trezor/messages/LiskMessageSignature.py new file mode 100644 index 0000000000..c6b98bc6a8 --- /dev/null +++ b/src/trezor/messages/LiskMessageSignature.py @@ -0,0 +1,18 @@ +# Automatically generated by pb2py +import protobuf as p + + +class LiskMessageSignature(p.MessageType): + MESSAGE_WIRE_TYPE = 119 + FIELDS = { + 1: ('public_key', p.BytesType, 0), + 2: ('signature', p.BytesType, 0), + } + + def __init__( + self, + public_key: bytes = None, + signature: bytes = None + ) -> None: + self.public_key = public_key + self.signature = signature diff --git a/src/trezor/messages/LiskSignMessage.py b/src/trezor/messages/LiskSignMessage.py new file mode 100644 index 0000000000..0f1ff67840 --- /dev/null +++ b/src/trezor/messages/LiskSignMessage.py @@ -0,0 +1,23 @@ +# Automatically generated by pb2py +import protobuf as p +if __debug__: + try: + from typing import List + except ImportError: + List = None + + +class LiskSignMessage(p.MessageType): + MESSAGE_WIRE_TYPE = 118 + FIELDS = { + 1: ('address_n', p.UVarintType, p.FLAG_REPEATED), + 2: ('message', p.BytesType, 0), + } + + def __init__( + self, + address_n: List[int] = None, + message: bytes = None + ) -> None: + self.address_n = address_n if address_n is not None else [] + self.message = message diff --git a/src/trezor/messages/LiskVerifyMessage.py b/src/trezor/messages/LiskVerifyMessage.py new file mode 100644 index 0000000000..7a83fc7de0 --- /dev/null +++ b/src/trezor/messages/LiskVerifyMessage.py @@ -0,0 +1,21 @@ +# Automatically generated by pb2py +import protobuf as p + + +class LiskVerifyMessage(p.MessageType): + MESSAGE_WIRE_TYPE = 120 + FIELDS = { + 1: ('public_key', p.BytesType, 0), + 2: ('signature', p.BytesType, 0), + 3: ('message', p.BytesType, 0), + } + + def __init__( + self, + public_key: bytes = None, + signature: bytes = None, + message: bytes = None + ) -> None: + self.public_key = public_key + self.signature = signature + self.message = message diff --git a/src/trezor/messages/MessageType.py b/src/trezor/messages/MessageType.py index 66b2787581..bed0c35415 100644 --- a/src/trezor/messages/MessageType.py +++ b/src/trezor/messages/MessageType.py @@ -88,6 +88,9 @@ LiskGetAddress = 114 LiskAddress = 115 LiskSignTx = 116 LiskSignedTx = 117 +LiskSignMessage = 118 +LiskMessageSignature = 119 +LiskVerifyMessage = 120 LiskGetPublicKey = 121 LiskPublicKey = 122 StellarGetPublicKey = 200 diff --git a/src/trezor/messages/wire_types.py b/src/trezor/messages/wire_types.py index 66b2787581..bed0c35415 100644 --- a/src/trezor/messages/wire_types.py +++ b/src/trezor/messages/wire_types.py @@ -88,6 +88,9 @@ LiskGetAddress = 114 LiskAddress = 115 LiskSignTx = 116 LiskSignedTx = 117 +LiskSignMessage = 118 +LiskMessageSignature = 119 +LiskVerifyMessage = 120 LiskGetPublicKey = 121 LiskPublicKey = 122 StellarGetPublicKey = 200 diff --git a/vendor/trezor-common b/vendor/trezor-common index 018eebac7e..babc60a48e 160000 --- a/vendor/trezor-common +++ b/vendor/trezor-common @@ -1 +1 @@ -Subproject commit 018eebac7e64ed082486d746d78d279fe815c65d +Subproject commit babc60a48ec95df8de0ddd11b9d7e24b0e7e1d46