Skip to content

Commit

Permalink
refactor: add the recipientKey in create invitation functionality
Browse files Browse the repository at this point in the history
Signed-off-by: KulkarniShashank <[email protected]>
Signed-off-by: tipusinghaw <[email protected]>
  • Loading branch information
KulkarniShashank authored and tipusinghaw committed Apr 30, 2024
1 parent 5dc99ac commit 07de941
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
20 changes: 18 additions & 2 deletions src/controllers/multi-tenancy/MultiTenancyController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -639,12 +639,27 @@ export class MultiTenancyController extends Controller {
public async createInvitation(
@Res() internalServerError: TsoaResponse<500, { message: string }>,
@Path('tenantId') tenantId: string,
@Body() config?: Omit<CreateOutOfBandInvitationConfig, 'routing' | 'appendedAttachments' | 'messages'> // props removed because of issues with serialization
@Body() config?: Omit<CreateOutOfBandInvitationConfig, 'routing'> & RecipientKeyOption // Remove routing property from type
) {
let outOfBandRecord: OutOfBandRecord | undefined
let finalConfig: Omit<CreateOutOfBandInvitationConfig, 'routing'> & RecipientKeyOption & { routing?: Routing } = {} // Initialize finalConfig

try {
await this.agent.modules.tenants.withTenantAgent({ tenantId }, async (tenantAgent) => {
outOfBandRecord = await tenantAgent.oob.createInvitation(config)
if (config?.recipientKey) {
const routing: Routing = {
// Initialize routing object
endpoints: tenantAgent.config.endpoints,
routingKeys: [],
recipientKey: Key.fromPublicKeyBase58(config.recipientKey, KeyType.Ed25519),
mediatorId: undefined,
}
finalConfig = { ...config, routing } // Assign finalConfig
} else {
finalConfig = { ...config, routing: await tenantAgent.mediationRecipient.getRouting({}) } // Assign finalConfig
}

outOfBandRecord = await tenantAgent.oob.createInvitation(finalConfig)
})

return {
Expand All @@ -655,6 +670,7 @@ export class MultiTenancyController extends Controller {
useDidSovPrefixWhereAllowed: this.agent.config.useDidSovPrefixWhereAllowed,
}),
outOfBandRecord: outOfBandRecord?.toJSON(),
...(finalConfig?.recipientKey ? {} : { recipientKey: finalConfig.routing?.recipientKey.publicKeyBase58 }), // Access recipientKey from routing
}
} catch (error) {
return internalServerError(500, { message: `something went wrong: ${error}` })
Expand Down
25 changes: 17 additions & 8 deletions src/controllers/outofband/OutOfBandController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { OutOfBandInvitationProps, OutOfBandRecordWithInvitationProps } from '../examples'
import type { AgentMessageType, RecipientKeyOption } from '../types'
import type { AgentMessageType, RecipientKeyOption, CreateInvitationOptions } from '../types'
import type { ConnectionRecordProps, CreateLegacyInvitationConfig, Routing } from '@credo-ts/core'

import {
Expand All @@ -14,12 +14,7 @@ import {
import { injectable } from 'tsyringe'

import { ConnectionRecordExample, outOfBandInvitationExample, outOfBandRecordExample, RecordId } from '../examples'
import {
AcceptInvitationConfig,
ReceiveInvitationByUrlProps,
ReceiveInvitationProps,
CreateInvitationOptions,
} from '../types'
import { AcceptInvitationConfig, ReceiveInvitationByUrlProps, ReceiveInvitationProps } from '../types'

import {
Body,
Expand Down Expand Up @@ -101,9 +96,22 @@ export class OutOfBandController extends Controller {
@Post('/create-invitation')
public async createInvitation(
@Res() internalServerError: TsoaResponse<500, { message: string }>,
@Body() config: CreateInvitationOptions // props removed because of issues with serialization
@Body() config: CreateInvitationOptions & RecipientKeyOption // props removed because of issues with serialization
) {
try {
let routing: Routing
if (config?.recipientKey) {
routing = {
endpoints: this.agent.config.endpoints,
routingKeys: [],
recipientKey: Key.fromPublicKeyBase58(config.recipientKey, KeyType.Ed25519),
mediatorId: undefined,
}
} else {
routing = await this.agent.mediationRecipient.getRouting({})
}

config.routing = routing
const outOfBandRecord = await this.agent.oob.createInvitation(config)
return {
invitationUrl: outOfBandRecord.outOfBandInvitation.toUrl({
Expand All @@ -113,6 +121,7 @@ export class OutOfBandController extends Controller {
useDidSovPrefixWhereAllowed: this.agent.config.useDidSovPrefixWhereAllowed,
}),
outOfBandRecord: outOfBandRecord.toJSON(),
...(config?.recipientKey ? {} : { recipientKey: routing.recipientKey.publicKeyBase58 }),
}
} catch (error) {
return internalServerError(500, { message: `something went wrong: ${error}` })
Expand Down

0 comments on commit 07de941

Please sign in to comment.