@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 • 12.4 kB
Source Map (JSON)
{"version":3,"file":"Ed25519Signature2020.mjs","names":["jsonld"],"sources":["../../../../../../src/modules/vc/data-integrity/signature-suites/ed25519/Ed25519Signature2020.ts"],"sourcesContent":["import { MultiBaseEncoder, TypedArrayEncoder } from '../../../../../utils'\nimport { Ed25519PublicJwk, PublicJwk } from '../../../../kms'\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'\nimport { ED25519_SUITE_CONTEXT_URL_2020 } from './constants'\nimport { ed25519Signature2020Context } from './context2020'\n\ntype Ed25519Signature2020Options = Pick<\n JwsLinkedDataSignatureOptions,\n 'key' | 'proof' | 'date' | 'useNativeCanonize' | 'LDKeyClass'\n>\n\nexport class Ed25519Signature2020 extends JwsLinkedDataSignature {\n public static CONTEXT_URL = ED25519_SUITE_CONTEXT_URL_2020\n public static CONTEXT = ed25519Signature2020Context.get(ED25519_SUITE_CONTEXT_URL_2020)\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: ED25519_SUITE_CONTEXT_URL_2020,\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}'. Verification method type MUST be 'Ed25519VerificationKey2020'.`\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 publicJwk = PublicJwk.fromFingerprint(verificationMethod.publicKeyMultibase)\n if (!publicJwk.is(Ed25519PublicJwk)) {\n throw new Error('Expected multibase key to be of type Ed25519.')\n }\n\n // -- update type\n verificationMethod.type = 'Ed25519VerificationKey2018'\n\n verificationMethod = {\n ...verificationMethod,\n publicKeyMultibase: undefined,\n publicKeyBase58: TypedArrayEncoder.toBase58(publicJwk.publicKey.publicKey),\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 /**\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 // Handle the unfortunate Ed25519Signature2018 / credentials/v1 collision\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 // Either one by itself is fine, for this suite\n return hasEd2020 || hasCred || hasSecV2\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_SUIT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