diff --git a/.env.example b/.env.example index 7f76b62..15ddd31 100644 --- a/.env.example +++ b/.env.example @@ -38,4 +38,11 @@ VC_SCHEMA_URL= # VP_DIR_PATH: The directory where your verification presentations are stored. If no value is provided, it defaults to '/src/verification_presentation'. # VP: The name of the verification presentation you're using. VP_DIR_PATH= -VP= \ No newline at end of file +VP= + +# Vidos configuraiton +# These values should be aquired from Vidos Dashboard: https://dashboard.vidos.id +# VIDOS_RESOLVER_URL: The URL of the resolver instance +# VIDOS_API_KEY: Vidos API key with policy that allows resolver instance access +VIDOS_RESOLVER_URL= +VIDOS_API_KEY= \ No newline at end of file diff --git a/config.ts b/config.ts index af47938..b953bbe 100644 --- a/config.ts +++ b/config.ts @@ -45,6 +45,10 @@ export const VP_DIR_PATH = getParam("VP_DIR_PATH") || "./src/verifiable_presentation"; export const VP = getParam("VP"); +//Vidos config +export const VIDOS_RESOLVER_URL = getParam("VIDOS_RESOLVER_URL"); +export const VIDOS_API_KEY = getParam("VIDOS_API_KEY"); + export const provider = new ethers.providers.JsonRpcProvider(NETWORK_RPC_URL!); export const ethrProvider = { name: NETWORK_NAME!, diff --git a/src/utils/resolver.ts b/src/utils/resolver.ts new file mode 100644 index 0000000..e8e03f7 --- /dev/null +++ b/src/utils/resolver.ts @@ -0,0 +1,35 @@ +import { + EthrDIDMethod, + KeyDIDMethod, + getSupportedResolvers, +} from "@jpmorganchase/onyx-ssi-sdk"; +import { Resolvable } from "did-resolver"; +import { ethrProvider, VIDOS_API_KEY, VIDOS_RESOLVER_URL } from "../../config"; +import { VidosResolver } from "./vidosResolver"; + +/** + * Utility method for creation of DID resolver and DID methods. + * If there is configuration for Vidos, it will create a VidosResolver, otherwise it will create a resolver with the `getDIDResolver` method from the supported DID methods. + */ +export function createDidResolver() { + const keyDidMethod = new KeyDIDMethod(); + const ethrDidMethod = new EthrDIDMethod(ethrProvider); + let didResolver: Resolvable; + + if (VIDOS_API_KEY || VIDOS_RESOLVER_URL) { + if (VIDOS_API_KEY && VIDOS_RESOLVER_URL) { + didResolver = new VidosResolver(VIDOS_RESOLVER_URL, VIDOS_API_KEY); + } else { + throw new Error( + "Cannot create VidosResolver, VIDOS_API_KEY and VIDOS_RESOLVER_URL must be set together" + ); + } + } else { + didResolver = getSupportedResolvers([]); + } + + return { + didMethods: { key: keyDidMethod, ethr: ethrDidMethod }, + didResolver, + }; +} diff --git a/src/utils/vidosResolver.ts b/src/utils/vidosResolver.ts new file mode 100644 index 0000000..25d5146 --- /dev/null +++ b/src/utils/vidosResolver.ts @@ -0,0 +1,42 @@ +import type { + DIDResolutionOptions, + DIDResolutionResult, + Resolvable +} from "did-resolver"; + +/** + * Create resolver that is using the Vidos service to resolve DIDs. + * + * - For more info: [https://vidos.id/](https://vidos.id/). + * - For resolver configuration: [https://dashboard.vidos.id/](https://dashboard.vidos.id/). + * - For docs: [https://vidos.id/docs/](https://vidos.id/docs/). + */ +export class VidosResolver implements Resolvable { + constructor(readonly instanceUrl: string, readonly apiKey: string) {} + + async resolve( + didUrl: string, + options?: DIDResolutionOptions | undefined + ): Promise { + const resolutionResponse = await fetch(`${this.instanceUrl}/${didUrl}`, { + method: "GET", + headers: { + Authorization: `Bearer ${this.apiKey}`, + Accept: options?.accept ?? "", + }, + }); + + return resolutionResponse.json(); + } + + async getSupportedDidMethods(): Promise { + const methodsResponse = await fetch(`${this.instanceUrl}/methods`, { + method: "GET", + }); + if (methodsResponse.ok) { + const { methods } = await methodsResponse.json(); + return methods; + } + throw new Error("Failed to fetch supported DID methods"); + } +} diff --git a/src/verifier/verify-dates.ts b/src/verifier/verify-dates.ts index 2df0345..8b02b22 100644 --- a/src/verifier/verify-dates.ts +++ b/src/verifier/verify-dates.ts @@ -1,9 +1,6 @@ import { - EthrDIDMethod, JWTService, - KeyDIDMethod, getCredentialsFromVP, - getSupportedResolvers, verifyCredentialJWT, verifyDID, verifyPresentationJWT, @@ -11,15 +8,14 @@ import { import fs from "fs"; import { camelCase } from "lodash"; import path from "path"; -import { JwtPayload, VP, VP_DIR_PATH, ethrProvider } from "../../config"; +import { JwtPayload, VP, VP_DIR_PATH } from "../../config"; +import { createDidResolver } from "../utils/resolver"; -const didKey = new KeyDIDMethod(); -const didEthr = new EthrDIDMethod(ethrProvider); const jwtService = new JWTService(); const verificationWithDates = async () => { // Instantiating the didResolver - const didResolver = getSupportedResolvers([didKey, didEthr]); + const { didResolver } = createDidResolver(); if (VP) { try { diff --git a/src/verifier/verify-revocation-status.ts b/src/verifier/verify-revocation-status.ts index 5e1723c..9cb9339 100644 --- a/src/verifier/verify-revocation-status.ts +++ b/src/verifier/verify-revocation-status.ts @@ -1,8 +1,5 @@ import { - EthrDIDMethod, - JWTService, getCredentialsFromVP, - getSupportedResolvers, verifyDIDs, verifyPresentationJWT, verifyRevocationStatus, @@ -10,14 +7,12 @@ import { import fs from "fs"; import { camelCase } from "lodash"; import path from "path"; -import { VP, VP_DIR_PATH, ethrProvider } from "../../config"; - -const didEthr = new EthrDIDMethod(ethrProvider); -const jwtService = new JWTService(); +import { VP, VP_DIR_PATH } from "../../config"; +import { createDidResolver } from "../utils/resolver"; const verificationWithRevocationStatus = async () => { // Instantiating the didResolver - const didResolver = getSupportedResolvers([didEthr]); + const { didResolver } = createDidResolver(); if (VP) { try { diff --git a/src/verifier/verify-schema-validation.ts b/src/verifier/verify-schema-validation.ts index 745f6f6..489d71b 100644 --- a/src/verifier/verify-schema-validation.ts +++ b/src/verifier/verify-schema-validation.ts @@ -1,9 +1,5 @@ import { - EthrDIDMethod, - JWTService, - KeyDIDMethod, getCredentialsFromVP, - getSupportedResolvers, verifyDIDs, verifyPresentationJWT, verifySchema, @@ -11,15 +7,12 @@ import { import fs from "fs"; import { camelCase } from "lodash"; import path from "path"; -import { VP, VP_DIR_PATH, ethrProvider } from "../../config"; - -const didKey = new KeyDIDMethod(); -const didEthr = new EthrDIDMethod(ethrProvider); -const jwtService = new JWTService(); +import { VP, VP_DIR_PATH} from "../../config"; +import { createDidResolver } from "../utils/resolver"; const verificationWithSchemaValidation = async () => { // Instantiating the didResolver - const didResolver = getSupportedResolvers([didKey, didEthr]); + const { didResolver } = createDidResolver(); if (VP) { try { diff --git a/src/verifier/verify.ts b/src/verifier/verify.ts index 49af976..4076f05 100644 --- a/src/verifier/verify.ts +++ b/src/verifier/verify.ts @@ -1,24 +1,17 @@ import { - EthrDIDMethod, - JWTService, - KeyDIDMethod, getCredentialsFromVP, - getSupportedResolvers, verifyDIDs, verifyPresentationJWT, } from "@jpmorganchase/onyx-ssi-sdk"; import fs from "fs"; import { camelCase } from "lodash"; import path from "path"; -import { VP, VP_DIR_PATH, ethrProvider } from "../../config"; - -const didKey = new KeyDIDMethod(); -const didEthr = new EthrDIDMethod(ethrProvider); -const jwtService = new JWTService(); +import { VP, VP_DIR_PATH } from "../../config"; +import { createDidResolver } from "../utils/resolver"; const verification = async () => { // Instantiating the didResolver - const didResolver = getSupportedResolvers([didKey, didEthr]); + const { didResolver } = createDidResolver(); if (VP) { try {