-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: util functions to static methods
- Loading branch information
Showing
5 changed files
with
89 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,41 @@ | ||
import { calculateBlockHash, createHash } from "./util/crypto"; | ||
import crypto = require("crypto"); | ||
|
||
export interface BlockParams { | ||
index: number; | ||
previousHash: string; | ||
data: string; | ||
} | ||
export class Block<T> { | ||
public static calculateHash(block: Block<any>): string { | ||
const json: string = JSON.stringify(block.data); | ||
const text: string = `${block.index}${block.predecessor}${block.time}${json}`; | ||
return crypto | ||
.createHash("sha256") | ||
.update(text, "utf8") | ||
.digest("hex"); | ||
} | ||
|
||
public static validate(block: Block<any>, predecessor: Block<any>): boolean { | ||
return ( | ||
predecessor.index + 1 === block.index && | ||
predecessor.hash === block.predecessor && | ||
Block.calculateHash(block) === block.hash | ||
); | ||
} | ||
|
||
export class Block { | ||
public index: number; | ||
public previousHash: string; | ||
public timestamp: number; | ||
public data: string; | ||
public hash?: string; | ||
public predecessor: string; | ||
public time: number; | ||
public data: T | string; | ||
public hash: string; | ||
|
||
constructor(predecessor: Block<T>, data: T | string) { | ||
if (predecessor) { | ||
this.index = predecessor.index + 1; | ||
this.predecessor = predecessor.hash; | ||
} else { | ||
this.index = 0; | ||
this.predecessor = null; | ||
} | ||
|
||
constructor({ index, previousHash, data }: BlockParams) { | ||
this.index = index; | ||
this.previousHash = previousHash; | ||
this.data = data; | ||
|
||
this.timestamp = Date.now(); | ||
this.hash = calculateBlockHash(this); | ||
this.time = Date.now(); | ||
this.hash = Block.calculateHash(this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,57 @@ | ||
import { Block, BlockParams } from "./block.model"; | ||
import { isValidChain } from "./util/blockchain"; | ||
import { calculateBlockHash } from "./util/crypto"; | ||
import { Block } from "./block.model"; | ||
|
||
const GENESIS_BLOCK_PARAMS: BlockParams = { | ||
data: "GENESIS", | ||
index: 0, | ||
previousHash: null | ||
}; | ||
export class Blockchain<T> { | ||
public static validate(genesis: Block<any>, chain: Block<any>[]): boolean { | ||
if (Block.calculateHash(genesis) !== chain[0].hash) { | ||
return false; | ||
} | ||
|
||
return chain.reduce((valid, block) => { | ||
if (block.index === 0) { | ||
return true; | ||
} | ||
return Block.validate(block, chain[block.index - 1]) || valid; | ||
}, true); | ||
} | ||
|
||
export class Blockchain { | ||
private chain: Block[]; | ||
private chain: Block<T>[]; | ||
|
||
constructor(chain?: Block[]) { | ||
constructor(chain?: Block<T>[]) { | ||
if (chain) { | ||
this.chain = chain; | ||
} else { | ||
const genesisBlock: Block = new Block(GENESIS_BLOCK_PARAMS); | ||
const genesisBlock: Block<T> = new Block<T>(null, "GENESIS"); | ||
this.chain = [genesisBlock]; | ||
} | ||
} | ||
|
||
public getChain(): Block[] { | ||
public getChain(): Block<T>[] { | ||
return this.chain; | ||
} | ||
|
||
public getGenesisBlock(): Block { | ||
public getGenesisBlock(): Block<T> { | ||
return this.chain[0]; | ||
} | ||
|
||
public getLatestBlock(): Block { | ||
public getLatestBlock(): Block<T> { | ||
return this.chain[this.chain.length - 1]; | ||
} | ||
|
||
public generateNextBlock(data: string): Block { | ||
const previousBlock: Block = this.getLatestBlock(); | ||
|
||
const nextBlock: Block = new Block({ | ||
data, | ||
index: previousBlock.index + 1, | ||
previousHash: previousBlock.hash | ||
}); | ||
|
||
public add(data: T): Block<T> { | ||
const predecessor: Block<T> = this.getLatestBlock(); | ||
const nextBlock: Block<T> = new Block<T>(predecessor, data); | ||
this.chain.push(nextBlock); | ||
|
||
return nextBlock; | ||
} | ||
|
||
public replaceChain(newBlocks: Block[]): void { | ||
if (isValidChain(this.getGenesisBlock(), newBlocks) && newBlocks.length > this.chain.length) { | ||
public replaceChain(newBlocks: Block<T>[]): void { | ||
const isValid: boolean = Blockchain.validate(this.getGenesisBlock(), newBlocks); | ||
if (isValid && newBlocks.length > this.chain.length) { | ||
this.chain = newBlocks; | ||
} | ||
} | ||
|
||
public toJSON(): string { | ||
return JSON.stringify(this.chain, null, 2); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.