From 550afcf91b8cb2545aed40c8ef9c81ded4c72b6f Mon Sep 17 00:00:00 2001 From: DaevMithran <61043607+DaevMithran@users.noreply.github.com> Date: Fri, 5 Apr 2024 09:02:34 +0530 Subject: [PATCH] fix: Fix did:vda validator (#512) * feat: Support multiple verida networks * Use VeridaDidValidator * Fix verida DID validator * Add missing catch block * fix: Fix verida namespace validation * fix verida init * Add RPCs for each verida network --- .github/workflows/build.yml | 5 ++++- src/controllers/validator/did.ts | 2 +- src/services/connectors/verida.ts | 10 +++++++--- src/types/constants.ts | 8 +++++++- src/types/environment.d.ts | 5 ++++- 5 files changed, 23 insertions(+), 7 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index f590cb54..d8b0c123 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -53,7 +53,10 @@ jobs: LOGTO_WEBHOOK_SECRET: ${{ secrets.LOGTO_WEBHOOK_SECRET }} MAINNET_RPC_URL: ${{ vars.MAINNET_RPC_URL }} POLYGON_PRIVATE_KEY: ${{ secrets.POLYGON_PRIVATE_KEY }} - POLYGON_RPC_URL: ${{ vars.POLYGON_RPC_URL }} + POLYGON_RPC_URL_MAINNET: ${{ vars.POLYGON_RPC_URL_MAINNET }} + POLYGON_RPC_URL_TESTNET: ${{ vars.POLYGON_RPC_URL_TESTNET }} + POLYGON_RPC_URL_DEVNET: ${{ vars.POLYGON_RPC_URL_DEVNET }} + POLYGON_RPC_URL_LOCAL: ${{ vars.POLYGON_RPC_URL_LOCAL }} RESOLVER_URL: ${{ vars.RESOLVER_URL }} TEST_USER_EMAIL: ${{ secrets.TEST_USER_EMAIL }} TEST_USER_PASSWORD: ${{ secrets.TEST_USER_PASSWORD }} diff --git a/src/controllers/validator/did.ts b/src/controllers/validator/did.ts index c49db7be..2170b045 100644 --- a/src/controllers/validator/did.ts +++ b/src/controllers/validator/did.ts @@ -184,7 +184,7 @@ export class VeridaDIDValidator extends BaseDidValidator implements IValidator { } // Check if namespace is valid - if (!(namespace in EnvironmentType)) { + if (!Object.values(EnvironmentType).includes(namespace)) { return { valid: false, error: `Verida DID namespace must be ${EnvironmentType.MAINNET} or ${EnvironmentType.TESTNET}`, diff --git a/src/services/connectors/verida.ts b/src/services/connectors/verida.ts index 5445f2c1..1c01eda0 100644 --- a/src/services/connectors/verida.ts +++ b/src/services/connectors/verida.ts @@ -45,7 +45,7 @@ export class VeridaService { didClientConfig: { callType: 'web3', web3Config: { - rpcUrl: POLYGON_RPC_URL, + rpcUrl: POLYGON_RPC_URL[environment], privateKey: polygonPrivateKey, // Polygon private key for creating DID, not needed in our case but required in the current version of the config. }, }, @@ -61,8 +61,12 @@ export class VeridaService { }, account: this.account[environment]!, }); + + if (this.context[environment] === undefined) { + throw new Error(`Verida client connection failed for environment: ${environment}`); + } } catch (error) { - throw new Error(`Error: ${error}`); + throw new Error(`${(error as Error).message || error}`); } } @@ -75,7 +79,7 @@ export class VeridaService { */ async sendData(environment: EnvironmentType, recipientDid: string, subject: string, data: DataRecord) { try { - if (!this.context) { + if (!this.context[environment]) { await VeridaService.instance.init( environment, VERIDA_APP_NAME, diff --git a/src/types/constants.ts b/src/types/constants.ts index 9e66e19e..a8e5b99c 100644 --- a/src/types/constants.ts +++ b/src/types/constants.ts @@ -1,3 +1,4 @@ +import type { EnvironmentType } from '@verida/types'; import * as dotenv from 'dotenv'; dotenv.config(); @@ -62,7 +63,12 @@ export const VC_REMOVE_ORIGINAL_FIELDS = true; export const CORS_ERROR_MSG = 'The CORS policy for this site does not allow access from the specified Origin.'; // Verida -export const POLYGON_RPC_URL = process.env.POLYGON_RPC_URL || 'https://rpc.ankr.com/polygon_mumbai'; +export const POLYGON_RPC_URL: Record = { + mainnet: process.env.POLYGON_RPC_URL_MAINNET || 'https://polygon-rpc.com', + testnet: process.env.POLYGON_RPC_URL_TESTNET || 'https://rpc.ankr.com/polygon_mumbai', + devnet: process.env.POLYGON_RPC_URL_DEVNET, + local: process.env.POLYGON_RPC_URL_LOCAL, +}; export const VERIDA_APP_NAME = 'Cheqd Verida Connector'; // Schema to store a Verifiable Credential on the Verida Network. export const VERIDA_CREDENTIAL_RECORD_SCHEMA = 'https://common.schemas.verida.io/credential/base/v0.2.0/schema.json'; diff --git a/src/types/environment.d.ts b/src/types/environment.d.ts index 7af29e21..a920ffd2 100644 --- a/src/types/environment.d.ts +++ b/src/types/environment.d.ts @@ -38,7 +38,10 @@ declare global { // Verida ENABLE_VERIDA_CONNECTOR: string | 'false'; - POLYGON_RPC_URL: string; + POLYGON_RPC_URL_MAINNET: string; + POLYGON_RPC_URL_TESTNET: string; + POLYGON_RPC_URL_DEVNET: string; + POLYGON_RPC_URL_LOCAL: string; VERIDA_PRIVATE_KEY: string; POLYGON_PRIVATE_KEY: string;