Skip to content

Instantly share code, notes, and snippets.

@andywenk
Created September 28, 2017 07:47
Show Gist options
  • Save andywenk/6eed838f321038bc668670d38a8f962a to your computer and use it in GitHub Desktop.
Save andywenk/6eed838f321038bc668670d38a8f962a to your computer and use it in GitHub Desktop.
Blockchain in JS
// Example for a minimal Blockchain. Written by Savjee
// https://www.youtube.com/watch?v=zVqczFZr124
// before running, install crypto-js via npm
const SHA256 = require('crypto-js/sha256');
class Block {
constructor(index, timestamp, data, previousHash = '') {
this.index = index;
this.timestamp = timestamp;
this.data = data;
this.previousHash = previousHash;
this.hash = this.calculateHash();
}
calculateHash() {
return SHA256(this.index + this.previousHash + this.timestamp + JSON.stringify(this.data)).toString();
}
}
class Blockchain {
constructor() {
this.chain = [this.creategenesisBlock()];
}
creategenesisBlock() {
return new Block(0, "01/01/2017", "genesis Block", "0");
}
getLatestBlock() {
return this.chain[this.chain.length - 1];
}
addBlock(newBlock) {
newBlock.previousHash = this.getLatestBlock().hash;
newBlock.hash = newBlock.calculateHash();
this.chain.push(newBlock);
}
isChainValid(){
for(let i = 1; i < this.chain.length; i++) {
const currentBlock = this.chain[i];
const previousBlock = this.chain[i - 1];
if(currentBlock.hash !== currentBlock.calculateHash()) {
return false;
}
if(currentBlock.previousHash !== previousBlock.hash) {
return false;
}
}
return true;
}
}
let andyCoin = new Blockchain();
andyCoin.addBlock(new Block(1, "29/08/2017", { amount: 4 }));
andyCoin.addBlock(new Block(2, "10/09/2017", { amount: 29 }));
andyCoin.addBlock(new Block(3, "11/09/2017", { amount: 6 }));
console.log('Our Blockchain:');
console.log(JSON.stringify(andyCoin, null, 4));
console.log('---');
console.log('Is blockchain valid? ' + andyCoin.isChainValid());
console.log('---');
console.log('Changing the data of block 1');
andyCoin.chain[1].data = { amount: 30 };
console.log('Is blockchain valid? ' + andyCoin.isChainValid());
console.log('---');
console.log('Recalculating the hash of block 1');
andyCoin.chain[1].hash = andyCoin.chain[1].calculateHash();
console.log('Is blockchain valid? ' + andyCoin.isChainValid());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment