Skip to content

Instantly share code, notes, and snippets.

@spenserhuang
Last active December 28, 2017 06:35
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 spenserhuang/057b1c4c19dd3d8baf20465bafb7aeb9 to your computer and use it in GitHub Desktop.
Save spenserhuang/057b1c4c19dd3d8baf20465bafb7aeb9 to your computer and use it in GitHub Desktop.
Blockchain Object
class Blockchain{
constructor() {
this.chain = [this.createGenesis()];
}
createGenesis() {
return new Block(0, "01/01/2017", "Genesis block", "0")
}
latestBlock() {
return this.chain[this.chain.length - 1]
}
addBlock(newBlock){
newBlock.previousHash = this.latestBlock().hash;
newBlock.hash = newBlock.calculateHash();
this.chain.push(newBlock);
}
checkValid() {
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment