Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to use legacy did sov prefix #442

Merged
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
4 changes: 4 additions & 0 deletions packages/core/src/agent/AgentConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,8 @@ export class AgentConfig {
public get clearDefaultMediator() {
return this.initConfig.clearDefaultMediator ?? false
}

public get useLegacyDidSovPrefix() {
return this.initConfig.useLegacyDidSovPrefix ?? false
}
}
7 changes: 7 additions & 0 deletions packages/core/src/agent/EnvelopeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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<WireMessage> {
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)
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export interface InitConfig {
clearDefaultMediator?: boolean
mediatorPollingInterval?: number
mediatorPickupStrategy?: MediatorPickupStrategy

useLegacyDidSovPrefix?: boolean
}

export interface UnpackedMessage {
Expand Down
37 changes: 36 additions & 1 deletion packages/core/src/utils/__tests__/messageType.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { replaceLegacyDidSovPrefix, replaceLegacyDidSovPrefixOnMessage } from '../messageType'
import {
replaceLegacyDidSovPrefix,
replaceLegacyDidSovPrefixOnMessage,
replaceNewDidCommPrefixWithLegacyDidSov,
replaceNewDidCommPrefixWithLegacyDidSovOnMessage,
} from '../messageType'

describe('messageType', () => {
describe('replaceLegacyDidSovPrefixOnMessage()', () => {
Expand Down Expand Up @@ -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'
)
})
})
})
15 changes: 15 additions & 0 deletions packages/core/src/utils/messageType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ export function replaceLegacyDidSovPrefixOnMessage(message: UnpackedMessage) {
message['@type'] = replaceLegacyDidSovPrefix(message['@type'])
}

export function replaceNewDidCommPrefixWithLegacyDidSovOnMessage(message: Record<string, unknown>) {
message['@type'] = replaceNewDidCommPrefixWithLegacyDidSov(message['@type'] as string)
}

export function replaceLegacyDidSovPrefix(messageType: string) {
const didSovPrefix = 'did:sov:BzCbsNYhMrjHiqZDTUASHg;spec'
const didCommPrefix = 'https://didcomm.org'
Expand All @@ -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
}