2021-12-08 09:10:58 +00:00
|
|
|
from typing import TYPE_CHECKING
|
2018-07-03 14:20:26 +00:00
|
|
|
from ubinascii import hexlify
|
|
|
|
|
2021-02-23 23:55:23 +00:00
|
|
|
from trezor import utils, wire
|
2018-10-08 12:34:51 +00:00
|
|
|
from trezor.crypto.hashlib import blake256, sha256
|
2018-07-03 14:20:26 +00:00
|
|
|
|
2022-01-17 12:03:21 +00:00
|
|
|
from apps.common.writers import write_compact_size
|
2016-11-16 22:08:41 +00:00
|
|
|
|
2021-12-08 09:10:58 +00:00
|
|
|
if TYPE_CHECKING:
|
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.")
|
2022-01-17 12:03:21 +00:00
|
|
|
write_compact_size(h, len(coin.signed_message_header))
|
2020-09-15 11:03:12 +00:00
|
|
|
h.extend(coin.signed_message_header.encode())
|
2022-01-17 12:03:21 +00:00
|
|
|
write_compact_size(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
|
|
|
|
|
|
|
|
2020-12-11 11:02:28 +00:00
|
|
|
def decode_message(message: bytes) -> str:
|
2018-02-27 01:35:20 +00:00
|
|
|
try:
|
2020-12-11 11:02:28 +00:00
|
|
|
return bytes(message).decode()
|
2018-02-27 01:35:20 +00:00
|
|
|
except UnicodeError:
|
2021-09-27 10:13:51 +00:00
|
|
|
return f"hex({hexlify(message).decode()})"
|