How to build decentralized applications using blockchain
Major credit for blockchain popularity goes to bitcoin. The invention of bitcoin by Satoshi in 2009 led to the revolution in FinTech, especially in financial transactions. Until March last year, bitcoin was big, from then on, it had the greatest downfall.
Technicians and scientists figured out a way to take the best part of bitcoin popularity and the way it works in immutable decentralized ledgers to apply it to various applications.
The term blockchain may be new, but the technology running behind blockchain, like decentralization or cryptography or Merkle trees, have actually been around for a while now. In the 90s concepts of Merkle tree, distributed applications, decentralization and asymmetric cryptographies were already there. Satoshi in 2009 then combined multiple technologies together to solve one simple problem, the problem to remove third-party trust issues by using immutable stored ledgers spread across multiple peers around the world. Although designed only for transactional storage, when volumes of transactions increased around 2011, 2012 the majority of the bitcoins started being stored on a blockchain.
Things changed with Vitalik Buterin invention of Etherum, other than using blockchain for transactions, you could now also write code and execute it. And, blockchain-based smart contracts was born. These contracts help you to write decisions, the way we write or code small functions and the various decision controls which get executed on top of blockchain-based on different conditions. Let’s take an escrow for example, so a middle party who is facilitating service and payment between two parties can actually be programmed and deployed on Ethereum. Then, that third-party trust will be remote and peer-to-peer can actually make sure that, if one has provided a service with the Ethereum or the money will be transferred from the service provider account to the one who did the work. These contracts actually revolutionized the way we think about blockchain. Initially, it was all about storing transactions or disrupting the financial industry or creating cryptocurrencies for countries for varied applications. But smart contracts started a wave of new kinds of applications. It is possible now to run your app on a decentralized network on top of your existing Internet. This is what we term as decentralized applications, which is either running on permission blockchain or a public blockchain.
A public blockchain is great, but the openness of the network and anonymity of peers pose as a risk for organisations. So, the majority of organisations who want to leverage blockchain feature usually want it within their own permission network. For this, there are federated or permitted blockchain and one of the most popular projects in that is Hyperledger. You can consider Hyperledger as the ecosystem of the blockchain network. They don’t just provide Hyperledger fabric, they have Hyperledger ID, Hyperledger Quill for different scenarios. For example, if someone wants to have a private and permission supply chain blockchain system they can use Hyperledger Quill to build that. If they want to have an Identity-based blockchain system they can use Hyperledger Quill to do that. In the Hyperledger fabric, there is a basic blockchain network on which you can execute the contracts.
The consensus in the public blockchain network is done by mining. So the way it works is, whenever someone wants to add a block into the network, they have to prove that they have actually worked for it. The way bitcoin does that is, they give you the difficulty number which is in hexadecimal, they also give you the target number, and all you have to do is calculate a hash which has that number of zeros in front of it. It seems simple but calculating a random hash is a very CPU intensive task, that takes a lot and lot of energy to basically compute a hash which matches the difficulty number. But in case of permission blockchain, you don’t need to do that at all because all your nodes and peer-to-peer network are joined only with your permission and they have been assigned a certificate of authority.
In the case of Hyperledger as well, there are endorser peer, anchor peer and normal general peers. If we take the example of Accion Labs, Company A is Accion Labs Mumbai and Company B is Accion Labs in Bangalore. The ordering peer is the one that communicates between the 2 permission blockchain networks. If the client wants to add something to the blockchain, the first contact the endorsement peer, endorsement peer’s job is to validate whether the client is correct or not by verifying the certificate authority, that’s pretty much it. Then, the client contacts the ordering peer, ordering peer does the entry into the blockchain and ordering peer then contacts the anchor peer of each network which in-turn are going to update each of the blockchain networks, and all the peers get synched up and consensus happens. These applications, especially private blockchain network is very suitable for highly sensitive data such as the hospital data which includes patient information or identity information. Putting these in a public blockchain is not right, all this information should be in a private hyperledger network. Companies such as Amazon, IBM, Infura, Azure, they are providing blockchain as a service that you can participate by paying a small fee. All you have to do is run your smart contracts into that and give focus on business logic rather than maintaining all the peers into the network. Some of the providers are AWS, Azure, Google, IBM, Oracle, SAP, Ethereum, Quorum, The Hyperledger, Digital Asset and CRDA.
Who is getting disrupted
Let’s look at some of the industries that blockchain is either disrupting or will be disrupting. These are broadly divided into 4 sections i.e smart contracts, digital currency, record-keeping, and securities. Blockchain is already disrupting the financial sector by putting all the transactional records in the distributed ledger. In record-keeping, intellectual property or ownership, the tracking of assets from its source to its current owner is recorded in each of the steps along the supply chain journey in the blockchain networks. It is also a revolution in counterfeit operation. For example, when buying a phone, if a chip inside your phone by just a scan can provide all the details of its journey as an asset written into the blockchain network, you can know buyer information to manufacture details in one go. In Securities, you can put equity information, property information and all those sensitive information which actually comes under the trust-based issues on a blockchain. In smart contracts, there is Escrow, Wager, Digital Rights. However, the area that is emerging and being developed inspired by blockchain is Decentralized Application.
Decentralized web applications
The world wide web right now is completely centralized, you have a server you have your client and you have multiple data centres, but the centralised nature of the world wide web and Internet right now has given a lot of issues on the central point of failure, stealing of data, intrusion by any government etc. What if we make Internet public and public participates and becomes a peer rather than trusting one person who will host the central server. This will revolutionise and build a new Internet which we are referring to as Web 3.0. This is already happening. If we consider Ethereum for example, then blockchain is good to store transactional data which is in kbs as all you have to store is ‘from address’, ‘to address’, a bit of metadata, hash it together and put it on the chip. But when it comes to storing files such as videos or any such data on the blockchain, Ethereum won’t work because it’s not meant to store this type of data.
Ethereum has a project called Swarm, which is like file storage on a decentralized network, communicating together with Ethereum to make sure every entry is on record. They also have a project called Whisper, which is like an event cue between decentralized applications, because in order to develop an application from the developers' point of view, you will need a database, you will need event cues and you need to have a protocol on which you can communicate with other peers. The only drawback is that you have to be on the Ethereum platform in order to use these tools.
Then comes the open decentralized applications. The most popular one is from protocol.ai which is dedicated to making a decentralized version of the Internet. Consider like a TCP/IP stack, application layer, networking layer, data layer. And, the networking layer right now is huge. The InterPlanetary File System (IPFS) are building a protocol which is going to run on top of existing protocols such as TCP/IP, UDP, Web RCTs so that not every mass user has to update their browser and everything else. The LibP2P is the peer to peer library to make sure peers are recognised and identified, they are connected together, named together and you’re sharing your data in complete encryption format using asymmetric cryptography used in blockchain. IPNS or InterPlanetary Naming service works the way we use DNS to name our nodes and route the traffic from one node to another in a decentralized manner. Then comes the layer which is the IPFS stack – the development layer. So, these two layers, the LibP2P and IPNS, these are networking. When it comes to developing a decentralized app, you have projects such as IPFS, which is InterPlanetary File System. These two computers, right now are IPFS Peered, and you can run your IPFS master server in your machine, and share that file across your peer. Once you add the file, you get the hash. You share that hash with anyone, they are easily going to access it. This is one of the screenshots of IPFS, you have multiple peers connected across the world, you can share your files, and you can ask anyone in the whole peer to access that file without using any centralised server, it’s all peer to peer.
There was also a very interesting IPFS project going on called Steemit https://steemit.com which is a social media platform where everyone gets paid for creating and curating content in cryptocurrencies. They were the first one to actually solve the problem of putting metadata information on a blockchain. It is more like Reddit, where you can ask questions, you can upvote, comment etc, and it is completely decentralized. The Steemit blockchain allows you to put your metadata info in blockchain and the interesting feature is that users also get paid on the platform. So whoever is part of the network and if they are performing well, Steem actually rewards them using Steem coin, which you can then go to the exchanges like banks, and convert it into dollars. The same idea has been taken to the next example, this is called D.Tube, It’s a decentralized version of YouTube. Currently working on IPFS and steem blockchain.
Demo: Decentralized blockchain applications
It looks and sounds very similar to YouTube, right? But DTube is not client to server-based, it is completely peer to peer, and people who are participating in the network are actually getting paid in Steem coins. So, if your content is good and you’re getting a lot of views, you’ll get paid. Not by advertisers, but by the system itself. That is a very good example, so if I play any video, It’s not as good as YouTube, but this video is being played on IPFS, this video is stored on IPFS, the metadata is stored on Steem, and if he gets a lot of views you can see he is getting paid. This is really a great example of the emerging trends with decentralization applications where things are fast – and digital products are already hitting the markets. This is one of the examples, there are a lot more on the Internet, where people are actually contributing and developing on top of IPFS stack or Ethereum stack to develop new types of applications for the next generation of the web.
Demo: Ethereum P2P network
Before we go ahead on decentralization, I just want to give a small demo on how smart contracts actually work. This is a bit of sample application architecture, so if you want to be part of the Ethereum network or any bitcoin network, basically you need to become a peer. So, you become a peer, join the network, synchronize all the data of the network, and that is the part of Ethereum P2P network, you can write your code which will be Ethereum code which is already a programming language and your code will be deployed on an Ethereum blockchain that can be invoked using web3.js which is also the Ethereum’s library. Let’s look over one small development like I gave you the escrow example, I’ve made a small demo. This is the screen to a freelancer who requested an invoice to a person, this is just a sample form to get the details and to whom he wants to send and all the product details. The rate and the price we are actually calculating in Ethereum. Suppose he has done some service and he wants to be paid in the form of Ethereum, 100 Ethereum to a company. So, he can send an invoice and this invoice will be recorded in a blockchain, He’ll be given a transaction hash, this is the hash given on every contract execution. Either your contract is like adding an invoice or if your contract is, for example, the main task basically in the blockchain you can consider it is recorded and you have a hash. Whatever you do, your action is recorded and you get a hash as a response that your record has been added to the blockchain. This you can actually verify. This is the small tool we run in our local machine called Ganache. In that, if you place your transaction hash you can see that it is already added into the network. In a practical scenario, this will be added and mined into the blockchain network so that it can’t be altered at all. So, the guy at the company level cannot say that they haven’t received the invoice because it is recorded in the blockchain, so the trust factor has been removed by that part. Then comes the company so, earlier the company received the invoice and we can again verify the hash because we also get the hash of the customer and then if he approves, he also gets a hash for that he has approved and the money has been transferred. So that even the freelancer can’t say that I haven’t received the money because it’s also recorded in the blockchain network. When you go to the client screen you can check into the network, you can see here that the balance has been increased, and if you see the company’s account, he also gets the approval hash, the company’s balance is deducted, this all happened without having any third party having to facilitate the payment. It’s all peer to peer.
Here’s a very simple app where one can log-in and add the patient information. These are already added. Just click on, give them some ID, name, payment details, patient medical information, all those sensitive data that you want to store. It will first get added into the Hyperledger and then any middle-level databases, so have cross-references between ID and the blockchain hash. This entry you can then verify your Hyperledger network. Hyperledger also provides the blockchain viewer software as well. Basically, provides all the development you need to develop an app either on Hyperledger fabric or on facilitated technology as well. This information is added into the blockchain network, and then you can add that information back to whichever database you want to have for cross-referencing.
Demo: IPFS OrbitDB
One of the projects which is going on, it is also open-source, it is called OrbitDB, which is like MongoDB for decentralized apps, running on top of IPFS. Any developer can use that, add their entries into the database, which will get replicated across the peers. We have made two machines IPFS peers, and made a small demo just to show the replication part of the IPFS database. This is a very simple To-Do app, where one can make an entry, and the way I did the configuration is that this peer, and the databases that I created, I’m replicating on that peer. So, if I make an entry over there, I should get it over here as well without any centralized server synchronization. Let me just check if that IPFS is synced, this is the real screen, you can see peers. Let me make a small entry here. The entry is added, and it is added into the IPFS. There you go. This synchronization happens without any centralization server, and if I make any changes in this note, which is also IPFS note, like presumably if I check this note, I should get it here as well. Ideally the To-Do doesn’t have to work like this, I just want to show the replication part of OrbitDB running on top of IPFS.
With that demo, I just want to conclude that decentralized web is going to be the future of the Internet and it already has digital products running on it even as we speak.