Skip to content

Commit

Permalink
Use keys from inbound message instead of service when sending via ret…
Browse files Browse the repository at this point in the history
…run route

Signed-off-by: Jakub Koci <[email protected]>
  • Loading branch information
jakubkoci committed Jun 22, 2021
1 parent 3809402 commit f759f0e
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 40 deletions.
11 changes: 5 additions & 6 deletions src/agent/Dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,12 @@ class Dispatcher {

// Check for return routing, with thread id
if (message.hasReturnRouting(threadId)) {
// Find service with highest priority and keys to pack message
const [service] = this.transportService.findDidCommServices(outboundMessage.connection)
if (!service) {
throw new AriesFrameworkError(`Connection with id ${outboundMessage.connection.id} has no service!`)
const keys = {
recipientKeys: messageContext.senderVerkey ? [messageContext.senderVerkey] : [],
routingKeys: [],
senderKey: messageContext.connection?.verkey || null,
}
// TODO Should we validate inbound transport session scheme and service endpoint scheme?
return await this.messageSender.packMessage(outboundMessage, service)
return await this.messageSender.packMessage(outboundMessage, keys)
}

await this.messageSender.sendMessage(outboundMessage)
Expand Down
6 changes: 4 additions & 2 deletions src/agent/EnvelopeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Wallet } from '../wallet/Wallet'

import { AgentConfig } from './AgentConfig'

interface Keys {
export interface EnvelopeKeys {
recipientKeys: Verkey[]
routingKeys: Verkey[]
senderKey: Verkey | null
Expand All @@ -27,10 +27,12 @@ class EnvelopeService {
this.logger = agentConfig.logger
}

public async packMessage(keys: Keys, payload: AgentMessage): Promise<JsonWebKey> {
public async packMessage(payload: AgentMessage, keys: EnvelopeKeys): Promise<JsonWebKey> {
const { routingKeys, recipientKeys, senderKey: senderVk } = keys
const message = payload.toJSON()

this.logger.debug('Pack outbound message', { message })

let wireMessage = await this.wallet.pack(message, recipientKeys, senderVk)

if (routingKeys && routingKeys.length > 0) {
Expand Down
44 changes: 25 additions & 19 deletions src/agent/MessageSender.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { DidCommService } from '../modules/connections'
import type { OutboundTransporter } from '../transport/OutboundTransporter'
import type { OutboundMessage, OutboundPackage } from '../types'
import type { EnvelopeKeys } from './EnvelopeService'

import { inject, Lifecycle, scoped } from 'tsyringe'

Expand Down Expand Up @@ -36,37 +36,43 @@ export class MessageSender {
return this._outboundTransporter
}

public async packMessage(outboundMessage: OutboundMessage, service: DidCommService): Promise<OutboundPackage> {
public async packMessage(outboundMessage: OutboundMessage, keys: EnvelopeKeys): Promise<OutboundPackage> {
const { connection, payload } = outboundMessage
const { verkey, theirKey } = connection
const endpoint = service.serviceEndpoint
const message = payload.toJSON()
this.logger.debug('outboundMessage', { verkey, theirKey, message })
const keys = {
recipientKeys: service.recipientKeys,
routingKeys: service.routingKeys || [],
senderKey: connection.verkey,
}
const wireMessage = await this.envelopeService.packMessage(keys, outboundMessage.payload)
const responseRequested = outboundMessage.payload.hasReturnRouting()
return { connection, payload: wireMessage, endpoint, responseRequested }
const wireMessage = await this.envelopeService.packMessage(payload, keys)
return { connection, payload: wireMessage }
}

public async sendMessage(outboundMessage: OutboundMessage): Promise<void> {
if (!this.outboundTransporter) {
throw new AriesFrameworkError('Agent has no outbound transporter!')
}

const services = this.transportService.findDidCommServices(outboundMessage.connection)
const { connection, payload } = outboundMessage
const { id, verkey, theirKey } = connection
const message = payload.toJSON()
this.logger.debug('Send outbound message', {
messageId: message.id,
connection: { id, verkey, theirKey },
})

const services = this.transportService.findDidCommServices(connection)
if (services.length === 0) {
throw new AriesFrameworkError(`Connection with id ${outboundMessage.connection.id} has no service!`)
throw new AriesFrameworkError(`Connection with id ${connection.id} has no service!`)
}

for await (const service of services) {
this.logger.debug(`Sending outbound message to service:`, { service })
this.logger.debug(`Sending outbound message to service:`, { messageId: message.id, service })
try {
const outboundPackage = await this.packMessage(outboundMessage, service)
outboundPackage.session = this.transportService.findSession(outboundMessage.connection.id)
const keys = {
recipientKeys: service.recipientKeys,
routingKeys: service.routingKeys || [],
senderKey: connection.verkey,
}
const outboundPackage = await this.packMessage(outboundMessage, keys)
outboundPackage.session = this.transportService.findSession(connection.id)
outboundPackage.endpoint = service.serviceEndpoint
outboundPackage.responseRequested = outboundMessage.payload.hasReturnRouting()

await this.outboundTransporter.sendMessage(outboundPackage)
break
} catch (error) {
Expand Down
39 changes: 26 additions & 13 deletions src/agent/__tests__/MessageSender.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,26 @@ describe('MessageSender', () => {
})
expect(sendMessageSpy).toHaveBeenCalledTimes(2)
})

test('calls send message with responseRequested when message has return route', async () => {
messageSender.setOutboundTransporter(outboundTransporter)
const sendMessageSpy = jest.spyOn(outboundTransporter, 'sendMessage')

const message = new AgentMessage()
message.setReturnRouting(ReturnRouteTypes.all)
const outboundMessage = createOutboundMessage(connection, message)

await messageSender.sendMessage(outboundMessage)

expect(sendMessageSpy).toHaveBeenCalledWith({
connection,
payload: wireMessage,
endpoint: firstDidCommService.serviceEndpoint,
responseRequested: true,
session,
})
expect(sendMessageSpy).toHaveBeenCalledTimes(1)
})
})

describe('packMessage', () => {
Expand All @@ -158,24 +178,17 @@ describe('MessageSender', () => {
const message = new AgentMessage()
const outboundMessage = createOutboundMessage(connection, message)

const result = await messageSender.packMessage(outboundMessage, firstDidCommService)
const keys = {
recipientKeys: ['service.recipientKeys'],
routingKeys: [],
senderKey: connection.verkey,
}
const result = await messageSender.packMessage(outboundMessage, keys)

expect(result).toEqual({
connection,
payload: wireMessage,
endpoint: firstDidCommService.serviceEndpoint,
responseRequested: false,
})
})

test('when message has return route returns outbound message context with responseRequested', async () => {
const message = new AgentMessage()
message.setReturnRouting(ReturnRouteTypes.all)
const outboundMessage = createOutboundMessage(connection, message)

const result = await messageSender.packMessage(outboundMessage, firstDidCommService)

expect(result.responseRequested).toEqual(true)
})
})
})

0 comments on commit f759f0e

Please sign in to comment.