Skip to content

Commit

Permalink
fix: abandon proof protocol if presentation fails (openwallet-foundat…
Browse files Browse the repository at this point in the history
…ion#1610)

Signed-off-by: Timo Glastra <[email protected]>
Signed-off-by: Martin Auer <[email protected]>
  • Loading branch information
TimoGlastra authored and auer-martin committed Dec 4, 2023
1 parent 68fdca5 commit ec17a87
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 21 deletions.
43 changes: 35 additions & 8 deletions packages/anoncreds/src/protocols/proofs/v1/V1ProofProtocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -777,26 +777,53 @@ export class V1ProofProtocol extends BaseProofProtocol implements ProofProtocol<

const presentationAttachment = presentationMessage.getPresentationAttachmentById(INDY_PROOF_ATTACHMENT_ID)
if (!presentationAttachment) {
throw new AriesFrameworkError('Missing indy proof attachment in processPresentation')
proofRecord.errorMessage = 'Missing indy proof attachment'
await this.updateState(agentContext, proofRecord, ProofState.Abandoned)
throw new V1PresentationProblemReportError(proofRecord.errorMessage, {
problemCode: PresentationProblemReportReason.Abandoned,
})
}

const requestAttachment = requestMessage.getRequestAttachmentById(INDY_PROOF_REQUEST_ATTACHMENT_ID)
if (!requestAttachment) {
throw new AriesFrameworkError('Missing indy proof request attachment in processPresentation')
proofRecord.errorMessage = 'Missing indy proof request attachment'
await this.updateState(agentContext, proofRecord, ProofState.Abandoned)
throw new V1PresentationProblemReportError(proofRecord.errorMessage, {
problemCode: PresentationProblemReportReason.Abandoned,
})
}

const isValid = await this.indyProofFormat.processPresentation(agentContext, {
proofRecord,
attachment: presentationAttachment,
requestAttachment,
})

await didCommMessageRepository.saveAgentMessage(agentContext, {
agentMessage: presentationMessage,
associatedRecordId: proofRecord.id,
role: DidCommMessageRole.Receiver,
})

let isValid: boolean
try {
isValid = await this.indyProofFormat.processPresentation(agentContext, {
proofRecord,
attachment: presentationAttachment,
requestAttachment,
})
} catch (error) {
proofRecord.errorMessage = error.message ?? 'Error verifying proof on presentation'
proofRecord.isVerified = false
await this.updateState(agentContext, proofRecord, ProofState.Abandoned)
throw new V1PresentationProblemReportError('Error verifying proof on presentation', {
problemCode: PresentationProblemReportReason.Abandoned,
})
}

if (!isValid) {
proofRecord.errorMessage = 'Invalid proof'
proofRecord.isVerified = false
await this.updateState(agentContext, proofRecord, ProofState.Abandoned)
throw new V1PresentationProblemReportError('Invalid proof', {
problemCode: PresentationProblemReportReason.Abandoned,
})
}

// Update record
proofRecord.isVerified = isValid
await this.updateState(agentContext, proofRecord, ProofState.PresentationReceived)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ export class ProofFormatCoordinator<PFs extends ProofFormatService[]> {
requestMessage: V2RequestPresentationMessage
formatServices: ProofFormatService[]
}
) {
): Promise<{ isValid: true; message: undefined } | { isValid: false; message: string }> {
const didCommMessageRepository = agentContext.dependencyManager.resolve(DidCommMessageRepository)

const formatVerificationResults: boolean[] = []
Expand All @@ -476,13 +476,21 @@ export class ProofFormatCoordinator<PFs extends ProofFormatService[]> {
requestMessage.requestAttachments
)

const isValid = await formatService.processPresentation(agentContext, {
attachment,
requestAttachment,
proofRecord,
})

formatVerificationResults.push(isValid)
try {
// TODO: this should return a more complex object explaining why it is invalid
const isValid = await formatService.processPresentation(agentContext, {
attachment,
requestAttachment,
proofRecord,
})

formatVerificationResults.push(isValid)
} catch (error) {
return {
message: error.message,
isValid: false,
}
}
}

await didCommMessageRepository.saveOrUpdateAgentMessage(agentContext, {
Expand All @@ -491,7 +499,19 @@ export class ProofFormatCoordinator<PFs extends ProofFormatService[]> {
associatedRecordId: proofRecord.id,
})

return formatVerificationResults.every((isValid) => isValid === true)
const isValid = formatVerificationResults.every((isValid) => isValid === true)

if (isValid) {
return {
isValid,
message: undefined,
}
} else {
return {
isValid,
message: 'Not all presentations are valid',
}
}
}

public getAttachmentForService(
Expand Down
23 changes: 19 additions & 4 deletions packages/core/src/modules/proofs/protocol/v2/V2ProofProtocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import { composeAutoAccept } from '../../utils/composeAutoAccept'
import { BaseProofProtocol } from '../BaseProofProtocol'

import { ProofFormatCoordinator } from './ProofFormatCoordinator'
import { V2PresentationProblemReportError } from './errors'
import { V2PresentationAckHandler } from './handlers/V2PresentationAckHandler'
import { V2PresentationHandler } from './handlers/V2PresentationHandler'
import { V2PresentationProblemReportHandler } from './handlers/V2PresentationProblemReportHandler'
Expand Down Expand Up @@ -672,19 +673,33 @@ export class V2ProofProtocol<PFs extends ProofFormatService[] = ProofFormatServi
}

const formatServices = this.getFormatServicesFromMessage(presentationMessage.formats)
// Abandon if no supported formats
if (formatServices.length === 0) {
throw new AriesFrameworkError(`Unable to process presentation. No supported formats`)
proofRecord.errorMessage = `Unable to process presentation. No supported formats`
await this.updateState(messageContext.agentContext, proofRecord, ProofState.Abandoned)
throw new V2PresentationProblemReportError(proofRecord.errorMessage, {
problemCode: PresentationProblemReportReason.Abandoned,
})
}

const isValid = await this.proofFormatCoordinator.processPresentation(messageContext.agentContext, {
const result = await this.proofFormatCoordinator.processPresentation(messageContext.agentContext, {
proofRecord,
formatServices,
requestMessage: lastSentMessage,
message: presentationMessage,
})

proofRecord.isVerified = isValid
await this.updateState(messageContext.agentContext, proofRecord, ProofState.PresentationReceived)
proofRecord.isVerified = result.isValid
if (result.isValid) {
await this.updateState(messageContext.agentContext, proofRecord, ProofState.PresentationReceived)
} else {
proofRecord.errorMessage = result.message
proofRecord.isVerified = false
await this.updateState(messageContext.agentContext, proofRecord, ProofState.Abandoned)
throw new V2PresentationProblemReportError(proofRecord.errorMessage, {
problemCode: PresentationProblemReportReason.Abandoned,
})
}

return proofRecord
}
Expand Down

0 comments on commit ec17a87

Please sign in to comment.