import { NodeProfile } from '../../@types/nodeProfile'; import { IDENTITY_SEPARATOR } from '../../env/nodeProfile'; import { API } from '../API'; import { HTTP_METHOD } from '../httpMethod'; import { makeAuthenticatedRequest } from '../loginUtil'; import scenarioContext from '../scenarioContext'; export async function checkForIdentitySwitching( party: string, node: string, nodeProfile: NodeProfile, role: string ): Promise { if (scenarioContext().isPartyScenariosPresent()) { const identityX500Name = scenarioContext().isScenarioPartyAChild(party) ? generateChildIdentityName( nodeProfile.name, nodeProfile.childIdentities[scenarioContext().scenario[party].split(IDENTITY_SEPARATOR)[1]] ) : nodeProfile.name; logger.info('Switching identity..'); await switchIdentity(identityX500Name, node, role); } else { await switchIdentity(nodeProfile.name, node, role); } } export async function switchIdentity(identityX500Name: string, node: string, role: string): Promise { await makeAuthenticatedRequest(node, role, API.IDENTITY, HTTP_METHOD.PUT, identityX500Name); } export function generateChildIdentityName(nodeX500Name: string, identity: string): string { if (identity) { return nodeX500Name .split(',') .map((property) => { return getX500Property(property, identity); }) .join(','); } return nodeX500Name; } export function getX500Property(property: string, identity: string): string { const propKey = property.split('=')[0]; if (propKey.trim() == 'C') { return property; } else if (propKey.trim() == 'L') { return `${propKey}=${identity} - Location`; } else { return `${propKey}=${identity}`; } }