Skip to content

Commit

Permalink
feat: Add resource connector in credential issuance
Browse files Browse the repository at this point in the history
  • Loading branch information
DaevMithran committed Sep 23, 2024
1 parent e18d4ac commit 332471b
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/services/api/credentials.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { CredentialPayload, VerifiableCredential } from '@veramo/core';

import { VC_CONTEXT, VC_TYPE } from '../../types/constants.js';
import type { CredentialRequest } from '../../types/credential.js';
import { CredentialConnectors, type CredentialRequest } from '../../types/credential.js';
import { IdentityServiceStrategySetup } from '../identity/index.js';
import { v4 } from 'uuid';
import * as dotenv from 'dotenv';
import type { CustomerEntity } from '../../database/entities/customer.entity.js';
import { VeridaDIDValidator } from '../../controllers/validator/did.js';
import { ResourceConnector } from '../connectors/resource.js';
dotenv.config();

const { ENABLE_VERIDA_CONNECTOR } = process.env;
Expand All @@ -27,6 +27,7 @@ export class Credentials {
credentialStatus,
'@context': context,
format,
connector,
...additionalData
} = request;

Expand Down Expand Up @@ -54,7 +55,12 @@ export class Credentials {
).agent.createCredential(credential, format, statusOptions, customer);

const isVeridaDid = new VeridaDIDValidator().validate(subjectDid);
if (ENABLE_VERIDA_CONNECTOR === 'true' && isVeridaDid.valid && isVeridaDid.namespace) {
if (
ENABLE_VERIDA_CONNECTOR === 'true' &&
connector === CredentialConnectors.Verida &&
isVeridaDid.valid &&
isVeridaDid.namespace
) {
if (!credentialSchema) throw new Error('Credential schema is required');

// dynamic import to avoid circular dependency
Expand All @@ -69,6 +75,15 @@ export class Credentials {
credentialSchema,
credentialSummary
);
} else if (connector && connector === CredentialConnectors.Resource) {
await ResourceConnector.instance.sendCredential(
customer,
issuerDid,
verifiable_credential,
credentialName || v4(),
type ? type[0] : 'Verifiable Credential',
v4()
);
}
return verifiable_credential;
}
Expand Down
51 changes: 51 additions & 0 deletions src/services/connectors/resource.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import * as dotenv from 'dotenv';
import type { VerifiableCredential } from '@veramo/core';
import { IdentityServiceStrategySetup } from '../identity';
import type { AlternativeUri, MsgCreateResourcePayload } from '@cheqd/ts-proto/cheqd/resource/v2';
import { v4 } from 'uuid';
import { fromString } from 'uint8arrays';
import type { CustomerEntity } from '../../database/entities/customer.entity';
dotenv.config();

/**
* Helper class for the Verida protocol.
*
* Run the init method before running any other method.
*/
export class ResourceConnector {
static instance = new ResourceConnector();

/**
* Send a Verifiable Credential to a DID via the Verida protocol.
*
* @param recipientDid The DID of the recipient.
* @param messageSubject The subject of the message in which the Credential will be sent to the recipient (similar to an email subject).
* @param credential The credential itself.
* @param credentialName The name of the credential. For instance, will be displayed in the Verida Wallet UI.
* @param credentialSummary A summary of the credential. For instance, will be displayed in the Verida Wallet UI.
*/
async sendCredential(
customer: CustomerEntity,
recipientDid: string,
credential: VerifiableCredential,
credentialName: string,
resourceType: string,
resourceVersion: string,
alsoKnownAs?: AlternativeUri[]
) {
// Get strategy e.g. postgres or local
const identityServiceStrategySetup = new IdentityServiceStrategySetup(customer.customerId);

let resourcePayload: Partial<MsgCreateResourcePayload> = {};
resourcePayload = {
collectionId: recipientDid.split(':').pop(),
id: v4(),
name: credentialName,
resourceType,
data: fromString(credential, 'utf-8'),
version: resourceVersion,
alsoKnownAs,
};
await identityServiceStrategySetup.agent.createResource(recipientDid.split(':')[2], resourcePayload, customer);
}
}
6 changes: 6 additions & 0 deletions src/types/credential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import type {
} from '@cheqd/did-provider-cheqd';
import type { VerificationOptions } from './shared.js';

export enum CredentialConnectors {
Verida,
Resource,
}

export interface CredentialRequest {
subjectDid: string;
attributes: Record<string, unknown>;
Expand All @@ -26,6 +31,7 @@ export interface CredentialRequest {
termsOfUse?: AdditionalData | AdditionalData[];
refreshService?: AdditionalData | AdditionalData[];
evidence?: AdditionalData | AdditionalData[];
connector?: CredentialConnectors;

[x: string]: any;
}
Expand Down
5 changes: 5 additions & 0 deletions src/types/swagger-api-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@
* documentPresence: Physical,
* licenseNumber: 123AB4567
* }
* connector:
* type: string
* enum:
* - verida
* - resource
* required:
* - issuerDid
* - subjectDid
Expand Down

0 comments on commit 332471b

Please sign in to comment.