parent
69bea5cff4
commit
78a8b48f1e
@ -1,85 +0,0 @@
|
||||
/*
|
||||
* This file is part of the Trezor project, https://trezor.io/
|
||||
*
|
||||
* Copyright (c) SatoshiLabs
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "py/runtime.h"
|
||||
|
||||
#if MICROPY_PY_TREZORPROTO
|
||||
|
||||
#include "librust.h"
|
||||
|
||||
/// from trezor.protobuf import MessageType
|
||||
/// T = TypeVar("T", bound=MessageType)
|
||||
|
||||
/// def type_for_name(name: str) -> type[T]:
|
||||
/// """Find the message definition for the given protobuf name."""
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorutils_protobuf_type_for_name_obj,
|
||||
protobuf_type_for_name);
|
||||
|
||||
/// def type_for_wire(wire_type: int) -> type[T]:
|
||||
/// """Find the message definition for the given wire type (numeric
|
||||
/// identifier)."""
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorutils_protobuf_type_for_wire_obj,
|
||||
protobuf_type_for_wire);
|
||||
|
||||
/// def decode(
|
||||
/// buffer: bytes,
|
||||
/// msg_type: Type[T],
|
||||
/// enable_experimental: bool,
|
||||
/// ) -> T:
|
||||
/// """Decode data in the buffer into the specified message type."""
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_3(mod_trezorutils_protobuf_decode_obj,
|
||||
protobuf_decode);
|
||||
|
||||
/// def encoded_length(msg: MessageType) -> int:
|
||||
/// """Calculate length of encoding of the specified message."""
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_trezorutils_protobuf_encoded_length_obj,
|
||||
protobuf_len);
|
||||
|
||||
/// def encode(buffer: bytearray, msg: MessageType) -> int:
|
||||
/// """Encode the message into the specified buffer. Return length of
|
||||
/// encoding."""
|
||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(mod_trezorutils_protobuf_encode_obj,
|
||||
protobuf_encode);
|
||||
|
||||
STATIC const mp_rom_map_elem_t mp_module_trezorproto_globals_table[] = {
|
||||
{MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_trezorproto)},
|
||||
|
||||
{MP_ROM_QSTR(MP_QSTR_type_for_name),
|
||||
MP_ROM_PTR(&mod_trezorutils_protobuf_type_for_name_obj)},
|
||||
{MP_ROM_QSTR(MP_QSTR_type_for_wire),
|
||||
MP_ROM_PTR(&mod_trezorutils_protobuf_type_for_wire_obj)},
|
||||
{MP_ROM_QSTR(MP_QSTR_decode),
|
||||
MP_ROM_PTR(&mod_trezorutils_protobuf_decode_obj)},
|
||||
{MP_ROM_QSTR(MP_QSTR_encoded_length),
|
||||
MP_ROM_PTR(&mod_trezorutils_protobuf_encoded_length_obj)},
|
||||
{MP_ROM_QSTR(MP_QSTR_encode),
|
||||
MP_ROM_PTR(&mod_trezorutils_protobuf_encode_obj)},
|
||||
};
|
||||
|
||||
STATIC MP_DEFINE_CONST_DICT(mp_module_trezorproto_globals,
|
||||
mp_module_trezorproto_globals_table);
|
||||
|
||||
const mp_obj_module_t mp_module_trezorproto = {
|
||||
.base = {&mp_type_module},
|
||||
.globals = (mp_obj_dict_t *)&mp_module_trezorproto_globals,
|
||||
};
|
||||
|
||||
MP_REGISTER_MODULE(MP_QSTR_trezorproto, mp_module_trezorproto);
|
||||
|
||||
#endif // MICROPY_PY_TREZORPROTO
|
@ -1,34 +1,47 @@
|
||||
from typing import *
|
||||
from trezor.protobuf import MessageType
|
||||
from typing_extensions import Self
|
||||
# XXX
|
||||
# Note that MessageType "subclasses" are not true subclasses, but instead instances
|
||||
# of the built-in metaclass MsgDef. MessageType instances are in fact instances of
|
||||
# the built-in type Msg. That is why isinstance checks do not work, and instead the
|
||||
# MessageTypeSubclass.is_type_of() method must be used.
|
||||
|
||||
|
||||
# rust/src/protobuf/obj.rs
|
||||
class MessageType:
|
||||
MESSAGE_NAME: ClassVar[str] = "MessageType"
|
||||
MESSAGE_WIRE_TYPE: ClassVar[int | None] = None
|
||||
@classmethod
|
||||
def is_type_of(cls: type[Self], msg: "MessageType") -> TypeGuard[Self]:
|
||||
"""Identify if the provided message belongs to this type."""
|
||||
T = TypeVar("T", bound=MessageType)
|
||||
|
||||
|
||||
# extmod/rustmods/modtrezorproto.c
|
||||
def type_for_name(name: str) -> type[T]:
|
||||
# rust/src/protobuf/obj.rs
|
||||
def type_for_name(name: str) -> type[MessageType]:
|
||||
"""Find the message definition for the given protobuf name."""
|
||||
|
||||
|
||||
# extmod/rustmods/modtrezorproto.c
|
||||
def type_for_wire(wire_type: int) -> type[T]:
|
||||
"""Find the message definition for the given wire type (numeric
|
||||
identifier)."""
|
||||
# rust/src/protobuf/obj.rs
|
||||
def type_for_wire(wire_id: int) -> type[MessageType]:
|
||||
"""Find the message definition for the given wire type (numeric identifier)."""
|
||||
|
||||
|
||||
# extmod/rustmods/modtrezorproto.c
|
||||
# rust/src/protobuf/obj.rs
|
||||
def decode(
|
||||
buffer: bytes,
|
||||
msg_type: Type[T],
|
||||
msg_type: type[T],
|
||||
enable_experimental: bool,
|
||||
) -> T:
|
||||
"""Decode data in the buffer into the specified message type."""
|
||||
|
||||
|
||||
# extmod/rustmods/modtrezorproto.c
|
||||
# rust/src/protobuf/obj.rs
|
||||
def encoded_length(msg: MessageType) -> int:
|
||||
"""Calculate length of encoding of the specified message."""
|
||||
|
||||
|
||||
# extmod/rustmods/modtrezorproto.c
|
||||
# rust/src/protobuf/obj.rs
|
||||
def encode(buffer: bytearray, msg: MessageType) -> int:
|
||||
"""Encode the message into the specified buffer. Return length of
|
||||
encoding."""
|
||||
|
Loading…
Reference in new issue