-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add discover features protocol (#390)
- Loading branch information
1 parent
8e83c03
commit 3347424
Showing
15 changed files
with
318 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,5 +49,8 @@ | |
"rimraf": "~3.0.2", | ||
"tslog": "^3.2.0", | ||
"typescript": "~4.3.0" | ||
}, | ||
"resolutions": { | ||
"@types/node": "^15.14.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
packages/core/src/modules/discover-features/DiscoverFeaturesModule.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { Lifecycle, scoped } from 'tsyringe' | ||
|
||
import { Dispatcher } from '../../agent/Dispatcher' | ||
import { MessageSender } from '../../agent/MessageSender' | ||
import { createOutboundMessage } from '../../agent/helpers' | ||
import { ConnectionService } from '../connections/services' | ||
|
||
import { DiscloseMessageHandler, QueryMessageHandler } from './handlers' | ||
import { DiscoverFeaturesService } from './services' | ||
|
||
@scoped(Lifecycle.ContainerScoped) | ||
export class DiscoverFeaturesModule { | ||
private connectionService: ConnectionService | ||
private messageSender: MessageSender | ||
private discoverFeaturesService: DiscoverFeaturesService | ||
|
||
public constructor( | ||
dispatcher: Dispatcher, | ||
connectionService: ConnectionService, | ||
messageSender: MessageSender, | ||
discoverFeaturesService: DiscoverFeaturesService | ||
) { | ||
this.connectionService = connectionService | ||
this.messageSender = messageSender | ||
this.discoverFeaturesService = discoverFeaturesService | ||
this.registerHandlers(dispatcher) | ||
} | ||
|
||
public async queryFeatures(connectionId: string, options: { query: string; comment?: string }) { | ||
const connection = await this.connectionService.getById(connectionId) | ||
|
||
const queryMessage = await this.discoverFeaturesService.createQuery(options) | ||
|
||
const outbound = createOutboundMessage(connection, queryMessage) | ||
await this.messageSender.sendMessage(outbound) | ||
} | ||
|
||
private registerHandlers(dispatcher: Dispatcher) { | ||
dispatcher.registerHandler(new DiscloseMessageHandler()) | ||
dispatcher.registerHandler(new QueryMessageHandler(this.discoverFeaturesService)) | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
packages/core/src/modules/discover-features/__tests__/DiscoverFeaturesService.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import type { Dispatcher } from '../../../agent/Dispatcher' | ||
|
||
import { DiscoverFeaturesQueryMessage } from '../messages' | ||
import { DiscoverFeaturesService } from '../services/DiscoverFeaturesService' | ||
|
||
const supportedMessageTypes = [ | ||
'https://didcomm.org/connections/1.0/invitation', | ||
'https://didcomm.org/connections/1.0/request', | ||
'https://didcomm.org/connections/1.0/response', | ||
'https://didcomm.org/notification/1.0/ack', | ||
'https://didcomm.org/issue-credential/1.0/credential-proposal', | ||
] | ||
|
||
describe('DiscoverFeaturesService', () => { | ||
const discoverFeaturesService = new DiscoverFeaturesService({ supportedMessageTypes } as Dispatcher) | ||
|
||
describe('createDisclose', () => { | ||
it('should return all protocols when query is *', async () => { | ||
const queryMessage = new DiscoverFeaturesQueryMessage({ | ||
query: '*', | ||
}) | ||
|
||
const message = await discoverFeaturesService.createDisclose(queryMessage) | ||
|
||
expect(message.protocols.map((p) => p.protocolId)).toStrictEqual([ | ||
'https://didcomm.org/connections/1.0/', | ||
'https://didcomm.org/notification/1.0/', | ||
'https://didcomm.org/issue-credential/1.0/', | ||
]) | ||
}) | ||
|
||
it('should return only one protocol if the query specifies a specific protocol', async () => { | ||
const queryMessage = new DiscoverFeaturesQueryMessage({ | ||
query: 'https://didcomm.org/connections/1.0/', | ||
}) | ||
|
||
const message = await discoverFeaturesService.createDisclose(queryMessage) | ||
|
||
expect(message.protocols.map((p) => p.protocolId)).toStrictEqual(['https://didcomm.org/connections/1.0/']) | ||
}) | ||
|
||
it('should respect a wild card at the end of the query', async () => { | ||
const queryMessage = new DiscoverFeaturesQueryMessage({ | ||
query: 'https://didcomm.org/connections/*', | ||
}) | ||
|
||
const message = await discoverFeaturesService.createDisclose(queryMessage) | ||
|
||
expect(message.protocols.map((p) => p.protocolId)).toStrictEqual(['https://didcomm.org/connections/1.0/']) | ||
}) | ||
}) | ||
|
||
describe('createQuery', () => { | ||
it('should return a query message with the query and comment', async () => { | ||
const message = await discoverFeaturesService.createQuery({ | ||
query: '*', | ||
comment: 'Hello', | ||
}) | ||
|
||
expect(message.query).toBe('*') | ||
expect(message.comment).toBe('Hello') | ||
}) | ||
}) | ||
}) |
13 changes: 13 additions & 0 deletions
13
packages/core/src/modules/discover-features/handlers/DiscloseMessageHandler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import type { Handler, HandlerInboundMessage } from '../../../agent/Handler' | ||
|
||
import { DiscoverFeaturesDiscloseMessage } from '../messages' | ||
|
||
export class DiscloseMessageHandler implements Handler { | ||
public supportedMessages = [DiscoverFeaturesDiscloseMessage] | ||
|
||
public async handle(inboundMessage: HandlerInboundMessage<DiscloseMessageHandler>) { | ||
// We don't really need to do anything with this at the moment | ||
// The result can be hooked into through the generic message processed event | ||
inboundMessage.assertReadyConnection() | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
packages/core/src/modules/discover-features/handlers/QueryMessageHandler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import type { Handler, HandlerInboundMessage } from '../../../agent/Handler' | ||
import type { DiscoverFeaturesService } from '../services/DiscoverFeaturesService' | ||
|
||
import { createOutboundMessage } from '../../../agent/helpers' | ||
import { DiscoverFeaturesQueryMessage } from '../messages' | ||
|
||
export class QueryMessageHandler implements Handler { | ||
private discoverFeaturesService: DiscoverFeaturesService | ||
public supportedMessages = [DiscoverFeaturesQueryMessage] | ||
|
||
public constructor(discoverFeaturesService: DiscoverFeaturesService) { | ||
this.discoverFeaturesService = discoverFeaturesService | ||
} | ||
|
||
public async handle(inboundMessage: HandlerInboundMessage<QueryMessageHandler>) { | ||
const connection = inboundMessage.assertReadyConnection() | ||
|
||
const discloseMessage = await this.discoverFeaturesService.createDisclose(inboundMessage.message) | ||
|
||
return createOutboundMessage(connection, discloseMessage) | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
packages/core/src/modules/discover-features/handlers/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './DiscloseMessageHandler' | ||
export * from './QueryMessageHandler' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export * from './DiscoverFeaturesModule' | ||
export * from './handlers' | ||
export * from './messages' | ||
export * from './services' |
54 changes: 54 additions & 0 deletions
54
packages/core/src/modules/discover-features/messages/DiscoverFeaturesDiscloseMessage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { Expose, Type } from 'class-transformer' | ||
import { Equals, IsInstance, IsOptional, IsString } from 'class-validator' | ||
|
||
import { AgentMessage } from '../../../agent/AgentMessage' | ||
|
||
export interface DiscloseProtocolOptions { | ||
protocolId: string | ||
roles?: string[] | ||
} | ||
|
||
export class DiscloseProtocol { | ||
public constructor(options: DiscloseProtocolOptions) { | ||
if (options) { | ||
this.protocolId = options.protocolId | ||
this.roles = options.roles | ||
} | ||
} | ||
|
||
@Expose({ name: 'pid' }) | ||
@IsString() | ||
public protocolId!: string | ||
|
||
@IsString({ each: true }) | ||
@IsOptional() | ||
public roles?: string[] | ||
} | ||
|
||
export interface DiscoverFeaturesDiscloseMessageOptions { | ||
id?: string | ||
threadId: string | ||
protocols: DiscloseProtocolOptions[] | ||
} | ||
|
||
export class DiscoverFeaturesDiscloseMessage extends AgentMessage { | ||
public constructor(options: DiscoverFeaturesDiscloseMessageOptions) { | ||
super() | ||
|
||
if (options) { | ||
this.id = options.id ?? this.generateId() | ||
this.protocols = options.protocols.map((p) => new DiscloseProtocol(p)) | ||
this.setThread({ | ||
threadId: options.threadId, | ||
}) | ||
} | ||
} | ||
|
||
@Equals(DiscoverFeaturesDiscloseMessage.type) | ||
public readonly type = DiscoverFeaturesDiscloseMessage.type | ||
public static readonly type = 'https://didcomm.org/discover-features/1.0/disclose' | ||
|
||
@IsInstance(DiscloseProtocol, { each: true }) | ||
@Type(() => DiscloseProtocol) | ||
public protocols!: DiscloseProtocol[] | ||
} |
32 changes: 32 additions & 0 deletions
32
packages/core/src/modules/discover-features/messages/DiscoverFeaturesQueryMessage.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { Equals, IsOptional, IsString } from 'class-validator' | ||
|
||
import { AgentMessage } from '../../../agent/AgentMessage' | ||
|
||
export interface DiscoverFeaturesQueryMessageOptions { | ||
id?: string | ||
query: string | ||
comment?: string | ||
} | ||
|
||
export class DiscoverFeaturesQueryMessage extends AgentMessage { | ||
public constructor(options: DiscoverFeaturesQueryMessageOptions) { | ||
super() | ||
|
||
if (options) { | ||
this.id = options.id ?? this.generateId() | ||
this.query = options.query | ||
this.comment = options.comment | ||
} | ||
} | ||
|
||
@Equals(DiscoverFeaturesQueryMessage.type) | ||
public readonly type = DiscoverFeaturesQueryMessage.type | ||
public static readonly type = 'https://didcomm.org/discover-features/1.0/query' | ||
|
||
@IsString() | ||
public query!: string | ||
|
||
@IsString() | ||
@IsOptional() | ||
public comment?: string | ||
} |
2 changes: 2 additions & 0 deletions
2
packages/core/src/modules/discover-features/messages/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './DiscoverFeaturesDiscloseMessage' | ||
export * from './DiscoverFeaturesQueryMessage' |
Oops, something went wrong.