1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-01-20 20:31:06 +00:00
trezor-firmware/core/src/apps/common/mnemonic.py

119 lines
3.1 KiB
Python
Raw Normal View History

from typing import TYPE_CHECKING
import storage.device as storage_device
from trezor import utils
if TYPE_CHECKING:
from trezor.enums import BackupType
from trezor.ui.layouts.common import ProgressLayout
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.
"""
from trezor.enums import BackupType
2019-09-19 07:37:23 +00:00
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
if not utils.BITCOIN_ONLY:
def derive_cardano_icarus(
passphrase: str = "",
trezor_derivation: bool = True,
progress_bar: bool = True,
) -> bytes:
if not is_bip39():
raise ValueError # should not be called for SLIP-39
mnemonic_secret = get_secret()
if mnemonic_secret is None:
raise ValueError("Mnemonic not set")
render_func = None
if progress_bar and not utils.DISABLE_ANIMATION:
_start_progress()
render_func = _render_progress
from trezor.crypto import cardano
seed = cardano.derive_icarus(
mnemonic_secret.decode(), passphrase, trezor_derivation, render_func
)
_finish_progress()
return seed
_progress_obj: ProgressLayout | None = None
2019-07-03 13:07:04 +00:00
def _start_progress() -> None:
from trezor import workflow
2023-05-04 12:38:31 +00:00
from trezor.ui.layouts.progress import progress
global _progress_obj
# Because we are drawing to the screen manually, without a layout, we
# should make sure that no other layout is running.
workflow.close_others()
_progress_obj = progress()
2019-07-03 13:07:04 +00:00
def _render_progress(progress: int, total: int) -> None:
global _progress_obj
if _progress_obj is not None:
_progress_obj.report(1000 * progress // total)
def _finish_progress() -> None:
global _progress_obj
_progress_obj = None