From 5dc6db0b8e18c55b8d065dd22b704389e33bb79d Mon Sep 17 00:00:00 2001 From: garrettmills Date: Sat, 10 Apr 2021 03:51:21 -0500 Subject: [PATCH] Add support for checking for genesis block --- src/app/units/Blockchain.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/app/units/Blockchain.ts b/src/app/units/Blockchain.ts index 53412e1..42c6195 100644 --- a/src/app/units/Blockchain.ts +++ b/src/app/units/Blockchain.ts @@ -29,6 +29,12 @@ export class Block implements BlockResourceItem { this.proof = rec.proof; } + /** Returns true if this is the genesis block. */ + isGenesis() { + // FIXME sign this with GPG to verify that it came from here + return this.uuid === '0000' + } + /** Generate the hash for this block. */ hash() { return crypto.createHash('sha256') @@ -119,8 +125,7 @@ export class Blockchain extends Unit { public async validate(chain: Block[]) { const blocks = collect(chain) return blocks.every((block: Block, idx: number) => { - if ( idx === 0 ) return true; // TODO handle genesis - + if ( idx === 0 ) return block.isGenesis() return block.lastBlockHash === blocks.at(idx)!.hash() }) }