Creating A Simple BlockChain using JavaScript

In this article, I am going to demonstrate how to create a Simple Blockchain using JavaScript. So hang in there and watch me create my own Crypto Coin and I am excited already !!!!

So let’s start with creating a block with basic entities in it, and those are :

  • index(optional) — where exactly the block lies in the blockchain
  • timestamp- time at which block was created
  • data- Information that needs to be passed
  • hash- A unique identifier of the block
  • previousHash- hash of previous block that is connected

Here, I am using SH256 hash function from library crypto-js

With this, we can now create a block and can link it to a chain.

Above I have created another class BlockChain that represents our blockchain as a whole.

  • startGenesisBlock- Genesis Block is the initial block in a chain. It uses the same Block class which we created earlier
  • getLatestBlock- To get the Latest block
  • addNewBlock- To add a new block. precedingHash of the block is updated with the hash of the latest block and a new hash is also created after which it gets added into the chain

That’s it! We have our blockchain in place and now let’s just create our coin, straight away!

And I am clearly +80, already !! xD

Wow !! That was simple, but these systems in the real world are more complex with multiple checks and authentication revolving around it. Nonetheless, this is the basic architecture that is followed in creating the systems.

In the next article, we will be seeing how we can add Validation and Proof-of-Work in this Blockchain. You can find the code snippet here

Do Let me know your thoughts on this in the comments below. Thank you so much for your time. 😊

Venturing life, one bit at a time . I love to travel, read and write code