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:
parent
287c2b5289
commit
0f4e529c1f
@ -120,11 +120,7 @@ async def handle_pairing_request(
|
|||||||
await ctx.show_pairing_method_screen()
|
await ctx.show_pairing_method_screen()
|
||||||
except UnexpectedMessageException as e:
|
except UnexpectedMessageException as e:
|
||||||
raw_response = e.msg
|
raw_response = e.msg
|
||||||
name = message_handler.get_msg_name(raw_response.type)
|
req_type = protobuf.type_for_wire(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)
|
response = message_handler.wrap_protobuf_load(raw_response.data, req_type)
|
||||||
|
|
||||||
if Cancel.is_type_of(response):
|
if Cancel.is_type_of(response):
|
||||||
|
@ -51,25 +51,6 @@ def wrap_protobuf_load(
|
|||||||
raise DataError("Failed to decode message")
|
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:
|
async def handle_single_message(ctx: Context, msg: Message) -> bool:
|
||||||
"""Handle a message that was loaded from a WireInterface by the caller.
|
"""Handle a message that was loaded from a WireInterface by the caller.
|
||||||
|
|
||||||
|
@ -4,7 +4,6 @@ from micropython import const
|
|||||||
from storage.cache_thp import SESSION_ID_LENGTH
|
from storage.cache_thp import SESSION_ID_LENGTH
|
||||||
from trezor import protobuf, utils
|
from trezor import protobuf, utils
|
||||||
from trezor.wire.errors import WireBufferError
|
from trezor.wire.errors import WireBufferError
|
||||||
from trezor.wire.message_handler import get_msg_type
|
|
||||||
|
|
||||||
from . import ThpError
|
from . import ThpError
|
||||||
from .writer import MAX_PAYLOAD_LEN, MESSAGE_TYPE_LENGTH
|
from .writer import MAX_PAYLOAD_LEN, MESSAGE_TYPE_LENGTH
|
||||||
@ -149,8 +148,6 @@ def encode_into_buffer(
|
|||||||
|
|
||||||
# cannot write message without wire type
|
# cannot write message without wire type
|
||||||
msg_type = msg.MESSAGE_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:
|
if msg_type is None:
|
||||||
raise Exception("Message has no wire type.")
|
raise Exception("Message has no wire type.")
|
||||||
|
|
||||||
|
@ -108,11 +108,8 @@ class PairingContext(Context):
|
|||||||
raise UnexpectedMessageException(message)
|
raise UnexpectedMessageException(message)
|
||||||
|
|
||||||
if expected_type is None:
|
if expected_type is None:
|
||||||
name = message_handler.get_msg_name(message.type)
|
expected_type = protobuf.type_for_wire(message.type)
|
||||||
if name is None:
|
assert expected_type is not None
|
||||||
expected_type = protobuf.type_for_wire(message.type)
|
|
||||||
else:
|
|
||||||
expected_type = protobuf.type_for_name(name)
|
|
||||||
|
|
||||||
return message_handler.wrap_protobuf_load(message.data, expected_type)
|
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]
|
self, msg: protobuf.MessageType, expected_type: type[protobuf.MessageType]
|
||||||
) -> protobuf.MessageType:
|
) -> protobuf.MessageType:
|
||||||
expected_wire_type = expected_type.MESSAGE_WIRE_TYPE
|
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
|
assert expected_wire_type is not None
|
||||||
|
|
||||||
await self.write(msg)
|
await self.write(msg)
|
||||||
@ -271,11 +263,13 @@ async def handle_message(
|
|||||||
try:
|
try:
|
||||||
# Find a protobuf.MessageType subclass that describes this
|
# Find a protobuf.MessageType subclass that describes this
|
||||||
# message. Raises if the type is not found.
|
# message. Raises if the type is not found.
|
||||||
name = message_handler.get_msg_name(msg.type)
|
req_type = protobuf.type_for_wire(msg.type)
|
||||||
if name is None:
|
|
||||||
req_type = protobuf.type_for_wire(msg.type)
|
# name = message_handler.get_msg_name(msg.type)
|
||||||
else:
|
# if name is None:
|
||||||
req_type = protobuf.type_for_name(name)
|
# 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
|
# Try to decode the message according to schema from
|
||||||
# `req_type`. Raises if the message is malformed.
|
# `req_type`. Raises if the message is malformed.
|
||||||
|
Loading…
Reference in New Issue
Block a user