Purpose: Show how IPLD can be used to work with any hash-linked datastructure.
This exercise assumes basic familiarity with GIT.
Exploring A GIT Log
Use the IPLD explorer to explore a git commit graph. What's the hash of the first commit?
Task: Find the hash of the first commit.
Naming git objects in IPLD
In this exercise, we'll learn the power of CIDs and how to name git commits in IPLD.
In IPLD, we name objects using CIDs. CIDs are self-describing content identifiers; they uniquely name IPLD "nodes" (with a multihash) and tell us how to decode them (with a multicodec). Their binary format is: <cid-version><multicodec><multihash>
however, when written in text documents, we prefix them with a multibase codec. For example, a CID pointing to a raw byte array encoded in base16 might look like f015512208fc4f3c25ee968efc9a3890995cec77c4137f0f7381803aa6f74194b1ad87a00
where f
is the multibase prefix for hex, 0x01 is the CID version, 0x55 is the multicodec, 0x12 is the multihash hash function (sha2-256), and 0x20 is the length of the hash (32 bytes, 256 bits).
With a little bit of work, we can convert from a git commit hash to a CID. In git, a commit is named by its SHA1 hash. To turn this into a CID that can be used with IPLD, we first have to turn the SHA1 hash into a multihash and then construct a CID from that. To convert the commit hash to a multihash, you'll want to use js-multihash. Then, to convert that multihash to a CID, you'll need to use js-cid. The "multicodec" for git objects is git-raw
.
Task: Write a function that converts a git commit id to a CID.
Retrieve A Git Commit Using IPLD
In this exercise, we'll learn how to retrieve IPLD object (including git objects that have been added to IPFS) using js-ipfs.
Now that we can name git commits by CID, we can fetch the git commit via IPFS using the DAG API.
Task: Write a function that takes a commit ID, retrieves the commit from IPFS, and returns the commit as a JSON object {{ need to convert? }}.
Pathing
In this exercise, we'll learn how to use IPLD paths to traverse an IPLD DAG.
Now that we can fetch commits, we can start traversing the commit DAG (or the "git log"). In IPLD, we traverse DAGs by asking an IPLD resolver to lookup a path in an IPLD DAG. In IPLD, a path has the form /ipld/CID/path/to/field
and can traverse multiple nodes, transparently crossing the links between nodes. In js-ipfs, we'll use the same DAG API but this time, we'll give it a path.
Task: Write a function that takes a commit by ID and returns the author of a commit's parent.
Bonus: Find the most frequent contributor
If you reach this point and have some extra time, try writing a function that, given a commit ID, finds the most frequent contributor. Good luck!