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 all 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
53 changes: 27 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,35 @@ 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 yet.`)
}

public async processForwardMessage(
Expand Down Expand Up @@ -150,7 +151,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
15 changes: 15 additions & 0 deletions packages/core/src/storage/migration/__tests__/0.1.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { DidInfo, DidConfig } from '../../../wallet'
import type { V0_1ToV0_2UpdateConfig } from '../updates/0.1-0.2'

import { unlinkSync, readFileSync } from 'fs'
Expand All @@ -9,6 +10,7 @@ import { Agent } from '../../../../src'
import { agentDependencies } from '../../../../tests/helpers'
import { InjectionSymbols } from '../../../constants'
import * as uuid from '../../../utils/uuid'
import { IndyWallet } from '../../../wallet/IndyWallet'
import { UpdateAssistant } from '../UpdateAssistant'

const backupDate = new Date('2022-01-21T22:50:20.522Z')
Expand All @@ -28,6 +30,19 @@ const mediationRoleUpdateStrategies: V0_1ToV0_2UpdateConfig['mediationRoleUpdate
]

describe('UpdateAssistant | v0.1 - v0.2', () => {
let createDidSpy: jest.SpyInstance<Promise<DidInfo>, [didConfig?: DidConfig | undefined]>

beforeAll(async () => {
// We need to mock did generation to create a consistent mediator routing record across sessions
createDidSpy = jest
.spyOn(IndyWallet.prototype, 'createDid')
.mockImplementation(async () => ({ did: 'mock-did', verkey: 'ocxwFbXouLkzuTCyyjFg1bPGK3nM6aPv1pZ6fn5RNgD' }))
})

afterAll(async () => {
createDidSpy.mockReset()
})

it(`should correctly update the role in the mediation record`, async () => {
const aliceMediationRecordsString = readFileSync(
path.join(__dirname, '__fixtures__/alice-4-mediators-0.1.json'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,20 @@ Object {
"threadId": "0b2f1133-ced9-49f1-83a1-eb6ba1c24cdf",
},
},
"MEDIATOR_ROUTING_RECORD": Object {
"id": "MEDIATOR_ROUTING_RECORD",
"tags": Object {},
"type": "MediatorRoutingRecord",
"value": Object {
"_tags": Object {},
"createdAt": "2022-01-21T22:50:20.522Z",
"id": "MEDIATOR_ROUTING_RECORD",
"metadata": Object {},
"routingKeys": Array [
"ocxwFbXouLkzuTCyyjFg1bPGK3nM6aPv1pZ6fn5RNgD",
],
},
},
"STORAGE_VERSION_RECORD_ID": Object {
"id": "STORAGE_VERSION_RECORD_ID",
"tags": Object {},
Expand Down Expand Up @@ -2961,6 +2975,20 @@ Object {
"role": "receiver",
},
},
"MEDIATOR_ROUTING_RECORD": Object {
"id": "MEDIATOR_ROUTING_RECORD",
"tags": Object {},
"type": "MediatorRoutingRecord",
"value": Object {
"_tags": Object {},
"createdAt": "2022-01-21T22:50:20.522Z",
"id": "MEDIATOR_ROUTING_RECORD",
"metadata": Object {},
"routingKeys": Array [
"ocxwFbXouLkzuTCyyjFg1bPGK3nM6aPv1pZ6fn5RNgD",
],
},
},
"STORAGE_VERSION_RECORD_ID": Object {
"id": "STORAGE_VERSION_RECORD_ID",
"tags": Object {},
Expand Down