I’m trying to reproduce the binary representation of a simple “Hello World\n” block. I know what the output should be from this…
============================
% echo Hello World | ipfs add
added QmWATWQ7fVPP2EFGu71UkfnqhYXDYH566qy47CnJDgvs8u
…etc.
============================
% ipfs block get QmWATWQ7fVPP2EFGu71UkfnqhYXDYH566qy47CnJDgvs8u | hexdump -C
00000000 0a 12 08 02 12 0c 48 65 6c 6c 6f 20 57 6f 72 6c |…Hello Worl|
00000010 64 0a 18 0c |d…|
00000014
============================
I created the Protocol Buffers code with this proto file:
syntax = “proto2”;
message PBLink {
optional bytes Hash = 1;
optional string Name = 2;
optional uint64 Tsize = 3;
}
message PBNode {
optional bytes Data = 1;
repeated PBLink Links = 2;
}
message Metadata {
optional string MimeType = 1;
}
message UnixTime {
required int64 Seconds = 1;
optional fixed32 FractionalNanoseconds = 2;
}
message Data {
enum DataType {
Raw = 0;
Directory = 1;
File = 2;
Metadata = 3;
Symlink = 4;
HAMTShard = 5;
}
required DataType Type = 1;
optional bytes Data = 2;
optional uint64 filesize = 3;
repeated uint64 blocksizes = 4;
optional uint64 hashType = 5;
optional uint64 fanout = 6;
optional uint32 mode = 7;
optional UnixTime mtime = 8;
}
============================
Here is Python code that used the Protocol Buffers “compiled” code:
#!/usr/bin/env python3
import ipfs_pb2
import subprocess
unixfs = ipfs_pb2.Data()
unixfs.Type = 2
unixfs.Data = b"Hello World\n"
dag = ipfs_pb2.PBNode()
dag.Data = unixfs.SerializeToString()
print(dag.SerializeToString())
print(dag.SerializeToString().hex())
============================
Here is the output of my Python code above…
b’\n\x10\x08\x02\x12\x0cHello World\n’
0a100802120c48656c6c6f20576f726c640a
============================
Notice the correct output has 2 extra bytes after the “Hello World\n”. I suspect the problem is that I’m not specifying the empty PBLink vector in the PBNode of the DAG? How specify that empty object?
Any help greatly appreciated.
Sincerely,
Chris