{"version":3,"file":"status-list2021-credential.cjs","sources":["../../../src/vc/status-list2021-credential.js"],"sourcesContent":["import {\n  decodeList,\n  createList,\n  createCredential,\n  StatusList, // eslint-disable-line\n} from '@digitalcredentials/vc-status-list';\nimport { gzip, ungzip } from 'pako';\nimport { u8aToU8a } from '../utils/types/bytes';\nimport VerifiableCredential from './verifiable-credential';\nimport { ensureStatusListId } from '../utils';\n\n/**\n * Status list 2021 verifiable credential as per https://www.w3.org/TR/vc-status-list/#statuslist2021credential.\n */\nexport default class StatusList2021Credential extends VerifiableCredential {\n  static Type = 'statusList2021';\n\n  /**\n   * Create a new Status List 2021 Verifiable Credential instance.\n   * @param {string} id - id of the credential\n   */\n  constructor(id) {\n    super(id);\n\n    // Caches decoded status list.\n    Object.defineProperty(this, 'internalCachedStatusList', {\n      value: { encoded: void 0, decoded: void 0 },\n      writable: true,\n    });\n  }\n\n  /**\n   * Fail if the given verifiable credential id isn't a valid `StatusList2021Credential` id.\n   * @param {*} id\n   */\n  static verifyID(id) {\n    ensureStatusListId(id);\n  }\n\n  /**\n   * Creates new `StatusList2021Credential` with supplied `id` and option `statusPurpose` = `revocation` by default,\n   * `length` and `revokeIndices`. Note that credential with `statusPurpose` = `revocation` can't unsuspend its indices.\n   * To allow unrevoking indices in the future, use `statusPurpose` = `suspension`.\n   * The proof will be generated immediately using supplied `keyDoc`.\n   *\n   * @param {KeyDoc} keyDoc\n   * @param {string} id - on-chain hex identifier for the `StatusList2021Credential`.\n   * @param {object} [params={}]\n   * @param {'revocation'|'suspension'} [params.statusPurpose=revocation] - `statusPurpose` of the `StatusList2021Credential`.\n   * Can be either `revocation` or `suspension`.\n   * @param {number} [params.length=1e4] - length of the underlying `StatusList`.\n   * @param {Iterable<number>} [params.revokeIndices=[]] - iterable producing indices to be revoked or suspended initially\n   * @returns {Promise<StatusList2021Credential>}\n   */\n  static async create(\n    keyDoc,\n    id,\n    { statusPurpose = 'revocation', length = 1e4, revokeIndices = [] } = {},\n  ) {\n    const statusList = await createList({ length });\n    this.updateStatusList(statusPurpose, statusList, revokeIndices);\n\n    const jsonCred = await createCredential({\n      id: String(id),\n      list: statusList,\n      statusPurpose,\n    });\n\n    const cred = this.fromJSON(jsonCred);\n\n    return await cred.sign(keyDoc);\n  }\n\n  /**\n   * Revokes indices and unsuspends other indices in the underlying status list, regenerating the proof.\n   * If `statusPurpose` = `revocation`, indices can't be unsuspended.\n   * The status list revoked (suspended)/unsuspended indices will be set atomically and in case of an error,\n   * the underlying value won't be modified.\n   * Throws an error if the underlying status list can't be decoded or any of the supplied indices is out of range.\n   *\n   * @param {KeyDoc} keyDoc\n   * @param {object} [update={}]\n   * @param {Iterable<number>} update.revokeIndices - indices to be revoked or suspended\n   * @param {Iterable<number>} update.unsuspendIndices - indices to be unsuspended\n   * @returns {Promise<this>}\n   */\n  async update(keyDoc, { revokeIndices = [], unsuspendIndices = [] }) {\n    const currentStatusList = await this.decodedStatusList();\n    const statusList = new StatusList({\n      buffer: new Uint8Array(currentStatusList.bitstring.bits),\n    });\n\n    this.constructor.updateStatusList(\n      this.credentialSubject.statusPurpose,\n      statusList,\n      revokeIndices,\n      unsuspendIndices,\n    );\n\n    this.credentialSubject.encodedList = await statusList.encode();\n    // Remove `proof` so that an array of `proof`s is not created by the following `sign` call.\n    delete this.proof;\n    this.setIssuanceDate(new Date().toISOString());\n\n    await this.sign(keyDoc);\n\n    return this;\n  }\n\n  /**\n   * Returns a `Promise` resolving to the decoded `StatusList`.\n   *\n   * @returns {Promise<StatusList>}\n   */\n  async decodedStatusList() {\n    const { encoded, decoded } = this.internalCachedStatusList;\n\n    if (encoded === this.credentialSubject.encodedList && decoded !== void 0) {\n      return decoded;\n    } else {\n      this.internalCachedStatusList = {\n        encoded: this.credentialSubject.encodedList,\n        decoded: decodeList(this.credentialSubject),\n      };\n\n      return this.internalCachedStatusList.decoded;\n    }\n  }\n\n  /**\n   * Returns `true` if given index is revoked or suspended, `false` otherwise.\n   * Throws an error if the underlying status list can't be decoded or supplied index is out of range.\n   *\n   * @param {number} index\n   * @returns {Promise<boolean>}\n   */\n  async revoked(index) {\n    const decodedStatusList = await this.decodedStatusList();\n\n    return decodedStatusList.getStatus(index);\n  }\n\n  /**\n   * Accepts an iterable of indices to be checked and returns an array containing `true` in the positions\n   * of revoked (suspended) indices and `false` for non-revoked (non-suspended) indices.\n   * Throws an error if the underlying status list can't be decoded or any of supplied indices is out of range.\n   *\n   * @param {Iterable<number>} indices\n   * @returns {Promise<Array<boolean>>}\n   */\n  async revokedBatch(indices) {\n    const decodedStatusList = await this.decodedStatusList();\n\n    return [...indices].map((index) => decodedStatusList.getStatus(index));\n  }\n\n  /**\n   * Decodes `StatusList2021Credential` from provided bytes.\n   * @param {Uint8Array} bytes\n   */\n  static fromBytes(bytes) {\n    const gzipBufferCred = Buffer.from(u8aToU8a(bytes));\n    const stringifiedCred = ungzip(gzipBufferCred, { to: 'string' });\n    const parsedCred = JSON.parse(stringifiedCred);\n\n    return this.fromJSON(parsedCred);\n  }\n\n  /**\n   * Instantiates `StatusList2021Credential` from the provided `JSON`.\n   *\n   * @param {object} json\n   * @returns {StatusList2021Credential}\n   */\n  static fromJSON(json) {\n    const cred = super.fromJSON(json);\n    cred.validate();\n\n    return cred;\n  }\n\n  /**\n   * Converts `StatusList2021Credential` to its JSON representation.\n   * @returns {object}\n   */\n  toJSON() {\n    this.validate();\n\n    return super.toJSON();\n  }\n\n  /**\n   * Encodes `StatusList2021Credential` as bytes.\n   * @returns {Uint8Array}\n   */\n  toBytes() {\n    const jsonCred = this.toJSON();\n    const stringifiedCred = JSON.stringify(jsonCred);\n    const bufferCred = Buffer.from(stringifiedCred);\n\n    return gzip(bufferCred);\n  }\n\n  /**\n   * Validates underlying `StatusList2021Credential`.\n   */\n  validate() {\n    const { credentialSubject } = this;\n\n    if (!credentialSubject) throw new Error('Missing `credentialSubject`');\n    if (!this.constructor.statusPurposes.has(credentialSubject.statusPurpose)) {\n      throw new Error(\n        `Invalid \\`statusPurpose\\`, expected one of \\`${[\n          ...this.constructor.statusPurposes,\n        ].join(', ')}\\``,\n      );\n    }\n    if (typeof credentialSubject.id !== 'string' || !credentialSubject.id) {\n      throw new Error('Missing `credentialSubject.id`');\n    }\n    if (credentialSubject.type !== 'StatusList2021') {\n      throw new Error(\n        '`credentialSubject.type` must be set to `StatusList2021`',\n      );\n    }\n    if (!credentialSubject.encodedList) {\n      throw new Error('`credentialSubject.encodedList` must be present');\n    }\n  }\n\n  /**\n   * Revokes (suspends) `revokeIndices` and unsuspends `unsuspendIndices` from the supplied `StatusList`.\n   *\n   * Throws an error if\n   * - non-empty `unsuspendIndices` passed along with `statusPurpose != suspension`\n   * - any index is present in both iterables\n   * - some index is out of bounds.\n   *\n   * @param {'revocation'|'suspension'} statusPurpose\n   * @param {StatusList} statusList\n   * @param {Iterable<number>} revokeIndices\n   * @param {Iterable<number>} unsuspendIndices\n   */\n  static updateStatusList(\n    statusPurpose,\n    statusList,\n    revokeIndices = [],\n    unsuspendIndices = [],\n  ) {\n    const unsuspendIndiceSet = new Set(unsuspendIndices);\n    if (statusPurpose !== 'suspension' && unsuspendIndiceSet.size > 0) {\n      throw new Error(\n        `Can't unsuspend indices for credential with \\`statusPurpose\\` = \\`${statusPurpose}\\`, it's only possible with \\`statusPurpose\\` = \\`suspension\\``,\n      );\n    }\n\n    for (const idx of revokeIndices) {\n      if (unsuspendIndiceSet.has(idx)) {\n        throw new Error(\n          `Index \\`${idx}\\` appears in both revoke and unsuspend sets`,\n        );\n      }\n\n      statusList.setStatus(idx, true);\n    }\n    for (const idx of unsuspendIndiceSet) {\n      statusList.setStatus(idx, false);\n    }\n  }\n}\n\n/**\n * Allowed status purposes for this credential type.\n */\nStatusList2021Credential.statusPurposes = new Set(['revocation', 'suspension']);\n"],"names":["VerifiableCredential","ensureStatusListId","createList","createCredential","StatusList","decodeList","u8aToU8a","ungzip","gzip"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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