1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-19 12:58:13 +00:00

apps/management/reset_device: minor fixes

This commit is contained in:
Jan Pochyla 2018-01-22 15:29:53 +01:00
parent 5701e57b48
commit 57e350fbbf
2 changed files with 59 additions and 73 deletions

View File

@ -12,8 +12,8 @@ def dispatch_LoadDevice(*args, **kwargs):
@unimport @unimport
def dispatch_ResetDevice(*args, **kwargs): def dispatch_ResetDevice(*args, **kwargs):
from .reset_device import layout_reset_device from .reset_device import reset_device
return layout_reset_device(*args, **kwargs) return reset_device(*args, **kwargs)
@unimport @unimport

View File

@ -1,6 +1,5 @@
from micropython import const from micropython import const
from trezor import wire, ui from trezor import config, ui, wire
from trezor.ui.container import Container
from trezor.utils import unimport, chunks from trezor.utils import unimport, chunks
from ubinascii import hexlify from ubinascii import hexlify
@ -8,11 +7,11 @@ if __debug__:
internal_entropy = None internal_entropy = None
current_word = None current_word = None
@unimport @unimport
async def layout_reset_device(ctx, msg): async def reset_device(ctx, msg):
from trezor import config
from trezor.ui.text import Text from trezor.ui.text import Text
from trezor.crypto import hashlib, random, bip39, random from trezor.crypto import hashlib, random, bip39
from trezor.ui.keyboard import MnemonicKeyboard from trezor.ui.keyboard import MnemonicKeyboard
from trezor.messages.EntropyRequest import EntropyRequest from trezor.messages.EntropyRequest import EntropyRequest
from trezor.messages.Success import Success from trezor.messages.Success import Success
@ -36,11 +35,13 @@ async def layout_reset_device(ctx, msg):
internal_entropy = random.bytes(32) internal_entropy = random.bytes(32)
# display internal entropy
if msg.display_random: if msg.display_random:
entropy_lines = chunks(hexlify(internal_entropy).decode(), 16) entropy_lines = chunks(hexlify(internal_entropy).decode(), 16)
entropy_content = Text('Internal entropy', ui.ICON_RESET, ui.MONO, *entropy_lines) entropy_content = Text('Internal entropy', ui.ICON_RESET, ui.MONO, *entropy_lines)
await require_confirm(ctx, entropy_content, ButtonRequestType.ResetDevice) await require_confirm(ctx, entropy_content, ButtonRequestType.ResetDevice)
# request new PIN
if msg.pin_protection: if msg.pin_protection:
curpin = '' curpin = ''
newpin = await request_pin_confirm(ctx) newpin = await request_pin_confirm(ctx)
@ -48,6 +49,7 @@ async def layout_reset_device(ctx, msg):
curpin = '' curpin = ''
newpin = '' newpin = ''
# request external entropy and compute mnemonic
external_entropy_ack = await ctx.call(EntropyRequest(), EntropyAck) external_entropy_ack = await ctx.call(EntropyRequest(), EntropyAck)
ehash = hashlib.sha256() ehash = hashlib.sha256()
ehash.update(internal_entropy) ehash.update(internal_entropy)
@ -55,79 +57,62 @@ async def layout_reset_device(ctx, msg):
entropy = ehash.digest() entropy = ehash.digest()
mnemonic = bip39.from_data(entropy[:msg.strength // 8]) mnemonic = bip39.from_data(entropy[:msg.strength // 8])
# seed-copy warning # mnemonic safety warning
warning_content = Text('Backup your seed', ui.ICON_NOCOPY, ui.NORMAL, warning_content = Text(
'Never make a digital', 'Backup your seed', ui.ICON_NOCOPY, ui.NORMAL,
'copy of your recovery', 'Never make a digital',
'seed and never upload', 'copy of your recovery',
'it online!') 'seed and never upload',
await require_confirm(ctx, warning_content, ButtonRequestType.ResetDevice) 'it online!')
await require_confirm(
ctx,
warning_content,
ButtonRequestType.ResetDevice,
confirm='I understand',
cancel=None)
# ask to write down mnemonic # ask to write down mnemonic
await show_mnemonic(mnemonic) await show_mnemonic(mnemonic)
# ask for random number to check correctness # ask for random word to check correctness
words = list(enumerate(mnemonic.split())) words = mnemonic.split()
index = random.uniform(len(words)) index = random.uniform(len(words))
word = words[index][1] res = await MnemonicKeyboard('Type %s. word' % (index + 1))
board = MnemonicKeyboard() if res != words[index]:
board.prompt = ('Type %s. word' % (index + 1)) content = Text(
res = await board 'Wrong entry!', ui.ICON_CLEAR,
if res != word: 'You have entered',
fail_content = Text('Wrong entry!', ui.ICON_CLEAR, ui.NORMAL, 'wrong seed word.',
'You have entered', 'Please, reconnect',
'wrong seed word.', 'the device and try again.', icon_color=ui.RED)
'Please, reconnect', ui.display.clear()
'device and try again.', icon_color=ui.RED) await content
# todo redesign dialog to single cancel button with text 'Reconnect' or something else (no icon) raise wire.FailureError(FailureType.DataError, 'Wrong entry')
await require_confirm(ctx, fail_content, ButtonRequestType.ResetDevice)
# write into storage
if curpin != newpin: if curpin != newpin:
config.change_pin(curpin, newpin) config.change_pin(curpin, newpin)
storage.load_settings(label=msg.label, storage.load_settings(
use_passphrase=msg.passphrase_protection) label=msg.label, use_passphrase=msg.passphrase_protection)
storage.load_mnemonic(mnemonic) storage.load_mnemonic(mnemonic)
fail_content = Text('Backup is done!', ui.ICON_CONFIRM, ui.NORMAL, # show success message
'Never make a digital', content = Text(
'copy of your recovery', 'Backup is done!', ui.ICON_CONFIRM,
'seed and never upload', 'Never make a digital',
'it online!', icon_color=ui.GREEN) 'copy of your recovery',
# todo redesign dialog to single cancel button with text 'Finish?' or something else (no icon) 'seed and never upload',
await require_confirm(ctx, fail_content, ButtonRequestType.ResetDevice) 'it online!', icon_color=ui.GREEN)
await require_confirm(
ctx,
content,
ButtonRequestType.ResetDevice,
confirm='Finish setup',
cancel=None)
return Success(message='Initialized') return Success(message='Initialized')
async def show_mnemonic_by_word(ctx, mnemonic):
from trezor.ui.text import Text
from trezor.messages.ButtonRequestType import ConfirmWord
from apps.common.confirm import confirm
words = mnemonic.split()
if __debug__:
global current_word
for index, word in enumerate(words):
if __debug__:
current_word = word
await confirm(ctx,
Text('Recovery seed setup', ui.ICON_RESET,
ui.NORMAL, 'Write down seed word', '',
ui.BOLD, '%d. %s' % (index + 1, word)),
ConfirmWord, confirm='Next', cancel=None)
for index, word in enumerate(words):
if __debug__:
current_word = word
await confirm(ctx,
Text('Recovery seed setup', ui.ICON_RESET,
ui.NORMAL, 'Confirm seed word', '',
ui.BOLD, '%d. %s' % (index + 1, word)),
ConfirmWord, confirm='Next', cancel=None)
async def show_mnemonic(mnemonic): async def show_mnemonic(mnemonic):
from trezor.ui.scroll import paginate from trezor.ui.scroll import paginate
@ -143,17 +128,18 @@ async def show_mnemonic_page(page, page_count, mnemonic):
from trezor.ui.text import Text from trezor.ui.text import Text
from trezor.ui.scroll import Scrollpage, animate_swipe from trezor.ui.scroll import Scrollpage, animate_swipe
lines = [] lines = ['%d. %s' % (wi + 1, word) for wi, word in mnemonic[page]]
for pi, (wi, word) in enumerate(mnemonic[page]): scroll_page = Scrollpage(
pos = wi + 1 Text('Recovery seed setup', ui.ICON_RESET, ui.MONO, lines),
lines.append(str('%d. %s' % (pos, word))) page,
page_count)
ui.display.clear() ui.display.clear()
scroll_page = Scrollpage(Text('Recovery seed setup', ui.ICON_RESET, ui.MONO, lines), page, page_count)
scroll_page.render() scroll_page.render()
if page + 1 == page_count: if page + 1 == page_count:
await Button( await Button(
(0, 240 - 48, 240, 48), ui.grid(4, n_x=1),
'Finalize', "I'm done",
normal_style=ui.BTN_CONFIRM, normal_style=ui.BTN_CONFIRM,
active_style=ui.BTN_CONFIRM_ACTIVE) active_style=ui.BTN_CONFIRM_ACTIVE)
ui.display.clear() ui.display.clear()