diff --git a/core/src/all_modules.py b/core/src/all_modules.py index 38b4c8fca..2b0b2e4cb 100644 --- a/core/src/all_modules.py +++ b/core/src/all_modules.py @@ -347,8 +347,6 @@ apps.management.reset_device import apps.management.reset_device apps.management.reset_device.layout import apps.management.reset_device.layout -apps.management.sd_backup -import apps.management.sd_backup apps.management.sd_protect import apps.management.sd_protect apps.management.set_u2f_counter diff --git a/core/src/apps/management/recovery_device/homescreen.py b/core/src/apps/management/recovery_device/homescreen.py index 4cd0ff467..7999208e0 100644 --- a/core/src/apps/management/recovery_device/homescreen.py +++ b/core/src/apps/management/recovery_device/homescreen.py @@ -99,7 +99,7 @@ async def _offer_backup_on_another_medium( if previous_medium == BackupMedium.Words: from trezor.enums import BackupType - from apps.management.sd_backup import sdcard_backup_seed + from apps.management.reset_device import sdcard_backup_seed await sdcard_backup_seed(secret, BackupType.Bip39) else: @@ -186,9 +186,7 @@ async def _recover_mnemonic_or_share( while True: backup_medium = BackupMedium.Words if utils.USE_SD_CARD: - from apps.management.sd_backup import choose_recovery_medium - - backup_medium = await choose_recovery_medium( + backup_medium = await layout.choose_recovery_medium( backup_types.is_slip39_backup_type(backup_type), dry_run ) @@ -208,10 +206,8 @@ async def _recover_mnemonic_or_share( return words, word_count, BackupMedium.Words else: # try to recover from SD card - from apps.management.sd_backup import sdcard_recover_seed - try: - mnemonic, _ = await sdcard_recover_seed() # TODO backup type needed? + mnemonic, _ = await _sdcard_recover_seed() # TODO backup type needed? if mnemonic is None: # TODO warn and repeat pass @@ -226,6 +222,18 @@ async def _recover_mnemonic_or_share( pass +async def _sdcard_recover_seed() -> tuple[str | None, BackupType | None]: + from storage.sd_seed_backup import recover_seed_from_sdcard + + from apps.common.sdcard import ensure_sdcard + + await ensure_sdcard(ensure_filesystem=False) + mnemonic_bytes, backup_type = recover_seed_from_sdcard() + if mnemonic_bytes is None or backup_type is None: + return (None, None) + return mnemonic_bytes.decode("utf-8"), backup_type + + async def _process_words(words: str) -> tuple[bytes | None, BackupType]: word_count = len(words.split(" ")) is_slip39 = backup_types.is_slip39_word_count(word_count) diff --git a/core/src/apps/management/recovery_device/layout.py b/core/src/apps/management/recovery_device/layout.py index 3bc030c9c..14cc19f85 100644 --- a/core/src/apps/management/recovery_device/layout.py +++ b/core/src/apps/management/recovery_device/layout.py @@ -18,6 +18,12 @@ if TYPE_CHECKING: from trezor.enums import BackupType +async def choose_recovery_medium(is_slip39: bool, dry_run: bool) -> BackupMedium: + from trezor.ui.layouts import choose_recovery_medium + + return await choose_recovery_medium(is_slip39, dry_run) + + async def _confirm_abort(dry_run: bool = False) -> None: if dry_run: await confirm_action( diff --git a/core/src/apps/management/reset_device/__init__.py b/core/src/apps/management/reset_device/__init__.py index 744002aa5..9e3301611 100644 --- a/core/src/apps/management/reset_device/__init__.py +++ b/core/src/apps/management/reset_device/__init__.py @@ -125,9 +125,7 @@ async def _backup_mnemonic_or_share( # let the user choose between Words/SDcard backup backup_medium = BackupMedium.Words if utils.USE_SD_CARD: - from apps.management.sd_backup import choose_backup_medium - - backup_medium = await choose_backup_medium(share_index, group_index) + backup_medium = await layout.choose_backup_medium(share_index, group_index) # proceed with backup if backup_medium == BackupMedium.Words: @@ -142,8 +140,6 @@ async def _backup_mnemonic_or_share( return else: # try to store seed on SD card - from apps.management.sd_backup import sdcard_backup_seed - try: await sdcard_backup_seed(mnemonic, backup_type) return @@ -157,6 +153,39 @@ async def _backup_mnemonic_or_share( pass +async def sdcard_backup_seed(mnemonic_secret: bytes, backup_type: BackupType) -> None: + from storage.sd_seed_backup import is_backup_present, store_seed_on_sdcard + from trezor.ui.layouts import confirm_action, show_success + + from apps.common.sdcard import ensure_sdcard, is_trz_card + + await ensure_sdcard(ensure_filesystem=False) + if not is_trz_card(): + await confirm_action( + "confirm_not_trezor_card", + "Not Trezor card", + action="This is not Trezor Card! Still continue?", + verb="Continue", + ) + if is_backup_present(): + await confirm_action( + "confirm_sdcard_backup_exists", + "Backup present", + action="There is already a Trezor backup on this card!", + description="Replace the backup?", + verb="Replace", + verb_cancel="Abort", + hold=True, + hold_danger=True, + reverse=True, + ) + await ensure_sdcard(ensure_filesystem=True, for_sd_backup=True) + + store_seed_on_sdcard(mnemonic_secret, backup_type) + + await show_success("success_sdcard_backup", "Backup on SD card successful!") + + async def _backup_bip39(mnemonic_secret: bytes): await layout.show_backup_warning() await _backup_mnemonic_or_share(mnemonic_secret, BAK_T_BIP39) diff --git a/core/src/apps/management/reset_device/layout.py b/core/src/apps/management/reset_device/layout.py index 38f4d47b0..585436541 100644 --- a/core/src/apps/management/reset_device/layout.py +++ b/core/src/apps/management/reset_device/layout.py @@ -1,5 +1,5 @@ from micropython import const -from typing import Sequence +from typing import TYPE_CHECKING, Sequence from trezor.enums import ButtonRequestType from trezor.ui.layouts import show_success @@ -12,9 +12,20 @@ from trezor.ui.layouts.reset import ( # noqa: F401 slip39_show_checklist, ) +if TYPE_CHECKING: + from storage.sd_seed_backup import BackupMedium + _NUM_OF_CHOICES = const(3) +async def choose_backup_medium( + share_index: int | None, group_index: int | None +) -> BackupMedium: + from trezor.ui.layouts import choose_backup_medium + + return await choose_backup_medium(share_index, group_index) + + async def show_internal_entropy(entropy: bytes) -> None: from trezor.ui.layouts import confirm_blob diff --git a/core/src/apps/management/sd_backup.py b/core/src/apps/management/sd_backup.py deleted file mode 100644 index aa6e7bfe5..000000000 --- a/core/src/apps/management/sd_backup.py +++ /dev/null @@ -1,64 +0,0 @@ -from typing import TYPE_CHECKING - -if TYPE_CHECKING: - from storage.sd_seed_backup import BackupMedium - from trezor.enums import BackupType - - -async def choose_recovery_medium(is_slip39: bool, dry_run: bool) -> BackupMedium: - from trezor.ui.layouts import choose_recovery_medium - - return await choose_recovery_medium(is_slip39, dry_run) - - -async def choose_backup_medium( - share_index: int | None, group_index: int | None, recovery: bool = False -) -> BackupMedium: - from trezor.ui.layouts import choose_backup_medium - - return await choose_backup_medium(share_index, group_index) - - -async def sdcard_backup_seed(mnemonic_secret: bytes, backup_type: BackupType) -> None: - from storage.sd_seed_backup import is_backup_present, store_seed_on_sdcard - from trezor.ui.layouts import confirm_action, show_success - - from apps.common.sdcard import ensure_sdcard, is_trz_card - - await ensure_sdcard(ensure_filesystem=False) - if not is_trz_card(): - await confirm_action( - "confirm_not_trezor_card", - "Not Trezor card", - action="This is not Trezor Card! Still continue?", - verb="Continue", - ) - if is_backup_present(): - await confirm_action( - "confirm_sdcard_backup_exists", - "Backup present", - action="There is already a Trezor backup on this card!", - description="Replace the backup?", - verb="Replace", - verb_cancel="Abort", - hold=True, - hold_danger=True, - reverse=True, - ) - await ensure_sdcard(ensure_filesystem=True, for_sd_backup=True) - - store_seed_on_sdcard(mnemonic_secret, backup_type) - - await show_success("success_sdcard_backup", "Backup on SD card successful!") - - -async def sdcard_recover_seed() -> tuple[str | None, BackupType | None]: - from storage.sd_seed_backup import recover_seed_from_sdcard - - from apps.common.sdcard import ensure_sdcard - - await ensure_sdcard(ensure_filesystem=False) - mnemonic_bytes, backup_type = recover_seed_from_sdcard() - if mnemonic_bytes is None or backup_type is None: - return (None, None) - return mnemonic_bytes.decode("utf-8"), backup_type