{"version":3,"file":"Ed25519Signature2020.mjs","names":["jsonld"],"sources":["../../../../../src/modules/vc/linked-data-proofs/signature-suites/Ed25519Signature2020.ts"],"sourcesContent":["import { MultiBaseEncoder } from '../../../../utils'\nimport { SECURITY_ED25519_2020_CONTEXT_URL } from '../../constants'\nimport { DEFAULT_CONTEXTS } from '../../jsonld/contexts'\nimport jsonld from '../../jsonld/jsonld'\nimport type { DocumentLoader, JsonLdDoc, Proof, VerificationMethod } from '../proof-ops/jsonldUtil'\nimport { _includesContext } from '../proof-ops/jsonldUtil'\nimport type { JwsLinkedDataSignatureOptions } from './JwsLinkedDataSignature'\nimport { JwsLinkedDataSignature } from './JwsLinkedDataSignature'\n\nconst jsonldWithHasValue = jsonld as typeof jsonld & {\n  hasValue(document: JsonLdDoc, key: string, value: string): boolean\n}\n\ntype Ed25519Signature2020Options = Pick<\n  JwsLinkedDataSignatureOptions,\n  'key' | 'proof' | 'date' | 'useNativeCanonize' | 'LDKeyClass'\n>\n\nexport class Ed25519Signature2020 extends JwsLinkedDataSignature {\n  // TODO: Centralise suite context metadata outside suite class statics.\n  public static CONTEXT_URL = SECURITY_ED25519_2020_CONTEXT_URL\n  public static CONTEXT = DEFAULT_CONTEXTS[SECURITY_ED25519_2020_CONTEXT_URL]\n\n  /**\n   * @param {object} options - Options hashmap.\n   *\n   * Either a `key` OR at least one of `signer`/`verifier` is required.\n   *\n   * @param {object} [options.key] - An optional key object (containing an\n   *   `id` property, and either `signer` or `verifier`, depending on the\n   *   intended operation. Useful for when the application is managing keys\n   *   itself (when using a KMS, you never have access to the private key,\n   *   and so should use the `signer` param instead).\n   * @param {Function} [options.signer] - Signer function that returns an\n   *   object with an async sign() method. This is useful when interfacing\n   *   with a KMS (since you don't get access to the private key and its\n   *   `signer()`, the KMS client gives you only the signer function to use).\n   * @param {Function} [options.verifier] - Verifier function that returns\n   *   an object with an async `verify()` method. Useful when working with a\n   *   KMS-provided verifier function.\n   *\n   * Advanced optional parameters and overrides.\n   *\n   * @param {object} [options.proof] - A JSON-LD document with options to use\n   *   for the `proof` node. Any other custom fields can be provided here\n   *   using a context different from security-v2).\n   * @param {string|Date} [options.date] - Signing date to use if not passed.\n   * @param {boolean} [options.useNativeCanonize] - Whether to use a native\n   *   canonize algorithm.\n   */\n  public constructor(options: Ed25519Signature2020Options = {}) {\n    super({\n      type: 'Ed25519Signature2020',\n      algorithm: 'EdDSA',\n      LDKeyClass: options.LDKeyClass,\n      contextUrl: Ed25519Signature2020.CONTEXT_URL,\n      key: options.key,\n      proof: options.proof,\n      date: options.date,\n      useNativeCanonize: options.useNativeCanonize,\n    })\n    this.requiredKeyType = 'Ed25519VerificationKey2020'\n  }\n\n  public async assertVerificationMethod(document: JsonLdDoc) {\n    if (!_includesCompatibleContext({ document: document })) {\n      // For DID Documents, since keys do not have their own contexts,\n      // the suite context is usually provided by the documentLoader logic\n      throw new TypeError(\n        `The '@context' of the verification method (key) MUST contain the context url \"${this.contextUrl}\".`\n      )\n    }\n\n    if (!_isEd2020Key(document)) {\n      const verificationMethodType = jsonld.getValues(document, 'type')[0]\n      throw new Error(\n        `Unsupported verification method type '${verificationMethodType}' for proof type '${this.type}'. Verification method type MUST be '${this.requiredKeyType}'.`\n      )\n    }\n\n    // ensure verification method has not been revoked\n    if (document.revoked !== undefined) {\n      throw new Error('The verification method has been revoked.')\n    }\n  }\n\n  /**\n   * Ensures the document to be signed contains the required signature suite\n   * specific `@context`, by either adding it (if `addSuiteContext` is true),\n   * or throwing an error if it's missing.\n   *\n   * @override\n   *\n   * @param {object} options - Options hashmap.\n   * @param {object} options.document - JSON-LD document to be signed.\n   * @param {boolean} options.addSuiteContext - Add suite context?\n   */\n  public ensureSuiteContext(options: { document: JsonLdDoc; addSuiteContext: boolean }) {\n    if (_includesCompatibleContext({ document: options.document })) {\n      return\n    }\n\n    super.ensureSuiteContext({ document: options.document, addSuiteContext: options.addSuiteContext })\n  }\n\n  /**\n   * Checks whether a given proof exists in the document.\n   *\n   * @override\n   *\n   * @param {object} options - Options hashmap.\n   * @param {object} options.proof - A proof.\n   * @param {object} options.document - A JSON-LD document.\n   * @param {object} options.purpose - A jsonld-signatures ProofPurpose\n   *  instance (e.g. AssertionProofPurpose, AuthenticationProofPurpose, etc).\n   * @param {Function} options.documentLoader  - A secure document loader (it is\n   *   recommended to use one that provides static known documents, instead of\n   *   fetching from the web) for returning contexts, controller documents,\n   *   keys, and other relevant URLs needed for the proof.\n   *\n   * @returns {Promise<boolean>} Whether a match for the proof was found.\n   */\n  public async matchProof(options: {\n    proof: Proof\n    document: VerificationMethod\n    // biome-ignore lint/suspicious/noExplicitAny: no explanation\n    purpose: any\n    documentLoader?: DocumentLoader\n  }) {\n    if (!_includesCompatibleContext({ document: options.document })) {\n      return false\n    }\n    return super.matchProof({\n      proof: options.proof,\n      document: options.document,\n      purpose: options.purpose,\n      documentLoader: options.documentLoader,\n    })\n  }\n\n  /**\n   * @param options - Options hashmap.\n   * @param options.verifyData - The data to sign.\n   * @param options.proof - A JSON-LD document with options to use\n   *   for the `proof` node. Any other custom fields can be provided here\n   *   using a context different from `security-v2`.\n   *\n   * @returns The proof containing the signature value.\n   */\n  public async sign(options: { verifyData: Uint8Array; proof: Proof }) {\n    if (!(this.signer && typeof this.signer.sign === 'function')) {\n      throw new Error('A signer API has not been specified.')\n    }\n    const signature = await this.signer.sign({ data: options.verifyData })\n    const encodedSignature = MultiBaseEncoder.encode(signature, 'base58btc')\n\n    // create detached content signature\n    options.proof.proofValue = encodedSignature\n    return options.proof\n  }\n\n  /**\n   * @param options - Options hashmap.\n   * @param options.verifyData - The data to verify.\n   * @param options.verificationMethod - A verification method.\n   * @param options.proof - The proof to be verified.\n   *\n   * @returns Resolves with the verification result.\n   */\n  public async verifySignature(options: {\n    verifyData: Uint8Array\n    verificationMethod: VerificationMethod\n    proof: Proof\n  }) {\n    if (!(options.proof.proofValue && typeof options.proof.proofValue === 'string')) {\n      throw new TypeError('The proof does not include a valid \"proofValue\" property.')\n    }\n    const signature = MultiBaseEncoder.decode(options.proof.proofValue).data\n\n    let { verifier } = this\n    if (!verifier) {\n      const key = await this.LDKeyClass.from(options.verificationMethod)\n      verifier = key.verifier()\n    }\n    return verifier.verify({ data: options.verifyData, signature })\n  }\n}\n\nfunction _includesCompatibleContext(options: { document: JsonLdDoc }) {\n  // Only Ed25519Signature2020 context defines the terms for this suite.\n  // The 2018 credentials context and generic security context do not include\n  // Ed25519Signature2020 type definitions, so we must not treat them as compatible.\n  const hasEd2020 = _includesContext({\n    document: options.document,\n    contextUrl: Ed25519Signature2020.CONTEXT_URL,\n  })\n\n  return hasEd2020\n}\n\nfunction _isEd2020Key(verificationMethod: JsonLdDoc) {\n  return jsonldWithHasValue.hasValue(verificationMethod, 'type', 'Ed25519VerificationKey2020')\n}\n"],"mappings":";;;;;;;;AASA,MAAM,qBAAqBA;AAS3B,IAAa,uBAAb,MAAa,6BAA6B,uBAAuB;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAgC/D,YAAmB,UAAuC,CAAC,GAAG;EAC5D,MAAM;GACJ,MAAM;GACN,WAAW;GACX,YAAY,QAAQ;GACpB,YAAY,qBAAqB;GACjC,KAAK,QAAQ;GACb,OAAO,QAAQ;GACf,MAAM,QAAQ;GACd,mBAAmB,QAAQ;EAC7B,CAAC;EACD,KAAK,kBAAkB;CACzB;CAEA,MAAa,yBAAyB,UAAqB;EACzD,IAAI,CAAC,2BAA2B,EAAY,SAAS,CAAC,GAGpD,MAAM,IAAI,UACR,iFAAiF,KAAK,WAAW,GACnG;EAGF,IAAI,CAAC,aAAa,QAAQ,GAAG;GAC3B,MAAM,yBAAyBA,eAAO,UAAU,UAAU,MAAM,CAAC,CAAC;GAClE,MAAM,IAAI,MACR,yCAAyC,uBAAuB,oBAAoB,KAAK,KAAK,uCAAuC,KAAK,gBAAgB,GAC5J;EACF;EAGA,IAAI,SAAS,YAAY,KAAA,GACvB,MAAM,IAAI,MAAM,2CAA2C;CAE/D;;;;;;;;;;;;CAaA,mBAA0B,SAA4D;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