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: Add reuse connection #593

Merged
merged 4 commits into from
Mar 13, 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
3 changes: 2 additions & 1 deletion apps/api-gateway/src/connection/connection.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ export class ConnectionService extends BaseService {
goal: connectionDto.goal,
handshake: connectionDto.handshake,
handshakeProtocols: connectionDto.handshakeProtocols,
user
user,
recipientKey:connectionDto.recipientKey
};

return this.sendNatsMessage(this.connectionServiceProxy, 'create-connection', connectionDetails);
Expand Down
6 changes: 6 additions & 0 deletions apps/api-gateway/src/connection/dtos/connection.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ export class CreateConnectionDto {
handshakeProtocols: string[];

orgId: string;

@ApiPropertyOptional()
@IsString()
@IsOptional()
@IsNotEmpty({ message: 'Please provide recipientKey' })
recipientKey: string;
}

export class ConnectionDto {
Expand Down
6 changes: 6 additions & 0 deletions apps/api-gateway/src/issuance/dtos/issuance.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ class CredentialsIssuanceDto {
@IsOptional()
credentialType:IssueCredentialType;

@ApiPropertyOptional({ default: true })
@IsOptional()
@IsNotEmpty({ message: 'please provide valid value for reuseConnection' })
@IsBoolean({ message: 'reuseConnection must be a boolean' })
reuseConnection?: boolean;

orgId: string;
}

Expand Down
4 changes: 2 additions & 2 deletions apps/api-gateway/src/issuance/issuance.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ export class IssuanceService extends BaseService {
}> {
let payload;
if (IssueCredentialType.INDY === issueCredentialDto.credentialType) {
payload = { attributes: issueCredentialDto.attributes, comment: issueCredentialDto.comment, credentialDefinitionId: issueCredentialDto.credentialDefinitionId, orgId: issueCredentialDto.orgId, protocolVersion: issueCredentialDto.protocolVersion, goalCode: issueCredentialDto.goalCode, parentThreadId: issueCredentialDto.parentThreadId, willConfirm: issueCredentialDto.willConfirm, label: issueCredentialDto.label, autoAcceptCredential: issueCredentialDto.autoAcceptCredential, credentialType: issueCredentialDto.credentialType, isShortenUrl: issueCredentialDto.isShortenUrl };
payload = { attributes: issueCredentialDto.attributes, comment: issueCredentialDto.comment, credentialDefinitionId: issueCredentialDto.credentialDefinitionId, orgId: issueCredentialDto.orgId, protocolVersion: issueCredentialDto.protocolVersion, goalCode: issueCredentialDto.goalCode, parentThreadId: issueCredentialDto.parentThreadId, willConfirm: issueCredentialDto.willConfirm, label: issueCredentialDto.label, autoAcceptCredential: issueCredentialDto.autoAcceptCredential, credentialType: issueCredentialDto.credentialType, isShortenUrl: issueCredentialDto.isShortenUrl, reuseConnection : issueCredentialDto.reuseConnection };
}
if (IssueCredentialType.JSONLD === issueCredentialDto.credentialType) {
payload = { credential: issueCredentialDto.credential, options: issueCredentialDto.options, comment: issueCredentialDto.comment, orgId: issueCredentialDto.orgId, protocolVersion: issueCredentialDto.protocolVersion, goalCode: issueCredentialDto.goalCode, parentThreadId: issueCredentialDto.parentThreadId, willConfirm: issueCredentialDto.willConfirm, label: issueCredentialDto.label, autoAcceptCredential: issueCredentialDto.autoAcceptCredential, credentialType: issueCredentialDto.credentialType, isShortenUrl: issueCredentialDto.isShortenUrl };
payload = { credential: issueCredentialDto.credential, options: issueCredentialDto.options, comment: issueCredentialDto.comment, orgId: issueCredentialDto.orgId, protocolVersion: issueCredentialDto.protocolVersion, goalCode: issueCredentialDto.goalCode, parentThreadId: issueCredentialDto.parentThreadId, willConfirm: issueCredentialDto.willConfirm, label: issueCredentialDto.label, autoAcceptCredential: issueCredentialDto.autoAcceptCredential, credentialType: issueCredentialDto.credentialType, isShortenUrl: issueCredentialDto.isShortenUrl, reuseConnection : issueCredentialDto.reuseConnection };
}

return this.sendNats(this.issuanceProxy, 'send-credential-create-offer-oob', payload);
Expand Down
7 changes: 7 additions & 0 deletions apps/api-gateway/src/verification/dto/request-proof.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,5 +332,12 @@ export class SendProofRequestPayload {
@IsOptional()
@IsNotEmpty({message:'Please provide the flag for shorten url.'})
isShortenUrl?: boolean;

@ApiPropertyOptional({ default: true })
@IsOptional()
@IsNotEmpty({ message: 'please provide valid value for reuseConnection' })
@IsBoolean({ message: 'reuseConnection must be a boolean' })
reuseConnection?: boolean;

}

6 changes: 4 additions & 2 deletions apps/connection/src/connection.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ export class ConnectionRepository {
async saveAgentConnectionInvitations(
connectionInvitation: string,
agentId: string,
orgId: string
orgId: string,
recipientKey: string
// eslint-disable-next-line camelcase
): Promise<agent_invitations> {
try {
Expand All @@ -57,7 +58,8 @@ export class ConnectionRepository {
orgId: String(orgId),
agentId,
connectionInvitation,
multiUse: true
multiUse: true,
recipientKey
}
});
return agentDetails;
Expand Down
16 changes: 10 additions & 6 deletions apps/connection/src/connection.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ export class ConnectionService {
goal,
goalCode,
handshake,
handshakeProtocols
handshakeProtocols,
recipientKey
} = payload;
try {
const agentDetails = await this.connectionRepository.getAgentEndPoint(orgId);
Expand All @@ -72,7 +73,8 @@ export class ConnectionService {
goal: goal || undefined,
goalCode: goalCode || undefined,
handshake: handshake || undefined,
handshakeProtocols: handshakeProtocols || undefined
handshakeProtocols: handshakeProtocols || undefined,
recipientKey:recipientKey || undefined
};

const orgAgentType = await this.connectionRepository.getOrgAgentType(agentDetails?.orgAgentTypeId);
Expand All @@ -83,18 +85,19 @@ export class ConnectionService {
apiKey = await this._getOrgAgentApiKey(orgId);
}
const createConnectionInvitation = await this._createConnectionInvitation(connectionPayload, url, apiKey);

const connectionInvitationUrl: string = createConnectionInvitation?.message?.invitationUrl;
const shortenedUrl: string = await this.storeConnectionObjectAndReturnUrl(
connectionInvitationUrl,
connectionPayload.multiUseInvitation
);

const recipientsKey = createConnectionInvitation?.message?.recipientKey || recipientKey;
const saveConnectionDetails = await this.connectionRepository.saveAgentConnectionInvitations(
shortenedUrl,
agentId,
orgId
orgId,
recipientsKey
);

const connectionDetailRecords: ConnectionResponseDetail = {
id: saveConnectionDetails.id,
orgId: saveConnectionDetails.orgId,
Expand All @@ -105,7 +108,8 @@ export class ConnectionService {
createdBy: saveConnectionDetails.createdBy,
lastChangedDateTime: saveConnectionDetails.lastChangedDateTime,
lastChangedBy: saveConnectionDetails.lastChangedBy,
recordId: createConnectionInvitation.message.outOfBandRecord.id
recordId: createConnectionInvitation.message.outOfBandRecord.id,
recipientKey:saveConnectionDetails.recipientKey
};
return connectionDetailRecords;
} catch (error) {
Expand Down
2 changes: 2 additions & 0 deletions apps/connection/src/interfaces/connection.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface IConnection {
handshake: string;
handshakeProtocols: string[];
orgId: string;
recipientKey?: string;
}
export interface IUserRequestInterface {
userId: string;
Expand Down Expand Up @@ -264,4 +265,5 @@ export interface ConnectionResponseDetail {
lastChangedDateTime: Date;
lastChangedBy: number;
recordId: string;
recipientKey:string;
}
18 changes: 18 additions & 0 deletions apps/issuance/src/issuance.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Injectable, InternalServerErrorException, Logger, NotFoundException } f
import { PrismaService } from '@credebl/prisma-service';
// eslint-disable-next-line camelcase
import {
agent_invitations,
credentials,
file_data,
file_upload,
Expand Down Expand Up @@ -70,6 +71,23 @@ export class IssuanceRepository {
}
}


async getRecipientKeyByOrgId(orgId: string): Promise<agent_invitations[]> {
try {
return this.prisma.agent_invitations.findMany({
where: {
orgId
},
orderBy: {
createDateTime: 'asc'
}
});
} catch (error) {
this.logger.error(`Error in getRecipientKey in issuance repository: ${error.message}`);
throw error;
}
}

async getAllIssuedCredentials(
user: IUserRequest,
orgId: string,
Expand Down
20 changes: 14 additions & 6 deletions apps/issuance/src/issuance.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { io } from 'socket.io-client';
import { IIssuedCredentialSearchParams, IssueCredentialType } from 'apps/api-gateway/src/issuance/interfaces';
import { IIssuedCredential } from '@credebl/common/interfaces/issuance.interface';
import { OOBIssueCredentialDto } from 'apps/api-gateway/src/issuance/dtos/issuance.dto';
import { organisation } from '@prisma/client';
import { agent_invitations, organisation } from '@prisma/client';


@Injectable()
Expand Down Expand Up @@ -150,7 +150,7 @@ export class IssuanceService {
async sendCredentialOutOfBand(payload: OOBIssueCredentialDto): Promise<{ response: object }> {
try {

const { orgId, credentialDefinitionId, comment, attributes, protocolVersion, credential, options, credentialType, isShortenUrl } = payload;
const { orgId, credentialDefinitionId, comment, attributes, protocolVersion, credential, options, credentialType, isShortenUrl, reuseConnection } = payload;
if (credentialType === IssueCredentialType.INDY) {
const schemadetailsResponse: SchemaDetails = await this.issuanceRepository.getCredentialDefinitionDetails(
credentialDefinitionId
Expand Down Expand Up @@ -181,8 +181,14 @@ export class IssuanceService {
}

const agentDetails = await this.issuanceRepository.getAgentEndPoint(orgId);
// eslint-disable-next-line camelcase

let recipientKey: string | undefined;
if (true === reuseConnection) {
const data: agent_invitations[] = await this.issuanceRepository.getRecipientKeyByOrgId(orgId);
if (data && 0 < data.length) {
const [firstElement] = data;
recipientKey = firstElement?.recipientKey ?? undefined;
}
}
const { agentEndPoint, organisation } = agentDetails;

if (!agentDetails) {
Expand Down Expand Up @@ -217,7 +223,8 @@ export class IssuanceService {
willConfirm: payload.willConfirm || undefined,
imageUrl: organisation?.logoUrl || payload?.imageUrl || undefined,
label: organisation?.name,
comment: comment || ''
comment: comment || '',
recipientKey:recipientKey || undefined
};

}
Expand All @@ -237,7 +244,8 @@ export class IssuanceService {
willConfirm: payload.willConfirm || undefined,
imageUrl: organisation?.logoUrl || payload?.imageUrl || undefined,
label: organisation?.name,
comment: comment || ''
comment: comment || '',
recipientKey:recipientKey || undefined
};
}
const credentialCreateOfferDetails = await this._outOfBandCredentialOffer(issueData, url, apiKey);
Expand Down
6 changes: 4 additions & 2 deletions apps/verification/src/interfaces/verification.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,10 @@ export interface ISendProofRequestPayload {
willConfirm?: boolean;
imageUrl?: string;
isShortenUrl?: boolean;
type?:string;
presentationDefinition?:IProofRequestPresentationDefinition;
type?:string;
presentationDefinition?:IProofRequestPresentationDefinition;
reuseConnection?: boolean;
recipientKey?:string;
}

export interface IWSendProofRequestPayload {
Expand Down
19 changes: 18 additions & 1 deletion apps/verification/src/repositories/verification.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ResponseMessages } from '@credebl/common/response-messages';
import { PrismaService } from '@credebl/prisma-service';
import { Injectable, Logger, NotFoundException } from '@nestjs/common';
// eslint-disable-next-line camelcase
import { org_agents, organisation, platform_config, presentations } from '@prisma/client';
import { agent_invitations, org_agents, organisation, platform_config, presentations } from '@prisma/client';
import { IProofPresentation } from '../interfaces/verification.interface';
import { IProofRequestSearchCriteria } from '../interfaces/verification.interface';
import { IUserRequest } from '@credebl/user-request/user-request.interface';
Expand Down Expand Up @@ -185,4 +185,21 @@ export class VerificationRepository {
throw error;
}
}

// eslint-disable-next-line camelcase
async getRecipientKeyByOrgId(orgId: string): Promise<agent_invitations[]> {
try {
return this.prisma.agent_invitations.findMany({
where: {
orgId
},
orderBy: {
createDateTime: 'asc' // or 'desc' for descending order
}
});
} catch (error) {
this.logger.error(`Error in getRecipientKey in verification repository: ${error.message}`);
throw error;
}
}
}
16 changes: 13 additions & 3 deletions apps/verification/src/verification.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { map } from 'rxjs/operators';
import { IGetAllProofPresentations, IProofRequestSearchCriteria, IGetProofPresentationById, IProofPresentation, IProofRequestPayload, IRequestProof, ISendProofRequestPayload, IVerifyPresentation, IVerifiedProofData, IPresentationExchangeProofRequestPayload} from './interfaces/verification.interface';
import { VerificationRepository } from './repositories/verification.repository';
import { CommonConstants } from '@credebl/common/common.constant';
import { org_agents, organisation, presentations } from '@prisma/client';
import { agent_invitations, org_agents, organisation, presentations } from '@prisma/client';
import { OrgAgentType } from '@credebl/enum/enum';
import { ResponseMessages } from '@credebl/common/response-messages';
import * as QRCode from 'qrcode';
Expand Down Expand Up @@ -358,13 +358,22 @@ export class VerificationService {
apiKey = await this._getOrgAgentApiKey(user.orgId);
}

const { isShortenUrl, type, ...updateOutOfBandRequestProof } = outOfBandRequestProof;
const { isShortenUrl, type, reuseConnection, ...updateOutOfBandRequestProof } = outOfBandRequestProof;
let recipientKey: string | undefined;
if (true === reuseConnection) {
const data: agent_invitations[] = await this.verificationRepository.getRecipientKeyByOrgId(user.orgId);
if (data && 0 < data.length) {
const [firstElement] = data;
recipientKey = firstElement?.recipientKey ?? undefined;
}
}
outOfBandRequestProof.autoAcceptProof = outOfBandRequestProof.autoAcceptProof || 'always';

let payload: IProofRequestPayload | IPresentationExchangeProofRequestPayload;

if (ProofRequestType.INDY === type) {
updateOutOfBandRequestProof.protocolVersion = updateOutOfBandRequestProof.protocolVersion || 'v1';
updateOutOfBandRequestProof.recipientKey = recipientKey || undefined;
payload = {
apiKey,
url,
Expand All @@ -389,7 +398,8 @@ export class VerificationService {
}
}
},
autoAcceptProof:outOfBandRequestProof.autoAcceptProof || 'always'
autoAcceptProof:outOfBandRequestProof.autoAcceptProof || 'always',
recipientKey:recipientKey || undefined
}
};
}
Expand Down
1 change: 1 addition & 0 deletions libs/common/src/interfaces/agent-service.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface InvitationMessage {
routingKeys: string[];
};
outOfBandRecord: OutOfBandRecord;
recipientKey?:string
};
}

Expand Down
1 change: 1 addition & 0 deletions libs/common/src/interfaces/connection.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ export interface IConnectionsListCount {
createdBy: number;
lastChangedDateTime: Date;
lastChangedBy: number;
recipientKey?:string;
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "agent_invitations" ADD COLUMN "recipientKey" TEXT;
1 change: 1 addition & 0 deletions libs/prisma-service/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ model agent_invitations {
lastChangedBy Int @default(1)
org_agents org_agents @relation(fields: [agentId], references: [id])
organisation organisation @relation(fields: [orgId], references: [id])
recipientKey String?
}

model connections {
Expand Down