Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(did-comm): sendDIDCommMessage - returnMessage #1283

Merged
merged 2 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,33 @@ describe('messagepickup-message-handler', () => {
)
})

it('should contain returnMessage', async () => {

// Send StatusRequest
const statusRequestMessage: IDIDCommMessage = {
id: v4(),
type: STATUS_REQUEST_MESSAGE_TYPE,
to: mediator.did,
from: recipient.did,
return_route: 'all',
body: {
recipient_key: `${recipient.did}#${recipient.keys[0].kid}`,
},
}
const packedMessage = await agent.packDIDCommMessage({
packing: 'authcrypt',
message: statusRequestMessage,
})
const result = await agent.sendDIDCommMessage({
messageId: statusRequestMessage.id,
packedMessage,
recipientDidUrl: mediator.did,
})

expect(result.transportId).toBeDefined()
expect(result.returnMessage).toBeDefined()
})

it('should not respond to StatusRequest with no return_route', async () => {
expect.assertions(1)
const statusRequestMessage: IDIDCommMessage = {
Expand Down
22 changes: 18 additions & 4 deletions packages/did-comm/src/didcomm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,19 @@ export interface ISendDIDCommMessageArgs {
recipientDidUrl: string
}


/**
* The response from the {@link DIDComm.sendDIDCommMessage} method.
*
* @beta This API may change without a BREAKING CHANGE notice.
* `return_message` is only present if the `return_route: 'all'` was used
* in the packedMessage.
*/
export interface ISendDIDCommMessageResponse {
transportId: string
returnMessage?: IMessage
}

/**
* DID Comm plugin for {@link @veramo/core#Agent}
*
Expand Down Expand Up @@ -715,8 +728,8 @@ export class DIDComm implements IAgentPlugin {
/** {@inheritdoc IDIDComm.sendDIDCommMessage} */
async sendDIDCommMessage(
args: ISendDIDCommMessageArgs,
context: IAgentContext<IDIDManager & IKeyManager & IResolver>,
): Promise<string> {
context: IAgentContext<IDIDManager & IKeyManager & IResolver & IMessageHandler>,
): Promise<ISendDIDCommMessageResponse> {
const { packedMessage, returnTransportId, recipientDidUrl, messageId } = args

if (returnTransportId) {
Expand Down Expand Up @@ -839,11 +852,12 @@ export class DIDComm implements IAgentPlugin {

if (response.returnMessage) {
// Handle return message
await context.agent.handleMessage({
const returnMessage = await context.agent.handleMessage({
raw: response.returnMessage,
})
return { transportId: transport.id, returnMessage }
}
return transport.id
return { transportId: transport.id }
}

/** {@inheritdoc IDIDComm.sendMessageDIDCommAlpha1} */
Expand Down
107 changes: 61 additions & 46 deletions packages/did-comm/src/plugin.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -224,59 +224,20 @@
],
"description": "The input to the {@link DIDComm.sendDIDCommMessage } method. The provided `messageId` will be used in the emitted event to allow event/message correlation."
},
"ISendMessageDIDCommAlpha1Args": {
"ISendDIDCommMessageResponse": {
"type": "object",
"properties": {
"url": {
"transportId": {
"type": "string"
},
"save": {
"type": "boolean"
},
"data": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"from": {
"type": "string"
},
"to": {
"type": "string"
},
"type": {
"type": "string"
},
"body": {
"anyOf": [
{
"type": "object"
},
{
"type": "string"
}
]
}
},
"required": [
"from",
"to",
"type",
"body"
]
},
"headers": {
"type": "object",
"additionalProperties": {
"type": "string"
}
"returnMessage": {
"$ref": "#/components/schemas/IMessage"
}
},
"required": [
"data"
"transportId"
],
"deprecated": "Please use {@link IDIDComm.sendDIDCommMessage } instead. This will be removed in Veramo 4.0.\nInput arguments for {@link IDIDComm.sendMessageDIDCommAlpha1 }"
"description": "The response from the {@link DIDComm.sendDIDCommMessage } method."
},
"IMessage": {
"type": "object",
Expand Down Expand Up @@ -647,6 +608,60 @@
},
"description": "The DIDComm message structure for data in an attachment. See https://identity.foundation/didcomm-messaging/spec/#attachments"
},
"ISendMessageDIDCommAlpha1Args": {
"type": "object",
"properties": {
"url": {
"type": "string"
},
"save": {
"type": "boolean"
},
"data": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"from": {
"type": "string"
},
"to": {
"type": "string"
},
"type": {
"type": "string"
},
"body": {
"anyOf": [
{
"type": "object"
},
{
"type": "string"
}
]
}
},
"required": [
"from",
"to",
"type",
"body"
]
},
"headers": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"required": [
"data"
],
"deprecated": "Please use {@link IDIDComm.sendDIDCommMessage } instead. This will be removed in Veramo 4.0.\nInput arguments for {@link IDIDComm.sendMessageDIDCommAlpha1 }"
},
"IUnpackDIDCommMessageArgs": {
"$ref": "#/components/schemas/IPackedDIDCommMessage",
"description": "The input to the {@link DIDComm.unpackDIDCommMessage } method."
Expand Down Expand Up @@ -705,7 +720,7 @@
"$ref": "#/components/schemas/ISendDIDCommMessageArgs"
},
"returnType": {
"type": "string"
"$ref": "#/components/schemas/ISendDIDCommMessageResponse"
}
},
"sendMessageDIDCommAlpha1": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class TrustPingMessageHandler extends AbstractMessageHandler {
packedMessage: packedResponse,
recipientDidUrl: from!,
})
message.addMetaData({ type: 'TrustPingResponseSent', value: sent })
message.addMetaData({ type: 'TrustPingResponseSent', value: JSON.stringify(sent) })
} catch (ex) {
debug(ex)
}
Expand Down
3 changes: 2 additions & 1 deletion packages/did-comm/src/types/IDIDComm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import {
IPackDIDCommMessageArgs,
ISendDIDCommMessageArgs,
ISendDIDCommMessageResponse,
ISendMessageDIDCommAlpha1Args,
IUnpackDIDCommMessageArgs,
} from '../didcomm.js'
Expand Down Expand Up @@ -97,7 +98,7 @@ export interface IDIDComm extends IPluginMethodMap {
*
* @beta This API may change without a BREAKING CHANGE notice.
*/
sendDIDCommMessage(args: ISendDIDCommMessageArgs, context: IAgentContext<IResolver>): Promise<string>
sendDIDCommMessage(args: ISendDIDCommMessageArgs, context: IAgentContext<IResolver>): Promise<ISendDIDCommMessageResponse>
mirceanis marked this conversation as resolved.
Show resolved Hide resolved

/**
*
Expand Down