Skip to content

Commit 1d4c782

Browse files
feat: simplify wallet provisioning flow (detach did creation) (#1528)
* refactor: create shared agent wallet api Signed-off-by: pranalidhanavade <pranali.dhanavade@ayanworks.com> * refactor: changes in shared agent api to support wallet creation without did for shared agent Signed-off-by: pranalidhanavade <pranali.dhanavade@ayanworks.com> * refactor: org agent setup logic Signed-off-by: pranalidhanavade <pranali.dhanavade@ayanworks.com> * refactor: agent spinup status issue Signed-off-by: pranalidhanavade <pranali.dhanavade@ayanworks.com> * refactor: agent spinup flow with shared agent Signed-off-by: pranalidhanavade <pranali.dhanavade@ayanworks.com> * removed unnecessary comments Signed-off-by: pranalidhanavade <pranali.dhanavade@ayanworks.com> * resolve: coderabbit comments Signed-off-by: pranalidhanavade <pranali.dhanavade@ayanworks.com> * resolve: sonarlint comments Signed-off-by: pranalidhanavade <pranali.dhanavade@ayanworks.com> * resolve: sonarlint warnings Signed-off-by: pranalidhanavade <pranali.dhanavade@ayanworks.com> * resolve: coderabbit warnings Signed-off-by: pranalidhanavade <pranali.dhanavade@ayanworks.com> * resolve: coderabbit warning Signed-off-by: pranalidhanavade <pranali.dhanavade@ayanworks.com> * resolve: coderabbit warnings Signed-off-by: pranalidhanavade <pranali.dhanavade@ayanworks.com> * revert: changes related to create invitation URL Signed-off-by: pranalidhanavade <pranali.dhanavade@ayanworks.com> * resolved: comments on PR Signed-off-by: pranalidhanavade <pranali.dhanavade@ayanworks.com> * fix: comments on PR Signed-off-by: pranalidhanavade <pranali.dhanavade@ayanworks.com> * fix: resolved comments on PR Signed-off-by: pranalidhanavade <pranali.dhanavade@ayanworks.com> --------- Signed-off-by: pranalidhanavade <pranali.dhanavade@ayanworks.com>
1 parent 5137cc8 commit 1d4c782

File tree

6 files changed

+154
-143
lines changed

6 files changed

+154
-143
lines changed

apps/agent-service/src/agent-service.controller.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
1-
import { Controller } from '@nestjs/common';
2-
import { MessagePattern } from '@nestjs/microservices';
3-
import { AgentServiceService } from './agent-service.service';
41
import {
2+
IAgentConfigure,
3+
IAgentProofRequest,
4+
IAgentSpinupDto,
55
IAgentStatus,
66
IConnectionDetails,
7-
IUserRequestInterface,
8-
ISendProofRequestPayload,
9-
IAgentSpinUpSatus,
7+
ICreateConnectionInvitation,
8+
IDidCreate,
109
IGetCredDefAgentRedirection,
1110
IGetSchemaAgentRedirection,
12-
IAgentSpinupDto,
1311
IIssuanceCreateOffer,
12+
IOutOfBandCredentialOffer,
13+
ISendProofRequestPayload,
14+
IStoreAgent,
15+
IStoreOrgAgentDetails,
1416
ITenantCredDef,
1517
ITenantDto,
16-
ITenantSchema,
17-
IOutOfBandCredentialOffer,
18-
IAgentProofRequest,
19-
IDidCreate,
20-
IWallet,
2118
ITenantRecord,
22-
ICreateConnectionInvitation,
23-
IStoreAgent,
24-
IAgentConfigure
19+
ITenantSchema,
20+
IUserRequestInterface,
21+
IWallet
2522
} from './interface/agent-service.interface';
26-
import { user } from '@prisma/client';
27-
import { InvitationMessage } from '@credebl/common/interfaces/agent-service.interface';
23+
24+
import { AgentServiceService } from './agent-service.service';
2825
import { AgentSpinUpStatus } from '@credebl/enum/enum';
26+
import { Controller } from '@nestjs/common';
27+
import { InvitationMessage } from '@credebl/common/interfaces/agent-service.interface';
28+
import { MessagePattern } from '@nestjs/microservices';
2929
import { SignDataDto } from '../../api-gateway/src/agent-service/dto/agent-service.dto';
30+
import { user } from '@prisma/client';
3031

3132
@Controller()
3233
export class AgentServiceController {
@@ -49,7 +50,7 @@ export class AgentServiceController {
4950
async createTenant(payload: {
5051
createTenantDto: ITenantDto;
5152
user: IUserRequestInterface;
52-
}): Promise<IAgentSpinUpSatus> {
53+
}): Promise<IStoreOrgAgentDetails> {
5354
return this.agentServiceService.createTenant(payload.createTenantDto, payload.user);
5455
}
5556

apps/agent-service/src/agent-service.service.ts

Lines changed: 36 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import {
3232
ISendProofRequestPayload,
3333
IIssuanceCreateOffer,
3434
IOutOfBandCredentialOffer,
35-
IAgentSpinUpSatus,
3635
ICreateTenant,
3736
IAgentStatus,
3837
ICreateOrgAgent,
@@ -125,15 +124,15 @@ export class AgentServiceService {
125124
}> {
126125
let agentProcess: ICreateOrgAgent;
127126
try {
128-
await this.processWalletProvision(agentSpinupDto, user);
129-
return { agentSpinupStatus: AgentSpinUpStatus.PROCESSED };
127+
await this.provisionWallet(agentSpinupDto, user);
128+
return { agentSpinupStatus: AgentSpinUpStatus.DID_CREATED };
130129
} catch (error) {
131130
this.handleErrorOnWalletProvision(agentSpinupDto, error, agentProcess);
132131
throw new RpcException(error.response ?? error);
133132
}
134133
}
135134

136-
private async processWalletProvision(agentSpinupDto: IAgentSpinupDto, user: IUserRequestInterface): Promise<void> {
135+
private async provisionWallet(agentSpinupDto: IAgentSpinupDto, user: IUserRequestInterface): Promise<void> {
137136
let platformAdminUser;
138137
let userId: string;
139138
let agentProcess: ICreateOrgAgent;
@@ -143,6 +142,7 @@ export class AgentServiceService {
143142
this.agentServiceRepository.getPlatformConfigDetails(),
144143
this.agentServiceRepository.getAgentTypeDetails(),
145144
this.agentServiceRepository.getLedgerDetails(
145+
// TODO: Do we want to get first element from ledgerName
146146
agentSpinupDto.ledgerName ? agentSpinupDto.ledgerName : [Ledgers.Indicio_Demonet]
147147
)
148148
]);
@@ -181,20 +181,13 @@ export class AgentServiceService {
181181
// Get genesis URL and ledger details
182182
const ledgerDetails = await this.agentServiceRepository.getGenesisUrl(agentSpinupDto.ledgerId);
183183

184-
if (AgentSpinUpStatus.PROCESSED === getOrgAgent?.agentSpinUpStatus) {
185-
throw new BadRequestException(ResponseMessages.agent.error.walletAlreadyProcessing, {
184+
if (AgentSpinUpStatus.WALLET_CREATED === getOrgAgent?.agentSpinUpStatus) {
185+
throw new BadRequestException(ResponseMessages.agent.error.walletAlreadyCreated, {
186186
cause: new Error(),
187187
description: ResponseMessages.errorMessages.badRequest
188188
});
189189
}
190190

191-
if (AgentSpinUpStatus.COMPLETED === getOrgAgent?.agentSpinUpStatus) {
192-
throw new ConflictException(ResponseMessages.agent.error.walletAlreadyCreated, {
193-
cause: new Error(),
194-
description: ResponseMessages.errorMessages.conflict
195-
});
196-
}
197-
198191
if (!agentSpinupDto.orgId) {
199192
if (platformAdminOrgDetails) {
200193
agentSpinupDto.orgId = platformAdminOrgDetails;
@@ -228,10 +221,10 @@ export class AgentServiceService {
228221
const socket: Socket = await this.initSocketConnection(`${process.env.SOCKET_HOST}`);
229222
this.emitAgentSpinupInitiatedEvent(agentSpinupDto, socket);
230223

231-
const agentSpinUpStatus = AgentSpinUpStatus.PROCESSED;
232-
agentProcess = await this.createOrgAgent(agentSpinUpStatus, userId);
224+
const agentSpinUpStatus = AgentSpinUpStatus.WALLET_CREATED;
225+
agentProcess = await this.createOrgAgent(agentSpinUpStatus, userId, agentSpinupDto.orgId);
233226

234-
// AFJ agent spin-up
227+
// Credo agent spin-up
235228
this._agentSpinup(
236229
walletProvisionPayload,
237230
agentSpinupDto,
@@ -297,7 +290,7 @@ export class AgentServiceService {
297290
const storeAgentConfig = await this.agentServiceRepository.storeOrgAgentDetails({
298291
did,
299292
isDidPublic: true,
300-
agentSpinUpStatus: AgentSpinUpStatus.COMPLETED,
293+
agentSpinUpStatus: AgentSpinUpStatus.DID_CREATED,
301294
walletName,
302295
agentsTypeId,
303296
orgId,
@@ -440,9 +433,9 @@ export class AgentServiceService {
440433
return socket;
441434
}
442435

443-
async createOrgAgent(agentSpinUpStatus: AgentSpinUpStatus, userId: string): Promise<ICreateOrgAgent> {
436+
async createOrgAgent(agentSpinUpStatus: AgentSpinUpStatus, userId: string, orgId: string): Promise<ICreateOrgAgent> {
444437
try {
445-
const agentProcess = await this.agentServiceRepository.createOrgAgent(agentSpinUpStatus, userId);
438+
const agentProcess = await this.agentServiceRepository.createOrgAgent(agentSpinUpStatus, userId, orgId);
446439
this.logger.log(`Organization agent created with status: ${agentSpinUpStatus}`);
447440
return agentProcess;
448441
} catch (error) {
@@ -487,7 +480,7 @@ export class AgentServiceService {
487480
ledgerId: string[],
488481
agentProcess: ICreateOrgAgent
489482
): Promise<void> {
490-
let ledgerIdData = [];
483+
let ledgerIdData;
491484

492485
try {
493486
if (agentSpinupDto.method !== DidMethod.KEY && agentSpinupDto.method !== DidMethod.WEB) {
@@ -609,7 +602,7 @@ export class AgentServiceService {
609602
did: '',
610603
verkey: '',
611604
isDidPublic: true,
612-
agentSpinUpStatus: AgentSpinUpStatus.COMPLETED,
605+
agentSpinUpStatus: AgentSpinUpStatus.DID_CREATED,
613606
walletName: payload.walletName,
614607
agentsTypeId: payload.agentsTypeId,
615608
orgId: payload.orgId,
@@ -725,32 +718,20 @@ export class AgentServiceService {
725718
* @param user
726719
* @returns Get agent status
727720
*/
728-
async createTenant(payload: ITenantDto, user: IUserRequestInterface): Promise<IAgentSpinUpSatus> {
721+
async createTenant(payload: ITenantDto, user: IUserRequestInterface): Promise<IStoreOrgAgentDetails> {
729722
try {
730-
const agentStatusResponse = {
731-
agentSpinupStatus: AgentSpinUpStatus.PROCESSED
732-
};
733723
const getOrgAgent = await this.agentServiceRepository.getAgentDetails(payload.orgId);
734724

735-
if (AgentSpinUpStatus.COMPLETED === getOrgAgent?.agentSpinUpStatus) {
736-
this.logger.error(`Your wallet is already been created.`);
725+
if (AgentSpinUpStatus.WALLET_CREATED === getOrgAgent?.agentSpinUpStatus) {
726+
this.logger.error(`Your wallet is already created.`);
737727
throw new ConflictException(ResponseMessages.agent.error.walletAlreadyCreated, {
738728
cause: new Error(),
739729
description: ResponseMessages.errorMessages.conflict
740730
});
741731
}
742-
743-
if (AgentSpinUpStatus.PROCESSED === getOrgAgent?.agentSpinUpStatus) {
744-
this.logger.error(`Your wallet is already processing.`);
745-
throw new ConflictException(ResponseMessages.agent.error.walletAlreadyProcessing, {
746-
cause: new Error(),
747-
description: ResponseMessages.errorMessages.conflict
748-
});
749-
}
750-
751732
// Create tenant
752-
this._createTenant(payload, user);
753-
return agentStatusResponse;
733+
const createdTenant = await this._createTenant(payload, user);
734+
return createdTenant;
754735
} catch (error) {
755736
this.logger.error(`error in create tenant : ${JSON.stringify(error)}`);
756737
throw new RpcException(error.response ? error.response : error);
@@ -763,7 +744,7 @@ export class AgentServiceService {
763744
* @param user
764745
* @returns Get agent status
765746
*/
766-
async _createTenant(payload: ITenantDto, user: IUserRequestInterface): Promise<void> {
747+
async _createTenant(payload: ITenantDto, user: IUserRequestInterface): Promise<IStoreOrgAgentDetails> {
767748
let agentProcess;
768749
let ledgerIdData = [];
769750
try {
@@ -785,26 +766,27 @@ export class AgentServiceService {
785766
}
786767
ledgerIdData = await this.agentServiceRepository.getLedgerDetails(ledger);
787768

788-
const agentSpinUpStatus = AgentSpinUpStatus.PROCESSED;
769+
const agentSpinUpStatus = AgentSpinUpStatus.WALLET_CREATED;
789770

790771
// Create and stored agent details
791-
agentProcess = await this.agentServiceRepository.createOrgAgent(agentSpinUpStatus, user?.id);
772+
agentProcess = await this.agentServiceRepository.createOrgAgent(agentSpinUpStatus, user?.id, payload.orgId);
792773

793774
// Get platform admin details
794775
const platformAdminSpinnedUp = await this.getPlatformAdminAndNotify(payload.clientSocketId);
795776

796777
payload.endpoint = platformAdminSpinnedUp.org_agents[0].agentEndPoint;
797-
// Create tenant wallet and DID
778+
// Create tenant wallet
798779
const tenantDetails = await this.createTenantAndNotify(payload, platformAdminSpinnedUp);
799-
if (!tenantDetails?.walletResponseDetails?.id || !tenantDetails?.DIDCreationOption?.did) {
780+
781+
if (!tenantDetails?.walletResponseDetails?.id) {
800782
this.logger.error(`Error in getting wallet id and wallet did`);
801783
throw new NotFoundException(ResponseMessages.agent.error.notAbleToSpinUpAgent, {
802784
cause: new Error(),
803785
description: ResponseMessages.errorMessages.notFound
804786
});
805787
}
806788

807-
if (AgentSpinUpStatus.COMPLETED !== platformAdminSpinnedUp.org_agents[0].agentSpinUpStatus) {
789+
if (AgentSpinUpStatus.DID_CREATED !== platformAdminSpinnedUp.org_agents[0].agentSpinUpStatus) {
808790
this.logger.error(`Platform-admin agent is not spun-up`);
809791
throw new NotFoundException(ResponseMessages.agent.error.platformAdminNotAbleToSpinp, {
810792
cause: new Error(),
@@ -816,50 +798,34 @@ export class AgentServiceService {
816798
// Get agent type details
817799
const agentTypeId = await this.agentServiceRepository.getAgentTypeId(AgentType.AFJ);
818800
const storeOrgAgentData: IStoreOrgAgentDetails = {
819-
did: tenantDetails.DIDCreationOption.did,
820-
isDidPublic: true,
821-
didDoc: tenantDetails.DIDCreationOption.didDocument || tenantDetails.DIDCreationOption.didDoc, //changed the didDoc into didDocument
822-
agentSpinUpStatus: AgentSpinUpStatus.COMPLETED,
801+
agentSpinUpStatus: AgentSpinUpStatus.WALLET_CREATED,
823802
agentsTypeId: agentTypeId,
824803
orgId: payload.orgId,
825804
agentEndPoint: platformAdminSpinnedUp.org_agents[0].agentEndPoint,
826805
orgAgentTypeId,
827806
tenantId: tenantDetails.walletResponseDetails['id'],
828807
walletName: payload.label,
829-
ledgerId: ledgerIdData.map((item) => item.id),
830808
id: agentProcess?.id,
831809
apiKey: await this.commonService.dataEncryption(tenantDetails.walletResponseDetails['token'])
832810
};
811+
833812
// Get organization data
834813
const getOrganization = await this.agentServiceRepository.getOrgDetails(payload.orgId);
835814

836815
this.notifyClientSocket('agent-spinup-process-completed', payload.clientSocketId);
837816

838817
const orgAgentDetails = await this.agentServiceRepository.storeOrgAgentDetails(storeOrgAgentData);
839818

840-
const createdDidDetails = {
841-
orgId: payload.orgId,
842-
did: tenantDetails.DIDCreationOption.did,
843-
didDocument: tenantDetails.DIDCreationOption.didDocument || tenantDetails.DIDCreationOption.didDoc,
844-
isPrimaryDid: true,
845-
orgAgentId: orgAgentDetails.id,
846-
userId: user.id
847-
};
848-
849-
await this.agentServiceRepository.storeDidDetails(createdDidDetails);
850-
851819
this.notifyClientSocket('invitation-url-creation-started', payload.clientSocketId);
852820

853821
// Create the legacy connection invitation
854822
await this._createConnectionInvitation(payload.orgId, user, getOrganization.name);
855823

856824
this.notifyClientSocket('invitation-url-creation-success', payload.clientSocketId);
825+
826+
return orgAgentDetails;
857827
} catch (error) {
858828
this.handleError(error, payload.clientSocketId);
859-
860-
if (agentProcess && agentProcess?.id) {
861-
this.agentServiceRepository.removeOrgAgent(agentProcess?.id);
862-
}
863829
throw error;
864830
}
865831
}
@@ -913,7 +879,6 @@ export class AgentServiceService {
913879
}
914880
}
915881
}
916-
917882
const getApiKey = await this.getOrgAgentApiKey(orgId);
918883
const url = this.constructUrl(agentDetails);
919884

@@ -944,6 +909,9 @@ export class AgentServiceService {
944909
if (isPrimaryDid) {
945910
await this.setPrimaryDidAndLedger(orgId, storeDidDetails, createDidPayload.network, createDidPayload.method);
946911
}
912+
if (agentDetails.agentSpinUpStatus === AgentSpinUpStatus.WALLET_CREATED) {
913+
await this.agentServiceRepository.updateAgentSpinupStatus(orgId);
914+
}
947915

948916
return storeDidDetails;
949917
} catch (error) {
@@ -1102,20 +1070,12 @@ export class AgentServiceService {
11021070
platformAdminSpinnedUp.org_agents[0].agentEndPoint,
11031071
getDcryptedToken
11041072
);
1105-
if (!walletResponseDetails && !walletResponseDetails.id && !walletResponseDetails.token) {
1073+
if (!walletResponseDetails || !walletResponseDetails.id || !walletResponseDetails.token) {
11061074
throw new InternalServerErrorException('Error while creating the wallet');
11071075
}
1108-
const didCreateOption = {
1109-
didPayload: WalletSetupPayload,
1110-
agentEndpoint: platformAdminSpinnedUp.org_agents[0].agentEndPoint,
1111-
apiKey: walletResponseDetails.token
1076+
return {
1077+
walletResponseDetails
11121078
};
1113-
const DIDCreationOption = await this._createDID(didCreateOption);
1114-
if (!DIDCreationOption) {
1115-
throw new InternalServerErrorException('Error while creating the wallet');
1116-
}
1117-
1118-
return { walletResponseDetails, DIDCreationOption };
11191079
}
11201080
//
11211081

0 commit comments

Comments
 (0)