{"version":3,"file":"Ed25519Signature2018.mjs","names":["jsonld"],"sources":["../../../../../../src/modules/vc/data-integrity/signature-suites/ed25519/Ed25519Signature2018.ts"],"sourcesContent":["import { MultiBaseEncoder, TypedArrayEncoder } from '../../../../../utils'\nimport { CREDENTIALS_CONTEXT_V1_URL, SECURITY_CONTEXT_URL } from '../../../constants'\nimport type { DocumentLoader, JsonLdDoc, Proof, VerificationMethod } from '../../jsonldUtil'\nimport { _includesContext } from '../../jsonldUtil'\nimport jsonld from '../../libraries/jsonld'\nimport type { JwsLinkedDataSignatureOptions } from '../JwsLinkedDataSignature'\nimport { JwsLinkedDataSignature } from '../JwsLinkedDataSignature'\n\nimport { ED25519_SUITE_CONTEXT_URL_2018, ED25519_SUITE_CONTEXT_URL_2020 } from './constants'\nimport { ed25519Signature2018Context } from './context'\n\ntype Ed25519Signature2018Options = Pick<\n  JwsLinkedDataSignatureOptions,\n  'key' | 'proof' | 'date' | 'useNativeCanonize' | 'LDKeyClass'\n>\n\nexport class Ed25519Signature2018 extends JwsLinkedDataSignature {\n  public static CONTEXT_URL = ED25519_SUITE_CONTEXT_URL_2018\n  public static CONTEXT = ed25519Signature2018Context.get(ED25519_SUITE_CONTEXT_URL_2018)\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: Ed25519Signature2018Options) {\n    super({\n      type: 'Ed25519Signature2018',\n      algorithm: 'EdDSA',\n      LDKeyClass: options.LDKeyClass,\n      contextUrl: ED25519_SUITE_CONTEXT_URL_2018,\n      key: options.key,\n      proof: options.proof,\n      date: options.date,\n      useNativeCanonize: options.useNativeCanonize,\n    })\n    this.requiredKeyType = 'Ed25519VerificationKey2018'\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 (!_isEd2018Key(document) && !_isEd2020Key(document)) {\n      const verificationMethodType = jsonld.getValues(document, 'type')[0]\n      throw new Error(\n        `Unsupported verification method type '${verificationMethodType}'. Verification method type MUST be 'Ed25519VerificationKey2018' or 'Ed25519VerificationKey2020'.`\n      )\n    }\n    if (_isEd2018Key(document) && !_includesEd2018Context(document)) {\n      throw new Error(\n        `For verification method type 'Ed25519VerificationKey2018' the '@context' MUST contain the context url \"${ED25519_SUITE_CONTEXT_URL_2018}\".`\n      )\n    }\n    if (_isEd2020Key(document) && !_includesEd2020Context(document)) {\n      throw new Error(\n        `For verification method type 'Ed25519VerificationKey2020' the '@context' MUST contain the context url \"${ED25519_SUITE_CONTEXT_URL_2020}\".`\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  public async getVerificationMethod(options: { proof: Proof; documentLoader?: DocumentLoader }) {\n    let verificationMethod = await super.getVerificationMethod({\n      proof: options.proof,\n      documentLoader: options.documentLoader,\n    })\n\n    // convert Ed25519VerificationKey2020 to Ed25519VerificationKey2018\n    if (_isEd2020Key(verificationMethod) && _includesEd2020Context(verificationMethod)) {\n      // -- convert multibase to base58 --\n      const publicKeyBuffer = MultiBaseEncoder.decode(verificationMethod.publicKeyMultibase)\n\n      // -- update context --\n      // remove 2020 context\n      const context2020Index = verificationMethod['@context'].indexOf(ED25519_SUITE_CONTEXT_URL_2020)\n      verificationMethod['@context'].splice(context2020Index, 1)\n\n      // add 2018 context\n      verificationMethod['@context'].push(ED25519_SUITE_CONTEXT_URL_2018)\n\n      // -- update type\n      verificationMethod.type = 'Ed25519VerificationKey2018'\n\n      verificationMethod = {\n        ...verificationMethod,\n        publicKeyMultibase: undefined,\n        publicKeyBase58: TypedArrayEncoder.toBase58(publicKeyBuffer.data),\n      }\n    }\n\n    return verificationMethod\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\nfunction _includesCompatibleContext(options: { document: JsonLdDoc }) {\n  // Handle the unfortunate Ed25519Signature2018 / credentials/v1 collision\n  const hasEd2018 = _includesContext({\n    document: options.document,\n    contextUrl: ED25519_SUITE_CONTEXT_URL_2018,\n  })\n  const hasEd2020 = _includesContext({\n    document: options.document,\n    contextUrl: ED25519_SUITE_CONTEXT_URL_2020,\n  })\n  const hasCred = _includesContext({ document: options.document, contextUrl: CREDENTIALS_CONTEXT_V1_URL })\n  const hasSecV2 = _includesContext({ document: options.document, contextUrl: SECURITY_CONTEXT_URL })\n\n  // TODO: the console.warn statements below should probably be replaced with logging statements. However, this would currently require injection and I'm not sure we want to do that.\n  if (hasEd2018 && hasCred) {\n    // Warn if both are present\n    // console.warn('Warning: The ed25519-2018/v1 and credentials/v1 ' + 'contexts are incompatible.')\n    // console.warn('For VCs using Ed25519Signature2018 suite,' + ' using the credentials/v1 context is sufficient.')\n    return false\n  }\n\n  if (hasEd2018 && hasSecV2) {\n    // Warn if both are present\n    // console.warn('Warning: The ed25519-2018/v1 and security/v2 ' + 'contexts are incompatible.')\n    // console.warn('For VCs using Ed25519Signature2018 suite,' + ' using the security/v2 context is sufficient.')\n    return false\n  }\n\n  // Either one by itself is fine, for this suite\n  return hasEd2018 || hasEd2020 || hasCred || hasSecV2\n}\n\nfunction _isEd2018Key(verificationMethod: JsonLdDoc) {\n  // @ts-expect-error - .hasValue is not part of the public API\n  return jsonld.hasValue(verificationMethod, 'type', 'Ed25519VerificationKey2018')\n}\n\nfunction _includesEd2018Context(document: JsonLdDoc) {\n  return _includesContext({ document, contextUrl: ED25519_SUITE_CONTEXT_URL_2018 })\n}\n\nfunction _isEd2020Key(verificationMethod: JsonLdDoc) {\n  // @ts-expect-error - .hasValue is not part of the public API\n  return jsonld.hasValue(verificationMethod, 'type', 'Ed25519VerificationKey2020')\n}\n\nfunction _includesEd2020Context(document: JsonLdDoc) {\n  return _includesContext({ document, contextUrl: ED25519_SUITE_CONTEXT_URL_2020 })\n}\n"],"mappings":";;;;;;;;;;;;;AAgBA,IAAa,uBAAb,cAA0C,uBAAuB;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA+B/D,AAAO,YAAY,SAAsC;AACvD,QAAM;GACJ,MAAM;GACN,WAAW;GACX,YAAY,QAAQ;GACpB,YAAY;GACZ,KAAK,QAAQ;GACb,OAAO,QAAQ;GACf,MAAM,QAAQ;GACd,mBAAmB,QAAQ;GAC5B,CAAC;AACF,OAAK,kBAAkB;;CAGzB,MAAa,yBAAyB,UAAqB;AACzD,MAAI,CAAC,2BAA2B,EAAY,UAAU,CAAC,CAGrD,OAAM,IAAI,UACR,iFAAiF,KAAK,WAAW,IAClG;AAGH,MAAI,CAAC,aAAa,SAAS,IAAI,CAAC,aAAa,SAAS,EAAE;GACtD,MAAM,yBAAyBA,eAAO,UAAU,UAAU,OAAO,CAAC;AAClE,SAAM,IAAI,MACR,yCAAyC,uBAAuB,mGACjE;;AAEH,MAAI,aAAa,SAAS,IAAI,CAAC,uBAAuB,SAAS,CAC7D,OAAM,IAAI,MACR,0GAA0G,+BAA+B,IAC1I;AAEH,MAAI,aAAa,SAAS,IAAI,CAAC,uBAAuB,SAAS,CAC7D,OAAM,IAAI,MACR,0GAA0G,+BAA+B,IAC1I;AAIH,MAAI,SAAS,YAAY,OACvB,OAAM,IAAI,MAAM,4CAA4C;;CAIhE,MAAa,sBAAsB,SAA4D;EAC7F,IAAI,qBAAqB,MAAM,MAAM,sBAAsB;GACzD,OAAO,QAAQ;GACf,gBAAgB,QAAQ;GACzB,CAAC;AAGF,MAAI,aAAa,mBAAmB,IAAI,uBAAuB,mBA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