All files / src/parsers/helpers retrieveBlockcertsVersion.ts

100% Statements 24/24
100% Branches 5/5
100% Functions 4/4
100% Lines 20/20

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            1637x       337x       149x                   5x     149x           14x         682x   135x 405x         5x 5x   29x 29x     1x 1x     4x 4x   96x 96x     135x          
import { versionParserMap } from '../index';
import type { JsonLDContext } from '../../models/Blockcerts';
import { isString } from '../../helpers/string';
import Versions from '../../constants/certificateVersions';
 
function lookupVersion (array: string[], v: string): boolean {
  return array.some(str => str.includes(`v${v}`) || str.includes(`${v}.`));
}
 
function filterBlockcertsContext (contextList: string[]): string {
  return contextList.find((ctx: string) => ctx.toLowerCase().indexOf('blockcerts') > 0);
}
 
function filterStringContexts (context: JsonLDContext): string[] {
  return context.filter(isString);
}
 
export interface BlockcertsVersion {
  versionNumber: number;
  version: Versions;
}
 
export function retrieveBlockcertsVersion (context: JsonLDContext | string): BlockcertsVersion {
  if (typeof context === 'string') {
    context = [context];
  }
 
  const blockcertsContext: string = filterBlockcertsContext(filterStringContexts(context));
  if (!blockcertsContext) {
    // TODO: with that we are introducing verification support for certs that are not exactly Blockcerts documents
    // TODO: i.e.: revocation list in Revocation Status List 2021, essentially allowing verification of documents
    // TODO: signed with supported verification suites. We need to improve what's being returned here and verify the
    // TODO: vc compliance as well as return proper content
    return {
      version: Versions.V3_0,
      versionNumber: 3
    };
  }
  const blockcertsContextArray: string[] = blockcertsContext.split('/').filter(str => str !== '');
 
  const availableVersions: string[] = Object.keys(versionParserMap);
  const versionNumber = parseInt(availableVersions.filter(version => lookupVersion(blockcertsContextArray, version.toString()))[0], 10);
 
  let version: Versions;
  switch (versionNumber) {
    case 1:
      version = Versions.V1_2;
      break;
    case 2:
      version = Versions.V2_0;
      break;
    case 3:
      if (blockcertsContext.includes('-alpha')) {
        version = Versions.V3_0_alpha;
        break;
      }
      if (blockcertsContext.includes('-beta')) {
        version = Versions.V3_0_beta;
        break;
      }
      version = Versions.V3_0;
      break;
  }
 
  return {
    versionNumber,
    version
  };
}