mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-22 15:38:11 +00:00
add Storage to reset_device, stub session support in config
This commit is contained in:
parent
d1ff96af04
commit
dd713fe6e2
@ -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.workflows.request_pin import request_new_pin
|
||||||
from trezor.messages.wire_types import EntropyAck
|
from trezor.messages.wire_types import EntropyAck
|
||||||
from trezor.ui.button import Button, CONFIRM_BUTTON, CONFIRM_BUTTON_ACTIVE
|
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
|
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
|
@unimport
|
||||||
async def generate_mnemonic(strength, display_random, session_id):
|
async def generate_mnemonic(strength, display_random, session_id):
|
||||||
from trezor.messages.EntropyRequest import EntropyRequest
|
from trezor.messages.EntropyRequest import EntropyRequest
|
||||||
from trezor.messages.FailureType import Other
|
from trezor.messages.FailureType import Other
|
||||||
|
|
||||||
if strength not in (128, 192, 256):
|
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:
|
# if display_random:
|
||||||
# raise wire.FailureError(Other, 'Entropy display not implemented')
|
# raise wire.FailureError(Other, 'Entropy display not implemented')
|
||||||
|
|
||||||
ack = await wire.reply_message(session_id,
|
ack = await wire.reply_message(
|
||||||
EntropyRequest(),
|
session_id, EntropyRequest(), EntropyAck)
|
||||||
EntropyAck)
|
|
||||||
|
|
||||||
strength_bytes = strength // 8
|
strength_bytes = strength // 8
|
||||||
ctx = hashlib.sha256()
|
ctx = hashlib.sha256()
|
||||||
@ -31,28 +60,6 @@ async def generate_mnemonic(strength, display_random, session_id):
|
|||||||
return bip39.from_data(entropy)
|
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):
|
async def show_mnemonic(mnemonic):
|
||||||
first_page = const(0)
|
first_page = const(0)
|
||||||
words_per_page = const(4)
|
words_per_page = const(4)
|
||||||
@ -61,18 +68,24 @@ async def show_mnemonic(mnemonic):
|
|||||||
await paginate(show_mnemonic_page, len(pages), first_page, pages)
|
await paginate(show_mnemonic_page, len(pages), first_page, pages)
|
||||||
|
|
||||||
|
|
||||||
@unimport
|
async def show_mnemonic_page(page, page_count, mnemonic):
|
||||||
async def layout_reset_device(message, session_id):
|
ui.clear()
|
||||||
from trezor.messages.Success import Success
|
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,
|
for pi, (wi, word) in enumerate(mnemonic[page]):
|
||||||
message.display_random,
|
top = pi * 30 + 74
|
||||||
session_id)
|
pos = wi + 1
|
||||||
# await show_mnemonic(mnemonic)
|
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:
|
if page + 1 == page_count:
|
||||||
pin = await request_new_pin(session_id)
|
await Button(
|
||||||
|
(0, 240 - 48, 240, 48), 'Finish',
|
||||||
|
normal_style=CONFIRM_BUTTON,
|
||||||
|
active_style=CONFIRM_BUTTON_ACTIVE)
|
||||||
else:
|
else:
|
||||||
pin = None
|
await animate_swipe()
|
||||||
|
|
||||||
return Success()
|
|
||||||
|
@ -5,11 +5,12 @@ import ustruct
|
|||||||
|
|
||||||
_mock = {}
|
_mock = {}
|
||||||
|
|
||||||
if sys.platform in ['trezor', 'pyboard']: # stmhal
|
if sys.platform in ['trezor', 'pyboard']: # stmhal
|
||||||
_file = '/flash/trezor.config'
|
_file = '/flash/trezor.config'
|
||||||
else:
|
else:
|
||||||
_file = '/var/tmp/trezor.config'
|
_file = '/var/tmp/trezor.config'
|
||||||
|
|
||||||
|
|
||||||
def _load():
|
def _load():
|
||||||
try:
|
try:
|
||||||
with open(_file, 'rb') as f:
|
with open(_file, 'rb') as f:
|
||||||
@ -23,6 +24,7 @@ def _load():
|
|||||||
except OSError:
|
except OSError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def _save():
|
def _save():
|
||||||
with open(_file, 'wb') as f:
|
with open(_file, 'wb') as f:
|
||||||
for k, v in _mock.items():
|
for k, v in _mock.items():
|
||||||
@ -31,10 +33,18 @@ def _save():
|
|||||||
|
|
||||||
_load()
|
_load()
|
||||||
|
|
||||||
def get(app, key, default=None):
|
|
||||||
return _mock.get((app << 8) | key, default)
|
|
||||||
|
|
||||||
def set(app, key, value):
|
def get(session_id, app_id, key, default=None):
|
||||||
_mock[(app << 8) | key] = value
|
# 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()
|
_save()
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def commit(session_id):
|
||||||
|
pass
|
||||||
|
Loading…
Reference in New Issue
Block a user