2022-09-19 09:17:36 +00:00
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
|
|
|
import storage.device as storage_device
|
|
|
|
from trezor import utils
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
from trezor.enums import BackupType
|
2019-06-23 10:18:59 +00:00
|
|
|
|
|
|
|
|
2021-03-23 12:29:25 +00:00
|
|
|
def get() -> tuple[bytes | None, BackupType]:
|
2019-07-24 17:37:04 +00:00
|
|
|
return get_secret(), get_type()
|
|
|
|
|
|
|
|
|
2021-03-18 09:48:50 +00:00
|
|
|
def get_secret() -> bytes | None:
|
2022-09-19 09:17:36 +00:00
|
|
|
return storage_device.get_mnemonic_secret()
|
2019-07-24 17:37:04 +00:00
|
|
|
|
|
|
|
|
2021-03-23 12:35:27 +00:00
|
|
|
def get_type() -> BackupType:
|
2022-09-19 09:17:36 +00:00
|
|
|
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.
|
|
|
|
"""
|
2022-09-19 09:17:36 +00:00
|
|
|
from trezor.enums import BackupType
|
|
|
|
|
2019-09-19 07:37:23 +00:00
|
|
|
return get_type() == BackupType.Bip39
|
2019-06-23 10:18:59 +00:00
|
|
|
|
|
|
|
|
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()
|
2019-07-24 17:37:04 +00:00
|
|
|
if mnemonic_secret is None:
|
2021-10-15 16:27:22 +00:00
|
|
|
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():
|
2021-03-22 15:14:24 +00:00
|
|
|
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
|
2021-03-22 15:14:24 +00:00
|
|
|
from trezor.crypto import slip39
|
|
|
|
|
2022-09-19 09:17:36 +00:00
|
|
|
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(
|
2021-10-13 14:06:08 +00:00
|
|
|
mnemonic_secret,
|
|
|
|
passphrase.encode(),
|
|
|
|
iteration_exponent,
|
|
|
|
identifier,
|
|
|
|
render_func,
|
2019-06-23 10:18:59 +00:00
|
|
|
)
|
|
|
|
|
2019-07-11 14:52:25 +00:00
|
|
|
return seed
|
|
|
|
|
2019-06-23 10:18:59 +00:00
|
|
|
|
2021-10-15 16:27:22 +00:00
|
|
|
if not utils.BITCOIN_ONLY:
|
|
|
|
|
2021-10-20 14:18:15 +00:00
|
|
|
def derive_cardano_icarus(
|
|
|
|
passphrase: str = "",
|
|
|
|
trezor_derivation: bool = True,
|
|
|
|
progress_bar: bool = True,
|
2021-10-15 16:27:22 +00:00
|
|
|
) -> 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
|
|
|
|
|
2021-10-20 14:18:15 +00:00
|
|
|
return cardano.derive_icarus(
|
|
|
|
mnemonic_secret.decode(), passphrase, trezor_derivation, render_func
|
2021-10-15 16:27:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-07-03 13:07:04 +00:00
|
|
|
def _start_progress() -> None:
|
2022-09-19 09:17:36 +00:00
|
|
|
from trezor import workflow
|
2021-03-23 12:29:25 +00:00
|
|
|
from trezor.ui.layouts import draw_simple_text
|
2021-03-22 15:14:24 +00:00
|
|
|
|
2019-08-20 14:20:02 +00:00
|
|
|
# Because we are drawing to the screen manually, without a layout, we
|
2020-05-25 11:04:15 +00:00
|
|
|
# should make sure that no other layout is running.
|
|
|
|
workflow.close_others()
|
2021-03-23 12:29:25 +00:00
|
|
|
draw_simple_text("Please wait")
|
2019-06-23 10:18:59 +00:00
|
|
|
|
|
|
|
|
2019-07-03 13:07:04 +00:00
|
|
|
def _render_progress(progress: int, total: int) -> None:
|
2022-09-19 09:17:36 +00:00
|
|
|
from trezor import ui
|
|
|
|
|
2019-06-23 10:18:59 +00:00
|
|
|
p = 1000 * progress // total
|
|
|
|
ui.display.loader(p, False, 18, ui.WHITE, ui.BG)
|
2019-12-09 16:01:04 +00:00
|
|
|
ui.refresh()
|