From 7f503ce199a28b69ab8b2d1f31f2e8f1ef81cea3 Mon Sep 17 00:00:00 2001 From: M1nd3r Date: Mon, 14 Oct 2024 18:39:50 +0200 Subject: [PATCH] refactor(trezorlib): improve transport protocol file structure [no changelog] --- python/src/trezorlib/cli/__init__.py | 2 +- python/src/trezorlib/cli/trezorctl.py | 2 +- python/src/trezorlib/client.py | 8 ++++---- python/src/trezorlib/debuglink.py | 2 +- python/src/trezorlib/transport/new/__init__.py | 0 python/src/trezorlib/transport/session.py | 4 ++-- .../transport/{new => thp}/alternating_bit_protocol.py | 0 .../src/trezorlib/transport/{new => thp}/channel_data.py | 0 .../trezorlib/transport/{new => thp}/channel_database.py | 6 +++--- .../src/trezorlib/transport/{new => thp}/control_byte.py | 0 .../transport/{new => thp}/protocol_and_channel.py | 2 +- .../src/trezorlib/transport/{new => thp}/protocol_v1.py | 0 .../src/trezorlib/transport/{new => thp}/protocol_v2.py | 4 ++-- python/tools/pybridge.py | 3 +-- tests/conftest.py | 4 ++-- 15 files changed, 18 insertions(+), 19 deletions(-) delete mode 100644 python/src/trezorlib/transport/new/__init__.py rename python/src/trezorlib/transport/{new => thp}/alternating_bit_protocol.py (100%) rename python/src/trezorlib/transport/{new => thp}/channel_data.py (100%) rename python/src/trezorlib/transport/{new => thp}/channel_database.py (94%) rename python/src/trezorlib/transport/{new => thp}/control_byte.py (100%) rename python/src/trezorlib/transport/{new => thp}/protocol_and_channel.py (94%) rename python/src/trezorlib/transport/{new => thp}/protocol_v1.py (100%) rename python/src/trezorlib/transport/{new => thp}/protocol_v2.py (99%) diff --git a/python/src/trezorlib/cli/__init__.py b/python/src/trezorlib/cli/__init__.py index c5af2b5708..7f639fd0f0 100644 --- a/python/src/trezorlib/cli/__init__.py +++ b/python/src/trezorlib/cli/__init__.py @@ -29,7 +29,7 @@ from .. import exceptions, transport, ui from ..client import TrezorClient from ..messages import Capability from ..transport import Transport -from ..transport.new import channel_database +from ..transport.thp import channel_database LOG = logging.getLogger(__name__) diff --git a/python/src/trezorlib/cli/trezorctl.py b/python/src/trezorlib/cli/trezorctl.py index 640adc3273..2ecdacd30d 100755 --- a/python/src/trezorlib/cli/trezorctl.py +++ b/python/src/trezorlib/cli/trezorctl.py @@ -27,8 +27,8 @@ import click from .. import __version__, log, messages, protobuf from ..client import TrezorClient from ..transport import DeviceIsBusy, enumerate_devices -from ..transport.new import channel_database from ..transport.session import Session +from ..transport.thp import channel_database from ..transport.udp import UdpTransport from . import ( AliasedGroup, diff --git a/python/src/trezorlib/client.py b/python/src/trezorlib/client.py index 236a62e822..98c7f41e19 100644 --- a/python/src/trezorlib/client.py +++ b/python/src/trezorlib/client.py @@ -23,10 +23,10 @@ from . import mapping, messages, models from .mapping import ProtobufMapping from .tools import parse_path from .transport import Transport, get_transport -from .transport.new.channel_data import ChannelData -from .transport.new.protocol_and_channel import ProtocolAndChannel -from .transport.new.protocol_v1 import ProtocolV1 -from .transport.new.protocol_v2 import ProtocolV2 +from .transport.thp.channel_data import ChannelData +from .transport.thp.protocol_and_channel import ProtocolAndChannel +from .transport.thp.protocol_v1 import ProtocolV1 +from .transport.thp.protocol_v2 import ProtocolV2 if t.TYPE_CHECKING: from .transport.session import Session diff --git a/python/src/trezorlib/debuglink.py b/python/src/trezorlib/debuglink.py index 2b52408ad0..e726025681 100644 --- a/python/src/trezorlib/debuglink.py +++ b/python/src/trezorlib/debuglink.py @@ -37,8 +37,8 @@ from .exceptions import TrezorFailure from .log import DUMP_BYTES from .messages import DebugWaitType from .tools import expect -from .transport.new.protocol_v1 import ProtocolV1 from .transport.session import Session +from .transport.thp.protocol_v1 import ProtocolV1 if t.TYPE_CHECKING: from typing_extensions import Protocol diff --git a/python/src/trezorlib/transport/new/__init__.py b/python/src/trezorlib/transport/new/__init__.py deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/python/src/trezorlib/transport/session.py b/python/src/trezorlib/transport/session.py index 9713ae708b..1aae80eb9e 100644 --- a/python/src/trezorlib/transport/session.py +++ b/python/src/trezorlib/transport/session.py @@ -4,8 +4,8 @@ import logging import typing as t from .. import exceptions, messages, models -from .new.protocol_v1 import ProtocolV1 -from .new.protocol_v2 import ProtocolV2 +from .thp.protocol_v1 import ProtocolV1 +from .thp.protocol_v2 import ProtocolV2 if t.TYPE_CHECKING: from ..client import TrezorClient diff --git a/python/src/trezorlib/transport/new/alternating_bit_protocol.py b/python/src/trezorlib/transport/thp/alternating_bit_protocol.py similarity index 100% rename from python/src/trezorlib/transport/new/alternating_bit_protocol.py rename to python/src/trezorlib/transport/thp/alternating_bit_protocol.py diff --git a/python/src/trezorlib/transport/new/channel_data.py b/python/src/trezorlib/transport/thp/channel_data.py similarity index 100% rename from python/src/trezorlib/transport/new/channel_data.py rename to python/src/trezorlib/transport/thp/channel_data.py diff --git a/python/src/trezorlib/transport/new/channel_database.py b/python/src/trezorlib/transport/thp/channel_database.py similarity index 94% rename from python/src/trezorlib/transport/new/channel_database.py rename to python/src/trezorlib/transport/thp/channel_database.py index 2de48bba03..fc8c612e2b 100644 --- a/python/src/trezorlib/transport/new/channel_database.py +++ b/python/src/trezorlib/transport/thp/channel_database.py @@ -3,7 +3,7 @@ import logging import os import typing as t -from .channel_data import ChannelData +from ..thp.channel_data import ChannelData from .protocol_and_channel import ProtocolAndChannel LOG = logging.getLogger(__name__) @@ -69,7 +69,7 @@ def save_channel(new_channel: ProtocolAndChannel): channels = read_all_channels() transport_path = new_channel.transport.get_path() - # If channel is modified: replace the old by the new + # If the channel is found in database: replace the old entry by the new for i, channel in enumerate(channels): if channel["transport_path"] == transport_path: LOG.debug("Modified channel entry for %s", transport_path) @@ -77,7 +77,7 @@ def save_channel(new_channel: ProtocolAndChannel): save_all_channels(channels) return - # Else: add a new channel entry + # Channel was not found: add a new channel entry LOG.debug("Created a new channel entry on path %s", transport_path) channels.append(new_channel.get_channel_data().to_dict()) save_all_channels(channels) diff --git a/python/src/trezorlib/transport/new/control_byte.py b/python/src/trezorlib/transport/thp/control_byte.py similarity index 100% rename from python/src/trezorlib/transport/new/control_byte.py rename to python/src/trezorlib/transport/thp/control_byte.py diff --git a/python/src/trezorlib/transport/new/protocol_and_channel.py b/python/src/trezorlib/transport/thp/protocol_and_channel.py similarity index 94% rename from python/src/trezorlib/transport/new/protocol_and_channel.py rename to python/src/trezorlib/transport/thp/protocol_and_channel.py index a2c847caff..fa420ac0af 100644 --- a/python/src/trezorlib/transport/new/protocol_and_channel.py +++ b/python/src/trezorlib/transport/thp/protocol_and_channel.py @@ -5,7 +5,7 @@ import logging from ... import messages from ...mapping import ProtobufMapping from .. import Transport -from .channel_data import ChannelData +from ..thp.channel_data import ChannelData LOG = logging.getLogger(__name__) diff --git a/python/src/trezorlib/transport/new/protocol_v1.py b/python/src/trezorlib/transport/thp/protocol_v1.py similarity index 100% rename from python/src/trezorlib/transport/new/protocol_v1.py rename to python/src/trezorlib/transport/thp/protocol_v1.py diff --git a/python/src/trezorlib/transport/new/protocol_v2.py b/python/src/trezorlib/transport/thp/protocol_v2.py similarity index 99% rename from python/src/trezorlib/transport/new/protocol_v2.py rename to python/src/trezorlib/transport/thp/protocol_v2.py index 2e681749c8..4e11901bf8 100644 --- a/python/src/trezorlib/transport/new/protocol_v2.py +++ b/python/src/trezorlib/transport/thp/protocol_v2.py @@ -15,10 +15,10 @@ from ... import exceptions, messages from ...mapping import ProtobufMapping from .. import Transport from ..thp import checksum, curve25519, thp_io +from ..thp.channel_data import ChannelData from ..thp.checksum import CHECKSUM_LENGTH from ..thp.message_header import MessageHeader from . import channel_database, control_byte -from .channel_data import ChannelData from .protocol_and_channel import ProtocolAndChannel LOG = logging.getLogger(__name__) @@ -365,7 +365,7 @@ class ProtocolV2(ProtocolAndChannel): if not is_valid: click.echo( "Received a message with an invalid checksum:" - + hexlify(header.to_bytes_init() + payload + chksum), + + hexlify(header.to_bytes_init() + payload + chksum).decode(), err=True, ) header, payload, chksum = thp_io.read(self.transport) diff --git a/python/tools/pybridge.py b/python/tools/pybridge.py index a48850a80c..d94ec121d0 100644 --- a/python/tools/pybridge.py +++ b/python/tools/pybridge.py @@ -24,8 +24,7 @@ from __future__ import annotations from gevent import monkey -import trezorlib.transport.new -import trezorlib.transport.new.transport +import trezorlib.transport monkey.patch_all() diff --git a/tests/conftest.py b/tests/conftest.py index a453bab5a2..728cedd85a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -33,7 +33,7 @@ from trezorlib.debuglink import TrezorClientDebugLink as Client from trezorlib.device import apply_settings from trezorlib.device import wipe as wipe_device from trezorlib.transport import enumerate_devices, get_transport -from trezorlib.transport.new.protocol_v1 import ProtocolV1 +from trezorlib.transport.thp.protocol_v1 import ProtocolV1 # register rewrites before importing from local package # so that we see details of failed asserts from this module @@ -310,7 +310,7 @@ def client( wipe_device(session) sleep(1) # Makes tests more stable (wait for wipe to finish) - from trezorlib.transport.new import channel_database + from trezorlib.transport.thp import channel_database channel_database.clear_stored_channels() _raw_client.protocol = None