1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-18 12:28:09 +00:00

apps/management/recovery_device: adapt for new keyboard

This commit is contained in:
Jan Pochyla 2018-01-11 20:06:35 +01:00
parent cd0fa4df4a
commit 77b78e277a
2 changed files with 39 additions and 31 deletions

View File

@ -24,8 +24,8 @@ def dispatch_WipeDevice(*args, **kwargs):
@unimport
def dispatch_RecoveryDevice(*args, **kwargs):
from .recovery_device import layout_recovery_device
return layout_recovery_device(*args, **kwargs)
from .recovery_device import recovery_device
return recovery_device(*args, **kwargs)
@unimport

View File

@ -1,47 +1,55 @@
from trezor import ui, wire
from trezor.utils import unimport
@unimport
async def layout_recovery_device(ctx, msg):
async def recovery_device(ctx, msg):
'''
Recover BIP39 seed into empty device.
1. Ask for the number of words in recovered seed.
2. Let user type in the mnemonic words one by one.
3. Optionally check the seed validity.
4. Optionally ask for the PIN, with confirmation.
5. Save into storage.
'''
from trezor import config
from trezor.crypto import bip39
from trezor.messages.FailureType import UnexpectedMessage, ProcessError
from trezor.messages.Success import Success
from trezor.ui.keyboard import KeyboardMultiTap
from trezor.ui.text import Text
from apps.common import storage
from apps.common.confirm import require_confirm
from apps.common.request_pin import request_pin
from apps.common.request_words import request_words
if storage.is_initialized():
raise wire.FailureError(UnexpectedMessage, 'Already initialized')
words = []
wordcount = await request_words(ctx,
Text('Device recovery', ui.ICON_RECOVERY,
'Number of words?'))
mnemonic = await request_mnemonic(wordcount, 'Type %s. word')
wc = await request_words(ctx, Text(
'Device recovery', ui.ICON_RECOVERY, 'Number of words?'))
msg.word_count = int(wc)
ui.display.clear()
kbd = KeyboardMultiTap()
for i in range(0, msg.word_count):
kbd.prompt = 'Type %s. word' % (i + 1)
word = await kbd
words.append(word)
# TODO: confirm words, start again?
await require_confirm(ctx, Text(
'Recovering seed', ui.ICON_RESET))
mnemonic = ' '.join(words)
if not msg.enforce_wordlist and not bip39.check(mnemonic):
if msg.enforce_wordlist and not bip39.check(mnemonic):
raise wire.FailureError(ProcessError, 'Mnemonic is not valid')
# TODO: request pin
pin = ''
if msg.pin_protection:
curpin = ''
newpin = await request_pin(ctx)
config.change_pin(curpin, newpin)
storage.load_settings(label=msg.label,
use_passphrase=msg.passphrase_protection)
storage.load_mnemonic(mnemonic)
storage.load_settings(pin=pin,
passphrase_protection=msg.passphrase_protection,
language=msg.language,
label=msg.label)
return Success()
async def request_mnemonic(count: int, prompt: str) -> str:
from trezor.ui.keyboard import MnemonicKeyboard
words = []
board = MnemonicKeyboard()
for i in range(0, count):
board.prompt = prompt % (i + 1)
word = await board
words.append(word)
return ' '.join(words)