Create your own tiny blockchain using JavaScript

Blockchain and Bitcoin are a few words that are used a lot these days by the Media. But do we really know what is blockchain? In this article I try to highlight a few technical aspects of Blockchain. We will do this building our own tiny Blockchain in Javascript.

Of course, our blockchain will not be anything fancy, just a simple program that demonstrates how blockchain works. First of all let’s understand what is Blockchain.

The blockchain is a list of records called as blocks that are linked to each other forming a ledger. This ledger is secured cryptographically (the hash of the previous block is a part of the data for the next block) using proof-of-work and stored in a decentralized way.

The distribution and the cryptographic security forming a chain makes data in Blockchain technology practically unalterable. There is a whole lot of debate between private blockchain and public blockchain, we won’t get much into that. Without further ado, let’s start with creating our own Blockchain.

Here we have a class that stores, various properties of a Block. We also have a calculateHash method that will calculate the hash of the data combined. prevHash is the hash of the previous block. This is important so when we form a chain, and if an attacker tries to change a Block in the Blockchain, he will have to recalculate the hash of all the following block. Which is practically impossible in a decentralized system sealed by proof of work.

Now that we have our block class ready. Let’s add a class named Blockchain. This class will represent a Blockchain.

We have also added a method to get the last block in the blockchain. This is required because we need to store the hash of the previous block with the current block.

Next, the people using our Blockchain will need a method that will validate the chain. For this we will create a method isChainValid. In this method, we will iterate through the entire chain and validate each and every block.
We must check if the hash store in the chain matches the calculated hash. And also, we will validate if the value in the prevHash of the current block is equal to hash property of the previous Block.

You can run the express server using node app.js and use a tool like Postman to test our APIs

A note on Proof-of-work and Nuance

Here we have built blockchain, but it’s not really secure. The reason is that it’s very easy for an attacker to alter the data in the blockchain and rehash the entire chain. That’s where proof-of-work comes in. In proof-of-work, it’s not just enough to hash the block, but there is a need for a very specific condition to be met for the hash to be valid. Bitcoin, for example, requires the hash to start with a specific number of zeroes, this is also known as the difficulty. But how will we generate a hash that starts with zeroes with the same data? To solve this problem Bitcoin uses a nuance value, which is just a random number generated and added to the data until a valid hash is found.

Conclusion

Thus we have seen how the Blockchain works and we have created a tiny Blockchain using Javascript. I would like to give a shoutout to Savjee, for his content on Blockchain, this article has taken ideas from his brilliant video on creating a blockchain. Watch here.