From 1bda3f0733a472b536059cee8d34e25fb04c9f2d Mon Sep 17 00:00:00 2001 From: Ariel Gentile Date: Mon, 27 Feb 2023 15:35:16 -0300 Subject: [PATCH 1/2] fix(anoncreds-rs): save revocation registry index (#1351) Signed-off-by: Ariel Gentile --- packages/anoncreds-rs/src/services/AnonCredsRsHolderService.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/anoncreds-rs/src/services/AnonCredsRsHolderService.ts b/packages/anoncreds-rs/src/services/AnonCredsRsHolderService.ts index b4c1e02f53..f0516665ee 100644 --- a/packages/anoncreds-rs/src/services/AnonCredsRsHolderService.ts +++ b/packages/anoncreds-rs/src/services/AnonCredsRsHolderService.ts @@ -251,6 +251,7 @@ export class AnonCredsRsHolderService implements AnonCredsHolderService { schemaName: schema.name, schemaIssuerId: schema.issuerId, schemaVersion: schema.version, + credentialRevocationId: processedCredential.revocationRegistryIndex?.toString(), }) ) From 78ecf1ed959c9daba1c119d03f4596f1db16c57c Mon Sep 17 00:00:00 2001 From: Ariel Gentile Date: Wed, 1 Mar 2023 18:44:26 -0300 Subject: [PATCH 2/2] refactor!: remove Dispatcher.registerMessageHandler (#1354) BREAKING CHANGE: `Dispatcher.registerMessageHandler` has been removed in favour of `MessageHandlerRegistry.registerMessageHandler`. If you want to register message handlers in an extension module, you can use directly `agentContext.dependencyManager.registerMessageHandlers`. Signed-off-by: Ariel Gentile --- packages/action-menu/src/ActionMenuApi.ts | 17 +++++------ packages/core/src/agent/Dispatcher.ts | 8 ------ .../src/agent/__tests__/Dispatcher.test.ts | 8 ++++-- .../basic-messages/BasicMessagesApi.ts | 10 +++---- .../src/modules/connections/ConnectionsApi.ts | 28 +++++++++++-------- .../services/RevocationNotificationService.ts | 14 ++++------ .../RevocationNotificationService.test.ts | 10 +++---- .../protocol/v1/V1DiscoverFeaturesService.ts | 14 +++++----- .../V1DiscoverFeaturesService.test.ts | 10 +++---- .../protocol/v2/V2DiscoverFeaturesService.ts | 14 +++++----- .../V2DiscoverFeaturesService.test.ts | 10 +++---- .../services/DiscoverFeaturesService.ts | 4 --- .../core/src/modules/routing/MediatorApi.ts | 18 ++++++------ .../core/src/modules/routing/RecipientApi.ts | 20 ++++++------- .../pickup/v1/MessagePickupService.ts | 14 ++++------ .../pickup/v2/V2MessagePickupService.ts | 20 ++++++------- .../__tests__/V2MessagePickupService.test.ts | 10 +++---- .../question-answer/src/QuestionAnswerApi.ts | 13 ++++----- samples/extension-module/dummy/DummyApi.ts | 12 +++----- 19 files changed, 116 insertions(+), 138 deletions(-) diff --git a/packages/action-menu/src/ActionMenuApi.ts b/packages/action-menu/src/ActionMenuApi.ts index bb6f3cd4f3..6abe1b3fac 100644 --- a/packages/action-menu/src/ActionMenuApi.ts +++ b/packages/action-menu/src/ActionMenuApi.ts @@ -10,7 +10,6 @@ import { AgentContext, AriesFrameworkError, ConnectionService, - Dispatcher, MessageSender, OutboundMessageContext, injectable, @@ -36,7 +35,6 @@ export class ActionMenuApi { private agentContext: AgentContext public constructor( - dispatcher: Dispatcher, connectionService: ConnectionService, messageSender: MessageSender, actionMenuService: ActionMenuService, @@ -46,7 +44,13 @@ export class ActionMenuApi { this.messageSender = messageSender this.actionMenuService = actionMenuService this.agentContext = agentContext - this.registerMessageHandlers(dispatcher) + + this.agentContext.dependencyManager.registerMessageHandlers([ + new ActionMenuProblemReportHandler(this.actionMenuService), + new MenuMessageHandler(this.actionMenuService), + new MenuRequestMessageHandler(this.actionMenuService), + new PerformMessageHandler(this.actionMenuService), + ]) } /** @@ -160,11 +164,4 @@ export class ActionMenuApi { return actionMenuRecord ? await this.actionMenuService.clearMenu(this.agentContext, { actionMenuRecord }) : null } - - private registerMessageHandlers(dispatcher: Dispatcher) { - dispatcher.registerMessageHandler(new ActionMenuProblemReportHandler(this.actionMenuService)) - dispatcher.registerMessageHandler(new MenuMessageHandler(this.actionMenuService)) - dispatcher.registerMessageHandler(new MenuRequestMessageHandler(this.actionMenuService)) - dispatcher.registerMessageHandler(new PerformMessageHandler(this.actionMenuService)) - } } diff --git a/packages/core/src/agent/Dispatcher.ts b/packages/core/src/agent/Dispatcher.ts index 50ff6da42e..709ba04ea0 100644 --- a/packages/core/src/agent/Dispatcher.ts +++ b/packages/core/src/agent/Dispatcher.ts @@ -1,6 +1,5 @@ import type { AgentMessage } from './AgentMessage' import type { AgentMessageProcessedEvent } from './Events' -import type { MessageHandler } from './MessageHandler' import type { InboundMessageContext } from './models/InboundMessageContext' import { InjectionSymbols } from '../constants' @@ -35,13 +34,6 @@ class Dispatcher { this.logger = logger } - /** - * @deprecated Use {@link MessageHandlerRegistry.registerMessageHandler} directly - */ - public registerMessageHandler(messageHandler: MessageHandler) { - this.messageHandlerRegistry.registerMessageHandler(messageHandler) - } - public async dispatch(messageContext: InboundMessageContext): Promise { const { agentContext, connection, senderKey, recipientKey, message } = messageContext const messageHandler = this.messageHandlerRegistry.getHandlerForMessageType(message.type) diff --git a/packages/core/src/agent/__tests__/Dispatcher.test.ts b/packages/core/src/agent/__tests__/Dispatcher.test.ts index 3c91825d2d..7bbcb89f95 100644 --- a/packages/core/src/agent/__tests__/Dispatcher.test.ts +++ b/packages/core/src/agent/__tests__/Dispatcher.test.ts @@ -22,17 +22,18 @@ describe('Dispatcher', () => { describe('dispatch()', () => { it('calls the handle method of the handler', async () => { + const messageHandlerRegistry = new MessageHandlerRegistry() const dispatcher = new Dispatcher( new MessageSenderMock(), eventEmitter, - new MessageHandlerRegistry(), + messageHandlerRegistry, agentConfig.logger ) const customProtocolMessage = new CustomProtocolMessage() const inboundMessageContext = new InboundMessageContext(customProtocolMessage, { agentContext }) const mockHandle = jest.fn() - dispatcher.registerMessageHandler({ supportedMessages: [CustomProtocolMessage], handle: mockHandle }) + messageHandlerRegistry.registerMessageHandler({ supportedMessages: [CustomProtocolMessage], handle: mockHandle }) await dispatcher.dispatch(inboundMessageContext) @@ -40,6 +41,7 @@ describe('Dispatcher', () => { }) it('throws an error if no handler for the message could be found', async () => { + const messageHandlerRegistry = new MessageHandlerRegistry() const dispatcher = new Dispatcher( new MessageSenderMock(), eventEmitter, @@ -50,7 +52,7 @@ describe('Dispatcher', () => { const inboundMessageContext = new InboundMessageContext(customProtocolMessage, { agentContext }) const mockHandle = jest.fn() - dispatcher.registerMessageHandler({ supportedMessages: [], handle: mockHandle }) + messageHandlerRegistry.registerMessageHandler({ supportedMessages: [], handle: mockHandle }) await expect(dispatcher.dispatch(inboundMessageContext)).rejects.toThrow( 'No handler for message type "https://didcomm.org/fake-protocol/1.5/message" found' diff --git a/packages/core/src/modules/basic-messages/BasicMessagesApi.ts b/packages/core/src/modules/basic-messages/BasicMessagesApi.ts index 938f6b8407..ff788e00fe 100644 --- a/packages/core/src/modules/basic-messages/BasicMessagesApi.ts +++ b/packages/core/src/modules/basic-messages/BasicMessagesApi.ts @@ -2,7 +2,7 @@ import type { BasicMessageRecord } from './repository/BasicMessageRecord' import type { Query } from '../../storage/StorageService' import { AgentContext } from '../../agent' -import { Dispatcher } from '../../agent/Dispatcher' +import { MessageHandlerRegistry } from '../../agent/MessageHandlerRegistry' import { MessageSender } from '../../agent/MessageSender' import { OutboundMessageContext } from '../../agent/models' import { injectable } from '../../plugins' @@ -19,7 +19,7 @@ export class BasicMessagesApi { private agentContext: AgentContext public constructor( - dispatcher: Dispatcher, + messageHandlerRegistry: MessageHandlerRegistry, basicMessageService: BasicMessageService, messageSender: MessageSender, connectionService: ConnectionService, @@ -29,7 +29,7 @@ export class BasicMessagesApi { this.messageSender = messageSender this.connectionService = connectionService this.agentContext = agentContext - this.registerMessageHandlers(dispatcher) + this.registerMessageHandlers(messageHandlerRegistry) } /** @@ -91,7 +91,7 @@ export class BasicMessagesApi { await this.basicMessageService.deleteById(this.agentContext, basicMessageRecordId) } - private registerMessageHandlers(dispatcher: Dispatcher) { - dispatcher.registerMessageHandler(new BasicMessageHandler(this.basicMessageService)) + private registerMessageHandlers(messageHandlerRegistry: MessageHandlerRegistry) { + messageHandlerRegistry.registerMessageHandler(new BasicMessageHandler(this.basicMessageService)) } } diff --git a/packages/core/src/modules/connections/ConnectionsApi.ts b/packages/core/src/modules/connections/ConnectionsApi.ts index 9777bc903a..468c02c507 100644 --- a/packages/core/src/modules/connections/ConnectionsApi.ts +++ b/packages/core/src/modules/connections/ConnectionsApi.ts @@ -5,7 +5,7 @@ import type { Query } from '../../storage/StorageService' import type { OutOfBandRecord } from '../oob/repository' import { AgentContext } from '../../agent' -import { Dispatcher } from '../../agent/Dispatcher' +import { MessageHandlerRegistry } from '../../agent/MessageHandlerRegistry' import { MessageSender } from '../../agent/MessageSender' import { OutboundMessageContext } from '../../agent/models' import { ReturnRouteTypes } from '../../decorators/transport/TransportDecorator' @@ -55,7 +55,7 @@ export class ConnectionsApi { private agentContext: AgentContext public constructor( - dispatcher: Dispatcher, + messageHandlerRegistry: MessageHandlerRegistry, didExchangeProtocol: DidExchangeProtocol, connectionService: ConnectionService, outOfBandService: OutOfBandService, @@ -78,7 +78,7 @@ export class ConnectionsApi { this.agentContext = agentContext this.config = connectionsModuleConfig - this.registerMessageHandlers(dispatcher) + this.registerMessageHandlers(messageHandlerRegistry) } public async acceptOutOfBandInvitation( @@ -407,8 +407,8 @@ export class ConnectionsApi { return this.connectionService.findByInvitationDid(this.agentContext, invitationDid) } - private registerMessageHandlers(dispatcher: Dispatcher) { - dispatcher.registerMessageHandler( + private registerMessageHandlers(messageHandlerRegistry: MessageHandlerRegistry) { + messageHandlerRegistry.registerMessageHandler( new ConnectionRequestHandler( this.connectionService, this.outOfBandService, @@ -417,14 +417,16 @@ export class ConnectionsApi { this.config ) ) - dispatcher.registerMessageHandler( + messageHandlerRegistry.registerMessageHandler( new ConnectionResponseHandler(this.connectionService, this.outOfBandService, this.didResolverService, this.config) ) - dispatcher.registerMessageHandler(new AckMessageHandler(this.connectionService)) - dispatcher.registerMessageHandler(new TrustPingMessageHandler(this.trustPingService, this.connectionService)) - dispatcher.registerMessageHandler(new TrustPingResponseMessageHandler(this.trustPingService)) + messageHandlerRegistry.registerMessageHandler(new AckMessageHandler(this.connectionService)) + messageHandlerRegistry.registerMessageHandler( + new TrustPingMessageHandler(this.trustPingService, this.connectionService) + ) + messageHandlerRegistry.registerMessageHandler(new TrustPingResponseMessageHandler(this.trustPingService)) - dispatcher.registerMessageHandler( + messageHandlerRegistry.registerMessageHandler( new DidExchangeRequestHandler( this.didExchangeProtocol, this.outOfBandService, @@ -434,7 +436,7 @@ export class ConnectionsApi { ) ) - dispatcher.registerMessageHandler( + messageHandlerRegistry.registerMessageHandler( new DidExchangeResponseHandler( this.didExchangeProtocol, this.outOfBandService, @@ -443,6 +445,8 @@ export class ConnectionsApi { this.config ) ) - dispatcher.registerMessageHandler(new DidExchangeCompleteHandler(this.didExchangeProtocol, this.outOfBandService)) + messageHandlerRegistry.registerMessageHandler( + new DidExchangeCompleteHandler(this.didExchangeProtocol, this.outOfBandService) + ) } } diff --git a/packages/core/src/modules/credentials/protocol/revocation-notification/services/RevocationNotificationService.ts b/packages/core/src/modules/credentials/protocol/revocation-notification/services/RevocationNotificationService.ts index e2b9d6e1f9..6a641d9457 100644 --- a/packages/core/src/modules/credentials/protocol/revocation-notification/services/RevocationNotificationService.ts +++ b/packages/core/src/modules/credentials/protocol/revocation-notification/services/RevocationNotificationService.ts @@ -5,8 +5,8 @@ import type { RevocationNotificationReceivedEvent } from '../../../CredentialEve import type { V1RevocationNotificationMessage } from '../messages/V1RevocationNotificationMessage' import type { V2RevocationNotificationMessage } from '../messages/V2RevocationNotificationMessage' -import { Dispatcher } from '../../../../../agent/Dispatcher' import { EventEmitter } from '../../../../../agent/EventEmitter' +import { MessageHandlerRegistry } from '../../../../../agent/MessageHandlerRegistry' import { InjectionSymbols } from '../../../../../constants' import { AriesFrameworkError } from '../../../../../error/AriesFrameworkError' import { Logger } from '../../../../../logger' @@ -22,21 +22,19 @@ import { v1ThreadRegex, v2IndyRevocationFormat, v2IndyRevocationIdentifierRegex export class RevocationNotificationService { private credentialRepository: CredentialRepository private eventEmitter: EventEmitter - private dispatcher: Dispatcher private logger: Logger public constructor( credentialRepository: CredentialRepository, eventEmitter: EventEmitter, - dispatcher: Dispatcher, + messageHandlerRegistry: MessageHandlerRegistry, @inject(InjectionSymbols.Logger) logger: Logger ) { this.credentialRepository = credentialRepository this.eventEmitter = eventEmitter - this.dispatcher = dispatcher this.logger = logger - this.registerMessageHandlers() + this.registerMessageHandlers(messageHandlerRegistry) } private async processRevocationNotification( @@ -147,8 +145,8 @@ export class RevocationNotificationService { } } - private registerMessageHandlers() { - this.dispatcher.registerMessageHandler(new V1RevocationNotificationHandler(this)) - this.dispatcher.registerMessageHandler(new V2RevocationNotificationHandler(this)) + private registerMessageHandlers(messageHandlerRegistry: MessageHandlerRegistry) { + messageHandlerRegistry.registerMessageHandler(new V1RevocationNotificationHandler(this)) + messageHandlerRegistry.registerMessageHandler(new V2RevocationNotificationHandler(this)) } } diff --git a/packages/core/src/modules/credentials/protocol/revocation-notification/services/__tests__/RevocationNotificationService.test.ts b/packages/core/src/modules/credentials/protocol/revocation-notification/services/__tests__/RevocationNotificationService.test.ts index e834ca5585..90d07d81ae 100644 --- a/packages/core/src/modules/credentials/protocol/revocation-notification/services/__tests__/RevocationNotificationService.test.ts +++ b/packages/core/src/modules/credentials/protocol/revocation-notification/services/__tests__/RevocationNotificationService.test.ts @@ -6,8 +6,8 @@ import { Subject } from 'rxjs' import { CredentialExchangeRecord, CredentialState, InboundMessageContext } from '../../../../../..' import { getAgentConfig, getAgentContext, getMockConnection, mockFunction } from '../../../../../../../tests/helpers' -import { Dispatcher } from '../../../../../../agent/Dispatcher' import { EventEmitter } from '../../../../../../agent/EventEmitter' +import { MessageHandlerRegistry } from '../../../../../../agent/MessageHandlerRegistry' import { DidExchangeState } from '../../../../../connections' import { CredentialEventTypes } from '../../../../CredentialEvents' import { CredentialRepository } from '../../../../repository/CredentialRepository' @@ -18,9 +18,9 @@ jest.mock('../../../../repository/CredentialRepository') const CredentialRepositoryMock = CredentialRepository as jest.Mock const credentialRepository = new CredentialRepositoryMock() -jest.mock('../../../../../../agent/Dispatcher') -const DispatcherMock = Dispatcher as jest.Mock -const dispatcher = new DispatcherMock() +jest.mock('../../../../../../agent/MessageHandlerRegistry') +const MessageHandlerRegistryMock = MessageHandlerRegistry as jest.Mock +const messageHandlerRegistry = new MessageHandlerRegistryMock() const connection = getMockConnection({ state: DidExchangeState.Completed, @@ -40,7 +40,7 @@ describe('RevocationNotificationService', () => { revocationNotificationService = new RevocationNotificationService( credentialRepository, eventEmitter, - dispatcher, + messageHandlerRegistry, agentConfig.logger ) }) diff --git a/packages/core/src/modules/discover-features/protocol/v1/V1DiscoverFeaturesService.ts b/packages/core/src/modules/discover-features/protocol/v1/V1DiscoverFeaturesService.ts index 39a694ccd1..172381d316 100644 --- a/packages/core/src/modules/discover-features/protocol/v1/V1DiscoverFeaturesService.ts +++ b/packages/core/src/modules/discover-features/protocol/v1/V1DiscoverFeaturesService.ts @@ -10,9 +10,9 @@ import type { DiscoverFeaturesProtocolMsgReturnType, } from '../../DiscoverFeaturesServiceOptions' -import { Dispatcher } from '../../../../agent/Dispatcher' import { EventEmitter } from '../../../../agent/EventEmitter' import { FeatureRegistry } from '../../../../agent/FeatureRegistry' +import { MessageHandlerRegistry } from '../../../../agent/MessageHandlerRegistry' import { Protocol } from '../../../../agent/models' import { InjectionSymbols } from '../../../../constants' import { AriesFrameworkError } from '../../../../error' @@ -30,13 +30,13 @@ export class V1DiscoverFeaturesService extends DiscoverFeaturesService { public constructor( featureRegistry: FeatureRegistry, eventEmitter: EventEmitter, - dispatcher: Dispatcher, + messageHandlerRegistry: MessageHandlerRegistry, @inject(InjectionSymbols.Logger) logger: Logger, discoverFeaturesConfig: DiscoverFeaturesModuleConfig ) { - super(featureRegistry, eventEmitter, dispatcher, logger, discoverFeaturesConfig) + super(featureRegistry, eventEmitter, logger, discoverFeaturesConfig) - this.registerMessageHandlers(dispatcher) + this.registerMessageHandlers(messageHandlerRegistry) } /** @@ -44,9 +44,9 @@ export class V1DiscoverFeaturesService extends DiscoverFeaturesService { */ public readonly version = 'v1' - private registerMessageHandlers(dispatcher: Dispatcher) { - dispatcher.registerMessageHandler(new V1DiscloseMessageHandler(this)) - dispatcher.registerMessageHandler(new V1QueryMessageHandler(this)) + private registerMessageHandlers(messageHandlerRegistry: MessageHandlerRegistry) { + messageHandlerRegistry.registerMessageHandler(new V1DiscloseMessageHandler(this)) + messageHandlerRegistry.registerMessageHandler(new V1QueryMessageHandler(this)) } public async createQuery( diff --git a/packages/core/src/modules/discover-features/protocol/v1/__tests__/V1DiscoverFeaturesService.test.ts b/packages/core/src/modules/discover-features/protocol/v1/__tests__/V1DiscoverFeaturesService.test.ts index 133a2b3442..03db2cf74a 100644 --- a/packages/core/src/modules/discover-features/protocol/v1/__tests__/V1DiscoverFeaturesService.test.ts +++ b/packages/core/src/modules/discover-features/protocol/v1/__tests__/V1DiscoverFeaturesService.test.ts @@ -7,9 +7,9 @@ import type { DiscoverFeaturesProtocolMsgReturnType } from '../../../DiscoverFea import { Subject } from 'rxjs' import { agentDependencies, getAgentContext, getMockConnection } from '../../../../../../tests/helpers' -import { Dispatcher } from '../../../../../agent/Dispatcher' import { EventEmitter } from '../../../../../agent/EventEmitter' import { FeatureRegistry } from '../../../../../agent/FeatureRegistry' +import { MessageHandlerRegistry } from '../../../../../agent/MessageHandlerRegistry' import { Protocol } from '../../../../../agent/models' import { InboundMessageContext } from '../../../../../agent/models/InboundMessageContext' import { ConsoleLogger } from '../../../../../logger/ConsoleLogger' @@ -19,8 +19,8 @@ import { DiscoverFeaturesModuleConfig } from '../../../DiscoverFeaturesModuleCon import { V1DiscoverFeaturesService } from '../V1DiscoverFeaturesService' import { V1DiscloseMessage, V1QueryMessage } from '../messages' -jest.mock('../../../../../agent/Dispatcher') -const DispatcherMock = Dispatcher as jest.Mock +jest.mock('../../../../../agent/MessageHandlerRegistry') +const MessageHandlerRegistryMock = MessageHandlerRegistry as jest.Mock const eventEmitter = new EventEmitter(agentDependencies, new Subject()) const featureRegistry = new FeatureRegistry() featureRegistry.register(new Protocol({ id: 'https://didcomm.org/connections/1.0' })) @@ -36,7 +36,7 @@ describe('V1DiscoverFeaturesService - auto accept queries', () => { const discoverFeaturesService = new V1DiscoverFeaturesService( featureRegistry, eventEmitter, - new DispatcherMock(), + new MessageHandlerRegistryMock(), new LoggerMock(), discoverFeaturesModuleConfig ) @@ -239,7 +239,7 @@ describe('V1DiscoverFeaturesService - auto accept disabled', () => { const discoverFeaturesService = new V1DiscoverFeaturesService( featureRegistry, eventEmitter, - new DispatcherMock(), + new MessageHandlerRegistry(), new LoggerMock(), discoverFeaturesModuleConfig ) diff --git a/packages/core/src/modules/discover-features/protocol/v2/V2DiscoverFeaturesService.ts b/packages/core/src/modules/discover-features/protocol/v2/V2DiscoverFeaturesService.ts index 2e007ae142..0196a351c4 100644 --- a/packages/core/src/modules/discover-features/protocol/v2/V2DiscoverFeaturesService.ts +++ b/packages/core/src/modules/discover-features/protocol/v2/V2DiscoverFeaturesService.ts @@ -9,9 +9,9 @@ import type { CreateDisclosureOptions, } from '../../DiscoverFeaturesServiceOptions' -import { Dispatcher } from '../../../../agent/Dispatcher' import { EventEmitter } from '../../../../agent/EventEmitter' import { FeatureRegistry } from '../../../../agent/FeatureRegistry' +import { MessageHandlerRegistry } from '../../../../agent/MessageHandlerRegistry' import { InjectionSymbols } from '../../../../constants' import { Logger } from '../../../../logger' import { inject, injectable } from '../../../../plugins' @@ -27,12 +27,12 @@ export class V2DiscoverFeaturesService extends DiscoverFeaturesService { public constructor( featureRegistry: FeatureRegistry, eventEmitter: EventEmitter, - dispatcher: Dispatcher, + messageHandlerRegistry: MessageHandlerRegistry, @inject(InjectionSymbols.Logger) logger: Logger, discoverFeaturesModuleConfig: DiscoverFeaturesModuleConfig ) { - super(featureRegistry, eventEmitter, dispatcher, logger, discoverFeaturesModuleConfig) - this.registerMessageHandlers(dispatcher) + super(featureRegistry, eventEmitter, logger, discoverFeaturesModuleConfig) + this.registerMessageHandlers(messageHandlerRegistry) } /** @@ -40,9 +40,9 @@ export class V2DiscoverFeaturesService extends DiscoverFeaturesService { */ public readonly version = 'v2' - private registerMessageHandlers(dispatcher: Dispatcher) { - dispatcher.registerMessageHandler(new V2DisclosuresMessageHandler(this)) - dispatcher.registerMessageHandler(new V2QueriesMessageHandler(this)) + private registerMessageHandlers(messageHandlerRegistry: MessageHandlerRegistry) { + messageHandlerRegistry.registerMessageHandler(new V2DisclosuresMessageHandler(this)) + messageHandlerRegistry.registerMessageHandler(new V2QueriesMessageHandler(this)) } public async createQuery( diff --git a/packages/core/src/modules/discover-features/protocol/v2/__tests__/V2DiscoverFeaturesService.test.ts b/packages/core/src/modules/discover-features/protocol/v2/__tests__/V2DiscoverFeaturesService.test.ts index 9669c9a63f..897fe5d1b4 100644 --- a/packages/core/src/modules/discover-features/protocol/v2/__tests__/V2DiscoverFeaturesService.test.ts +++ b/packages/core/src/modules/discover-features/protocol/v2/__tests__/V2DiscoverFeaturesService.test.ts @@ -7,9 +7,9 @@ import type { DiscoverFeaturesProtocolMsgReturnType } from '../../../DiscoverFea import { Subject } from 'rxjs' import { agentDependencies, getAgentContext, getMockConnection } from '../../../../../../tests/helpers' -import { Dispatcher } from '../../../../../agent/Dispatcher' import { EventEmitter } from '../../../../../agent/EventEmitter' import { FeatureRegistry } from '../../../../../agent/FeatureRegistry' +import { MessageHandlerRegistry } from '../../../../../agent/MessageHandlerRegistry' import { InboundMessageContext, Protocol, GoalCode } from '../../../../../agent/models' import { ConsoleLogger } from '../../../../../logger/ConsoleLogger' import { DidExchangeState } from '../../../../connections' @@ -18,8 +18,8 @@ import { DiscoverFeaturesModuleConfig } from '../../../DiscoverFeaturesModuleCon import { V2DiscoverFeaturesService } from '../V2DiscoverFeaturesService' import { V2DisclosuresMessage, V2QueriesMessage } from '../messages' -jest.mock('../../../../../agent/Dispatcher') -const DispatcherMock = Dispatcher as jest.Mock +jest.mock('../../../../../agent/MessageHandlerRegistry') +const MessageHandlerRegistryMock = MessageHandlerRegistry as jest.Mock const eventEmitter = new EventEmitter(agentDependencies, new Subject()) const featureRegistry = new FeatureRegistry() featureRegistry.register(new Protocol({ id: 'https://didcomm.org/connections/1.0' })) @@ -38,7 +38,7 @@ describe('V2DiscoverFeaturesService - auto accept queries', () => { const discoverFeaturesService = new V2DiscoverFeaturesService( featureRegistry, eventEmitter, - new DispatcherMock(), + new MessageHandlerRegistryMock(), new LoggerMock(), discoverFeaturesModuleConfig ) @@ -250,7 +250,7 @@ describe('V2DiscoverFeaturesService - auto accept disabled', () => { const discoverFeaturesService = new V2DiscoverFeaturesService( featureRegistry, eventEmitter, - new DispatcherMock(), + new MessageHandlerRegistryMock(), new LoggerMock(), discoverFeaturesModuleConfig ) diff --git a/packages/core/src/modules/discover-features/services/DiscoverFeaturesService.ts b/packages/core/src/modules/discover-features/services/DiscoverFeaturesService.ts index fb5cd56a1e..c9e532b4c7 100644 --- a/packages/core/src/modules/discover-features/services/DiscoverFeaturesService.ts +++ b/packages/core/src/modules/discover-features/services/DiscoverFeaturesService.ts @@ -1,5 +1,4 @@ import type { AgentMessage } from '../../../agent/AgentMessage' -import type { Dispatcher } from '../../../agent/Dispatcher' import type { EventEmitter } from '../../../agent/EventEmitter' import type { FeatureRegistry } from '../../../agent/FeatureRegistry' import type { InboundMessageContext } from '../../../agent/models/InboundMessageContext' @@ -14,20 +13,17 @@ import type { export abstract class DiscoverFeaturesService { protected featureRegistry: FeatureRegistry protected eventEmitter: EventEmitter - protected dispatcher: Dispatcher protected logger: Logger protected discoverFeaturesModuleConfig: DiscoverFeaturesModuleConfig public constructor( featureRegistry: FeatureRegistry, eventEmitter: EventEmitter, - dispatcher: Dispatcher, logger: Logger, discoverFeaturesModuleConfig: DiscoverFeaturesModuleConfig ) { this.featureRegistry = featureRegistry this.eventEmitter = eventEmitter - this.dispatcher = dispatcher this.logger = logger this.discoverFeaturesModuleConfig = discoverFeaturesModuleConfig } diff --git a/packages/core/src/modules/routing/MediatorApi.ts b/packages/core/src/modules/routing/MediatorApi.ts index 78a1cbd849..af477e6052 100644 --- a/packages/core/src/modules/routing/MediatorApi.ts +++ b/packages/core/src/modules/routing/MediatorApi.ts @@ -2,8 +2,8 @@ import type { MediationRecord } from './repository' import type { EncryptedMessage } from '../../types' import { AgentContext } from '../../agent' -import { Dispatcher } from '../../agent/Dispatcher' import { EventEmitter } from '../../agent/EventEmitter' +import { MessageHandlerRegistry } from '../../agent/MessageHandlerRegistry' import { MessageSender } from '../../agent/MessageSender' import { OutboundMessageContext } from '../../agent/models' import { injectable } from '../../plugins' @@ -28,7 +28,7 @@ export class MediatorApi { private connectionService: ConnectionService public constructor( - dispatcher: Dispatcher, + messageHandlerRegistry: MessageHandlerRegistry, mediationService: MediatorService, messagePickupService: MessagePickupService, // Only imported so it is injected and handlers are registered @@ -46,7 +46,7 @@ export class MediatorApi { this.connectionService = connectionService this.agentContext = agentContext this.config = config - this.registerMessageHandlers(dispatcher) + this.registerMessageHandlers(messageHandlerRegistry) } public async initialize() { @@ -85,13 +85,13 @@ export class MediatorApi { return this.messagePickupService.queueMessage(connectionId, message) } - private registerMessageHandlers(dispatcher: Dispatcher) { - dispatcher.registerMessageHandler(new KeylistUpdateHandler(this.mediatorService)) - dispatcher.registerMessageHandler( + private registerMessageHandlers(messageHandlerRegistry: MessageHandlerRegistry) { + messageHandlerRegistry.registerMessageHandler(new KeylistUpdateHandler(this.mediatorService)) + messageHandlerRegistry.registerMessageHandler( new ForwardHandler(this.mediatorService, this.connectionService, this.messageSender) ) - dispatcher.registerMessageHandler(new BatchPickupHandler(this.messagePickupService)) - dispatcher.registerMessageHandler(new BatchHandler(this.eventEmitter)) - dispatcher.registerMessageHandler(new MediationRequestHandler(this.mediatorService, this.config)) + messageHandlerRegistry.registerMessageHandler(new BatchPickupHandler(this.messagePickupService)) + messageHandlerRegistry.registerMessageHandler(new BatchHandler(this.eventEmitter)) + messageHandlerRegistry.registerMessageHandler(new MediationRequestHandler(this.mediatorService, this.config)) } } diff --git a/packages/core/src/modules/routing/RecipientApi.ts b/packages/core/src/modules/routing/RecipientApi.ts index e74ee664ca..65216881b6 100644 --- a/packages/core/src/modules/routing/RecipientApi.ts +++ b/packages/core/src/modules/routing/RecipientApi.ts @@ -8,9 +8,9 @@ import { firstValueFrom, interval, merge, ReplaySubject, Subject, timer } from ' import { delayWhen, filter, first, takeUntil, tap, throttleTime, timeout } from 'rxjs/operators' import { AgentContext } from '../../agent' -import { Dispatcher } from '../../agent/Dispatcher' import { EventEmitter } from '../../agent/EventEmitter' import { filterContextCorrelationId } from '../../agent/Events' +import { MessageHandlerRegistry } from '../../agent/MessageHandlerRegistry' import { MessageSender } from '../../agent/MessageSender' import { OutboundMessageContext } from '../../agent/models' import { InjectionSymbols } from '../../constants' @@ -58,7 +58,7 @@ export class RecipientApi { private readonly stopMessagePickup$ = new Subject() public constructor( - dispatcher: Dispatcher, + messageHandlerRegistry: MessageHandlerRegistry, mediationRecipientService: MediationRecipientService, connectionService: ConnectionService, dids: DidsApi, @@ -84,7 +84,7 @@ export class RecipientApi { this.agentContext = agentContext this.stop$ = stop$ this.config = recipientModuleConfig - this.registerMessageHandlers(dispatcher) + this.registerMessageHandlers(messageHandlerRegistry) } public async initialize() { @@ -484,12 +484,12 @@ export class RecipientApi { } // Register handlers for the several messages for the mediator. - private registerMessageHandlers(dispatcher: Dispatcher) { - dispatcher.registerMessageHandler(new KeylistUpdateResponseHandler(this.mediationRecipientService)) - dispatcher.registerMessageHandler(new MediationGrantHandler(this.mediationRecipientService)) - dispatcher.registerMessageHandler(new MediationDenyHandler(this.mediationRecipientService)) - dispatcher.registerMessageHandler(new StatusHandler(this.mediationRecipientService)) - dispatcher.registerMessageHandler(new MessageDeliveryHandler(this.mediationRecipientService)) - //dispatcher.registerMessageHandler(new KeylistListHandler(this.mediationRecipientService)) // TODO: write this + private registerMessageHandlers(messageHandlerRegistry: MessageHandlerRegistry) { + messageHandlerRegistry.registerMessageHandler(new KeylistUpdateResponseHandler(this.mediationRecipientService)) + messageHandlerRegistry.registerMessageHandler(new MediationGrantHandler(this.mediationRecipientService)) + messageHandlerRegistry.registerMessageHandler(new MediationDenyHandler(this.mediationRecipientService)) + messageHandlerRegistry.registerMessageHandler(new StatusHandler(this.mediationRecipientService)) + messageHandlerRegistry.registerMessageHandler(new MessageDeliveryHandler(this.mediationRecipientService)) + //messageHandlerRegistry.registerMessageHandler(new KeylistListHandler(this.mediationRecipientService)) // TODO: write this } } diff --git a/packages/core/src/modules/routing/protocol/pickup/v1/MessagePickupService.ts b/packages/core/src/modules/routing/protocol/pickup/v1/MessagePickupService.ts index 9211359eb0..aa97fc51a4 100644 --- a/packages/core/src/modules/routing/protocol/pickup/v1/MessagePickupService.ts +++ b/packages/core/src/modules/routing/protocol/pickup/v1/MessagePickupService.ts @@ -2,8 +2,8 @@ import type { BatchPickupMessage } from './messages' import type { InboundMessageContext } from '../../../../../agent/models/InboundMessageContext' import type { EncryptedMessage } from '../../../../../types' -import { Dispatcher } from '../../../../../agent/Dispatcher' import { EventEmitter } from '../../../../../agent/EventEmitter' +import { MessageHandlerRegistry } from '../../../../../agent/MessageHandlerRegistry' import { OutboundMessageContext } from '../../../../../agent/models' import { InjectionSymbols } from '../../../../../constants' import { inject, injectable } from '../../../../../plugins' @@ -15,19 +15,17 @@ import { BatchMessage, BatchMessageMessage } from './messages' @injectable() export class MessagePickupService { private messageRepository: MessageRepository - private dispatcher: Dispatcher private eventEmitter: EventEmitter public constructor( @inject(InjectionSymbols.MessageRepository) messageRepository: MessageRepository, - dispatcher: Dispatcher, + messageHandlerRegistry: MessageHandlerRegistry, eventEmitter: EventEmitter ) { this.messageRepository = messageRepository - this.dispatcher = dispatcher this.eventEmitter = eventEmitter - this.registerMessageHandlers() + this.registerMessageHandlers(messageHandlerRegistry) } public async batch(messageContext: InboundMessageContext) { @@ -57,8 +55,8 @@ export class MessagePickupService { await this.messageRepository.add(connectionId, message) } - protected registerMessageHandlers() { - this.dispatcher.registerMessageHandler(new BatchPickupHandler(this)) - this.dispatcher.registerMessageHandler(new BatchHandler(this.eventEmitter)) + protected registerMessageHandlers(messageHandlerRegistry: MessageHandlerRegistry) { + messageHandlerRegistry.registerMessageHandler(new BatchPickupHandler(this)) + messageHandlerRegistry.registerMessageHandler(new BatchHandler(this.eventEmitter)) } } diff --git a/packages/core/src/modules/routing/protocol/pickup/v2/V2MessagePickupService.ts b/packages/core/src/modules/routing/protocol/pickup/v2/V2MessagePickupService.ts index 147467f10d..dc99c47856 100644 --- a/packages/core/src/modules/routing/protocol/pickup/v2/V2MessagePickupService.ts +++ b/packages/core/src/modules/routing/protocol/pickup/v2/V2MessagePickupService.ts @@ -2,7 +2,7 @@ import type { DeliveryRequestMessage, MessagesReceivedMessage, StatusRequestMess import type { InboundMessageContext } from '../../../../../agent/models/InboundMessageContext' import type { EncryptedMessage } from '../../../../../types' -import { Dispatcher } from '../../../../../agent/Dispatcher' +import { MessageHandlerRegistry } from '../../../../../agent/MessageHandlerRegistry' import { OutboundMessageContext } from '../../../../../agent/models' import { InjectionSymbols } from '../../../../../constants' import { Attachment } from '../../../../../decorators/attachment/Attachment' @@ -23,19 +23,17 @@ import { MessageDeliveryMessage, StatusMessage } from './messages' @injectable() export class V2MessagePickupService { private messageRepository: MessageRepository - private dispatcher: Dispatcher private mediationRecipientService: MediationRecipientService public constructor( @inject(InjectionSymbols.MessageRepository) messageRepository: MessageRepository, - dispatcher: Dispatcher, + messageHandlerRegistry: MessageHandlerRegistry, mediationRecipientService: MediationRecipientService ) { this.messageRepository = messageRepository - this.dispatcher = dispatcher this.mediationRecipientService = mediationRecipientService - this.registerMessageHandlers() + this.registerMessageHandlers(messageHandlerRegistry) } public async processStatusRequest(messageContext: InboundMessageContext) { @@ -116,11 +114,11 @@ export class V2MessagePickupService { return new OutboundMessageContext(statusMessage, { agentContext: messageContext.agentContext, connection }) } - protected registerMessageHandlers() { - this.dispatcher.registerMessageHandler(new StatusRequestHandler(this)) - this.dispatcher.registerMessageHandler(new DeliveryRequestHandler(this)) - this.dispatcher.registerMessageHandler(new MessagesReceivedHandler(this)) - this.dispatcher.registerMessageHandler(new StatusHandler(this.mediationRecipientService)) - this.dispatcher.registerMessageHandler(new MessageDeliveryHandler(this.mediationRecipientService)) + protected registerMessageHandlers(messageHandlerRegistry: MessageHandlerRegistry) { + messageHandlerRegistry.registerMessageHandler(new StatusRequestHandler(this)) + messageHandlerRegistry.registerMessageHandler(new DeliveryRequestHandler(this)) + messageHandlerRegistry.registerMessageHandler(new MessagesReceivedHandler(this)) + messageHandlerRegistry.registerMessageHandler(new StatusHandler(this.mediationRecipientService)) + messageHandlerRegistry.registerMessageHandler(new MessageDeliveryHandler(this.mediationRecipientService)) } } diff --git a/packages/core/src/modules/routing/services/__tests__/V2MessagePickupService.test.ts b/packages/core/src/modules/routing/services/__tests__/V2MessagePickupService.test.ts index 53012f73b0..95055f4945 100644 --- a/packages/core/src/modules/routing/services/__tests__/V2MessagePickupService.test.ts +++ b/packages/core/src/modules/routing/services/__tests__/V2MessagePickupService.test.ts @@ -2,7 +2,7 @@ import type { MessageRepository } from '../../../../storage/MessageRepository' import type { EncryptedMessage } from '../../../../types' import { getAgentContext, getMockConnection, mockFunction } from '../../../../../tests/helpers' -import { Dispatcher } from '../../../../agent/Dispatcher' +import { MessageHandlerRegistry } from '../../../../agent/MessageHandlerRegistry' import { InboundMessageContext } from '../../../../agent/models/InboundMessageContext' import { InMemoryMessageRepository } from '../../../../storage/InMemoryMessageRepository' import { DidExchangeState } from '../../../connections' @@ -23,11 +23,11 @@ const mockConnection = getMockConnection({ // Mock classes jest.mock('../MediationRecipientService') jest.mock('../../../../storage/InMemoryMessageRepository') -jest.mock('../../../../agent/Dispatcher') +jest.mock('../../../../agent/MessageHandlerRegistry') // Mock typed object const MediationRecipientServiceMock = MediationRecipientService as jest.Mock -const DispatcherMock = Dispatcher as jest.Mock +const MessageHandlerRegistryMock = MessageHandlerRegistry as jest.Mock const InMessageRepositoryMock = InMemoryMessageRepository as jest.Mock const agentContext = getAgentContext() @@ -45,11 +45,11 @@ describe('V2MessagePickupService', () => { let messageRepository: MessageRepository beforeEach(async () => { - const dispatcher = new DispatcherMock() + const messageHandlerRegistry = new MessageHandlerRegistryMock() const mediationRecipientService = new MediationRecipientServiceMock() messageRepository = new InMessageRepositoryMock() - pickupService = new V2MessagePickupService(messageRepository, dispatcher, mediationRecipientService) + pickupService = new V2MessagePickupService(messageRepository, messageHandlerRegistry, mediationRecipientService) }) describe('processStatusRequest', () => { diff --git a/packages/question-answer/src/QuestionAnswerApi.ts b/packages/question-answer/src/QuestionAnswerApi.ts index 6fed567575..97ea98c143 100644 --- a/packages/question-answer/src/QuestionAnswerApi.ts +++ b/packages/question-answer/src/QuestionAnswerApi.ts @@ -5,7 +5,6 @@ import { AgentContext, ConnectionService, OutboundMessageContext, - Dispatcher, injectable, MessageSender, } from '@aries-framework/core' @@ -22,7 +21,6 @@ export class QuestionAnswerApi { private agentContext: AgentContext public constructor( - dispatcher: Dispatcher, questionAnswerService: QuestionAnswerService, messageSender: MessageSender, connectionService: ConnectionService, @@ -32,7 +30,11 @@ export class QuestionAnswerApi { this.messageSender = messageSender this.connectionService = connectionService this.agentContext = agentContext - this.registerMessageHandlers(dispatcher) + + this.agentContext.dependencyManager.registerMessageHandlers([ + new QuestionMessageHandler(this.questionAnswerService), + new AnswerMessageHandler(this.questionAnswerService), + ]) } /** @@ -131,9 +133,4 @@ export class QuestionAnswerApi { public findById(questionAnswerId: string) { return this.questionAnswerService.findById(this.agentContext, questionAnswerId) } - - private registerMessageHandlers(dispatcher: Dispatcher) { - dispatcher.registerMessageHandler(new QuestionMessageHandler(this.questionAnswerService)) - dispatcher.registerMessageHandler(new AnswerMessageHandler(this.questionAnswerService)) - } } diff --git a/samples/extension-module/dummy/DummyApi.ts b/samples/extension-module/dummy/DummyApi.ts index 1bb998336c..9d4aa765d3 100644 --- a/samples/extension-module/dummy/DummyApi.ts +++ b/samples/extension-module/dummy/DummyApi.ts @@ -5,7 +5,6 @@ import { OutboundMessageContext, AgentContext, ConnectionService, - Dispatcher, injectable, MessageSender, } from '@aries-framework/core' @@ -22,7 +21,6 @@ export class DummyApi { private agentContext: AgentContext public constructor( - dispatcher: Dispatcher, messageSender: MessageSender, dummyService: DummyService, connectionService: ConnectionService, @@ -33,7 +31,10 @@ export class DummyApi { this.connectionService = connectionService this.agentContext = agentContext - this.registerMessageHandlers(dispatcher) + this.agentContext.dependencyManager.registerMessageHandlers([ + new DummyRequestHandler(this.dummyService), + new DummyResponseHandler(this.dummyService), + ]) } /** @@ -93,9 +94,4 @@ export class DummyApi { public findAllByQuery(query: Query): Promise { return this.dummyService.findAllByQuery(this.agentContext, query) } - - private registerMessageHandlers(dispatcher: Dispatcher) { - dispatcher.registerMessageHandler(new DummyRequestHandler(this.dummyService)) - dispatcher.registerMessageHandler(new DummyResponseHandler(this.dummyService)) - } }