Skip to content

Instantly share code, notes, and snippets.

@tpgmartin
Created July 20, 2017 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tpgmartin/0b34dae2e8f72acdf9b1d0675e44a388 to your computer and use it in GitHub Desktop.
Save tpgmartin/0b34dae2e8f72acdf9b1d0675e44a388 to your computer and use it in GitHub Desktop.
Simple blockchain implementation in JavaScript
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