PUBLISHED

24.06.2021

SHARE

TwitterFacebookLinkedin

How to Build Your Own Blockchain in NodeJS

When we’re trying to learn something new, it can oftentimes be tempting to dive headfirst into documentation, articles, and conceptual explanations. While all of that is certainly important, programmers have a great tool in their arsenal for understanding complex topics that they often take for granted.

Building models for different topics in code can help us understand how different pieces of an idea fit together and operate in practice.

In this tutorial, I’ll show you how to build a rudimentary blockchain with some relatively simple Javascript.

What is a Blockchain?

It can be helpful to think of blockchains as augmented linked lists, or arrays in which each element points to the preceding array.

Within each block (equivalent to an element in an array) of the blockchain, there contains at least the following:

  • A timestamp of when the block was added to the chain
  • Some sort of relevant data. In the case of a cryptocurrency, this data would store transactions, but blockchains can be helpful in storing much more than just transactions for a cryptocurrency
  • The encrypted hash of the block that precedes it
  • An encrypted hash based on the data contained within the block(Including the hash of the previous block)

The key component that makes a blockchain so powerful is that embedded in each block’s hash is the data of the previous block (stored through the previous block’s hash). This means that if you alter the data of a block, you will alter its hash, and therefore invalidate the hashes of all future blocks.

Creating a Block

While this can probably be done with vanilla Javascript, for the sake of simplicity we are going to be making a Node.js script and be taking advantage of Node.js’s built-in Crypto package to calculate our hashes.

We can define a block in code like so:

Note that we use SHA256 encryption to hash our function. This is the standard cryptographic hash function that is used in most blockchains because it is incredibly easy to calculate, but incredibly hard to reverse.

We can then create instances of these blocks like so:

let a = new Block({from: “Joe”, to: “Jane”}, precedingHash = “0”)

let b = new Block({from: “Jane”, to: “Joe”}, precedingHash = a.hash)

Try printing out the hashes for these blocks and note how they are different. Also note that if you alter the data of the first block, the hashes of both will change.

Creating a Blockchain

Now that we have our building blocks (pun intended), let’s create a class for our chain. We can define it like so:

First, note that we call the initial block in the chain the Genesis Block. Since this block is the first in the chain, it cannot store any previous hash value.

Next, we also created a function to check the validity of the blockchain to monitor tampering. We are checking for two possibilities.

  • Someone tampered with the data and that the stored hash value is no longer the correct hash value
  • Someone tampered with a previous block’s data, and that the prevHash value stored is therefore incorrect.

If you run that code and print out the value of the chain, you should be able to see how each block in the chain is storing both its own hash, and the hash of the prior block!

That’s all for this example, but if you want to get more comfortable with blockchain, I highly recommend playing around with this code and seeing what breaks the validity of the chain!

Happy coding from your good friends at Codesphere, the next-generation cloud provider.

MORE