To create a simple Blockchain in Node.js, we need one Genesis Block. You can refer to Genesis Block as the first block of the Blockchain because Blockchain is an array of blocks, and Genesis is the first block of the Blockchain.
Genesis Block
The genesis block is the first block of the Blockchain. The genesis block is generally hardcoded in the applications that utilize its Blockchain. The Genesis Block is also known as Block Zero or Block 0. It is an ancestor of every Blockchain network’s block that can be traced to its origin back. Remember how every block in the Blockchain is linked back to the previous block using the hash in the block header? So you keep returning and realize every block is connected to the genesis block.
Our Genesis Block contains the following fields or properties.
- timestamp
- lastHash
- hash
- data
So, if we combine all these properties into one object, it will become a Genesis Block. So based on this block, we will mine the second block. Then we will extract the third block from the second block, and so on. So that is why we need a Genesis Block to start a Blockchain.
Now, let us start a project by creating a project folder and then start building an excellent blockchain using Javascript.
Step 1: Create a project and create a Genesis Data
The first step is to create a project. Type the following command.
mkdir cryptochain
Go inside that folder.
cd cryptochain
Now, generate the package.json file if we need to install any node modules in the future.
npm init -y
The next step is to create a new file inside the root called genesis.js and add the following code.
const GENESIS_DATA = {
timestamp: Date.now(),
lastHash: '64b7edc786326651e031a4d12d9838d279571946d8c9a5d448c70db94b0e143f',
hash: 'c671c84681b9d682b9fd43b2a2ef01a343eab7cfa410df9835f8165007d38467',
data: 'krunal'
};
module.exports = { GENESIS_DATA };
Here, we have taken an object with its initial values.
As I have defined earlier, our block contains the four properties. This is genesis data, so we need to hardcode these values.
The properties timestamp and data are you can understand. But I have used the SHA256 algorithm to convert the simple text into hash text.
The actual text for lastHash is a krunal, and the actual text for the hash is a krunalHash.
You can verify it by going to this URL: https://passwordsgenerator.net/sha256-hash-generator/
Here, you can enter my name as a text, and it will give the SHA256 hash of that text. You must convert that text to lowercase using Javascript’s toLowercase(). That is it. You will find the exact hash like me. Cool!! So now, you have Genesis Data ready. The next thing is to create a Block from this data.
Step 2: Create a Block
Create a file called block.js inside your project root and add the following code.
const { GENESIS_DATA } = require('./genesis.js');
class Block {
constructor({timestamp, lastHash, hash, data}) {
this.timestamp = timestamp;
this.lastHash = lastHash;
this.hash = hash;
this.data = data;
}
static genesis() {
return new this(GENESIS_DATA);
}
}
module.exports = Block;
So, here we have imported the GENESIS_DATA for our block.
Then we defined the Block class and passed the parameters to the constructor when we created an object of the Block.
We have also defined the static method called genesis() responsible for returning the Genesis Block for our Blockchain.
Remember, we have previously defined the GENESIS_DATA, not block. Therefore, after creating an object of this class, it will become a Genesis Block.
Step 3: Create a hash based on a previous block.
We must define a function to create a hash based on the previous block’s hash.
So, first, let us create a new file inside the root called crypto-hash.js and add the following code.
// crypto-hash.js
const crypto = require('crypto');
const cryptoHash =(...inputs) => {
const hash = crypto.createHash('sha256');
hash.update(inputs.sort().join(' '));
return hash.digest('hex');
}
module.exports = cryptoHash;
Now, to create a Hash, we need the three properties.
- timestamp
- lastHash
- data
So, we have first required the crypto module provided by Node.js.
Then we have defined the cryptoHash () function, which will accept the inputs. Here we have used the spread operator, which is the syntax of ES6.
Inside that function, we have called the createHash() method and passed the sha256 as a parameter. That means we need to create a hash based on the sha256 algorithm.
Then we sorted and joined that three parameters and returned their hex values.
So, finally, we can get the current block’s hash based on the previous block’s three properties.
Step 4: Mine a new Block based on a previous Block
So, we got the Genesis Block and the hash of that block. We need to write the function to generate a new Block based on the previous one. Right now, in our case, it is a Genesis Block.
Now, import the crypto-hash module inside the block.js file, create a new function called mineBlock(), and pass the two parameters.
- lastBlock
- data
So, our final block.js file looks like this.
// block.js
const { GENESIS_DATA } = require('./genesis.js');
const cryptoHash = require('./crypto-hash');
class Block {
constructor({timestamp, lastHash, hash, data}) {
this.timestamp = timestamp;
this.lastHash = lastHash;
this.hash = hash;
this.data = data;
}
static genesis() {
return new this(GENESIS_DATA);
}
static mineBlock({lastBlock, data}) {
const timestamp = Date.now();
const lastHash = lastBlock.hash;
return new this({
timestamp,
lastHash,
data,
hash: cryptoHash(timestamp, lastHash, data)
});
}
}
module.exports = Block;
So, we have defined the method called mineBlock and passed the two parameters.
The mineBlock() will return an entirely new Block based on the previous block.
Now, it is time to create a Blockchain from these blocks.
Step 5: Create a Blockchain
We have completed the step of creating a Genesis Block and mining a new Block. Now it is the time to develop a blockchain. That is why create a new file inside the root called blockchain.js and add the following code inside it.
// blockchain.js
const Block = require('./block');
class Blockchain {
constructor() {
this.chain = [Block.genesis()];
}
addBlock({ data }) {
const newBlock = Block.mineBlock({
lastBlock: this.chain[this.chain.length-1],
data
});
this.chain.push(newBlock);
}
}
module.exports = Blockchain;
So, I first imported the Block.js file and then created a class called Blockchain.
The Blockchain class is responsible for adding a new block inside the blockchBlockchainlockchain is an array of the blocks starting with Genesis block.
So in the constructor, we have not defined an empty array. Instead, we have filled the chain array with the Genesis block.
Then we have defined one function called addBlock(), which accepts the data.
Now, before adding a new block to the chain array, we need to mine it. That is why we have called the Block class’s mineBlock() method to extract a new block.
For mining a new block, we need a previous block and data. So that is why we have used this code this.chain[this.chain.length-1] because it will return the last Blockchain to the Blockchain and the data we already pass to that function. So we get the new block based on the previous block and data.
The next step is to add that newly mined Blockchain to our Blockchain, and that is it.
The last step is to run this projeBlockchain the Blockchain.
Step 6: Run the projeBlockchain on the Blockchain.
The last step is to create a file inside the project’s root called server.js and add the following code.
// server.js
const Blockchain = require('./blockchain');
const Block = require('./block');
const blockchain = new Blockchain();
for(let i=0; i<5; i++) {
const newData = 'krunal'+i;
blockchain.addBlock({data: newData});
}
console.log(blockchain);
So, here we have imported both the block.js and blockchain.js file and created an object of the Blockchain.
Then we will loop through that Blockchain, add the data, and create the Blockchain because one is Genesis, and five are based on previous blocks.
Go to the terminal and start the node server.
node server
You will see an output like this.
That’s it.

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.
Hi Krunal can you assist me on this error. I just change the data ‘krunal’ using my first name. Thanks in advance. All js are save in one project folder.
internal/modules/cjs/loader.js:583
throw err;
^
Error: Cannot find module ‘./genesis.js’
Thanks for explaining the things. There are many-things which clears my doubt regarding penetration testing.