From dd713fe6e2faa50666643441a967fb99f6ba9fe5 Mon Sep 17 00:00:00 2001 From: Jan Pochyla Date: Mon, 26 Sep 2016 12:27:14 +0200 Subject: [PATCH] add Storage to reset_device, stub session support in config --- src/apps/management/layout_reset_device.py | 91 ++++++++++++---------- src/trezor/config.py | 20 +++-- 2 files changed, 67 insertions(+), 44 deletions(-) diff --git a/src/apps/management/layout_reset_device.py b/src/apps/management/layout_reset_device.py index ea852ee3aa..b020d36d8f 100644 --- a/src/apps/management/layout_reset_device.py +++ b/src/apps/management/layout_reset_device.py @@ -1,4 +1,4 @@ -from trezor import wire, ui +from trezor import wire, ui, config from trezor.workflows.request_pin import request_new_pin from trezor.messages.wire_types import EntropyAck from trezor.ui.button import Button, CONFIRM_BUTTON, CONFIRM_BUTTON_ACTIVE @@ -7,20 +7,49 @@ from trezor.crypto import hashlib, random, bip39 from trezor.utils import unimport, chunks +APP_MANAGEMENT = const(1) +CFG_STORAGE = const(1) + + +@unimport +async def layout_reset_device(message, session_id): + from trezor.messages.Success import Success + from trezor.messages.Storage import Storage + + mnemonic = await generate_mnemonic( + message.strength, message.display_random, session_id) + + await show_mnemonic(mnemonic) + + if message.pin_protection: + pin = await request_new_pin(session_id) + else: + pin = '' + + storage = Storage( + version=1, pin=pin, mnemonic=mnemonic, + passphrase_protection=message.passphrase_protection, + language=message.language, label=message.label) + + config.set(session_id, APP_MANAGEMENT, CFG_STORAGE, await storage.dumps()) + + return Success() + + @unimport async def generate_mnemonic(strength, display_random, session_id): from trezor.messages.EntropyRequest import EntropyRequest from trezor.messages.FailureType import Other if strength not in (128, 192, 256): - raise wire.FailureError(Other, 'Invalid seed strength') + raise wire.FailureError( + Other, 'Invalid strength (has to be 128, 192 or 256 bits)') # if display_random: # raise wire.FailureError(Other, 'Entropy display not implemented') - ack = await wire.reply_message(session_id, - EntropyRequest(), - EntropyAck) + ack = await wire.reply_message( + session_id, EntropyRequest(), EntropyAck) strength_bytes = strength // 8 ctx = hashlib.sha256() @@ -31,28 +60,6 @@ async def generate_mnemonic(strength, display_random, session_id): return bip39.from_data(entropy) -async def show_mnemonic_page(page, page_count, mnemonic): - ui.clear() - ui.display.text(10, 30, 'Write down your seed', - ui.BOLD, ui.LIGHT_GREEN, ui.BLACK) - render_scrollbar(page, page_count) - - for pi, (wi, word) in enumerate(mnemonic[page]): - top = pi * 30 + 74 - pos = wi + 1 - ui.display.text_right(40, top, '%d.' % pos, - ui.BOLD, ui.LIGHT_GREEN, ui.BLACK) - ui.display.text(45, top, '%s' % word, - ui.BOLD, ui.WHITE, ui.BLACK) - - if page + 1 == page_count: - await Button((0, 240 - 48, 240, 48), 'Finish', - normal_style=CONFIRM_BUTTON, - active_style=CONFIRM_BUTTON_ACTIVE) - else: - await animate_swipe() - - async def show_mnemonic(mnemonic): first_page = const(0) words_per_page = const(4) @@ -61,18 +68,24 @@ async def show_mnemonic(mnemonic): await paginate(show_mnemonic_page, len(pages), first_page, pages) -@unimport -async def layout_reset_device(message, session_id): - from trezor.messages.Success import Success +async def show_mnemonic_page(page, page_count, mnemonic): + ui.clear() + ui.display.text( + 10, 30, 'Write down your seed', ui.BOLD, ui.LIGHT_GREEN, ui.BLACK) + render_scrollbar(page, page_count) - mnemonic = await generate_mnemonic(message.strength, - message.display_random, - session_id) - # await show_mnemonic(mnemonic) + for pi, (wi, word) in enumerate(mnemonic[page]): + top = pi * 30 + 74 + pos = wi + 1 + ui.display.text_right( + 40, top, '%d.' % pos, ui.BOLD, ui.LIGHT_GREEN, ui.BLACK) + ui.display.text( + 45, top, '%s' % word, ui.BOLD, ui.WHITE, ui.BLACK) - if message.pin_protection: - pin = await request_new_pin(session_id) + if page + 1 == page_count: + await Button( + (0, 240 - 48, 240, 48), 'Finish', + normal_style=CONFIRM_BUTTON, + active_style=CONFIRM_BUTTON_ACTIVE) else: - pin = None - - return Success() + await animate_swipe() diff --git a/src/trezor/config.py b/src/trezor/config.py index 40d707bf6d..2e77aa4e8e 100644 --- a/src/trezor/config.py +++ b/src/trezor/config.py @@ -5,11 +5,12 @@ import ustruct _mock = {} -if sys.platform in ['trezor', 'pyboard']: # stmhal +if sys.platform in ['trezor', 'pyboard']: # stmhal _file = '/flash/trezor.config' else: _file = '/var/tmp/trezor.config' + def _load(): try: with open(_file, 'rb') as f: @@ -23,6 +24,7 @@ def _load(): except OSError: pass + def _save(): with open(_file, 'wb') as f: for k, v in _mock.items(): @@ -31,10 +33,18 @@ def _save(): _load() -def get(app, key, default=None): - return _mock.get((app << 8) | key, default) -def set(app, key, value): - _mock[(app << 8) | key] = value +def get(session_id, app_id, key, default=None): + # TODO: session_id + return _mock.get((app_id << 8) | key, default) + + +def set(session_id, app_id, key, value): + # TODO: session_id + _mock[(app_id << 8) | key] = value _save() return True + + +def commit(session_id): + pass