Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
Signed-off-by: Timo Glastra <timo@animo.id>
  • Loading branch information
TimoGlastra committed Jan 30, 2024
1 parent 45c53e7 commit 00449b4
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export enum HandshakeProtocol {
/**
* Get the handshake protocol passed in, but with the minor version replaces with an x.
*
* E.g. `https://didocmm.org/connections/1.1` becomes `https://didcomm.org/connections/1.x`
* E.g. `https://didcomm.org/connections/1.1` becomes `https://didcomm.org/connections/1.x`
* This allows to match message types but ignoring the minor version.
*/
export function getHandshakeProtocolWithoutMinorVersion(handshakeProtocol: string) {
Expand Down
31 changes: 31 additions & 0 deletions packages/core/src/utils/__tests__/messageType.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AgentMessage } from '../../agent/AgentMessage'
import {
canHandleMessageType,
parseDidCommProtocolUri,
parseMessageType,
replaceLegacyDidSovPrefix,
replaceLegacyDidSovPrefixOnMessage,
Expand Down Expand Up @@ -121,6 +122,36 @@ describe('messageType', () => {
messageTypeUri: 'https://didcomm.org/issue-credential/4.5/propose-credential',
})
})

test('throws error when invalid message type is passed', () => {
expect(() => parseMessageType('https://didcomm.org/connections/1.0/message-type/and-else')).toThrow()
})
})

describe('parseDidCommProtocolUri()', () => {
test('correctly parses the protocol uri', () => {
expect(parseDidCommProtocolUri('https://didcomm.org/connections/1.0')).toEqual({
documentUri: 'https://didcomm.org',
protocolName: 'connections',
protocolVersion: '1.0',
protocolMajorVersion: 1,
protocolMinorVersion: 0,
protocolUri: 'https://didcomm.org/connections/1.0',
})

expect(parseDidCommProtocolUri('https://didcomm.org/issue-credential/4.5')).toEqual({
documentUri: 'https://didcomm.org',
protocolName: 'issue-credential',
protocolVersion: '4.5',
protocolMajorVersion: 4,
protocolMinorVersion: 5,
protocolUri: `https://didcomm.org/issue-credential/4.5`,
})
})

test('throws error when message type is passed', () => {
expect(() => parseDidCommProtocolUri('https://didcomm.org/connections/1.0/message-type')).toThrow()
})
})

describe('supportsIncomingMessageType()', () => {
Expand Down
11 changes: 0 additions & 11 deletions packages/core/src/utils/__tests__/string.test.ts

This file was deleted.

61 changes: 44 additions & 17 deletions packages/core/src/utils/messageType.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
import type { VersionString } from './version'
import type { PlaintextMessage } from '../types'
import type { ValidationOptions, ValidationArguments } from 'class-validator'

import { ValidateBy, buildMessage } from 'class-validator'

import { rightSplit } from './string'
import { parseVersionString } from './version'

export interface ParsedMessageType {
/**
* Message name
*
* @example request
*/
messageName: string
const PROTOCOL_URI_REGEX = /^(.+)\/([^/\\]+)\/(\d+).(\d+)$/
const MESSAGE_TYPE_REGEX = /^(.+)\/([^/\\]+)\/(\d+).(\d+)\/([^/\\]+)$/

export interface ParsedDidCommProtocolUri {
/**
* Version of the protocol
*
Expand Down Expand Up @@ -58,6 +50,15 @@ export interface ParsedMessageType {
* @example https://didcomm.org/connections/1.0
*/
protocolUri: string
}

export interface ParsedMessageType extends ParsedDidCommProtocolUri {
/**
* Message name
*
* @example request
*/
messageName: string

/**
* Uri identifier of the message. Includes all parts
Expand All @@ -68,22 +69,48 @@ export interface ParsedMessageType {
messageTypeUri: string
}

// TODO: rename to `parseDidCommMessageType` and `DidCommParsedProtocolUri`
// in the future
export function parseMessageType(messageType: string): ParsedMessageType {
const [documentUri, protocolName, protocolVersion, messageName] = rightSplit(messageType, '/', 3)
const [protocolMajorVersion, protocolMinorVersion] = parseVersionString(protocolVersion as VersionString)
const match = MESSAGE_TYPE_REGEX.exec(messageType)

if (!match) {
throw new Error(`Invalid message type: ${messageType}`)
}

const [, documentUri, protocolName, protocolVersionMajor, protocolVersionMinor, messageName] = match

return {
documentUri,
protocolName,
protocolVersion,
protocolMajorVersion,
protocolMinorVersion,
protocolVersion: `${protocolVersionMajor}.${protocolVersionMinor}`,
protocolMajorVersion: parseInt(protocolVersionMajor),
protocolMinorVersion: parseInt(protocolVersionMinor),
messageName,
protocolUri: `${documentUri}/${protocolName}/${protocolVersion}`,
protocolUri: `${documentUri}/${protocolName}/${protocolVersionMajor}.${protocolVersionMinor}`,
messageTypeUri: messageType,
}
}

export function parseDidCommProtocolUri(didCommProtocolUri: string): ParsedDidCommProtocolUri {
const match = PROTOCOL_URI_REGEX.exec(didCommProtocolUri)

if (!match) {
throw new Error(`Invalid protocol uri: ${didCommProtocolUri}`)
}

const [, documentUri, protocolName, protocolVersionMajor, protocolVersionMinor] = match

return {
documentUri,
protocolName,
protocolVersion: `${protocolVersionMajor}.${protocolVersionMinor}`,
protocolMajorVersion: parseInt(protocolVersionMajor),
protocolMinorVersion: parseInt(protocolVersionMinor),
protocolUri: `${documentUri}/${protocolName}/${protocolVersionMajor}.${protocolVersionMinor}`,
}
}

/**
* Check whether the incoming message type is a message type that can be handled by comparing it to the expected message type.
* In this case the expected message type is e.g. the type declared on an agent message class, and the incoming message type is the type
Expand Down
4 changes: 0 additions & 4 deletions packages/core/src/utils/string.ts

This file was deleted.

0 comments on commit 00449b4

Please sign in to comment.