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.
|
|
|
|
#
|
|
|
|
# Copyright (C) 2012-2019 SatoshiLabs and contributors
|
|
|
|
#
|
|
|
|
# This library is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Lesser General Public License version 3
|
|
|
|
# as published by the Free Software Foundation.
|
|
|
|
#
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the License along with this library.
|
|
|
|
# If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
|
|
|
|
|
2020-03-24 15:02:48 +00:00
|
|
|
import functools
|
|
|
|
import sys
|
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
|
|
|
|
|
2020-03-24 15:02:48 +00:00
|
|
|
from .. import exceptions
|
|
|
|
from ..client import TrezorClient
|
|
|
|
from ..transport import get_transport
|
|
|
|
from ..ui import ClickUI
|
|
|
|
|
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):
|
|
|
|
def __init__(self, typemap):
|
2020-03-24 15:02:48 +00:00
|
|
|
super().__init__(typemap.keys())
|
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
|
|
|
self.typemap = typemap
|
|
|
|
|
|
|
|
def convert(self, value, param, ctx):
|
2020-03-24 15:02:48 +00:00
|
|
|
value = super().convert(value, param, ctx)
|
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
|
|
|
|
|
|
|
|
|
|
|
class TrezorConnection:
|
|
|
|
def __init__(self, path, session_id, passphrase_on_host):
|
|
|
|
self.path = path
|
|
|
|
self.session_id = session_id
|
|
|
|
self.passphrase_on_host = passphrase_on_host
|
|
|
|
|
|
|
|
def get_transport(self):
|
|
|
|
try:
|
|
|
|
# look for transport without prefix search
|
|
|
|
return get_transport(self.path, prefix_search=False)
|
|
|
|
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
|
|
|
|
return get_transport(self.path, prefix_search=True)
|
|
|
|
|
|
|
|
def get_ui(self):
|
|
|
|
return ClickUI(passphrase_on_host=self.passphrase_on_host)
|
|
|
|
|
|
|
|
def get_client(self):
|
|
|
|
transport = self.get_transport()
|
|
|
|
ui = self.get_ui()
|
|
|
|
return TrezorClient(transport, ui=ui, session_id=self.session_id)
|
|
|
|
|
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()
|
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:
|
|
|
|
click.echo("Using path: {}".format(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
|
|
|
|
|
|
|
|
|
|
|
|
def with_client(func):
|
|
|
|
"""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, *args, **kwargs):
|
|
|
|
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
|
2020-03-24 15:02:48 +00:00
|
|
|
|
|
|
|
return trezorctl_command_with_client
|