1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-22 07:28:10 +00:00

python/trezorctl: add get-session

This commit is contained in:
matejcik 2020-02-10 16:14:21 +01:00
parent 64838bca2d
commit d4343ad8b7
2 changed files with 41 additions and 3 deletions

View File

@ -143,11 +143,23 @@ def configure_logging(verbose: int):
@click.option(
"-P", "--passphrase-on-host", is_flag=True, help="Enter passphrase on host.",
)
@click.option(
"-s",
"--session-id",
help="Resume given session ID.",
default=os.environ.get("TREZOR_SESSION_ID"),
)
@click.version_option()
@click.pass_context
def cli(ctx, path, verbose, is_json, passphrase_on_host):
def cli(ctx, path, verbose, is_json, passphrase_on_host, session_id):
configure_logging(verbose)
if session_id:
try:
session_id = bytes.fromhex(session_id)
except ValueError:
raise click.ClickException("Not a valid session id: {}".format(session_id))
def get_device():
try:
device = get_transport(path, prefix_search=False)
@ -160,7 +172,9 @@ def cli(ctx, path, verbose, is_json, passphrase_on_host):
click.echo("Using path: {}".format(path))
sys.exit(1)
return TrezorClient(
transport=device, ui=ui.ClickUI(passphrase_on_host=passphrase_on_host)
transport=device,
ui=ui.ClickUI(passphrase_on_host=passphrase_on_host),
session_id=session_id,
)
ctx.obj = get_device
@ -204,7 +218,7 @@ def list_devices():
@cli.command()
def version():
"""Show version of trezorctl/trezorlib."""
from trezorlib import __version__ as VERSION
from .. import __version__ as VERSION
return VERSION
@ -223,6 +237,29 @@ def ping(connect, message, button_protection):
return connect().ping(message, button_protection=button_protection)
@cli.command()
@click.pass_obj
def get_session(connect):
"""Get a session ID for subsequent commands.
Unlocks Trezor with a passphrase and returns a session ID. Use this session ID with
`trezorctl -s SESSION_ID`, or set it to an environment variable `TREZOR_SESSION_ID`,
to avoid having to enter passphrase for subsequent commands.
The session ID is valid until another client starts using Trezor, until the next
get-session call, or until Trezor is disconnected.
"""
from ..btc import get_address
from ..client import PASSPHRASE_TEST_PATH
client = connect()
get_address(client, "Testnet", PASSPHRASE_TEST_PATH)
if client.session_id is None:
raise click.ClickException("Passphrase not enabled, session ID not available.")
else:
return client.session_id.hex()
@cli.command()
@click.pass_obj
def clear_session(connect):

View File

@ -34,6 +34,7 @@ VENDORS = ("bitcointrezor.com", "trezor.io")
MAX_PASSPHRASE_LENGTH = 50
PASSPHRASE_ON_DEVICE = object()
PASSPHRASE_TEST_PATH = tools.parse_path("44h/1h/19h/0/1337")
DEPRECATION_ERROR = """
Incompatible Trezor library detected.