1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-07-09 08:08:09 +00:00
trezor-firmware/src/apps/ethereum/verify_message.py
Tomas Susanka 31f987e988 coins: validate derivation paths
Based on SLIP-44 ids and other checks. See docs/coins/README for info.
2018-11-12 12:10:32 +01:00

47 lines
1.4 KiB
Python

from ubinascii import hexlify
from trezor.crypto.curve import secp256k1
from trezor.crypto.hashlib import sha3_256
from trezor.messages.Success import Success
from trezor.ui.text import Text
from .address import validate_full_path
from apps.common import paths
from apps.common.confirm import require_confirm
from apps.common.layout import split_address
from apps.common.signverify import split_message
from apps.ethereum.sign_message import message_digest
async def verify_message(ctx, msg):
await paths.validate_path(ctx, validate_full_path, path=msg.address)
digest = message_digest(msg.message)
sig = bytearray([msg.signature[64]]) + msg.signature[:64]
pubkey = secp256k1.verify_recover(sig, digest)
if not pubkey:
raise ValueError("Invalid signature")
pkh = sha3_256(pubkey[1:], keccak=True).digest()[-20:]
if msg.address != pkh:
raise ValueError("Invalid signature")
address = "0x" + hexlify(msg.address).decode()
await require_confirm_verify_message(ctx, address, msg.message)
return Success(message="Message verified")
async def require_confirm_verify_message(ctx, address, message):
text = Text("Confirm address", new_lines=False)
text.mono(*split_address(address))
await require_confirm(ctx, text)
text = Text("Verify message", new_lines=False)
text.mono(*split_message(message))
await require_confirm(ctx, text)