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 • 6.07 kB
{"version":3,"file":"v2-di-utils.mjs","names":[],"sources":["../../../../src/modules/vc/data-integrity/v2-di-utils.ts"],"sourcesContent":["import type { AgentContext } from '../../../agent'\nimport { CredoError } from '../../../error'\nimport { asArray } from '../../../utils'\nimport { type DidPurpose, DidsApi, type VerificationMethod } from '../../dids'\n\n/**\n * A Data Integrity proof, narrowed to the properties needed to identify the signer.\n */\ntype DataIntegrityProof = {\n verificationMethod: string\n}\n\nfunction isDataIntegrityProof(proof: unknown): proof is DataIntegrityProof {\n if (typeof proof !== 'object' || proof === null) return false\n\n const { verificationMethod } = proof as Record<string, unknown>\n return typeof verificationMethod === 'string'\n}\n\n/**\n * The part of a Data Integrity secured document this module needs: the embedded proof or proof\n * set. Both secured credentials and secured presentations satisfy this.\n */\ntype DataIntegritySecuredDocument = { proof: unknown }\n\nfunction getProofs(securedDocument: DataIntegritySecuredDocument): DataIntegrityProof[] {\n return asArray(securedDocument.proof).filter(isDataIntegrityProof)\n}\n\n/**\n * Resolves the verification method that secured a Data Integrity credential or presentation for\n * an already verified proof or proof set.\n *\n * Mirrors `getVerificationMethodForJwt` for the Data Integrity securing mechanism, so that all\n * three securing mechanisms derive a signer the same way at the call site.\n *\n * A `proof` value may be a proof set, which per VC-DATA-INTEGRITY has no order. Its proof purposes\n * have already been validated by w3c-di before this helper is called. Where several proofs name\n * different verification methods, position therefore carries no meaning. `expectedController` is\n * used to disambiguate: the proof whose verification method is controlled by that identity is\n * selected. This is the co-signature case — several parties sign, and only the one identified as\n * the issuer or holder is relevant here.\n *\n * @param expectedController the identity the signer is expected to be, where known — the\n * credential's `issuer` or the presentation's `holder`. Omit when unknown, in which case an\n * ambiguous proof set is rejected rather than resolved arbitrarily.\n *\n * @throws {CredoError} if there is no proof with a verification method, if several verification\n * methods cannot be disambiguated, or if a verification method cannot be resolved.\n */\nexport async function getVerificationMethodForDataIntegrityProof(\n agentContext: AgentContext,\n securedDocument: DataIntegritySecuredDocument,\n purpose: DidPurpose,\n expectedController?: string\n): Promise<VerificationMethod> {\n const proofs = getProofs(securedDocument)\n\n if (proofs.length === 0) {\n throw new CredoError('Data Integrity secured document does not contain a proof with a verification method')\n }\n\n const didsApi = agentContext.dependencyManager.resolve(DidsApi)\n\n const resolveVerificationMethod = async (verificationMethodId: string) => {\n const didDocument = await didsApi.resolveDidDocument(verificationMethodId)\n return didDocument.dereferenceKey(verificationMethodId, [purpose])\n }\n\n const verificationMethodIds = new Set(proofs.map((proof) => proof.verificationMethod))\n\n if (verificationMethodIds.size === 1) {\n return resolveVerificationMethod(proofs[0].verificationMethod)\n }\n\n if (!expectedController) {\n throw new CredoError(\n `Data Integrity secured document contains multiple proofs with different verification methods (${[...verificationMethodIds].join(', ')}). Unable to determine the signer.`\n )\n }\n\n const resolved = await Promise.all([...verificationMethodIds].map(resolveVerificationMethod))\n const matching = resolved.filter((verificationMethod) => verificationMethod.controller === expectedController)\n\n if (matching.length === 0) {\n throw new CredoError(\n `Data Integrity secured document contains multiple proofs, none of which is controlled by '${expectedController}'`\n )\n }\n\n return matching[0]\n}\n\n/**\n * Resolves the identity that secured a Data Integrity credential or presentation for the given\n * proof purpose.\n *\n * @param expectedController see `getVerificationMethodForDataIntegrityProof`.\n */\nexport async function getSignerForDataIntegrityProof(\n agentContext: AgentContext,\n securedDocument: DataIntegritySecuredDocument,\n purpose: DidPurpose,\n expectedController?: string\n): Promise<string> {\n const verificationMethod = await getVerificationMethodForDataIntegrityProof(\n agentContext,\n securedDocument,\n purpose,\n expectedController\n )\n\n if (!verificationMethod.controller) {\n throw new CredoError(\n `Verification method '${verificationMethod.id}' does not have a controller to use as the signer`\n )\n }\n\n return verificationMethod.controller\n}\n"],"mappings":";;;;;;;AAYA,SAA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