2019-10-18 11:36:08 +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>.
|
|
|
|
|
2024-05-27 22:19:01 +00:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from trezorlib import device, models
|
2022-01-28 20:04:50 +00:00
|
|
|
from trezorlib.debuglink import DebugLink
|
2024-04-23 10:26:46 +00:00
|
|
|
from trezorlib.messages import RecoveryStatus
|
2019-10-18 11:36:08 +00:00
|
|
|
|
|
|
|
from .. import buttons
|
2019-11-11 10:07:23 +00:00
|
|
|
from ..click_tests import recovery
|
|
|
|
from ..common import MNEMONIC_SLIP39_ADVANCED_20, MNEMONIC_SLIP39_BASIC_20_3of6
|
2019-10-18 11:36:08 +00:00
|
|
|
from ..device_handler import BackgroundDeviceHandler
|
2023-03-30 10:31:09 +00:00
|
|
|
from ..emulators import Emulator
|
2019-10-25 12:04:05 +00:00
|
|
|
from ..upgrade_tests import core_only
|
2019-10-18 11:36:08 +00:00
|
|
|
|
|
|
|
|
2023-03-30 10:31:09 +00:00
|
|
|
def _restart(
|
|
|
|
device_handler: BackgroundDeviceHandler, core_emulator: Emulator
|
|
|
|
) -> DebugLink:
|
|
|
|
device_handler.restart(core_emulator)
|
2019-11-11 10:07:23 +00:00
|
|
|
return device_handler.debuglink()
|
|
|
|
|
|
|
|
|
2019-10-22 11:55:17 +00:00
|
|
|
@core_only
|
2023-03-30 10:31:09 +00:00
|
|
|
def test_abort(core_emulator: Emulator):
|
|
|
|
device_handler = BackgroundDeviceHandler(core_emulator.client)
|
2019-10-22 11:55:17 +00:00
|
|
|
debug = device_handler.debuglink()
|
|
|
|
features = device_handler.features()
|
|
|
|
|
2024-05-27 22:19:01 +00:00
|
|
|
if debug.model is models.T3T1:
|
|
|
|
pytest.skip("abort not supported on T3T1")
|
|
|
|
|
2024-05-23 14:09:16 +00:00
|
|
|
assert features.recovery_status == RecoveryStatus.Nothing
|
2019-10-18 11:36:08 +00:00
|
|
|
|
2019-10-22 11:55:17 +00:00
|
|
|
device_handler.run(device.recover, pin_protection=False)
|
2023-05-04 12:17:12 +00:00
|
|
|
|
2024-04-28 18:28:51 +00:00
|
|
|
assert debug.wait_layout().title().lower() == "recover wallet"
|
2019-10-18 11:36:08 +00:00
|
|
|
|
2019-10-22 11:55:17 +00:00
|
|
|
layout = debug.click(buttons.OK, wait=True)
|
2023-05-12 07:18:57 +00:00
|
|
|
assert "number of words" in layout.text_content()
|
2019-10-18 11:36:08 +00:00
|
|
|
|
2023-03-30 10:31:09 +00:00
|
|
|
debug = _restart(device_handler, core_emulator)
|
2019-10-22 11:55:17 +00:00
|
|
|
features = device_handler.features()
|
2019-10-18 11:36:08 +00:00
|
|
|
|
2024-05-23 14:09:16 +00:00
|
|
|
assert features.recovery_status == RecoveryStatus.Recovery
|
2019-10-18 11:36:08 +00:00
|
|
|
|
2019-10-22 11:55:17 +00:00
|
|
|
# no waiting for layout because layout doesn't change
|
2023-05-12 07:18:57 +00:00
|
|
|
assert "number of words" in debug.read_layout().text_content()
|
2023-07-27 13:55:01 +00:00
|
|
|
# clicking at 24 in word choice (the same coords as CANCEL)
|
|
|
|
layout = debug.click(buttons.CANCEL, wait=True)
|
|
|
|
|
|
|
|
# Cancelling the backup
|
|
|
|
assert "Enter your backup" in debug.read_layout().text_content()
|
2019-10-22 11:55:17 +00:00
|
|
|
layout = debug.click(buttons.CANCEL, wait=True)
|
2019-10-18 11:36:08 +00:00
|
|
|
|
2024-04-28 18:28:51 +00:00
|
|
|
assert layout.title().lower() in ("abort recovery", "cancel recovery")
|
2019-10-22 11:55:17 +00:00
|
|
|
layout = debug.click(buttons.OK, wait=True)
|
2019-10-18 11:36:08 +00:00
|
|
|
|
2023-05-04 12:17:12 +00:00
|
|
|
assert layout.main_component() == "Homescreen"
|
2019-10-22 11:55:17 +00:00
|
|
|
features = device_handler.features()
|
2024-05-23 14:09:16 +00:00
|
|
|
assert features.recovery_status == RecoveryStatus.Nothing
|
2019-11-11 10:07:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
@core_only
|
2023-03-30 10:31:09 +00:00
|
|
|
def test_recovery_single_reset(core_emulator: Emulator):
|
|
|
|
device_handler = BackgroundDeviceHandler(core_emulator.client)
|
2019-11-11 10:07:23 +00:00
|
|
|
debug = device_handler.debuglink()
|
|
|
|
features = device_handler.features()
|
|
|
|
|
|
|
|
assert features.initialized is False
|
2024-05-23 14:09:16 +00:00
|
|
|
assert features.recovery_status == RecoveryStatus.Nothing
|
2019-11-11 10:07:23 +00:00
|
|
|
|
|
|
|
device_handler.run(device.recover, pin_protection=False)
|
2023-05-04 12:17:12 +00:00
|
|
|
|
2019-11-11 10:07:23 +00:00
|
|
|
recovery.confirm_recovery(debug)
|
|
|
|
|
|
|
|
recovery.select_number_of_words(debug)
|
|
|
|
|
2023-03-30 10:31:09 +00:00
|
|
|
debug = _restart(device_handler, core_emulator)
|
2019-11-11 10:07:23 +00:00
|
|
|
features = device_handler.features()
|
2024-05-23 14:09:16 +00:00
|
|
|
assert features.recovery_status == RecoveryStatus.Recovery
|
2019-11-11 10:07:23 +00:00
|
|
|
|
|
|
|
# we need to enter the number of words again, that's a feature
|
2023-05-12 09:19:35 +00:00
|
|
|
recovery.select_number_of_words(debug, wait=False)
|
2019-11-11 10:07:23 +00:00
|
|
|
recovery.enter_shares(debug, MNEMONIC_SLIP39_BASIC_20_3of6)
|
|
|
|
recovery.finalize(debug)
|
|
|
|
|
|
|
|
features = device_handler.features()
|
|
|
|
assert features.initialized is True
|
2024-05-23 14:09:16 +00:00
|
|
|
assert features.recovery_status == RecoveryStatus.Nothing
|
2019-11-11 10:07:23 +00:00
|
|
|
|
|
|
|
|
2021-05-26 10:01:47 +00:00
|
|
|
@core_only
|
2023-03-30 10:31:09 +00:00
|
|
|
def test_recovery_on_old_wallet(core_emulator: Emulator):
|
2021-05-26 10:01:47 +00:00
|
|
|
"""Check that the recovery workflow started on a disconnected device can survive
|
|
|
|
handling by the old Wallet.
|
|
|
|
|
|
|
|
While Suite will send a RecoveryDevice message and hook into the running recovery
|
|
|
|
flow, old Wallet can't do that and instead must repeatedly ask for features (via
|
|
|
|
Initialize+GetFeatures). At minimum, these two messages must not interrupt the
|
|
|
|
running recovery.
|
|
|
|
"""
|
2023-03-30 10:31:09 +00:00
|
|
|
device_handler = BackgroundDeviceHandler(core_emulator.client)
|
2021-05-26 10:01:47 +00:00
|
|
|
debug = device_handler.debuglink()
|
|
|
|
features = device_handler.features()
|
|
|
|
|
|
|
|
assert features.initialized is False
|
2024-05-23 14:09:16 +00:00
|
|
|
assert features.recovery_status == RecoveryStatus.Nothing
|
2021-05-26 10:01:47 +00:00
|
|
|
|
|
|
|
# enter recovery mode
|
|
|
|
device_handler.run(device.recover, pin_protection=False)
|
2023-05-04 12:17:12 +00:00
|
|
|
|
2021-05-26 10:01:47 +00:00
|
|
|
recovery.confirm_recovery(debug)
|
|
|
|
|
|
|
|
# restart to get into stand-alone recovery
|
2023-03-30 10:31:09 +00:00
|
|
|
debug = _restart(device_handler, core_emulator)
|
2021-05-26 10:01:47 +00:00
|
|
|
features = device_handler.features()
|
2024-05-23 14:09:16 +00:00
|
|
|
assert features.recovery_status == RecoveryStatus.Recovery
|
2021-05-26 10:01:47 +00:00
|
|
|
|
|
|
|
# enter number of words
|
2023-05-12 09:19:35 +00:00
|
|
|
recovery.select_number_of_words(debug, wait=False)
|
2021-05-26 10:01:47 +00:00
|
|
|
|
|
|
|
first_share = MNEMONIC_SLIP39_BASIC_20_3of6[0]
|
|
|
|
words = first_share.split(" ")
|
|
|
|
|
|
|
|
# start entering first share
|
2024-06-03 23:24:09 +00:00
|
|
|
assert "Enter each word of your wallet backup" in debug.read_layout().text_content()
|
2021-05-26 10:01:47 +00:00
|
|
|
debug.press_yes()
|
2023-05-04 12:17:12 +00:00
|
|
|
assert debug.wait_layout().main_component() == "MnemonicKeyboard"
|
2021-05-26 10:01:47 +00:00
|
|
|
|
|
|
|
# enter first word
|
|
|
|
debug.input(words[0])
|
|
|
|
layout = debug.wait_layout()
|
|
|
|
|
|
|
|
# while keyboard is open, hit the device with Initialize/GetFeatures
|
|
|
|
device_handler.client.init_device()
|
|
|
|
device_handler.client.refresh_features()
|
|
|
|
|
|
|
|
# try entering remaining 19 words
|
|
|
|
for word in words[1:]:
|
2023-05-04 12:17:12 +00:00
|
|
|
assert layout.main_component() == "MnemonicKeyboard"
|
2021-05-26 10:01:47 +00:00
|
|
|
debug.input(word)
|
|
|
|
layout = debug.wait_layout()
|
|
|
|
|
|
|
|
# check that we entered the first share successfully
|
2024-05-27 22:19:01 +00:00
|
|
|
assert "2 more shares needed" in layout.text_content()
|
2021-05-26 10:01:47 +00:00
|
|
|
|
|
|
|
# try entering the remaining shares
|
|
|
|
for share in MNEMONIC_SLIP39_BASIC_20_3of6[1:3]:
|
|
|
|
recovery.enter_share(debug, share)
|
|
|
|
|
|
|
|
recovery.finalize(debug)
|
|
|
|
|
|
|
|
# check that the recovery succeeded
|
|
|
|
features = device_handler.features()
|
|
|
|
assert features.initialized is True
|
2024-05-23 14:09:16 +00:00
|
|
|
assert features.recovery_status == RecoveryStatus.Nothing
|
2021-05-26 10:01:47 +00:00
|
|
|
|
|
|
|
|
2019-11-11 10:07:23 +00:00
|
|
|
@core_only
|
2023-03-30 10:31:09 +00:00
|
|
|
def test_recovery_multiple_resets(core_emulator: Emulator):
|
2022-01-28 20:04:50 +00:00
|
|
|
def enter_shares_with_restarts(debug: DebugLink) -> None:
|
2019-11-11 10:07:23 +00:00
|
|
|
shares = MNEMONIC_SLIP39_ADVANCED_20
|
|
|
|
layout = debug.read_layout()
|
2024-06-03 23:24:09 +00:00
|
|
|
expected_text = "Enter each word of your wallet backup"
|
2019-11-11 10:07:23 +00:00
|
|
|
remaining = len(shares)
|
|
|
|
for share in shares:
|
2023-05-04 12:17:12 +00:00
|
|
|
assert expected_text in layout.text_content()
|
2019-11-11 10:07:23 +00:00
|
|
|
layout = recovery.enter_share(debug, share)
|
|
|
|
remaining -= 1
|
2022-10-25 10:46:37 +00:00
|
|
|
expected_text = "You have entered"
|
2023-03-30 10:31:09 +00:00
|
|
|
debug = _restart(device_handler, core_emulator)
|
2019-11-11 10:07:23 +00:00
|
|
|
|
2024-05-22 00:02:20 +00:00
|
|
|
assert "Wallet recovery completed" in layout.text_content()
|
2019-11-11 10:07:23 +00:00
|
|
|
|
2023-03-30 10:31:09 +00:00
|
|
|
device_handler = BackgroundDeviceHandler(core_emulator.client)
|
2019-11-11 10:07:23 +00:00
|
|
|
debug = device_handler.debuglink()
|
|
|
|
features = device_handler.features()
|
|
|
|
|
|
|
|
assert features.initialized is False
|
2024-05-23 14:09:16 +00:00
|
|
|
assert features.recovery_status == RecoveryStatus.Nothing
|
2019-11-11 10:07:23 +00:00
|
|
|
|
|
|
|
# start device and recovery
|
|
|
|
device_handler.run(device.recover, pin_protection=False)
|
2023-05-04 12:17:12 +00:00
|
|
|
|
2019-11-11 10:07:23 +00:00
|
|
|
recovery.confirm_recovery(debug)
|
|
|
|
|
|
|
|
# set number of words
|
|
|
|
recovery.select_number_of_words(debug)
|
|
|
|
|
|
|
|
# restart
|
2023-03-30 10:31:09 +00:00
|
|
|
debug = _restart(device_handler, core_emulator)
|
2019-11-11 10:07:23 +00:00
|
|
|
features = device_handler.features()
|
2024-05-23 14:09:16 +00:00
|
|
|
assert features.recovery_status == RecoveryStatus.Recovery
|
2019-11-11 10:07:23 +00:00
|
|
|
|
|
|
|
# enter the number of words again, that's a feature!
|
2023-05-12 09:19:35 +00:00
|
|
|
recovery.select_number_of_words(debug, wait=False)
|
2019-11-11 10:07:23 +00:00
|
|
|
|
|
|
|
# enter shares and restart after each one
|
|
|
|
enter_shares_with_restarts(debug)
|
|
|
|
debug = device_handler.debuglink()
|
2023-05-04 12:17:12 +00:00
|
|
|
assert debug.read_layout().main_component() == "Homescreen"
|
2019-11-11 10:07:23 +00:00
|
|
|
|
|
|
|
features = device_handler.features()
|
|
|
|
assert features.initialized is True
|
2024-05-23 14:09:16 +00:00
|
|
|
assert features.recovery_status == RecoveryStatus.Nothing
|