Created
July 20, 2017 13:06
-
-
Save tpgmartin/0b34dae2e8f72acdf9b1d0675e44a388 to your computer and use it in GitHub Desktop.
Simple blockchain implementation in JavaScript
This file contains 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
const crypto = require('crypto') | |
class Block { | |
constructor(index, timestamp, data, previousHash) { | |
this.index = index | |
this.timestamp = timestamp | |
this.data = data | |
this.previousHash = previousHash | |
this.hash = this.hashBlock() | |
} | |
hashBlock() { | |
const sha = crypto.createHash('sha256') | |
sha.update(this.index + this.timestamp + this.data + this.previousHash) | |
return sha.digest('hex') | |
} | |
} | |
// Generate genesis block | |
function createGenesisBlock() { | |
// Manually construct a block with index zero and arbitrary previous hash | |
return new Block(0, Date(), 'Genesis Block', '0') | |
} | |
// Generate all later blocks in the blockchain | |
function nextBlock(lastBlock) { | |
let index = lastBlock.index + 1 | |
let timestamp = Date() | |
let data = `Hey! I'm block ${index}` | |
let hash = lastBlock.hash | |
return new Block(index, timestamp, data, hash) | |
} | |
// Initialise the blockchain with the genesis block | |
const blockchain = [createGenesisBlock()] | |
let previousBlock = blockchain[0] | |
const numBlocksToAdd = 20 | |
// Add blocks to chain and log | |
for (let i = 0; i < numBlocksToAdd; i++) { | |
let blockToAdd = nextBlock(previousBlock) | |
blockchain.push(blockToAdd) | |
previousBlock = blockToAdd | |
console.log(`Block #${blockToAdd.index} has been added to the blockchain!`) | |
console.log(`Hash: ${blockToAdd.hash}\n`) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment