1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-21 04:41:18 +00:00
trezor-firmware/core/src/apps/common/mnemonic.py

73 lines
2.0 KiB
Python
Raw Normal View History

import storage.device
2019-12-09 16:01:04 +00:00
from trezor import ui, utils, workflow
from trezor.enums import BackupType
def get() -> tuple[bytes | None, BackupType]:
return get_secret(), get_type()
def get_secret() -> bytes | None:
return storage.device.get_mnemonic_secret()
def get_type() -> BackupType:
return storage.device.get_backup_type()
2019-09-19 07:37:23 +00:00
def is_bip39() -> bool:
"""
If False then SLIP-39 (either Basic or Advanced).
Other invalid values are checked directly in storage.
"""
return get_type() == BackupType.Bip39
2019-07-03 13:07:04 +00:00
def get_seed(passphrase: str = "", progress_bar: bool = True) -> bytes:
2019-09-19 07:37:23 +00:00
mnemonic_secret = get_secret()
if mnemonic_secret is None:
raise ValueError("Mnemonic not set")
2019-07-11 14:52:25 +00:00
render_func = None
2019-12-09 16:01:04 +00:00
if progress_bar and not utils.DISABLE_ANIMATION:
2019-07-11 14:52:25 +00:00
_start_progress()
render_func = _render_progress
2019-09-19 07:37:23 +00:00
if is_bip39():
from trezor.crypto import bip39
2019-07-11 14:52:25 +00:00
seed = bip39.seed(mnemonic_secret.decode(), passphrase, render_func)
2019-09-19 07:37:23 +00:00
else: # SLIP-39
from trezor.crypto import slip39
identifier = storage.device.get_slip39_identifier()
iteration_exponent = storage.device.get_slip39_iteration_exponent()
2019-07-30 08:25:26 +00:00
if identifier is None or iteration_exponent is None:
# Identifier or exponent expected but not found
raise RuntimeError
2019-07-11 14:52:25 +00:00
seed = slip39.decrypt(
mnemonic_secret,
passphrase.encode(),
iteration_exponent,
identifier,
render_func,
)
2019-07-11 14:52:25 +00:00
return seed
2019-07-03 13:07:04 +00:00
def _start_progress() -> None:
from trezor.ui.layouts import draw_simple_text
# Because we are drawing to the screen manually, without a layout, we
# should make sure that no other layout is running.
workflow.close_others()
draw_simple_text("Please wait")
2019-07-03 13:07:04 +00:00
def _render_progress(progress: int, total: int) -> None:
p = 1000 * progress // total
ui.display.loader(p, False, 18, ui.WHITE, ui.BG)
2019-12-09 16:01:04 +00:00
ui.refresh()