
Intro
Hi hows it going and welcome to another tutorial, with me Norbert BM.
In todays tutorial I am going to show you the inner workings of a blockchain by building a small simulation using JavaScript and NodeJS. This is not a complete Blockchain, but the information in here is just enough for you to understand in a practical way how a block would be created and then added to the blockchain.
I also had a compllet thoraticle and istorical presentation on the Blockchain tehnology in one of my podcast apisodes, so if you are intrerested in that then you can check it out by following the link in the description below. Also the entiere code is down in the description below, including all the resources need for this tutorial.
Table of content and TIMESTAMPS:
Project setup, creating a block using JS classes
- create dir
- create block.js
- create class Block
class Block { constructor(index, timestamp, data, previousHash = "") { this.index = index; this.timestamp = timestamp; this.data = data; this.previousHash = previousHash; this.hash = ""; }
Explanation of the Block components:
- index = indicates where the block sits in the chain
- timestamp = indicates when the block was created
- data = this can be any type of data that you want to be included in the block, for example amount of currency
- previousHash = hash of the privies block.
- hash = the hash of the current block what needs to be calculated
Calculate the hash of a block
In order to calculate the hash of a block I will use SHA256 witch I need to install and then import . The SHA-256 is a patented cryptographic hash function that outputs a value that is 256 bits long.
Note:
From this point on you need NodeJS and npm installed to proccede, if you don’t have them installed and don’t know how to do so, then pls pause the tutorial and visit my videos on how to install npm and nodejs, link are down below
How to install and run NodeJS with NPM on Windows and Mac
- in order to have access to the SHA-256 hash function we need to install it using:
npm install -sava crypto-js
- then bring it in to our js file
const SHA256 = require("crypto-js/sha256");
- now we can add it to the block class via a method
constructor(index, timestamp, data, previousHash = "") { this.index = index; this.timestamp = timestamp; this.data = data; this.previousHash = previousHash; // this.hash = ""; this.hash = this.calculateHash(); this.nonce = 0; } calculateHash() { return SHA256( this.index + this.previousHash + this.timestamp + JSON.stringify(this.data) ).toString(); }
Create the blockchain and the Genesis Block using js classes
Now that we have our bloc class we can add it to the block chain, but for this we first need to create one.
We are going to use classes again.
}
In the blockchain is where we ar going to add our block by initiating them via the contructor function and asignim them to a array
constructor() {
this.chain = [];
}
}
The very first block in the block chain is what is called the “Genesis Block”, and it is created once with the blockchain, this means that we need a method that will create the “Genesis Block” and assign it to the blockchain we the appropriated information in it.
constructor() {
this.chain = [this.createGenesisBlock()];
}
createGenesisBlock() {
return new Block(0, "01/01/2022", "Genesis Block", "0");
}
}
Adding new blocks to the blockchain using JS
Now that we have our blockchain and we also have a block class that creates blocks, we need a method that adds them to the chain. But before we do that we should think about the fact that a new block needs the hash of the previous block. This means that we first need to the previous block, and we need a method for that.
getLatestBlock() {
return this.chain[this.chain.length - 1];
}
After we have our privious block we can now add the new block to the chain by creating a method for this also and refer to the latest block.
addBlock(newBlock) {
newBlock.previousHash = this.getLatestBlock().hash;
newBlock.hash = newBlock.calculateHash();
this.chain.push(newBlock);
}
constructor() {
this.chain = [this.createGenesisBlock()];
}
createGenesisBlock() {
return new Block(0, "01/01/2022", "Genesis Block", "0");
}
// todo: get last block that was added to the chain
getLatestBlock() {
return this.chain[this.chain.length - 1];
}
// todo: add the new Block to the Blockchain
addBlock(newBlock) {
newBlock.previousHash = this.getLatestBlock().hash;
newBlock.hash = newBlock.calculateHash();
this.chain.push(newBlock);
}
}
Test the blockchain
Now it time to test our blockchain by creating and adding blocks to is.
First we need to initiate the blockchain
let norbiCoin = new Blockchain()
norbiCoin.addblock(new Block(1, "23/01/2022", { amount: 100 }))
Add another block norbiCoin.addblock(new Block(2, "15/02/2022", { amount: 20 }))
console.log(JSON.stringify(norbiCoin,null,0))