From afb3e04c24e48f80ffb9e1808c0872e273eb9ee2 Mon Sep 17 00:00:00 2001 From: matejcik Date: Tue, 20 Mar 2018 13:00:36 +0100 Subject: [PATCH] trezorlib/protobuf.py: return BytesType from wire as bytes, not bytearray. This makes more sense, because bytes are immutable and callers have no business mutating structures from the wire anyway. Incidentally this should fix issue #236, where rlp library would treat bytes and bytearrays differently and produce invalid structures in our usecase. Also very minor nitpicks and code cleanup for neater typing. --- trezorlib/protobuf.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/trezorlib/protobuf.py b/trezorlib/protobuf.py index 08699808af..fc50651a54 100644 --- a/trezorlib/protobuf.py +++ b/trezorlib/protobuf.py @@ -244,12 +244,13 @@ def load_message(reader, msg_type): elif ftype is BoolType: fvalue = bool(ivalue) elif ftype is BytesType: - fvalue = bytearray(ivalue) - reader.readinto(fvalue) + buf = bytearray(ivalue) + reader.readinto(buf) + fvalue = bytes(buf) elif ftype is UnicodeType: - fvalue = bytearray(ivalue) - reader.readinto(fvalue) - fvalue = fvalue.decode() + buf = bytearray(ivalue) + reader.readinto(buf) + fvalue = buf.decode() elif issubclass(ftype, MessageType): fvalue = load_message(LimitedReader(reader, ivalue), ftype) else: @@ -270,10 +271,7 @@ def dump_message(writer, msg): fields = mtype.FIELDS for ftag in fields: - field = fields[ftag] - fname = field[0] - ftype = field[1] - fflags = field[2] + fname, ftype, fflags = fields[ftag] fvalue = getattr(msg, fname, None) if fvalue is None: