From 3abb67f47eb347884c1baff562b9a7d602494bf0 Mon Sep 17 00:00:00 2001 From: M1nd3r Date: Tue, 12 Mar 2024 13:24:16 +0100 Subject: [PATCH] Fix minor and formatting issues --- core/src/apps/base.py | 1 - core/src/storage/cache.py | 5 +++-- core/src/storage/cache_codec.py | 4 ++-- core/src/storage/cache_common.py | 3 ++- core/src/storage/cache_thp.py | 5 ++++- core/src/trezor/utils.py | 15 ++++++++++----- core/src/trezor/wire/__init__.py | 4 +--- core/src/trezor/wire/context.py | 6 ++++-- core/src/trezor/wire/thp_session.py | 4 +--- core/src/trezor/wire/thp_v1.py | 6 ++---- 10 files changed, 29 insertions(+), 24 deletions(-) diff --git a/core/src/apps/base.py b/core/src/apps/base.py index cf40d9672..62debbb27 100644 --- a/core/src/apps/base.py +++ b/core/src/apps/base.py @@ -1,7 +1,6 @@ from typing import TYPE_CHECKING import storage.cache as storage_cache -import storage.cache_thp as storage_thp_cache import storage.device as storage_device from trezor import TR, config, utils, wire, workflow from trezor.enums import HomescreenFormat, MessageType diff --git a/core/src/storage/cache.py b/core/src/storage/cache.py index 311b6e932..bae005d8b 100644 --- a/core/src/storage/cache.py +++ b/core/src/storage/cache.py @@ -9,7 +9,7 @@ from storage.cache_common import InvalidSessionError, SessionlessCache SESSIONLESS_FLAG = const(128) if TYPE_CHECKING: - from typing import Sequence, TypeVar, overload + from typing import TypeVar, overload T = TypeVar("T") @@ -102,7 +102,8 @@ def delete(key: int) -> None: if TYPE_CHECKING: @overload - def get(key: int) -> bytes | None: ... + def get(key: int) -> bytes | None: + ... @overload def get(key: int, default: T) -> bytes | T: # noqa: F811 diff --git a/core/src/storage/cache_codec.py b/core/src/storage/cache_codec.py index 90ccb92d6..161128077 100644 --- a/core/src/storage/cache_codec.py +++ b/core/src/storage/cache_codec.py @@ -2,10 +2,10 @@ import builtins from micropython import const from typing import TYPE_CHECKING from trezor import utils -from storage.cache_common import DataCache, InvalidSessionError +from storage.cache_common import DataCache if TYPE_CHECKING: - from typing import Sequence, TypeVar, overload + from typing import TypeVar T = TypeVar("T") diff --git a/core/src/storage/cache_common.py b/core/src/storage/cache_common.py index 8e06768dd..39d79186a 100644 --- a/core/src/storage/cache_common.py +++ b/core/src/storage/cache_common.py @@ -27,7 +27,8 @@ class DataCache: if TYPE_CHECKING: @overload - def get(self, key: int) -> bytes | None: ... + def get(self, key: int) -> bytes | None: # noqa: F811 + ... @overload def get(self, key: int, default: T) -> bytes | T: # noqa: F811 diff --git a/core/src/storage/cache_thp.py b/core/src/storage/cache_thp.py index 63f505dc0..970ab17e3 100644 --- a/core/src/storage/cache_thp.py +++ b/core/src/storage/cache_thp.py @@ -7,7 +7,7 @@ from trezor import utils if TYPE_CHECKING: - from typing import Sequence, TypeVar, overload + from typing import TypeVar T = TypeVar("T") @@ -218,6 +218,9 @@ def start_session(session_id: bytes | None) -> bytes: # TODO incomplete def start_existing_session(session_id: bytes) -> bytes: + global _active_session_idx + global _is_active_session_authenticated + if session_id is None: raise ValueError("session_id cannot be None") if get_active_session_id() == session_id: diff --git a/core/src/trezor/utils.py b/core/src/trezor/utils.py index 7b7f00c18..1bf1843e9 100644 --- a/core/src/trezor/utils.py +++ b/core/src/trezor/utils.py @@ -148,19 +148,24 @@ def chunks(items: Chunkable, size: int) -> Iterator[Chunkable]: if TYPE_CHECKING: class HashContext(Protocol): - def update(self, __buf: bytes) -> None: ... + def update(self, __buf: bytes) -> None: + ... - def digest(self) -> bytes: ... + def digest(self) -> bytes: + ... class HashContextInitable(HashContext, Protocol): def __init__( # pylint: disable=super-init-not-called self, __data: bytes | None = None - ) -> None: ... + ) -> None: + ... class Writer(Protocol): - def append(self, __b: int) -> None: ... + def append(self, __b: int) -> None: + ... - def extend(self, __buf: bytes) -> None: ... + def extend(self, __buf: bytes) -> None: + ... if False: # noqa diff --git a/core/src/trezor/wire/__init__.py b/core/src/trezor/wire/__init__.py index f5daa69fe..5d73c3dca 100644 --- a/core/src/trezor/wire/__init__.py +++ b/core/src/trezor/wire/__init__.py @@ -23,17 +23,15 @@ reads the message's header. When the message type is known the first handler is """ -from apps import workflow_handlers from micropython import const from typing import TYPE_CHECKING -from storage.cache_codec import InvalidSessionError +from storage.cache_common import InvalidSessionError from trezor import log, loop, protobuf, utils, workflow from trezor.enums import FailureType from trezor.messages import Failure from trezor.wire import codec_v1, context, protocol_common from trezor.wire.errors import ActionCancelled, DataError, Error -import trezor.enums.MessageType as MT # Import all errors into namespace, so that `wire.Error` is available from # other packages. diff --git a/core/src/trezor/wire/context.py b/core/src/trezor/wire/context.py index 6418df5c2..89701bb5e 100644 --- a/core/src/trezor/wire/context.py +++ b/core/src/trezor/wire/context.py @@ -80,12 +80,14 @@ class Context: @overload async def read( self, expected_types: Container[int] - ) -> protobuf.MessageType: ... + ) -> protobuf.MessageType: + ... @overload async def read( self, expected_types: Container[int], expected_type: type[LoadedMessageType] - ) -> LoadedMessageType: ... + ) -> LoadedMessageType: + ... async def read( self, diff --git a/core/src/trezor/wire/thp_session.py b/core/src/trezor/wire/thp_session.py index 3fd38ec61..1db0b4a3c 100644 --- a/core/src/trezor/wire/thp_session.py +++ b/core/src/trezor/wire/thp_session.py @@ -1,10 +1,8 @@ import ustruct from storage import cache_thp as storage_thp_cache -from storage.cache_thp import SessionThpCache, BROADCAST_CHANNEL_ID -from trezor import io +from storage.cache_thp import SessionThpCache from trezor.wire.protocol_common import WireError from typing import TYPE_CHECKING -from ubinascii import hexlify if TYPE_CHECKING: from trezorio import WireInterface diff --git a/core/src/trezor/wire/thp_v1.py b/core/src/trezor/wire/thp_v1.py index ddb479edd..1263e49bc 100644 --- a/core/src/trezor/wire/thp_v1.py +++ b/core/src/trezor/wire/thp_v1.py @@ -1,17 +1,15 @@ import ustruct from micropython import const from typing import TYPE_CHECKING -from storage.cache_thp import SessionThpCache +from storage.cache_thp import SessionThpCache, BROADCAST_CHANNEL_ID from trezor import io, loop, utils from trezor.crypto import crc from trezor.wire.protocol_common import Message import trezor.wire.thp_session as THP from trezor.wire.thp_session import ( ThpError, - SessionState, - BROADCAST_CHANNEL_ID, + SessionState ) -from ubinascii import hexlify if TYPE_CHECKING: from trezorio import WireInterface