1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-02-17 01:52:02 +00:00

refactor(core): Clean up BackupType usage.

This commit is contained in:
Andrew Kozlik 2024-05-17 16:14:23 +02:00 committed by Andrew Kozlik
parent 9d0d1b3402
commit b14b557efc
4 changed files with 19 additions and 40 deletions

View File

@ -152,7 +152,7 @@ async def _finish_recovery(secret: bytes, backup_type: BackupType) -> Success:
storage_device.store_mnemonic_secret( storage_device.store_mnemonic_secret(
secret, backup_type, needs_backup=False, no_backup=False secret, backup_type, needs_backup=False, no_backup=False
) )
if backup_type in (BackupType.Slip39_Basic, BackupType.Slip39_Advanced): if backup_types.is_slip39_backup_type(backup_type):
identifier = storage_recovery.get_slip39_identifier() identifier = storage_recovery.get_slip39_identifier()
extendable = storage_recovery.get_slip39_extendable() extendable = storage_recovery.get_slip39_extendable()
exponent = storage_recovery.get_slip39_iteration_exponent() exponent = storage_recovery.get_slip39_iteration_exponent()

View File

@ -8,6 +8,7 @@ from trezor.enums import BackupType
from trezor.ui.layouts import confirm_action from trezor.ui.layouts import confirm_action
from trezor.wire import ProcessError from trezor.wire import ProcessError
from .. import backup_types
from . import layout from . import layout
if __debug__: if __debug__:
@ -70,7 +71,7 @@ async def reset_device(msg: ResetDevice) -> Success:
if backup_type == BAK_T_BIP39: if backup_type == BAK_T_BIP39:
# in BIP-39 we store mnemonic string instead of the secret # in BIP-39 we store mnemonic string instead of the secret
secret = bip39.from_data(secret).encode() secret = bip39.from_data(secret).encode()
elif backup_type in (BAK_T_SLIP39_BASIC, BAK_T_SLIP39_ADVANCED): elif backup_types.is_slip39_backup_type(backup_type):
# generate and set SLIP39 parameters # generate and set SLIP39 parameters
storage_device.set_slip39_identifier(slip39.generate_random_identifier()) storage_device.set_slip39_identifier(slip39.generate_random_identifier())
storage_device.set_slip39_extendable(slip39.DEFAULT_EXTENDABLE_FLAG) storage_device.set_slip39_extendable(slip39.DEFAULT_EXTENDABLE_FLAG)
@ -113,11 +114,11 @@ async def _backup_slip39_basic(encrypted_master_secret: bytes) -> None:
group_threshold = 1 group_threshold = 1
# get number of shares # get number of shares
await layout.slip39_show_checklist(0, BAK_T_SLIP39_BASIC) await layout.slip39_show_checklist(0, advanced=False)
share_count = await layout.slip39_prompt_number_of_shares() share_count = await layout.slip39_prompt_number_of_shares()
# get threshold # get threshold
await layout.slip39_show_checklist(1, BAK_T_SLIP39_BASIC) await layout.slip39_show_checklist(1, advanced=False)
share_threshold = await layout.slip39_prompt_threshold(share_count) share_threshold = await layout.slip39_prompt_threshold(share_count)
mnemonics = _get_slip39_mnemonics( mnemonics = _get_slip39_mnemonics(
@ -125,21 +126,21 @@ async def _backup_slip39_basic(encrypted_master_secret: bytes) -> None:
) )
# show and confirm individual shares # show and confirm individual shares
await layout.slip39_show_checklist(2, BAK_T_SLIP39_BASIC) await layout.slip39_show_checklist(2, advanced=False)
await layout.slip39_basic_show_and_confirm_shares(mnemonics[0]) await layout.slip39_basic_show_and_confirm_shares(mnemonics[0])
async def _backup_slip39_advanced(encrypted_master_secret: bytes) -> None: async def _backup_slip39_advanced(encrypted_master_secret: bytes) -> None:
# get number of groups # get number of groups
await layout.slip39_show_checklist(0, BAK_T_SLIP39_ADVANCED) await layout.slip39_show_checklist(0, advanced=True)
groups_count = await layout.slip39_advanced_prompt_number_of_groups() groups_count = await layout.slip39_advanced_prompt_number_of_groups()
# get group threshold # get group threshold
await layout.slip39_show_checklist(1, BAK_T_SLIP39_ADVANCED) await layout.slip39_show_checklist(1, advanced=True)
group_threshold = await layout.slip39_advanced_prompt_group_threshold(groups_count) group_threshold = await layout.slip39_advanced_prompt_group_threshold(groups_count)
# get shares and thresholds # get shares and thresholds
await layout.slip39_show_checklist(2, BAK_T_SLIP39_ADVANCED) await layout.slip39_show_checklist(2, advanced=True)
groups = [] groups = []
for i in range(groups_count): for i in range(groups_count):
share_count = await layout.slip39_prompt_number_of_shares(i) share_count = await layout.slip39_prompt_number_of_shares(i)
@ -206,18 +207,15 @@ def _validate_reset_device(msg: ResetDevice) -> None:
from .. import backup_types from .. import backup_types
backup_type = msg.backup_type or _DEFAULT_BACKUP_TYPE backup_type = msg.backup_type or _DEFAULT_BACKUP_TYPE
if backup_type not in (
BAK_T_BIP39,
BAK_T_SLIP39_BASIC,
BAK_T_SLIP39_ADVANCED,
):
raise ProcessError("Backup type not implemented")
if backup_types.is_slip39_backup_type(backup_type): if backup_types.is_slip39_backup_type(backup_type):
if msg.strength not in (128, 256): if msg.strength not in (128, 256):
raise ProcessError("Invalid strength (has to be 128 or 256 bits)") raise ProcessError("Invalid strength (has to be 128 or 256 bits)")
else: # BIP-39 elif backup_type == BAK_T_BIP39:
if msg.strength not in (128, 192, 256): if msg.strength not in (128, 192, 256):
raise ProcessError("Invalid strength (has to be 128, 192 or 256 bits)") raise ProcessError("Invalid strength (has to be 128, 192 or 256 bits)")
else:
raise ProcessError("Backup type not implemented")
if msg.display_random and (msg.skip_backup or msg.no_backup): if msg.display_random and (msg.skip_backup or msg.no_backup):
raise ProcessError("Can't show internal entropy when backup is skipped") raise ProcessError("Can't show internal entropy when backup is skipped")
if storage_device.is_initialized(): if storage_device.is_initialized():

View File

@ -1,4 +1,4 @@
from typing import TYPE_CHECKING from typing import Sequence
import trezorui2 import trezorui2
from trezor import TR from trezor import TR
@ -10,11 +10,6 @@ from . import RustLayout, confirm_action, show_warning
CONFIRMED = trezorui2.CONFIRMED # global_import_cache CONFIRMED = trezorui2.CONFIRMED # global_import_cache
if TYPE_CHECKING:
from typing import Sequence
from trezor.enums import BackupType
async def show_share_words( async def show_share_words(
share_words: Sequence[str], share_words: Sequence[str],
@ -104,18 +99,14 @@ async def select_word(
return words[result] return words[result]
async def slip39_show_checklist(step: int, backup_type: BackupType) -> None: async def slip39_show_checklist(step: int, advanced: bool) -> None:
from trezor.enums import BackupType
assert backup_type in (BackupType.Slip39_Basic, BackupType.Slip39_Advanced)
items = ( items = (
( (
TR.reset__slip39_checklist_num_shares, TR.reset__slip39_checklist_num_shares,
TR.reset__slip39_checklist_set_threshold, TR.reset__slip39_checklist_set_threshold,
TR.reset__slip39_checklist_write_down, TR.reset__slip39_checklist_write_down,
) )
if backup_type == BackupType.Slip39_Basic if not advanced
else ( else (
TR.reset__slip39_checklist_num_groups, TR.reset__slip39_checklist_num_groups,
TR.reset__slip39_checklist_num_shares, TR.reset__slip39_checklist_num_shares,

View File

@ -1,4 +1,4 @@
from typing import TYPE_CHECKING from typing import Callable, Sequence
import trezorui2 import trezorui2
from trezor import TR from trezor import TR
@ -9,12 +9,6 @@ from trezor.wire.context import wait as ctx_wait
from ..common import interact from ..common import interact
from . import RustLayout, raise_if_not_confirmed from . import RustLayout, raise_if_not_confirmed
if TYPE_CHECKING:
from typing import Callable, Sequence
from trezor.enums import BackupType
CONFIRMED = trezorui2.CONFIRMED # global_import_cache CONFIRMED = trezorui2.CONFIRMED # global_import_cache
@ -112,18 +106,14 @@ async def select_word(
return words[result] return words[result]
async def slip39_show_checklist(step: int, backup_type: BackupType) -> None: async def slip39_show_checklist(step: int, advanced: bool) -> None:
from trezor.enums import BackupType
assert backup_type in (BackupType.Slip39_Basic, BackupType.Slip39_Advanced)
items = ( items = (
( (
TR.reset__slip39_checklist_set_num_shares, TR.reset__slip39_checklist_set_num_shares,
TR.reset__slip39_checklist_set_threshold, TR.reset__slip39_checklist_set_threshold,
TR.reset__slip39_checklist_write_down_recovery, TR.reset__slip39_checklist_write_down_recovery,
) )
if backup_type == BackupType.Slip39_Basic if not advanced
else ( else (
TR.reset__slip39_checklist_set_num_groups, TR.reset__slip39_checklist_set_num_groups,
TR.reset__slip39_checklist_set_num_shares, TR.reset__slip39_checklist_set_num_shares,