1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-16 11:28:14 +00:00

Merge pull request #217 from alepop/restore-lisk-sign-verify-message

Lisk sign/verify functionality
This commit is contained in:
Jan Pochyla 2018-06-07 15:55:21 +02:00 committed by GitHub
commit f72667c761
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 145 additions and 2 deletions

View File

@ -1,6 +1,6 @@
from trezor.wire import register, protobuf_workflow from trezor.wire import register, protobuf_workflow
from trezor.messages.wire_types import \ from trezor.messages.wire_types import \
LiskGetAddress, LiskSignTx, LiskGetPublicKey LiskGetAddress, LiskSignTx, LiskGetPublicKey, LiskSignMessage, LiskVerifyMessage
def dispatch_LiskGetAddress(*args, **kwargs): def dispatch_LiskGetAddress(*args, **kwargs):
@ -18,7 +18,19 @@ def dispatch_LiskSignTx(*args, **kwargs):
return lisk_sign_tx(*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(): def boot():
register(LiskGetPublicKey, protobuf_workflow, dispatch_LiskGetPublicKey) register(LiskGetPublicKey, protobuf_workflow, dispatch_LiskGetPublicKey)
register(LiskGetAddress, protobuf_workflow, dispatch_LiskGetAddress) register(LiskGetAddress, protobuf_workflow, dispatch_LiskGetAddress)
register(LiskSignMessage, protobuf_workflow, dispatch_LiskSignMessage)
register(LiskVerifyMessage, protobuf_workflow, dispatch_LiskVerifyMessage)
register(LiskSignTx, protobuf_workflow, dispatch_LiskSignTx) register(LiskSignTx, protobuf_workflow, dispatch_LiskSignTx)

View File

@ -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)

View File

@ -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')

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -88,6 +88,9 @@ LiskGetAddress = 114
LiskAddress = 115 LiskAddress = 115
LiskSignTx = 116 LiskSignTx = 116
LiskSignedTx = 117 LiskSignedTx = 117
LiskSignMessage = 118
LiskMessageSignature = 119
LiskVerifyMessage = 120
LiskGetPublicKey = 121 LiskGetPublicKey = 121
LiskPublicKey = 122 LiskPublicKey = 122
StellarGetPublicKey = 200 StellarGetPublicKey = 200

View File

@ -88,6 +88,9 @@ LiskGetAddress = 114
LiskAddress = 115 LiskAddress = 115
LiskSignTx = 116 LiskSignTx = 116
LiskSignedTx = 117 LiskSignedTx = 117
LiskSignMessage = 118
LiskMessageSignature = 119
LiskVerifyMessage = 120
LiskGetPublicKey = 121 LiskGetPublicKey = 121
LiskPublicKey = 122 LiskPublicKey = 122
StellarGetPublicKey = 200 StellarGetPublicKey = 200

@ -1 +1 @@
Subproject commit 018eebac7e64ed082486d746d78d279fe815c65d Subproject commit babc60a48ec95df8de0ddd11b9d7e24b0e7e1d46