Image for post
Image for post
Photo by David Shares on Unsplash

Blockchain and the supply chain — IPFS

Kushagra Jindal
May 13 · 3 min read

IPFS stands for Interplanetary File System. It is a protocol used for storing and sharing data in the distributed file system. This file structure is a Merkle DAG, a combination of Merkle trees and Directed Acyclic Graphs. Making IPFS unique in identifying the files, as each file is referred by a hash.

IPFS helps in increasing the security and speed of the network. For example, DDoS attack won’t work as there is no central system and every request can be responded from the nearest node.

In this blog, I will share how we can store metadata for any product in the distributed file system, IPFS.

Installation:-

To start with IPFS, you need to install it in your system. Download the appropriate setup, according to your Operating System from here, and execute the install.sh file.

After the installation is done, we can check it by the ipfs command in the console.

Command ipfs refs local can be used to check all the files that are already on the ipfs server.

To get details of any file we can use the command.

An Apple Use Case:-

Now, we will see IPFS in action for an apple recorded in the blockchain. In our use case, we will store two important information about an apple which are the colour of apple (maybe a red apple or a green apple) and the date when the apple was plugged. The date here is very important piece of information for our supply chain, as it provides information on the freshness of apple. You can store any other information you find important.

Before we start the implementation start the ipfs server using the command ipfs daemon.

IPFS using python

We can add data to IPFS in three simple steps. First, creating a connection to IPFS server using “/dns/127.0.0.1/tcp/5001/http”. The second step is to collect the metadata which is taken from the user. The colour and date entered by the user are converted to the JSON format.

The final step is to add data to the IPFS server. We can do that using add_json function.

Now, the major task left is to integrate our python script with the smart contract, I created in a previous blog. Before that, we have to interact with the smart contract using web3.js.

We can now interact with the apple smart contract using the ‘apples’ object. For example, we need to call the _mint function of the apple smart contract, we can use apples._mint(address). To call python script from script we will use child_process module. Make sure you import the child_process using const { spawn } = require(‘child_process’) in the node.js file.

Conclusion

You now know, how we can store metadata for a product on the IPFS. We can create a front-end using HTML or React and create a complete distributed application for an apple supply chain.

In a previous blog, I had shared how we can use blockchain technology in supply chain, using an apple use case. If you haven’t read it, find it here.

Integraate

A Software Development Company

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store