So you want to start coding blockchain? … IPFS & OrbitDB [4.1]

Khang Vu Tien
4 min readJul 8, 2021
Photo by Suzanne Emily O’Connor on Unsplash

(Warning of October 2022: more than one year after publication, this article is not valid any more because its development environment has changed. See comment below by a careful reader. I’m in the process of rewriting it. Stay tuned but please don’t hold your breath 😉)

In the previous posts we have gone through several steps in learning how to code blockchain.

  • Step 0: describes how efficient are the modern development tools, with a hands-on example to code and deploy your own web server and its client in only 10 lines of code.
  • Steps 1, 2 and 3: (1) explains MetaMask and how to install and use it to sign and pay transactions, (2) explains installation of nodeJS, truffle, git, (3) introduces to VSCode.

We continue this journey here with IPFS and OrbitDB, through a short JavaScript application. In the same approach as in Step 0, this application shows how to leverage existing powerful libraries to create and use a peer-to-peer decentralised database with only a few lines of code.

What will we learn today?

Instead of proceeding chronologically, the plan of this tutorial innovates as follows:

  • We start with explaining the code itself, in case you have already done the steps above and have your development environment.
  • For those who have not, we repeat how to set it up. If you are really new, jump to this section and then come back to run the code part.
  • Last but not least, we’ll talk about IPFS and OrbitDB design principles and see how they differ from legacy databases. If you are a Manager or a System Architect, you may want to skip the coding and jump directly here. We’ll also explain how in project Machu Picchu we use IPFS and OrbitDB to complement the blockchain.

The application (kind of) “Hello World” storyboard

The code

  • First, install the relevant npm packages. For this, navigate to in your working directory and in your console (Terminal on MacOS), type this command: npm install ipfs-http-client orbit-db
  • In your favorite text editor, copy-paste the following JavaScript code
  • save the file as index.js
  • in your console, type the following command: ipfs daemon --enable-pubsub-experiment & node index.js
  • the output should be something similar to the following

Congratulations. You have spawned an IPFS node, created a peer-to-peer key-value database, written 2 entries and read them back. That’s all folks.

What have you done?

  • You have installed 2 JavaScript packages to use IPFS and OrbitDB. The ipfs package includes a daemon that can be called from the command line to create an IPFS peer client
  • You have launched the IPFS daemon to run in background an IPFS client, also called a “peer” or a “node”.
  • You have used nodeJS to run your index.js code. NodeJS has nothing to do with the IPFS node. You’ll see peers and nodes everywhere when programming blockchain and IPFS 🤓).

You may want to pursue and read more about IPFS and OrbitDB and see how their design differ from legacy databases.

Explanation of the code

  • line 6 imports the package ipfs.
  • line 7 imports the package orbit-db.
  • line 9–32 declare an asynchronous function, that is executed on line 33.
  • line 11 creates a JavaScript JSON to interface the API exposed by the spawned IPFS node.
  • line 13 creates a JavaScript JSON with all methods to handle a peer-to-peer distributed database on IPFS.
  • line 16 creates a key-value database that is actually an IPFS file.
  • lines 19-20 write 2 key-value pairs in the file.
  • line 21 closes the database (stops replication?).
  • lines 25–31 reopen the database and read data.

Reminder: how to install your development environment

Setup instructions for MacOS

  • Install Brew
  • Install nodeJS via brew: brew install node@14.17.0 (this version of nodeJS is the last long term stable support)

Setup instructions for Linux Ubuntu

curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt-get install -y nodejs

Under Windows, it is strongly recommended to run a VirtualBox for Windows and install a Ubuntu Virtual Machine inside it. Here is a YouTube video of 4 minutes, that can guide you through this double installation process: https://youtu.be/8mns5yqMfZk.

Next step: how to design a peer-to-peer decentralised database using IPFS & OrbitDB

See the second part here: https://kvutien-yes.medium.com/so-you-want-to-start-coding-blockchain-ipfs-orbitdb-4-2-9f65b1c66da.

--

--