Skip to content

Commit

Permalink
feat: Allow additional data in credential request [DEV-4257] (#572)
Browse files Browse the repository at this point in the history
* feat: Allow additional data in credential request

* Make credentialRequest generic
  • Loading branch information
DaevMithran authored Aug 23, 2024
1 parent f24b7b6 commit 35dc34b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 16 deletions.
48 changes: 32 additions & 16 deletions src/services/api/credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,43 +15,59 @@ export class Credentials {
public static instance = new Credentials();

async issue_credential(request: CredentialRequest, customer: CustomerEntity): Promise<VerifiableCredential> {
const {
attributes,
credentialName,
credentialSummary,
credentialSchema,
issuerDid,
subjectDid,
type,
expirationDate,
credentialStatus,
'@context': context,
format,
...additionalData
} = request;

const credential: CredentialPayload = {
'@context': [...(request['@context'] || []), ...VC_CONTEXT],
type: [...(request.type || []), VC_TYPE],
issuer: { id: request.issuerDid },
credentialSchema: request.credentialSchema,
'@context': [...(context || []), ...VC_CONTEXT],
type: [...(type || []), VC_TYPE],
issuer: { id: issuerDid },
credentialSchema,
credentialSubject: {
id: request.subjectDid,
...request.attributes,
id: subjectDid,
...attributes,
},
issuanceDate: new Date().toISOString(),
...additionalData,
};

if (request.expirationDate) {
credential.expirationDate = request.expirationDate;
if (expirationDate) {
credential.expirationDate = expirationDate;
}

const statusOptions = request.credentialStatus || null;
const statusOptions = credentialStatus || null;

const verifiable_credential = await new IdentityServiceStrategySetup(
customer.customerId
).agent.createCredential(credential, request.format, statusOptions, customer);
).agent.createCredential(credential, format, statusOptions, customer);

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

// dynamic import to avoid circular dependency
const { VeridaService } = await import('../connectors/verida.js');

await VeridaService.instance.sendCredential(
isVeridaDid.namespace,
request.subjectDid,
subjectDid,
'New Verifiable Credential',
verifiable_credential,
request.credentialName || v4(),
request.credentialSchema,
request.credentialSummary
credentialName || v4(),
credentialSchema,
credentialSummary
);
}
return verifiable_credential;
Expand Down
12 changes: 12 additions & 0 deletions src/types/credential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,20 @@ export interface CredentialRequest {
credentialSchema?: string;
credentialName?: string;
credentialSummary?: string;
termsOfUse?: AdditionalData | AdditionalData[];
refreshService?: AdditionalData | AdditionalData[];
evidence?: AdditionalData | AdditionalData[];

[x: string]: any;
}

export type AdditionalData = {
type: string | string[];
id?: string;

[x: string]: any;
};

export type VerificationPoliciesRequest = {
policies: VerificationPolicies;
};
Expand Down

0 comments on commit 35dc34b

Please sign in to comment.