Blockchain – Introduction to IPFS

September 13, 2018

Author: Michael Gord

Tutorial - MLG Blockchain

InterPlanetary File System (IPFS) is a new protocol, released as an open source project in 2014, that took advantage of the blockchain protocol and network infrastructure to create a permanent and decentralized method of storing and sharing files securely. IPFS is a single swarm of nodes, exchanging data in a way that is similar to a single Git repository.

IPFS uses a content-addressable distribution protocol with a content-address for each document that is “hyperlinked” to the data. Addresses in IPFS are directly linked to an exact and unalterable block of content. They are also required to view the content, which will be demonstrated in this tutorial. A new content-address is created if the content has been changed, which guarantees that no tampering of data occurs, and is also demonstrated in this tutorial.

IPFS is currently implemented in Go, with a Javascript implementation that exists and a Python implementation in progress. For the purposes of this walkthrough, we assume that you already have homebrew installed.

First we need to install the prerequisites. This tutorial assumes that you have already downloaded HomeBrew. If you haven’t you can download it at this link: https://brew.sh/. Use brew to install

  • brew install git hg
  • brew install Caskroom/cask/osxfuse
  • You must also have golang downloaded

Next download IPFS from here -> https://ipfs.io/docs/install/

In the go-ipfs folder that is created copy and paste the file called IPFS to your /bin folder. Confirm that you have correctly placed IPFS by running the following commands from your terminal. Your terminal should return the same location for IPFS.

$ which IPFS 

Create a new application with EmbarkYou should also be able check the version of IPFS you are using with the following command. Make sure you have ipfs version 0.4.5 to follow along with this tutorial.

$ ipfs version 

Create a new blockchain instance with EmbarkYou can also check the list of commands available with IPFS using help. Many of these we will go over in this tutorial.

$ ipfs help 

Your Embark account has been fundedNow change directory in your terminal to the folder you want to create an IPFS instance in. Initialize IPFS with ipfs init. This creates a file structure with IPFS and a peer identity on the global network. At this point however, you are working locally and are not exposed to the global network.

Run your Embark application to see event logsThis should return a command to your terminal that tells you how to get started. Use that command to ensure everything is working as expected. The code to insert might be different for you.

ipfs cat /ipfs/QmVLDAhCY3X9P2uRudKAryuQFPM5zqA3Yij1dY8FpGbL7T/readme 

Run your Embark application to see event logsThis file is also stored on your computer and was put there when you initialized IPFS. A .ipfs folder was also created on your computer with a folder for keys, a folder for to store data, a folder for block information, and a text file for the version and configuratin. This folder needs to be in a place with alot of space because it is where files you store on IPFS are going to be saved. The folder structure is displayed in the picture below.

Run your Embark application to see event logsYou can check the size of the folder from your terminal with the following command

du -sh ~/.ipfs

Run your Embark application to see event logsNow if you want to add a file to ipfs you can do that with the command ipfs add fileName, which will return a hash. It is called a multihash which is its IPFS content address. Its address is directly based on its content. If you create the same file it will have the exact same hash, as it is the exact same file.

Run your Embark application to see event logsWe use the multihash/content address to read files in IPFS. In the example below you insert in the hash that was returned to you, based on the content you have in your file

ipfs cash multihash

Run your Embark application to see event logsYou cant use the filename to find the content, you need to use the content address. If we make a change to the file, it will have a new content address, so you know that the content stored to your content address has not been tampered with. As you can see, in the example below, the content address changed after the content of the file changed.

Run your Embark application to see event logsThe new content address will bring up the new content. If you change the content back to the initial content, it will change the content address back to the original address.

Run your Embark application to see event logsRight now, this is all being done locally on your computer. If you want to add your files to the IPFS network you must first start the IPFS daemon. Connecting to the IPFS daemon means that your file will be functioning on the live IPFS network. You should see a messaged confirmed when it is completed saying that the daemon is ready.

ipfs daemon

Run your Embark application to see event logsFrom the live network we can now send and receive access to files by sharing the files unique content address.

To see a list of the files that have been saved (note that previous hashes are listed)

ipfs ref local

Run your Embark application to see event logsTo save a file to IPFS and make it so that it is not removable by anyone you need to pin it. To see the current pinned files run (notice that the current hashes are not pinned).

ipfs pin ls

Run your Embark application to see event logsYou can pin a file with the pin command and the content address

ipfs pin add contentAddressHash

Run your Embark application to see event logsYou can remove a previously pinned file with the remove command.

ipfs pin rm contentAddressHash

You can remove files from IPFS storage with the garbage collector command.

ipfs repo gc

Run your Embark application to see event logsMLG Blockchain is a blockchain development and consulting firm based in Toronto that is focused on building next generation applications using blockchain and smart contract technology. View all our blockchain development tutorials at www.mlgblockchain.com/learn.html.

MLG Blockchain