Written by Philipe Atela

In our quest to make Swapy as decentralized as possible, it is imperative to find a decentralized solution for temporary and/or permanent data storage. Considering IPFS as one of the most mature distributed storage alternatives, the point turns out into finding a way to calculate the path to a specific file on IPFS out of the node. Although IPFS uses multihash hashes to index files on its filesystem, it is not possible to retrieve the desired hash by simply hashing the target data entirely. In fact, before storing the data, IPFS first organizes it into a DAG of 256Kb chunks.

As noted by David Dias on this thread on IPFS, the process to calculate the hash of a certain piece of data on IPFS would be:

1) Process the data to obtain chunks of 256kb from it;

2) Compute a DAG node for each chunk;

3) Link each chunk to obtain the final DAG;

4) Hash (multihash algorithm) the final DAG;

To avoid implementing this whole process from scratch, we therefore explored the ipld-dag-pb javascript library:

Code for offline Multihash generation

In the code snippet above, we first create a Unixfs object from the data buffer, then we create the DAG structure (`DAGNode.create`) out of a protobuf encoded Unixfs object (`unixFs.marshal`) in order to finally retrieve the structure’s multihash (`dagNode.toJSON().multihash`).

The process of obtaining an IPFS file multihash out of the node is being used by Swapy in task such as:

* Cross-user data sharing

* Decentralized authentication

* Decentralized session

* More upcoming features

Since it took some time to figure out a solution to this issue, we thought it would be interesting to share it with the community. We hope this code snippet can be valuable to other developers out there working with IPFS!

Learn more about Swapy Network: