mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-03-17 16:46:05 +00:00
feat(python): make failing to resume session hard-fail
This commit is contained in:
parent
24f3e3ff24
commit
bc9d8dc277
@ -144,7 +144,12 @@ class TrezorConnection:
|
|||||||
if must_resume:
|
if must_resume:
|
||||||
if session.id != self.session_id or session.id is None:
|
if session.id != self.session_id or session.id is None:
|
||||||
click.echo("Failed to resume session")
|
click.echo("Failed to resume session")
|
||||||
RuntimeError("Failed to resume session - no session id provided")
|
env_var = os.environ.get("TREZOR_SESSION_ID")
|
||||||
|
if env_var and bytes.fromhex(env_var) == self.session_id:
|
||||||
|
click.echo(
|
||||||
|
"Session-id stored in TREZOR_SESSION_ID is no longer valid. Call 'unset TREZOR_SESSION_ID' to clear it."
|
||||||
|
)
|
||||||
|
raise exceptions.FailedSessionResumption()
|
||||||
return session
|
return session
|
||||||
|
|
||||||
features = client.protocol.get_features()
|
features = client.protocol.get_features()
|
||||||
@ -271,6 +276,8 @@ class TrezorConnection:
|
|||||||
except transport.DeviceIsBusy:
|
except transport.DeviceIsBusy:
|
||||||
click.echo("Device is in use by another process.")
|
click.echo("Device is in use by another process.")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
except exceptions.FailedSessionResumption:
|
||||||
|
sys.exit(1)
|
||||||
except Exception:
|
except Exception:
|
||||||
click.echo("Failed to find a Trezor device.")
|
click.echo("Failed to find a Trezor device.")
|
||||||
if self.path is not None:
|
if self.path is not None:
|
||||||
@ -312,17 +319,19 @@ def with_session(
|
|||||||
def function_with_session(
|
def function_with_session(
|
||||||
obj: TrezorConnection, *args: "P.args", **kwargs: "P.kwargs"
|
obj: TrezorConnection, *args: "P.args", **kwargs: "P.kwargs"
|
||||||
) -> "R":
|
) -> "R":
|
||||||
|
is_resume_mandatory = must_resume or obj.session_id is not None
|
||||||
|
|
||||||
with obj.session_context(
|
with obj.session_context(
|
||||||
empty_passphrase=empty_passphrase,
|
empty_passphrase=empty_passphrase,
|
||||||
derive_cardano=derive_cardano,
|
derive_cardano=derive_cardano,
|
||||||
seedless=seedless,
|
seedless=seedless,
|
||||||
must_resume=must_resume,
|
must_resume=is_resume_mandatory,
|
||||||
) as session:
|
) as session:
|
||||||
try:
|
try:
|
||||||
return func(session, *args, **kwargs)
|
return func(session, *args, **kwargs)
|
||||||
|
|
||||||
finally:
|
finally:
|
||||||
if not must_resume:
|
if not is_resume_mandatory:
|
||||||
session.end()
|
session.end()
|
||||||
|
|
||||||
return function_with_session
|
return function_with_session
|
||||||
|
@ -85,3 +85,10 @@ class UnexpectedMessageError(TrezorException):
|
|||||||
self.expected = expected
|
self.expected = expected
|
||||||
self.actual = actual
|
self.actual = actual
|
||||||
super().__init__(f"Expected {expected.__name__} but Trezor sent {actual}")
|
super().__init__(f"Expected {expected.__name__} but Trezor sent {actual}")
|
||||||
|
|
||||||
|
|
||||||
|
class FailedSessionResumption(TrezorException):
|
||||||
|
"""Provided session_id is not valid / session cannot be resumed.
|
||||||
|
|
||||||
|
Raised when `trezorctl -s <sesssion_id>` is used or `TREZOR_SESSION_ID = <session_id>`
|
||||||
|
is set and resumption of session with the `session_id` fails."""
|
||||||
|
Loading…
Reference in New Issue
Block a user