From dd6ce901e6daa6ed3a3ea80808b3e57df8481534 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ioan=20Biz=C4=83u?= Date: Wed, 24 Apr 2024 12:30:53 +0200 Subject: [PATCH] refactor(core): make 1-of-1 SLIP39 backups use same messaging as BIP39 --- .../apps/management/reset_device/__init__.py | 42 +++++++++++++------ .../apps/management/reset_device/layout.py | 16 +++---- core/src/trezor/ui/layouts/tr/reset.py | 2 +- core/src/trezor/ui/layouts/tt/reset.py | 2 +- 4 files changed, 40 insertions(+), 22 deletions(-) diff --git a/core/src/apps/management/reset_device/__init__.py b/core/src/apps/management/reset_device/__init__.py index 2e1380332..9f93f6865 100644 --- a/core/src/apps/management/reset_device/__init__.py +++ b/core/src/apps/management/reset_device/__init__.py @@ -121,10 +121,14 @@ async def _backup_slip39_basic(encrypted_master_secret: bytes) -> None: await layout.slip39_show_checklist(1, BAK_T_SLIP39_BASIC) share_threshold = await layout.slip39_prompt_threshold(share_count) - await _backup_slip39( + mnemonics = await _backup_slip39( encrypted_master_secret, group_threshold, [(share_threshold, share_count)] ) + # show and confirm individual shares + await layout.slip39_show_checklist(2, BAK_T_SLIP39_BASIC) + await layout.slip39_basic_show_and_confirm_shares(mnemonics[0]) + async def _backup_slip39_advanced(encrypted_master_secret: bytes) -> None: # get number of groups @@ -143,9 +147,30 @@ async def _backup_slip39_advanced(encrypted_master_secret: bytes) -> None: share_threshold = await layout.slip39_prompt_threshold(share_count, i) groups.append((share_threshold, share_count)) - await _backup_slip39(encrypted_master_secret, group_threshold, groups) + mnemonics = await _backup_slip39(encrypted_master_secret, group_threshold, groups) + + # show and confirm individual shares + await layout.slip39_advanced_show_and_confirm_shares(mnemonics) +async def _backup_slip39_custom( + encrypted_master_secret: bytes, + group_threshold: int, + groups: Collection[tuple[int, int]], +): + mnemonics = await _backup_slip39(encrypted_master_secret, group_threshold, groups) + + # show and confirm individual shares + if len(groups) == 1 and groups[0][0] == 1 and groups[0][1] == 1: + # for a single 1-of-1 group, we use the same layouts as for BIP39 + await layout.show_and_confirm_mnemonic(mnemonics[0][0]) + else: + # TODO: a confirmation screen will be shown: "do you want to create a -of- Shamir backup?" + if len(groups) == 1: + await layout.slip39_basic_show_and_confirm_shares(mnemonics[0]) + else: + await layout.slip39_advanced_show_and_confirm_shares(mnemonics) + async def _backup_slip39( encrypted_master_secret: bytes, group_threshold: int, @@ -157,7 +182,7 @@ async def _backup_slip39( raise ValueError # generate the mnemonics - mnemonics = slip39.split_ems( + return slip39.split_ems( group_threshold, groups, identifier, @@ -165,13 +190,6 @@ async def _backup_slip39( encrypted_master_secret, ) - # show and confirm individual shares - if len(groups) == 1: - await layout.slip39_show_checklist(2, BAK_T_SLIP39_BASIC) - await layout.slip39_basic_show_and_confirm_shares(mnemonics[0]) - else: - await layout.slip39_advanced_show_and_confirm_shares(mnemonics) - def _validate_reset_device(msg: ResetDevice) -> None: from trezor.wire import UnexpectedMessage @@ -225,10 +243,10 @@ async def backup_seed( assert backup_type != BAK_T_BIP39 or group_threshold is None if group_threshold is not None: - await _backup_slip39(mnemonic_secret, group_threshold, groups) + await _backup_slip39_custom(mnemonic_secret, group_threshold, groups) elif backup_type == BAK_T_SLIP39_BASIC: await _backup_slip39_basic(mnemonic_secret) elif backup_type == BAK_T_SLIP39_ADVANCED: await _backup_slip39_advanced(mnemonic_secret) else: - await layout.bip39_show_and_confirm_mnemonic(mnemonic_secret.decode()) + await layout.show_and_confirm_mnemonic(mnemonic_secret.decode()) diff --git a/core/src/apps/management/reset_device/layout.py b/core/src/apps/management/reset_device/layout.py index 70eb22db2..0d31b2aa9 100644 --- a/core/src/apps/management/reset_device/layout.py +++ b/core/src/apps/management/reset_device/layout.py @@ -110,7 +110,7 @@ async def _show_confirmation_success( num_of_shares: int | None = None, group_index: int | None = None, ) -> None: - if share_index is None or num_of_shares is None: # it is a BIP39 backup + if share_index is None or num_of_shares is None: # it is a BIP39 or a 1-of-1 SLIP39 backup subheader = TR.reset__finished_verifying_seed text = "" @@ -150,10 +150,10 @@ async def _show_confirmation_failure() -> None: ) -async def show_backup_warning(slip39: bool = False) -> None: +async def show_backup_warning() -> None: from trezor.ui.layouts.reset import show_warning_backup - await show_warning_backup(slip39) + await show_warning_backup() async def show_backup_success() -> None: @@ -162,11 +162,11 @@ async def show_backup_success() -> None: await show_success_backup() -# BIP39 +# Simple setups: BIP39 or SLIP39 1-of-1 # === -async def bip39_show_and_confirm_mnemonic(mnemonic: str) -> None: +async def show_and_confirm_mnemonic(mnemonic: str) -> None: # warn user about mnemonic safety await show_backup_warning() @@ -181,13 +181,13 @@ async def bip39_show_and_confirm_mnemonic(mnemonic: str) -> None: break # mnemonic is confirmed, go next -# SLIP39 +# Complex setups: SLIP39, except 1-of-1 # === async def slip39_basic_show_and_confirm_shares(shares: Sequence[str]) -> None: # warn user about mnemonic safety - await show_backup_warning(True) + await show_backup_warning() for index, share in enumerate(shares): share_words = share.split(" ") @@ -204,7 +204,7 @@ async def slip39_advanced_show_and_confirm_shares( shares: Sequence[Sequence[str]], ) -> None: # warn user about mnemonic safety - await show_backup_warning(True) + await show_backup_warning() for group_index, group in enumerate(shares): for share_index, share in enumerate(group): diff --git a/core/src/trezor/ui/layouts/tr/reset.py b/core/src/trezor/ui/layouts/tr/reset.py index 28397f117..0c8149b98 100644 --- a/core/src/trezor/ui/layouts/tr/reset.py +++ b/core/src/trezor/ui/layouts/tr/reset.py @@ -258,7 +258,7 @@ async def slip39_advanced_prompt_group_threshold(num_of_groups: int) -> int: ) -async def show_warning_backup(slip39: bool) -> None: +async def show_warning_backup() -> None: await show_warning( "backup_warning", TR.words__title_remember, diff --git a/core/src/trezor/ui/layouts/tt/reset.py b/core/src/trezor/ui/layouts/tt/reset.py index 0dd51dc96..81a5a561f 100644 --- a/core/src/trezor/ui/layouts/tt/reset.py +++ b/core/src/trezor/ui/layouts/tt/reset.py @@ -319,7 +319,7 @@ async def slip39_advanced_prompt_group_threshold(num_of_groups: int) -> int: ) -async def show_warning_backup(slip39: bool) -> None: +async def show_warning_backup() -> None: result = await interact( RustLayout( trezorui2.show_info(