All files / src/suites MerkleProof2017.ts

91.04% Statements 61/67
100% Branches 2/2
100% Functions 12/12
91.04% Lines 61/67

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229                            23x 23x 23x 23x 23x 23x 23x 23x 23x       40x                                         40x     40x   40x   40x 40x 40x 40x 40x 40x 40x 40x 40x                     190x             40x 305x         40x         2x           20x       22x       22x 22x       44x       22x       77x       66x         66x       22x 22x       22x 22x                     3x 3x 3x 3x 3x                 23x   23x           25x   25x           23x   23x                   25x   25x           23x   21x           25x   21x           23x   18x           23x   15x          
import * as inspectors from '../inspectors';
import domain from '../domain';
import { removeEntry } from '../helpers/array';
import { Suite } from '../models/Suite';
import type { Blockcerts } from '../models/Blockcerts';
import type { ExplorerAPI, TransactionData, IBlockchainObject } from '@blockcerts/explorer-lookup';
import type { Receipt } from '../models/Receipt';
import type { Issuer, IssuerPublicKeyList } from '../models/Issuer';
import type { BlockcertsV2 } from '../models/BlockcertsV2';
import type VerificationSubstep from '../domain/verifier/valueObjects/VerificationSubstep';
import type { SuiteAPI } from '../models/Suite';
import type { MerkleProof2017 as TMerkleProof2017 } from '../models/MerkleProof2017';
import type { ITransactionLink } from '../domain/certificates/useCases/getTransactionLink';
 
enum SUB_STEPS {
  getTransactionId = 'getTransactionId',
  computeLocalHash = 'computeLocalHash',
  fetchRemoteHash = 'fetchRemoteHash',
  parseIssuerKeys = 'parseIssuerKeys',
  compareHashes = 'compareHashes',
  checkMerkleRoot = 'checkMerkleRoot',
  checkReceipt = 'checkReceipt',
  checkAuthenticity = 'checkAuthenticity'
}
 
export default class MerkleProof2017 extends Suite {
  public verificationProcess = [
    SUB_STEPS.getTransactionId,
    SUB_STEPS.computeLocalHash,
    SUB_STEPS.fetchRemoteHash,
    SUB_STEPS.compareHashes,
    SUB_STEPS.checkMerkleRoot,
    SUB_STEPS.checkReceipt,
    SUB_STEPS.parseIssuerKeys,
    SUB_STEPS.checkAuthenticity
  ];
 
  public transactionId: string;
  public localHash: string;
  public documentToVerify: Blockcerts;
  public txData: TransactionData;
  public chain: IBlockchainObject;
  public explorerAPIs: ExplorerAPI[];
  public receipt: Receipt;
  public issuerPublicKeyList: IssuerPublicKeyList;
  public issuer: Issuer;
  public proof: TMerkleProof2017;
  public type = 'MerkleProof2017';
 
  constructor (props: SuiteAPI) {
    super(props);
    if (props.executeStep) {
      this.executeStep = props.executeStep;
    }
    this.documentToVerify = props.document;
    this.explorerAPIs = props.explorerAPIs;
    this.issuer = props.issuer;
    this.proof = props.proof as TMerkleProof2017;
    this.validateProofType();
    this.receipt = (this.documentToVerify as BlockcertsV2).signature;
    this.chain = domain.certificates.getChain('', this.receipt);
    this.transactionId = domain.certificates.getTransactionId(this.receipt);
    this.adaptVerificationProcessToChain();
  }
 
  async init (): Promise<void> {}
 
  async verifyProof (): Promise<void> {
    for (const verificationStep of this.verificationProcess) {
      if (!this[verificationStep]) {
        console.error('verification logic for', verificationStep, 'not implemented');
        return;
      }
      await this[verificationStep]();
    }
  }
 
  async verifyIdentity (): Promise<void> {}
 
  getProofVerificationSteps (parentStepKey): VerificationSubstep[] {
    return this.verificationProcess.map(childStepKey =>
      domain.verifier.convertToVerificationSubsteps(parentStepKey, childStepKey)
    );
  }
 
  getIdentityVerificationSteps (): VerificationSubstep[] {
    return [];
  }
 
  getIssuerPublicKey (): string {
    if (domain.chains.isMockChain(this.chain)) {
      return 'This mock chain does not support issuing addresses';
    }
    if (!this.txData) {
      console.error('Trying to access issuing address when txData not available yet. Did you run the `verify` method yet?');
      return;
    }
    return this.txData.issuingAddress;
  }
 
  getIssuerName (): string {
    return this.issuer.name;
  }
 
  getIssuerProfileDomain (): string {
    const issuerProfileUrl = new URL(this.getIssuerProfileUrl());
    return issuerProfileUrl?.hostname;
  }
 
  getIssuerProfileUrl (): string {
    return this.issuer.id;
  }
 
  getSigningDate (): string {
    return (this.documentToVerify as BlockcertsV2).issuedOn;
  }
 
  getChain (): IBlockchainObject {
    return this.chain;
  }
 
  getReceipt (): Receipt {
    return this.receipt;
  }
 
  // TODO: rename inspector method to make this function `getTransactionId`
  getTransactionIdString (): string {
    return domain.certificates.getTransactionId(this.getReceipt());
  }
 
  getTransactionLink (): string {
    const transactionLinks: ITransactionLink = domain.certificates.getTransactionLink(this.getTransactionIdString(), this.getChain());
    return transactionLinks.transactionLink;
  }
 
  getRawTransactionLink (): string {
    const transactionLinks: ITransactionLink = domain.certificates.getTransactionLink(this.getTransactionIdString(), this.getChain());
    return transactionLinks.rawTransactionLink;
  }
 
  private validateProofType (): void {
    if (this.proof.type[0] !== this.type) {
      throw new Error(`Incompatible proof type passed. Expected: ${this.type}, Got: ${this.proof.type[0]}`);
    }
  }
 
  private adaptVerificationProcessToChain (): void {
    if (domain.chains.isMockChain(this.chain)) {
      removeEntry(this.verificationProcess, SUB_STEPS.getTransactionId);
      removeEntry(this.verificationProcess, SUB_STEPS.fetchRemoteHash);
      removeEntry(this.verificationProcess, SUB_STEPS.parseIssuerKeys);
      removeEntry(this.verificationProcess, SUB_STEPS.checkMerkleRoot);
      removeEntry(this.verificationProcess, SUB_STEPS.checkAuthenticity);
    }
  }
 
  async executeStep (step: SUB_STEPS, action, verificationSuite: string): Promise<any> {
    throw new Error('doAction method needs to be overwritten by injecting from CVJS');
  }
 
  private async getTransactionId (): Promise<void> {
    await this.executeStep(
      SUB_STEPS.getTransactionId,
      () => inspectors.isTransactionIdValid(this.transactionId),
      this.type
    );
  }
 
  private async computeLocalHash (): Promise<void> {
    this.localHash = await this.executeStep(
      SUB_STEPS.computeLocalHash,
      async () => await inspectors.computeLocalHash(this.documentToVerify),
      this.type
    );
  }
 
  private async fetchRemoteHash (): Promise<void> {
    this.txData = await this.executeStep(
      SUB_STEPS.fetchRemoteHash,
      async () => await domain.verifier.lookForTx({
        transactionId: this.transactionId,
        chain: this.chain.code,
        explorerAPIs: this.explorerAPIs
      }),
      this.type
    );
  }
 
  private async compareHashes (): Promise<void> {
    await this.executeStep(
      SUB_STEPS.compareHashes,
      () => inspectors.ensureHashesEqual(this.localHash, this.receipt.targetHash),
      this.type
    );
  }
 
  private async checkMerkleRoot (): Promise<void> {
    await this.executeStep(
      SUB_STEPS.checkMerkleRoot,
      () => inspectors.ensureMerkleRootEqual(this.receipt.merkleRoot, this.txData.remoteHash),
      this.type
    );
  }
 
  private async checkReceipt (): Promise<void> {
    await this.executeStep(
      SUB_STEPS.checkReceipt,
      () => { inspectors.ensureValidReceipt(this.receipt); },
      this.type
    );
  }
 
  private async parseIssuerKeys (): Promise<void> {
    this.issuerPublicKeyList = await this.executeStep(
      SUB_STEPS.parseIssuerKeys,
      () => domain.verifier.parseIssuerKeys(this.issuer),
      this.type
    );
  }
 
  private async checkAuthenticity (): Promise<void> {
    await this.executeStep(
      SUB_STEPS.checkAuthenticity,
      () => { inspectors.ensureValidIssuingKey(this.issuerPublicKeyList, this.txData.issuingAddress, this.txData.time); },
      this.type
    );
  }
}