{"version":3,"file":"verifiable-presentation.cjs","sources":["../../../src/vc/verifiable-presentation.js"],"sourcesContent":["import { signPresentation, verifyPresentation } from './presentations';\n\nimport {\n  ensureObjectWithId,\n  ensureString,\n  ensureURI,\n  isObject,\n  getUniqueElementsFromArray,\n} from '../utils';\n\nimport VerifiableCredential from './verifiable-credential';\n// import DIDResolver from \"../resolver/did/did-resolver\"; // eslint-disable-line\n\nconst DEFAULT_CONTEXT = 'https://www.w3.org/2018/credentials/v1';\nconst DEFAULT_TYPE = 'VerifiablePresentation';\n\n/**\n * @typedef {object} VerifiablePresentationVerificationResult The presentation verification result\n * @property {object} presentationResult Is this presentqtion verified or not\n * @property {array} credentialResults Verification results\n * @property {Boolean} verified Is verified or not\n * @property {any} [error] Optional error\n */\n\n/**\n * Representation of a Verifiable Presentation.\n */\nclass VerifiablePresentation {\n  /**\n   * Create a new Verifiable Presentation instance.\n   * @param {string} id - id of the presentation\n   * @constructor\n   */\n  constructor(id) {\n    ensureURI(id);\n    this.id = id;\n    this.context = [DEFAULT_CONTEXT];\n    this.type = [DEFAULT_TYPE];\n    this.credentials = [];\n    this.proof = null;\n  }\n\n  static fromJSON(json) {\n    const {\n      verifiableCredential, id, type, ...rest\n    } = json;\n    const vp = new VerifiablePresentation(id);\n\n    if (type) {\n      vp.type = [];\n      if (type.length !== undefined) {\n        type.forEach((typeVal) => {\n          vp.addType(typeVal);\n        });\n      } else {\n        vp.addType(type);\n      }\n    } else {\n      throw new Error(\n        'No type found in JSON object, verifiable presentations must have a type field.',\n      );\n    }\n\n    const context = rest['@context'];\n    if (context) {\n      vp.setContext(rest['@context']);\n      delete rest['@context'];\n    } else {\n      throw new Error(\n        'No context found in JSON object, verifiable presentations must have a @context field.',\n      );\n    }\n\n    if (verifiableCredential) {\n      if (verifiableCredential.length) {\n        verifiableCredential.forEach((credential) => {\n          vp.addCredential(credential);\n        });\n      } else {\n        vp.addCredential(verifiableCredential);\n      }\n    }\n\n    Object.assign(vp, rest);\n    return vp;\n  }\n\n  /**\n   * Sets the context to the given value, overrding all others\n   * @param {string|object} context - Context to assign\n   * @returns {VerifiablePresentation}\n   */\n  setContext(context) {\n    if (!isObject(context) && !Array.isArray(context)) {\n      ensureURI(context);\n    }\n    this.context = context;\n    return this;\n  }\n\n  /**\n   * Add a context to this Presentation's context array. Duplicates are omitted.\n   * @param {string|object} context - Context to add to the presentation context array\n   * @returns {VerifiablePresentation}\n   */\n  addContext(context) {\n    if (!isObject(context)) {\n      ensureURI(context);\n    }\n    this.context = getUniqueElementsFromArray(\n      [...this.context, context],\n      JSON.stringify,\n    );\n    return this;\n  }\n\n  /**\n   * Add a type to this Presentation's type array. Duplicates are omitted.\n   * @param {string} type - Type to add to the presentation type array\n   * @returns {VerifiablePresentation}\n   */\n  addType(type) {\n    ensureString(type);\n    this.type = [...new Set([...this.type, type])];\n    return this;\n  }\n\n  /**\n   * Set a holder for this Presentation\n   * @param {string} holder - Holder to add to the presentation\n   * @returns {VerifiablePresentation}\n   */\n  setHolder(holder) {\n    ensureURI(holder);\n    this.holder = holder;\n    return this;\n  }\n\n  /**\n   * Add a Verifiable Credential to this Presentation.\n   * @param {object} credential -  Verifiable Credential for the presentation\n   * @returns {VerifiablePresentation}\n   */\n  addCredential(credential) {\n    let cred = credential;\n    if (credential instanceof VerifiableCredential) {\n      cred = credential.toJSON();\n    }\n    ensureObjectWithId(cred, 'credential');\n    this.credentials.push(cred);\n\n    return this;\n  }\n\n  /**\n   * Add multiple Verifiable Credentials to this Presentation.\n   * @param {Array<object>} credentials -  Verifiable Credential for the presentation\n   * @returns {VerifiablePresentation}\n   */\n  addCredentials(credentials) {\n    credentials.forEach(this.addCredential.bind(this));\n  }\n\n  /**\n   * Define the JSON representation of a Verifiable Presentation.\n   * @returns {object}\n   */\n  toJSON() {\n    const { context, credentials, ...rest } = this;\n    return {\n      '@context': context,\n      verifiableCredential: credentials,\n      ...rest,\n    };\n  }\n\n  /**\n   * Sign a Verifiable Presentation using the provided keyDoc\n   * @param {object} keyDoc - document with `id`, `controller`, `type`, `privateKeyBase58` and `publicKeyBase58`\n   * @param {string} challenge - proof challenge Required.\n   * @param {string} domain - proof domain (optional)\n   * @param {DIDResolver} [resolver] - Resolver for DIDs.\n   * @param {Boolean} [compactProof] - Whether to compact the JSON-LD or not.\n   * @returns {Promise<VerifiablePresentation>}\n   */\n  async sign(keyDoc, challenge, domain, resolver = null, compactProof = true) {\n    const signedVP = await signPresentation(\n      this.toJSON(),\n      keyDoc,\n      challenge,\n      domain,\n      resolver,\n      compactProof,\n    );\n    this.context = signedVP['@context'];\n    this.proof = signedVP.proof;\n    return this;\n  }\n\n  /**\n   * Verify a Verifiable Presentation\n   * @param {object} [params] Verify parameters (TODO: add type info for this object)\n   * @returns {Promise<VerifiablePresentationVerificationResult>} - verification result.\n   */\n  async verify({\n    challenge,\n    domain,\n    resolver = null,\n    compactProof = true,\n    skipRevocationCheck = false,\n    skipSchemaCheck = false,\n    verifyMatchingIssuersForRevocation = true,\n    suite = [],\n  } = {}) {\n    if (!this.proof) {\n      throw new Error('The current VerifiablePresentation has no proof.');\n    }\n\n    return verifyPresentation(this.toJSON(), {\n      challenge,\n      domain,\n      resolver,\n      compactProof,\n      skipRevocationCheck,\n      skipSchemaCheck,\n      verifyMatchingIssuersForRevocation,\n      suite,\n    });\n  }\n}\n\nexport default VerifiablePresentation;\n"],"names":["ensureURI","isObject","getUniqueElementsFromArray","ensureString","VerifiableCredential","ensureObjectWithId","signPresentation","verifyPresentation"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAWA;;AAEA,MAAM,eAAe,GAAG,wCAAwC;AAChE,MAAM,YAAY,GAAG,wBAAwB;;AAE7C;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;AACA,MAAM,sBAAsB,CAAC;AAC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