Skip to content

Commit

Permalink
feat(did-comm) Recipient handles MediateDeny
Browse files Browse the repository at this point in the history
  • Loading branch information
codynhat committed Jan 9, 2023
1 parent 6e95f3c commit e241fc2
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,41 @@ describe('coordinate-mediation-message-handler', () => {
expect(service?.serviceEndpoint).toEqual([{ uri: mediator.did }])
})

it('should remove service on mediate deny', async () => {
const mediateRequestMessage = createMediateRequestMessage(recipient.did, mediator.did)
const packedMessage = await agent.packDIDCommMessage({
packing: 'authcrypt',
message: mediateRequestMessage,
})
await agent.sendDIDCommMessage({
messageId: mediateRequestMessage.id,
packedMessage,
recipientDidUrl: mediator.did,
})

const msgid = v4()
const packedDenyMessage = await agent.packDIDCommMessage({
packing: 'authcrypt',
message: {
type: 'https://didcomm.org/coordinate-mediation/2.0/mediate-deny',
from: mediator.did,
to: recipient.did,
id: msgid,
thid: '',
body: {},
},
})
await agent.sendDIDCommMessage({
messageId: msgid,
packedMessage: packedDenyMessage,
recipientDidUrl: recipient.did,
})

const didDoc = (await agent.resolveDid({ didUrl: recipient.did })).didDocument
const service = didDoc?.service?.find((s) => s.id === `${recipient.did}#didcomm-mediator`)
expect(service).toBeUndefined()
})

it('should not save service if mediate request cannot be found', async () => {
const mediateGrantMessage = createMediateGrantMessage(recipient.did, mediator.did, '')
const packedMessage = await agent.packDIDCommMessage({
Expand All @@ -289,20 +324,20 @@ describe('coordinate-mediation-message-handler', () => {
})

it('should not save service if mediate grant message has bad routing_did', async () => {
const mediateGrantMessage = createMediateGrantMessage(recipient.did, mediator.did, '')
const msgid = v4()
const packedMessage = await agent.packDIDCommMessage({
packing: 'authcrypt',
message: {
type: 'https://didcomm.org/coordinate-mediation/2.0/mediate-grant',
from: mediator.did,
to: recipient.did,
id: v4(),
id: msgid,
thid: '',
body: {},
},
})
await agent.sendDIDCommMessage({
messageId: mediateGrantMessage.id,
messageId: msgid,
packedMessage,
recipientDidUrl: recipient.did,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,32 @@ export class CoordinateMediationRecipientMessageHandler extends AbstractMessageH
debug(ex)
}
return message
} else if (message.type === MEDIATE_DENY_MESSAGE_TYPE) {
debug('MediateDeny Message Received')
try {
const { from, to } = message
if (!from) {
throw new Error('invalid_argument: MediateGrant received without `from` set')
}
if (!to) {
throw new Error('invalid_argument: MediateGrant received without `to` set')
}

// Delete service if it exists
const did = await context.agent.didManagerGet({
did: to,
})
const existingService = did.services.find(
(s) =>
s.serviceEndpoint === from ||
(Array.isArray(s.serviceEndpoint) && s.serviceEndpoint.includes(from)),
)
if (existingService) {
await context.agent.didManagerRemoveService({ did: to, id: existingService.id })
}
} catch (ex) {
debug(ex)
}
}

return super.handle(message, context)
Expand Down

0 comments on commit e241fc2

Please sign in to comment.