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

54 lines (53 loc) • 2.49 kB
import { CredoError } from "../../error/CredoError.mjs"; import "../../error/index.mjs"; import { tryParseDid } from "../dids/domain/parse.mjs"; //#region src/modules/vc/util.ts /** * Formats an input date to w3c standard date format * @param date {number|string} Optional if not defined current date is returned * * @returns {string} date in a standard format as a string */ const w3cDate = (date) => { let result = /* @__PURE__ */ new Date(); if (typeof date === "number" || typeof date === "string") result = new Date(date); const str = result.toISOString(); return `${str.substr(0, str.length - 5)}Z`; }; /** * Returns the bare DID for a DID URL, stripping any fragment, query and path. * Returns the input unchanged if it is not a (parseable) DID. */ function normalizeDidIdentifier(identifier) { return tryParseDid(identifier)?.did ?? identifier; } /** * Validates whether the given `controller` (e.g. the controller of a verification method) * authenticates one of the provided `credentialSubjectIds`. This authenticates that the * presentation creator controls one of the credential subject ids. * * A credential subject id may reference a specific verification method (e.g. * `did:example:123#0`), while a verification method controller is always the bare DID * (`did:example:123`). Both refer to the same DID subject, so for DIDs the fragment (and * any DID URL parameters/path) is stripped before comparison. Non-DID identifiers (e.g. * UUIDs or HTTP URLs) are compared as-is. * * If the credential has no credential subject ids, the validation passes by default as * there is no subject to authenticate. * * NOTE: this doesn't take into account the case where the credentialSubject is not the * holder. In the future we can add support for other flows, but for now this is the most * common use case. */ function validateCredentialSubjectAuthentication(credentialSubjectIds, controller) { const normalizedController = normalizeDidIdentifier(controller); const authenticatesCredentialSubject = credentialSubjectIds.some((subjectId) => normalizeDidIdentifier(subjectId) === normalizedController); if (credentialSubjectIds.length > 0 && !authenticatesCredentialSubject) return { isValid: false, error: new CredoError("Credential has one or more credentialSubject ids, but presentation does not authenticate credential subject") }; return { isValid: true }; } //#endregion export { validateCredentialSubjectAuthentication, w3cDate }; //# sourceMappingURL=util.mjs.map