All files / src/inspectors ensureValidIssuingKey.ts

100% Statements 11/11
100% Branches 6/6
100% Functions 0/0
100% Lines 11/11

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            39x       38x       39x       39x 39x 39x     2x     2x     1x     1x       6x            
import { dateToUnixTimestamp } from '../helpers/date';
import VerifierError from '../models/verifierError';
import { getText } from '../domain/i18n/useCases';
import type { IssuerPublicKeyList, ParsedKeyObjectV2 } from '../models/Issuer';
 
function getCaseInsensitiveKey (obj: IssuerPublicKeyList, value: string): ParsedKeyObjectV2 {
  let key = null;
  for (const prop in obj) {
    if (Object.prototype.hasOwnProperty.call(obj, prop)) {
      if (prop.toLowerCase() === value.toLowerCase()) {
        key = prop;
      }
    }
  }
  return obj[key];
}
 
export default function ensureValidIssuingKey (keyMap: IssuerPublicKeyList, txIssuingAddress: string, txTime: Date | string): void {
  let errorMessage: string = '';
  const theKey: ParsedKeyObjectV2 = getCaseInsensitiveKey(keyMap, txIssuingAddress);
  const txTimeToUnixTimestamp = dateToUnixTimestamp(txTime);
  if (theKey) {
    if (theKey.created && txTimeToUnixTimestamp <= theKey.created) {
      errorMessage = 'invalidIssuingAddressCreationTime';
    }
    if (theKey.revoked && txTimeToUnixTimestamp >= theKey.revoked) {
      errorMessage = 'invalidIssuingAddressRevoked';
    }
    if (theKey.expires && txTimeToUnixTimestamp >= theKey.expires) {
      errorMessage = 'invalidIssuingAddressExpired';
    }
  } else {
    errorMessage = 'invalidIssuingAddressUnknown';
  }
 
  if (errorMessage) {
    throw new VerifierError(
      'checkAuthenticity',
      getText('errors', errorMessage)
    );
  }
}