python/trezorctl: split trezorctl into separate modules
Instead of all commands (like `load-device`, `change-pin`,
`tezos-sign-tx`, `ethereum-verify-message`...) living in trezorctl.py,
each functional group is now defined in a separate file.
With that, better structuring of the trezorctl command becomes
available:
- instead of `trezorctl set-label`, use `trezorctl set label`
- instead of `trezorctl change-pin`, use `trezorctl set pin`
- instead of `trezorctl enable-passphrase`, use `trezorctl set
passphrase enabled`
For common commands, such as `sign-tx`, it is possible to use the
currency name or shortcut:
- `trezorctl btc sign-tx`
- `trezorctl ethereum sign-tx`
- `trezorctl xtz sign-tx`
- `trezorctl doge sign-tx`
etc.
Some aliases have been retained for better compatibility. For others,
refer to `trezorctl --help` and `trezorctl <command> --help`.
2019-09-10 13:00:27 +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
|
python/trezorctl: split trezorctl into separate modules
Instead of all commands (like `load-device`, `change-pin`,
`tezos-sign-tx`, `ethereum-verify-message`...) living in trezorctl.py,
each functional group is now defined in a separate file.
With that, better structuring of the trezorctl command becomes
available:
- instead of `trezorctl set-label`, use `trezorctl set label`
- instead of `trezorctl change-pin`, use `trezorctl set pin`
- instead of `trezorctl enable-passphrase`, use `trezorctl set
passphrase enabled`
For common commands, such as `sign-tx`, it is possible to use the
currency name or shortcut:
- `trezorctl btc sign-tx`
- `trezorctl ethereum sign-tx`
- `trezorctl xtz sign-tx`
- `trezorctl doge sign-tx`
etc.
Some aliases have been retained for better compatibility. For others,
refer to `trezorctl --help` and `trezorctl <command> --help`.
2019-09-10 13:00:27 +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>.
|
|
|
|
|
2024-12-02 14:44:10 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-03-24 15:02:48 +00:00
|
|
|
import functools
|
2024-12-02 14:44:10 +00:00
|
|
|
import logging
|
|
|
|
import os
|
2020-03-24 15:02:48 +00:00
|
|
|
import sys
|
2024-12-02 14:44:10 +00:00
|
|
|
import typing as t
|
2020-08-26 14:01:58 +00:00
|
|
|
from contextlib import contextmanager
|
2020-03-24 15:02:48 +00:00
|
|
|
|
python/trezorctl: split trezorctl into separate modules
Instead of all commands (like `load-device`, `change-pin`,
`tezos-sign-tx`, `ethereum-verify-message`...) living in trezorctl.py,
each functional group is now defined in a separate file.
With that, better structuring of the trezorctl command becomes
available:
- instead of `trezorctl set-label`, use `trezorctl set label`
- instead of `trezorctl change-pin`, use `trezorctl set pin`
- instead of `trezorctl enable-passphrase`, use `trezorctl set
passphrase enabled`
For common commands, such as `sign-tx`, it is possible to use the
currency name or shortcut:
- `trezorctl btc sign-tx`
- `trezorctl ethereum sign-tx`
- `trezorctl xtz sign-tx`
- `trezorctl doge sign-tx`
etc.
Some aliases have been retained for better compatibility. For others,
refer to `trezorctl --help` and `trezorctl <command> --help`.
2019-09-10 13:00:27 +00:00
|
|
|
import click
|
|
|
|
|
2024-12-02 14:44:10 +00:00
|
|
|
from .. import exceptions, transport, ui
|
|
|
|
from ..client import ProtocolVersion, TrezorClient
|
|
|
|
from ..messages import Capability
|
|
|
|
from ..transport import Transport
|
|
|
|
from ..transport.session import Session, SessionV1, SessionV2
|
|
|
|
from ..transport.thp.channel_database import get_channel_db
|
|
|
|
|
|
|
|
LOG = logging.getLogger(__name__)
|
2020-03-24 15:02:48 +00:00
|
|
|
|
2024-12-02 14:44:10 +00:00
|
|
|
if t.TYPE_CHECKING:
|
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
|
|
|
# Needed to enforce a return value from decorators
|
|
|
|
# More details: https://www.python.org/dev/peps/pep-0612/
|
2023-08-15 15:44:47 +00:00
|
|
|
|
|
|
|
from typing_extensions import Concatenate, ParamSpec
|
|
|
|
|
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
|
|
|
P = ParamSpec("P")
|
2024-12-02 14:44:10 +00:00
|
|
|
R = t.TypeVar("R")
|
|
|
|
FuncWithSession = t.Callable[Concatenate[Session, P], R]
|
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
|
|
|
|
python/trezorctl: split trezorctl into separate modules
Instead of all commands (like `load-device`, `change-pin`,
`tezos-sign-tx`, `ethereum-verify-message`...) living in trezorctl.py,
each functional group is now defined in a separate file.
With that, better structuring of the trezorctl command becomes
available:
- instead of `trezorctl set-label`, use `trezorctl set label`
- instead of `trezorctl change-pin`, use `trezorctl set pin`
- instead of `trezorctl enable-passphrase`, use `trezorctl set
passphrase enabled`
For common commands, such as `sign-tx`, it is possible to use the
currency name or shortcut:
- `trezorctl btc sign-tx`
- `trezorctl ethereum sign-tx`
- `trezorctl xtz sign-tx`
- `trezorctl doge sign-tx`
etc.
Some aliases have been retained for better compatibility. For others,
refer to `trezorctl --help` and `trezorctl <command> --help`.
2019-09-10 13:00:27 +00:00
|
|
|
|
|
|
|
class ChoiceType(click.Choice):
|
2024-12-02 14:44:10 +00:00
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self, typemap: t.Dict[str, t.Any], case_sensitive: bool = True
|
|
|
|
) -> None:
|
2022-03-11 12:40:22 +00:00
|
|
|
super().__init__(list(typemap.keys()))
|
2023-10-17 09:21:41 +00:00
|
|
|
self.case_sensitive = case_sensitive
|
|
|
|
if case_sensitive:
|
|
|
|
self.typemap = typemap
|
|
|
|
else:
|
|
|
|
self.typemap = {k.lower(): v for k, v in typemap.items()}
|
python/trezorctl: split trezorctl into separate modules
Instead of all commands (like `load-device`, `change-pin`,
`tezos-sign-tx`, `ethereum-verify-message`...) living in trezorctl.py,
each functional group is now defined in a separate file.
With that, better structuring of the trezorctl command becomes
available:
- instead of `trezorctl set-label`, use `trezorctl set label`
- instead of `trezorctl change-pin`, use `trezorctl set pin`
- instead of `trezorctl enable-passphrase`, use `trezorctl set
passphrase enabled`
For common commands, such as `sign-tx`, it is possible to use the
currency name or shortcut:
- `trezorctl btc sign-tx`
- `trezorctl ethereum sign-tx`
- `trezorctl xtz sign-tx`
- `trezorctl doge sign-tx`
etc.
Some aliases have been retained for better compatibility. For others,
refer to `trezorctl --help` and `trezorctl <command> --help`.
2019-09-10 13:00:27 +00:00
|
|
|
|
2024-12-02 14:44:10 +00:00
|
|
|
def convert(self, value: t.Any, param: t.Any, ctx: click.Context) -> t.Any:
|
2021-09-07 10:21:37 +00:00
|
|
|
if value in self.typemap.values():
|
|
|
|
return value
|
2020-03-24 15:02:48 +00:00
|
|
|
value = super().convert(value, param, ctx)
|
2023-10-17 09:21:41 +00:00
|
|
|
if isinstance(value, str) and not self.case_sensitive:
|
|
|
|
value = value.lower()
|
python/trezorctl: split trezorctl into separate modules
Instead of all commands (like `load-device`, `change-pin`,
`tezos-sign-tx`, `ethereum-verify-message`...) living in trezorctl.py,
each functional group is now defined in a separate file.
With that, better structuring of the trezorctl command becomes
available:
- instead of `trezorctl set-label`, use `trezorctl set label`
- instead of `trezorctl change-pin`, use `trezorctl set pin`
- instead of `trezorctl enable-passphrase`, use `trezorctl set
passphrase enabled`
For common commands, such as `sign-tx`, it is possible to use the
currency name or shortcut:
- `trezorctl btc sign-tx`
- `trezorctl ethereum sign-tx`
- `trezorctl xtz sign-tx`
- `trezorctl doge sign-tx`
etc.
Some aliases have been retained for better compatibility. For others,
refer to `trezorctl --help` and `trezorctl <command> --help`.
2019-09-10 13:00:27 +00:00
|
|
|
return self.typemap[value]
|
2020-03-24 15:02:48 +00:00
|
|
|
|
|
|
|
|
2024-12-02 14:44:10 +00:00
|
|
|
def get_passphrase(
|
|
|
|
passphrase_on_host: bool, available_on_device: bool
|
|
|
|
) -> t.Union[str, object]:
|
|
|
|
if available_on_device and not passphrase_on_host:
|
|
|
|
return ui.PASSPHRASE_ON_DEVICE
|
|
|
|
|
|
|
|
env_passphrase = os.getenv("PASSPHRASE")
|
|
|
|
if env_passphrase is not None:
|
|
|
|
ui.echo("Passphrase required. Using PASSPHRASE environment variable.")
|
|
|
|
return env_passphrase
|
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
passphrase = ui.prompt(
|
|
|
|
"Passphrase required",
|
|
|
|
hide_input=True,
|
|
|
|
default="",
|
|
|
|
show_default=False,
|
|
|
|
)
|
|
|
|
# In case user sees the input on the screen, we do not need confirmation
|
|
|
|
if not ui.CAN_HANDLE_HIDDEN_INPUT:
|
|
|
|
return passphrase
|
|
|
|
second = ui.prompt(
|
|
|
|
"Confirm your passphrase",
|
|
|
|
hide_input=True,
|
|
|
|
default="",
|
|
|
|
show_default=False,
|
|
|
|
)
|
|
|
|
if passphrase == second:
|
|
|
|
return passphrase
|
|
|
|
else:
|
|
|
|
ui.echo("Passphrase did not match. Please try again.")
|
|
|
|
except click.Abort:
|
|
|
|
raise exceptions.Cancelled from None
|
|
|
|
|
|
|
|
|
|
|
|
def get_client(transport: Transport) -> TrezorClient:
|
|
|
|
stored_channels = get_channel_db().load_stored_channels()
|
|
|
|
stored_transport_paths = [ch.transport_path for ch in stored_channels]
|
|
|
|
path = transport.get_path()
|
|
|
|
if path in stored_transport_paths:
|
|
|
|
stored_channel_with_correct_transport_path = next(
|
|
|
|
ch for ch in stored_channels if ch.transport_path == path
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
client = TrezorClient.resume(
|
|
|
|
transport, stored_channel_with_correct_transport_path
|
|
|
|
)
|
|
|
|
except Exception:
|
|
|
|
LOG.debug("Failed to resume a channel. Replacing by a new one.")
|
|
|
|
get_channel_db().remove_channel(path)
|
|
|
|
client = TrezorClient(transport)
|
|
|
|
else:
|
|
|
|
client = TrezorClient(transport)
|
|
|
|
return client
|
|
|
|
|
|
|
|
|
2020-03-24 15:02:48 +00:00
|
|
|
class TrezorConnection:
|
2024-12-02 14:44:10 +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 __init__(
|
2021-12-16 10:24:00 +00:00
|
|
|
self,
|
|
|
|
path: str,
|
2024-12-02 14:44:10 +00:00
|
|
|
session_id: bytes | None,
|
2021-12-16 10:24:00 +00:00
|
|
|
passphrase_on_host: bool,
|
|
|
|
script: bool,
|
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
|
|
|
) -> None:
|
2020-03-24 15:02:48 +00:00
|
|
|
self.path = path
|
|
|
|
self.session_id = session_id
|
|
|
|
self.passphrase_on_host = passphrase_on_host
|
2021-12-16 10:24:00 +00:00
|
|
|
self.script = script
|
2020-03-24 15:02:48 +00:00
|
|
|
|
2024-12-02 14:44:10 +00:00
|
|
|
def get_session(
|
|
|
|
self,
|
|
|
|
derive_cardano: bool = False,
|
|
|
|
empty_passphrase: bool = False,
|
|
|
|
must_resume: bool = False,
|
|
|
|
) -> Session:
|
|
|
|
client = self.get_client()
|
|
|
|
if must_resume and self.session_id is None:
|
|
|
|
click.echo("Failed to resume session - no session id provided")
|
|
|
|
raise RuntimeError("Failed to resume session - no session id provided")
|
|
|
|
|
|
|
|
# Try resume session from id
|
|
|
|
if self.session_id is not None:
|
|
|
|
if client.protocol_version is ProtocolVersion.PROTOCOL_V1:
|
|
|
|
session = SessionV1.resume_from_id(
|
|
|
|
client=client, session_id=self.session_id
|
|
|
|
)
|
|
|
|
elif client.protocol_version is ProtocolVersion.PROTOCOL_V2:
|
|
|
|
session = SessionV2(client, self.session_id)
|
|
|
|
# TODO fix resumption on THP
|
|
|
|
else:
|
|
|
|
raise Exception("Unsupported client protocol", client.protocol_version)
|
|
|
|
if must_resume:
|
|
|
|
if session.id != self.session_id or session.id is None:
|
|
|
|
click.echo("Failed to resume session")
|
|
|
|
RuntimeError("Failed to resume session - no session id provided")
|
|
|
|
return session
|
|
|
|
|
|
|
|
features = client.protocol.get_features()
|
|
|
|
|
|
|
|
passphrase_enabled = True # TODO what to do here?
|
|
|
|
|
|
|
|
if not passphrase_enabled:
|
|
|
|
return client.get_session(derive_cardano=derive_cardano)
|
|
|
|
|
|
|
|
if empty_passphrase:
|
|
|
|
passphrase = ""
|
|
|
|
else:
|
|
|
|
available_on_device = Capability.PassphraseEntry in features.capabilities
|
|
|
|
passphrase = get_passphrase(available_on_device, self.passphrase_on_host)
|
|
|
|
# TODO handle case when PASSPHRASE_ON_DEVICE is returned from get_passphrase func
|
|
|
|
if not isinstance(passphrase, str):
|
|
|
|
raise RuntimeError("Passphrase must be a str")
|
|
|
|
session = client.get_session(
|
|
|
|
passphrase=passphrase, derive_cardano=derive_cardano
|
|
|
|
)
|
|
|
|
return session
|
|
|
|
|
2021-12-16 10:24:00 +00:00
|
|
|
def get_transport(self) -> "Transport":
|
2020-03-24 15:02:48 +00:00
|
|
|
try:
|
|
|
|
# look for transport without prefix search
|
2022-06-29 11:11:37 +00:00
|
|
|
return transport.get_transport(self.path, prefix_search=False)
|
2020-03-24 15:02:48 +00:00
|
|
|
except Exception:
|
|
|
|
# most likely not found. try again below.
|
|
|
|
pass
|
|
|
|
|
|
|
|
# look for transport with prefix search
|
|
|
|
# if this fails, we want the exception to bubble up to the caller
|
2022-06-29 11:11:37 +00:00
|
|
|
return transport.get_transport(self.path, prefix_search=True)
|
2020-03-24 15:02:48 +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_client(self) -> TrezorClient:
|
2024-12-02 14:44:10 +00:00
|
|
|
return get_client(self.get_transport())
|
|
|
|
|
|
|
|
def get_management_session(self) -> Session:
|
|
|
|
client = self.get_client()
|
|
|
|
management_session = client.get_management_session()
|
|
|
|
return management_session
|
2020-03-24 15:02:48 +00:00
|
|
|
|
2020-08-26 14:01:58 +00:00
|
|
|
@contextmanager
|
|
|
|
def client_context(self):
|
|
|
|
"""Get a client instance as a context manager. Handle errors in a manner
|
|
|
|
appropriate for end-users.
|
2020-03-24 15:02:48 +00:00
|
|
|
|
2020-08-26 14:01:58 +00:00
|
|
|
Usage:
|
|
|
|
>>> with obj.client_context() as client:
|
|
|
|
>>> do_your_actions_here()
|
|
|
|
"""
|
2020-03-24 15:02:48 +00:00
|
|
|
try:
|
2020-08-26 14:01:58 +00:00
|
|
|
client = self.get_client()
|
2022-06-29 11:11:37 +00:00
|
|
|
except transport.DeviceIsBusy:
|
|
|
|
click.echo("Device is in use by another process.")
|
|
|
|
sys.exit(1)
|
2020-03-24 15:02:48 +00:00
|
|
|
except Exception:
|
|
|
|
click.echo("Failed to find a Trezor device.")
|
2020-08-26 14:01:58 +00:00
|
|
|
if self.path is not None:
|
2021-09-27 10:13:51 +00:00
|
|
|
click.echo(f"Using path: {self.path}")
|
2020-03-24 15:02:48 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
try:
|
2020-08-26 14:01:58 +00:00
|
|
|
yield client
|
2020-03-24 15:02:48 +00:00
|
|
|
except exceptions.Cancelled:
|
2020-08-26 14:01:58 +00:00
|
|
|
# handle cancel action
|
2020-03-24 15:02:48 +00:00
|
|
|
click.echo("Action was cancelled.")
|
|
|
|
sys.exit(1)
|
|
|
|
except exceptions.TrezorException as e:
|
2020-08-26 14:01:58 +00:00
|
|
|
# handle any Trezor-sent exceptions as user-readable
|
2020-03-24 15:02:48 +00:00
|
|
|
raise click.ClickException(str(e)) from e
|
2020-08-26 14:01:58 +00:00
|
|
|
# other exceptions may cause a traceback
|
|
|
|
|
|
|
|
|
2024-12-02 14:44:10 +00:00
|
|
|
def with_session(
|
|
|
|
func: "t.Callable[Concatenate[Session, P], R]|None" = None,
|
|
|
|
*,
|
|
|
|
empty_passphrase: bool = False,
|
|
|
|
derive_cardano: bool = False,
|
|
|
|
management: bool = False,
|
|
|
|
must_resume: bool = False,
|
|
|
|
) -> t.Callable[[FuncWithSession], t.Callable[P, R]]:
|
|
|
|
"""Provides a Click command with parameter `session=obj.get_session(...)` or
|
|
|
|
`session=obj.get_management_session()` based on the parameters provided.
|
|
|
|
|
|
|
|
If default parameters are ok, this decorator can be used without parentheses.
|
|
|
|
|
|
|
|
TODO: handle resumption of sessions and their (potential) closure.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def decorator(
|
|
|
|
func: FuncWithSession,
|
|
|
|
) -> "t.Callable[P, R]":
|
|
|
|
|
|
|
|
@click.pass_obj
|
|
|
|
@functools.wraps(func)
|
|
|
|
def function_with_session(
|
|
|
|
obj: TrezorConnection, *args: "P.args", **kwargs: "P.kwargs"
|
|
|
|
) -> "R":
|
|
|
|
if management:
|
|
|
|
session = obj.get_management_session()
|
|
|
|
else:
|
2024-12-04 16:50:21 +00:00
|
|
|
# TODO try (sys.exit ve finally)
|
2024-12-02 14:44:10 +00:00
|
|
|
session = obj.get_session(
|
|
|
|
derive_cardano=derive_cardano,
|
|
|
|
empty_passphrase=empty_passphrase,
|
|
|
|
must_resume=must_resume,
|
|
|
|
)
|
|
|
|
try:
|
|
|
|
return func(session, *args, **kwargs)
|
|
|
|
finally:
|
|
|
|
pass
|
|
|
|
# TODO try end session if not resumed
|
|
|
|
|
|
|
|
return function_with_session
|
|
|
|
|
|
|
|
# If the decorator @get_session is used without parentheses
|
|
|
|
if func and callable(func):
|
|
|
|
return decorator(func) # type: ignore [Function return type]
|
|
|
|
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
|
|
|
def with_client(
|
|
|
|
func: "t.Callable[Concatenate[TrezorClient, P], R]",
|
|
|
|
) -> "t.Callable[P, R]":
|
2020-08-26 14:01:58 +00:00
|
|
|
"""Wrap a Click command in `with obj.client_context() as client`.
|
|
|
|
|
|
|
|
Sessions are handled transparently. The user is warned when session did not resume
|
|
|
|
cleanly. The session is closed after the command completes - unless the session
|
|
|
|
was resumed, in which case it should remain open.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@click.pass_obj
|
|
|
|
@functools.wraps(func)
|
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 trezorctl_command_with_client(
|
|
|
|
obj: TrezorConnection, *args: "P.args", **kwargs: "P.kwargs"
|
|
|
|
) -> "R":
|
2020-08-26 14:01:58 +00:00
|
|
|
with obj.client_context() as client:
|
2024-12-02 14:44:10 +00:00
|
|
|
# session_was_resumed = obj.session_id == client.session_id
|
|
|
|
# if not session_was_resumed and obj.session_id is not None:
|
|
|
|
# # tried to resume but failed
|
|
|
|
# click.echo("Warning: failed to resume session.", err=True)
|
|
|
|
click.echo(
|
|
|
|
"Warning: resume session detection is not implemented yet!", err=True
|
|
|
|
)
|
2020-08-26 14:01:58 +00:00
|
|
|
try:
|
|
|
|
return func(client, *args, **kwargs)
|
|
|
|
finally:
|
2024-12-02 14:44:10 +00:00
|
|
|
if client.protocol_version == ProtocolVersion.PROTOCOL_V2:
|
|
|
|
get_channel_db().save_channel(client.protocol)
|
|
|
|
# if not session_was_resumed:
|
|
|
|
# try:
|
|
|
|
# client.end_session()
|
|
|
|
# except Exception:
|
|
|
|
# pass
|
2020-03-24 15:02:48 +00:00
|
|
|
|
2024-11-19 09:10:17 +00:00
|
|
|
return trezorctl_command_with_client
|
2022-02-11 13:11:43 +00:00
|
|
|
|
|
|
|
|
2024-12-02 14:44:10 +00:00
|
|
|
# def with_client(
|
|
|
|
# func: "t.Callable[Concatenate[TrezorClient, P], R]",
|
|
|
|
# ) -> "t.Callable[P, R]":
|
|
|
|
# """Wrap a Click command in `with obj.client_context() as client`.
|
|
|
|
|
|
|
|
# Sessions are handled transparently. The user is warned when session did not resume
|
|
|
|
# cleanly. The session is closed after the command completes - unless the session
|
|
|
|
# was resumed, in which case it should remain open.
|
|
|
|
# """
|
|
|
|
|
|
|
|
# @click.pass_obj
|
|
|
|
# @functools.wraps(func)
|
|
|
|
# def trezorctl_command_with_client(
|
|
|
|
# obj: TrezorConnection, *args: "P.args", **kwargs: "P.kwargs"
|
|
|
|
# ) -> "R":
|
|
|
|
# with obj.client_context() as client:
|
|
|
|
# session_was_resumed = obj.session_id == client.session_id
|
|
|
|
# if not session_was_resumed and obj.session_id is not None:
|
|
|
|
# # tried to resume but failed
|
|
|
|
# click.echo("Warning: failed to resume session.", err=True)
|
|
|
|
|
|
|
|
# try:
|
|
|
|
# return func(client, *args, **kwargs)
|
|
|
|
# finally:
|
|
|
|
# if not session_was_resumed:
|
|
|
|
# try:
|
|
|
|
# client.end_session()
|
|
|
|
# except Exception:
|
|
|
|
# pass
|
|
|
|
|
|
|
|
# # the return type of @click.pass_obj is improperly specified and pyright doesn't
|
|
|
|
# # understand that it converts f(obj, *args, **kwargs) to f(*args, **kwargs)
|
|
|
|
# return trezorctl_command_with_client
|
|
|
|
|
|
|
|
|
2022-02-11 13:11:43 +00:00
|
|
|
class AliasedGroup(click.Group):
|
|
|
|
"""Command group that handles aliases and Click 6.x compatibility.
|
|
|
|
|
|
|
|
Click 7.0 silently switched all underscore_commands to dash-commands.
|
|
|
|
This implementation of `click.Group` responds to underscore_commands by invoking
|
|
|
|
the respective dash-command.
|
|
|
|
|
|
|
|
Supply an `aliases` dict at construction time to provide an alternative list of
|
|
|
|
command names:
|
|
|
|
|
|
|
|
>>> @click.command(cls=AliasedGroup, aliases={"do_bar", do_foo})
|
|
|
|
>>> def cli():
|
|
|
|
>>> ...
|
|
|
|
|
|
|
|
If these commands are not known at the construction time, they can be set later:
|
|
|
|
|
|
|
|
>>> @click.command(cls=AliasedGroup)
|
|
|
|
>>> def cli():
|
|
|
|
>>> ...
|
|
|
|
>>>
|
|
|
|
>>> @cli.command()
|
|
|
|
>>> def do_foo():
|
|
|
|
>>> ...
|
|
|
|
>>>
|
|
|
|
>>> cli.aliases={"do_bar", do_foo}
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
2024-12-02 14:44:10 +00:00
|
|
|
aliases: t.Dict[str, click.Command] | None = None,
|
|
|
|
*args: t.Any,
|
|
|
|
**kwargs: t.Any,
|
2022-02-11 13:11:43 +00:00
|
|
|
) -> None:
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.aliases = aliases or {}
|
|
|
|
|
2024-12-02 14:44:10 +00:00
|
|
|
def get_command(self, ctx: click.Context, cmd_name: str) -> click.Command | None:
|
2022-02-11 13:11:43 +00:00
|
|
|
cmd_name = cmd_name.replace("_", "-")
|
|
|
|
# try to look up the real name
|
|
|
|
cmd = super().get_command(ctx, cmd_name)
|
|
|
|
if cmd:
|
|
|
|
return cmd
|
|
|
|
|
|
|
|
# look for a backwards compatibility alias
|
|
|
|
if cmd_name in self.aliases:
|
|
|
|
return self.aliases[cmd_name]
|
|
|
|
|
|
|
|
return None
|