Skip to content

Commit

Permalink
fix: Restrict verida to mainnet and testnet (#515)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaevMithran committed Apr 5, 2024
1 parent 9f264d3 commit ca27b47
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 16 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ jobs:
# POLYGON_PRIVATE_KEY: ${{ secrets.POLYGON_PRIVATE_KEY }}
# 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 }}
Expand Down
6 changes: 4 additions & 2 deletions src/controllers/validator/did.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@ export class VeridaDIDValidator extends BaseDidValidator implements IValidator {
return this.subject;
}

validate(did: Validatable): IValidationResult & { namespace?: EnvironmentType; identifier?: string } {
validate(
did: Validatable
): IValidationResult & { namespace?: EnvironmentType.TESTNET | EnvironmentType.MAINNET; identifier?: string } {
// Call base validation
let _v = super.validate(did);
if (!_v.valid) {
Expand All @@ -184,7 +186,7 @@ export class VeridaDIDValidator extends BaseDidValidator implements IValidator {
}

// Check if namespace is valid
if (!Object.values(EnvironmentType).includes(namespace)) {
if (namespace !== EnvironmentType.MAINNET && namespace !== EnvironmentType.TESTNET) {
return {
valid: false,
error: `Verida DID namespace must be ${EnvironmentType.MAINNET} or ${EnvironmentType.TESTNET}`,
Expand Down
19 changes: 12 additions & 7 deletions src/services/connectors/verida.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const { VERIDA_PRIVATE_KEY, POLYGON_PRIVATE_KEY } = process.env;
* Run the init method before running any other method.
*/
export class VeridaService {
private context: Partial<Record<EnvironmentType, Context>> = {};
private account: Partial<Record<EnvironmentType, AutoAccount>> = {};
private context: Partial<Record<EnvironmentType.TESTNET | EnvironmentType.MAINNET, Context>> = {};
private account: Partial<Record<EnvironmentType.TESTNET | EnvironmentType.MAINNET, AutoAccount>> = {};

static instance = new VeridaService();

Expand All @@ -30,7 +30,7 @@ export class VeridaService {
* @param accountPrivateKey The private key of the account
*/
async init(
environment: EnvironmentType,
environment: EnvironmentType.TESTNET | EnvironmentType.MAINNET,
contextName: string,
accountPrivateKey: string,
polygonPrivateKey: string
Expand Down Expand Up @@ -77,7 +77,12 @@ export class VeridaService {
* @param subject The subject of the message (similar to an email subject).
* @param data The data to be sent.
*/
async sendData(environment: EnvironmentType, recipientDid: string, subject: string, data: DataRecord) {
async sendData(
environment: EnvironmentType.TESTNET | EnvironmentType.MAINNET,
recipientDid: string,
subject: string,
data: DataRecord
) {
try {
if (!this.context[environment]) {
await VeridaService.instance.init(
Expand All @@ -88,7 +93,7 @@ export class VeridaService {
);
}

const messagingClient = await this.context[environment]?.getMessaging();
const messagingClient = await this.context[environment]!.getMessaging();

const messageType = 'inbox/type/dataSend'; // There are different types of message, here we are sending some data.
const messageData = {
Expand All @@ -99,7 +104,7 @@ export class VeridaService {
did: recipientDid,
};

await messagingClient?.send(recipientDid, messageType, messageData, subject, messageConfig);
await messagingClient.send(recipientDid, messageType, messageData, subject, messageConfig);
} catch (error) {
throw new Error(`${(error as Error).message || error}`);
}
Expand All @@ -115,7 +120,7 @@ export class VeridaService {
* @param credentialSummary A summary of the credential. For instance, will be displayed in the Verida Wallet UI.
*/
async sendCredential(
environment: EnvironmentType,
environment: EnvironmentType.TESTNET | EnvironmentType.MAINNET,
recipientDid: string,
messageSubject: string,
credential: VerifiableCredential,
Expand Down
5 changes: 2 additions & 3 deletions src/types/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,11 @@ 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: Record<EnvironmentType, string> = {
export const POLYGON_RPC_URL: Record<EnvironmentType.MAINNET | EnvironmentType.TESTNET, string> = {
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';
Expand Down
2 changes: 0 additions & 2 deletions src/types/environment.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ declare global {
ENABLE_VERIDA_CONNECTOR: string | 'false';
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;

Expand Down

0 comments on commit ca27b47

Please sign in to comment.