Skip to content

Commit

Permalink
Chain Validation | Code
Browse files Browse the repository at this point in the history
  • Loading branch information
15Dkatz committed Sep 27, 2022
1 parent f99b2fb commit 80db7be
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions blockchain.js
@@ -1,4 +1,5 @@
const Block = require('./block');
const cryptoHash = require('./crypto-hash');

class Blockchain {
constructor() {
Expand All @@ -13,6 +14,24 @@ class Blockchain {

this.chain.push(newBlock);
}

static isValidChain(chain) {
if(JSON.stringify(chain[0]) !== JSON.stringify(Block.genesis())) return false;

for (let i=1; i<chain.length; i++) {
const { timestamp, lastHash, hash, data } = chain[i];

const actualLastHash = chain[i-1].hash;

if (lastHash !== actualLastHash) return false;

const validatedHash = cryptoHash(timestamp, lastHash, data);

if (hash !== validatedHash) return false;
}

return true;
}
}

module.exports = Blockchain;

0 comments on commit 80db7be

Please sign in to comment.