1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-21 04:41:18 +00:00
trezor-firmware/core/src/apps/common/signverify.py

60 lines
1.8 KiB
Python
Raw Normal View History

2018-07-03 14:20:26 +00:00
from ubinascii import hexlify
from trezor import ui, utils, wire
from trezor.crypto.hashlib import blake256, sha256
from trezor.ui.text import Text
2018-07-03 14:20:26 +00:00
from apps.common.confirm import require_confirm
from apps.common.layout import paginate_text, split_address
from apps.common.writers import write_bitcoin_varint
2019-07-03 13:07:04 +00:00
if False:
from apps.common.coininfo import CoinInfo
2017-06-13 17:35:14 +00:00
2019-07-03 13:07:04 +00:00
def message_digest(coin: CoinInfo, message: bytes) -> bytes:
if not utils.BITCOIN_ONLY and coin.decred:
h = utils.HashWriter(blake256())
else:
h = utils.HashWriter(sha256())
if not coin.signed_message_header:
raise wire.DataError("Empty message header not allowed.")
write_bitcoin_varint(h, len(coin.signed_message_header))
h.extend(coin.signed_message_header.encode())
write_bitcoin_varint(h, len(message))
h.extend(message)
ret = h.get_digest()
if coin.sign_hash_double:
ret = sha256(ret).digest()
return ret
def decode_message(message: bytes) -> str:
try:
return bytes(message).decode()
except UnicodeError:
return "hex(%s)" % hexlify(message).decode()
async def require_confirm_sign_message(
ctx: wire.Context, coin: str, message: bytes
) -> None:
header = "Sign {} message".format(coin)
await require_confirm(ctx, paginate_text(decode_message(message), header))
async def require_confirm_verify_message(
ctx: wire.Context, address: str, coin: str, message: bytes
) -> None:
header = "Verify {} message".format(coin)
text = Text(header, new_lines=False)
text.bold("Confirm address:")
text.br()
text.mono(*split_address(address))
await require_confirm(ctx, text)
await require_confirm(
ctx,
paginate_text(decode_message(message), header, font=ui.MONO),
)