UNPKG

@credo-ts/core

Version:

<p align="center"> <br /> <img alt="Credo Logo" src="https://github.com/openwallet-foundation/credo-ts/blob/c7886cb8377ceb8ee4efe8d264211e561a75072d/images/credo-logo.png" height="250px" /> </p> <h1 align="center"><b>Credo - Core</b></h1

1 lines • 9.06 kB
{"version":3,"file":"Ed25519Signature2018.mjs","names":["jsonld"],"sources":["../../../../../src/modules/vc/linked-data-proofs/signature-suites/Ed25519Signature2018.ts"],"sourcesContent":["import { CREDENTIALS_CONTEXT_V1_URL, SECURITY_CONTEXT_URL, SECURITY_ED25519_2018_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 Ed25519Signature2018Options = Pick<\n JwsLinkedDataSignatureOptions,\n 'key' | 'proof' | 'date' | 'useNativeCanonize' | 'LDKeyClass'\n>\n\nexport class Ed25519Signature2018 extends JwsLinkedDataSignature {\n // TODO: Centralise suite context metadata outside suite class statics.\n public static CONTEXT_URL = SECURITY_ED25519_2018_CONTEXT_URL\n public static CONTEXT = DEFAULT_CONTEXTS[SECURITY_ED25519_2018_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: Ed25519Signature2018Options = {}) {\n super({\n type: 'Ed25519Signature2018',\n algorithm: 'EdDSA',\n LDKeyClass: options.LDKeyClass,\n contextUrl: Ed25519Signature2018.CONTEXT_URL,\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)) {\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\nfunction _includesCompatibleContext(options: { document: JsonLdDoc }) {\n // Handle the unfortunate Ed25519Signature2018 / credentials/v1 collision\n const hasEd2018 = _includesContext({\n document: options.document,\n contextUrl: Ed25519Signature2018.CONTEXT_URL,\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 || hasCred || hasSecV2\n}\n\nfunction _isEd2018Key(verificationMethod: JsonLdDoc) {\n return jsonldWithHasValue.hasValue(verificationMethod, 'type', 'Ed25519VerificationKey2018')\n}\n"],"mappings":";;;;;;AAQA,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;EACpF,IAAI,2BAA2B,EAAE,UAAU,QAAQ,SAAS,CAAC,GAC3D;EAGF,MAAM,mBAAmB;GAAE,UAAU,QAAQ;GAAU,iBAAiB,QAAQ;EAAgB,CAAC;CACnG;;;;;;;;;;;;;;;;;;CAmBA,MAAa,WAAW,SAMrB;EACD,IAAI,CAAC,2BAA2B,EAAE,UAAU,QAAQ,SAAS,CAAC,GAC5D,OAAO;EAET,OAAO,MAAM,WAAW;GACtB,OAAO,QAAQ;GACf,UAAU,QAAQ;GAClB,SAAS,QAAQ;GACjB,gBAAgB,QAAQ;EAC1B,CAAC;CACH;AACF;AAvHE,qBAAc,cAAc;AAC5B,qBAAc,UAAU,iBAAiB;AAwH3C,SAAS,2BAA2B,SAAkC;CAEpE,MAAM,YAAY,iBAAiB;EACjC,UAAU,QAAQ;EAClB,YAAY,qBAAqB;CACnC,CAAC;CACD,MAAM,UAAU,iBAAiB;EAAE,UAAU,QAAQ;EAAU,YAAY;CAA2B,CAAC;CACvG,MAAM,WAAW,iBAAiB;EAAE,UAAU,QAAQ;EAAU,YAAY;CAAqB,CAAC;CAGlG,IAAI,aAAa,SAIf,OAAO;CAGT,IAAI,aAAa,UAIf,OAAO;CAIT,OAAO,aAAa,WAAW;AACjC;AAEA,SAAS,aAAa,oBAA+B;CACnD,OAAO,mBAAmB,SAAS,oBAAoB,QAAQ,4BAA4B;AAC7F"}