Skip to content

Commit

Permalink
feat(did-comm) Support DIDComm Mediation (#1095)
Browse files Browse the repository at this point in the history
* feat(did-comm) Wrap message in forward when routingKeys exists or serviceEndpoint uri is DID

* feat(remote-server) Add support for returnMessage and return_route in MessagingRouter

* feat(did-comm) Initial coordinate-mediation handler and return_route header support

* feat(did-comm) Send MediateGrant reply via return_route

* feat(did-comm) Recipient handles MediateGrant

* feat(did-comm) Recipient handles MediateDeny

* feat(did-comm) Routing protocol to handle forward messages

* feat(did-comm) Pickup protocol Status messages

* feat(did-comm) Pickup protocol Delivery messages

* feat(did-comm) Pickup protocol MessagesReceived handlers

* feat(did-comm) Pickup protocol handle message batch

* test(did-comm) Fix coordinate-mediation-message-handler test cases

* Fix jest tests

* Add .js to local package imports

* Fix core-types

* Revert pnpm-lock file
  • Loading branch information
codynhat authored Feb 9, 2023
1 parent 16bc302 commit 4b46a79
Show file tree
Hide file tree
Showing 20 changed files with 3,141 additions and 38 deletions.
486 changes: 485 additions & 1 deletion __tests__/shared/didCommWithFakeDidFlow.ts

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions packages/core-types/src/plugin.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1922,6 +1922,19 @@
"IDataStore": {
"components": {
"schemas": {
"IDataStoreDeleteMessageArgs": {
"type": "object",
"properties": {
"id": {
"type": "string",
"description": "Required. Message ID"
}
},
"required": [
"id"
],
"description": "Input arguments for {@link IDataStore.dataStoreDeleteMessage | dataStoreDeleteMessage }"
},
"IDataStoreDeleteVerifiableCredentialArgs": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -2039,6 +2052,10 @@
"$ref": "#/components/schemas/IMessageAttachment"
},
"description": "Optional. Array of generic attachments"
},
"returnRoute": {
"type": "string",
"description": "Optional. Signal how to reuse transport for return messages"
}
},
"required": [
Expand Down Expand Up @@ -2376,6 +2393,15 @@
}
},
"methods": {
"dataStoreDeleteMessage": {
"description": "Deletes message from the data store",
"arguments": {
"$ref": "#/components/schemas/IDataStoreDeleteMessageArgs"
},
"returnType": {
"type": "boolean"
}
},
"dataStoreDeleteVerifiableCredential": {
"description": "Deletes verifiable credential from the data store",
"arguments": {
Expand Down Expand Up @@ -2889,6 +2915,10 @@
"$ref": "#/components/schemas/IMessageAttachment"
},
"description": "Optional. Array of generic attachments"
},
"returnRoute": {
"type": "string",
"description": "Optional. Signal how to reuse transport for return messages"
}
},
"required": [
Expand Down Expand Up @@ -3743,6 +3773,10 @@
"$ref": "#/components/schemas/IMessageAttachment"
},
"description": "Optional. Array of generic attachments"
},
"returnRoute": {
"type": "string",
"description": "Optional. Signal how to reuse transport for return messages"
}
},
"required": [
Expand Down
18 changes: 18 additions & 0 deletions packages/core-types/src/types/IDataStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ export interface IDataStoreGetMessageArgs {
id: string
}

/**
* Input arguments for {@link IDataStore.dataStoreDeleteMessage | dataStoreDeleteMessage}
* @public
*/
export interface IDataStoreDeleteMessageArgs {
/**
* Required. Message ID
*/
id: string
}

/**
* Input arguments for {@link IDataStore.dataStoreSaveVerifiableCredential | dataStoreSaveVerifiableCredential}
* @public
Expand Down Expand Up @@ -98,6 +109,13 @@ export interface IDataStore extends IPluginMethodMap {
*/
dataStoreGetMessage(args: IDataStoreGetMessageArgs): Promise<IMessage>

/**
* Deletes message from the data store
* @param args - arguments for deleting message
* @returns a promise that resolves to a boolean
*/
dataStoreDeleteMessage(args: IDataStoreDeleteMessageArgs): Promise<boolean>

/**
* Saves verifiable credential to the data store
* @param args - verifiable credential
Expand Down
5 changes: 5 additions & 0 deletions packages/core-types/src/types/IMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,9 @@ export interface IMessage {
* Optional. Array of generic attachments
*/
attachments?: IMessageAttachment[]

/**
* Optional. Signal how to reuse transport for return messages
*/
returnRoute?: string
}
75 changes: 47 additions & 28 deletions packages/data-store-json/src/data-store-json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
IDataStore,
IDataStoreDeleteVerifiableCredentialArgs,
IDataStoreGetMessageArgs,
IDataStoreDeleteMessageArgs,
IDataStoreGetVerifiableCredentialArgs,
IDataStoreGetVerifiablePresentationArgs,
IDataStoreORM,
Expand Down Expand Up @@ -76,7 +77,7 @@ export class DataStoreJson implements IAgentPlugin {
// IDataStore methods
dataStoreSaveMessage: this.dataStoreSaveMessage.bind(this),
dataStoreGetMessage: this.dataStoreGetMessage.bind(this),
//dataStoreDeleteMessage: this.dataStoreDeleteMessage.bind(this),
dataStoreDeleteMessage: this.dataStoreDeleteMessage.bind(this),
dataStoreSaveVerifiableCredential: this.dataStoreSaveVerifiableCredential.bind(this),
dataStoreGetVerifiableCredential: this.dataStoreGetVerifiableCredential.bind(this),
dataStoreDeleteVerifiableCredential: this.dataStoreDeleteVerifiableCredential.bind(this),
Expand Down Expand Up @@ -137,6 +138,18 @@ export class DataStoreJson implements IAgentPlugin {
}
}

async dataStoreDeleteMessage(args: IDataStoreDeleteMessageArgs): Promise<boolean> {
const message = this.cacheTree.messages[args.id]
if (message) {
const oldTree = deserialize(serialize(this.cacheTree, { lossy: true }))
delete this.cacheTree.messages[args.id]
await this.notifyUpdate(oldTree, this.cacheTree)
return true
} else {
return false
}
}

private async _dataStoreSaveVerifiableCredential(
args: IDataStoreSaveVerifiableCredentialArgs,
postUpdates: boolean = true,
Expand Down Expand Up @@ -393,15 +406,17 @@ export class DataStoreJson implements IAgentPlugin {
filteredCredentials.add(this.cacheTree.credentials[claim.credentialHash])
})

return deserialize(serialize(
Array.from(filteredCredentials).map((cred) => {
const { hash, parsedCredential } = cred
return {
hash,
verifiableCredential: parsedCredential,
}
}),
))
return deserialize(
serialize(
Array.from(filteredCredentials).map((cred) => {
const { hash, parsedCredential } = cred
return {
hash,
verifiableCredential: parsedCredential,
}
}),
),
)
}

async dataStoreORMGetVerifiableCredentialsByClaimsCount(
Expand All @@ -422,15 +437,17 @@ export class DataStoreJson implements IAgentPlugin {
context.authorizedDID,
)

return deserialize(serialize(
credentials.map((cred: any) => {
const { hash, parsedCredential } = cred
return {
hash,
verifiableCredential: parsedCredential,
}
}),
))
return deserialize(
serialize(
credentials.map((cred: any) => {
const { hash, parsedCredential } = cred
return {
hash,
verifiableCredential: parsedCredential,
}
}),
),
)
}

async dataStoreORMGetVerifiableCredentialsCount(
Expand All @@ -451,15 +468,17 @@ export class DataStoreJson implements IAgentPlugin {
context.authorizedDID,
)

return deserialize(serialize(
presentations.map((pres: any) => {
const { hash, parsedPresentation } = pres
return {
hash,
verifiablePresentation: parsedPresentation,
}
}),
))
return deserialize(
serialize(
presentations.map((pres: any) => {
const { hash, parsedPresentation } = pres
return {
hash,
verifiablePresentation: parsedPresentation,
}
}),
),
)
}

async dataStoreORMGetVerifiablePresentationsCount(
Expand Down
16 changes: 16 additions & 0 deletions packages/data-store/src/data-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
IDataStore,
IDataStoreDeleteVerifiableCredentialArgs,
IDataStoreGetMessageArgs,
IDataStoreDeleteMessageArgs,
IDataStoreGetVerifiableCredentialArgs,
IDataStoreGetVerifiablePresentationArgs,
IDataStoreSaveMessageArgs,
Expand Down Expand Up @@ -45,6 +46,7 @@ export class DataStore implements IAgentPlugin {
this.methods = {
dataStoreSaveMessage: this.dataStoreSaveMessage.bind(this),
dataStoreGetMessage: this.dataStoreGetMessage.bind(this),
dataStoreDeleteMessage: this.dataStoreDeleteMessage.bind(this),
dataStoreDeleteVerifiableCredential: this.dataStoreDeleteVerifiableCredential.bind(this),
dataStoreSaveVerifiableCredential: this.dataStoreSaveVerifiableCredential.bind(this),
dataStoreGetVerifiableCredential: this.dataStoreGetVerifiableCredential.bind(this),
Expand All @@ -70,6 +72,20 @@ export class DataStore implements IAgentPlugin {
return createMessage(messageEntity)
}

async dataStoreDeleteMessage(args: IDataStoreDeleteMessageArgs): Promise<boolean> {
const messageEntity = await (await getConnectedDb(this.dbConnection)).getRepository(Message).findOne({
where: { id: args.id },
relations: ['credentials', 'presentations'],
})
if (!messageEntity) {
return false
}

await (await getConnectedDb(this.dbConnection)).getRepository(Message).remove(messageEntity)

return true
}

async dataStoreDeleteVerifiableCredential(
args: IDataStoreDeleteVerifiableCredentialArgs,
): Promise<boolean> {
Expand Down
Loading

0 comments on commit 4b46a79

Please sign in to comment.