diff --git a/packages/core/src/agent/AgentConfig.ts b/packages/core/src/agent/AgentConfig.ts index b40258dc1e..503f3f05c1 100644 --- a/packages/core/src/agent/AgentConfig.ts +++ b/packages/core/src/agent/AgentConfig.ts @@ -111,4 +111,8 @@ export class AgentConfig { public get clearDefaultMediator() { return this.initConfig.clearDefaultMediator ?? false } + + public get useLegacyDidSovPrefix() { + return this.initConfig.useLegacyDidSovPrefix ?? false + } } diff --git a/packages/core/src/agent/EnvelopeService.ts b/packages/core/src/agent/EnvelopeService.ts index 462d68557d..09a619df16 100644 --- a/packages/core/src/agent/EnvelopeService.ts +++ b/packages/core/src/agent/EnvelopeService.ts @@ -6,6 +6,7 @@ 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' @@ -20,16 +21,22 @@ export interface EnvelopeKeys { class EnvelopeService { private wallet: Wallet private logger: Logger + private config: AgentConfig public constructor(@inject(InjectionSymbols.Wallet) wallet: Wallet, agentConfig: AgentConfig) { this.wallet = wallet this.logger = agentConfig.logger + this.config = agentConfig } public async packMessage(payload: AgentMessage, keys: EnvelopeKeys): Promise { const { routingKeys, recipientKeys, senderKey } = keys const message = payload.toJSON() + if (this.config.useLegacyDidSovPrefix) { + replaceNewDidCommPrefixWithLegacyDidSovOnMessage(message) + } + this.logger.debug(`Pack outbound message ${payload.type}`) let wireMessage = await this.wallet.pack(message, recipientKeys, senderKey ?? undefined) diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index d03fa3221e..9002e5652f 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -45,6 +45,8 @@ export interface InitConfig { clearDefaultMediator?: boolean mediatorPollingInterval?: number mediatorPickupStrategy?: MediatorPickupStrategy + + useLegacyDidSovPrefix?: boolean } export interface UnpackedMessage { diff --git a/packages/core/src/utils/__tests__/messageType.test.ts b/packages/core/src/utils/__tests__/messageType.test.ts index 24c40d8bdf..d091ca2d04 100644 --- a/packages/core/src/utils/__tests__/messageType.test.ts +++ b/packages/core/src/utils/__tests__/messageType.test.ts @@ -1,4 +1,9 @@ -import { replaceLegacyDidSovPrefix, replaceLegacyDidSovPrefixOnMessage } from '../messageType' +import { + replaceLegacyDidSovPrefix, + replaceLegacyDidSovPrefixOnMessage, + replaceNewDidCommPrefixWithLegacyDidSov, + replaceNewDidCommPrefixWithLegacyDidSovOnMessage, +} from '../messageType' describe('messageType', () => { describe('replaceLegacyDidSovPrefixOnMessage()', () => { @@ -46,4 +51,34 @@ describe('messageType', () => { expect(replaceLegacyDidSovPrefix(messageTypeDidComm)).toBe('https://didcomm.org/basicmessage/1.0/message') }) }) + + describe('replaceNewDidCommPrefixWithLegacyDidSovOnMessage()', () => { + it('should replace the message type prefix with did:sov:BzCbsNYhMrjHiqZDTUASHg;spec if it starts with https://didcomm.org', () => { + const message = { + '@type': 'https://didcomm.org/basicmessage/1.0/message', + } + + replaceNewDidCommPrefixWithLegacyDidSovOnMessage(message) + + expect(message['@type']).toBe('did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/basicmessage/1.0/message') + }) + }) + + describe('replaceNewDidCommPrefixWithLegacyDidSov()', () => { + it('should replace the message type prefix with did:sov:BzCbsNYhMrjHiqZDTUASHg;spec if it starts with https://didcomm.org', () => { + const type = 'https://didcomm.org/basicmessage/1.0/message' + + expect(replaceNewDidCommPrefixWithLegacyDidSov(type)).toBe( + 'did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/basicmessage/1.0/message' + ) + }) + + it("should not replace the message type prefix with did:sov:BzCbsNYhMrjHiqZDTUASHg;spec if it doesn't start with https://didcomm.org", () => { + const messageTypeOtherDidSov = 'did:sov:another_did;spec/basicmessage/1.0/message' + + expect(replaceNewDidCommPrefixWithLegacyDidSov(messageTypeOtherDidSov)).toBe( + 'did:sov:another_did;spec/basicmessage/1.0/message' + ) + }) + }) }) diff --git a/packages/core/src/utils/messageType.ts b/packages/core/src/utils/messageType.ts index 6c39d5fda2..581470630c 100644 --- a/packages/core/src/utils/messageType.ts +++ b/packages/core/src/utils/messageType.ts @@ -4,6 +4,10 @@ export function replaceLegacyDidSovPrefixOnMessage(message: UnpackedMessage) { message['@type'] = replaceLegacyDidSovPrefix(message['@type']) } +export function replaceNewDidCommPrefixWithLegacyDidSovOnMessage(message: Record) { + message['@type'] = replaceNewDidCommPrefixWithLegacyDidSov(message['@type'] as string) +} + export function replaceLegacyDidSovPrefix(messageType: string) { const didSovPrefix = 'did:sov:BzCbsNYhMrjHiqZDTUASHg;spec' const didCommPrefix = 'https://didcomm.org' @@ -14,3 +18,14 @@ export function replaceLegacyDidSovPrefix(messageType: string) { return messageType } + +export function replaceNewDidCommPrefixWithLegacyDidSov(messageType: string) { + const didSovPrefix = 'did:sov:BzCbsNYhMrjHiqZDTUASHg;spec' + const didCommPrefix = 'https://didcomm.org' + + if (messageType.startsWith(didCommPrefix)) { + return messageType.replace(didCommPrefix, didSovPrefix) + } + + return messageType +}