diff --git a/packages/core/src/agent/Agent.ts b/packages/core/src/agent/Agent.ts index 49c9e37050..ce9a324b19 100644 --- a/packages/core/src/agent/Agent.ts +++ b/packages/core/src/agent/Agent.ts @@ -42,7 +42,6 @@ import { WalletModule } from '../wallet/WalletModule' import { WalletError } from '../wallet/error' import { AgentConfig } from './AgentConfig' -import { AgentContext } from './AgentContext' import { Dispatcher } from './Dispatcher' import { EnvelopeService } from './EnvelopeService' import { EventEmitter } from './EventEmitter' @@ -50,6 +49,7 @@ import { AgentEventTypes } from './Events' import { MessageReceiver } from './MessageReceiver' import { MessageSender } from './MessageSender' import { TransportService } from './TransportService' +import { AgentContext, DefaultAgentContextProvider } from './context' export class Agent { protected agentConfig: AgentConfig @@ -138,8 +138,9 @@ export class Agent { .pipe( takeUntil(this.stop$), concatMap((e) => - this.messageReceiver.receiveMessage(this.agentContext, e.payload.message, { + this.messageReceiver.receiveMessage(e.payload.message, { connection: e.payload.connection, + contextCorrelationId: e.payload.contextCorrelationId, }) ) ) @@ -269,8 +270,18 @@ export class Agent { return this.agentContext.wallet.publicDid } + /** + * Receive a message. This should mainly be used for receiving connection-less messages. + * + * If you want to receive messages that originated from e.g. a transport make sure to use the {@link MessageReceiver} + * for this. The `receiveMessage` method on the `Agent` class will associate the current context to the message, which + * may not be what should happen (e.g. in case of multi tenancy). + */ public async receiveMessage(inboundMessage: unknown, session?: TransportSession) { - return await this.messageReceiver.receiveMessage(this.agentContext, inboundMessage, { session }) + return await this.messageReceiver.receiveMessage(inboundMessage, { + session, + contextCorrelationId: this.agentContext.contextCorrelationId, + }) } public get injectionContainer() { @@ -368,6 +379,16 @@ export class Agent { W3cVcModule ) - dependencyManager.registerInstance(AgentContext, new AgentContext({ dependencyManager })) + // TODO: contextCorrelationId for base wallet + // Bind the default agent context to the container for use in modules etc. + dependencyManager.registerInstance( + AgentContext, + new AgentContext({ dependencyManager, contextCorrelationId: 'default' }) + ) + + // If no agent context provider has been registered we use the default agent context provider. + if (!this.dependencyManager.isRegistered(InjectionSymbols.AgentContextProvider)) { + this.dependencyManager.registerSingleton(InjectionSymbols.AgentContextProvider, DefaultAgentContextProvider) + } } } diff --git a/packages/core/src/agent/AgentContext.ts b/packages/core/src/agent/AgentContext.ts deleted file mode 100644 index a8e176d67f..0000000000 --- a/packages/core/src/agent/AgentContext.ts +++ /dev/null @@ -1,32 +0,0 @@ -import type { DependencyManager } from '../plugins' -import type { Wallet } from '../wallet' - -import { InjectionSymbols } from '../constants' - -import { AgentConfig } from './AgentConfig' - -export class AgentContext { - /** - * Dependency manager holds all dependencies for the current context. Possibly a child of a parent dependency manager, - * in which case all singleton dependencies from the parent context are also available to this context. - */ - public readonly dependencyManager: DependencyManager - - public constructor({ dependencyManager }: { dependencyManager: DependencyManager }) { - this.dependencyManager = dependencyManager - } - - /** - * Convenience method to access the agent config for the current context. - */ - public get config() { - return this.dependencyManager.resolve(AgentConfig) - } - - /** - * Convenience method to access the wallet for the current context. - */ - public get wallet() { - return this.dependencyManager.resolve(InjectionSymbols.Wallet) - } -} diff --git a/packages/core/src/agent/EnvelopeService.ts b/packages/core/src/agent/EnvelopeService.ts index d2ca8e4e51..cd50b22fc0 100644 --- a/packages/core/src/agent/EnvelopeService.ts +++ b/packages/core/src/agent/EnvelopeService.ts @@ -1,6 +1,6 @@ import type { EncryptedMessage, PlaintextMessage } from '../types' -import type { AgentContext } from './AgentContext' import type { AgentMessage } from './AgentMessage' +import type { AgentContext } from './context' import { InjectionSymbols } from '../constants' import { Key, KeyType } from '../crypto' diff --git a/packages/core/src/agent/EventEmitter.ts b/packages/core/src/agent/EventEmitter.ts index 284dcc1709..3dfe6205b3 100644 --- a/packages/core/src/agent/EventEmitter.ts +++ b/packages/core/src/agent/EventEmitter.ts @@ -1,5 +1,5 @@ -import type { AgentContext } from './AgentContext' import type { BaseEvent } from './Events' +import type { AgentContext } from './context' import type { EventEmitter as NativeEventEmitter } from 'events' import { fromEventPattern, Subject } from 'rxjs' diff --git a/packages/core/src/agent/Events.ts b/packages/core/src/agent/Events.ts index f6bc64a7bb..9c34620ca4 100644 --- a/packages/core/src/agent/Events.ts +++ b/packages/core/src/agent/Events.ts @@ -16,6 +16,7 @@ export interface AgentMessageReceivedEvent extends BaseEvent { payload: { message: unknown connection?: ConnectionRecord + contextCorrelationId?: string } } diff --git a/packages/core/src/agent/MessageReceiver.ts b/packages/core/src/agent/MessageReceiver.ts index 31282e0252..5fbef72a0f 100644 --- a/packages/core/src/agent/MessageReceiver.ts +++ b/packages/core/src/agent/MessageReceiver.ts @@ -1,17 +1,17 @@ import type { ConnectionRecord } from '../modules/connections' import type { InboundTransport } from '../transport' import type { EncryptedMessage, PlaintextMessage } from '../types' -import type { AgentContext } from './AgentContext' import type { AgentMessage } from './AgentMessage' import type { DecryptedMessageContext } from './EnvelopeService' import type { TransportSession } from './TransportService' +import type { AgentContext } from './context' import { InjectionSymbols } from '../constants' import { AriesFrameworkError } from '../error' import { Logger } from '../logger' import { ConnectionService } from '../modules/connections' import { ProblemReportError, ProblemReportMessage, ProblemReportReason } from '../modules/problem-reports' -import { injectable, inject } from '../plugins' +import { inject, injectable } from '../plugins' import { isValidJweStructure } from '../utils/JWE' import { JsonTransformer } from '../utils/JsonTransformer' import { canHandleMessageType, parseMessageType, replaceLegacyDidSovPrefixOnMessage } from '../utils/messageType' @@ -20,6 +20,7 @@ import { Dispatcher } from './Dispatcher' import { EnvelopeService } from './EnvelopeService' import { MessageSender } from './MessageSender' import { TransportService } from './TransportService' +import { AgentContextProvider } from './context' import { createOutboundMessage } from './helpers' import { InboundMessageContext } from './models/InboundMessageContext' @@ -31,6 +32,7 @@ export class MessageReceiver { private dispatcher: Dispatcher private logger: Logger private connectionService: ConnectionService + private agentContextProvider: AgentContextProvider public readonly inboundTransports: InboundTransport[] = [] public constructor( @@ -39,6 +41,7 @@ export class MessageReceiver { messageSender: MessageSender, connectionService: ConnectionService, dispatcher: Dispatcher, + @inject(InjectionSymbols.AgentContextProvider) agentContextProvider: AgentContextProvider, @inject(InjectionSymbols.Logger) logger: Logger ) { this.envelopeService = envelopeService @@ -46,6 +49,7 @@ export class MessageReceiver { this.messageSender = messageSender this.connectionService = connectionService this.dispatcher = dispatcher + this.agentContextProvider = agentContextProvider this.logger = logger } @@ -54,17 +58,26 @@ export class MessageReceiver { } /** - * Receive and handle an inbound DIDComm message. It will decrypt the message, transform it + * Receive and handle an inbound DIDComm message. It will determine the agent context, decrypt the message, transform it * to it's corresponding message class and finally dispatch it to the dispatcher. * * @param inboundMessage the message to receive and handle */ public async receiveMessage( - agentContext: AgentContext, inboundMessage: unknown, - { session, connection }: { session?: TransportSession; connection?: ConnectionRecord } + { + session, + connection, + contextCorrelationId, + }: { session?: TransportSession; connection?: ConnectionRecord; contextCorrelationId?: string } = {} ) { - this.logger.debug(`Agent ${agentContext.config.label} received message`) + this.logger.debug(`Agent received message`) + + // Find agent context for the inbound message + const agentContext = await this.agentContextProvider.getContextForInboundMessage(inboundMessage, { + contextCorrelationId, + }) + if (this.isEncryptedMessage(inboundMessage)) { await this.receiveEncryptedMessage(agentContext, inboundMessage as EncryptedMessage, session) } else if (this.isPlaintextMessage(inboundMessage)) { diff --git a/packages/core/src/agent/MessageSender.ts b/packages/core/src/agent/MessageSender.ts index a817124e31..5269bf72be 100644 --- a/packages/core/src/agent/MessageSender.ts +++ b/packages/core/src/agent/MessageSender.ts @@ -4,10 +4,10 @@ import type { DidDocument } from '../modules/dids' import type { OutOfBandRecord } from '../modules/oob/repository' import type { OutboundTransport } from '../transport/OutboundTransport' import type { OutboundMessage, OutboundPackage, EncryptedMessage } from '../types' -import type { AgentContext } from './AgentContext' import type { AgentMessage } from './AgentMessage' import type { EnvelopeKeys } from './EnvelopeService' import type { TransportSession } from './TransportService' +import type { AgentContext } from './context' import { DID_COMM_TRANSPORT_QUEUE, InjectionSymbols } from '../constants' import { ReturnRouteTypes } from '../decorators/transport/TransportDecorator' diff --git a/packages/core/src/agent/context/AgentContext.ts b/packages/core/src/agent/context/AgentContext.ts new file mode 100644 index 0000000000..ef425377b2 --- /dev/null +++ b/packages/core/src/agent/context/AgentContext.ts @@ -0,0 +1,49 @@ +import type { DependencyManager } from '../../plugins' +import type { Wallet } from '../../wallet' + +import { InjectionSymbols } from '../../constants' +import { AgentConfig } from '../AgentConfig' + +export class AgentContext { + /** + * Dependency manager holds all dependencies for the current context. Possibly a child of a parent dependency manager, + * in which case all singleton dependencies from the parent context are also available to this context. + */ + public readonly dependencyManager: DependencyManager + + /** + * An identifier that allows to correlate this context across sessions. This identifier is created by the `AgentContextProvider` + * and should only be meaningful to the `AgentContextProvider`. The `contextCorrelationId` MUST uniquely identity the context and + * should be enough to start a new session. + * + * An example of the `contextCorrelationId` is for example the id of the `TenantRecord` that is associated with this context when using the tenant module. + * The `TenantAgentContextProvider` will set the `contextCorrelationId` to the `TenantRecord` id when creating the context, and will be able to create a context + * for a specific tenant using the `contextCorrelationId`. + */ + public readonly contextCorrelationId: string + + public constructor({ + dependencyManager, + contextCorrelationId, + }: { + dependencyManager: DependencyManager + contextCorrelationId: string + }) { + this.dependencyManager = dependencyManager + this.contextCorrelationId = contextCorrelationId + } + + /** + * Convenience method to access the agent config for the current context. + */ + public get config() { + return this.dependencyManager.resolve(AgentConfig) + } + + /** + * Convenience method to access the wallet for the current context. + */ + public get wallet() { + return this.dependencyManager.resolve(InjectionSymbols.Wallet) + } +} diff --git a/packages/core/src/agent/context/AgentContextProvider.ts b/packages/core/src/agent/context/AgentContextProvider.ts new file mode 100644 index 0000000000..09047d38b6 --- /dev/null +++ b/packages/core/src/agent/context/AgentContextProvider.ts @@ -0,0 +1,17 @@ +import type { AgentContext } from './AgentContext' + +export interface AgentContextProvider { + /** + * Find the agent context based for an inbound message. It's possible to provide a contextCorrelationId to make it + * easier for the context provider implementation to correlate inbound messages to the correct context. This can be useful if + * a plaintext message is passed and the context provider can't determine the context based on the recipient public keys + * of the inbound message. + * + * The implementation of this method could range from a very simple one that always returns the same context to + * a complex one that manages the context for a multi-tenant agent. + */ + getContextForInboundMessage( + inboundMessage: unknown, + options?: { contextCorrelationId?: string } + ): Promise +} diff --git a/packages/core/src/agent/context/DefaultAgentContextProvider.ts b/packages/core/src/agent/context/DefaultAgentContextProvider.ts new file mode 100644 index 0000000000..3227dadc55 --- /dev/null +++ b/packages/core/src/agent/context/DefaultAgentContextProvider.ts @@ -0,0 +1,24 @@ +import type { AgentContextProvider } from './AgentContextProvider' + +import { injectable } from '../../plugins' + +import { AgentContext } from './AgentContext' + +/** + * Default implementation of AgentContextProvider. + * + * Holds a single `AgentContext` instance that will be used for all messages, i.e. a + * a single tenant agent. + */ +@injectable() +export class DefaultAgentContextProvider implements AgentContextProvider { + private agentContext: AgentContext + + public constructor(agentContext: AgentContext) { + this.agentContext = agentContext + } + + public async getContextForInboundMessage(): Promise { + return this.agentContext + } +} diff --git a/packages/core/src/agent/context/__tests__/DefaultAgentContextProvider.test.ts b/packages/core/src/agent/context/__tests__/DefaultAgentContextProvider.test.ts new file mode 100644 index 0000000000..f7faa58c78 --- /dev/null +++ b/packages/core/src/agent/context/__tests__/DefaultAgentContextProvider.test.ts @@ -0,0 +1,18 @@ +import type { AgentContextProvider } from '../AgentContextProvider' + +import { getAgentContext } from '../../../../tests/helpers' +import { DefaultAgentContextProvider } from '../DefaultAgentContextProvider' + +const agentContext = getAgentContext() + +describe('DefaultAgentContextProvider', () => { + describe('getContextForInboundMessage()', () => { + test('returns the agent context provided in the constructor', async () => { + const agentContextProvider: AgentContextProvider = new DefaultAgentContextProvider(agentContext) + + const message = {} + + await expect(agentContextProvider.getContextForInboundMessage(message)).resolves.toBe(agentContext) + }) + }) +}) diff --git a/packages/core/src/agent/context/index.ts b/packages/core/src/agent/context/index.ts new file mode 100644 index 0000000000..6f46b27942 --- /dev/null +++ b/packages/core/src/agent/context/index.ts @@ -0,0 +1,3 @@ +export * from './AgentContext' +export * from './AgentContextProvider' +export * from './DefaultAgentContextProvider' diff --git a/packages/core/src/agent/index.ts b/packages/core/src/agent/index.ts index 615455eb43..630b4d7e78 100644 --- a/packages/core/src/agent/index.ts +++ b/packages/core/src/agent/index.ts @@ -1 +1 @@ -export * from './AgentContext' +export * from './context' diff --git a/packages/core/src/agent/models/InboundMessageContext.ts b/packages/core/src/agent/models/InboundMessageContext.ts index a31d7a8614..c3f3628e09 100644 --- a/packages/core/src/agent/models/InboundMessageContext.ts +++ b/packages/core/src/agent/models/InboundMessageContext.ts @@ -1,7 +1,7 @@ import type { Key } from '../../crypto' import type { ConnectionRecord } from '../../modules/connections' -import type { AgentContext } from '../AgentContext' import type { AgentMessage } from '../AgentMessage' +import type { AgentContext } from '../context' import { AriesFrameworkError } from '../../error' diff --git a/packages/core/src/constants.ts b/packages/core/src/constants.ts index 9d7fdcbc61..0c67d367f6 100644 --- a/packages/core/src/constants.ts +++ b/packages/core/src/constants.ts @@ -2,6 +2,7 @@ export const InjectionSymbols = { MessageRepository: Symbol('MessageRepository'), StorageService: Symbol('StorageService'), Logger: Symbol('Logger'), + AgentContextProvider: Symbol('AgentContextProvider'), AgentDependencies: Symbol('AgentDependencies'), Stop$: Symbol('Stop$'), FileSystem: Symbol('FileSystem'), diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index c2de657677..ac2359d908 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -1,7 +1,8 @@ // reflect-metadata used for class-transformer + class-validator import 'reflect-metadata' -export { AgentContext } from './agent/AgentContext' +export { AgentContext } from './agent' +export { MessageReceiver } from './agent/MessageReceiver' export { Agent } from './agent/Agent' export { EventEmitter } from './agent/EventEmitter' export { Handler, HandlerInboundMessage } from './agent/Handler' diff --git a/packages/core/src/modules/oob/OutOfBandModule.ts b/packages/core/src/modules/oob/OutOfBandModule.ts index 0a5ac5b55b..8d6cce08f1 100644 --- a/packages/core/src/modules/oob/OutOfBandModule.ts +++ b/packages/core/src/modules/oob/OutOfBandModule.ts @@ -625,6 +625,7 @@ export class OutOfBandModule { payload: { message: plaintextMessage, connection: connectionRecord, + contextCorrelationId: this.agentContext.contextCorrelationId, }, }) } @@ -666,6 +667,7 @@ export class OutOfBandModule { type: AgentEventTypes.AgentMessageReceived, payload: { message: plaintextMessage, + contextCorrelationId: this.agentContext.contextCorrelationId, }, }) } diff --git a/packages/core/src/modules/proofs/ProofResponseCoordinator.ts b/packages/core/src/modules/proofs/ProofResponseCoordinator.ts index 7e95e73682..26f1d6b795 100644 --- a/packages/core/src/modules/proofs/ProofResponseCoordinator.ts +++ b/packages/core/src/modules/proofs/ProofResponseCoordinator.ts @@ -1,4 +1,4 @@ -import type { AgentContext } from '../../agent/AgentContext' +import type { AgentContext } from '../../agent/context' import type { ProofRecord } from './repository' import { injectable } from '../../plugins' diff --git a/packages/core/src/modules/routing/protocol/pickup/v1/handlers/BatchHandler.ts b/packages/core/src/modules/routing/protocol/pickup/v1/handlers/BatchHandler.ts index b1449f6af5..b67bf8b1bb 100644 --- a/packages/core/src/modules/routing/protocol/pickup/v1/handlers/BatchHandler.ts +++ b/packages/core/src/modules/routing/protocol/pickup/v1/handlers/BatchHandler.ts @@ -24,6 +24,7 @@ export class BatchHandler implements Handler { type: AgentEventTypes.AgentMessageReceived, payload: { message: message.message, + contextCorrelationId: messageContext.agentContext.contextCorrelationId, }, }) }) diff --git a/packages/core/src/modules/routing/services/MediationRecipientService.ts b/packages/core/src/modules/routing/services/MediationRecipientService.ts index f9a8b50be9..a345231d9e 100644 --- a/packages/core/src/modules/routing/services/MediationRecipientService.ts +++ b/packages/core/src/modules/routing/services/MediationRecipientService.ts @@ -300,6 +300,7 @@ export class MediationRecipientService { type: AgentEventTypes.AgentMessageReceived, payload: { message: attachment.getDataAsJson(), + contextCorrelationId: messageContext.agentContext.contextCorrelationId, }, }) } diff --git a/packages/core/src/modules/routing/services/__tests__/MediationRecipientService.test.ts b/packages/core/src/modules/routing/services/__tests__/MediationRecipientService.test.ts index 7e4263512b..92f1cd2141 100644 --- a/packages/core/src/modules/routing/services/__tests__/MediationRecipientService.test.ts +++ b/packages/core/src/modules/routing/services/__tests__/MediationRecipientService.test.ts @@ -244,12 +244,14 @@ describe('MediationRecipientService', () => { type: AgentEventTypes.AgentMessageReceived, payload: { message: { first: 'value' }, + contextCorrelationId: agentContext.contextCorrelationId, }, }) expect(eventEmitter.emit).toHaveBeenNthCalledWith(2, agentContext, { type: AgentEventTypes.AgentMessageReceived, payload: { message: { second: 'value' }, + contextCorrelationId: agentContext.contextCorrelationId, }, }) }) diff --git a/packages/core/tests/helpers.ts b/packages/core/tests/helpers.ts index f031137044..5c5be4adaf 100644 --- a/packages/core/tests/helpers.ts +++ b/packages/core/tests/helpers.ts @@ -142,14 +142,16 @@ export function getAgentContext({ dependencyManager = new DependencyManager(), wallet, agentConfig, + contextCorrelationId = 'mock', }: { dependencyManager?: DependencyManager wallet?: Wallet agentConfig?: AgentConfig + contextCorrelationId?: string } = {}) { if (wallet) dependencyManager.registerInstance(InjectionSymbols.Wallet, wallet) if (agentConfig) dependencyManager.registerInstance(AgentConfig, agentConfig) - return new AgentContext({ dependencyManager }) + return new AgentContext({ dependencyManager, contextCorrelationId }) } export async function waitForProofRecord( diff --git a/packages/node/src/transport/HttpInboundTransport.ts b/packages/node/src/transport/HttpInboundTransport.ts index 59144ee392..4c7fea9fdf 100644 --- a/packages/node/src/transport/HttpInboundTransport.ts +++ b/packages/node/src/transport/HttpInboundTransport.ts @@ -2,7 +2,7 @@ import type { InboundTransport, Agent, TransportSession, EncryptedMessage } from import type { Express, Request, Response } from 'express' import type { Server } from 'http' -import { DidCommMimeType, AriesFrameworkError, TransportService, utils } from '@aries-framework/core' +import { DidCommMimeType, AriesFrameworkError, TransportService, utils, MessageReceiver } from '@aries-framework/core' import express, { text } from 'express' export class HttpInboundTransport implements InboundTransport { @@ -30,6 +30,7 @@ export class HttpInboundTransport implements InboundTransport { public async start(agent: Agent) { const transportService = agent.dependencyManager.resolve(TransportService) + const messageReceiver = agent.dependencyManager.resolve(MessageReceiver) agent.config.logger.debug(`Starting HTTP inbound transport`, { port: this.port, @@ -40,7 +41,9 @@ export class HttpInboundTransport implements InboundTransport { try { const message = req.body const encryptedMessage = JSON.parse(message) - await agent.receiveMessage(encryptedMessage, session) + await messageReceiver.receiveMessage(encryptedMessage, { + session, + }) // If agent did not use session when processing message we need to send response here. if (!res.headersSent) { diff --git a/packages/node/src/transport/WsInboundTransport.ts b/packages/node/src/transport/WsInboundTransport.ts index 25528c44ca..2fa23c6168 100644 --- a/packages/node/src/transport/WsInboundTransport.ts +++ b/packages/node/src/transport/WsInboundTransport.ts @@ -1,6 +1,6 @@ import type { Agent, InboundTransport, Logger, TransportSession, EncryptedMessage } from '@aries-framework/core' -import { AriesFrameworkError, TransportService, utils } from '@aries-framework/core' +import { AriesFrameworkError, TransportService, utils, MessageReceiver } from '@aries-framework/core' import WebSocket, { Server } from 'ws' export class WsInboundTransport implements InboundTransport { @@ -58,11 +58,13 @@ export class WsInboundTransport implements InboundTransport { } private listenOnWebSocketMessages(agent: Agent, socket: WebSocket, session: TransportSession) { + const messageReceiver = agent.injectionContainer.resolve(MessageReceiver) + // eslint-disable-next-line @typescript-eslint/no-explicit-any socket.addEventListener('message', async (event: any) => { this.logger.debug('WebSocket message event received.', { url: event.target.url }) try { - await agent.receiveMessage(JSON.parse(event.data), session) + await messageReceiver.receiveMessage(JSON.parse(event.data), { session }) } catch (error) { this.logger.error('Error processing message') } diff --git a/tests/transport/SubjectInboundTransport.ts b/tests/transport/SubjectInboundTransport.ts index cd713f7d3f..572784fb17 100644 --- a/tests/transport/SubjectInboundTransport.ts +++ b/tests/transport/SubjectInboundTransport.ts @@ -3,6 +3,7 @@ import type { TransportSession } from '../../packages/core/src/agent/TransportSe import type { EncryptedMessage } from '../../packages/core/src/types' import type { Subject, Subscription } from 'rxjs' +import { MessageReceiver } from '../../packages/core/src' import { TransportService } from '../../packages/core/src/agent/TransportService' import { uuid } from '../../packages/core/src/utils/uuid' @@ -27,6 +28,7 @@ export class SubjectInboundTransport implements InboundTransport { private subscribe(agent: Agent) { const logger = agent.config.logger const transportService = agent.dependencyManager.resolve(TransportService) + const messageReceiver = agent.dependencyManager.resolve(MessageReceiver) this.subscription = this.ourSubject.subscribe({ next: async ({ message, replySubject }: SubjectMessage) => { @@ -44,7 +46,7 @@ export class SubjectInboundTransport implements InboundTransport { }) } - await agent.receiveMessage(message, session) + await messageReceiver.receiveMessage(message, { session }) }, }) } diff --git a/tests/transport/SubjectOutboundTransport.ts b/tests/transport/SubjectOutboundTransport.ts index 1754dbe067..7a7adfaa8e 100644 --- a/tests/transport/SubjectOutboundTransport.ts +++ b/tests/transport/SubjectOutboundTransport.ts @@ -3,7 +3,7 @@ import type { OutboundPackage, OutboundTransport, Agent, Logger } from '@aries-f import { takeUntil, Subject, take } from 'rxjs' -import { InjectionSymbols, AriesFrameworkError } from '@aries-framework/core' +import { MessageReceiver, InjectionSymbols, AriesFrameworkError } from '@aries-framework/core' export class SubjectOutboundTransport implements OutboundTransport { private logger!: Logger @@ -29,6 +29,7 @@ export class SubjectOutboundTransport implements OutboundTransport { } public async sendMessage(outboundPackage: OutboundPackage) { + const messageReceiver = this.agent.injectionContainer.resolve(MessageReceiver) this.logger.debug(`Sending outbound message to endpoint ${outboundPackage.endpoint}`, { endpoint: outboundPackage.endpoint, }) @@ -53,7 +54,7 @@ export class SubjectOutboundTransport implements OutboundTransport { next: async ({ message }: SubjectMessage) => { this.logger.test('Received message') - await this.agent.receiveMessage(message) + await messageReceiver.receiveMessage(message) }, })