{"version":3,"file":"W3cV2DataIntegrityVerifiableCredential.mjs","names":[],"sources":["../../../../src/modules/vc/data-integrity/W3cV2DataIntegrityVerifiableCredential.ts"],"sourcesContent":["import { CredoError } from '../../../error'\nimport { JsonTransformer, MessageValidator } from '../../../utils'\nimport { ClaimFormat } from '../models/ClaimFormat'\nimport { W3cV2Credential } from '../models/credential/W3cV2Credential'\n\nexport type W3cV2DataIntegritySecuredCredential = Record<string, unknown> & { proof: unknown }\n\nexport interface W3cV2DataIntegrityVerifiableCredentialOptions {\n  securedCredential: W3cV2DataIntegritySecuredCredential\n}\n\n/**\n * Represents a Verifiable Credential secured with Data Integrity proof(s).\n *\n * @see https://www.w3.org/TR/vc-data-integrity/\n */\nexport class W3cV2DataIntegrityVerifiableCredential {\n  public constructor(options: W3cV2DataIntegrityVerifiableCredentialOptions) {\n    this.securedCredential = options.securedCredential\n    this.resolvedCredential = JsonTransformer.fromJSON(options.securedCredential, W3cV2Credential, {\n      validate: false,\n    })\n\n    // Validates the credential structure and proof presence\n    this.validate()\n  }\n\n  public static fromObject(credential: W3cV2DataIntegritySecuredCredential) {\n    return new W3cV2DataIntegrityVerifiableCredential({\n      securedCredential: credential,\n    })\n  }\n\n  /**\n   * The original credential object with embedded Data Integrity proof(s).\n   */\n  public readonly securedCredential: W3cV2DataIntegritySecuredCredential\n\n  /**\n   * Resolved credential is the fully resolved {@link W3cV2Credential} instance.\n   */\n  public readonly resolvedCredential: W3cV2Credential\n\n  /**\n   * The JSON representation of this credential.\n   */\n  public get encoded() {\n    return JSON.stringify(this.securedCredential)\n  }\n\n  /**\n   * The {@link ClaimFormat} of the credential.\n   *\n   * For W3C VC Data Integrity credentials this is always `di_vc`.\n   */\n  public get claimFormat(): ClaimFormat.DiVc {\n    return ClaimFormat.DiVc\n  }\n\n  /**\n   * Validates the credential and proof structure.\n   */\n  public validate() {\n    // Validate the resolved credential according to the data model\n    MessageValidator.validateSync(this.resolvedCredential)\n\n    // Validate that proof field exists and is properly structured\n    const proof = this.securedCredential.proof\n    if (!proof) {\n      throw new CredoError('The provided credential does not have a proof field.')\n    }\n\n    // Proof should be either a single proof object or an array of proofs\n    if (typeof proof !== 'object') {\n      throw new CredoError('The proof field must be an object or array of objects.')\n    }\n\n    if (\n      !Array.isArray(proof) &&\n      typeof proof === 'object' &&\n      (!('type' in proof) || proof.type !== 'DataIntegrityProof')\n    ) {\n      throw new CredoError('The proof must have type \"DataIntegrityProof\".')\n    }\n\n    if (Array.isArray(proof)) {\n      for (const p of proof) {\n        if (typeof p !== 'object' || !('type' in p) || p.type !== 'DataIntegrityProof') {\n          throw new CredoError('All proofs in the proof array must have type \"DataIntegrityProof\".')\n        }\n      }\n    }\n  }\n}\n"],"mappings":";;;;;;;;;;;;;AAgBA,IAAa,yCAAb,MAAa,uCAAuC;CAClD,YAAmB,SAAwD;EACzE,KAAK,oBAAoB,QAAQ;EACjC,KAAK,qBAAqB,gBAAgB,SAAS,QAAQ,mBAAmB,iBAAiB,EAC7F,UAAU,MACZ,CAAC;EAGD,KAAK,SAAS;CAChB;CAEA,OAAc,WAAW,YAAiD;EACxE,OAAO,IAAI,uCAAuC,EAChD,mBAAmB,WACrB,CAAC;CACH;;;;CAeA,IAAW,UAAU;EACnB,OAAO,KAAK,UAAU,KAAK,iBAAiB;CAC9C;;;;;;CAOA,IAAW,cAAgC;EACzC,OAAA;CACF;;;;CAKA,WAAkB;EAEhB,iBAAiB,aAAa,KAAK,kBAAkB;EAGrD,MAAM,QAAQ,KAAK,kBAAkB;EACrC,IAAI,CAAC,OACH,MAAM,IAAI,WAAW,sDAAsD;EAI7E,IAAI,OAAO,UAAU,UACnB,MAAM,IAAI,WAAW,wDAAwD;EAG/E,IACE,CAAC,MAAM,QAAQ,KAAK,KACpB,OAAO,UAAU,aAChB,EAAE,UAAU,UAAU,MAAM,SAAS,uBAEtC,MAAM,IAAI,WAAW,kDAAgD;EAGvE,IAAI,MAAM,QAAQ,KAAK,GAChB;QAAA,MAAM,KAAK,OACd,IAAI,OAAO,MAAM,YAAY,EAAE,UAAU,MAAM,EAAE,SAAS,sBACxD,MAAM,IAAI,WAAW,sEAAoE;EAAA;CAIjG;AACF"}