Skip to content

Commit

Permalink
feat(routing): add routing service (#909)
Browse files Browse the repository at this point in the history
Signed-off-by: Timo Glastra <timo@animo.id>
  • Loading branch information
TimoGlastra authored Jun 28, 2022
1 parent 90dc7bb commit 6e51e90
Show file tree
Hide file tree
Showing 23 changed files with 355 additions and 159 deletions.
5 changes: 4 additions & 1 deletion packages/core/src/agent/Agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { ProofsModule } from '../modules/proofs/ProofsModule'
import { QuestionAnswerModule } from '../modules/question-answer/QuestionAnswerModule'
import { MediatorModule } from '../modules/routing/MediatorModule'
import { RecipientModule } from '../modules/routing/RecipientModule'
import { RoutingService } from '../modules/routing/services/RoutingService'
import { StorageUpdateService } from '../storage'
import { InMemoryMessageRepository } from '../storage/InMemoryMessageRepository'
import { IndyStorageService } from '../storage/IndyStorageService'
Expand Down Expand Up @@ -53,6 +54,7 @@ export class Agent {
private _isInitialized = false
public messageSubscription: Subscription
private walletService: Wallet
private routingService: RoutingService

public readonly connections: ConnectionsModule
public readonly proofs: ProofsModule
Expand Down Expand Up @@ -117,6 +119,7 @@ export class Agent {
this.messageReceiver = this.container.resolve(MessageReceiver)
this.transportService = this.container.resolve(TransportService)
this.walletService = this.container.resolve(InjectionSymbols.Wallet)
this.routingService = this.container.resolve(RoutingService)

// We set the modules in the constructor because that allows to set them as read-only
this.connections = this.container.resolve(ConnectionsModule)
Expand Down Expand Up @@ -284,7 +287,7 @@ export class Agent {
if (!connection) {
this.logger.debug('Mediation connection does not exist, creating connection')
// We don't want to use the current default mediator when connecting to another mediator
const routing = await this.mediationRecipient.getRouting({ useDefaultMediator: false })
const routing = await this.routingService.getRouting({ useDefaultMediator: false })

this.logger.debug('Routing created', routing)
const { connectionRecord: newConnection } = await this.oob.receiveInvitation(outOfBandInvitation, {
Expand Down
15 changes: 7 additions & 8 deletions packages/core/src/modules/connections/ConnectionsModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { AriesFrameworkError } from '../../error'
import { DidResolverService } from '../dids'
import { DidRepository } from '../dids/repository'
import { OutOfBandService } from '../oob/OutOfBandService'
import { MediationRecipientService } from '../routing/services/MediationRecipientService'
import { RoutingService } from '../routing/services/RoutingService'

import { DidExchangeProtocol } from './DidExchangeProtocol'
import {
Expand All @@ -38,7 +38,7 @@ export class ConnectionsModule {
private outOfBandService: OutOfBandService
private messageSender: MessageSender
private trustPingService: TrustPingService
private mediationRecipientService: MediationRecipientService
private routingService: RoutingService
private didRepository: DidRepository
private didResolverService: DidResolverService

Expand All @@ -49,7 +49,7 @@ export class ConnectionsModule {
connectionService: ConnectionService,
outOfBandService: OutOfBandService,
trustPingService: TrustPingService,
mediationRecipientService: MediationRecipientService,
routingService: RoutingService,
didRepository: DidRepository,
didResolverService: DidResolverService,
messageSender: MessageSender
Expand All @@ -59,7 +59,7 @@ export class ConnectionsModule {
this.connectionService = connectionService
this.outOfBandService = outOfBandService
this.trustPingService = trustPingService
this.mediationRecipientService = mediationRecipientService
this.routingService = routingService
this.didRepository = didRepository
this.messageSender = messageSender
this.didResolverService = didResolverService
Expand All @@ -79,8 +79,7 @@ export class ConnectionsModule {
) {
const { protocol, label, alias, imageUrl, autoAcceptConnection } = config

const routing =
config.routing || (await this.mediationRecipientService.getRouting({ mediatorId: outOfBandRecord.mediatorId }))
const routing = config.routing || (await this.routingService.getRouting({ mediatorId: outOfBandRecord.mediatorId }))

let result
if (protocol === HandshakeProtocol.DidExchange) {
Expand Down Expand Up @@ -270,7 +269,7 @@ export class ConnectionsModule {
this.agentConfig,
this.connectionService,
this.outOfBandService,
this.mediationRecipientService,
this.routingService,
this.didRepository
)
)
Expand All @@ -291,7 +290,7 @@ export class ConnectionsModule {
this.agentConfig,
this.didExchangeProtocol,
this.outOfBandService,
this.mediationRecipientService,
this.routingService,
this.didRepository
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { AgentConfig } from '../../../agent/AgentConfig'
import type { Handler, HandlerInboundMessage } from '../../../agent/Handler'
import type { DidRepository } from '../../dids/repository'
import type { OutOfBandService } from '../../oob/OutOfBandService'
import type { MediationRecipientService } from '../../routing'
import type { RoutingService } from '../../routing/services/RoutingService'
import type { ConnectionService } from '../services/ConnectionService'

import { createOutboundMessage } from '../../../agent/helpers'
Expand All @@ -13,21 +13,21 @@ export class ConnectionRequestHandler implements Handler {
private agentConfig: AgentConfig
private connectionService: ConnectionService
private outOfBandService: OutOfBandService
private mediationRecipientService: MediationRecipientService
private routingService: RoutingService
private didRepository: DidRepository
public supportedMessages = [ConnectionRequestMessage]

public constructor(
agentConfig: AgentConfig,
connectionService: ConnectionService,
outOfBandService: OutOfBandService,
mediationRecipientService: MediationRecipientService,
routingService: RoutingService,
didRepository: DidRepository
) {
this.agentConfig = agentConfig
this.connectionService = connectionService
this.outOfBandService = outOfBandService
this.mediationRecipientService = mediationRecipientService
this.routingService = routingService
this.didRepository = didRepository
}

Expand Down Expand Up @@ -59,7 +59,7 @@ export class ConnectionRequestHandler implements Handler {

if (connectionRecord?.autoAcceptConnection ?? this.agentConfig.autoAcceptConnections) {
// TODO: Allow rotation of keys used in the invitation for new ones not only when out-of-band is reusable
const routing = outOfBandRecord.reusable ? await this.mediationRecipientService.getRouting() : undefined
const routing = outOfBandRecord.reusable ? await this.routingService.getRouting() : undefined

const { message } = await this.connectionService.createResponse(connectionRecord, outOfBandRecord, routing)
return createOutboundMessage(connectionRecord, message, outOfBandRecord)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { AgentConfig } from '../../../agent/AgentConfig'
import type { Handler, HandlerInboundMessage } from '../../../agent/Handler'
import type { DidRepository } from '../../dids/repository'
import type { OutOfBandService } from '../../oob/OutOfBandService'
import type { MediationRecipientService } from '../../routing/services/MediationRecipientService'
import type { RoutingService } from '../../routing/services/RoutingService'
import type { DidExchangeProtocol } from '../DidExchangeProtocol'

import { createOutboundMessage } from '../../../agent/helpers'
Expand All @@ -14,21 +14,21 @@ export class DidExchangeRequestHandler implements Handler {
private didExchangeProtocol: DidExchangeProtocol
private outOfBandService: OutOfBandService
private agentConfig: AgentConfig
private mediationRecipientService: MediationRecipientService
private routingService: RoutingService
private didRepository: DidRepository
public supportedMessages = [DidExchangeRequestMessage]

public constructor(
agentConfig: AgentConfig,
didExchangeProtocol: DidExchangeProtocol,
outOfBandService: OutOfBandService,
mediationRecipientService: MediationRecipientService,
routingService: RoutingService,
didRepository: DidRepository
) {
this.agentConfig = agentConfig
this.didExchangeProtocol = didExchangeProtocol
this.outOfBandService = outOfBandService
this.mediationRecipientService = mediationRecipientService
this.routingService = routingService
this.didRepository = didRepository
}

Expand Down Expand Up @@ -72,7 +72,7 @@ export class DidExchangeRequestHandler implements Handler {
if (connectionRecord?.autoAcceptConnection ?? this.agentConfig.autoAcceptConnections) {
// TODO We should add an option to not pass routing and therefore do not rotate keys and use the keys from the invitation
// TODO: Allow rotation of keys used in the invitation for new ones not only when out-of-band is reusable
const routing = outOfBandRecord.reusable ? await this.mediationRecipientService.getRouting() : undefined
const routing = outOfBandRecord.reusable ? await this.routingService.getRouting() : undefined

const message = await this.didExchangeProtocol.createResponse(connectionRecord, outOfBandRecord, routing)
return createOutboundMessage(connectionRecord, message, outOfBandRecord)
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/modules/credentials/CredentialsModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import { AriesFrameworkError } from '../../error'
import { DidCommMessageRole } from '../../storage'
import { DidCommMessageRepository } from '../../storage/didcomm/DidCommMessageRepository'
import { ConnectionService } from '../connections/services'
import { MediationRecipientService } from '../routing'
import { RoutingService } from '../routing/services/RoutingService'

import { CredentialState } from './models/CredentialState'
import { V1CredentialService } from './protocol/v1/V1CredentialService'
Expand Down Expand Up @@ -95,7 +95,7 @@ export class CredentialsModule<
private credentialRepository: CredentialRepository
private agentConfig: AgentConfig
private didCommMessageRepo: DidCommMessageRepository
private mediatorRecipientService: MediationRecipientService
private routingService: RoutingService
private logger: Logger
private serviceMap: ServiceMap<CFs, CSs>

Expand All @@ -104,7 +104,7 @@ export class CredentialsModule<
connectionService: ConnectionService,
agentConfig: AgentConfig,
credentialRepository: CredentialRepository,
mediationRecipientService: MediationRecipientService,
mediationRecipientService: RoutingService,
didCommMessageRepository: DidCommMessageRepository,
v1Service: V1CredentialService,
v2Service: V2CredentialService<CFs>,
Expand All @@ -116,7 +116,7 @@ export class CredentialsModule<
this.connectionService = connectionService
this.credentialRepository = credentialRepository
this.agentConfig = agentConfig
this.mediatorRecipientService = mediationRecipientService
this.routingService = mediationRecipientService
this.didCommMessageRepo = didCommMessageRepository
this.logger = agentConfig.logger

Expand Down Expand Up @@ -304,7 +304,7 @@ export class CredentialsModule<
// Use ~service decorator otherwise
else if (offerMessage?.service) {
// Create ~service decorator
const routing = await this.mediatorRecipientService.getRouting()
const routing = await this.routingService.getRouting()
const ourService = new ServiceDecorator({
serviceEndpoint: routing.endpoints[0],
recipientKeys: [routing.recipientKey.publicKeyBase58],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { isLinkedAttachment } from '../../../../utils/attachment'
import { uuid } from '../../../../utils/uuid'
import { AckStatus } from '../../../common'
import { ConnectionService } from '../../../connections/services'
import { MediationRecipientService } from '../../../routing'
import { RoutingService } from '../../../routing/services/RoutingService'
import { CredentialProblemReportReason } from '../../errors'
import { IndyCredentialFormatService } from '../../formats/indy/IndyCredentialFormatService'
import { IndyCredPropose } from '../../formats/indy/models'
Expand Down Expand Up @@ -67,13 +67,13 @@ import { V1CredentialPreview } from './messages/V1CredentialPreview'
export class V1CredentialService extends CredentialService<[IndyCredentialFormat]> {
private connectionService: ConnectionService
private formatService: IndyCredentialFormatService
private mediationRecipientService: MediationRecipientService
private routingService: RoutingService

public constructor(
connectionService: ConnectionService,
didCommMessageRepository: DidCommMessageRepository,
agentConfig: AgentConfig,
mediationRecipientService: MediationRecipientService,
routingService: RoutingService,
dispatcher: Dispatcher,
eventEmitter: EventEmitter,
credentialRepository: CredentialRepository,
Expand All @@ -83,7 +83,7 @@ export class V1CredentialService extends CredentialService<[IndyCredentialFormat
this.connectionService = connectionService
this.formatService = formatService
this.didCommMessageRepository = didCommMessageRepository
this.mediationRecipientService = mediationRecipientService
this.routingService = routingService

this.registerHandlers()
}
Expand Down Expand Up @@ -1120,12 +1120,7 @@ export class V1CredentialService extends CredentialService<[IndyCredentialFormat
protected registerHandlers() {
this.dispatcher.registerHandler(new V1ProposeCredentialHandler(this, this.agentConfig))
this.dispatcher.registerHandler(
new V1OfferCredentialHandler(
this,
this.agentConfig,
this.mediationRecipientService,
this.didCommMessageRepository
)
new V1OfferCredentialHandler(this, this.agentConfig, this.routingService, this.didCommMessageRepository)
)
this.dispatcher.registerHandler(
new V1RequestCredentialHandler(this, this.agentConfig, this.didCommMessageRepository)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { uuid } from '../../../../../utils/uuid'
import { AckStatus } from '../../../../common'
import { DidExchangeState } from '../../../../connections'
import { ConnectionService } from '../../../../connections/services/ConnectionService'
import { MediationRecipientService } from '../../../../routing/services/MediationRecipientService'
import { RoutingService } from '../../../../routing/services/RoutingService'
import { CredentialEventTypes } from '../../../CredentialEvents'
import { credDef, credReq } from '../../../__tests__/fixtures'
import { CredentialProblemReportReason } from '../../../errors/CredentialProblemReportReason'
Expand Down Expand Up @@ -47,21 +47,21 @@ import {
jest.mock('../../../repository/CredentialRepository')
jest.mock('../../../formats/indy/IndyCredentialFormatService')
jest.mock('../../../../../storage/didcomm/DidCommMessageRepository')
jest.mock('../../../../routing/services/MediationRecipientService')
jest.mock('../../../../routing/services/RoutingService')
jest.mock('../../../../connections/services/ConnectionService')
jest.mock('../../../../../agent/Dispatcher')

// Mock typed object
const CredentialRepositoryMock = CredentialRepository as jest.Mock<CredentialRepository>
const IndyCredentialFormatServiceMock = IndyCredentialFormatService as jest.Mock<IndyCredentialFormatService>
const DidCommMessageRepositoryMock = DidCommMessageRepository as jest.Mock<DidCommMessageRepository>
const MediationRecipientServiceMock = MediationRecipientService as jest.Mock<MediationRecipientService>
const RoutingServiceMock = RoutingService as jest.Mock<RoutingService>
const ConnectionServiceMock = ConnectionService as jest.Mock<ConnectionService>
const DispatcherMock = Dispatcher as jest.Mock<Dispatcher>

const credentialRepository = new CredentialRepositoryMock()
const didCommMessageRepository = new DidCommMessageRepositoryMock()
const mediationRecipientService = new MediationRecipientServiceMock()
const routingService = new RoutingServiceMock()
const indyCredentialFormatService = new IndyCredentialFormatServiceMock()
const dispatcher = new DispatcherMock()
const connectionService = new ConnectionServiceMock()
Expand Down Expand Up @@ -239,7 +239,7 @@ describe('V1CredentialService', () => {
connectionService,
didCommMessageRepository,
agentConfig,
mediationRecipientService,
routingService,
dispatcher,
eventEmitter,
credentialRepository,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { JsonTransformer } from '../../../../../utils'
import { DidExchangeState } from '../../../../connections'
import { ConnectionService } from '../../../../connections/services/ConnectionService'
import { IndyLedgerService } from '../../../../ledger/services'
import { MediationRecipientService } from '../../../../routing/services/MediationRecipientService'
import { RoutingService } from '../../../../routing/services/RoutingService'
import { CredentialEventTypes } from '../../../CredentialEvents'
import { schema, credDef } from '../../../__tests__/fixtures'
import { IndyCredentialFormatService } from '../../../formats'
Expand All @@ -30,7 +30,7 @@ jest.mock('../../../repository/CredentialRepository')
jest.mock('../../../../ledger/services/IndyLedgerService')
jest.mock('../../../formats/indy/IndyCredentialFormatService')
jest.mock('../../../../../storage/didcomm/DidCommMessageRepository')
jest.mock('../../../../routing/services/MediationRecipientService')
jest.mock('../../../../routing/services/RoutingService')
jest.mock('../../../../connections/services/ConnectionService')
jest.mock('../../../../../agent/Dispatcher')

Expand All @@ -39,13 +39,13 @@ const CredentialRepositoryMock = CredentialRepository as jest.Mock<CredentialRep
const IndyLedgerServiceMock = IndyLedgerService as jest.Mock<IndyLedgerService>
const IndyCredentialFormatServiceMock = IndyCredentialFormatService as jest.Mock<IndyCredentialFormatService>
const DidCommMessageRepositoryMock = DidCommMessageRepository as jest.Mock<DidCommMessageRepository>
const MediationRecipientServiceMock = MediationRecipientService as jest.Mock<MediationRecipientService>
const RoutingServiceMock = RoutingService as jest.Mock<RoutingService>
const ConnectionServiceMock = ConnectionService as jest.Mock<ConnectionService>
const DispatcherMock = Dispatcher as jest.Mock<Dispatcher>

const credentialRepository = new CredentialRepositoryMock()
const didCommMessageRepository = new DidCommMessageRepositoryMock()
const mediationRecipientService = new MediationRecipientServiceMock()
const routingService = new RoutingServiceMock()
const indyLedgerService = new IndyLedgerServiceMock()
const indyCredentialFormatService = new IndyCredentialFormatServiceMock()
const dispatcher = new DispatcherMock()
Expand Down Expand Up @@ -108,7 +108,7 @@ describe('V1CredentialServiceProposeOffer', () => {
connectionService,
didCommMessageRepository,
agentConfig,
mediationRecipientService,
routingService,
dispatcher,
eventEmitter,
credentialRepository,
Expand Down
Loading

0 comments on commit 6e51e90

Please sign in to comment.