1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-22 07:28:10 +00:00

trezor.utils: rework split_words, use it in CipherKeyValue and Sign/Verify Message layouts

This commit is contained in:
Pavol Rusnak 2018-02-27 03:33:59 +01:00
parent 9c7ddba217
commit 2854583b52
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
4 changed files with 18 additions and 17 deletions

View File

@ -1,5 +1,7 @@
from ubinascii import hexlify
from trezor.crypto.hashlib import sha256
from trezor import ui
from trezor.ui.text import TEXT_MARGIN_LEFT
from trezor.utils import chunks, split_words
from apps.common.hash_writer import HashWriter
from apps.wallet.sign_tx.signing import write_varint
@ -17,7 +19,7 @@ def message_digest(coin, message):
def split_message(message):
try:
m = bytes(message).decode()
lines = split_words(m, 18)
lines = split_words(m, ui.WIDTH - 2 * TEXT_MARGIN_LEFT, metric=lambda x: ui.display.text_width(x, ui.NORMAL))
except UnicodeError:
m = hexlify(message)
lines = chunks(m, 16)

View File

@ -4,7 +4,8 @@ from trezor.crypto.aes import AES_CBC_Decrypt, AES_CBC_Encrypt
from trezor.crypto.hashlib import sha512
from trezor.messages.CipheredKeyValue import CipheredKeyValue
from trezor.messages.FailureType import DataError
from trezor.ui.text import Text
from trezor.ui.text import Text, TEXT_MARGIN_LEFT
from trezor.utils import split_words
from apps.common import seed
from apps.common.confirm import require_confirm
@ -21,7 +22,8 @@ async def cipher_key_value(ctx, msg):
title = 'Encrypt value'
else:
title = 'Decrypt value'
await require_confirm(ctx, Text(title, ui.ICON_DEFAULT, msg.key))
lines = split_words(msg.key, ui.WIDTH - 2 * TEXT_MARGIN_LEFT, metric=lambda x: ui.display.text_width(x, ui.NORMAL))
await require_confirm(ctx, Text(title, ui.ICON_DEFAULT, *lines))
node = await seed.derive_node(ctx, msg.address_n)
value = compute_cipher_key_value(msg, node.private_key())

View File

@ -34,5 +34,5 @@ async def sign_message(ctx, msg):
async def confirm_sign_message(ctx, message):
message = split_message(message)
content = Text('Sign message', ui.ICON_CONFIRM, ui.MONO, *message)
content = Text('Sign message', ui.ICON_CONFIRM, *message)
await require_confirm(ctx, content)

View File

@ -29,19 +29,16 @@ def chunks(items, size):
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
line = []
for w in sentence.split(' '):
if not w:
continue
if metric(' '.join(line + [w])) >= width:
yield ' '.join(line)
line = [w]
else:
line.append(w)
yield ' '.join(line)
def format_amount(amount, decimals):