1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2025-03-12 22:26:08 +00:00

fix: remove unnecessary get_msg_name and get_msg_type functions

[no changelog]
This commit is contained in:
M1nd3r 2025-02-25 13:45:19 +01:00
parent 287c2b5289
commit 0f4e529c1f
4 changed files with 10 additions and 42 deletions

View File

@ -120,11 +120,7 @@ async def handle_pairing_request(
await ctx.show_pairing_method_screen()
except UnexpectedMessageException as e:
raw_response = e.msg
name = message_handler.get_msg_name(raw_response.type)
if name is None:
req_type = protobuf.type_for_wire(raw_response.type)
else:
req_type = protobuf.type_for_name(name)
response = message_handler.wrap_protobuf_load(raw_response.data, req_type)
if Cancel.is_type_of(response):

View File

@ -51,25 +51,6 @@ def wrap_protobuf_load(
raise DataError("Failed to decode message")
if utils.USE_THP:
from trezor.enums import ThpMessageType
def get_msg_name(msg_type: int) -> str | None:
for name in dir(ThpMessageType):
if not name.startswith("__"): # Skip built-in attributes
value = getattr(ThpMessageType, name)
if isinstance(value, int):
if value == msg_type:
return name
return None
def get_msg_type(msg_name: str) -> int | None:
value = getattr(ThpMessageType, msg_name)
if isinstance(value, int):
return value
return None
async def handle_single_message(ctx: Context, msg: Message) -> bool:
"""Handle a message that was loaded from a WireInterface by the caller.

View File

@ -4,7 +4,6 @@ from micropython import const
from storage.cache_thp import SESSION_ID_LENGTH
from trezor import protobuf, utils
from trezor.wire.errors import WireBufferError
from trezor.wire.message_handler import get_msg_type
from . import ThpError
from .writer import MAX_PAYLOAD_LEN, MESSAGE_TYPE_LENGTH
@ -149,8 +148,6 @@ def encode_into_buffer(
# cannot write message without wire type
msg_type = msg.MESSAGE_WIRE_TYPE
if msg_type is None:
msg_type = get_msg_type(msg.MESSAGE_NAME)
if msg_type is None:
raise Exception("Message has no wire type.")

View File

@ -108,11 +108,8 @@ class PairingContext(Context):
raise UnexpectedMessageException(message)
if expected_type is None:
name = message_handler.get_msg_name(message.type)
if name is None:
expected_type = protobuf.type_for_wire(message.type)
else:
expected_type = protobuf.type_for_name(name)
assert expected_type is not None
return message_handler.wrap_protobuf_load(message.data, expected_type)
@ -126,11 +123,6 @@ class PairingContext(Context):
self, msg: protobuf.MessageType, expected_type: type[protobuf.MessageType]
) -> protobuf.MessageType:
expected_wire_type = expected_type.MESSAGE_WIRE_TYPE
if expected_wire_type is None:
expected_wire_type = message_handler.get_msg_type(
expected_type.MESSAGE_NAME
)
assert expected_wire_type is not None
await self.write(msg)
@ -271,11 +263,13 @@ async def handle_message(
try:
# Find a protobuf.MessageType subclass that describes this
# message. Raises if the type is not found.
name = message_handler.get_msg_name(msg.type)
if name is None:
req_type = protobuf.type_for_wire(msg.type)
else:
req_type = protobuf.type_for_name(name)
# name = message_handler.get_msg_name(msg.type)
# if name is None:
# req_type = protobuf.type_for_wire(msg.type)
# else:
# req_type = protobuf.type_for_name(name)
# Try to decode the message according to schema from
# `req_type`. Raises if the message is malformed.