Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: w3c credential issuance by connection id #693

Merged
merged 2 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/api-gateway/src/issuance/dtos/issuance.dto.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/array-type */

import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { ArrayMaxSize, ArrayMinSize, IsArray, IsBoolean, IsDefined, IsEmail, IsEnum, IsNotEmpty, IsObject, IsOptional, IsString, MaxLength, ValidateIf, ValidateNested } from 'class-validator';
import { ArrayMaxSize, ArrayMinSize, IsArray, IsBoolean, IsDefined, IsEmail, IsEnum, IsNotEmpty, IsObject, IsOptional, IsString, MaxLength, ValidateNested } from 'class-validator';
import { IsCredentialJsonLdContext, SingleOrArray } from '../utils/helper';
import { IssueCredentialType, JsonLdCredentialDetailCredentialStatusOptions, JsonLdCredentialDetailOptionsOptions, JsonObject } from '../interfaces';
import { Transform, Type } from 'class-transformer';
Expand Down Expand Up @@ -125,11 +125,11 @@ export class Attribute {

}
export class CredentialsIssuanceDto {
@ValidateIf((obj) => obj.credentialType === IssueCredentialType.INDY)
@ApiProperty({ example: 'string' })
@IsNotEmpty({ message: 'Credential definition Id is required' })
@IsString({ message: 'Credential definition id should be string' })
@Transform(({ value }) => value.trim())
@IsOptional()
credentialDefinitionId?: string;

@ApiProperty({ example: 'string' })
Expand Down
17 changes: 9 additions & 8 deletions apps/api-gateway/src/issuance/dtos/multi-connection.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ class ConnectionAttributes {
connectionId: string;

@ApiProperty({
example: [
{
value: 'string',
name: 'string'
}
]
example: [
{
value: 'string',
name: 'string'
}
]
})
@IsArray()
@ValidateNested({ each: true })
Expand All @@ -28,6 +28,7 @@ class ConnectionAttributes {
@IsOptional()
attributes?: Attribute[];

@ApiProperty()
@IsNotEmpty({ message: 'Please provide valid credential' })
@IsObject({ message: 'credential should be an object' })
@Type(() => Credential)
Expand All @@ -36,11 +37,11 @@ class ConnectionAttributes {
credential?: Credential;

@ApiProperty()
@IsOptional()
@IsNotEmpty({ message: 'Please provide valid options' })
@IsObject({ message: 'options should be an object' })
@ValidateNested({ each: true })
@Type(() => JsonLdCredentialDetailOptions)
@IsOptional()
@ValidateNested({ each: true })
options?:JsonLdCredentialDetailOptions;
}

Expand Down
19 changes: 14 additions & 5 deletions apps/api-gateway/src/issuance/issuance.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,17 +534,26 @@ export class IssuanceController {
issueCredentialDto.credentialType = credentialType;

const credOffer = issueCredentialDto?.credentialData || [];
if (IssueCredentialType.INDY !== credentialType && IssueCredentialType.JSONLD !== credentialType) {

if (IssueCredentialType.INDY !== credentialType && IssueCredentialType.JSONLD !== credentialType) {
throw new NotFoundException(ResponseMessages.issuance.error.invalidCredentialType);
}
if (issueCredentialDto.credentialType === IssueCredentialType.JSONLD && credOffer.every(offer => (!offer?.credential || 0 === Object.keys(offer?.credential).length))) {
}

if (credentialType === IssueCredentialType.INDY && !issueCredentialDto.credentialDefinitionId) {
throw new BadRequestException(ResponseMessages.credentialDefinition.error.isRequired);
}

if (issueCredentialDto.credentialType !== IssueCredentialType.INDY && !credOffer.every(offer => (!offer?.attributes || 0 === Object.keys(offer?.attributes).length))) {
throw new BadRequestException(ResponseMessages.issuance.error.attributesAreRequired);
}

if (issueCredentialDto.credentialType === IssueCredentialType.JSONLD && credOffer.every(offer => (!offer?.credential || 0 === Object.keys(offer?.credential).length))) {
throw new BadRequestException(ResponseMessages.issuance.error.credentialNotPresent);
}

if (issueCredentialDto.credentialType === IssueCredentialType.JSONLD && credOffer.every(offer => (!offer?.options || 0 === Object.keys(offer?.options).length))) {
if (issueCredentialDto.credentialType === IssueCredentialType.JSONLD && credOffer.every(offer => (!offer?.options || 0 === Object.keys(offer?.options).length))) {
throw new BadRequestException(ResponseMessages.issuance.error.optionsNotPresent);
}

const getCredentialDetails = await this.issueCredentialService.sendCredentialCreateOffer(issueCredentialDto, user);

const finalResponse: IResponse = {
Expand Down
33 changes: 11 additions & 22 deletions apps/issuance/src/issuance.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ export class IssuanceService {
const schemaResponse: SchemaDetails = await this.issuanceRepository.getCredentialDefinitionDetails(
credentialDefinitionId
);

if (schemaResponse?.attributes) {
const schemaResponseError = [];
const attributesArray: IAttributes[] = JSON.parse(schemaResponse.attributes);
Expand All @@ -82,31 +81,26 @@ export class IssuanceService {
}

const agentDetails = await this.issuanceRepository.getAgentEndPoint(orgId);

if (!agentDetails) {
throw new NotFoundException(ResponseMessages.issuance.error.agentEndPointNotFound);
}

const { agentEndPoint } = agentDetails;

const orgAgentType = await this.issuanceRepository.getOrgAgentType(agentDetails?.orgAgentTypeId);

if (!orgAgentType) {
throw new NotFoundException(ResponseMessages.issuance.error.orgAgentTypeNotFound);
}

const issuanceMethodLabel = 'create-offer';

const url = await this.getAgentUrl(issuanceMethodLabel, orgAgentType, agentEndPoint, agentDetails?.tenantId);

const issuancePromises: Promise<ICreateOfferResponse>[] = [];
const issuancePromises = credentialData.map(async (credentials) => {
const { connectionId, attributes, credential, options } = credentials;
let issueData;

let issueData;
if (payload.credentialType === IssueCredentialType.INDY) {
for (const credentials of credentialData) {
const { connectionId, attributes } = credentials;
if (payload.credentialType === IssueCredentialType.INDY) {
issueData = {
protocolVersion: 'v1',
protocolVersion: payload.protocolVersion || 'v1',
connectionId,
credentialFormats: {
indy: {
Expand All @@ -118,13 +112,7 @@ export class IssuanceService {
autoAcceptCredential: payload.autoAcceptCredential || 'always',
comment
};
}
}

if (payload.credentialType === IssueCredentialType.JSONLD) {
for (const credentials of credentialData) {
const { connectionId, credential, options } = credentials;

} else if (payload.credentialType === IssueCredentialType.JSONLD) {
issueData = {
protocolVersion: payload.protocolVersion || 'v2',
connectionId,
Expand All @@ -138,16 +126,17 @@ export class IssuanceService {
comment: comment || ''
};
}
}

await this.delay(500);
const credentialCreateOfferDetails = this._sendCredentialCreateOffer(issueData, url, orgId);
issuancePromises.push(credentialCreateOfferDetails);
await this.delay(500);
return this._sendCredentialCreateOffer(issueData, url, orgId);
});

const results = await Promise.allSettled(issuancePromises);
return results;
} catch (error) {
this.logger.error(`[sendCredentialCreateOffer] - error in create credentials : ${JSON.stringify(error)}`);
const errorStack = error?.status?.message?.error?.reason || error?.status?.message?.error;

if (errorStack) {
throw new RpcException({
error: errorStack?.message ? errorStack?.message : errorStack,
Expand Down
2 changes: 2 additions & 0 deletions libs/common/src/response-messages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ export const ResponseMessages = {
NotSaved: 'Error in saving credential definition.',
Conflict: 'Credential definition already exists',
schemaIdNotFound: 'SchemaLedgerId not found',
isRequired: 'Credential definition Id is required',
OrgDidNotFound: 'OrgDid not found',
credDefIdNotFound: 'Credential Definition Id not found'
}
Expand Down Expand Up @@ -290,6 +291,7 @@ export const ResponseMessages = {
orgAgentTypeNotFound: 'Organization agent type not found',
credentialNotPresent: 'credential is required',
optionsNotPresent:'options are required',
attributesAreRequired: 'attributes are required',
invalidCredentialType:'invalid credential type'
}
},
Expand Down