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.

Installation:-

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
QmaZSXM7bS5qqteg7mXL3cJVPEsHmi49q82zpqXenHAfs2
QmX2oWY4UeY28rK6BNM5qKaY8JEjS8g9v6CXbnYYLL3Jrh
QmXsabADbc6vWfSXpuiQ5xuZ69V7FZJHLpgwaeNPHaHEK1
QmS2XRHiTbC4DTV2ENvm5BGxCtDhmbWr7FnQdCUopE9S6v
QmX8PaDUodWrmEweYasSv6QNovah1QhANhuVhy1g9MT6aE
QmZZRTyhDpL5Jgift1cHbAhexeE1m2Hw8x8g7rTcPahDvo
QmTDLvFYuD8Huu5wGMfpwkdt7a8rVdCGEKxYs82XSn3fbg
QmPwsqkxiERcmVfsn61CHf7nujYPqo1g76tVgkrTdHan5p
QmV3CmGQtWg5ahRkD5CBiVC9hvX5BXAzcj9hNCJr9ReqXk
QmRAQj9Couqv6sFpnNLY25DkbSgKGKYXy9NqegVhJd4uYi
Qmc2HDfypUUUBzo4oShTTM63rMKZwsfWqD8eLZYr6tuvsu
QmPhJPTLKV6Nf5d5WX5gW65DkFkndZQwUAXmb99quHz7gS
QmZRaKEFfLW1F3CsUbkRkjz63RHmmU1aRNPd5pcysj8VDE
QmYfPjmyhXu58odWcZBfiYcRjBxtTvuyMrkrfJMbgzPDmN
QmbK9C9VaBNsEAuBcayFhcoTaXueuyjqPaHAzGBzPsHzZv
.....

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/127.0.0.1/tcp/4001
Swarm listening on /ip4/172.20.10.2/tcp/4001
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/127.0.0.1/tcp/4001
Swarm announcing /ip4/132.154.99.76/tcp/46138
Swarm announcing /ip4/172.20.10.2/tcp/4001
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/127.0.0.1/tcp/5001
WebUI: http://127.0.0.1:5001/webui
Gateway (readonly) server listening on /ip4/127.0.0.1/tcp/8080
Daemon is ready

IPFS using python

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

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.

const Web3 = require('web3',3000)const web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:8545"))var appleContract = web3.eth.contract('contract abi');apples = appleContract.at('contract address');apples = CoursesContract.at("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/ipfsHandler.py',color , date]);process.stdout.on( 'data', function(data){  console.log(data.toString())});

Conclusion

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