Skip to content

Commit

Permalink
feat: allow to use legacy did sov prefix (#442)
Browse files Browse the repository at this point in the history
Co-authored-by: Timo Glastra <[email protected]>
  • Loading branch information
janrtvld and TimoGlastra authored Aug 27, 2021
1 parent ee1a229 commit c41526f
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 1 deletion.
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
}

0 comments on commit c41526f

Please sign in to comment.