IPFS & Blockchain: Two Peas in a Pod

Viraz Malhotra
Nov 26, 2019 · 4 min read
How to store documents on IPFS
How to store documents on IPFS

Hi Guys, I am Viraz Malhotra, a Blockchain Developer & Geek, and In this Blog, I would be talking about how you can Integrate IPFS in your Dapps. But first, let’s talk about what is IPFS(interplanetary file system) and why it has created a buzz around the world.

What is IPFS?

Let’s talk about how today’s Web Architecture works(Web 2.0), It’s a typical Client-Server architecture, where if you want a file/document/any kind of data, you go online & search where do you want to find a particular file(Location-Based Searching), but the issue is it’s centralized, where data is stored on a server, and there is redundancy involved when you talk about files/data etc. for instance if you request a file from the server, there is a high chance, that someone on the internet has that file, so now there are two copies of the file. So to summarise this is one of the issues coupled with centralization that are there with Web 2.0.

The idea behind IPFS is to achieve Decentralization of the Web(Web 3.0), where we move away from location-based searching to content-based searching, for instance each person is a node over the IPFSnetwork, and when you want a particular file, you get the hash of the file, and broadcast it over the network by asking if anyone has the hash of the file, they can provide the particular IPFS Object to the requesting party. The best thing is that at any point there is only one copy of a particular file, so this eliminates redundancy and makes the complete process more efficient & logical.

For more details please check out this video.

Integrate IPFS into Dapps

Now, let go a bit more practical, how can you integrate IPFS in your Dapps, well don’t worry:) I have it all covered for you, so basically what I have done is use an IPFS module, to get the hash of a particular file, and stored it on Blockchain.

I have Implemented this on Ethereum & Arweave, I will go through each of them one by one.

IPFS & Ethereum

As I mentioned earlier, I have used this IPFS Module, the process is pretty simple covert the selected file into a Buffer, then use the add method of the ipfs module to get the hash of the file, and then just call a setter method in your Smart Contract & Whoof it’s done:)

const ipfsClient = require(‘ipfs-http-client’);var ipfs = ipfsClient({ host: ‘ipfs.infura.io’, port: 5001, protocol: ‘https’ })//Getting hash of file after adding to ipfs & store it on Ethereum via Smart Contractipfs.add(this.state.buffer, (error, result) => {       console.log('IPFS Result', result)
var memeHash = result[0].hash;
if (error) {
console.log(error);
return;
}
this.state.contract.methods.set(memeHash).send({
from : this.state.account}).then((r) =>{
return this.setState({memeHash})
})
})

IPFS & Arweave

I have recently implemented this while working on a Bounty, Arweave is a platform where you can host Websites, data permanently onto the Blockchain very efficiently. I borked on creating a Bridge between IPFS & Arweave, by storing IPFS data onto Arweave just like I explained above.

So the initial process of getting the hash of the file remains the same. For storing data onto Arweave, you can use its SDK. First, you need to define the arweave client in the code that you will be using and need to have an Arweave wallet with sufficient funds, just a browser extension like metamask, you can find the instructions here.

Interacting with the SDK is a two-part process, first, after creating your wallet you will get a file with your private key in it, so you need to fetch the wallet address by parsing the key store file.

Then after creating the hash of the file with the ipfs module, you just create the transaction via it, sign and submit and is you receive a valid response. Your data is stored on Arweave, so simple:)

Here is complete code snippet:)

//parsing the keystore file to get the wallet address
getwallet = (event) => {
event.preventDefault()
let wallet
const fileReader = new FileReader();fileReader.onload = async (e) => {wallet = JSON.parse(e.target.result);console.log(wallet)const address = await this.state.arweave.wallets.jwkToAddress(wallet)console.log(await this.state.arweave.wallets.getBalance(address))this.setState({ wallet })this.setState({ address })}fileReader.readAsText(event.target.files[0]);}//Submitting the transaction to Arweave Network
let transaction = await this.state.arweave.createTransaction({
data: '<html><head><meta charset="UTF-8"><title>IPFS Data Bridge</title></head><body></body></html>',}, this.state.wallet);transaction.addTag('IPFS-Add', hash);await this.state.arweave.transactions.sign(transaction, this.state.wallet);const response = await this.state.arweave.transactions.post(transaction);console.log(response)

Challenges for IPFS

So what the challenges that IPFS faces in order to be accepted globally. One of the most important things is incentivization, which we see is there in Blockchain’s like Bitcoin & Ethereum, so any node who is storing a particular file for a long time must be incentivized accordingly, I think will really give IPFS a big lift, that said there are like Gitcoin, pulse, Seedless.

Hope you guys, liked this Blog, please share your feedback & do not forget to clap:) You can reach out me on Linkedin for any queries etc.

About Zubi:

Zubi is building a community around emerging technologies, helping people to connect and help each other. Hoping to help millions of people around the globe to connect, help others and make way for new and interesting projects.

Zubi.io

Zubi is a platform that focuses on emerging technologies and provides a gamified experience for an entire journey of learning to career making.

Viraz Malhotra

Written by

Blockchain Developer & Enthusiast|Full Stack Developer|Aim to Decentralize the world|Travelling Addict|Cricket is love

Zubi.io

Zubi.io

Zubi is a platform that focuses on emerging technologies and provides a gamified experience for an entire journey of learning to career making.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade