Skip to content

Commit

Permalink
feat: oob without handhsake improvements and routing (#1511)
Browse files Browse the repository at this point in the history
Signed-off-by: Timo Glastra <[email protected]>
  • Loading branch information
TimoGlastra authored Jul 25, 2023
1 parent c6f03e4 commit 9e69cf4
Show file tree
Hide file tree
Showing 55 changed files with 1,321 additions and 1,075 deletions.
20 changes: 10 additions & 10 deletions packages/action-menu/src/ActionMenuApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {
AriesFrameworkError,
ConnectionService,
MessageSender,
OutboundMessageContext,
injectable,
getOutboundMessageContext,
} from '@aries-framework/core'

import { ActionMenuRole } from './ActionMenuRole'
Expand Down Expand Up @@ -66,10 +66,10 @@ export class ActionMenuApi {
connection,
})

const outboundMessageContext = new OutboundMessageContext(message, {
agentContext: this.agentContext,
connection,
const outboundMessageContext = await getOutboundMessageContext(this.agentContext, {
message,
associatedRecord: record,
connectionRecord: connection,
})

await this.messageSender.sendMessage(outboundMessageContext)
Expand All @@ -92,10 +92,10 @@ export class ActionMenuApi {
menu: options.menu,
})

const outboundMessageContext = new OutboundMessageContext(message, {
agentContext: this.agentContext,
connection,
const outboundMessageContext = await getOutboundMessageContext(this.agentContext, {
message,
associatedRecord: record,
connectionRecord: connection,
})

await this.messageSender.sendMessage(outboundMessageContext)
Expand Down Expand Up @@ -126,10 +126,10 @@ export class ActionMenuApi {
performedAction: options.performedAction,
})

const outboundMessageContext = new OutboundMessageContext(message, {
agentContext: this.agentContext,
connection,
const outboundMessageContext = await getOutboundMessageContext(this.agentContext, {
message,
associatedRecord: record,
connectionRecord: connection,
})

await this.messageSender.sendMessage(outboundMessageContext)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,18 +215,18 @@ export class V1CredentialProtocol
credentialRecord.assertProtocolVersion('v1')
credentialRecord.assertState(CredentialState.OfferSent)

const previousReceivedMessage = await didCommMessageRepository.findAgentMessage(messageContext.agentContext, {
const lastReceivedMessage = await didCommMessageRepository.findAgentMessage(messageContext.agentContext, {
associatedRecordId: credentialRecord.id,
messageClass: V1ProposeCredentialMessage,
})
const previousSentMessage = await didCommMessageRepository.getAgentMessage(messageContext.agentContext, {
const lastSentMessage = await didCommMessageRepository.getAgentMessage(messageContext.agentContext, {
associatedRecordId: credentialRecord.id,
messageClass: V1OfferCredentialMessage,
})

connectionService.assertConnectionOrServiceDecorator(messageContext, {
previousReceivedMessage,
previousSentMessage,
await connectionService.assertConnectionOrOutOfBandExchange(messageContext, {
lastReceivedMessage,
lastSentMessage,
})

await this.indyCredentialFormat.processProposal(messageContext.agentContext, {
Expand Down Expand Up @@ -257,7 +257,7 @@ export class V1CredentialProtocol
})

// Assert
connectionService.assertConnectionOrServiceDecorator(messageContext)
await connectionService.assertConnectionOrOutOfBandExchange(messageContext)

// Save record
await credentialRepository.save(messageContext.agentContext, credentialRecord)
Expand Down Expand Up @@ -506,21 +506,21 @@ export class V1CredentialProtocol
}

if (credentialRecord) {
const previousSentMessage = await didCommMessageRepository.getAgentMessage(messageContext.agentContext, {
const lastSentMessage = await didCommMessageRepository.getAgentMessage(messageContext.agentContext, {
associatedRecordId: credentialRecord.id,
messageClass: V1ProposeCredentialMessage,
})
const previousReceivedMessage = await didCommMessageRepository.findAgentMessage(messageContext.agentContext, {
const lastReceivedMessage = await didCommMessageRepository.findAgentMessage(messageContext.agentContext, {
associatedRecordId: credentialRecord.id,
messageClass: V1OfferCredentialMessage,
})

// Assert
credentialRecord.assertProtocolVersion('v1')
credentialRecord.assertState(CredentialState.ProposalSent)
connectionService.assertConnectionOrServiceDecorator(messageContext, {
previousReceivedMessage,
previousSentMessage,
await connectionService.assertConnectionOrOutOfBandExchange(messageContext, {
lastReceivedMessage,
lastSentMessage,
})

await this.indyCredentialFormat.processOffer(messageContext.agentContext, {
Expand All @@ -546,7 +546,7 @@ export class V1CredentialProtocol
})

// Assert
connectionService.assertConnectionOrServiceDecorator(messageContext)
await connectionService.assertConnectionOrOutOfBandExchange(messageContext)

await this.indyCredentialFormat.processOffer(messageContext.agentContext, {
credentialRecord,
Expand Down Expand Up @@ -750,9 +750,9 @@ export class V1CredentialProtocol
// Assert
credentialRecord.assertProtocolVersion('v1')
credentialRecord.assertState(CredentialState.OfferSent)
connectionService.assertConnectionOrServiceDecorator(messageContext, {
previousReceivedMessage: proposalMessage ?? undefined,
previousSentMessage: offerMessage ?? undefined,
await connectionService.assertConnectionOrOutOfBandExchange(messageContext, {
lastReceivedMessage: proposalMessage ?? undefined,
lastSentMessage: offerMessage ?? undefined,
})

const requestAttachment = requestMessage.getRequestAttachmentById(INDY_CREDENTIAL_REQUEST_ATTACHMENT_ID)
Expand Down Expand Up @@ -886,9 +886,9 @@ export class V1CredentialProtocol
// Assert
credentialRecord.assertProtocolVersion('v1')
credentialRecord.assertState(CredentialState.RequestSent)
connectionService.assertConnectionOrServiceDecorator(messageContext, {
previousReceivedMessage: offerCredentialMessage,
previousSentMessage: requestCredentialMessage,
await connectionService.assertConnectionOrOutOfBandExchange(messageContext, {
lastReceivedMessage: offerCredentialMessage,
lastSentMessage: requestCredentialMessage,
})

const issueAttachment = issueMessage.getCredentialAttachmentById(INDY_CREDENTIAL_ATTACHMENT_ID)
Expand Down Expand Up @@ -981,9 +981,9 @@ export class V1CredentialProtocol
// Assert
credentialRecord.assertProtocolVersion('v1')
credentialRecord.assertState(CredentialState.CredentialIssued)
connectionService.assertConnectionOrServiceDecorator(messageContext, {
previousReceivedMessage: requestCredentialMessage,
previousSentMessage: issueCredentialMessage,
await connectionService.assertConnectionOrOutOfBandExchange(messageContext, {
lastReceivedMessage: requestCredentialMessage,
lastSentMessage: issueCredentialMessage,
})

// Update record
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ const credentialIssueMessage = new V1IssueCredentialMessage({

const didCommMessageRecord = new DidCommMessageRecord({
associatedRecordId: '04a2c382-999e-4de9-a1d2-9dec0b2fa5e4',
message: {},
message: { '@id': '123', '@type': 'https://didcomm.org/issue-credential/1.0/offer-credential' },
role: DidCommMessageRole.Receiver,
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ describe('V1 Connectionless Credentials', () => {
protocolVersion: 'v1',
})

const { message: offerMessage } = await faberAgent.oob.createLegacyConnectionlessInvitation({
const { invitationUrl } = await faberAgent.oob.createLegacyConnectionlessInvitation({
recordId: faberCredentialRecord.id,
message,
domain: 'https://a-domain.com',
})

await aliceAgent.receiveMessage(offerMessage.toJSON())
await aliceAgent.oob.receiveInvitationFromUrl(invitationUrl)

let aliceCredentialRecord = await waitForCredentialRecordSubject(aliceReplay, {
threadId: faberCredentialRecord.threadId,
Expand Down Expand Up @@ -162,7 +162,6 @@ describe('V1 Connectionless Credentials', () => {
})

const { message: offerMessage } = await faberAgent.oob.createLegacyConnectionlessInvitation({
recordId: faberCredentialRecord.id,
message,
domain: 'https://a-domain.com',
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { V1CredentialProtocol } from '../V1CredentialProtocol'
import type { MessageHandler, MessageHandlerInboundMessage, CredentialExchangeRecord } from '@aries-framework/core'

import { DidCommMessageRepository, OutboundMessageContext } from '@aries-framework/core'
import { AriesFrameworkError, getOutboundMessageContext } from '@aries-framework/core'

import { V1IssueCredentialMessage, V1RequestCredentialMessage } from '../messages'
import { V1IssueCredentialMessage } from '../messages'

export class V1IssueCredentialHandler implements MessageHandler {
private credentialProtocol: V1CredentialProtocol
Expand Down Expand Up @@ -36,31 +36,20 @@ export class V1IssueCredentialHandler implements MessageHandler {
credentialRecord,
})

const didCommMessageRepository = messageContext.agentContext.dependencyManager.resolve(DidCommMessageRepository)
const requestMessage = await didCommMessageRepository.getAgentMessage(messageContext.agentContext, {
associatedRecordId: credentialRecord.id,
messageClass: V1RequestCredentialMessage,
})

if (messageContext.connection) {
return new OutboundMessageContext(message, {
agentContext: messageContext.agentContext,
connection: messageContext.connection,
associatedRecord: credentialRecord,
})
} else if (messageContext.message.service && requestMessage.service) {
const recipientService = messageContext.message.service
const ourService = requestMessage.service

return new OutboundMessageContext(message, {
agentContext: messageContext.agentContext,
serviceParams: {
service: recipientService.resolvedDidCommService,
senderKey: ourService.resolvedDidCommService.recipientKeys[0],
},
})
const requestMessage = await this.credentialProtocol.findRequestMessage(
messageContext.agentContext,
credentialRecord.id
)
if (!requestMessage) {
throw new AriesFrameworkError(`No request message found for credential record with id '${credentialRecord.id}'`)
}

messageContext.agentContext.config.logger.error(`Could not automatically create credential ack`)
return getOutboundMessageContext(messageContext.agentContext, {
connectionRecord: messageContext.connection,
message,
associatedRecord: credentialRecord,
lastReceivedMessage: messageContext.message,
lastSentMessage: requestMessage,
})
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import type { V1CredentialProtocol } from '../V1CredentialProtocol'
import type { MessageHandler, MessageHandlerInboundMessage, CredentialExchangeRecord } from '@aries-framework/core'

import {
OutboundMessageContext,
RoutingService,
DidCommMessageRepository,
DidCommMessageRole,
ServiceDecorator,
} from '@aries-framework/core'
import { getOutboundMessageContext } from '@aries-framework/core'

import { V1OfferCredentialMessage } from '../messages'

Expand Down Expand Up @@ -37,47 +31,13 @@ export class V1OfferCredentialHandler implements MessageHandler {
messageContext: MessageHandlerInboundMessage<V1OfferCredentialHandler>
) {
messageContext.agentContext.config.logger.info(`Automatically sending request with autoAccept`)
if (messageContext.connection) {
const { message } = await this.credentialProtocol.acceptOffer(messageContext.agentContext, { credentialRecord })
const { message } = await this.credentialProtocol.acceptOffer(messageContext.agentContext, { credentialRecord })

return new OutboundMessageContext(message, {
agentContext: messageContext.agentContext,
connection: messageContext.connection,
associatedRecord: credentialRecord,
})
} else if (messageContext.message.service) {
const routingService = messageContext.agentContext.dependencyManager.resolve(RoutingService)
const routing = await routingService.getRouting(messageContext.agentContext)
const ourService = new ServiceDecorator({
serviceEndpoint: routing.endpoints[0],
recipientKeys: [routing.recipientKey.publicKeyBase58],
routingKeys: routing.routingKeys.map((key) => key.publicKeyBase58),
})
const recipientService = messageContext.message.service

const { message } = await this.credentialProtocol.acceptOffer(messageContext.agentContext, {
credentialRecord,
})

// Set and save ~service decorator to record (to remember our verkey)
message.service = ourService

const didCommMessageRepository = messageContext.agentContext.dependencyManager.resolve(DidCommMessageRepository)
await didCommMessageRepository.saveOrUpdateAgentMessage(messageContext.agentContext, {
agentMessage: message,
role: DidCommMessageRole.Sender,
associatedRecordId: credentialRecord.id,
})

return new OutboundMessageContext(message, {
agentContext: messageContext.agentContext,
serviceParams: {
service: recipientService.resolvedDidCommService,
senderKey: ourService.resolvedDidCommService.recipientKeys[0],
},
})
}

messageContext.agentContext.config.logger.error(`Could not automatically create credential request`)
return getOutboundMessageContext(messageContext.agentContext, {
connectionRecord: messageContext.connection,
message,
associatedRecord: credentialRecord,
lastReceivedMessage: messageContext.message,
})
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { V1CredentialProtocol } from '../V1CredentialProtocol'
import type { CredentialExchangeRecord, MessageHandler, MessageHandlerInboundMessage } from '@aries-framework/core'

import { OutboundMessageContext } from '@aries-framework/core'
import { getOutboundMessageContext } from '@aries-framework/core'

import { V1ProposeCredentialMessage } from '../messages'

Expand Down Expand Up @@ -44,9 +44,9 @@ export class V1ProposeCredentialHandler implements MessageHandler {
credentialRecord,
})

return new OutboundMessageContext(message, {
agentContext: messageContext.agentContext,
connection: messageContext.connection,
return getOutboundMessageContext(messageContext.agentContext, {
message,
connectionRecord: messageContext.connection,
associatedRecord: credentialRecord,
})
}
Expand Down
Loading

0 comments on commit 9e69cf4

Please sign in to comment.