A simple blockchain implemented with JavaScript
const SHA256 = require("crypto-js/sha256"); | |
class Block { | |
constructor(index, timestamp, data, previousHash = '') { | |
this.index = index; | |
this.previousHash = previousHash; | |
this.timestamp = timestamp; | |
this.data = data; | |
this.hash = this.calculateHash(); | |
this.nonce = 0; | |
} | |
calculateHash() { | |
return SHA256(this.index + this.previousHash + this.timestamp + JSON.stringify(this.data) + this.nonce).toString(); | |
} | |
mineBlock(difficulty) { | |
while (this.hash.substring(0, difficulty) !== Array(difficulty + 1).join("0")) { | |
this.nonce++; | |
this.hash = this.calculateHash(); | |
} | |
} | |
} | |
class Blockchain{ | |
constructor() { | |
this.chain = [this.createGenesisBlock()]; | |
this.difficulty = 5; | |
} | |
createGenesisBlock() { | |
return new Block(0, "05/01/2018", "Genesis Block", "0"); | |
} | |
getLastBlock() { | |
return this.chain[this.chain.length - 1]; | |
} | |
addBlock(newBlock) { | |
newBlock.previousHash = this.getLastBlock().hash; | |
newBlock.mineBlock(this.difficulty); | |
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 FancyCoin = new Blockchain(); | |
console.log('Mining block 1...'); | |
FancyCoin.addBlock(new Block(1, "06/01/2018", {amount: 10})); | |
console.log('Mining block 2...'); | |
FancyCoin.addBlock(new Block(2, "06/01/2018", {amount: 20})); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment