I am trying to serialize/deserialize a sub message using Protobuf. The reasons for this is signing. I want to be able to sign part of my message. To be able to do that, I need to canonicalize it somehow.
If it matters, I use protbuf 3.0.0-alpha (With the proto2 language) with Python3.4.
Example file: testp.proto
package my_package;
message my_mess {
message data {
optional uint64 x = 1;
optional uint64 y = 2;
optional uint64 z = 3;
}
optional bytes signature = 4;
}
In this example, I want to sign the data part of the message. Therefore I want to serialize only my_mess.data, sign it, put the signature into my_mess.signature, and then serialize the full message my_mess.
Compiling testp.proto:
$ protoc -I=. --python_out=. testp.proto
[libprotobuf WARNING google/protobuf/compiler/parser.cc:471] No syntax specified for the proto file. Please use 'syntax = "proto2";' or 'syntax = "proto3";' to specify a syntax version. (Defaulted to proto2 syntax.)
I noticed that mm.data have the methods SerializeToString and SerializePartialToString. However, it seems like it is not possible to run them directly. Here are my attempts:
$ ipython
In [1]: import testp_pb2
In [2]: mm = testp_pb2.my_mess()
In [3]: mm.data.x = 1
In [4]: mm.data.y = 2
In [5]: mm.data.z = 3
In [6]: mm.data.SerializeToString()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-6-faa8e25906ca> in <module>()
----> 1 mm.data.SerializeToString()
TypeError: SerializeToString() missing 1 required positional argument: 'self'
In [7]: mm.data.SerializePartialToString()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-7-4b2c1ff0b1c9> in <module>()
----> 1 mm.data.SerializePartialToString()
TypeError: SerializePartialToString() missing 1 required positional argument: 'self'
I was wondering if there is some simple solution I'm missing. I am open to other suggestions, if you have an idea of signing just a part of the message in an elegant way.
Thank you for your help.
I got the same error when I create an c++ example.I added 'syntax = "proto2";' before package declaration in my .proto file then it works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With