Photo by David Shares on Unsplash

Blockchain and the supply chain — IPFS

Kushagra Jindal
May 13, 2020 · 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.


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

ipfs --version

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

ipfs refs localQmSEind1iCuGcSFWpt2q1B3FjyTfWZFYY3nrV9KMqDe1BT

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

ipfs object get QmSEind1iCuGcSFWpt2q1B3FjyTfWZFYY3nrV9KMqDe1BT{"Links":[],"Data":"\u0008\u0002\u0012.\"{"color": "Red", "Date": "10.04.20"}\"\u0018."}

An Apple Use Case:-

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

ipfs daemonInitializing daemon...
go-ipfs version: 0.4.23-
Repo version: 7
System version: amd64/darwin
Golang version: go1.13.7
Swarm listening on /ip4/
Swarm listening on /ip4/
Swarm listening on /ip6/2409:4053:2e97:b7e2:a502:f0f9:faab:58ee/tcp/4001
Swarm listening on /ip6/2409:4053:2e97:b7e2:ee:7e5:6ec0:e9f4/tcp/4001
Swarm listening on /ip6/::1/tcp/4001
Swarm listening on /p2p-circuit
Swarm announcing /ip4/
Swarm announcing /ip4/
Swarm announcing /ip4/
Swarm announcing /ip6/2409:4053:2e97:b7e2:a502:f0f9:faab:58ee/tcp/4001
Swarm announcing /ip6/2409:4053:2e97:b7e2:ee:7e5:6ec0:e9f4/tcp/4001
Swarm announcing /ip6/::1/tcp/4001
API server listening on /ip4/
Gateway (readonly) server listening on /ip4/
Daemon is ready

IPFS using python

import ipfshttpclient
import json
import sys
client = ipfshttpclient.connect(f"/dns/")metadata = {}
metadata['color'] = sys.argv[1]
metadata['Date'] = sys.argv[2]
metadataJSON = json.dumps(metadata)
addJSON = client.add_json(metadataJSON)
print (addJSON)

We can add data to IPFS in three simple steps. First, creating a connection to IPFS server using “/dns/”. 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.

const Web3 = require('web3',3000)const web3 = new Web3(new Web3.providers.HttpProvider(""))var appleContract = web3.eth.contract('contract abi');apples ='contract address');apples ="0x019F2611875f2710bD1b08aD4Cc2711FA864b714");

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.

web3.eth.defaultAccount = minterAddress;index = parseInt(apples.totalSupply()) + 1apples._mint(minterAddress,{from:minterAddress,gas:3000000})const process = spawn('python3', ['./ipfs/Apple/',color , date]);process.stdout.on( 'data', function(data){  console.log(data.toString())});


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.


A Software Development Company