-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3331a3c
commit 735d578
Showing
15 changed files
with
636 additions
and
159 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** | ||
* Typing of the state for auto acceptance | ||
*/ | ||
export enum AutoAcceptProof { | ||
// Always auto accepts the proof no matter if it changed in subsequent steps | ||
Always = 'always', | ||
|
||
// Needs one acceptation and the rest will be automated if nothing changes | ||
ContentApproved = 'contentApproved', | ||
|
||
// DEFAULT: Never auto accept a proof | ||
Never = 'never', | ||
} |
86 changes: 86 additions & 0 deletions
86
packages/core/src/modules/proofs/ProofResponseCoordinator.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,86 @@ | ||
import type { ProofRecord } from './repository' | ||
|
||
import { scoped, Lifecycle } from 'tsyringe' | ||
|
||
import { AgentConfig } from '../../agent/AgentConfig' | ||
|
||
import { AutoAcceptProof } from './ProofAutoAcceptType' | ||
|
||
/** | ||
* This class handles all the automation with all the messages in the present proof protocol | ||
* Every function returns `true` if it should automate the flow and `false` if not | ||
*/ | ||
@scoped(Lifecycle.ContainerScoped) | ||
export class ProofResponseCoordinator { | ||
private agentConfig: AgentConfig | ||
|
||
public constructor(agentConfig: AgentConfig) { | ||
this.agentConfig = agentConfig | ||
} | ||
|
||
/** | ||
* Returns the proof auto accept config based on priority: | ||
* - The record config takes first priority | ||
* - Otherwise the agent config | ||
* - Otherwise {@link AutoAcceptProof.Never} is returned | ||
*/ | ||
private static composeAutoAccept( | ||
recordConfig: AutoAcceptProof | undefined, | ||
agentConfig: AutoAcceptProof | undefined | ||
) { | ||
return recordConfig ?? agentConfig ?? AutoAcceptProof.Never | ||
} | ||
|
||
/** | ||
* Checks whether it should automatically respond to a proposal | ||
*/ | ||
public shoudlAutoRespondToProposal(proofRecord: ProofRecord) { | ||
const autoAccept = ProofResponseCoordinator.composeAutoAccept( | ||
proofRecord.autoAcceptProof, | ||
this.agentConfig.autoAcceptProofs | ||
) | ||
|
||
if (autoAccept === AutoAcceptProof.Always) { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
/** | ||
* Checks whether it should automatically respond to a request | ||
*/ | ||
public shouldAutoRespondToRequest(proofRecord: ProofRecord) { | ||
const autoAccept = ProofResponseCoordinator.composeAutoAccept( | ||
proofRecord.autoAcceptProof, | ||
this.agentConfig.autoAcceptProofs | ||
) | ||
|
||
if ( | ||
autoAccept === AutoAcceptProof.Always || | ||
(autoAccept === AutoAcceptProof.ContentApproved && proofRecord.proposalMessage) | ||
) { | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
/** | ||
* Checks whether it should automatically respond to a presention of proof | ||
*/ | ||
public shouldAutoRespondToPresentation(proofRecord: ProofRecord) { | ||
const autoAccept = ProofResponseCoordinator.composeAutoAccept( | ||
proofRecord.autoAcceptProof, | ||
this.agentConfig.autoAcceptProofs | ||
) | ||
|
||
if ( | ||
autoAccept === AutoAcceptProof.Always || | ||
(autoAccept === AutoAcceptProof.ContentApproved && proofRecord.requestMessage) | ||
) { | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
} |
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
35 changes: 33 additions & 2 deletions
35
packages/core/src/modules/proofs/handlers/PresentationHandler.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 |
---|---|---|
@@ -1,17 +1,48 @@ | ||
import type { AgentConfig } from '../../../agent/AgentConfig' | ||
import type { Handler, HandlerInboundMessage } from '../../../agent/Handler' | ||
import type { ProofResponseCoordinator } from '../ProofResponseCoordinator' | ||
import type { ProofRecord } from '../repository' | ||
import type { ProofService } from '../services' | ||
|
||
import { createOutboundMessage } from '../../../agent/helpers' | ||
import { PresentationMessage } from '../messages' | ||
|
||
export class PresentationHandler implements Handler { | ||
private proofService: ProofService | ||
private agentConfig: AgentConfig | ||
private proofResponseCoordinator: ProofResponseCoordinator | ||
public supportedMessages = [PresentationMessage] | ||
|
||
public constructor(proofService: ProofService) { | ||
public constructor( | ||
proofService: ProofService, | ||
agentConfig: AgentConfig, | ||
proofResponseCoordinator: ProofResponseCoordinator | ||
) { | ||
this.proofService = proofService | ||
this.agentConfig = agentConfig | ||
this.proofResponseCoordinator = proofResponseCoordinator | ||
} | ||
|
||
public async handle(messageContext: HandlerInboundMessage<PresentationHandler>) { | ||
await this.proofService.processPresentation(messageContext) | ||
const proofRecord = await this.proofService.processPresentation(messageContext) | ||
|
||
if (this.proofResponseCoordinator.shouldAutoRespondToPresentation(proofRecord)) { | ||
return await this.createAck(proofRecord, messageContext) | ||
} | ||
} | ||
|
||
private async createAck(proofRecord: ProofRecord, messageContext: HandlerInboundMessage<PresentationHandler>) { | ||
this.agentConfig.logger.info( | ||
`Automatically sending acknowledgement with autoAccept on ${this.agentConfig.autoAcceptProofs}` | ||
) | ||
|
||
if (!messageContext.connection) { | ||
this.agentConfig.logger.error('No connection on the messageContext') | ||
return | ||
} | ||
|
||
const { message } = await this.proofService.createAck(proofRecord) | ||
|
||
return createOutboundMessage(messageContext.connection, message) | ||
} | ||
} |
49 changes: 47 additions & 2 deletions
49
packages/core/src/modules/proofs/handlers/ProposePresentationHandler.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 |
---|---|---|
@@ -1,17 +1,62 @@ | ||
import type { AgentConfig } from '../../../agent/AgentConfig' | ||
import type { Handler, HandlerInboundMessage } from '../../../agent/Handler' | ||
import type { ProofResponseCoordinator } from '../ProofResponseCoordinator' | ||
import type { ProofRecord } from '../repository' | ||
import type { ProofService } from '../services' | ||
|
||
import { createOutboundMessage } from '../../../agent/helpers' | ||
import { ProposePresentationMessage } from '../messages' | ||
|
||
export class ProposePresentationHandler implements Handler { | ||
private proofService: ProofService | ||
private agentConfig: AgentConfig | ||
private proofResponseCoordinator: ProofResponseCoordinator | ||
public supportedMessages = [ProposePresentationMessage] | ||
|
||
public constructor(proofService: ProofService) { | ||
public constructor( | ||
proofService: ProofService, | ||
agentConfig: AgentConfig, | ||
proofResponseCoordinator: ProofResponseCoordinator | ||
) { | ||
this.proofService = proofService | ||
this.agentConfig = agentConfig | ||
this.proofResponseCoordinator = proofResponseCoordinator | ||
} | ||
|
||
public async handle(messageContext: HandlerInboundMessage<ProposePresentationHandler>) { | ||
await this.proofService.processProposal(messageContext) | ||
const proofRecord = await this.proofService.processProposal(messageContext) | ||
|
||
if (this.proofResponseCoordinator.shoudlAutoRespondToProposal(proofRecord)) { | ||
return await this.createRequest(proofRecord, messageContext) | ||
} | ||
} | ||
|
||
private async createRequest( | ||
proofRecord: ProofRecord, | ||
messageContext: HandlerInboundMessage<ProposePresentationHandler> | ||
) { | ||
this.agentConfig.logger.info( | ||
`Automatically sending request with autoAccept on ${this.agentConfig.autoAcceptProofs}` | ||
) | ||
|
||
if (!messageContext.connection) { | ||
this.agentConfig.logger.error('No connection on the messageContext') | ||
return | ||
} | ||
if (!proofRecord.proposalMessage) { | ||
this.agentConfig.logger.error(`Proof record with id ${proofRecord.id} is missing required credential proposal`) | ||
return | ||
} | ||
const proofRequest = await this.proofService.createProofRequestFromProposal( | ||
proofRecord.proposalMessage.presentationProposal, | ||
{ | ||
name: 'proof-request', | ||
version: '1.0', | ||
} | ||
) | ||
|
||
const { message } = await this.proofService.createRequestAsResponse(proofRecord, proofRequest) | ||
|
||
return createOutboundMessage(messageContext.connection, message) | ||
} | ||
} |
51 changes: 49 additions & 2 deletions
51
packages/core/src/modules/proofs/handlers/RequestPresentationHandler.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 |
---|---|---|
@@ -1,17 +1,64 @@ | ||
import type { AgentConfig } from '../../../agent/AgentConfig' | ||
import type { Handler, HandlerInboundMessage } from '../../../agent/Handler' | ||
import type { ProofResponseCoordinator } from '../ProofResponseCoordinator' | ||
import type { ProofRecord } from '../repository' | ||
import type { ProofService } from '../services' | ||
|
||
import { createOutboundMessage } from '../../../agent/helpers' | ||
import { RequestPresentationMessage } from '../messages' | ||
|
||
export class RequestPresentationHandler implements Handler { | ||
private proofService: ProofService | ||
private agentConfig: AgentConfig | ||
private proofResponseCoordinator: ProofResponseCoordinator | ||
public supportedMessages = [RequestPresentationMessage] | ||
|
||
public constructor(proofService: ProofService) { | ||
public constructor( | ||
proofService: ProofService, | ||
agentConfig: AgentConfig, | ||
proofResponseCoordinator: ProofResponseCoordinator | ||
) { | ||
this.proofService = proofService | ||
this.agentConfig = agentConfig | ||
this.proofResponseCoordinator = proofResponseCoordinator | ||
} | ||
|
||
public async handle(messageContext: HandlerInboundMessage<RequestPresentationHandler>) { | ||
await this.proofService.processRequest(messageContext) | ||
const proofRecord = await this.proofService.processRequest(messageContext) | ||
|
||
if (this.proofResponseCoordinator.shouldAutoRespondToRequest(proofRecord)) { | ||
return await this.createPresentation(proofRecord, messageContext) | ||
} | ||
} | ||
|
||
private async createPresentation( | ||
proofRecord: ProofRecord, | ||
messageContext: HandlerInboundMessage<RequestPresentationHandler> | ||
) { | ||
const indyProofRequest = proofRecord.requestMessage?.indyProofRequest | ||
|
||
this.agentConfig.logger.info( | ||
`Automatically sending presentation with autoAccept on ${this.agentConfig.autoAcceptProofs}` | ||
) | ||
|
||
if (!messageContext.connection) { | ||
this.agentConfig.logger.error('No connection on the messageContext') | ||
return | ||
} | ||
|
||
if (!indyProofRequest) { | ||
return | ||
} | ||
|
||
const retrievedCredentials = await this.proofService.getRequestedCredentialsForProofRequest( | ||
indyProofRequest, | ||
proofRecord.proposalMessage?.presentationProposal | ||
) | ||
|
||
const requestedCredentials = this.proofService.autoSelectCredentialsForProofRequest(retrievedCredentials) | ||
|
||
const { message } = await this.proofService.createPresentation(proofRecord, requestedCredentials) | ||
|
||
return createOutboundMessage(messageContext.connection, message) | ||
} | ||
} |
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.