Skip to content

Commit 9deb93a

Browse files
authored
refactor: token API for tenant and create keypair (#1380)
* fix: schema by id Signed-off-by: Tipu_Singh <[email protected]> * fix: schema by id Signed-off-by: Tipu_Singh <[email protected]> * fix: duplicate URL creation Signed-off-by: Tipu_Singh <[email protected]> * fix: added await in common method Signed-off-by: Tipu_Singh <[email protected]> * fix: added logic for get token Signed-off-by: Tipu_Singh <[email protected]> * fix: polygon routes Signed-off-by: Tipu_Singh <[email protected]> --------- Signed-off-by: Tipu_Singh <[email protected]>
1 parent 2593951 commit 9deb93a

File tree

4 files changed

+27
-33
lines changed

4 files changed

+27
-33
lines changed

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,17 +1047,15 @@ export class AgentServiceService {
10471047
*/
10481048
async createSecp256k1KeyPair(orgId: string): Promise<object> {
10491049
try {
1050-
const orgAgentDetails = await this.agentServiceRepository.getAgentApiKey(orgId);
1051-
if (!orgAgentDetails) {
1052-
throw new NotFoundException(ResponseMessages.agent.error.orgAgentNotFound, {
1053-
cause: new Error(),
1054-
description: ResponseMessages.errorMessages.notFound
1055-
});
1056-
}
1057-
const getDcryptedToken = await this.commonService.decryptPassword(orgAgentDetails.apiKey);
1050+
const platformAdminSpinnedUp = await this.agentServiceRepository.platformAdminAgent(
1051+
CommonConstants.PLATFORM_ADMIN_ORG
1052+
);
10581053

1059-
const url = `${orgAgentDetails.agentEndPoint}${CommonConstants.CREATE_POLYGON_SECP256k1_KEY}`;
1054+
const getPlatformAgentEndPoint = platformAdminSpinnedUp.org_agents[0].agentEndPoint;
1055+
const getDcryptedToken = await this.commonService.decryptPassword(platformAdminSpinnedUp?.org_agents[0].apiKey);
10601056

1057+
const url = `${getPlatformAgentEndPoint}${CommonConstants.CREATE_POLYGON_SECP256k1_KEY}`;
1058+
this.logger.log(`Creating Secp256k1 key pair at URL: ${url}`);
10611059
const createKeyPairResponse = await this.commonService.httpPost(
10621060
url,
10631061
{},
@@ -1881,7 +1879,7 @@ export class AgentServiceService {
18811879
}
18821880
const walletDetails: WalletDetails = {
18831881
agentEndPoint: platformAdminSpinnedUp.org_agents[0]?.agentEndPoint,
1884-
apiKey: platformAdminSpinnedUp.org_agents[0]?.apiKey,
1882+
apiKey: await this.commonService.decryptPassword(platformAdminSpinnedUp.org_agents[0]?.apiKey),
18851883
tenantId: orgAgentDetails.tenantId,
18861884
orgId: orgAgentDetails.orgId
18871885
};

apps/ledger/src/schema/schema.service.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -297,17 +297,7 @@ export class SchemaService extends BaseService {
297297
description: ResponseMessages.errorMessages.badRequest
298298
});
299299
}
300-
301-
const getAgentDetails = await this.schemaRepository.getAgentType(orgId);
302-
const orgAgentType = await this.schemaRepository.getOrgAgentType(getAgentDetails.org_agents[0].orgAgentTypeId);
303-
let url;
304-
if (OrgAgentType.DEDICATED === orgAgentType) {
305-
url = `${agentEndPoint}${CommonConstants.DEDICATED_CREATE_POLYGON_W3C_SCHEMA}`;
306-
} else if (OrgAgentType.SHARED === orgAgentType) {
307-
const { tenantId } = await this.schemaRepository.getAgentDetailsByOrgId(orgId);
308-
url = `${agentEndPoint}${CommonConstants.SHARED_CREATE_POLYGON_W3C_SCHEMA}${tenantId}`;
309-
}
310-
300+
const url = `${agentEndPoint}${CommonConstants.CREATE_POLYGON_W3C_SCHEMA}`;
311301
const schemaObject = await w3cSchemaBuilder(attributes, schemaName, description);
312302
if (!schemaObject) {
313303
throw new BadRequestException(ResponseMessages.schema.error.schemaBuilder, {

libs/common/src/common.constant.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ export enum CommonConstants {
8787
URL_SCHM_GET_CRED_DEF_BY_ID = '/anoncreds/credential-definitions/#',
8888

8989
// POLYGON BASED W3C SCHEMAS
90-
DEDICATED_CREATE_POLYGON_W3C_SCHEMA = '/polygon/create-schema',
91-
SHARED_CREATE_POLYGON_W3C_SCHEMA = '/multi-tenancy/polygon-w3c/schema/',
90+
CREATE_POLYGON_W3C_SCHEMA = '/polygon/create-schema',
9291

9392
// SHARED AGENT
9493
URL_SHAGENT_CREATE_TENANT = '/multi-tenancy/create-tenant',

libs/common/src/common.service.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -311,25 +311,25 @@ export class CommonService {
311311
}
312312

313313
async getBaseAgentToken(agentEndPoint: string, apiKey: string): Promise<string> {
314-
const agentBaseWalletDetils = await this.httpPost(
315-
`${process.env.API_GATEWAY_PROTOCOL}://${agentEndPoint}${CommonConstants.URL_AGENT_TOKEN}`,
316-
'',
317-
{
318-
headers: {
319-
Accept: 'application/json',
320-
Authorization: apiKey
321-
}
314+
const normalizedBaseUrl = await this.normalizeUrlWithProtocol(agentEndPoint);
315+
this.logger.log(`Fetching base agent token from ${normalizedBaseUrl}`);
316+
const agentBaseWalletDetils = await this.httpPost(`${normalizedBaseUrl}${CommonConstants.URL_AGENT_TOKEN}`, '', {
317+
headers: {
318+
Accept: 'application/json',
319+
Authorization: apiKey
322320
}
323-
);
321+
});
324322
if (!agentBaseWalletDetils) {
325323
throw new NotFoundException(ResponseMessages.common.error.fetchBaseWalletToken);
326324
}
327325
return agentBaseWalletDetils.token;
328326
}
329327

330328
async getTenantWalletToken(agentEndPoint: string, apiKey: string, tenantId: string): Promise<string> {
329+
const normalizedBaseUrl = await this.normalizeUrlWithProtocol(agentEndPoint);
330+
this.logger.log(`Fetching tenant wallet token for tenantId: ${tenantId} from ${normalizedBaseUrl}`);
331331
const tenantWalletDetails = await this.httpPost(
332-
`${process.env.API_GATEWAY_PROTOCOL}://${agentEndPoint}${CommonConstants.URL_SHARED_WALLET_TOKEN}${tenantId}`,
332+
`${normalizedBaseUrl}${CommonConstants.URL_SHARED_WALLET_TOKEN}${tenantId}`,
333333
{},
334334
{
335335
headers: {
@@ -343,4 +343,11 @@ export class CommonService {
343343
}
344344
return tenantWalletDetails.token;
345345
}
346+
347+
async normalizeUrlWithProtocol(baseUrl: string): Promise<string> {
348+
if (baseUrl.startsWith('http://') || baseUrl.startsWith('https://')) {
349+
return baseUrl;
350+
}
351+
return `${process.env.API_GATEWAY_PROTOCOL}://${baseUrl}`;
352+
}
346353
}

0 commit comments

Comments
 (0)