Skip to content
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
15 changes: 9 additions & 6 deletions packages/didcomm/src/DidCommMessageSender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -548,13 +548,16 @@ export class DidCommMessageSender {
})
}

// If transport priority is set we will sort services by our priority
// If transport priority is set we will sort services by our priority.
// Services whose scheme is not listed in the priority list are kept but
// sorted after any prioritized ones (indexOf returns -1 for them, which
// would otherwise incorrectly rank them first).
if (transportPriority?.schemes) {
services = services.sort((a, b) => {
const aScheme = utils.getProtocolScheme(a.serviceEndpoint)
const bScheme = utils.getProtocolScheme(b.serviceEndpoint)
return transportPriority?.schemes.indexOf(aScheme) - transportPriority?.schemes.indexOf(bScheme)
})
const schemePriority = (endpoint: string) => {
const index = transportPriority.schemes.indexOf(utils.getProtocolScheme(endpoint))
return index === -1 ? Number.MAX_SAFE_INTEGER : index
}
services = services.sort((a, b) => schemePriority(a.serviceEndpoint) - schemePriority(b.serviceEndpoint))
}

agentContext.config.logger.debug(
Expand Down
40 changes: 40 additions & 0 deletions packages/didcomm/src/__tests__/DidCommMessageSender.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,46 @@ describe('DidCommMessageSender', () => {
},
})
})

test('prioritizes services whose scheme is listed in transportPriority over ones that are not', async () => {
// Simulate a DID Document exposing both https and wss service endpoints,
// where the https one is returned first by the resolver.
const httpsService = new DidCommV1Service({
id: '<did>;https',
serviceEndpoint: 'https://www.example.com',
recipientKeys: ['#authentication-1'],
})
const wssService = new DidCommV1Service({
id: '<did>;wss',
serviceEndpoint: 'wss://www.example.com',
recipientKeys: ['#authentication-1'],
})

resolveCreatedDidDocumentWithKeysMock.mockResolvedValue({
didDocument: getMockDidDocument({ service: [httpsService, wssService] }),
keys: [],
})
didResolverServiceResolveDidServicesMock.mockResolvedValue([
getMockResolvedDidService(httpsService),
getMockResolvedDidService(wssService),
])

const httpTransport = new DummyHttpOutboundTransport()
const wsTransport = new DummyWsOutboundTransport()
didCommModuleConfig.outboundTransports = [httpTransport, wsTransport]
const httpSendSpy = vi.spyOn(httpTransport, 'sendMessage')
const wsSendSpy = vi.spyOn(wsTransport, 'sendMessage')

await messageSender.sendMessage(outboundMessageContext, {
transportPriority: { schemes: ['wss', 'ws'] },
})

// The wss service must be tried first, not the https one, even though
// https is not listed in the priority schemes.
expect(wsSendSpy).toHaveBeenCalledTimes(1)
expect(wsSendSpy).toHaveBeenCalledWith(expect.objectContaining({ endpoint: wssService.serviceEndpoint }))
expect(httpSendSpy).not.toHaveBeenCalled()
})
})

describe('sendMessageToService', () => {
Expand Down
Loading