2019-10-16 15:39:06 +00:00
|
|
|
from concurrent.futures import ThreadPoolExecutor
|
2022-01-28 18:26:03 +00:00
|
|
|
from typing import TYPE_CHECKING
|
2019-10-16 15:39:06 +00:00
|
|
|
|
2020-07-08 14:14:13 +00:00
|
|
|
from trezorlib.client import PASSPHRASE_ON_DEVICE
|
2019-10-18 11:32:43 +00:00
|
|
|
from trezorlib.transport import udp
|
|
|
|
|
2022-01-28 18:26:03 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from trezorlib.messages import Features
|
2022-01-31 12:25:30 +00:00
|
|
|
from trezorlib.debuglink import DebugLink, TrezorClientDebugLink as Client
|
2022-01-28 18:26:03 +00:00
|
|
|
from trezorlib._internal.emulator import Emulator
|
|
|
|
|
|
|
|
|
2019-10-18 11:32:43 +00:00
|
|
|
udp.SOCKET_TIMEOUT = 0.1
|
|
|
|
|
2019-10-16 15:39:06 +00:00
|
|
|
|
|
|
|
class NullUI:
|
|
|
|
@staticmethod
|
|
|
|
def button_request(code):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_pin(code=None):
|
2020-05-25 10:50:30 +00:00
|
|
|
raise NotImplementedError("NullUI should not be used with T1")
|
2019-10-16 15:39:06 +00:00
|
|
|
|
|
|
|
@staticmethod
|
2022-10-25 10:46:37 +00:00
|
|
|
def get_passphrase(available_on_device: bool = False):
|
2020-07-08 14:14:13 +00:00
|
|
|
if available_on_device:
|
|
|
|
return PASSPHRASE_ON_DEVICE
|
|
|
|
else:
|
|
|
|
raise NotImplementedError("NullUI should not be used with T1")
|
2019-10-16 15:39:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BackgroundDeviceHandler:
|
|
|
|
_pool = ThreadPoolExecutor()
|
|
|
|
|
2022-01-31 12:25:30 +00:00
|
|
|
def __init__(self, client: "Client") -> None:
|
2020-05-25 10:50:30 +00:00
|
|
|
self._configure_client(client)
|
|
|
|
self.task = None
|
|
|
|
|
2022-01-31 12:25:30 +00:00
|
|
|
def _configure_client(self, client: "Client") -> None:
|
2019-10-16 15:39:06 +00:00
|
|
|
self.client = client
|
2022-01-28 18:26:03 +00:00
|
|
|
self.client.ui = NullUI # type: ignore [NullUI is OK UI]
|
2020-05-25 10:50:30 +00:00
|
|
|
self.client.watch_layout(True)
|
2019-10-16 15:39:06 +00:00
|
|
|
|
2022-10-25 10:46:37 +00:00
|
|
|
def run(self, function, *args, **kwargs) -> None:
|
2019-10-16 15:39:06 +00:00
|
|
|
if self.task is not None:
|
|
|
|
raise RuntimeError("Wait for previous task first")
|
|
|
|
self.task = self._pool.submit(function, self.client, *args, **kwargs)
|
|
|
|
|
2022-01-28 18:26:03 +00:00
|
|
|
def kill_task(self) -> None:
|
2019-10-18 11:32:43 +00:00
|
|
|
if self.task is not None:
|
|
|
|
# Force close the client, which should raise an exception in a client
|
|
|
|
# waiting on IO. Does not work over Bridge, because bridge doesn't have
|
|
|
|
# a close() method.
|
|
|
|
while self.client.session_counter > 0:
|
|
|
|
self.client.close()
|
|
|
|
try:
|
|
|
|
self.task.result()
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
self.task = None
|
|
|
|
|
2022-10-25 10:46:37 +00:00
|
|
|
def restart(self, emulator: "Emulator") -> None:
|
2019-10-22 11:54:17 +00:00
|
|
|
# TODO handle actual restart as well
|
|
|
|
self.kill_task()
|
|
|
|
emulator.restart()
|
2022-01-28 18:26:03 +00:00
|
|
|
self._configure_client(emulator.client) # type: ignore [client cannot be None]
|
2019-10-22 11:54:17 +00:00
|
|
|
|
2019-10-16 15:39:06 +00:00
|
|
|
def result(self):
|
|
|
|
if self.task is None:
|
|
|
|
raise RuntimeError("No task running")
|
|
|
|
try:
|
|
|
|
return self.task.result()
|
|
|
|
finally:
|
|
|
|
self.task = None
|
|
|
|
|
2022-01-28 18:26:03 +00:00
|
|
|
def features(self) -> "Features":
|
2019-10-16 15:39:06 +00:00
|
|
|
if self.task is not None:
|
|
|
|
raise RuntimeError("Cannot query features while task is running")
|
|
|
|
self.client.init_device()
|
|
|
|
return self.client.features
|
|
|
|
|
2022-01-28 18:26:03 +00:00
|
|
|
def debuglink(self) -> "DebugLink":
|
2019-10-16 15:39:06 +00:00
|
|
|
return self.client.debug
|
|
|
|
|
2022-01-28 18:26:03 +00:00
|
|
|
def check_finalize(self) -> bool:
|
2019-10-16 15:39:06 +00:00
|
|
|
if self.task is not None:
|
2019-10-18 11:32:43 +00:00
|
|
|
self.kill_task()
|
2019-10-16 15:39:06 +00:00
|
|
|
return False
|
|
|
|
return True
|
2019-10-18 11:32:43 +00:00
|
|
|
|
2022-01-28 18:26:03 +00:00
|
|
|
def __enter__(self) -> "BackgroundDeviceHandler":
|
2019-10-18 11:32:43 +00:00
|
|
|
return self
|
|
|
|
|
2022-01-28 18:26:03 +00:00
|
|
|
def __exit__(self, exc_type, exc_value, traceback) -> None:
|
2019-10-21 14:49:34 +00:00
|
|
|
finalized_ok = self.check_finalize()
|
|
|
|
if exc_type is None and not finalized_ok:
|
2019-10-18 11:32:43 +00:00
|
|
|
raise RuntimeError("Exit while task is unfinished")
|