Skip to Content
Hands-On Blockchain for Python Developers
book

Hands-On Blockchain for Python Developers

by Arjuna Sky Kok
February 2019
Intermediate to advanced
450 pages
9h 59m
English
Packt Publishing
Content preview from Hands-On Blockchain for Python Developers

Combining protobuf, multihash, and base58

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() ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Blockchain in Action

Blockchain in Action

Bina Ramamurthy
Learn Python by Building a Blockchain and Cryptocurrency

Learn Python by Building a Blockchain and Cryptocurrency

Academind by Maximilian Schwarzmüller GmbH

Publisher Resources

ISBN: 9781788627856Supplemental Content