Creating Sample Blockchain using NodeJS
//DemoBlockchain.js
const SHA256=require('crypto-js/sha256');
class Block
{
constructor(index,data)
{
this.index=index;
this.data=data;
this.timestamp=Date.now();
this.pHash='';
this.hash=this.hashing();
}
hashing()
{
return SHA256(this.index+this.timestamp+this.pHash+JSON.stringify(this.data)).toString();
}
}
class Blockchain
{
constructor()
{
this.chain=[this.genesicBlock()];
this.nextIndex=1;
}
genesicBlock()
{
return new Block(0,"Genesis Block");
}
getLatestBlock()
{
return this.chain[this.chain.length-1];
}
insertBlock(newBlock)
{
newBlock.pHash=this.getLatestBlock().hash;
newBlock.hash=newBlock.hashing();
this.chain.push(newBlock);
this.nextIndex++;
}
getIndex()
{
return this.nextIndex;
}
isTemperedWith()
{
for(let i=1;i<this.chain.lenght;i++)
{
const currentBlock=this.chain[i];
const previousBlock=this.chain[i-1];
if(currentBlock.hash!=currentBlock.hashing() || currentBlock.pHash !=previousBlock.hash)
return true;
}
return false;
}
}
let coin=new Blockchain();
coin.insertBlock(new Block(coin.getIndex(),{Amount:10}));
coin.insertBlock(new Block(coin.getIndex(),{Amount:45}));
coin.insertBlock(new Block(coin.getIndex(),{Amount:354}));
console.log(JSON.stringify(coin,null,2));
console.log('\nBlockchain : '+coin.isTemperedWith());
coin.chain[1].data={Data:1000};
coin.chain[1].hash=coin.chain[1].hashing();
console.log('Blockchain hacked '+coin.isTemperedWith());
// Dependency
// "crypto-js": "^3.1.9-1"
// How to run a blockchain
// npm init
// node DemoBlockchain.js
const SHA256=require('crypto-js/sha256');
class Block
{
constructor(index,data)
{
this.index=index;
this.data=data;
this.timestamp=Date.now();
this.pHash='';
this.hash=this.hashing();
}
hashing()
{
return SHA256(this.index+this.timestamp+this.pHash+JSON.stringify(this.data)).toString();
}
}
class Blockchain
{
constructor()
{
this.chain=[this.genesicBlock()];
this.nextIndex=1;
}
genesicBlock()
{
return new Block(0,"Genesis Block");
}
getLatestBlock()
{
return this.chain[this.chain.length-1];
}
insertBlock(newBlock)
{
newBlock.pHash=this.getLatestBlock().hash;
newBlock.hash=newBlock.hashing();
this.chain.push(newBlock);
this.nextIndex++;
}
getIndex()
{
return this.nextIndex;
}
isTemperedWith()
{
for(let i=1;i<this.chain.lenght;i++)
{
const currentBlock=this.chain[i];
const previousBlock=this.chain[i-1];
if(currentBlock.hash!=currentBlock.hashing() || currentBlock.pHash !=previousBlock.hash)
return true;
}
return false;
}
}
let coin=new Blockchain();
coin.insertBlock(new Block(coin.getIndex(),{Amount:10}));
coin.insertBlock(new Block(coin.getIndex(),{Amount:45}));
coin.insertBlock(new Block(coin.getIndex(),{Amount:354}));
console.log(JSON.stringify(coin,null,2));
console.log('\nBlockchain : '+coin.isTemperedWith());
coin.chain[1].data={Data:1000};
coin.chain[1].hash=coin.chain[1].hashing();
console.log('Blockchain hacked '+coin.isTemperedWith());
// Dependency
// "crypto-js": "^3.1.9-1"
// How to run a blockchain
// npm init
// node DemoBlockchain.js
Comments
Post a Comment