Skip to content

Commit c41526f

Browse files
feat: allow to use legacy did sov prefix (#442)
Co-authored-by: Timo Glastra <timo@animo.id>
1 parent ee1a229 commit c41526f

File tree

5 files changed

+64
-1
lines changed

5 files changed

+64
-1
lines changed

packages/core/src/agent/AgentConfig.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,8 @@ export class AgentConfig {
111111
public get clearDefaultMediator() {
112112
return this.initConfig.clearDefaultMediator ?? false
113113
}
114+
115+
public get useLegacyDidSovPrefix() {
116+
return this.initConfig.useLegacyDidSovPrefix ?? false
117+
}
114118
}

packages/core/src/agent/EnvelopeService.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { inject, scoped, Lifecycle } from 'tsyringe'
66

77
import { InjectionSymbols } from '../constants'
88
import { ForwardMessage } from '../modules/routing/messages'
9+
import { replaceNewDidCommPrefixWithLegacyDidSovOnMessage } from '../utils/messageType'
910
import { Wallet } from '../wallet/Wallet'
1011

1112
import { AgentConfig } from './AgentConfig'
@@ -20,16 +21,22 @@ export interface EnvelopeKeys {
2021
class EnvelopeService {
2122
private wallet: Wallet
2223
private logger: Logger
24+
private config: AgentConfig
2325

2426
public constructor(@inject(InjectionSymbols.Wallet) wallet: Wallet, agentConfig: AgentConfig) {
2527
this.wallet = wallet
2628
this.logger = agentConfig.logger
29+
this.config = agentConfig
2730
}
2831

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

36+
if (this.config.useLegacyDidSovPrefix) {
37+
replaceNewDidCommPrefixWithLegacyDidSovOnMessage(message)
38+
}
39+
3340
this.logger.debug(`Pack outbound message ${payload.type}`)
3441

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

packages/core/src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ export interface InitConfig {
4545
clearDefaultMediator?: boolean
4646
mediatorPollingInterval?: number
4747
mediatorPickupStrategy?: MediatorPickupStrategy
48+
49+
useLegacyDidSovPrefix?: boolean
4850
}
4951

5052
export interface UnpackedMessage {

packages/core/src/utils/__tests__/messageType.test.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { replaceLegacyDidSovPrefix, replaceLegacyDidSovPrefixOnMessage } from '../messageType'
1+
import {
2+
replaceLegacyDidSovPrefix,
3+
replaceLegacyDidSovPrefixOnMessage,
4+
replaceNewDidCommPrefixWithLegacyDidSov,
5+
replaceNewDidCommPrefixWithLegacyDidSovOnMessage,
6+
} from '../messageType'
27

38
describe('messageType', () => {
49
describe('replaceLegacyDidSovPrefixOnMessage()', () => {
@@ -46,4 +51,34 @@ describe('messageType', () => {
4651
expect(replaceLegacyDidSovPrefix(messageTypeDidComm)).toBe('https://didcomm.org/basicmessage/1.0/message')
4752
})
4853
})
54+
55+
describe('replaceNewDidCommPrefixWithLegacyDidSovOnMessage()', () => {
56+
it('should replace the message type prefix with did:sov:BzCbsNYhMrjHiqZDTUASHg;spec if it starts with https://didcomm.org', () => {
57+
const message = {
58+
'@type': 'https://didcomm.org/basicmessage/1.0/message',
59+
}
60+
61+
replaceNewDidCommPrefixWithLegacyDidSovOnMessage(message)
62+
63+
expect(message['@type']).toBe('did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/basicmessage/1.0/message')
64+
})
65+
})
66+
67+
describe('replaceNewDidCommPrefixWithLegacyDidSov()', () => {
68+
it('should replace the message type prefix with did:sov:BzCbsNYhMrjHiqZDTUASHg;spec if it starts with https://didcomm.org', () => {
69+
const type = 'https://didcomm.org/basicmessage/1.0/message'
70+
71+
expect(replaceNewDidCommPrefixWithLegacyDidSov(type)).toBe(
72+
'did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/basicmessage/1.0/message'
73+
)
74+
})
75+
76+
it("should not replace the message type prefix with did:sov:BzCbsNYhMrjHiqZDTUASHg;spec if it doesn't start with https://didcomm.org", () => {
77+
const messageTypeOtherDidSov = 'did:sov:another_did;spec/basicmessage/1.0/message'
78+
79+
expect(replaceNewDidCommPrefixWithLegacyDidSov(messageTypeOtherDidSov)).toBe(
80+
'did:sov:another_did;spec/basicmessage/1.0/message'
81+
)
82+
})
83+
})
4984
})

packages/core/src/utils/messageType.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ export function replaceLegacyDidSovPrefixOnMessage(message: UnpackedMessage) {
44
message['@type'] = replaceLegacyDidSovPrefix(message['@type'])
55
}
66

7+
export function replaceNewDidCommPrefixWithLegacyDidSovOnMessage(message: Record<string, unknown>) {
8+
message['@type'] = replaceNewDidCommPrefixWithLegacyDidSov(message['@type'] as string)
9+
}
10+
711
export function replaceLegacyDidSovPrefix(messageType: string) {
812
const didSovPrefix = 'did:sov:BzCbsNYhMrjHiqZDTUASHg;spec'
913
const didCommPrefix = 'https://didcomm.org'
@@ -14,3 +18,14 @@ export function replaceLegacyDidSovPrefix(messageType: string) {
1418

1519
return messageType
1620
}
21+
22+
export function replaceNewDidCommPrefixWithLegacyDidSov(messageType: string) {
23+
const didSovPrefix = 'did:sov:BzCbsNYhMrjHiqZDTUASHg;spec'
24+
const didCommPrefix = 'https://didcomm.org'
25+
26+
if (messageType.startsWith(didCommPrefix)) {
27+
return messageType.replace(didCommPrefix, didSovPrefix)
28+
}
29+
30+
return messageType
31+
}

0 commit comments

Comments
 (0)