Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How do I evaluate blockchain implemented in JavaScript?
Blockchain is a chain of blocks that contains information. In 2009, this technology was later adapted by Satoshi Nakamoto to create the digital cryptocurrency Bitcoin. This technology is completely open to anyone who wants to develop or analyze it. Blockchain has a feature that makes it very complex to make changes once data has been recorded in a block of the chain.
Here are the key terms used in blockchain programming:
Block ? Contains information like data, hash value, and previous block hash value.
Data ? Information stored in the block (for cryptocurrency: transaction details like sender, receiver, and amount).
Hash ? A unique string identifier for each block. Like a fingerprint, it uniquely identifies the block. Changing the block data changes the hash, making tampering detectable.
Previous Hash ? The hash value of the previous block, used to create the chain connection.
The first block is called the Genesis block as it cannot point to a previous block. If you change a hash value, the next block's previous hash value will no longer be valid, breaking the chain integrity.
Setting Up the Environment
We'll use the crypto-js library, which provides cryptographic algorithms for hashing, encryption, and key generation in Node.js environments. Install it using:
npm install crypto-js
Block Class Implementation
The Block class represents individual blocks in our blockchain:
class Block {
constructor(prev_hashValue, data) {
this.data = data;
this.prev_hashValue = prev_hashValue;
this.time_stamp = new Date();
this.pf_work = 0; // Proof of work nonce
this.hash = this.calculateHash();
}
calculateHash() {
return SHA256(this.pf_work + this.prev_hashValue + this.time_stamp + JSON.stringify(this.data)).toString();
}
mine(difficulty) {
while (!this.hash.startsWith("0".repeat(difficulty))) {
this.pf_work++;
this.hash = this.calculateHash();
}
console.log(`Block mined: ${this.hash}`);
}
}
The Block class has five properties:
data ? Stores the block's data (transactions, etc.)
hash ? The block's unique hash identifier
prev_hashValue ? Hash of the previous block
time_stamp ? When the block was created
pf_work ? Proof of work nonce (incremented during mining)
Blockchain Class Implementation
The Blockchain class manages the chain of blocks:
class Blockchain {
constructor() {
let genesisBlock = new Block("0", {isGenesisBlock: true});
this.chain = [genesisBlock];
}
addNewBlock(data) {
let lastBlock = this.chain[this.chain.length - 1];
let newBlock = new Block(lastBlock.hash, data);
newBlock.mine(2); // Mining difficulty = 2
this.chain.push(newBlock);
}
isValid_hash() {
for (let i = 1; i
Complete Working Example
const SHA256 = require('crypto-js/sha256');
class Block {
constructor(prev_hashValue, data) {
this.data = data;
this.prev_hashValue = prev_hashValue;
this.time_stamp = new Date();
this.pf_work = 0;
this.hash = this.calculateHash();
}
calculateHash() {
return SHA256(this.pf_work + this.prev_hashValue + this.time_stamp + JSON.stringify(this.data)).toString();
}
mine(difficulty) {
while (!this.hash.startsWith("0".repeat(difficulty))) {
this.pf_work++;
this.hash = this.calculateHash();
}
console.log(`Block mined: ${this.hash}`);
}
}
class Blockchain {
constructor() {
let genesisBlock = new Block("0", {isGenesisBlock: true});
this.chain = [genesisBlock];
}
addNewBlock(data) {
let lastBlock = this.chain[this.chain.length - 1];
let newBlock = new Block(lastBlock.hash, data);
newBlock.mine(2);
this.chain.push(newBlock);
}
isValid_hash() {
for (let i = 1; i {
console.log(`Block ${index}:`, {
data: block.data,
hash: block.hash.substring(0, 10) + "...",
previousHash: block.prev_hashValue.substring(0, 10) + "..."
});
});
Block mined: 00a1b2c3d4e5f6789...
Block mined: 004f5e6d7c8b9a1234...
Blockchain created with 3 blocks
Is blockchain valid? true
Block 0: { data: { isGenesisBlock: true }, hash: 'a7b8c9d0e1...', previousHash: '0...' }
Block 1: { data: { from: 'Joe', to: 'Juhi', amount: 100 }, hash: '00a1b2c3d4...', previousHash: 'a7b8c9d0e1...' }
Block 2: { data: { from: 'Martin', to: 'Genny', amount: 150 }, hash: '004f5e6d7c...', previousHash: '00a1b2c3d4...' }
How Proof of Work Mining Works
The mine() method implements a simple proof-of-work algorithm. It keeps incrementing the pf_work nonce until the hash starts with the required number of zeros (difficulty level). This computational work makes the blockchain secure against tampering.
Running the Code
To run this blockchain implementation:
- Install Node.js
- Install crypto-js:
npm install crypto-js
- Save the code as
blockchain.js
- Run:
node blockchain.js
Blockchain Validation
The isValid_hash() method verifies blockchain integrity by:
- Checking that each block's stored hash matches its calculated hash
- Verifying that each block's previous hash matches the actual previous block's hash
- Ensuring the chain hasn't been tampered with
Conclusion
This JavaScript blockchain implementation demonstrates core concepts like hashing, proof-of-work mining, and chain validation. While basic, it provides a foundation for understanding how blockchain technology ensures data integrity and immutability through cryptographic linking of blocks.
