1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-22 15:38:11 +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 ubinascii import hexlify
from trezor.crypto.hashlib import sha256 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 trezor.utils import chunks, split_words
from apps.common.hash_writer import HashWriter from apps.common.hash_writer import HashWriter
from apps.wallet.sign_tx.signing import write_varint from apps.wallet.sign_tx.signing import write_varint
@ -17,7 +19,7 @@ def message_digest(coin, message):
def split_message(message): def split_message(message):
try: try:
m = bytes(message).decode() 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: except UnicodeError:
m = hexlify(message) m = hexlify(message)
lines = chunks(m, 16) 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.crypto.hashlib import sha512
from trezor.messages.CipheredKeyValue import CipheredKeyValue from trezor.messages.CipheredKeyValue import CipheredKeyValue
from trezor.messages.FailureType import DataError 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 import seed
from apps.common.confirm import require_confirm from apps.common.confirm import require_confirm
@ -21,7 +22,8 @@ async def cipher_key_value(ctx, msg):
title = 'Encrypt value' title = 'Encrypt value'
else: else:
title = 'Decrypt value' 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) node = await seed.derive_node(ctx, msg.address_n)
value = compute_cipher_key_value(msg, node.private_key()) 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): async def confirm_sign_message(ctx, message):
message = split_message(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) await require_confirm(ctx, content)

View File

@ -29,19 +29,16 @@ def chunks(items, size):
def split_words(sentence, width, metric=len): def split_words(sentence, width, metric=len):
line = '' line = []
for c in sentence: for w in sentence.split(' '):
line += c if not w:
if metric(line) >= width: continue
c = line[-1] if metric(' '.join(line + [w])) >= width:
if c == ' ': yield ' '.join(line)
yield line line = [w]
line = '' else:
else: line.append(w)
yield line[:-1] + '-' yield ' '.join(line)
line = c
if line != '':
yield line
def format_amount(amount, decimals): def format_amount(amount, decimals):