February 2019
Intermediate to advanced
450 pages
9h 59m
English
Now that you have learned about protobuf, multihash, and base58, we can finally make sense of the puzzle of how the content of the b'I am a good unicorn.\n' file turned into 'QmY7MiYeySnsed1Z3KxqDVYuM8pfiT5gGTqprNaNhUpZgR'.
The b'I am a good unicorn.\n' data is wrapped in an IPFS node and serialized with protobuf into b'\n\x1b\x08\x02\x12\x15I am a good unicorn.\n\x18\x15'. Here's how to do it in Python.
Create a script and name it serialize_unicorn.py:
import unixfs_pb2import merkledag_pb2precious_data = b'I am a good unicorn.\n'unicorn = unixfs_pb2.Data()unicorn.Type = unixfs_pb2.Data.Fileunicorn.Data = precious_dataunicorn.filesize = len(precious_data)serialized_unicorn_node = unicorn.SerializeToString() ...