2018-07-03 14:20:26 +00:00
|
|
|
from ubinascii import hexlify
|
|
|
|
|
2020-02-26 08:58:06 +00:00
|
|
|
from trezor import utils, wire
|
2018-10-08 12:34:51 +00:00
|
|
|
from trezor.crypto.hashlib import blake256, sha256
|
2020-02-26 08:58:06 +00:00
|
|
|
from trezor.ui.text import Text
|
2018-07-03 14:20:26 +00:00
|
|
|
|
2020-02-26 08:58:06 +00:00
|
|
|
from apps.common.confirm import require_confirm
|
|
|
|
from apps.common.layout import split_address
|
2020-05-15 18:59:06 +00:00
|
|
|
from apps.common.writers import write_bitcoin_varint
|
2016-11-16 22:08:41 +00:00
|
|
|
|
2019-07-03 13:07:04 +00:00
|
|
|
if False:
|
|
|
|
from typing import List
|
2020-09-15 11:03:12 +00:00
|
|
|
from apps.common.coininfo import CoinInfo
|
2017-06-13 17:35:14 +00:00
|
|
|
|
2019-07-03 13:07:04 +00:00
|
|
|
|
2020-09-15 11:03:12 +00:00
|
|
|
def message_digest(coin: CoinInfo, message: bytes) -> bytes:
|
2019-08-26 11:48:41 +00:00
|
|
|
if not utils.BITCOIN_ONLY and coin.decred:
|
|
|
|
h = utils.HashWriter(blake256())
|
2018-10-08 12:34:51 +00:00
|
|
|
else:
|
2019-08-26 11:48:41 +00:00
|
|
|
h = utils.HashWriter(sha256())
|
2020-06-11 07:08:50 +00:00
|
|
|
if not coin.signed_message_header:
|
|
|
|
raise wire.DataError("Empty message header not allowed.")
|
2020-05-15 18:59:06 +00:00
|
|
|
write_bitcoin_varint(h, len(coin.signed_message_header))
|
2020-09-15 11:03:12 +00:00
|
|
|
h.extend(coin.signed_message_header.encode())
|
2020-05-15 18:59:06 +00:00
|
|
|
write_bitcoin_varint(h, len(message))
|
2016-11-16 22:08:41 +00:00
|
|
|
h.extend(message)
|
2018-07-01 14:22:52 +00:00
|
|
|
ret = h.get_digest()
|
|
|
|
if coin.sign_hash_double:
|
|
|
|
ret = sha256(ret).digest()
|
|
|
|
return ret
|
2018-02-06 13:07:46 +00:00
|
|
|
|
|
|
|
|
2019-07-03 13:07:04 +00:00
|
|
|
def split_message(message: bytes) -> List[str]:
|
2018-02-27 01:35:20 +00:00
|
|
|
try:
|
|
|
|
m = bytes(message).decode()
|
2018-08-30 13:34:34 +00:00
|
|
|
words = m.split(" ")
|
2018-02-27 01:35:20 +00:00
|
|
|
except UnicodeError:
|
2018-09-19 17:19:52 +00:00
|
|
|
m = "hex(%s)" % hexlify(message).decode()
|
2018-08-30 13:34:34 +00:00
|
|
|
words = [m]
|
|
|
|
return words
|
2020-02-26 08:58:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def require_confirm_sign_message(
|
2020-08-11 15:10:39 +00:00
|
|
|
ctx: wire.Context, coin: str, message: bytes
|
2020-02-26 08:58:06 +00:00
|
|
|
) -> None:
|
2020-08-11 15:10:39 +00:00
|
|
|
header = "Sign {} message".format(coin)
|
2020-09-15 11:03:12 +00:00
|
|
|
message_lines = split_message(message)
|
2020-02-26 08:58:06 +00:00
|
|
|
text = Text(header, new_lines=False)
|
2020-09-15 11:03:12 +00:00
|
|
|
text.normal(*message_lines)
|
2020-02-26 08:58:06 +00:00
|
|
|
await require_confirm(ctx, text)
|
|
|
|
|
|
|
|
|
|
|
|
async def require_confirm_verify_message(
|
2020-08-11 15:10:39 +00:00
|
|
|
ctx: wire.Context, address: str, coin: str, message: bytes
|
2020-02-26 08:58:06 +00:00
|
|
|
) -> None:
|
2020-08-11 15:10:39 +00:00
|
|
|
header = "Verify {} message".format(coin)
|
|
|
|
text = Text(header, new_lines=False)
|
|
|
|
text.bold("Confirm address:")
|
|
|
|
text.br()
|
2020-02-26 08:58:06 +00:00
|
|
|
text.mono(*split_address(address))
|
|
|
|
await require_confirm(ctx, text)
|
|
|
|
|
|
|
|
text = Text(header, new_lines=False)
|
|
|
|
text.mono(*split_message(message))
|
|
|
|
await require_confirm(ctx, text)
|