Skip to content

Commit

Permalink
vidos resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
ToTeTo committed Apr 16, 2024
1 parent 72bde28 commit fe9d175
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 36 deletions.
9 changes: 8 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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=
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=
4 changes: 4 additions & 0 deletions config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!,
Expand Down
35 changes: 35 additions & 0 deletions src/utils/resolver.ts
Original file line number Diff line number Diff line change
@@ -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,
};
}
42 changes: 42 additions & 0 deletions src/utils/vidosResolver.ts
Original file line number Diff line number Diff line change
@@ -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<DIDResolutionResult> {
const resolutionResponse = await fetch(`${this.instanceUrl}/${didUrl}`, {
method: "GET",
headers: {
Authorization: `Bearer ${this.apiKey}`,
Accept: options?.accept ?? "",
},
});

return resolutionResponse.json();
}

async getSupportedDidMethods(): Promise<string[]> {
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");
}
}
10 changes: 3 additions & 7 deletions src/verifier/verify-dates.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
import {
EthrDIDMethod,
JWTService,
KeyDIDMethod,
getCredentialsFromVP,
getSupportedResolvers,
verifyCredentialJWT,
verifyDID,
verifyPresentationJWT,
} from "@jpmorganchase/onyx-ssi-sdk";
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 {
Expand Down
11 changes: 3 additions & 8 deletions src/verifier/verify-revocation-status.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
import {
EthrDIDMethod,
JWTService,
getCredentialsFromVP,
getSupportedResolvers,
verifyDIDs,
verifyPresentationJWT,
verifyRevocationStatus,
} 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 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 {
Expand Down
13 changes: 3 additions & 10 deletions src/verifier/verify-schema-validation.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
import {
EthrDIDMethod,
JWTService,
KeyDIDMethod,
getCredentialsFromVP,
getSupportedResolvers,
verifyDIDs,
verifyPresentationJWT,
verifySchema,
} 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 verificationWithSchemaValidation = async () => {
// Instantiating the didResolver
const didResolver = getSupportedResolvers([didKey, didEthr]);
const { didResolver } = createDidResolver();

if (VP) {
try {
Expand Down
13 changes: 3 additions & 10 deletions src/verifier/verify.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down

0 comments on commit fe9d175

Please sign in to comment.