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 • 3.25 kB
{"version":3,"file":"util.mjs","names":[],"sources":["../../../src/modules/vc/util.ts"],"sourcesContent":["import { CredoError } from '../../error'\nimport { tryParseDid } from '../dids/domain/parse'\nimport type { SingleValidationResult } from './models'\n\n/**\n * Formats an input date to w3c standard date format\n * @param date {number|string} Optional if not defined current date is returned\n *\n * @returns {string} date in a standard format as a string\n */\nexport const w3cDate = (date?: number | string): string => {\n let result = new Date()\n if (typeof date === 'number' || typeof date === 'string') {\n result = new Date(date)\n }\n const str = result.toISOString()\n return `${str.substr(0, str.length - 5)}Z`\n}\n\n/**\n * Returns the bare DID for a DID URL, stripping any fragment, query and path.\n * Returns the input unchanged if it is not a (parseable) DID.\n */\nfunction normalizeDidIdentifier(identifier: string): string {\n return tryParseDid(identifier)?.did ?? identifier\n}\n\n/**\n * Validates whether the given `controller` (e.g. the controller of a verification method)\n * authenticates one of the provided `credentialSubjectIds`. This authenticates that the\n * presentation creator controls one of the credential subject ids.\n *\n * A credential subject id may reference a specific verification method (e.g.\n * `did:example:123#0`), while a verification method controller is always the bare DID\n * (`did:example:123`). Both refer to the same DID subject, so for DIDs the fragment (and\n * any DID URL parameters/path) is stripped before comparison. Non-DID identifiers (e.g.\n * UUIDs or HTTP URLs) are compared as-is.\n *\n * If the credential has no credential subject ids, the validation passes by default as\n * there is no subject to authenticate.\n *\n * NOTE: this doesn't take into account the case where the credentialSubject is not the\n * holder. In the future we can add support for other flows, but for now this is the most\n * common use case.\n */\nexport function validateCredentialSubjectAuthentication(\n credentialSubjectIds: string[],\n controller: string\n): SingleValidationResult {\n const normalizedController = normalizeDidIdentifier(controller)\n const authenticatesCredentialSubject = credentialSubjectIds.some(\n (subjectId) => normalizeDidIdentifier(subjectId) === normalizedController\n )\n\n if (credentialSubjectIds.length > 0 && !authenticatesCredentialSubject) {\n return {\n isValid: false,\n error: new CredoError(\n 'Credential has one or more credentialSubject ids, but presentation does not authenticate credential subject'\n ),\n }\n }\n\n return {\n isValid: true,\n }\n}\n"],"mappings":";;;;;;;;;;AAUA,MAAa,WAAW,SAAmC;CACzD,IAAI,yBAAS,IAAI,KAAK;CACtB,IAAI,OAAO,SAAS,YAAY,OAAO,SAAS,UAC9C,SAAS,IAAI,KAAK,IAAI;CAExB,MAAM,MAAM,OAAO,YAAY;CAC/B,OAAO,GAAG,IAAI,OAAO,GAAG,IAAI,SAAS,CAAC,EAAE;AAC1C;;;;;AAMA,SAAS,uBAAuB,YAA4B;CAC1D,OAAO,YAAY,UAAU,CAAC,EAAE,OAAO;AACzC;;;;;;;;;;;;;;;;;;;AAoBA,SAAgB,wCACd,sBACA,YACwB;CACxB,MAAM,uBAAuB,uBAAuB,UAAU;CAC9D,MAAM,iCAAiC,qBAAqB,MACzD,cAAc,uBAAuB,SAAS,MAAM,oBACvD;CAEA,IAAI,qBAAqB,SAAS,KAAK,CAAC,gCACtC,OAAO;EACL,SAAS;EACT,OAAO,IAAI,WACT,6GACF;CACF;CAGF,OAAO,EACL,SAAS,KACX;AACF"}