Skip to content

Commit

Permalink
fix!: don't emit legacy did:sov prefix for new protocols (openwallet-…
Browse files Browse the repository at this point in the history
…foundation#1245)

Signed-off-by: Timo Glastra <timo@animo.id>
  • Loading branch information
TimoGlastra authored Feb 20, 2023
1 parent c0e5339 commit 64e20f1
Show file tree
Hide file tree
Showing 25 changed files with 96 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export type V1CredentialAckMessageOptions = AckMessageOptions
* @see https://github.com/hyperledger/aries-rfcs/blob/master/features/0015-acks/README.md#explicit-acks
*/
export class V1CredentialAckMessage extends AckMessage {
public readonly allowDidSovPrefix = true

/**
* Create new CredentialAckMessage instance.
* @param options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export type V1CredentialProblemReportMessageOptions = ProblemReportMessageOption
* @see https://github.com/hyperledger/aries-rfcs/blob/main/features/0035-report-problem/README.md
*/
export class V1CredentialProblemReportMessage extends ProblemReportMessage {
public readonly allowDidSovPrefix = true

/**
* Create new CredentialProblemReportMessage instance.
* @param options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export interface V1IssueCredentialMessageOptions {
}

export class V1IssueCredentialMessage extends AgentMessage {
public readonly allowDidSovPrefix = true

public constructor(options: V1IssueCredentialMessageOptions) {
super()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export interface V1OfferCredentialMessageOptions {
* @see https://github.com/hyperledger/aries-rfcs/blob/master/features/0036-issue-credential/README.md#offer-credential
*/
export class V1OfferCredentialMessage extends AgentMessage {
public readonly allowDidSovPrefix = true

public constructor(options: V1OfferCredentialMessageOptions) {
super()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ export interface V1ProposeCredentialMessageOptions {
* @see https://github.com/hyperledger/aries-rfcs/blob/master/features/0036-issue-credential/README.md#propose-credential
*/
export class V1ProposeCredentialMessage extends AgentMessage {
public readonly allowDidSovPrefix = true

public constructor(options: V1ProposeCredentialMessageOptions) {
super()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export interface V1RequestCredentialMessageOptions {
}

export class V1RequestCredentialMessage extends AgentMessage {
public readonly allowDidSovPrefix = true

public constructor(options: V1RequestCredentialMessageOptions) {
super()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import type { AckMessageOptions } from '@aries-framework/core'
import { AckMessage, IsValidMessageType, parseMessageType } from '@aries-framework/core'

export class V1PresentationAckMessage extends AckMessage {
public readonly allowDidSovPrefix = true

public constructor(options: AckMessageOptions) {
super(options)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface V1PresentationMessageOptions {
* @see https://github.com/hyperledger/aries-rfcs/blob/master/features/0037-present-proof/README.md#presentation
*/
export class V1PresentationMessage extends AgentMessage {
public readonly allowDidSovPrefix = true
public constructor(options: V1PresentationMessageOptions) {
super()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export type V1PresentationProblemReportMessageOptions = ProblemReportMessageOpti
* @see https://github.com/hyperledger/aries-rfcs/blob/main/features/0035-report-problem/README.md
*/
export class V1PresentationProblemReportMessage extends ProblemReportMessage {
public readonly allowDidSovPrefix = true

/**
* Create new PresentationProblemReportMessage instance.
* @param options description of error and multiple optional fields for reporting problem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface V1ProposePresentationMessageOptions {
* @see https://github.com/hyperledger/aries-rfcs/blob/master/features/0037-present-proof/README.md#propose-presentation
*/
export class V1ProposePresentationMessage extends AgentMessage {
public readonly allowDidSovPrefix = true
public constructor(options: V1ProposePresentationMessageOptions) {
super()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export const INDY_PROOF_REQUEST_ATTACHMENT_ID = 'libindy-request-presentation-0'
* @see https://github.com/hyperledger/aries-rfcs/blob/master/features/0037-present-proof/README.md#request-presentation
*/
export class V1RequestPresentationMessage extends AgentMessage {
public readonly allowDidSovPrefix = true

public constructor(options: V1RequestPresentationMessageOptions) {
super()

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/agent/AgentConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ export class AgentConfig {
return this.initConfig.clearDefaultMediator ?? false
}

public get useLegacyDidSovPrefix() {
return this.initConfig.useLegacyDidSovPrefix ?? false
public get useDidSovPrefixWhereAllowed() {
return this.initConfig.useDidSovPrefixWhereAllowed ?? false
}

/**
Expand Down
21 changes: 19 additions & 2 deletions packages/core/src/agent/AgentMessage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { ParsedMessageType } from '../utils/messageType'
import type { Constructor } from '../utils/mixins'

import { Exclude } from 'class-transformer'

import { AckDecorated } from '../decorators/ack/AckDecoratorExtension'
import { AttachmentDecorated } from '../decorators/attachment/AttachmentExtension'
import { L10nDecorated } from '../decorators/l10n/L10nDecoratorExtension'
Expand All @@ -20,10 +22,25 @@ const Decorated = ThreadDecorated(
)

export class AgentMessage extends Decorated {
public toJSON({ useLegacyDidSovPrefix = false }: { useLegacyDidSovPrefix?: boolean } = {}): Record<string, unknown> {
/**
* Whether the protocol RFC was initially written using the legacy did:prefix instead of the
* new https://didcomm.org message type prefix.
*
* @see https://github.com/hyperledger/aries-rfcs/blob/main/features/0348-transition-msg-type-to-https/README.md
*/
@Exclude()
public readonly allowDidSovPrefix: boolean = false

public toJSON({ useDidSovPrefixWhereAllowed }: { useDidSovPrefixWhereAllowed?: boolean } = {}): Record<
string,
unknown
> {
const json = JsonTransformer.toJSON(this)

if (useLegacyDidSovPrefix) {
// If we have `useDidSovPrefixWhereAllowed` enabled, we want to replace the new https://didcomm.org prefix with the legacy did:sov prefix.
// However, we only do this if the protocol RFC was initially written with the did:sov message type prefix
// See https://github.com/hyperledger/aries-rfcs/blob/main/features/0348-transition-msg-type-to-https/README.md
if (this.allowDidSovPrefix && useDidSovPrefixWhereAllowed) {
replaceNewDidCommPrefixWithLegacyDidSovOnMessage(json)
}

Expand Down
6 changes: 4 additions & 2 deletions packages/core/src/agent/EnvelopeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class EnvelopeService {
const senderKeyBase58 = senderKey && senderKey.publicKeyBase58

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

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

Expand All @@ -49,7 +49,9 @@ export class EnvelopeService {
recipientKeysBase58 = [routingKeyBase58]
this.logger.debug('Forward message created', forwardMessage)

const forwardJson = forwardMessage.toJSON({ useLegacyDidSovPrefix: agentContext.config.useLegacyDidSovPrefix })
const forwardJson = forwardMessage.toJSON({
useDidSovPrefixWhereAllowed: agentContext.config.useDidSovPrefixWhereAllowed,
})

// Forward messages are anon packed
encryptedMessage = await agentContext.wallet.pack(forwardJson, [routingKeyBase58], undefined)
Expand Down
29 changes: 24 additions & 5 deletions packages/core/src/agent/__tests__/AgentMessage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,35 @@ class CustomProtocolMessage extends AgentMessage {
public static readonly type = parseMessageType('https://didcomm.org/fake-protocol/1.5/message')
}

class LegacyDidSovPrefixMessage extends AgentMessage {
public readonly allowDidSovPrefix = true

@IsValidMessageType(LegacyDidSovPrefixMessage.type)
public readonly type = LegacyDidSovPrefixMessage.type.messageTypeUri
public static readonly type = parseMessageType('https://didcomm.org/fake-protocol/1.5/another-message')
}

describe('AgentMessage', () => {
describe('toJSON', () => {
it('should only use did:sov message prefix if useLegacyDidSovPrefix is true', () => {
it('should only use did:sov message prefix if useDidSovPrefixWhereAllowed and allowDidSovPrefix are both true', () => {
const message = new TestMessage()
const legacyPrefixMessage = new LegacyDidSovPrefixMessage()

// useDidSovPrefixWhereAllowed & allowDidSovPrefix are both false
let testMessageJson = message.toJSON()
expect(testMessageJson['@type']).toBe('https://didcomm.org/connections/1.0/invitation')

// useDidSovPrefixWhereAllowed is true, but allowDidSovPrefix is false
testMessageJson = message.toJSON({ useDidSovPrefixWhereAllowed: true })
expect(testMessageJson['@type']).toBe('https://didcomm.org/connections/1.0/invitation')

const jsonDidComm = message.toJSON()
expect(jsonDidComm['@type']).toBe('https://didcomm.org/connections/1.0/invitation')
// useDidSovPrefixWhereAllowed is false, but allowDidSovPrefix is true
testMessageJson = legacyPrefixMessage.toJSON()
expect(testMessageJson['@type']).toBe('https://didcomm.org/fake-protocol/1.5/another-message')

const jsonSov = message.toJSON({ useLegacyDidSovPrefix: true })
expect(jsonSov['@type']).toBe('did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/connections/1.0/invitation')
// useDidSovPrefixWhereAllowed & allowDidSovPrefix are both true
testMessageJson = legacyPrefixMessage.toJSON({ useDidSovPrefixWhereAllowed: true })
expect(testMessageJson['@type']).toBe('did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/fake-protocol/1.5/another-message')
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { IsValidMessageType, parseMessageType } from '../../../utils/messageType
import { DateParser } from '../../../utils/transformers'

export class BasicMessage extends AgentMessage {
public readonly allowDidSovPrefix = true

/**
* Create new BasicMessage instance.
* sentTime will be assigned to new Date if not passed, id will be assigned to uuid/v4 if not passed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ describe('ConnectionInvitationMessage', () => {
expect(invitationUrl).toBe(`${domain}?c_i=${JsonEncoder.toBase64URL(json)}`)
})

it('should use did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/connections/1.0/invitation as type if useLegacyDidSovPrefix is set to true', async () => {
it('should use did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/connections/1.0/invitation as type if useDidSovPrefixWhereAllowed is set to true', async () => {
const invitation = new ConnectionInvitationMessage({
id: '04a2c382-999e-4de9-a1d2-9dec0b2fa5e4',
recipientKeys: ['recipientKeyOne', 'recipientKeyTwo'],
Expand All @@ -86,7 +86,7 @@ describe('ConnectionInvitationMessage', () => {

const invitationUrl = invitation.toUrl({
domain: 'https://example.com',
useLegacyDidSovPrefix: true,
useDidSovPrefixWhereAllowed: true,
})

const parsedUrl = parseUrl(invitationUrl).query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export interface DIDInvitationOptions {
* @see https://github.com/hyperledger/aries-rfcs/blob/master/features/0160-connection-protocol/README.md#0-invitation-to-connect
*/
export class ConnectionInvitationMessage extends AgentMessage {
public readonly allowDidSovPrefix = true

/**
* Create new ConnectionInvitationMessage instance.
* @param options
Expand Down Expand Up @@ -107,8 +109,14 @@ export class ConnectionInvitationMessage extends AgentMessage {
* @param domain domain name to use for invitation url
* @returns invitation url with base64 encoded invitation
*/
public toUrl({ domain, useLegacyDidSovPrefix = false }: { domain: string; useLegacyDidSovPrefix?: boolean }) {
const invitationJson = this.toJSON({ useLegacyDidSovPrefix })
public toUrl({
domain,
useDidSovPrefixWhereAllowed = false,
}: {
domain: string
useDidSovPrefixWhereAllowed?: boolean
}) {
const invitationJson = this.toJSON({ useDidSovPrefixWhereAllowed })

const encodedInvitation = JsonEncoder.toBase64URL(invitationJson)
const invitationUrl = `${domain}?c_i=${encodedInvitation}`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export type ConnectionProblemReportMessageOptions = ProblemReportMessageOptions
* @see https://github.com/hyperledger/aries-rfcs/blob/main/features/0035-report-problem/README.md
*/
export class ConnectionProblemReportMessage extends ProblemReportMessage {
public readonly allowDidSovPrefix = true

/**
* Create new ConnectionProblemReportMessage instance.
* @param options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export interface ConnectionRequestMessageOptions {
* @see https://github.com/hyperledger/aries-rfcs/blob/master/features/0160-connection-protocol/README.md#1-connection-request
*/
export class ConnectionRequestMessage extends AgentMessage {
public readonly allowDidSovPrefix = true

/**
* Create new ConnectionRequestMessage instance.
* @param options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export interface ConnectionResponseMessageOptions {
* @see https://github.com/hyperledger/aries-rfcs/blob/master/features/0160-connection-protocol/README.md#2-connection-response
*/
export class ConnectionResponseMessage extends AgentMessage {
public readonly allowDidSovPrefix = true

/**
* Create new ConnectionResponseMessage instance.
* @param options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export interface TrustPingMessageOptions {
* @see https://github.com/hyperledger/aries-rfcs/blob/master/features/0048-trust-ping/README.md#messages
*/
export class TrustPingMessage extends AgentMessage {
public readonly allowDidSovPrefix = true

/**
* Create new TrustPingMessage instance.
* responseRequested will be true if not passed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export interface TrustPingResponseMessageOptions {
* @see https://github.com/hyperledger/aries-rfcs/blob/master/features/0048-trust-ping/README.md#messages
*/
export class TrustPingResponseMessage extends AgentMessage {
public readonly allowDidSovPrefix = true

/**
* Create new TrustPingResponseMessage instance.
* responseRequested will be true if not passed
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/modules/routing/messages/ForwardMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export interface ForwardMessageOptions {
* @see https://github.com/hyperledger/aries-rfcs/blob/master/concepts/0094-cross-domain-messaging/README.md#corerouting10forward
*/
export class ForwardMessage extends AgentMessage {
public readonly allowDidSovPrefix = true

/**
* Create new ForwardMessage instance.
*
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export interface InitConfig {
logger?: Logger
didCommMimeType?: DidCommMimeType
useDidKeyInProtocols?: boolean
useLegacyDidSovPrefix?: boolean
useDidSovPrefixWhereAllowed?: boolean
connectionImageUrl?: string
autoUpdateStorageOnStartup?: boolean

Expand Down

0 comments on commit 64e20f1

Please sign in to comment.