2016-08-05 10:37:26 +00:00
|
|
|
from trezor import wire, ui
|
2016-06-09 17:01:43 +00:00
|
|
|
from trezor.ui.button import Button, CONFIRM_BUTTON, CONFIRM_BUTTON_ACTIVE
|
2016-08-05 10:37:26 +00:00
|
|
|
from trezor.ui.scroll import paginate, render_scrollbar, animate_swipe
|
2016-06-09 14:28:34 +00:00
|
|
|
from trezor.crypto import hashlib, random, bip39
|
2016-06-17 18:52:36 +00:00
|
|
|
from trezor.utils import unimport_gen, chunks
|
2016-06-09 14:28:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
def generate_mnemonic(strength, display_random):
|
|
|
|
from trezor.messages.EntropyRequest import EntropyRequest
|
|
|
|
from trezor.messages.EntropyAck import EntropyAck
|
|
|
|
|
|
|
|
ack = yield from wire.call(EntropyRequest(), EntropyAck)
|
|
|
|
|
|
|
|
ctx = hashlib.sha256()
|
|
|
|
ctx.update(random.bytes(32))
|
|
|
|
ctx.update(ack.entropy)
|
|
|
|
entropy = ctx.digest()
|
|
|
|
|
|
|
|
# TODO: handle strength
|
|
|
|
# TODO: handle display_random
|
|
|
|
|
|
|
|
return bip39.from_data(entropy)
|
|
|
|
|
|
|
|
|
|
|
|
def request_new_pin():
|
2016-06-17 18:52:36 +00:00
|
|
|
from trezor.workflows.request_pin import request_pin
|
2016-06-09 14:28:34 +00:00
|
|
|
|
|
|
|
pin = yield from request_pin()
|
|
|
|
pin_again = yield from request_pin('Enter PIN again')
|
|
|
|
|
|
|
|
if pin == pin_again:
|
|
|
|
return pin
|
|
|
|
else:
|
|
|
|
raise Exception() # TODO: wrong PIN should be handled in unified way
|
|
|
|
|
|
|
|
|
2016-06-17 18:52:36 +00:00
|
|
|
def show_mnemonic(mnemonic):
|
2016-06-09 14:28:34 +00:00
|
|
|
words_per_page = const(4)
|
2016-06-17 18:52:36 +00:00
|
|
|
mnemonic_words = list(enumerate(mnemonic.split()))
|
|
|
|
mnemonic_pages = list(chunks(mnemonic_words, words_per_page))
|
|
|
|
|
|
|
|
def render(page, page_count):
|
2016-06-09 14:28:34 +00:00
|
|
|
|
2016-06-17 18:52:36 +00:00
|
|
|
# Header
|
2016-06-09 14:28:34 +00:00
|
|
|
ui.clear()
|
|
|
|
ui.display.text(10, 30, 'Write down your seed', ui.BOLD, ui.LIGHT_GREEN, ui.BLACK)
|
2016-06-10 13:35:22 +00:00
|
|
|
|
2016-06-17 18:52:36 +00:00
|
|
|
render_scrollbar(page, page_count)
|
2016-06-10 13:35:22 +00:00
|
|
|
|
2016-06-17 18:52:36 +00:00
|
|
|
# Mnemonic page
|
|
|
|
for pi, (wi, word) in enumerate(mnemonic_pages[page]):
|
|
|
|
top = pi * 30 + 74
|
|
|
|
pos = wi + 1
|
|
|
|
ui.display.text_right(40, top, '%d.' % pos, ui.BOLD, ui.LIGHT_GREEN, ui.BLACK)
|
2016-06-09 16:31:48 +00:00
|
|
|
ui.display.text(45, top, '%s' % word, ui.BOLD, ui.WHITE, ui.BLACK)
|
2016-06-10 13:35:22 +00:00
|
|
|
|
2016-06-17 18:52:36 +00:00
|
|
|
if page + 1 == page_count:
|
2016-08-05 10:37:26 +00:00
|
|
|
# Finish button
|
2016-06-17 18:52:36 +00:00
|
|
|
finish = Button((0, 240 - 48, 240, 48), 'Finish',
|
|
|
|
normal_style=CONFIRM_BUTTON,
|
|
|
|
active_style=CONFIRM_BUTTON_ACTIVE)
|
|
|
|
yield from finish.wait()
|
|
|
|
else:
|
2016-08-05 10:37:26 +00:00
|
|
|
# Swipe icon
|
2016-06-17 18:52:36 +00:00
|
|
|
yield from animate_swipe()
|
|
|
|
|
|
|
|
yield from paginate(render, len(mnemonic_pages))
|
|
|
|
|
|
|
|
|
|
|
|
@unimport_gen
|
|
|
|
def layout_reset_device(m):
|
|
|
|
|
|
|
|
# TODO: Failure if not empty
|
|
|
|
|
|
|
|
mnemonic = yield from generate_mnemonic(m.strength, m.display_random)
|
|
|
|
|
|
|
|
# if m.pin_protection:
|
|
|
|
# pin = yield from request_new_pin()
|
|
|
|
# else:
|
|
|
|
# pin = None
|
|
|
|
|
|
|
|
yield from show_mnemonic(mnemonic)
|