Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: always initialize mediator #985

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core/src/agent/Agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ export class Agent {
const mediationConnection = await this.getMediationConnection(mediatorConnectionsInvite)
await this.mediationRecipient.provision(mediationConnection)
}

await this.mediator.initialize()
await this.mediationRecipient.initialize()

this._isInitialized = true
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/modules/routing/MediatorModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ export class MediatorModule {
this.registerHandlers(dispatcher)
}

public async initialize() {
await this.mediatorService.initialize()
}

public async grantRequestedMediation(mediatorId: string): Promise<MediationRecord> {
const record = await this.mediatorService.getById(mediatorId)
const connectionRecord = await this.connectionService.getById(record.connectionId)
Expand Down
55 changes: 29 additions & 26 deletions packages/core/src/modules/routing/services/MediatorService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,34 +49,37 @@ export class MediatorService {
this.eventEmitter = eventEmitter
}

private async getRoutingKeys() {
this.agentConfig.logger.debug('Retrieving mediator routing keys')
// If the routing record is not loaded yet, retrieve it from storage
if (!this._mediatorRoutingRecord) {
this.agentConfig.logger.debug('Mediator routing record not loaded yet, retrieving from storage')
let routingRecord = await this.mediatorRoutingRepository.findById(
this.mediatorRoutingRepository.MEDIATOR_ROUTING_RECORD_ID
)

// If we don't have a routing record yet, create it
if (!routingRecord) {
this.agentConfig.logger.debug('Mediator routing record does not exist yet, creating routing keys and record')
const { verkey } = await this.wallet.createDid()

routingRecord = new MediatorRoutingRecord({
id: this.mediatorRoutingRepository.MEDIATOR_ROUTING_RECORD_ID,
routingKeys: [verkey],
})

await this.mediatorRoutingRepository.save(routingRecord)
}
public async initialize() {
this.agentConfig.logger.debug('Mediator routing record not loaded yet, retrieving from storage')
let routingRecord = await this.mediatorRoutingRepository.findById(
this.mediatorRoutingRepository.MEDIATOR_ROUTING_RECORD_ID
)

// If we don't have a routing record yet, create it
if (!routingRecord) {
this.agentConfig.logger.debug('Mediator routing record does not exist yet, creating routing keys and record')
const { verkey } = await this.wallet.createDid()

routingRecord = new MediatorRoutingRecord({
id: this.mediatorRoutingRepository.MEDIATOR_ROUTING_RECORD_ID,
routingKeys: [verkey],
})

this._mediatorRoutingRecord = routingRecord
await this.mediatorRoutingRepository.save(routingRecord)
}

// Return the routing keys
this.agentConfig.logger.debug(`Returning mediator routing keys ${this._mediatorRoutingRecord.routingKeys}`)
return this._mediatorRoutingRecord.routingKeys
this._mediatorRoutingRecord = routingRecord
}

private getRoutingKeys() {
if (this._mediatorRoutingRecord) {
// Return the routing keys
this.agentConfig.logger.debug(`Returning mediator routing keys ${this._mediatorRoutingRecord.routingKeys}`)
return this._mediatorRoutingRecord.routingKeys
}
throw new AriesFrameworkError(
`Mediation service has not been initialized. Agent must include the config {mediationRole: MediationRole.Mediator}`
niall-shaw marked this conversation as resolved.
Show resolved Hide resolved
)
}

public async processForwardMessage(
Expand Down Expand Up @@ -150,7 +153,7 @@ export class MediatorService {

const message = new MediationGrantMessage({
endpoint: this.agentConfig.endpoints[0],
routingKeys: await this.getRoutingKeys(),
routingKeys: this.getRoutingKeys(),
threadId: mediationRecord.threadId,
})

Expand Down