2018-11-08 17:15:49 +00:00
|
|
|
# This file is part of the Trezor project.
|
|
|
|
#
|
2021-11-26 14:50:43 +00:00
|
|
|
# Copyright (C) 2012-2022 SatoshiLabs and contributors
|
2018-11-08 17:15:49 +00:00
|
|
|
#
|
|
|
|
# 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>.
|
|
|
|
|
2018-09-13 16:47:19 +00:00
|
|
|
import os
|
2021-11-30 10:51:52 +00:00
|
|
|
import sys
|
feat(python): add full type information
WIP - typing the trezorctl apps
typing functions trezorlib/cli
addressing most of mypy issue for trezorlib apps and _internal folder
fixing broken device tests by changing asserts in debuglink.py
addressing most of mypy issues in trezorlib/cli folder
adding types to some untyped functions, mypy section in setup.cfg
typing what can be typed, some mypy fixes, resolving circular import issues
importing type objects in "if TYPE_CHECKING:" branch
fixing CI by removing assert in emulator, better ignore comments
CI assert fix, style fixes, new config options
fixup! CI assert fix, style fixes, new config options
type fixes after rebasing on master
fixing python3.6 and 3.7 unittests by importing Literal from typing_extensions
couple mypy and style fixes
fixes and improvements from code review
silencing all but one mypy issues
trial of typing the tools.expect function
fixup! trial of typing the tools.expect function
@expect and @session decorators correctly type-checked
Optional args in CLI where relevant, not using general list/tuple/dict where possible
python/Makefile commands, adding them into CI, ignoring last mypy issue
documenting overload for expect decorator, two mypy fixes coming from that
black style fix
improved typing of decorators, pyright config file
addressing or ignoring pyright errors, replacing mypy in CI by pyright
fixing incomplete assert causing device tests to fail
pyright issue that showed in CI but not locally, printing pyright version in CI
fixup! pyright issue that showed in CI but not locally, printing pyright version in CI
unifying type:ignore statements for pyright usage
resolving PIL.Image issues, pyrightconfig not excluding anything
replacing couple asserts with TypeGuard on safe_issubclass
better error handling of usb1 import for webusb
better error handling of hid import
small typing details found out by strict pyright mode
improvements from code review
chore(python): changing List to Sequence for protobuf messages
small code changes to reflect the protobuf change to Sequence
importing TypedDict from typing_extensions to support 3.6 and 3.7
simplify _format_access_list function
fixup! simplify _format_access_list function
typing tools folder
typing helper-scripts folder
some click typing
enforcing all functions to have typed arguments
reverting the changed argument name in tools
replacing TransportType with Transport
making PinMatrixRequest.type protobuf attribute required
reverting the protobuf change, making argument into get_pin Optional
small fixes in asserts
solving the session decorator type issues
fixup! solving the session decorator type issues
improvements from code review
fixing new pyright errors introduced after version increase
changing -> Iterable to -> Sequence in enumerate_devices, change in wait_for_devices
style change in debuglink.py
chore(python): adding type annotation to Sequences in messages.py
better "self and cls" types on Transport
fixup! better "self and cls" types on Transport
fixing some easy things from strict pyright run
2021-11-03 22:12:53 +00:00
|
|
|
from typing import Any, Callable, Optional, Union
|
2018-09-13 16:47:19 +00:00
|
|
|
|
|
|
|
import click
|
|
|
|
from mnemonic import Mnemonic
|
2021-10-20 11:15:11 +00:00
|
|
|
from typing_extensions import Protocol
|
2018-09-13 16:47:19 +00:00
|
|
|
|
2021-10-20 11:15:11 +00:00
|
|
|
from . import device, messages
|
2020-08-14 08:06:53 +00:00
|
|
|
from .client import MAX_PIN_LENGTH, PASSPHRASE_ON_DEVICE
|
2018-09-13 16:47:19 +00:00
|
|
|
from .exceptions import Cancelled
|
2018-10-02 15:37:03 +00:00
|
|
|
from .messages import PinMatrixRequestType, WordRequestType
|
2018-09-13 16:47:19 +00:00
|
|
|
|
|
|
|
PIN_MATRIX_DESCRIPTION = """
|
2021-02-23 09:39:26 +00:00
|
|
|
Use the numeric keypad or lowercase letters to describe number positions.
|
|
|
|
|
|
|
|
The layout is:
|
|
|
|
|
|
|
|
7 8 9 e r t
|
|
|
|
4 5 6 -or- d f g
|
|
|
|
1 2 3 c v b
|
2018-09-13 16:47:19 +00:00
|
|
|
""".strip()
|
|
|
|
|
|
|
|
RECOVERY_MATRIX_DESCRIPTION = """
|
|
|
|
Use the numeric keypad to describe positions.
|
|
|
|
For the word list use only left and right keys.
|
|
|
|
Use backspace to correct an entry.
|
|
|
|
|
|
|
|
The keypad layout is:
|
|
|
|
7 8 9 7 | 9
|
|
|
|
4 5 6 4 | 6
|
|
|
|
1 2 3 1 | 3
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
PIN_GENERIC = None
|
|
|
|
PIN_CURRENT = PinMatrixRequestType.Current
|
|
|
|
PIN_NEW = PinMatrixRequestType.NewFirst
|
|
|
|
PIN_CONFIRM = PinMatrixRequestType.NewSecond
|
2019-10-18 13:59:12 +00:00
|
|
|
WIPE_CODE_NEW = PinMatrixRequestType.WipeCodeFirst
|
|
|
|
WIPE_CODE_CONFIRM = PinMatrixRequestType.WipeCodeSecond
|
2018-09-13 16:47:19 +00:00
|
|
|
|
2021-11-30 10:51:52 +00:00
|
|
|
# Workaround for limitation of Git Bash
|
|
|
|
# getpass function does not work correctly on Windows when not using a real terminal
|
|
|
|
# (the hidden input is not allowed and it also freezes the script completely)
|
|
|
|
# Details: https://bugs.python.org/issue44762
|
|
|
|
CAN_HANDLE_HIDDEN_INPUT = sys.stdin and sys.stdin.isatty()
|
|
|
|
|
2018-09-13 16:47:19 +00:00
|
|
|
|
2021-10-20 11:15:11 +00:00
|
|
|
class TrezorClientUI(Protocol):
|
2024-02-28 14:11:47 +00:00
|
|
|
def button_request(self, br: messages.ButtonRequest) -> None: ...
|
2021-10-20 11:15:11 +00:00
|
|
|
|
2024-02-28 14:11:47 +00:00
|
|
|
def get_pin(self, code: Optional[PinMatrixRequestType]) -> str: ...
|
2021-10-20 11:15:11 +00:00
|
|
|
|
2024-02-28 14:11:47 +00:00
|
|
|
def get_passphrase(self, available_on_device: bool) -> Union[str, object]: ...
|
2021-10-20 11:15:11 +00:00
|
|
|
|
|
|
|
|
feat(python): add full type information
WIP - typing the trezorctl apps
typing functions trezorlib/cli
addressing most of mypy issue for trezorlib apps and _internal folder
fixing broken device tests by changing asserts in debuglink.py
addressing most of mypy issues in trezorlib/cli folder
adding types to some untyped functions, mypy section in setup.cfg
typing what can be typed, some mypy fixes, resolving circular import issues
importing type objects in "if TYPE_CHECKING:" branch
fixing CI by removing assert in emulator, better ignore comments
CI assert fix, style fixes, new config options
fixup! CI assert fix, style fixes, new config options
type fixes after rebasing on master
fixing python3.6 and 3.7 unittests by importing Literal from typing_extensions
couple mypy and style fixes
fixes and improvements from code review
silencing all but one mypy issues
trial of typing the tools.expect function
fixup! trial of typing the tools.expect function
@expect and @session decorators correctly type-checked
Optional args in CLI where relevant, not using general list/tuple/dict where possible
python/Makefile commands, adding them into CI, ignoring last mypy issue
documenting overload for expect decorator, two mypy fixes coming from that
black style fix
improved typing of decorators, pyright config file
addressing or ignoring pyright errors, replacing mypy in CI by pyright
fixing incomplete assert causing device tests to fail
pyright issue that showed in CI but not locally, printing pyright version in CI
fixup! pyright issue that showed in CI but not locally, printing pyright version in CI
unifying type:ignore statements for pyright usage
resolving PIL.Image issues, pyrightconfig not excluding anything
replacing couple asserts with TypeGuard on safe_issubclass
better error handling of usb1 import for webusb
better error handling of hid import
small typing details found out by strict pyright mode
improvements from code review
chore(python): changing List to Sequence for protobuf messages
small code changes to reflect the protobuf change to Sequence
importing TypedDict from typing_extensions to support 3.6 and 3.7
simplify _format_access_list function
fixup! simplify _format_access_list function
typing tools folder
typing helper-scripts folder
some click typing
enforcing all functions to have typed arguments
reverting the changed argument name in tools
replacing TransportType with Transport
making PinMatrixRequest.type protobuf attribute required
reverting the protobuf change, making argument into get_pin Optional
small fixes in asserts
solving the session decorator type issues
fixup! solving the session decorator type issues
improvements from code review
fixing new pyright errors introduced after version increase
changing -> Iterable to -> Sequence in enumerate_devices, change in wait_for_devices
style change in debuglink.py
chore(python): adding type annotation to Sequences in messages.py
better "self and cls" types on Transport
fixup! better "self and cls" types on Transport
fixing some easy things from strict pyright run
2021-11-03 22:12:53 +00:00
|
|
|
def echo(*args: Any, **kwargs: Any) -> None:
|
2018-10-22 08:28:07 +00:00
|
|
|
return click.echo(*args, err=True, **kwargs)
|
|
|
|
|
|
|
|
|
2021-11-30 10:51:52 +00:00
|
|
|
def prompt(text: str, *, hide_input: bool = False, **kwargs: Any) -> Any:
|
|
|
|
# Disallowing hidden input and warning user when it would cause issues
|
|
|
|
if not CAN_HANDLE_HIDDEN_INPUT and hide_input:
|
|
|
|
hide_input = False
|
|
|
|
text += " (WARNING: will be displayed!)"
|
|
|
|
return click.prompt(text, hide_input=hide_input, err=True, **kwargs)
|
2018-10-22 08:28:07 +00:00
|
|
|
|
|
|
|
|
2018-09-13 16:47:19 +00:00
|
|
|
class ClickUI:
|
feat(python): add full type information
WIP - typing the trezorctl apps
typing functions trezorlib/cli
addressing most of mypy issue for trezorlib apps and _internal folder
fixing broken device tests by changing asserts in debuglink.py
addressing most of mypy issues in trezorlib/cli folder
adding types to some untyped functions, mypy section in setup.cfg
typing what can be typed, some mypy fixes, resolving circular import issues
importing type objects in "if TYPE_CHECKING:" branch
fixing CI by removing assert in emulator, better ignore comments
CI assert fix, style fixes, new config options
fixup! CI assert fix, style fixes, new config options
type fixes after rebasing on master
fixing python3.6 and 3.7 unittests by importing Literal from typing_extensions
couple mypy and style fixes
fixes and improvements from code review
silencing all but one mypy issues
trial of typing the tools.expect function
fixup! trial of typing the tools.expect function
@expect and @session decorators correctly type-checked
Optional args in CLI where relevant, not using general list/tuple/dict where possible
python/Makefile commands, adding them into CI, ignoring last mypy issue
documenting overload for expect decorator, two mypy fixes coming from that
black style fix
improved typing of decorators, pyright config file
addressing or ignoring pyright errors, replacing mypy in CI by pyright
fixing incomplete assert causing device tests to fail
pyright issue that showed in CI but not locally, printing pyright version in CI
fixup! pyright issue that showed in CI but not locally, printing pyright version in CI
unifying type:ignore statements for pyright usage
resolving PIL.Image issues, pyrightconfig not excluding anything
replacing couple asserts with TypeGuard on safe_issubclass
better error handling of usb1 import for webusb
better error handling of hid import
small typing details found out by strict pyright mode
improvements from code review
chore(python): changing List to Sequence for protobuf messages
small code changes to reflect the protobuf change to Sequence
importing TypedDict from typing_extensions to support 3.6 and 3.7
simplify _format_access_list function
fixup! simplify _format_access_list function
typing tools folder
typing helper-scripts folder
some click typing
enforcing all functions to have typed arguments
reverting the changed argument name in tools
replacing TransportType with Transport
making PinMatrixRequest.type protobuf attribute required
reverting the protobuf change, making argument into get_pin Optional
small fixes in asserts
solving the session decorator type issues
fixup! solving the session decorator type issues
improvements from code review
fixing new pyright errors introduced after version increase
changing -> Iterable to -> Sequence in enumerate_devices, change in wait_for_devices
style change in debuglink.py
chore(python): adding type annotation to Sequences in messages.py
better "self and cls" types on Transport
fixup! better "self and cls" types on Transport
fixing some easy things from strict pyright run
2021-11-03 22:12:53 +00:00
|
|
|
def __init__(
|
|
|
|
self, always_prompt: bool = False, passphrase_on_host: bool = False
|
|
|
|
) -> None:
|
2018-11-23 13:47:47 +00:00
|
|
|
self.pinmatrix_shown = False
|
2023-10-19 11:27:50 +00:00
|
|
|
self.last_prompt_shown = ""
|
2018-11-23 13:47:47 +00:00
|
|
|
self.always_prompt = always_prompt
|
2020-01-29 14:19:44 +00:00
|
|
|
self.passphrase_on_host = passphrase_on_host
|
2018-11-23 13:47:47 +00:00
|
|
|
|
2023-10-19 11:27:50 +00:00
|
|
|
def _prompt_for_button(self, br: messages.ButtonRequest) -> str:
|
|
|
|
if br.code == messages.ButtonRequestType.PassphraseEntry:
|
|
|
|
return "Please enter passphrase on your Trezor device."
|
|
|
|
if br.code == messages.ButtonRequestType.PinEntry:
|
|
|
|
return "Please enter PIN on your Trezor device."
|
|
|
|
|
|
|
|
return "Please confirm action on your Trezor device."
|
|
|
|
|
|
|
|
def button_request(self, br: messages.ButtonRequest) -> None:
|
|
|
|
prompt = self._prompt_for_button(br)
|
|
|
|
if prompt != self.last_prompt_shown:
|
|
|
|
echo(prompt)
|
2018-11-23 13:47:47 +00:00
|
|
|
if not self.always_prompt:
|
2023-10-19 11:27:50 +00:00
|
|
|
self.last_prompt_shown = prompt
|
2018-11-23 13:47:47 +00:00
|
|
|
|
feat(python): add full type information
WIP - typing the trezorctl apps
typing functions trezorlib/cli
addressing most of mypy issue for trezorlib apps and _internal folder
fixing broken device tests by changing asserts in debuglink.py
addressing most of mypy issues in trezorlib/cli folder
adding types to some untyped functions, mypy section in setup.cfg
typing what can be typed, some mypy fixes, resolving circular import issues
importing type objects in "if TYPE_CHECKING:" branch
fixing CI by removing assert in emulator, better ignore comments
CI assert fix, style fixes, new config options
fixup! CI assert fix, style fixes, new config options
type fixes after rebasing on master
fixing python3.6 and 3.7 unittests by importing Literal from typing_extensions
couple mypy and style fixes
fixes and improvements from code review
silencing all but one mypy issues
trial of typing the tools.expect function
fixup! trial of typing the tools.expect function
@expect and @session decorators correctly type-checked
Optional args in CLI where relevant, not using general list/tuple/dict where possible
python/Makefile commands, adding them into CI, ignoring last mypy issue
documenting overload for expect decorator, two mypy fixes coming from that
black style fix
improved typing of decorators, pyright config file
addressing or ignoring pyright errors, replacing mypy in CI by pyright
fixing incomplete assert causing device tests to fail
pyright issue that showed in CI but not locally, printing pyright version in CI
fixup! pyright issue that showed in CI but not locally, printing pyright version in CI
unifying type:ignore statements for pyright usage
resolving PIL.Image issues, pyrightconfig not excluding anything
replacing couple asserts with TypeGuard on safe_issubclass
better error handling of usb1 import for webusb
better error handling of hid import
small typing details found out by strict pyright mode
improvements from code review
chore(python): changing List to Sequence for protobuf messages
small code changes to reflect the protobuf change to Sequence
importing TypedDict from typing_extensions to support 3.6 and 3.7
simplify _format_access_list function
fixup! simplify _format_access_list function
typing tools folder
typing helper-scripts folder
some click typing
enforcing all functions to have typed arguments
reverting the changed argument name in tools
replacing TransportType with Transport
making PinMatrixRequest.type protobuf attribute required
reverting the protobuf change, making argument into get_pin Optional
small fixes in asserts
solving the session decorator type issues
fixup! solving the session decorator type issues
improvements from code review
fixing new pyright errors introduced after version increase
changing -> Iterable to -> Sequence in enumerate_devices, change in wait_for_devices
style change in debuglink.py
chore(python): adding type annotation to Sequences in messages.py
better "self and cls" types on Transport
fixup! better "self and cls" types on Transport
fixing some easy things from strict pyright run
2021-11-03 22:12:53 +00:00
|
|
|
def get_pin(self, code: Optional[PinMatrixRequestType] = None) -> str:
|
2018-09-13 16:47:19 +00:00
|
|
|
if code == PIN_CURRENT:
|
|
|
|
desc = "current PIN"
|
|
|
|
elif code == PIN_NEW:
|
|
|
|
desc = "new PIN"
|
|
|
|
elif code == PIN_CONFIRM:
|
|
|
|
desc = "new PIN again"
|
2019-10-18 13:59:12 +00:00
|
|
|
elif code == WIPE_CODE_NEW:
|
|
|
|
desc = "new wipe code"
|
|
|
|
elif code == WIPE_CODE_CONFIRM:
|
|
|
|
desc = "new wipe code again"
|
2018-09-13 16:47:19 +00:00
|
|
|
else:
|
|
|
|
desc = "PIN"
|
2018-11-23 13:47:47 +00:00
|
|
|
|
|
|
|
if not self.pinmatrix_shown:
|
2018-10-22 08:28:07 +00:00
|
|
|
echo(PIN_MATRIX_DESCRIPTION)
|
2018-11-23 13:47:47 +00:00
|
|
|
if not self.always_prompt:
|
|
|
|
self.pinmatrix_shown = True
|
2018-09-13 16:47:19 +00:00
|
|
|
|
|
|
|
while True:
|
2018-11-16 12:36:04 +00:00
|
|
|
try:
|
2021-09-27 10:13:51 +00:00
|
|
|
pin = prompt(f"Please enter {desc}", hide_input=True)
|
2018-11-16 12:36:04 +00:00
|
|
|
except click.Abort:
|
|
|
|
raise Cancelled from None
|
2021-02-23 09:39:26 +00:00
|
|
|
|
|
|
|
# translate letters to numbers if letters were used
|
|
|
|
if all(d in "cvbdfgert" for d in pin):
|
|
|
|
pin = pin.translate(str.maketrans("cvbdfgert", "123456789"))
|
|
|
|
|
2020-03-13 12:23:44 +00:00
|
|
|
if any(d not in "123456789" for d in pin):
|
2021-02-23 09:39:26 +00:00
|
|
|
echo(
|
|
|
|
"The value may only consist of digits 1 to 9 or letters cvbdfgert."
|
|
|
|
)
|
2020-08-14 08:06:53 +00:00
|
|
|
elif len(pin) > MAX_PIN_LENGTH:
|
2021-09-27 10:13:51 +00:00
|
|
|
echo(f"The value must be at most {MAX_PIN_LENGTH} digits in length.")
|
2018-09-13 16:47:19 +00:00
|
|
|
else:
|
|
|
|
return pin
|
|
|
|
|
feat(python): add full type information
WIP - typing the trezorctl apps
typing functions trezorlib/cli
addressing most of mypy issue for trezorlib apps and _internal folder
fixing broken device tests by changing asserts in debuglink.py
addressing most of mypy issues in trezorlib/cli folder
adding types to some untyped functions, mypy section in setup.cfg
typing what can be typed, some mypy fixes, resolving circular import issues
importing type objects in "if TYPE_CHECKING:" branch
fixing CI by removing assert in emulator, better ignore comments
CI assert fix, style fixes, new config options
fixup! CI assert fix, style fixes, new config options
type fixes after rebasing on master
fixing python3.6 and 3.7 unittests by importing Literal from typing_extensions
couple mypy and style fixes
fixes and improvements from code review
silencing all but one mypy issues
trial of typing the tools.expect function
fixup! trial of typing the tools.expect function
@expect and @session decorators correctly type-checked
Optional args in CLI where relevant, not using general list/tuple/dict where possible
python/Makefile commands, adding them into CI, ignoring last mypy issue
documenting overload for expect decorator, two mypy fixes coming from that
black style fix
improved typing of decorators, pyright config file
addressing or ignoring pyright errors, replacing mypy in CI by pyright
fixing incomplete assert causing device tests to fail
pyright issue that showed in CI but not locally, printing pyright version in CI
fixup! pyright issue that showed in CI but not locally, printing pyright version in CI
unifying type:ignore statements for pyright usage
resolving PIL.Image issues, pyrightconfig not excluding anything
replacing couple asserts with TypeGuard on safe_issubclass
better error handling of usb1 import for webusb
better error handling of hid import
small typing details found out by strict pyright mode
improvements from code review
chore(python): changing List to Sequence for protobuf messages
small code changes to reflect the protobuf change to Sequence
importing TypedDict from typing_extensions to support 3.6 and 3.7
simplify _format_access_list function
fixup! simplify _format_access_list function
typing tools folder
typing helper-scripts folder
some click typing
enforcing all functions to have typed arguments
reverting the changed argument name in tools
replacing TransportType with Transport
making PinMatrixRequest.type protobuf attribute required
reverting the protobuf change, making argument into get_pin Optional
small fixes in asserts
solving the session decorator type issues
fixup! solving the session decorator type issues
improvements from code review
fixing new pyright errors introduced after version increase
changing -> Iterable to -> Sequence in enumerate_devices, change in wait_for_devices
style change in debuglink.py
chore(python): adding type annotation to Sequences in messages.py
better "self and cls" types on Transport
fixup! better "self and cls" types on Transport
fixing some easy things from strict pyright run
2021-11-03 22:12:53 +00:00
|
|
|
def get_passphrase(self, available_on_device: bool) -> Union[str, object]:
|
2020-01-29 14:19:44 +00:00
|
|
|
if available_on_device and not self.passphrase_on_host:
|
|
|
|
return PASSPHRASE_ON_DEVICE
|
|
|
|
|
feat(python): add full type information
WIP - typing the trezorctl apps
typing functions trezorlib/cli
addressing most of mypy issue for trezorlib apps and _internal folder
fixing broken device tests by changing asserts in debuglink.py
addressing most of mypy issues in trezorlib/cli folder
adding types to some untyped functions, mypy section in setup.cfg
typing what can be typed, some mypy fixes, resolving circular import issues
importing type objects in "if TYPE_CHECKING:" branch
fixing CI by removing assert in emulator, better ignore comments
CI assert fix, style fixes, new config options
fixup! CI assert fix, style fixes, new config options
type fixes after rebasing on master
fixing python3.6 and 3.7 unittests by importing Literal from typing_extensions
couple mypy and style fixes
fixes and improvements from code review
silencing all but one mypy issues
trial of typing the tools.expect function
fixup! trial of typing the tools.expect function
@expect and @session decorators correctly type-checked
Optional args in CLI where relevant, not using general list/tuple/dict where possible
python/Makefile commands, adding them into CI, ignoring last mypy issue
documenting overload for expect decorator, two mypy fixes coming from that
black style fix
improved typing of decorators, pyright config file
addressing or ignoring pyright errors, replacing mypy in CI by pyright
fixing incomplete assert causing device tests to fail
pyright issue that showed in CI but not locally, printing pyright version in CI
fixup! pyright issue that showed in CI but not locally, printing pyright version in CI
unifying type:ignore statements for pyright usage
resolving PIL.Image issues, pyrightconfig not excluding anything
replacing couple asserts with TypeGuard on safe_issubclass
better error handling of usb1 import for webusb
better error handling of hid import
small typing details found out by strict pyright mode
improvements from code review
chore(python): changing List to Sequence for protobuf messages
small code changes to reflect the protobuf change to Sequence
importing TypedDict from typing_extensions to support 3.6 and 3.7
simplify _format_access_list function
fixup! simplify _format_access_list function
typing tools folder
typing helper-scripts folder
some click typing
enforcing all functions to have typed arguments
reverting the changed argument name in tools
replacing TransportType with Transport
making PinMatrixRequest.type protobuf attribute required
reverting the protobuf change, making argument into get_pin Optional
small fixes in asserts
solving the session decorator type issues
fixup! solving the session decorator type issues
improvements from code review
fixing new pyright errors introduced after version increase
changing -> Iterable to -> Sequence in enumerate_devices, change in wait_for_devices
style change in debuglink.py
chore(python): adding type annotation to Sequences in messages.py
better "self and cls" types on Transport
fixup! better "self and cls" types on Transport
fixing some easy things from strict pyright run
2021-11-03 22:12:53 +00:00
|
|
|
env_passphrase = os.getenv("PASSPHRASE")
|
|
|
|
if env_passphrase is not None:
|
2018-10-22 08:28:07 +00:00
|
|
|
echo("Passphrase required. Using PASSPHRASE environment variable.")
|
feat(python): add full type information
WIP - typing the trezorctl apps
typing functions trezorlib/cli
addressing most of mypy issue for trezorlib apps and _internal folder
fixing broken device tests by changing asserts in debuglink.py
addressing most of mypy issues in trezorlib/cli folder
adding types to some untyped functions, mypy section in setup.cfg
typing what can be typed, some mypy fixes, resolving circular import issues
importing type objects in "if TYPE_CHECKING:" branch
fixing CI by removing assert in emulator, better ignore comments
CI assert fix, style fixes, new config options
fixup! CI assert fix, style fixes, new config options
type fixes after rebasing on master
fixing python3.6 and 3.7 unittests by importing Literal from typing_extensions
couple mypy and style fixes
fixes and improvements from code review
silencing all but one mypy issues
trial of typing the tools.expect function
fixup! trial of typing the tools.expect function
@expect and @session decorators correctly type-checked
Optional args in CLI where relevant, not using general list/tuple/dict where possible
python/Makefile commands, adding them into CI, ignoring last mypy issue
documenting overload for expect decorator, two mypy fixes coming from that
black style fix
improved typing of decorators, pyright config file
addressing or ignoring pyright errors, replacing mypy in CI by pyright
fixing incomplete assert causing device tests to fail
pyright issue that showed in CI but not locally, printing pyright version in CI
fixup! pyright issue that showed in CI but not locally, printing pyright version in CI
unifying type:ignore statements for pyright usage
resolving PIL.Image issues, pyrightconfig not excluding anything
replacing couple asserts with TypeGuard on safe_issubclass
better error handling of usb1 import for webusb
better error handling of hid import
small typing details found out by strict pyright mode
improvements from code review
chore(python): changing List to Sequence for protobuf messages
small code changes to reflect the protobuf change to Sequence
importing TypedDict from typing_extensions to support 3.6 and 3.7
simplify _format_access_list function
fixup! simplify _format_access_list function
typing tools folder
typing helper-scripts folder
some click typing
enforcing all functions to have typed arguments
reverting the changed argument name in tools
replacing TransportType with Transport
making PinMatrixRequest.type protobuf attribute required
reverting the protobuf change, making argument into get_pin Optional
small fixes in asserts
solving the session decorator type issues
fixup! solving the session decorator type issues
improvements from code review
fixing new pyright errors introduced after version increase
changing -> Iterable to -> Sequence in enumerate_devices, change in wait_for_devices
style change in debuglink.py
chore(python): adding type annotation to Sequences in messages.py
better "self and cls" types on Transport
fixup! better "self and cls" types on Transport
fixing some easy things from strict pyright run
2021-11-03 22:12:53 +00:00
|
|
|
return env_passphrase
|
2018-09-13 16:47:19 +00:00
|
|
|
|
|
|
|
while True:
|
2018-11-16 12:36:04 +00:00
|
|
|
try:
|
2018-12-06 14:05:41 +00:00
|
|
|
passphrase = prompt(
|
|
|
|
"Passphrase required",
|
|
|
|
hide_input=True,
|
|
|
|
default="",
|
|
|
|
show_default=False,
|
|
|
|
)
|
2021-11-30 10:51:52 +00:00
|
|
|
# In case user sees the input on the screen, we do not need confirmation
|
|
|
|
if not CAN_HANDLE_HIDDEN_INPUT:
|
|
|
|
return passphrase
|
2018-12-06 14:05:41 +00:00
|
|
|
second = prompt(
|
|
|
|
"Confirm your passphrase",
|
|
|
|
hide_input=True,
|
|
|
|
default="",
|
|
|
|
show_default=False,
|
|
|
|
)
|
2018-11-16 12:36:04 +00:00
|
|
|
if passphrase == second:
|
|
|
|
return passphrase
|
|
|
|
else:
|
|
|
|
echo("Passphrase did not match. Please try again.")
|
|
|
|
except click.Abort:
|
|
|
|
raise Cancelled from None
|
2018-09-13 16:47:19 +00:00
|
|
|
|
|
|
|
|
2021-12-16 10:24:00 +00:00
|
|
|
class ScriptUI:
|
|
|
|
"""Interface to be used by scripts, not directly by user.
|
|
|
|
|
|
|
|
Communicates with a client application using print() and input().
|
|
|
|
|
|
|
|
Lot of `ClickUI` logic is outsourced to the client application, which
|
|
|
|
is responsible for supplying the PIN and passphrase.
|
|
|
|
|
|
|
|
Reference client implementation can be found under `tools/trezorctl_script_client.py`.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def button_request(br: messages.ButtonRequest) -> None:
|
|
|
|
# TODO: send name={br.name} when it will be supported
|
|
|
|
code = br.code.name if br.code else None
|
|
|
|
print(f"?BUTTON code={code} pages={br.pages}")
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_pin(code: Optional[PinMatrixRequestType] = None) -> str:
|
|
|
|
if code is None:
|
|
|
|
print("?PIN")
|
|
|
|
else:
|
|
|
|
print(f"?PIN code={code.name}")
|
|
|
|
|
|
|
|
pin = input()
|
|
|
|
if pin == "CANCEL":
|
|
|
|
raise Cancelled from None
|
|
|
|
elif not pin.startswith(":"):
|
|
|
|
raise RuntimeError("Sent PIN must start with ':'")
|
|
|
|
else:
|
|
|
|
return pin[1:]
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_passphrase(available_on_device: bool) -> Union[str, object]:
|
|
|
|
if available_on_device:
|
|
|
|
print("?PASSPHRASE available_on_device")
|
|
|
|
else:
|
|
|
|
print("?PASSPHRASE")
|
|
|
|
|
|
|
|
passphrase = input()
|
|
|
|
if passphrase == "CANCEL":
|
|
|
|
raise Cancelled from None
|
|
|
|
elif passphrase == "ON_DEVICE":
|
|
|
|
return PASSPHRASE_ON_DEVICE
|
|
|
|
elif not passphrase.startswith(":"):
|
|
|
|
raise RuntimeError("Sent passphrase must start with ':'")
|
|
|
|
else:
|
|
|
|
return passphrase[1:]
|
|
|
|
|
|
|
|
|
feat(python): add full type information
WIP - typing the trezorctl apps
typing functions trezorlib/cli
addressing most of mypy issue for trezorlib apps and _internal folder
fixing broken device tests by changing asserts in debuglink.py
addressing most of mypy issues in trezorlib/cli folder
adding types to some untyped functions, mypy section in setup.cfg
typing what can be typed, some mypy fixes, resolving circular import issues
importing type objects in "if TYPE_CHECKING:" branch
fixing CI by removing assert in emulator, better ignore comments
CI assert fix, style fixes, new config options
fixup! CI assert fix, style fixes, new config options
type fixes after rebasing on master
fixing python3.6 and 3.7 unittests by importing Literal from typing_extensions
couple mypy and style fixes
fixes and improvements from code review
silencing all but one mypy issues
trial of typing the tools.expect function
fixup! trial of typing the tools.expect function
@expect and @session decorators correctly type-checked
Optional args in CLI where relevant, not using general list/tuple/dict where possible
python/Makefile commands, adding them into CI, ignoring last mypy issue
documenting overload for expect decorator, two mypy fixes coming from that
black style fix
improved typing of decorators, pyright config file
addressing or ignoring pyright errors, replacing mypy in CI by pyright
fixing incomplete assert causing device tests to fail
pyright issue that showed in CI but not locally, printing pyright version in CI
fixup! pyright issue that showed in CI but not locally, printing pyright version in CI
unifying type:ignore statements for pyright usage
resolving PIL.Image issues, pyrightconfig not excluding anything
replacing couple asserts with TypeGuard on safe_issubclass
better error handling of usb1 import for webusb
better error handling of hid import
small typing details found out by strict pyright mode
improvements from code review
chore(python): changing List to Sequence for protobuf messages
small code changes to reflect the protobuf change to Sequence
importing TypedDict from typing_extensions to support 3.6 and 3.7
simplify _format_access_list function
fixup! simplify _format_access_list function
typing tools folder
typing helper-scripts folder
some click typing
enforcing all functions to have typed arguments
reverting the changed argument name in tools
replacing TransportType with Transport
making PinMatrixRequest.type protobuf attribute required
reverting the protobuf change, making argument into get_pin Optional
small fixes in asserts
solving the session decorator type issues
fixup! solving the session decorator type issues
improvements from code review
fixing new pyright errors introduced after version increase
changing -> Iterable to -> Sequence in enumerate_devices, change in wait_for_devices
style change in debuglink.py
chore(python): adding type annotation to Sequences in messages.py
better "self and cls" types on Transport
fixup! better "self and cls" types on Transport
fixing some easy things from strict pyright run
2021-11-03 22:12:53 +00:00
|
|
|
def mnemonic_words(
|
|
|
|
expand: bool = False, language: str = "english"
|
|
|
|
) -> Callable[[WordRequestType], str]:
|
2018-09-13 16:47:19 +00:00
|
|
|
if expand:
|
|
|
|
wordlist = Mnemonic(language).wordlist
|
|
|
|
else:
|
feat(python): add full type information
WIP - typing the trezorctl apps
typing functions trezorlib/cli
addressing most of mypy issue for trezorlib apps and _internal folder
fixing broken device tests by changing asserts in debuglink.py
addressing most of mypy issues in trezorlib/cli folder
adding types to some untyped functions, mypy section in setup.cfg
typing what can be typed, some mypy fixes, resolving circular import issues
importing type objects in "if TYPE_CHECKING:" branch
fixing CI by removing assert in emulator, better ignore comments
CI assert fix, style fixes, new config options
fixup! CI assert fix, style fixes, new config options
type fixes after rebasing on master
fixing python3.6 and 3.7 unittests by importing Literal from typing_extensions
couple mypy and style fixes
fixes and improvements from code review
silencing all but one mypy issues
trial of typing the tools.expect function
fixup! trial of typing the tools.expect function
@expect and @session decorators correctly type-checked
Optional args in CLI where relevant, not using general list/tuple/dict where possible
python/Makefile commands, adding them into CI, ignoring last mypy issue
documenting overload for expect decorator, two mypy fixes coming from that
black style fix
improved typing of decorators, pyright config file
addressing or ignoring pyright errors, replacing mypy in CI by pyright
fixing incomplete assert causing device tests to fail
pyright issue that showed in CI but not locally, printing pyright version in CI
fixup! pyright issue that showed in CI but not locally, printing pyright version in CI
unifying type:ignore statements for pyright usage
resolving PIL.Image issues, pyrightconfig not excluding anything
replacing couple asserts with TypeGuard on safe_issubclass
better error handling of usb1 import for webusb
better error handling of hid import
small typing details found out by strict pyright mode
improvements from code review
chore(python): changing List to Sequence for protobuf messages
small code changes to reflect the protobuf change to Sequence
importing TypedDict from typing_extensions to support 3.6 and 3.7
simplify _format_access_list function
fixup! simplify _format_access_list function
typing tools folder
typing helper-scripts folder
some click typing
enforcing all functions to have typed arguments
reverting the changed argument name in tools
replacing TransportType with Transport
making PinMatrixRequest.type protobuf attribute required
reverting the protobuf change, making argument into get_pin Optional
small fixes in asserts
solving the session decorator type issues
fixup! solving the session decorator type issues
improvements from code review
fixing new pyright errors introduced after version increase
changing -> Iterable to -> Sequence in enumerate_devices, change in wait_for_devices
style change in debuglink.py
chore(python): adding type annotation to Sequences in messages.py
better "self and cls" types on Transport
fixup! better "self and cls" types on Transport
fixing some easy things from strict pyright run
2021-11-03 22:12:53 +00:00
|
|
|
wordlist = []
|
2018-09-13 16:47:19 +00:00
|
|
|
|
feat(python): add full type information
WIP - typing the trezorctl apps
typing functions trezorlib/cli
addressing most of mypy issue for trezorlib apps and _internal folder
fixing broken device tests by changing asserts in debuglink.py
addressing most of mypy issues in trezorlib/cli folder
adding types to some untyped functions, mypy section in setup.cfg
typing what can be typed, some mypy fixes, resolving circular import issues
importing type objects in "if TYPE_CHECKING:" branch
fixing CI by removing assert in emulator, better ignore comments
CI assert fix, style fixes, new config options
fixup! CI assert fix, style fixes, new config options
type fixes after rebasing on master
fixing python3.6 and 3.7 unittests by importing Literal from typing_extensions
couple mypy and style fixes
fixes and improvements from code review
silencing all but one mypy issues
trial of typing the tools.expect function
fixup! trial of typing the tools.expect function
@expect and @session decorators correctly type-checked
Optional args in CLI where relevant, not using general list/tuple/dict where possible
python/Makefile commands, adding them into CI, ignoring last mypy issue
documenting overload for expect decorator, two mypy fixes coming from that
black style fix
improved typing of decorators, pyright config file
addressing or ignoring pyright errors, replacing mypy in CI by pyright
fixing incomplete assert causing device tests to fail
pyright issue that showed in CI but not locally, printing pyright version in CI
fixup! pyright issue that showed in CI but not locally, printing pyright version in CI
unifying type:ignore statements for pyright usage
resolving PIL.Image issues, pyrightconfig not excluding anything
replacing couple asserts with TypeGuard on safe_issubclass
better error handling of usb1 import for webusb
better error handling of hid import
small typing details found out by strict pyright mode
improvements from code review
chore(python): changing List to Sequence for protobuf messages
small code changes to reflect the protobuf change to Sequence
importing TypedDict from typing_extensions to support 3.6 and 3.7
simplify _format_access_list function
fixup! simplify _format_access_list function
typing tools folder
typing helper-scripts folder
some click typing
enforcing all functions to have typed arguments
reverting the changed argument name in tools
replacing TransportType with Transport
making PinMatrixRequest.type protobuf attribute required
reverting the protobuf change, making argument into get_pin Optional
small fixes in asserts
solving the session decorator type issues
fixup! solving the session decorator type issues
improvements from code review
fixing new pyright errors introduced after version increase
changing -> Iterable to -> Sequence in enumerate_devices, change in wait_for_devices
style change in debuglink.py
chore(python): adding type annotation to Sequences in messages.py
better "self and cls" types on Transport
fixup! better "self and cls" types on Transport
fixing some easy things from strict pyright run
2021-11-03 22:12:53 +00:00
|
|
|
def expand_word(word: str) -> str:
|
2018-09-13 16:47:19 +00:00
|
|
|
if not expand:
|
|
|
|
return word
|
|
|
|
if word in wordlist:
|
|
|
|
return word
|
|
|
|
matches = [w for w in wordlist if w.startswith(word)]
|
|
|
|
if len(matches) == 1:
|
2019-12-27 10:34:10 +00:00
|
|
|
return matches[0]
|
2018-10-22 08:28:07 +00:00
|
|
|
echo("Choose one of: " + ", ".join(matches))
|
2018-09-13 16:47:19 +00:00
|
|
|
raise KeyError(word)
|
|
|
|
|
feat(python): add full type information
WIP - typing the trezorctl apps
typing functions trezorlib/cli
addressing most of mypy issue for trezorlib apps and _internal folder
fixing broken device tests by changing asserts in debuglink.py
addressing most of mypy issues in trezorlib/cli folder
adding types to some untyped functions, mypy section in setup.cfg
typing what can be typed, some mypy fixes, resolving circular import issues
importing type objects in "if TYPE_CHECKING:" branch
fixing CI by removing assert in emulator, better ignore comments
CI assert fix, style fixes, new config options
fixup! CI assert fix, style fixes, new config options
type fixes after rebasing on master
fixing python3.6 and 3.7 unittests by importing Literal from typing_extensions
couple mypy and style fixes
fixes and improvements from code review
silencing all but one mypy issues
trial of typing the tools.expect function
fixup! trial of typing the tools.expect function
@expect and @session decorators correctly type-checked
Optional args in CLI where relevant, not using general list/tuple/dict where possible
python/Makefile commands, adding them into CI, ignoring last mypy issue
documenting overload for expect decorator, two mypy fixes coming from that
black style fix
improved typing of decorators, pyright config file
addressing or ignoring pyright errors, replacing mypy in CI by pyright
fixing incomplete assert causing device tests to fail
pyright issue that showed in CI but not locally, printing pyright version in CI
fixup! pyright issue that showed in CI but not locally, printing pyright version in CI
unifying type:ignore statements for pyright usage
resolving PIL.Image issues, pyrightconfig not excluding anything
replacing couple asserts with TypeGuard on safe_issubclass
better error handling of usb1 import for webusb
better error handling of hid import
small typing details found out by strict pyright mode
improvements from code review
chore(python): changing List to Sequence for protobuf messages
small code changes to reflect the protobuf change to Sequence
importing TypedDict from typing_extensions to support 3.6 and 3.7
simplify _format_access_list function
fixup! simplify _format_access_list function
typing tools folder
typing helper-scripts folder
some click typing
enforcing all functions to have typed arguments
reverting the changed argument name in tools
replacing TransportType with Transport
making PinMatrixRequest.type protobuf attribute required
reverting the protobuf change, making argument into get_pin Optional
small fixes in asserts
solving the session decorator type issues
fixup! solving the session decorator type issues
improvements from code review
fixing new pyright errors introduced after version increase
changing -> Iterable to -> Sequence in enumerate_devices, change in wait_for_devices
style change in debuglink.py
chore(python): adding type annotation to Sequences in messages.py
better "self and cls" types on Transport
fixup! better "self and cls" types on Transport
fixing some easy things from strict pyright run
2021-11-03 22:12:53 +00:00
|
|
|
def get_word(type: WordRequestType) -> str:
|
2018-09-13 16:47:19 +00:00
|
|
|
assert type == WordRequestType.Plain
|
|
|
|
while True:
|
|
|
|
try:
|
2018-10-22 08:28:07 +00:00
|
|
|
word = prompt("Enter one word of mnemonic")
|
2018-09-13 16:47:19 +00:00
|
|
|
return expand_word(word)
|
|
|
|
except KeyError:
|
|
|
|
pass
|
2018-11-16 12:36:04 +00:00
|
|
|
except click.Abort:
|
2018-09-13 16:47:19 +00:00
|
|
|
raise Cancelled from None
|
|
|
|
|
|
|
|
return get_word
|
|
|
|
|
|
|
|
|
feat(python): add full type information
WIP - typing the trezorctl apps
typing functions trezorlib/cli
addressing most of mypy issue for trezorlib apps and _internal folder
fixing broken device tests by changing asserts in debuglink.py
addressing most of mypy issues in trezorlib/cli folder
adding types to some untyped functions, mypy section in setup.cfg
typing what can be typed, some mypy fixes, resolving circular import issues
importing type objects in "if TYPE_CHECKING:" branch
fixing CI by removing assert in emulator, better ignore comments
CI assert fix, style fixes, new config options
fixup! CI assert fix, style fixes, new config options
type fixes after rebasing on master
fixing python3.6 and 3.7 unittests by importing Literal from typing_extensions
couple mypy and style fixes
fixes and improvements from code review
silencing all but one mypy issues
trial of typing the tools.expect function
fixup! trial of typing the tools.expect function
@expect and @session decorators correctly type-checked
Optional args in CLI where relevant, not using general list/tuple/dict where possible
python/Makefile commands, adding them into CI, ignoring last mypy issue
documenting overload for expect decorator, two mypy fixes coming from that
black style fix
improved typing of decorators, pyright config file
addressing or ignoring pyright errors, replacing mypy in CI by pyright
fixing incomplete assert causing device tests to fail
pyright issue that showed in CI but not locally, printing pyright version in CI
fixup! pyright issue that showed in CI but not locally, printing pyright version in CI
unifying type:ignore statements for pyright usage
resolving PIL.Image issues, pyrightconfig not excluding anything
replacing couple asserts with TypeGuard on safe_issubclass
better error handling of usb1 import for webusb
better error handling of hid import
small typing details found out by strict pyright mode
improvements from code review
chore(python): changing List to Sequence for protobuf messages
small code changes to reflect the protobuf change to Sequence
importing TypedDict from typing_extensions to support 3.6 and 3.7
simplify _format_access_list function
fixup! simplify _format_access_list function
typing tools folder
typing helper-scripts folder
some click typing
enforcing all functions to have typed arguments
reverting the changed argument name in tools
replacing TransportType with Transport
making PinMatrixRequest.type protobuf attribute required
reverting the protobuf change, making argument into get_pin Optional
small fixes in asserts
solving the session decorator type issues
fixup! solving the session decorator type issues
improvements from code review
fixing new pyright errors introduced after version increase
changing -> Iterable to -> Sequence in enumerate_devices, change in wait_for_devices
style change in debuglink.py
chore(python): adding type annotation to Sequences in messages.py
better "self and cls" types on Transport
fixup! better "self and cls" types on Transport
fixing some easy things from strict pyright run
2021-11-03 22:12:53 +00:00
|
|
|
def matrix_words(type: WordRequestType) -> str:
|
2018-09-13 16:47:19 +00:00
|
|
|
while True:
|
|
|
|
try:
|
2018-10-03 13:11:21 +00:00
|
|
|
ch = click.getchar()
|
2018-09-13 16:47:19 +00:00
|
|
|
except (KeyboardInterrupt, EOFError):
|
|
|
|
raise Cancelled from None
|
|
|
|
|
|
|
|
if ch in "\x04\x1b":
|
|
|
|
# Ctrl+D, Esc
|
|
|
|
raise Cancelled
|
|
|
|
if ch in "\x08\x7f":
|
|
|
|
# Backspace, Del
|
|
|
|
return device.RECOVERY_BACK
|
|
|
|
if type == WordRequestType.Matrix6 and ch in "147369":
|
|
|
|
return ch
|
|
|
|
if type == WordRequestType.Matrix9 and ch in "123456789":
|
|
|
|
return ch
|