DEV Community

Cover image for How to Build Your Own Blockchain in NodeJS
Simon Pfeiffer for Codesphere Inc.

Posted on

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.

Top comments (16)

salarc123 profile image

Nice article, but for your code snippets, I think you should limit the comments to only what is necessary. You don’t need a comment to explain “require(‘crypto’)” or “class Block” because those are self explanatory.

tpetrone profile image

Very peaky comment. It does not add up anything to learners, different from the maybe over explanatory comments

simoncodephere profile image
Simon Pfeiffer

Thanks for the feedback! :)

klajdi44 profile image
Klajdi Ajdini

Actually i would argue that the comments are really helpful for someone that is a beginner.

Thread Thread
salarc123 profile image

Yes a lot of the comments are really helpful, but the author used pretty good variable names so some lines don’t need to be explained because the code already explains itself

shnydercom profile image
Jonathan Schneider

Nice read! I looked at the ethereum white paper a couple of years ago and it was hard to wrap my head around the basics. This shows the essentials and with the pseudo-code I can share it with less techy people. Thanks for the article =)

simoncodephere profile image
Simon Pfeiffer

thank you! really glad you enjoyed it :)

akpome profile image

Great article. Better to over comment.

muzammilaalpha profile image

Nice one!

pratap2210 profile image
Pratap Sharma

This was nice and simple. Well written

okumujustine profile image

Nice article, loved it.

andemosa profile image
Anderson Osayerie

Informative. would definitely try this out

simoncodephere profile image
Simon Pfeiffer

happy to hear that!

prabhukadode profile image


fcolombo profile image
Freddy Colombo

Muy buen post,
Es simple, pero explica muy bien el concepto de Blockchain.
Como acotación, el source-code para block.js está errado.
Ambos snippet muestran el source-code de blockchain.js.

En base a su source-code, aquí le dejo mí implementación en GitHub:


ashkanmohammadi profile image

Thanks for the nice article. It was easy to understand for beginners like me.