Advertisement
CLooker

Untitled

Jun 21st, 2018
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const SHA256 = require('crypto-js/sha256');
  2.  
  3. interface BlockChain {
  4.   blocks: Block[];
  5.   headBlock: Block | null;
  6.   currentBlock: Block | null;
  7. }
  8.  
  9. class BlockChain {
  10.   constructor() {
  11.     this.blocks = [];
  12.     this.headBlock = null;
  13.     this.currentBlock = null;
  14.   }
  15.  
  16.   acceptBlock(block: Block): void {
  17.     if (!this.headBlock) {
  18.       this.headBlock = block;
  19.       this.headBlock.previousBlockHash = null;
  20.     }
  21.  
  22.     this.currentBlock = block;
  23.     this.blocks = [...this.blocks, block];
  24.   }
  25.  
  26.   verifyChain(): void {
  27.     if (!this.headBlock) {
  28.       throw new Error('Genesis block not set.');
  29.     }
  30.  
  31.     const isValid = this.headBlock.isValidChain(null, true);
  32.  
  33.     if (isValid) {
  34.       console.log('Blockchain integrity intact.\n');
  35.     } else {
  36.       console.log('Blockchain integrity is NOT intact.\n');
  37.     }
  38.   }
  39. }
  40.  
  41. interface Block {
  42.   blockHash: string;
  43.   claimNumber: string;
  44.   claimType: string;
  45.   settlementAmount: number;
  46.   settlementDate: Date;
  47.   carRegistration: string;
  48.   mileage: number;
  49.   blockNumber: number;
  50.   createdDate: Date;
  51.   previousBlockHash: any;
  52.   parent: any;
  53.   nextBlock: any;
  54. }
  55.  
  56. class Block {
  57.   constructor({
  58.     claimNumber,
  59.     claimType,
  60.     settlementAmount,
  61.     settlementDate,
  62.     carRegistration,
  63.     mileage,
  64.     blockNumber,
  65.     createdDate,
  66.     parent
  67.   }) {
  68.     this.claimNumber = claimNumber;
  69.     this.claimType = claimType;
  70.     this.settlementAmount = settlementAmount;
  71.     this.settlementDate = settlementDate;
  72.     this.carRegistration = carRegistration;
  73.     this.mileage = mileage;
  74.     this.blockNumber = blockNumber;
  75.     this.createdDate = createdDate;
  76.     this.parent = parent;
  77.     this.setBlockHash(this.parent);
  78.   }
  79.  
  80.   public calculateBlockhash(previousBlockHash: string | null): string {
  81.     const txnHash: string =
  82.       this.claimNumber +
  83.       this.settlementAmount +
  84.       this.carRegistration +
  85.       this.mileage +
  86.       this.claimType;
  87.     const blockHeader: string =
  88.       this.blockNumber + this.createdDate.toString() + previousBlockHash;
  89.     const combined: string = txnHash + blockHeader;
  90.  
  91.     const returnValue: string = SHA256(combined).toString();
  92.  
  93.     return returnValue;
  94.   }
  95.  
  96.   public setBlockHash(parent: Block | null): void {
  97.     if (parent) {
  98.       this.previousBlockHash = parent.blockHash;
  99.       parent.nextBlock = this;
  100.     } else {
  101.       this.previousBlockHash = null;
  102.     }
  103.  
  104.     this.blockHash = this.calculateBlockhash(this.previousBlockHash);
  105.   }
  106.  
  107.   public isValidChain(prevBlockHash: string | null, verbose: boolean): boolean {
  108.     let isValid: boolean = true;
  109.  
  110.     const newBlockHash: string = this.calculateBlockhash(prevBlockHash);
  111.     if (newBlockHash != this.blockHash) {
  112.       isValid = false;
  113.     } else {
  114.       isValid = isValid || this.previousBlockHash == prevBlockHash;
  115.     }
  116.  
  117.     this.printVerificationMessage(verbose, isValid);
  118.  
  119.     if (this.nextBlock) {
  120.       return this.nextBlock.isValidChain(newBlockHash, verbose);
  121.     }
  122.  
  123.     return isValid;
  124.   }
  125.  
  126.   private printVerificationMessage(verbose: boolean, isValid: boolean): void {
  127.     if (verbose) {
  128.       if (!isValid) {
  129.         console.log(`Block Number ${this.blockNumber}: FAILED VERIFICATION`);
  130.       } else {
  131.         console.log(`Block Number ${this.blockNumber}: PASSED VERIFICATION`);
  132.       }
  133.     }
  134.   }
  135. }
  136.  
  137. const blockChain: any = new BlockChain();
  138.  
  139. let blockNumber = 1;
  140.  
  141. const block1 = new Block({
  142.   claimNumber: 'ABC123',
  143.   claimType: 'totalLoss',
  144.   settlementAmount: 1000,
  145.   settlementDate: Date.now(),
  146.   carRegistration: 'QWE123',
  147.   mileage: 1000,
  148.   blockNumber: blockNumber++,
  149.   parent: null,
  150.   createdDate: Date.now()
  151. });
  152.  
  153. const block2 = new Block({
  154.   claimNumber: 'DEF456',
  155.   claimType: 'totalLoss',
  156.   settlementAmount: 2000,
  157.   settlementDate: Date.now(),
  158.   carRegistration: 'JKH567',
  159.   mileage: 2000,
  160.   blockNumber: blockNumber++,
  161.   parent: block1,
  162.   createdDate: Date.now()
  163. });
  164.  
  165. const block3 = new Block({
  166.   claimNumber: 'GHI789',
  167.   claimType: 'totalLoss',
  168.   settlementAmount: 3000,
  169.   settlementDate: Date.now(),
  170.   carRegistration: 'DH23ED',
  171.   mileage: 3000,
  172.   blockNumber: blockNumber++,
  173.   parent: block2,
  174.   createdDate: Date.now()
  175. });
  176.  
  177. const block4 = new Block({
  178.   claimNumber: 'JKL101112',
  179.   claimType: 'totalLoss',
  180.   settlementAmount: 4000,
  181.   settlementDate: Date.now(),
  182.   carRegistration: 'DH34K6',
  183.   mileage: 4000,
  184.   blockNumber: blockNumber++,
  185.   parent: block3,
  186.   createdDate: Date.now()
  187. });
  188.  
  189. const block5 = new Block({
  190.   claimNumber: 'MNO131415',
  191.   claimType: 'totalLoss',
  192.   settlementAmount: 5000,
  193.   settlementDate: Date.now(),
  194.   carRegistration: '28FNF4',
  195.   mileage: 5000,
  196.   blockNumber: blockNumber++,
  197.   parent: block4,
  198.   createdDate: Date.now()
  199. });
  200.  
  201. const block6 = new Block({
  202.   claimNumber: 'PQR161718',
  203.   claimType: 'totalLoss',
  204.   settlementAmount: 6000,
  205.   settlementDate: Date.now(),
  206.   carRegistration: 'FJK676',
  207.   mileage: 6000,
  208.   blockNumber: blockNumber++,
  209.   parent: block5,
  210.   createdDate: Date.now()
  211. });
  212.  
  213. const block7 = new Block({
  214.   claimNumber: 'STU192021',
  215.   claimType: 'totalLoss',
  216.   settlementAmount: 7000,
  217.   settlementDate: Date.now(),
  218.   carRegistration: 'LKU234',
  219.   mileage: 7000,
  220.   blockNumber: blockNumber++,
  221.   parent: block6,
  222.   createdDate: Date.now()
  223. });
  224.  
  225. const block8 = new Block({
  226.   claimNumber: 'VWX222324',
  227.   claimType: 'totalLoss',
  228.   settlementAmount: 8000,
  229.   settlementDate: Date.now(),
  230.   carRegistration: 'VBN456',
  231.   mileage: 8000,
  232.   blockNumber: blockNumber++,
  233.   parent: block7,
  234.   createdDate: Date.now()
  235. });
  236.  
  237. blockChain.acceptBlock(block1);
  238. blockChain.acceptBlock(block2);
  239. blockChain.acceptBlock(block3);
  240. blockChain.acceptBlock(block4);
  241. blockChain.acceptBlock(block5);
  242. blockChain.acceptBlock(block6);
  243. blockChain.acceptBlock(block7);
  244. blockChain.acceptBlock(block8);
  245.  
  246. // all good
  247. blockChain.verifyChain();
  248.  
  249. block4.createdDate = new Date();
  250.  
  251. // block4 and after are not verified
  252. blockChain.verifyChain();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement