Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const SHA256 = require('crypto-js/sha256');
- interface BlockChain {
- blocks: Block[];
- headBlock: Block | null;
- currentBlock: Block | null;
- }
- class BlockChain {
- constructor() {
- this.blocks = [];
- this.headBlock = null;
- this.currentBlock = null;
- }
- acceptBlock(block: Block): void {
- if (!this.headBlock) {
- this.headBlock = block;
- this.headBlock.previousBlockHash = null;
- }
- this.currentBlock = block;
- this.blocks = [...this.blocks, block];
- }
- verifyChain(): void {
- if (!this.headBlock) {
- throw new Error('Genesis block not set.');
- }
- const isValid = this.headBlock.isValidChain(null, true);
- if (isValid) {
- console.log('Blockchain integrity intact.\n');
- } else {
- console.log('Blockchain integrity is NOT intact.\n');
- }
- }
- }
- interface Block {
- blockHash: string;
- claimNumber: string;
- claimType: string;
- settlementAmount: number;
- settlementDate: Date;
- carRegistration: string;
- mileage: number;
- blockNumber: number;
- createdDate: Date;
- previousBlockHash: any;
- parent: any;
- nextBlock: any;
- }
- class Block {
- constructor({
- claimNumber,
- claimType,
- settlementAmount,
- settlementDate,
- carRegistration,
- mileage,
- blockNumber,
- createdDate,
- parent
- }) {
- this.claimNumber = claimNumber;
- this.claimType = claimType;
- this.settlementAmount = settlementAmount;
- this.settlementDate = settlementDate;
- this.carRegistration = carRegistration;
- this.mileage = mileage;
- this.blockNumber = blockNumber;
- this.createdDate = createdDate;
- this.parent = parent;
- this.setBlockHash(this.parent);
- }
- public calculateBlockhash(previousBlockHash: string | null): string {
- const txnHash: string =
- this.claimNumber +
- this.settlementAmount +
- this.carRegistration +
- this.mileage +
- this.claimType;
- const blockHeader: string =
- this.blockNumber + this.createdDate.toString() + previousBlockHash;
- const combined: string = txnHash + blockHeader;
- const returnValue: string = SHA256(combined).toString();
- return returnValue;
- }
- public setBlockHash(parent: Block | null): void {
- if (parent) {
- this.previousBlockHash = parent.blockHash;
- parent.nextBlock = this;
- } else {
- this.previousBlockHash = null;
- }
- this.blockHash = this.calculateBlockhash(this.previousBlockHash);
- }
- public isValidChain(prevBlockHash: string | null, verbose: boolean): boolean {
- let isValid: boolean = true;
- const newBlockHash: string = this.calculateBlockhash(prevBlockHash);
- if (newBlockHash != this.blockHash) {
- isValid = false;
- } else {
- isValid = isValid || this.previousBlockHash == prevBlockHash;
- }
- this.printVerificationMessage(verbose, isValid);
- if (this.nextBlock) {
- return this.nextBlock.isValidChain(newBlockHash, verbose);
- }
- return isValid;
- }
- private printVerificationMessage(verbose: boolean, isValid: boolean): void {
- if (verbose) {
- if (!isValid) {
- console.log(`Block Number ${this.blockNumber}: FAILED VERIFICATION`);
- } else {
- console.log(`Block Number ${this.blockNumber}: PASSED VERIFICATION`);
- }
- }
- }
- }
- const blockChain: any = new BlockChain();
- let blockNumber = 1;
- const block1 = new Block({
- claimNumber: 'ABC123',
- claimType: 'totalLoss',
- settlementAmount: 1000,
- settlementDate: Date.now(),
- carRegistration: 'QWE123',
- mileage: 1000,
- blockNumber: blockNumber++,
- parent: null,
- createdDate: Date.now()
- });
- const block2 = new Block({
- claimNumber: 'DEF456',
- claimType: 'totalLoss',
- settlementAmount: 2000,
- settlementDate: Date.now(),
- carRegistration: 'JKH567',
- mileage: 2000,
- blockNumber: blockNumber++,
- parent: block1,
- createdDate: Date.now()
- });
- const block3 = new Block({
- claimNumber: 'GHI789',
- claimType: 'totalLoss',
- settlementAmount: 3000,
- settlementDate: Date.now(),
- carRegistration: 'DH23ED',
- mileage: 3000,
- blockNumber: blockNumber++,
- parent: block2,
- createdDate: Date.now()
- });
- const block4 = new Block({
- claimNumber: 'JKL101112',
- claimType: 'totalLoss',
- settlementAmount: 4000,
- settlementDate: Date.now(),
- carRegistration: 'DH34K6',
- mileage: 4000,
- blockNumber: blockNumber++,
- parent: block3,
- createdDate: Date.now()
- });
- const block5 = new Block({
- claimNumber: 'MNO131415',
- claimType: 'totalLoss',
- settlementAmount: 5000,
- settlementDate: Date.now(),
- carRegistration: '28FNF4',
- mileage: 5000,
- blockNumber: blockNumber++,
- parent: block4,
- createdDate: Date.now()
- });
- const block6 = new Block({
- claimNumber: 'PQR161718',
- claimType: 'totalLoss',
- settlementAmount: 6000,
- settlementDate: Date.now(),
- carRegistration: 'FJK676',
- mileage: 6000,
- blockNumber: blockNumber++,
- parent: block5,
- createdDate: Date.now()
- });
- const block7 = new Block({
- claimNumber: 'STU192021',
- claimType: 'totalLoss',
- settlementAmount: 7000,
- settlementDate: Date.now(),
- carRegistration: 'LKU234',
- mileage: 7000,
- blockNumber: blockNumber++,
- parent: block6,
- createdDate: Date.now()
- });
- const block8 = new Block({
- claimNumber: 'VWX222324',
- claimType: 'totalLoss',
- settlementAmount: 8000,
- settlementDate: Date.now(),
- carRegistration: 'VBN456',
- mileage: 8000,
- blockNumber: blockNumber++,
- parent: block7,
- createdDate: Date.now()
- });
- blockChain.acceptBlock(block1);
- blockChain.acceptBlock(block2);
- blockChain.acceptBlock(block3);
- blockChain.acceptBlock(block4);
- blockChain.acceptBlock(block5);
- blockChain.acceptBlock(block6);
- blockChain.acceptBlock(block7);
- blockChain.acceptBlock(block8);
- // all good
- blockChain.verifyChain();
- block4.createdDate = new Date();
- // block4 and after are not verified
- blockChain.verifyChain();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement