{"version":3,"file":"revocation.cjs","sources":["../../../src/vc/revocation.js"],"sourcesContent":["import { blake2b } from 'blakejs';\nimport jsonld from 'jsonld';\n\nimport {\n  VB_ACCUMULATOR_22,\n  KB_UNI_ACCUMULATOR_24,\n} from '@docknetwork/crypto-wasm-ts';\nimport { randomAsHex, u8aToHex } from '../utils/types/bytes';\n\nimport {\n  RevRegType,\n  DockRevRegQualifier,\n  expandedStatusProperty,\n  credentialIDField,\n  credentialTypeField,\n} from './constants';\n\n// The revocation registry has id with the byte size `RevRegIdByteSize`\nexport const RevRegIdByteSize = 32;\n// Each entry in revocation registry has byte size `RevEntryByteSize`\nexport const RevEntryByteSize = 32;\n\nconst LD_SEC_TERM = 'https://ld.truvera.io/security#';\nconst LD_SEC_TERM_LEGACY = 'https://ld.dock.io/security#';\n\n/**\n * Return `credentialStatus` according to W3C spec when the revocation status is checked on Dock\n * @param registryId - Revocation registry id\n * @returns {{id: string, type: string}}\n */\nexport function buildDockCredentialStatus(registryId) {\n  return { id: `${DockRevRegQualifier}${registryId}`, type: RevRegType };\n}\n\n/**\n * Generate the revocation id of a credential usable by Dock. It hashes the credential id to get the\n * revocation id\n * @param credential\n * @returns {*}\n */\nexport function getDockRevIdFromCredential(credential) {\n  // The hash outputs the same number of bytes as required by Dock\n  return u8aToHex(blake2b(credential[credentialIDField], RevEntryByteSize * 8));\n}\n\n/**\n * Generate a random revocation registry id.\n * @returns {string} The id as a hex string\n */\nexport function createRandomRegistryId() {\n  return randomAsHex(RevRegIdByteSize);\n}\n\n/**\n * Retrieves a value under the `credentialStatus` property and ensures it has the expected properties.\n * Returns `null` if no value is found.\n * @param expanded\n */\nexport function getCredentialStatus(expanded) {\n  const statusValues = jsonld.getValues(expanded, expandedStatusProperty);\n  if (!statusValues.length) {\n    return null;\n  } else if (statusValues.length > 1) {\n    throw new Error(\n      `\\`statusPurpose\\` must be an array containing up to one item, received: \\`${expanded[expandedStatusProperty]}\\``,\n    );\n  }\n  const [status] = statusValues;\n\n  if (!status[credentialIDField]) {\n    throw new Error('\"credentialStatus\" must include an id.');\n  }\n  if (!status[credentialTypeField]) {\n    throw new Error('\"credentialStatus\" must include a type.');\n  }\n\n  return status;\n}\n\nconst ldTypeGen = (typ) => [\n  typ,\n  `${LD_SEC_TERM}${typ}`,\n  `${LD_SEC_TERM_LEGACY}${typ}`,\n  `/${typ}`,\n];\nconst REV_REG_LD_TYPES = [RevRegType].flatMap(ldTypeGen);\nconst ACCUMULATOR_LD_TYPES = [VB_ACCUMULATOR_22, KB_UNI_ACCUMULATOR_24].flatMap(\n  ldTypeGen,\n);\n\n/**\n * Returns `true` if supplied status is a registry revocation status.\n * @param status\n * @returns {boolean}\n */\nexport const isRegistryRevocationStatus = ({ [credentialTypeField]: type }) => REV_REG_LD_TYPES.some((t) => type.includes(t));\n\n/**\n * Returns `true` if supplied status is a accumulator revocation status.\n * @param status\n * @returns {boolean}\n */\nexport const isAccumulatorRevocationStatus = ({\n  [credentialTypeField]: type,\n}) => ACCUMULATOR_LD_TYPES.some((t) => type.includes(t));\n\n/**\n * Checks if a credential status has a registry revocation.\n * @param expanded\n * @returns {boolean}\n */\nexport function hasRegistryRevocationStatus(expanded) {\n  const status = getCredentialStatus(expanded);\n\n  return !!status && isRegistryRevocationStatus(status);\n}\n\n/**\n * Check if the credential is revoked or not according to the revocation registry mechanism.\n * @param credential\n * @param documentLoader\n * @returns {Promise<{verified: boolean}|{verified: boolean, error: string}>} The returned object will have a key `verified`\n * which is true if the credential is not revoked and false otherwise. The `error` will describe the error if any.\n */\nexport async function checkRevocationRegistryStatus(\n  credential,\n  documentLoader,\n) {\n  const status = getCredentialStatus(credential);\n  const revId = getDockRevIdFromCredential(credential);\n  if (!status) {\n    throw new Error('Missing `credentialStatus`');\n  }\n\n  if (!isRegistryRevocationStatus(status)) {\n    throw new Error(`Expected registry revocation status, got \\`${status}\\``);\n  }\n\n  const regId = status[credentialIDField];\n  const fullId = `${regId}#${revId}`;\n\n  // Hash credential id to get revocation id\n  const { document: revocationStatus } = await documentLoader(fullId);\n  if (revocationStatus) {\n    return { verified: false, error: 'Revocation check failed' };\n  }\n\n  return { verified: true };\n}\n"],"names":["DockRevRegQualifier","RevRegType","u8aToHex","blake2b","credentialIDField","randomAsHex","jsonld","expandedStatusProperty","credentialTypeField","VB_ACCUMULATOR_22","KB_UNI_ACCUMULATOR_24"],"mappings":";;;;;;;;;;;;AAiBA;AACY,MAAC,gBAAgB,GAAG;AAChC;AACY,MAAC,gBAAgB,GAAG;;AAEhC,MAAM,WAAW,GAAG,iCAAiC;AACrD,MAAM,kBAAkB,GAAG,8BAA8B;;AAEzD;AACA;AACA;AACA;AACA;AACO,SAAS,yBAAyB,CAAC,UAAU,EAAE;AACtD,EAAE,OAAO,EAAE,EAAE,EAAE,CAAC,EAAEA,gCAAmB,CAAC,EAAE,UAAU,CAAC,CAAC,EAAE,IAAI,EAAEC,uBAAU,EAAE;AACxE;;AAEA;AACA;AACA;AACA;AACA;AACA;AACO,SAAS,0BAA0B,CAAC,UAAU,EAAE;AACvD;AACA,EAAE,OAAOC,cAAQ,CAACC,eAAO,CAAC,UAAU,CAACC,8BAAiB,CAAC,EAAE,gBAAgB,GAAG,CAAC,CAAC,CAAC;AAC/E;;AAEA;AACA;AACA;AACA;AACO,SAAS,sBAAsB,GAAG;AACzC,EAAE,OAAOC,iBAAW,CAAC,gBAAgB,CAAC;AACtC;;AAEA;AACA;AACA;AACA;AACA;AACO,SAAS,mBAAmB,CAAC,QAAQ,EAAE;AAC9C,EAAE,MAAM,YAAY,GAAGC,uBAAM,CAAC,SAAS,CAAC,QAAQ,EAAEC,mCAAsB,CAAC;AACzE,EAAE,IAAI,CAAC,YAAY,CAAC,MAAM,EAAE;AAC5B,IAAI,OAAO,IAAI;AACf,EAAE,CAAC,MAAM,IAAI,YAAY,CAAC,MAAM,GAAG,CAAC,EAAE;AACtC,IAAI,MAAM,IAAI,KAAK;AACnB,MAAM,CAAC,0EAA0E,EAAE,QAAQ,CAACA,mCAAsB,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