Last active
September 11, 2017 05:30
-
-
Save joeynimu/322c4d120d36c12600c680f25fe74f28 to your computer and use it in GitHub Desktop.
Really simple demonstration of how block chain works.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// you need to first run `npm i --save crypto-js` | |
const SHA256 = require('crypto-js/sha256'); | |
class Block{ | |
constructor(index, data, previsousHash='', date = new Date()){ | |
// takes in the following params; index: number, timestamp: string/object, data: object and previousHash: string | |
this.index = index, | |
this.timestamp = this.formatTimeStamp(), | |
this.data = data, | |
this.previsousHash = this.calculateHash(), | |
this.date = date | |
} | |
calculateHash(){ | |
return SHA256(this.index + this.previousHash + this.timestamp + JSON.stringify(this.data)).toString(); | |
} | |
getTime(){ | |
let date = new Date(); | |
return { | |
hours: date.getHours(), | |
minutes: date.getMinutes(), | |
seconds: date.getSeconds(), | |
milliseconds: date.getMilliseconds() | |
} | |
} | |
formatTimeStamp(){ | |
let date = new Date(); | |
let day = date.getDay(); | |
let month = date.getMonth(); | |
let year = date.getFullYear(); | |
return { | |
day: day, | |
month: month, | |
time: this.getTime(), | |
year: year | |
} | |
} | |
} | |
class BlockChain{ | |
constructor(){ | |
this.chain = [this.createGenesisBlock()] | |
} | |
createGenesisBlock(){ | |
return new Block(0, 'Start 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(){ | |
// loops through all the chains doing some checks | |
for(let i = 1; i < this.chain.length; i++){ | |
let currBlock = this.chain[i]; | |
let prevBlock = this.chain[i - 1]; | |
// it currentHash doesn't match the calculateHash, chain is invalid | |
if(currBlock.hash !== currBlock.calculateHash()){ | |
return false; | |
} | |
// compare previousHash from currentblock doesn't match that from previous block hash chain is invalid | |
if(currBlock.previousHash !== prevBlock.hash){ | |
return false; | |
} | |
} | |
// else chain is valid | |
return true; | |
} | |
} | |
let bitCoin = new BlockChain(); | |
bitCoin.addBlock(new Block(1, {amount: 4})); | |
bitCoin.addBlock(new Block(2, {amount: 10})); | |
console.log('First time validation before inteferance: ' + bitCoin.isChainValid()); | |
bitCoin.chain[1].data = {amount: 10}; | |
bitCoin.chain[1].hash = bitCoin.chain[1].calculateHash(); | |
console.log('After inteferance: ' + bitCoin.isChainValid()); //this returns false | |
// console.log(JSON.stringify(bitCoin, null, 4)); //uncomment this to see the full bitCoin object |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment