So you want to start coding blockchain? … IPFS & OrbitDB [4.1]
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
git, (3) introduces to
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
- First, install the relevant
npmpackages. For this, navigate to in your working directory and in your console (
Terminalon MacOS), type this command:
npm install ipfs-http-client orbit-db
- save the file as
- 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?
ipfspackage 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 “
- You have used
nodeJSto run your
NodeJShas nothing to do with the IPFS
node. You’ll see
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
- line 7 imports the package
- line 9–32 declare an asynchronous function, that is executed on line 33.
- 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 email@example.com(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.