Skip to content

Commit

Permalink
fix: incorrect recip key with multi routing keys (#446)
Browse files Browse the repository at this point in the history
* fix: incorrect recip key with multi routing keys
* fix: also transform @type on forwrad messages

Signed-off-by: Timo Glastra <[email protected]>
  • Loading branch information
TimoGlastra authored Sep 2, 2021
1 parent 8aad3e9 commit db76823
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 26 deletions.
11 changes: 9 additions & 2 deletions packages/core/src/agent/AgentMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ThreadDecorated } from '../decorators/thread/ThreadDecoratorExtension'
import { TimingDecorated } from '../decorators/timing/TimingDecoratorExtension'
import { TransportDecorated } from '../decorators/transport/TransportDecoratorExtension'
import { JsonTransformer } from '../utils/JsonTransformer'
import { replaceNewDidCommPrefixWithLegacyDidSovOnMessage } from '../utils/messageType'
import { Compose } from '../utils/mixins'

import { BaseMessage } from './BaseMessage'
Expand All @@ -21,8 +22,14 @@ const DefaultDecorators = [
]

export class AgentMessage extends Compose(BaseMessage, DefaultDecorators) {
public toJSON(): Record<string, unknown> {
return JsonTransformer.toJSON(this)
public toJSON({ useLegacyDidSovPrefix = false }: { useLegacyDidSovPrefix?: boolean } = {}): Record<string, unknown> {
const json = JsonTransformer.toJSON(this)

if (useLegacyDidSovPrefix) {
replaceNewDidCommPrefixWithLegacyDidSovOnMessage(json)
}

return json
}

public is<C extends typeof AgentMessage>(Class: C): this is InstanceType<C> {
Expand Down
38 changes: 20 additions & 18 deletions packages/core/src/agent/EnvelopeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { inject, scoped, Lifecycle } from 'tsyringe'

import { InjectionSymbols } from '../constants'
import { ForwardMessage } from '../modules/routing/messages'
import { replaceNewDidCommPrefixWithLegacyDidSovOnMessage } from '../utils/messageType'
import { Wallet } from '../wallet/Wallet'

import { AgentConfig } from './AgentConfig'
Expand All @@ -30,29 +29,32 @@ class EnvelopeService {
}

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

if (this.config.useLegacyDidSovPrefix) {
replaceNewDidCommPrefixWithLegacyDidSovOnMessage(message)
}
// pass whether we want to use legacy did sov prefix
const message = payload.toJSON({ useLegacyDidSovPrefix: this.config.useLegacyDidSovPrefix })

this.logger.debug(`Pack outbound message ${payload.type}`)
this.logger.debug(`Pack outbound message ${message['@type']}`)

let wireMessage = await this.wallet.pack(message, recipientKeys, senderKey ?? undefined)

if (routingKeys && routingKeys.length > 0) {
for (const routingKey of routingKeys) {
const [recipientKey] = recipientKeys

const forwardMessage = new ForwardMessage({
to: recipientKey,
message: wireMessage,
})
this.logger.debug('Forward message created', forwardMessage)
wireMessage = await this.wallet.pack(forwardMessage.toJSON(), [routingKey], senderKey ?? undefined)
}
// If the message has routing keys (mediator) pack for each mediator
for (const routingKey of routingKeys) {
const forwardMessage = new ForwardMessage({
// Forward to first recipient key
to: recipientKeys[0],
message: wireMessage,
})
recipientKeys = [routingKey]
this.logger.debug('Forward message created', forwardMessage)

const forwardJson = forwardMessage.toJSON({ useLegacyDidSovPrefix: this.config.useLegacyDidSovPrefix })

// Forward messages are anon packed
wireMessage = await this.wallet.pack(forwardJson, [routingKey], undefined)
}

return wireMessage
}

Expand Down
19 changes: 19 additions & 0 deletions packages/core/src/agent/__tests__/AgentMessage.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { AgentMessage } from '../AgentMessage'

class TestMessage extends AgentMessage {
public readonly type = 'https://didcomm.org/connections/1.0/invitation'
}

describe('AgentMessage', () => {
describe('toJSON', () => {
it('should only use did:sov message prefix if useLegacyDidSovPrefix is true', () => {
const message = new TestMessage()

const jsonDidComm = message.toJSON()
expect(jsonDidComm['@type']).toBe('https://didcomm.org/connections/1.0/invitation')

const jsonSov = message.toJSON({ useLegacyDidSovPrefix: true })
expect(jsonSov['@type']).toBe('did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/connections/1.0/invitation')
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { AgentMessage } from '../../../agent/AgentMessage'
import { AriesFrameworkError } from '../../../error'
import { JsonEncoder } from '../../../utils/JsonEncoder'
import { JsonTransformer } from '../../../utils/JsonTransformer'
import { replaceLegacyDidSovPrefix, replaceNewDidCommPrefixWithLegacyDidSovOnMessage } from '../../../utils/messageType'
import { replaceLegacyDidSovPrefix } from '../../../utils/messageType'

// TODO: improve typing of `DIDInvitationData` and `InlineInvitationData` so properties can't be mixed
export interface InlineInvitationData {
Expand Down Expand Up @@ -92,11 +92,7 @@ export class ConnectionInvitationMessage extends AgentMessage {
* @returns invitation url with base64 encoded invitation
*/
public toUrl({ domain, useLegacyDidSovPrefix = false }: { domain: string; useLegacyDidSovPrefix?: boolean }) {
const invitationJson = this.toJSON()

if (useLegacyDidSovPrefix) {
replaceNewDidCommPrefixWithLegacyDidSovOnMessage(invitationJson)
}
const invitationJson = this.toJSON({ useLegacyDidSovPrefix })

const encodedInvitation = JsonEncoder.toBase64URL(invitationJson)
const invitationUrl = `${domain}?c_i=${encodedInvitation}`
Expand Down

0 comments on commit db76823

Please sign in to comment.