Skip to content

Commit

Permalink
Merge pull request #103 from credebl/refactor/create-invitation-response
Browse files Browse the repository at this point in the history
refactor: add the recipientKey in create invitation functionality
  • Loading branch information
pallavighule authored Apr 29, 2024
2 parents e913052 + db5f1a1 commit 5a2df6e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 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
20 changes: 18 additions & 2 deletions src/events/CredentialEvents.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { RestMultiTenantAgentModules } from '../cliAgent'
import type { ServerConfig } from '../utils/ServerConfig'
import type { Agent, CredentialStateChangedEvent } from '@credo-ts/core'

Expand All @@ -6,11 +7,26 @@ import { CredentialEventTypes } from '@credo-ts/core'
import { sendWebSocketEvent } from './WebSocketEvents'
import { sendWebhookEvent } from './WebhookEvent'

export const credentialEvents = async (agent: Agent, config: ServerConfig) => {
export const credentialEvents = async (agent: Agent<RestMultiTenantAgentModules>, config: ServerConfig) => {
agent.events.on(CredentialEventTypes.CredentialStateChanged, async (event: CredentialStateChangedEvent) => {
const record = event.payload.credentialRecord
const body = { ...record.toJSON(), ...event.metadata }

const body: Record<string, unknown> = {
...record.toJSON(),
...event.metadata,
outOfBandId: null,
}

if (event.metadata.contextCorrelationId !== 'default' && record?.connectionId) {
await agent.modules.tenants.withTenantAgent(
{ tenantId: event.metadata.contextCorrelationId },
async (tenantAgent) => {
const connectionRecord = await tenantAgent.connections.findById(record.connectionId!)

body.outOfBandId = connectionRecord?.outOfBandId
}
)
}
// Only send webhook if webhook url is configured
if (config.webhookUrl) {
await sendWebhookEvent(config.webhookUrl + '/credentials', body, agent.config.logger)
Expand Down

0 comments on commit 5a2df6e

Please sign in to comment.