Skip to content

Commit

Permalink
fix(routing): mediation recipient role for recipient (#661)
Browse files Browse the repository at this point in the history
Signed-off-by: Timo Glastra <[email protected]>
  • Loading branch information
TimoGlastra authored Mar 18, 2022
1 parent 3411f1d commit 88ad790
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 52 deletions.
14 changes: 14 additions & 0 deletions packages/core/src/modules/routing/repository/MediationRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@ export class MediationRecord
}
}

public addRecipientKey(recipientKey: string) {
this.recipientKeys.push(recipientKey)
}

public removeRecipientKey(recipientKey: string): boolean {
const index = this.recipientKeys.indexOf(recipientKey, 0)
if (index > -1) {
this.recipientKeys.splice(index, 1)
return true
}

return false
}

public get isReady() {
return this.state === MediationState.Granted
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class MediationRecipientService {
const mediationRecord = new MediationRecord({
threadId: message.threadId,
state: MediationState.Requested,
role: MediationRole.Mediator,
role: MediationRole.Recipient,
connectionId: connection.id,
})
await this.mediatorRepository.save(mediationRecord)
Expand All @@ -81,6 +81,7 @@ export class MediationRecipientService {

// Assert
mediationRecord.assertState(MediationState.Requested)
mediationRecord.assertRole(MediationRole.Recipient)

// Update record
mediationRecord.endpoint = messageContext.message.endpoint
Expand All @@ -93,16 +94,23 @@ export class MediationRecipientService {
const connection = messageContext.assertReadyConnection()

const mediationRecord = await this.mediatorRepository.getByConnectionId(connection.id)

// Assert
mediationRecord.assertReady()
mediationRecord.assertRole(MediationRole.Recipient)

const keylist = messageContext.message.updated

// update keylist in mediationRecord
for (const update of keylist) {
if (update.action === KeylistUpdateAction.add) {
await this.saveRoute(update.recipientKey, mediationRecord)
mediationRecord.addRecipientKey(update.recipientKey)
} else if (update.action === KeylistUpdateAction.remove) {
await this.removeRoute(update.recipientKey, mediationRecord)
mediationRecord.removeRecipientKey(update.recipientKey)
}
}

await this.mediatorRepository.update(mediationRecord)
this.eventEmitter.emit<KeylistUpdatedEvent>({
type: RoutingEventTypes.RecipientKeylistUpdated,
payload: {
Expand All @@ -120,6 +128,9 @@ export class MediationRecipientService {
const message = this.createKeylistUpdateMessage(verKey)
const connection = await this.connectionService.getById(mediationRecord.connectionId)

mediationRecord.assertReady()
mediationRecord.assertRole(MediationRole.Recipient)

// Create observable for event
const observable = this.eventEmitter.observable<KeylistUpdatedEvent>(RoutingEventTypes.RecipientKeylistUpdated)
const subject = new ReplaySubject<KeylistUpdatedEvent>(1)
Expand Down Expand Up @@ -182,19 +193,6 @@ export class MediationRecipientService {
return { endpoints, routingKeys, did, verkey, mediatorId: mediationRecord?.id }
}

public async saveRoute(recipientKey: string, mediationRecord: MediationRecord) {
mediationRecord.recipientKeys.push(recipientKey)
this.mediatorRepository.update(mediationRecord)
}

public async removeRoute(recipientKey: string, mediationRecord: MediationRecord) {
const index = mediationRecord.recipientKeys.indexOf(recipientKey, 0)
if (index > -1) {
mediationRecord.recipientKeys.splice(index, 1)
}
this.mediatorRepository.update(mediationRecord)
}

public async processMediationDeny(messageContext: InboundMessageContext<MediationDenyMessage>) {
const connection = messageContext.assertReadyConnection()

Expand All @@ -206,6 +204,7 @@ export class MediationRecipientService {
}

// Assert
mediationRecord.assertRole(MediationRole.Recipient)
mediationRecord.assertState(MediationState.Requested)

// Update record
Expand Down
48 changes: 12 additions & 36 deletions packages/core/src/modules/routing/services/MediatorService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export class MediatorService {

// Assert mediation record is ready to be used
mediationRecord.assertReady()
mediationRecord.assertRole(MediationRole.Mediator)

return {
encryptedMessage: message.message,
Expand All @@ -108,63 +109,38 @@ export class MediatorService {

const mediationRecord = await this.mediationRepository.getByConnectionId(connection.id)

mediationRecord.assertReady()
mediationRecord.assertRole(MediationRole.Mediator)

for (const update of message.updates) {
const updated = new KeylistUpdated({
action: update.action,
recipientKey: update.recipientKey,
result: KeylistUpdateResult.NoChange,
})
if (update.action === KeylistUpdateAction.add) {
updated.result = await this.saveRoute(update.recipientKey, mediationRecord)
mediationRecord.addRecipientKey(update.recipientKey)
updated.result = KeylistUpdateResult.Success

keylist.push(updated)
} else if (update.action === KeylistUpdateAction.remove) {
updated.result = await this.removeRoute(update.recipientKey, mediationRecord)
const success = mediationRecord.removeRecipientKey(update.recipientKey)
updated.result = success ? KeylistUpdateResult.Success : KeylistUpdateResult.NoChange
keylist.push(updated)
}
}

return new KeylistUpdateResponseMessage({ keylist, threadId: message.threadId })
}

public async saveRoute(recipientKey: string, mediationRecord: MediationRecord) {
try {
mediationRecord.recipientKeys.push(recipientKey)
this.mediationRepository.update(mediationRecord)
return KeylistUpdateResult.Success
} catch (error) {
this.agentConfig.logger.error(
`Error processing keylist update action for verkey '${recipientKey}' and mediation record '${mediationRecord.id}'`
)
return KeylistUpdateResult.ServerError
}
}

public async removeRoute(recipientKey: string, mediationRecord: MediationRecord) {
try {
const index = mediationRecord.recipientKeys.indexOf(recipientKey, 0)
if (index > -1) {
mediationRecord.recipientKeys.splice(index, 1)

await this.mediationRepository.update(mediationRecord)
return KeylistUpdateResult.Success
}
await this.mediationRepository.update(mediationRecord)

return KeylistUpdateResult.ServerError
} catch (error) {
this.agentConfig.logger.error(
`Error processing keylist remove action for verkey '${recipientKey}' and mediation record '${mediationRecord.id}'`
)
return KeylistUpdateResult.ServerError
}
return new KeylistUpdateResponseMessage({ keylist, threadId: message.threadId })
}

public async createGrantMediationMessage(mediationRecord: MediationRecord) {
// Assert
mediationRecord.assertState(MediationState.Requested)
mediationRecord.assertRole(MediationRole.Mediator)

mediationRecord.state = MediationState.Granted
await this.mediationRepository.update(mediationRecord)
await this.updateState(mediationRecord, MediationState.Granted)

const message = new MediationGrantMessage({
endpoint: this.agentConfig.endpoints[0],
Expand Down

0 comments on commit 88ad790

Please sign in to comment.