Skip to content

Instantly share code, notes, and snippets.

@ssaurel
Created December 18, 2019 14:23
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 ssaurel/c56374cffb7cfd75b5e7674473d7346e to your computer and use it in GitHub Desktop.
Save ssaurel/c56374cffb7cfd75b5e7674473d7346e to your computer and use it in GitHub Desktop.
Blockchain for the tutorial on the SSaurel's Blog
class Blockchain {
constructor(difficulty) {
this.difficulty = difficulty;
this.blocks = [];
// Add Genesis Block
var genesisBlock = new Block(0, null, Date.now(), "Genesis block");
genesisBlock.mineBlock(this.difficulty);
this.blocks.push(genesisBlock);
}
}
Blockchain.prototype.newBlock = function (data) {
var latestBlock = this.blocks[this.blocks.length - 1];
return new Block(latestBlock.index + 1, latestBlock.hash,
Date.now(), data);
}
Blockchain.prototype.addBlock = function(block) {
block.mineBlock(this.difficulty);
this.blocks.push(block);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment