1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-16 11:28:14 +00:00

fix(python): improve robustness of TrezorClientDebugLink setup

* improve sync_responses to work on uninitialized instance
* sync responses at construction time
This commit is contained in:
matejcik 2024-07-19 10:51:07 +02:00 committed by matejcik
parent e1b4a4a79a
commit b48c36c2bf

View File

@ -48,7 +48,7 @@ from .client import TrezorClient
from .exceptions import TrezorFailure from .exceptions import TrezorFailure
from .log import DUMP_BYTES from .log import DUMP_BYTES
from .messages import DebugWaitType from .messages import DebugWaitType
from .tools import expect from .tools import expect, session
if TYPE_CHECKING: if TYPE_CHECKING:
from typing_extensions import Protocol from typing_extensions import Protocol
@ -1014,8 +1014,11 @@ class TrezorClientDebugLink(TrezorClient):
else: else:
raise raise
self.reset_debug_features() # set transport explicitly so that sync_responses can work
self.transport = transport
self.reset_debug_features()
self.sync_responses()
super().__init__(transport, ui=self.ui) super().__init__(transport, ui=self.ui)
# So that we can choose right screenshotting logic (T1 vs TT) # So that we can choose right screenshotting logic (T1 vs TT)
@ -1300,14 +1303,23 @@ class TrezorClientDebugLink(TrezorClient):
# Start by canceling whatever is on screen. This will work to cancel T1 PIN # Start by canceling whatever is on screen. This will work to cancel T1 PIN
# prompt, which is in TINY mode and does not respond to `Ping`. # prompt, which is in TINY mode and does not respond to `Ping`.
# go to super() to avoid message filtering cancel_msg = mapping.DEFAULT_MAPPING.encode(messages.Cancel())
super()._raw_write(messages.Cancel()) self.transport.begin_session()
try:
self.transport.write(*cancel_msg)
message = "SYNC" + secrets.token_hex(8) message = "SYNC" + secrets.token_hex(8)
super()._raw_write(messages.Ping(message=message)) ping_msg = mapping.DEFAULT_MAPPING.encode(messages.Ping(message=message))
self.transport.write(*ping_msg)
resp = None resp = None
while resp != messages.Success(message=message): while resp != messages.Success(message=message):
resp = super()._raw_read() msg_id, msg_bytes = self.transport.read()
try:
resp = mapping.DEFAULT_MAPPING.decode(msg_id, msg_bytes)
except Exception:
pass
finally:
self.transport.end_session()
def mnemonic_callback(self, _) -> str: def mnemonic_callback(self, _) -> str:
word, pos = self.debug.read_recovery_word() word, pos = self.debug.read_recovery_word()