diff --git a/src/apps/common/signverify.py b/src/apps/common/signverify.py index 7b5c6b86b..f50379b3a 100644 --- a/src/apps/common/signverify.py +++ b/src/apps/common/signverify.py @@ -1,6 +1,7 @@ +from ubinascii import hexlify from micropython import const from trezor.crypto.hashlib import sha256 -from trezor.utils import chunks +from trezor.utils import chunks, split_words from apps.common.hash_writer import HashWriter from apps.wallet.sign_tx.signing import write_varint @@ -15,12 +16,10 @@ def message_digest(coin, message): def split_message(message): - chars_per_line = const(18) - message = stringify_message(message) - lines = chunks(message, chars_per_line) + try: + m = bytes(message).decode() + lines = split_words(m, 18) + except UnicodeError: + m = hexlify(message) + lines = chunks(m, 16) return lines - - -def stringify_message(message): - # TODO: account for invalid UTF-8 sequences - return str(message, 'utf-8') diff --git a/src/apps/wallet/sign_message.py b/src/apps/wallet/sign_message.py index 63eb14d7c..b1395ad09 100644 --- a/src/apps/wallet/sign_message.py +++ b/src/apps/wallet/sign_message.py @@ -1,5 +1,8 @@ from trezor import ui +from trezor.wire import FailureError from trezor.crypto.curve import secp256k1 +from trezor.messages.InputScriptType import SPENDADDRESS +from trezor.messages.FailureType import ProcessError from trezor.messages.MessageSignature import MessageSignature from trezor.ui.text import Text from apps.common import coins, seed @@ -11,8 +14,12 @@ async def sign_message(ctx, msg): message = msg.message address_n = msg.address_n coin_name = msg.coin_name or 'Bitcoin' + script_type = msg.script_type or 0 coin = coins.by_name(coin_name) + if script_type != SPENDADDRESS: + raise FailureError(ProcessError, 'Unsupported script type') + await confirm_sign_message(ctx, message) node = await seed.derive_node(ctx, address_n) diff --git a/src/trezor/utils.py b/src/trezor/utils.py index faeb49226..931b031e4 100644 --- a/src/trezor/utils.py +++ b/src/trezor/utils.py @@ -18,14 +18,30 @@ def unimport(genfunc): return inner +def ensure(cond): + if not cond: + raise AssertionError() + + def chunks(items, size): for i in range(0, len(items), size): yield items[i:i + size] -def ensure(cond): - if not cond: - raise AssertionError() +def split_words(sentence, width, metric=len): + line = '' + for c in sentence: + line += c + if metric(line) >= width: + c = line[-1] + if c == ' ': + yield line + line = '' + else: + yield line[:-1] + '-' + line = c + if line != '': + yield line def format_amount(amount, decimals):