2019-10-18 11:31:57 +00:00
|
|
|
# This file is part of the Trezor project.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2012-2019 SatoshiLabs and contributors
|
|
|
|
#
|
|
|
|
# This library is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser General Public License version 3
|
|
|
|
# as published by the Free Software Foundation.
|
|
|
|
#
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the License along with this library.
|
|
|
|
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
|
|
|
|
|
2023-05-04 12:16:40 +00:00
|
|
|
from contextlib import contextmanager
|
|
|
|
from typing import TYPE_CHECKING, Generator
|
2022-10-25 10:46:37 +00:00
|
|
|
|
2019-09-27 13:38:15 +00:00
|
|
|
import pytest
|
|
|
|
|
2024-09-02 11:01:16 +00:00
|
|
|
from trezorlib import device, messages
|
|
|
|
from trezorlib.debuglink import LayoutType
|
2019-09-27 13:38:15 +00:00
|
|
|
|
2023-05-04 12:16:40 +00:00
|
|
|
from ..common import MNEMONIC12, MNEMONIC_SLIP39_BASIC_20_3of6
|
2019-10-25 08:07:09 +00:00
|
|
|
from . import recovery
|
2019-09-27 13:38:15 +00:00
|
|
|
|
2022-10-25 10:46:37 +00:00
|
|
|
if TYPE_CHECKING:
|
2023-05-04 12:16:40 +00:00
|
|
|
from trezorlib.debuglink import DebugLink
|
2022-10-25 10:46:37 +00:00
|
|
|
|
2023-08-15 15:58:32 +00:00
|
|
|
from ..device_handler import BackgroundDeviceHandler
|
|
|
|
|
2019-09-27 13:38:15 +00:00
|
|
|
|
2024-09-02 11:01:16 +00:00
|
|
|
pytestmark = pytest.mark.models("core")
|
2023-05-04 12:16:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
def prepare_recovery_and_evaluate(
|
|
|
|
device_handler: "BackgroundDeviceHandler",
|
|
|
|
) -> Generator["DebugLink", None, None]:
|
2019-10-16 15:39:06 +00:00
|
|
|
features = device_handler.features()
|
|
|
|
debug = device_handler.debuglink()
|
|
|
|
assert features.initialized is False
|
2023-05-04 12:16:40 +00:00
|
|
|
device_handler.run(device.recover, pin_protection=False) # type: ignore
|
2019-09-27 13:38:15 +00:00
|
|
|
|
2023-05-04 12:16:40 +00:00
|
|
|
yield debug
|
2019-10-16 15:39:06 +00:00
|
|
|
|
|
|
|
assert isinstance(device_handler.result(), messages.Success)
|
|
|
|
features = device_handler.features()
|
|
|
|
assert features.initialized is True
|
2024-05-23 14:09:16 +00:00
|
|
|
assert features.recovery_status == messages.RecoveryStatus.Nothing
|
2023-05-04 12:16:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.setup_client(uninitialized=True)
|
|
|
|
def test_recovery_slip39_basic(device_handler: "BackgroundDeviceHandler"):
|
|
|
|
with prepare_recovery_and_evaluate(device_handler) as debug:
|
|
|
|
recovery.confirm_recovery(debug)
|
2024-09-02 11:01:16 +00:00
|
|
|
if debug.layout_type is LayoutType.TR:
|
2024-06-04 10:47:09 +00:00
|
|
|
recovery.confirm_recovery(debug)
|
2023-05-04 12:16:40 +00:00
|
|
|
|
|
|
|
recovery.select_number_of_words(debug)
|
|
|
|
recovery.enter_shares(debug, MNEMONIC_SLIP39_BASIC_20_3of6)
|
|
|
|
recovery.finalize(debug)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.setup_client(uninitialized=True)
|
|
|
|
def test_recovery_bip39(device_handler: "BackgroundDeviceHandler"):
|
|
|
|
with prepare_recovery_and_evaluate(device_handler) as debug:
|
|
|
|
recovery.confirm_recovery(debug)
|
2024-09-02 11:01:16 +00:00
|
|
|
if debug.layout_type is LayoutType.TR:
|
2024-06-04 10:47:09 +00:00
|
|
|
recovery.confirm_recovery(debug)
|
2023-05-04 12:16:40 +00:00
|
|
|
|
|
|
|
recovery.select_number_of_words(debug, num_of_words=12)
|
|
|
|
recovery.enter_seed(debug, MNEMONIC12.split())
|
|
|
|
recovery.finalize(debug)
|
2024-01-29 13:38:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.setup_client(uninitialized=True)
|
|
|
|
def test_recovery_bip39_previous_word(device_handler: "BackgroundDeviceHandler"):
|
|
|
|
with prepare_recovery_and_evaluate(device_handler) as debug:
|
|
|
|
recovery.confirm_recovery(debug)
|
2024-09-02 11:01:16 +00:00
|
|
|
if debug.layout_type is LayoutType.TR:
|
2024-06-04 10:47:09 +00:00
|
|
|
recovery.confirm_recovery(debug)
|
2024-01-29 13:38:02 +00:00
|
|
|
|
|
|
|
recovery.select_number_of_words(debug, num_of_words=12)
|
|
|
|
seed_words: list[str] = MNEMONIC12.split()
|
|
|
|
bad_indexes = {1: seed_words[-1], 7: seed_words[0]}
|
|
|
|
recovery.enter_seed_previous_correct(debug, seed_words, bad_indexes)
|
|
|
|
recovery.finalize(debug)
|