Implement a Blockchain in Javascript

In this post, I will walk you through implementing a Blockchain in Javascript. But before we dive into code, lets review some fundamentals.

A blockchain is simply a distributed database of linked blocks/records.

A more technical definition from Wikipedia

is a continuously growing list of records, called blocks, which are linked and secured using cryptography. Each block typically contains a cryptographic hash of the previous block, a timestamp and transaction data.

The Blockchain Technology is the backbone of cryptocurrencies such as Bitcoin.

Lets take a closer look. A typical block in a Blockchain contains

  • data which depends on the type/purpose of the Blockchain, for instance, for Bitcoin, the data will be details of the transaction such as Amount transacted, name, etc.
  • hash of the block which is computed from what is contained in the block,
  • hash of the previous block.
Image Source

Now, any change in block 1 will alter its hash value, consequently block 2’s reference to block 1 will be broken. This is the major strength of the blockchain. However, with more powerful computers, the hash of all the blocks can be recomputed thereby making the chain more valid. To further make the blockchain more secured, we have what is called Proof-of-Work. The idea is to slow down the creation of block

Another implementation to secure the Blockchain is the Smart Contract.

Now lets write some codes.

We will be importing the crypto lib to compute the hash of a block.

We create a Block class and initialize the following properties in its constructor method

See the Pen blockchain001 by Olawale Onabola (@walexi4great) on CodePen.32467

We implement a method to calculate the hash of the block from the index, timestamp, data, previousHash and nounce properties of the block. The method returns the string value of the computed hash.

See the Pen blockchain002 by Olawale Onabola (@walexi4great) on CodePen.32467

We add a method to mine a block. The while loop ensures the computed hash of the block starts with a number of zeroes that is equal to the difficulty parameter. The nounce variable is used to track the iteration.

See the Pen blockchain003 by Olawale Onabola (@walexi4great) on CodePen.32467

In a typical chain, a block has the hash of the previous block as a property. But what will be the previousHash of the first block (index 0)? One way of solving this is to initialize the previousHash of the first block to 0 (or anything you want). The first block is usually called the Genesis Block.

Next, we create a class Blockchain. In its constructor method, we initialize the chain (first block in the chain returned from the createGenesis method call) and the difficulty level associated with mining a block in this particular chain.

See the Pen Blockchain004 by Olawale Onabola (@walexi4great) on CodePen.32467

We add an utility method to get the last block in the chain array or the last block that was added to the chain. To add a new block to the chain, we implement the mineBlock method.

See the Pen Blockchain004 by Olawale Onabola (@walexi4great) on CodePen.32467

We might want to verify the integrity of our blockchain, perhaps it has been tampered with. We add a method to verify the validity of the blockchain.

See the Pen blockchain005 by Olawale Onabola (@walexi4great) on CodePen.32467

Now, lets implement an instance of the Blockchain class, add some blocks, and see things for ourselves. You can edit the code below and run it to see the different possible outcomes. Do things like adding new blocks, changing the difficulty level param, try to alter any block in the chain and then check for the validity.

See the Pen BlockChain Playground by Olawale Onabola (@walexi4great) on CodePen.32467

 

You can download the full source code here.

Sure you enjoy this post, dont forget to make comments and share; subscribe to get more of this. I will be writing a series on Encrypted and Decentralized AI – with Open Mined as a Case Study.

 

Leave a reply:

Your email address will not be published.

Site Footer