-
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(credentials)!: custom registration of credential protocols (#1158)
Signed-off-by: Timo Glastra <timo@animo.id>
- Loading branch information
1 parent
c9acef3
commit ff6293c
Showing
59 changed files
with
1,921 additions
and
1,383 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
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
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,59 @@ | ||
import type { AgentMessage } from './AgentMessage' | ||
import type { MessageHandler } from './MessageHandler' | ||
|
||
import { injectable } from 'tsyringe' | ||
|
||
import { canHandleMessageType, parseMessageType } from '../utils/messageType' | ||
|
||
@injectable() | ||
export class MessageHandlerRegistry { | ||
private messageHandlers: MessageHandler[] = [] | ||
|
||
public registerMessageHandler(messageHandler: MessageHandler) { | ||
this.messageHandlers.push(messageHandler) | ||
} | ||
|
||
public getHandlerForMessageType(messageType: string): MessageHandler | undefined { | ||
const incomingMessageType = parseMessageType(messageType) | ||
|
||
for (const handler of this.messageHandlers) { | ||
for (const MessageClass of handler.supportedMessages) { | ||
if (canHandleMessageType(MessageClass, incomingMessageType)) return handler | ||
} | ||
} | ||
} | ||
|
||
public getMessageClassForMessageType(messageType: string): typeof AgentMessage | undefined { | ||
const incomingMessageType = parseMessageType(messageType) | ||
|
||
for (const handler of this.messageHandlers) { | ||
for (const MessageClass of handler.supportedMessages) { | ||
if (canHandleMessageType(MessageClass, incomingMessageType)) return MessageClass | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Returns array of message types that dispatcher is able to handle. | ||
* Message type format is MTURI specified at https://github.com/hyperledger/aries-rfcs/blob/main/concepts/0003-protocols/README.md#mturi. | ||
*/ | ||
public get supportedMessageTypes() { | ||
return this.messageHandlers | ||
.reduce<typeof AgentMessage[]>((all, cur) => [...all, ...cur.supportedMessages], []) | ||
.map((m) => m.type) | ||
} | ||
|
||
/** | ||
* Returns array of protocol IDs that dispatcher is able to handle. | ||
* Protocol ID format is PIURI specified at https://github.com/hyperledger/aries-rfcs/blob/main/concepts/0003-protocols/README.md#piuri. | ||
*/ | ||
public get supportedProtocols() { | ||
return Array.from(new Set(this.supportedMessageTypes.map((m) => m.protocolUri))) | ||
} | ||
|
||
public filterSupportedProtocolsByMessageFamilies(messageFamilies: string[]) { | ||
return this.supportedProtocols.filter((protocolId) => | ||
messageFamilies.find((messageFamily) => protocolId.startsWith(messageFamily)) | ||
) | ||
} | ||
} |
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
Oops, something went wrong.