2022-10-25 10:46:37 +00:00
|
|
|
from typing import TYPE_CHECKING
|
|
|
|
|
2023-05-04 12:16:40 +00:00
|
|
|
from shamir_mnemonic import shamir # type: ignore
|
2019-11-04 14:45:54 +00:00
|
|
|
|
|
|
|
from trezorlib import messages
|
|
|
|
|
|
|
|
from .. import buttons
|
|
|
|
|
2022-10-25 10:46:37 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from trezorlib.debuglink import DebugLink
|
|
|
|
|
2019-11-04 14:45:54 +00:00
|
|
|
|
2023-05-04 12:16:40 +00:00
|
|
|
def confirm_new_wallet(debug: "DebugLink") -> None:
|
|
|
|
assert debug.wait_layout().title().startswith("WALLET CREATION")
|
2019-11-04 14:45:54 +00:00
|
|
|
debug.click(buttons.OK, wait=True)
|
|
|
|
|
|
|
|
|
2023-05-04 12:16:40 +00:00
|
|
|
def confirm_read(debug: "DebugLink", title: str, hold: bool = False) -> None:
|
2019-11-04 14:45:54 +00:00
|
|
|
layout = debug.read_layout()
|
2022-10-25 10:46:37 +00:00
|
|
|
if title == "Caution":
|
2023-05-04 12:16:40 +00:00
|
|
|
# TODO: could look into button texts
|
|
|
|
assert "OK, I UNDERSTAND" in layout.json_str
|
2022-10-25 10:46:37 +00:00
|
|
|
elif title == "Success":
|
2023-05-04 12:16:40 +00:00
|
|
|
# TODO: improve this
|
2022-10-25 10:46:37 +00:00
|
|
|
assert any(
|
2023-05-04 12:16:40 +00:00
|
|
|
text in layout.text_content()
|
|
|
|
for text in (
|
|
|
|
"success",
|
|
|
|
"finished",
|
|
|
|
"done",
|
|
|
|
"has been created",
|
|
|
|
"Keep it safe",
|
|
|
|
)
|
2022-10-25 10:46:37 +00:00
|
|
|
)
|
2023-05-04 12:16:40 +00:00
|
|
|
elif title == "Checklist":
|
|
|
|
assert "number of shares" in layout.text_content().lower()
|
2022-10-25 10:46:37 +00:00
|
|
|
else:
|
2023-05-04 12:16:40 +00:00
|
|
|
assert title.upper() in layout.title()
|
|
|
|
|
2019-11-04 14:45:54 +00:00
|
|
|
debug.click(buttons.OK, wait=True)
|
|
|
|
|
|
|
|
|
2022-10-25 10:46:37 +00:00
|
|
|
def set_selection(debug: "DebugLink", button: tuple[int, int], diff: int) -> None:
|
2023-05-04 12:16:40 +00:00
|
|
|
assert "NumberInputDialog" in debug.read_layout().all_components()
|
2019-11-04 14:45:54 +00:00
|
|
|
for _ in range(diff):
|
2023-05-04 12:16:40 +00:00
|
|
|
debug.click(button)
|
2019-11-04 14:45:54 +00:00
|
|
|
debug.click(buttons.OK, wait=True)
|
|
|
|
|
|
|
|
|
2023-05-04 12:16:40 +00:00
|
|
|
def read_words(
|
|
|
|
debug: "DebugLink", backup_type: messages.BackupType, do_htc: bool = True
|
|
|
|
) -> list[str]:
|
2022-10-25 10:46:37 +00:00
|
|
|
words: list[str] = []
|
2019-11-04 14:45:54 +00:00
|
|
|
layout = debug.read_layout()
|
2023-05-04 12:16:40 +00:00
|
|
|
|
|
|
|
if backup_type == messages.BackupType.Slip39_Advanced:
|
|
|
|
assert layout.title().startswith("GROUP")
|
|
|
|
elif backup_type == messages.BackupType.Slip39_Basic:
|
|
|
|
assert layout.title().startswith("RECOVERY SHARE #")
|
2019-11-04 14:45:54 +00:00
|
|
|
else:
|
2023-05-04 12:16:40 +00:00
|
|
|
assert layout.title() == "RECOVERY SEED"
|
2022-10-25 10:46:37 +00:00
|
|
|
|
|
|
|
# Swiping through all the page and loading the words
|
2023-05-04 12:16:40 +00:00
|
|
|
for _ in range(layout.page_count() - 1):
|
|
|
|
words.extend(layout.seed_words())
|
2022-10-25 10:46:37 +00:00
|
|
|
layout = debug.input(swipe=messages.DebugSwipeDirection.UP, wait=True)
|
2023-05-04 12:16:40 +00:00
|
|
|
assert layout is not None
|
|
|
|
words.extend(layout.seed_words())
|
2022-10-25 10:46:37 +00:00
|
|
|
|
2023-05-04 12:16:40 +00:00
|
|
|
# There is hold-to-confirm button
|
|
|
|
if do_htc:
|
|
|
|
debug.click_hold(buttons.OK, hold_ms=1500)
|
|
|
|
else:
|
|
|
|
# It would take a very long time to test 16-of-16 with doing 1500 ms HTC after
|
|
|
|
# each word set
|
|
|
|
debug.press_yes()
|
2019-11-04 14:45:54 +00:00
|
|
|
|
|
|
|
return words
|
|
|
|
|
|
|
|
|
2022-10-25 10:46:37 +00:00
|
|
|
def confirm_words(debug: "DebugLink", words: list[str]) -> None:
|
2019-11-04 14:45:54 +00:00
|
|
|
layout = debug.wait_layout()
|
2023-05-04 12:16:40 +00:00
|
|
|
assert "Select word" in layout.text_content()
|
2019-11-04 14:45:54 +00:00
|
|
|
for _ in range(3):
|
2019-11-15 14:54:16 +00:00
|
|
|
# "Select word 3 of 20"
|
|
|
|
# ^
|
2023-05-04 12:16:40 +00:00
|
|
|
word_pos = int(layout.text_content().split()[2])
|
2022-10-25 10:46:37 +00:00
|
|
|
# Unifying both the buttons and words to lowercase
|
2023-05-04 12:16:40 +00:00
|
|
|
btn_texts = [text.lower() for text in layout.button_contents()]
|
2022-10-25 10:46:37 +00:00
|
|
|
wanted_word = words[word_pos - 1].lower()
|
|
|
|
button_pos = btn_texts.index(wanted_word)
|
2019-11-15 14:54:16 +00:00
|
|
|
layout = debug.click(buttons.RESET_WORD_CHECK[button_pos], wait=True)
|
2019-11-04 14:45:54 +00:00
|
|
|
|
|
|
|
|
2022-10-25 10:46:37 +00:00
|
|
|
def validate_mnemonics(mnemonics: list[str], expected_ems: bytes) -> None:
|
2019-11-04 14:45:54 +00:00
|
|
|
# We expect these combinations to recreate the secret properly
|
|
|
|
# In case of click tests the mnemonics are always XofX so no need for combinations
|
2021-02-03 12:39:26 +00:00
|
|
|
groups = shamir.decode_mnemonics(mnemonics)
|
|
|
|
ems = shamir.recover_ems(groups)
|
|
|
|
assert expected_ems == ems.ciphertext
|