From e19b77972d2a62d04b6f6af5231b6830b7e0268a Mon Sep 17 00:00:00 2001 From: blu3beri Date: Wed, 23 Nov 2022 14:37:41 +0100 Subject: [PATCH 1/9] fix(proof): auto respond on content approved bug Signed-off-by: blu3beri --- .../modules/credentials/protocol/v1/V1CredentialService.ts | 2 +- .../core/src/modules/proofs/ProofResponseCoordinator.ts | 6 +++--- .../protocol/v1/handlers/V1RequestPresentationHandler.ts | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/core/src/modules/credentials/protocol/v1/V1CredentialService.ts b/packages/core/src/modules/credentials/protocol/v1/V1CredentialService.ts index c9bba83a18..c3598ab3bb 100644 --- a/packages/core/src/modules/credentials/protocol/v1/V1CredentialService.ts +++ b/packages/core/src/modules/credentials/protocol/v1/V1CredentialService.ts @@ -963,7 +963,7 @@ export class V1CredentialService extends CredentialService<[IndyCredentialFormat credentialRecord: CredentialExchangeRecord proposalMessage: V1ProposeCredentialMessage } - ): Promise { + ) { const { credentialRecord, proposalMessage } = options const autoAccept = composeAutoAccept( credentialRecord.autoAcceptCredential, diff --git a/packages/core/src/modules/proofs/ProofResponseCoordinator.ts b/packages/core/src/modules/proofs/ProofResponseCoordinator.ts index 8996649c4f..77c9a04fd5 100644 --- a/packages/core/src/modules/proofs/ProofResponseCoordinator.ts +++ b/packages/core/src/modules/proofs/ProofResponseCoordinator.ts @@ -34,7 +34,7 @@ export class ProofResponseCoordinator { /** * Checks whether it should automatically respond to a proposal */ - public shouldAutoRespondToProposal(agentContext: AgentContext, proofRecord: ProofExchangeRecord) { + public async shouldAutoRespondToProposal(agentContext: AgentContext, proofRecord: ProofExchangeRecord) { const autoAccept = ProofResponseCoordinator.composeAutoAccept( proofRecord.autoAcceptProof, agentContext.config.autoAcceptProofs @@ -54,7 +54,7 @@ export class ProofResponseCoordinator { /** * Checks whether it should automatically respond to a request */ - public shouldAutoRespondToRequest(agentContext: AgentContext, proofRecord: ProofExchangeRecord) { + public async shouldAutoRespondToRequest(agentContext: AgentContext, proofRecord: ProofExchangeRecord) { const autoAccept = ProofResponseCoordinator.composeAutoAccept( proofRecord.autoAcceptProof, agentContext.config.autoAcceptProofs @@ -74,7 +74,7 @@ export class ProofResponseCoordinator { /** * Checks whether it should automatically respond to a presentation of proof */ - public shouldAutoRespondToPresentation(agentContext: AgentContext, proofRecord: ProofExchangeRecord) { + public async shouldAutoRespondToPresentation(agentContext: AgentContext, proofRecord: ProofExchangeRecord) { const autoAccept = ProofResponseCoordinator.composeAutoAccept( proofRecord.autoAcceptProof, agentContext.config.autoAcceptProofs diff --git a/packages/core/src/modules/proofs/protocol/v1/handlers/V1RequestPresentationHandler.ts b/packages/core/src/modules/proofs/protocol/v1/handlers/V1RequestPresentationHandler.ts index 5a0455baba..eaa07fc66b 100644 --- a/packages/core/src/modules/proofs/protocol/v1/handlers/V1RequestPresentationHandler.ts +++ b/packages/core/src/modules/proofs/protocol/v1/handlers/V1RequestPresentationHandler.ts @@ -44,7 +44,7 @@ export class V1RequestPresentationHandler implements Handler { public async handle(messageContext: HandlerInboundMessage) { const proofRecord = await this.proofService.processRequest(messageContext) - if (this.proofResponseCoordinator.shouldAutoRespondToRequest(messageContext.agentContext, proofRecord)) { + if (await this.proofResponseCoordinator.shouldAutoRespondToRequest(messageContext.agentContext, proofRecord)) { return await this.createPresentation(proofRecord, messageContext) } } From ab8f0215b919c39a1e374a11dec96555840793b4 Mon Sep 17 00:00:00 2001 From: blu3beri Date: Wed, 23 Nov 2022 14:49:43 +0100 Subject: [PATCH 2/9] fix(proof): abstract the autoRespond to variable for IDE support Signed-off-by: blu3beri --- .../proofs/protocol/v1/handlers/V1PresentationHandler.ts | 7 ++++++- .../protocol/v1/handlers/V1ProposePresentationHandler.ts | 8 +++++++- .../protocol/v1/handlers/V1RequestPresentationHandler.ts | 8 +++++++- .../proofs/protocol/v2/handlers/V2PresentationHandler.ts | 7 ++++++- .../protocol/v2/handlers/V2ProposePresentationHandler.ts | 7 ++++++- .../protocol/v2/handlers/V2RequestPresentationHandler.ts | 8 +++++++- .../core/src/storage/migration/__tests__/backup.test.ts | 6 ++++-- 7 files changed, 43 insertions(+), 8 deletions(-) diff --git a/packages/core/src/modules/proofs/protocol/v1/handlers/V1PresentationHandler.ts b/packages/core/src/modules/proofs/protocol/v1/handlers/V1PresentationHandler.ts index c3fcd713d2..7ecd43d6ee 100644 --- a/packages/core/src/modules/proofs/protocol/v1/handlers/V1PresentationHandler.ts +++ b/packages/core/src/modules/proofs/protocol/v1/handlers/V1PresentationHandler.ts @@ -30,7 +30,12 @@ export class V1PresentationHandler implements Handler { public async handle(messageContext: HandlerInboundMessage) { const proofRecord = await this.proofService.processPresentation(messageContext) - if (this.proofResponseCoordinator.shouldAutoRespondToPresentation(messageContext.agentContext, proofRecord)) { + const shouldAutoRespond = await this.proofResponseCoordinator.shouldAutoRespondToPresentation( + messageContext.agentContext, + proofRecord + ) + + if (shouldAutoRespond) { return await this.createAck(proofRecord, messageContext) } } diff --git a/packages/core/src/modules/proofs/protocol/v1/handlers/V1ProposePresentationHandler.ts b/packages/core/src/modules/proofs/protocol/v1/handlers/V1ProposePresentationHandler.ts index 4e9d437669..bec5d693b0 100644 --- a/packages/core/src/modules/proofs/protocol/v1/handlers/V1ProposePresentationHandler.ts +++ b/packages/core/src/modules/proofs/protocol/v1/handlers/V1ProposePresentationHandler.ts @@ -33,7 +33,13 @@ export class V1ProposePresentationHandler implements Handler { public async handle(messageContext: HandlerInboundMessage) { const proofRecord = await this.proofService.processProposal(messageContext) - if (this.proofResponseCoordinator.shouldAutoRespondToProposal(messageContext.agentContext, proofRecord)) { + + const shouldAutoRespond = await this.proofResponseCoordinator.shouldAutoRespondToProposal( + messageContext.agentContext, + proofRecord + ) + + if (shouldAutoRespond) { return await this.createRequest(proofRecord, messageContext) } } diff --git a/packages/core/src/modules/proofs/protocol/v1/handlers/V1RequestPresentationHandler.ts b/packages/core/src/modules/proofs/protocol/v1/handlers/V1RequestPresentationHandler.ts index eaa07fc66b..addfdb3a6c 100644 --- a/packages/core/src/modules/proofs/protocol/v1/handlers/V1RequestPresentationHandler.ts +++ b/packages/core/src/modules/proofs/protocol/v1/handlers/V1RequestPresentationHandler.ts @@ -44,7 +44,13 @@ export class V1RequestPresentationHandler implements Handler { public async handle(messageContext: HandlerInboundMessage) { const proofRecord = await this.proofService.processRequest(messageContext) - if (await this.proofResponseCoordinator.shouldAutoRespondToRequest(messageContext.agentContext, proofRecord)) { + + const shouldAutoRespond = await this.proofResponseCoordinator.shouldAutoRespondToRequest( + messageContext.agentContext, + proofRecord + ) + + if (shouldAutoRespond) { return await this.createPresentation(proofRecord, messageContext) } } diff --git a/packages/core/src/modules/proofs/protocol/v2/handlers/V2PresentationHandler.ts b/packages/core/src/modules/proofs/protocol/v2/handlers/V2PresentationHandler.ts index c812b6e8bd..a0f2b3e5d1 100644 --- a/packages/core/src/modules/proofs/protocol/v2/handlers/V2PresentationHandler.ts +++ b/packages/core/src/modules/proofs/protocol/v2/handlers/V2PresentationHandler.ts @@ -30,7 +30,12 @@ export class V2PresentationHandler implements Handler { public async handle(messageContext: HandlerInboundMessage) { const proofRecord = await this.proofService.processPresentation(messageContext) - if (this.proofResponseCoordinator.shouldAutoRespondToPresentation(messageContext.agentContext, proofRecord)) { + const shouldAutoRespond = await this.proofResponseCoordinator.shouldAutoRespondToPresentation( + messageContext.agentContext, + proofRecord + ) + + if (shouldAutoRespond) { return await this.createAck(proofRecord, messageContext) } } diff --git a/packages/core/src/modules/proofs/protocol/v2/handlers/V2ProposePresentationHandler.ts b/packages/core/src/modules/proofs/protocol/v2/handlers/V2ProposePresentationHandler.ts index e822ed7a32..bbbf3f2b38 100644 --- a/packages/core/src/modules/proofs/protocol/v2/handlers/V2ProposePresentationHandler.ts +++ b/packages/core/src/modules/proofs/protocol/v2/handlers/V2ProposePresentationHandler.ts @@ -37,7 +37,12 @@ export class V2ProposePresentationHandler) { const proofRecord = await this.proofService.processProposal(messageContext) - if (this.proofResponseCoordinator.shouldAutoRespondToProposal(messageContext.agentContext, proofRecord)) { + const shouldAutoRespond = await this.proofResponseCoordinator.shouldAutoRespondToProposal( + messageContext.agentContext, + proofRecord + ) + + if (shouldAutoRespond) { return this.createRequest(proofRecord, messageContext) } } diff --git a/packages/core/src/modules/proofs/protocol/v2/handlers/V2RequestPresentationHandler.ts b/packages/core/src/modules/proofs/protocol/v2/handlers/V2RequestPresentationHandler.ts index 5c6bc970e7..fb8c7767d4 100644 --- a/packages/core/src/modules/proofs/protocol/v2/handlers/V2RequestPresentationHandler.ts +++ b/packages/core/src/modules/proofs/protocol/v2/handlers/V2RequestPresentationHandler.ts @@ -43,7 +43,13 @@ export class V2RequestPresentationHandler) { const proofRecord = await this.proofService.processRequest(messageContext) - if (this.proofResponseCoordinator.shouldAutoRespondToRequest(messageContext.agentContext, proofRecord)) { + + const shouldAutoRespond = await this.proofResponseCoordinator.shouldAutoRespondToRequest( + messageContext.agentContext, + proofRecord + ) + + if (shouldAutoRespond) { return await this.createPresentation(proofRecord, messageContext) } } diff --git a/packages/core/src/storage/migration/__tests__/backup.test.ts b/packages/core/src/storage/migration/__tests__/backup.test.ts index 5bcf588afb..b1efe9b580 100644 --- a/packages/core/src/storage/migration/__tests__/backup.test.ts +++ b/packages/core/src/storage/migration/__tests__/backup.test.ts @@ -35,10 +35,12 @@ describe('UpdateAssistant | Backup', () => { backupPath = `${fileSystem.basePath}/afj/migration/backup/${backupIdentifier}` // If tests fail it's possible the cleanup has been skipped. So remove before running tests - if (await fileSystem.exists(backupPath)) { + const doesFileSystemExist = await fileSystem.exists(backupPath) + if (doesFileSystemExist) { unlinkSync(backupPath) } - if (await fileSystem.exists(`${backupPath}-error`)) { + const doesbackupFileSystemExist = await fileSystem.exists(`${backupPath}-error`) + if (doesbackupFileSystemExist) { unlinkSync(`${backupPath}-error`) } From d90c3b5fa1583279dae0ac2da6dcafb8b9a783fd Mon Sep 17 00:00:00 2001 From: blu3beri Date: Thu, 24 Nov 2022 15:22:25 +0100 Subject: [PATCH 3/9] chore: CI rerun commit Signed-off-by: blu3beri From 8f91d0d34c5c20452ea87f38dbc6c7332ea03d96 Mon Sep 17 00:00:00 2001 From: blu3beri Date: Thu, 24 Nov 2022 18:27:42 +0100 Subject: [PATCH 4/9] fix: updated tests to reflect non-bug implementation Signed-off-by: blu3beri --- .../core/tests/v2-proofs-auto-accept.test.ts | 31 +++++++++---------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/packages/core/tests/v2-proofs-auto-accept.test.ts b/packages/core/tests/v2-proofs-auto-accept.test.ts index aa58d430d5..276fb5aeeb 100644 --- a/packages/core/tests/v2-proofs-auto-accept.test.ts +++ b/packages/core/tests/v2-proofs-auto-accept.test.ts @@ -141,7 +141,7 @@ describe('Auto accept present proof', () => { await aliceAgent.wallet.delete() }) - test('Alice starts with proof proposal to Faber, both with autoacceptproof on `contentApproved`', async () => { + test('Alice starts with proof proposal to Faber, both with autoAcceptProof on `contentApproved`', async () => { testLogger.test('Alice sends presentation proposal to Faber') let faberProofExchangeRecordPromise = waitForProofExchangeRecord(faberAgent, { @@ -162,11 +162,9 @@ describe('Auto accept present proof', () => { }, }) - testLogger.test('Faber waits for presentation proposal from Alice') - - await faberProofExchangeRecordPromise - testLogger.test('Faber accepts presentation proposal from Alice') + const { id: proofRecordId } = await faberProofExchangeRecordPromise + await faberAgent.proofs.acceptProposal({ proofRecordId }) faberProofExchangeRecordPromise = waitForProofExchangeRecord(faberAgent, { threadId: aliceProofExchangeRecord.threadId, @@ -185,7 +183,7 @@ describe('Auto accept present proof', () => { await aliceProofExchangeRecordPromise }) - test('Faber starts with proof requests to Alice, both with autoacceptproof on `contentApproved`', async () => { + test('Faber starts with proof requests to Alice, both with autoAcceptProof on `contentApproved`', async () => { testLogger.test('Faber sends presentation request to Alice') const attributes = { name: new ProofAttributeInfo({ @@ -210,14 +208,6 @@ describe('Auto accept present proof', () => { }), } - const faberProofExchangeRecordPromise = waitForProofExchangeRecord(faberAgent, { - state: ProofState.Done, - }) - - const aliceProofExchangeRecordPromise = waitForProofExchangeRecord(aliceAgent, { - state: ProofState.Done, - }) - await faberAgent.proofs.requestProof({ protocolVersion: 'v2', connectionId: faberConnection.id, @@ -232,11 +222,18 @@ describe('Auto accept present proof', () => { }, }) - testLogger.test('Faber waits for presentation from Alice') - await faberProofExchangeRecordPromise + testLogger.test('Alice waits for request from Faber') + const { id: proofRecordId } = await waitForProofExchangeRecord(aliceAgent, { + state: ProofState.RequestReceived, + }) + const { proofFormats } = await aliceAgent.proofs.autoSelectCredentialsForProofRequest({ proofRecordId }) + await aliceAgent.proofs.acceptRequest({ proofRecordId, proofFormats }) + + // Faber waits till it receives presentation ack + await waitForProofExchangeRecord(faberAgent, { state: ProofState.Done }) // Alice waits till it receives presentation ack - await aliceProofExchangeRecordPromise + await waitForProofExchangeRecord(aliceAgent, { state: ProofState.Done }) }) }) }) From 9187e595022ee0d5b28982ea10f6701892383be0 Mon Sep 17 00:00:00 2001 From: blu3beri Date: Fri, 25 Nov 2022 11:30:46 +0100 Subject: [PATCH 5/9] fix: fixed more proof tests Signed-off-by: blu3beri --- .../core/tests/v1-proofs-auto-accept.test.ts | 48 +++++++++---------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/packages/core/tests/v1-proofs-auto-accept.test.ts b/packages/core/tests/v1-proofs-auto-accept.test.ts index fbca03df04..b2131743da 100644 --- a/packages/core/tests/v1-proofs-auto-accept.test.ts +++ b/packages/core/tests/v1-proofs-auto-accept.test.ts @@ -146,10 +146,6 @@ describe('Auto accept present proof', () => { test('Alice starts with proof proposal to Faber, both with autoacceptproof on `contentApproved`', async () => { testLogger.test('Alice sends presentation proposal to Faber') - let faberProofExchangeRecordPromise = waitForProofExchangeRecord(faberAgent, { - state: ProofState.ProposalReceived, - }) - const aliceProofExchangeRecord = await aliceAgent.proofs.proposeProof({ connectionId: aliceConnection.id, protocolVersion: 'v1', @@ -165,26 +161,23 @@ describe('Auto accept present proof', () => { }) testLogger.test('Faber waits for presentation proposal from Alice') - - await faberProofExchangeRecordPromise + const faberProofExchangeRecord = await waitForProofExchangeRecord(faberAgent, { + threadId: aliceProofExchangeRecord.threadId, + state: ProofState.ProposalReceived, + }) testLogger.test('Faber accepts presentation proposal from Alice') + await faberAgent.proofs.acceptProposal({ proofRecordId: faberProofExchangeRecord.id }) - faberProofExchangeRecordPromise = waitForProofExchangeRecord(faberAgent, { + await waitForProofExchangeRecord(aliceAgent, { threadId: aliceProofExchangeRecord.threadId, state: ProofState.Done, }) - const aliceProofExchangeRecordPromise = waitForProofExchangeRecord(aliceAgent, { - threadId: aliceProofExchangeRecord.threadId, + await waitForProofExchangeRecord(faberAgent, { + threadId: faberProofExchangeRecord.threadId, state: ProofState.Done, }) - - testLogger.test('Faber waits for presentation from Alice') - - await faberProofExchangeRecordPromise - // Alice waits till it receives presentation ack - await aliceProofExchangeRecordPromise }) test('Faber starts with proof requests to Alice, both with autoacceptproof on `contentApproved`', async () => { @@ -212,14 +205,6 @@ describe('Auto accept present proof', () => { }), } - const faberProofExchangeRecordPromise = waitForProofExchangeRecord(faberAgent, { - state: ProofState.Done, - }) - - const aliceProofExchangeRecordPromise = waitForProofExchangeRecord(aliceAgent, { - state: ProofState.Done, - }) - await faberAgent.proofs.requestProof({ protocolVersion: 'v1', connectionId: faberConnection.id, @@ -234,11 +219,22 @@ describe('Auto accept present proof', () => { }, }) - testLogger.test('Faber waits for presentation from Alice') - await faberProofExchangeRecordPromise + testLogger.test('Alice waits for request from Faber') + const { id: proofRecordId } = await waitForProofExchangeRecord(aliceAgent, { + state: ProofState.RequestReceived, + }) + + const { proofFormats } = await aliceAgent.proofs.autoSelectCredentialsForProofRequest({ proofRecordId }) + await aliceAgent.proofs.acceptRequest({ proofRecordId, proofFormats }) // Alice waits till it receives presentation ack - await aliceProofExchangeRecordPromise + await waitForProofExchangeRecord(aliceAgent, { + state: ProofState.Done, + }) + // Faber waits till it receives presentation ack + await waitForProofExchangeRecord(faberAgent, { + state: ProofState.Done, + }) }) }) }) From 4b0e8200bb034895f14183ae2eca895fcf4b3670 Mon Sep 17 00:00:00 2001 From: blu3beri Date: Thu, 1 Dec 2022 09:45:59 +0100 Subject: [PATCH 6/9] fix(tests): increase auto-accept timeout Signed-off-by: blu3beri --- packages/core/tests/v1-proofs-auto-accept.test.ts | 2 ++ packages/core/tests/v2-proofs-auto-accept.test.ts | 2 ++ 2 files changed, 4 insertions(+) diff --git a/packages/core/tests/v1-proofs-auto-accept.test.ts b/packages/core/tests/v1-proofs-auto-accept.test.ts index b2131743da..6542979fa3 100644 --- a/packages/core/tests/v1-proofs-auto-accept.test.ts +++ b/packages/core/tests/v1-proofs-auto-accept.test.ts @@ -13,6 +13,8 @@ import { import { setupProofsTest, waitForProofExchangeRecord } from './helpers' import testLogger from './logger' +jest.setTimeout(120000) + describe('Auto accept present proof', () => { let faberAgent: Agent let aliceAgent: Agent diff --git a/packages/core/tests/v2-proofs-auto-accept.test.ts b/packages/core/tests/v2-proofs-auto-accept.test.ts index 276fb5aeeb..ccdf49473e 100644 --- a/packages/core/tests/v2-proofs-auto-accept.test.ts +++ b/packages/core/tests/v2-proofs-auto-accept.test.ts @@ -13,6 +13,8 @@ import { import { setupProofsTest, waitForProofExchangeRecord } from './helpers' import testLogger from './logger' +jest.setTimeout(120000) + describe('Auto accept present proof', () => { let faberAgent: Agent let aliceAgent: Agent From ef33b742161c94f3ac3e7dd2ccf4c49a69db21b7 Mon Sep 17 00:00:00 2001 From: blu3beri Date: Thu, 1 Dec 2022 14:34:39 +0100 Subject: [PATCH 7/9] fix: deep equals better check Signed-off-by: blu3beri --- .../jsonld/JsonLdCredentialFormatService.ts | 6 +- .../formats/indy/IndyProofFormatService.ts | 7 +- .../proofs/protocol/v2/V2ProofService.ts | 21 ++--- .../src/utils/__tests__/deepEquality.test.ts | 82 +++++++++++++++++++ .../src/utils/__tests__/objectCheck.test.ts | 34 -------- packages/core/src/utils/deepEquality.ts | 55 +++++++++++++ packages/core/src/utils/index.ts | 2 + packages/core/src/utils/objEqual.ts | 23 ------ packages/core/src/utils/objectCheck.ts | 70 ---------------- packages/core/src/utils/objectEquality.ts | 19 +++++ .../core/tests/v1-proofs-auto-accept.test.ts | 59 ++++--------- .../core/tests/v2-proofs-auto-accept.test.ts | 74 ++++++----------- 12 files changed, 214 insertions(+), 238 deletions(-) create mode 100644 packages/core/src/utils/__tests__/deepEquality.test.ts delete mode 100644 packages/core/src/utils/__tests__/objectCheck.test.ts create mode 100644 packages/core/src/utils/deepEquality.ts delete mode 100644 packages/core/src/utils/objEqual.ts delete mode 100644 packages/core/src/utils/objectCheck.ts create mode 100644 packages/core/src/utils/objectEquality.ts diff --git a/packages/core/src/modules/credentials/formats/jsonld/JsonLdCredentialFormatService.ts b/packages/core/src/modules/credentials/formats/jsonld/JsonLdCredentialFormatService.ts index b6c0d6c6c5..e8fdd4f329 100644 --- a/packages/core/src/modules/credentials/formats/jsonld/JsonLdCredentialFormatService.ts +++ b/packages/core/src/modules/credentials/formats/jsonld/JsonLdCredentialFormatService.ts @@ -24,9 +24,9 @@ import type { JsonLdOptionsRFC0593 } from './JsonLdOptionsRFC0593' import { injectable } from 'tsyringe' import { AriesFrameworkError } from '../../../../error' +import { objectEquality } from '../../../../utils' import { JsonTransformer } from '../../../../utils/JsonTransformer' import { MessageValidator } from '../../../../utils/MessageValidator' -import { deepEqual } from '../../../../utils/objEqual' import { findVerificationMethodByKeyType } from '../../../dids/domain/DidDocument' import { DidResolverService } from '../../../dids/services/DidResolverService' import { W3cCredentialService } from '../../../vc' @@ -344,7 +344,7 @@ export class JsonLdCredentialFormatService extends CredentialFormatService extends P ) } - const msg = new V2PresentationAckMessage({ + const message = new V2PresentationAckMessage({ threadId: options.proofRecord.threadId, status: AckStatus.OK, }) @@ -553,7 +553,7 @@ export class V2ProofService extends P await this.updateState(agentContext, options.proofRecord, ProofState.Done) return { - message: msg, + message, proofRecord: options.proofRecord, } } @@ -681,17 +681,16 @@ export class V2ProofService extends P messageClass: V2ProposalPresentationMessage, }) - if (!proposal) { - return false - } + if (!proposal) return false + const request = await this.didCommMessageRepository.findAgentMessage(agentContext, { associatedRecordId: proofRecord.id, messageClass: V2RequestPresentationMessage, }) - if (!request) { - return true - } - await MessageValidator.validateSync(proposal) + + if (!request) return false + + MessageValidator.validateSync(proposal) const proposalAttachments = proposal.getAttachmentFormats() const requestAttachments = request.getAttachmentFormats() @@ -737,7 +736,9 @@ export class V2ProofService extends P equalityResults.push(service?.proposalAndRequestAreEqual(proposalAttachments, requestAttachments)) } - return equalityResults.every((x) => x === true) + const foo = equalityResults.every((x) => x === true) + + return foo } public async shouldAutoRespondToPresentation( diff --git a/packages/core/src/utils/__tests__/deepEquality.test.ts b/packages/core/src/utils/__tests__/deepEquality.test.ts new file mode 100644 index 0000000000..645a35e329 --- /dev/null +++ b/packages/core/src/utils/__tests__/deepEquality.test.ts @@ -0,0 +1,82 @@ +import { Metadata } from '../../storage/Metadata' +import { deepEquality } from '../deepEquality' + +describe('deepEquality', () => { + test('evaluates to true with equal maps', () => { + const a = new Map([ + ['foo', 1], + ['bar', 2], + ['baz', 3], + ]) + const b = new Map([ + ['foo', 1], + ['bar', 2], + ['baz', 3], + ]) + expect(deepEquality(a, b)).toBe(true) + }) + test('evaluates to false with unequal maps', () => { + const c = new Map([ + ['foo', 1], + ['baz', 3], + ['bar', 2], + ]) + + const d = new Map([ + ['foo', 1], + ['bar', 2], + ['qux', 3], + ]) + expect(deepEquality(c, d)).toBe(false) + }) + + test('evaluates to true with equal maps with different order', () => { + const a = new Map([ + ['baz', 3], + ['bar', 2], + ['foo', 1], + ]) + const b = new Map([ + ['foo', 1], + ['bar', 2], + ['baz', 3], + ]) + expect(deepEquality(a, b)).toBe(true) + }) + test('evaluates to true with equal primitives', () => { + expect(deepEquality(1, 1)).toBe(true) + expect(deepEquality(true, true)).toBe(true) + expect(deepEquality('a', 'a')).toBe(true) + }) + + test('evaluates to false with unequal primitives', () => { + expect(deepEquality(1, 2)).toBe(false) + expect(deepEquality(true, false)).toBe(false) + expect(deepEquality('a', 'b')).toBe(false) + }) + + test('evaluates to true with equal complex types', () => { + const fn = () => 'hello World!' + expect(deepEquality(fn, fn)).toBe(true) + expect(deepEquality({}, {})).toBe(true) + expect(deepEquality({ foo: 'bar' }, { foo: 'bar' })).toBe(true) + expect(deepEquality({ foo: 'baz', bar: 'bar' }, { bar: 'bar', foo: 'baz' })).toBe(true) + expect(deepEquality(Metadata, Metadata)).toBe(true) + expect(deepEquality(new Metadata({}), new Metadata({}))).toBe(true) + }) + + test('evaluates to false with unequal complex types', () => { + const fn = () => 'hello World!' + const fnTwo = () => 'Goodbye World!' + class Bar { + public constructor() { + 'yes' + } + } + expect(deepEquality(fn, fnTwo)).toBe(false) + expect(deepEquality({ bar: 'foo' }, { a: 'b' })).toBe(false) + expect(deepEquality({ b: 'a' }, { b: 'a', c: 'd' })).toBe(false) + expect(deepEquality(Metadata, Bar)).toBe(false) + expect(deepEquality(new Metadata({}), new Bar())).toBe(false) + }) +}) diff --git a/packages/core/src/utils/__tests__/objectCheck.test.ts b/packages/core/src/utils/__tests__/objectCheck.test.ts deleted file mode 100644 index d2517c2d0e..0000000000 --- a/packages/core/src/utils/__tests__/objectCheck.test.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { objectEquals } from '../objectCheck' - -describe('objectEquals', () => { - it('correctly evaluates whether two map objects are equal', () => { - const mapA: Map = new Map([ - ['foo', 1], - ['bar', 2], - ['baz', 3], - ]) - const mapB: Map = new Map([ - ['foo', 1], - ['bar', 2], - ['baz', 3], - ]) - let retVal = objectEquals(mapA, mapB) - - // if the order is different, maps still equal - const mapC: Map = new Map([ - ['foo', 1], - ['baz', 3], - ['bar', 2], - ]) - retVal = objectEquals(mapA, mapC) - expect(retVal).toBe(true) - - const mapD: Map = new Map([ - ['foo', 1], - ['bar', 2], - ['qux', 3], - ]) - retVal = objectEquals(mapA, mapD) - expect(retVal).toBe(false) - }) -}) diff --git a/packages/core/src/utils/deepEquality.ts b/packages/core/src/utils/deepEquality.ts new file mode 100644 index 0000000000..0ebfae7971 --- /dev/null +++ b/packages/core/src/utils/deepEquality.ts @@ -0,0 +1,55 @@ +import { objectEquality } from './objectEquality' + +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export function deepEquality(x: any, y: any): boolean { + // We do a simple equals here to check primitives, functions, regex, etc. + // This will only happen if the typing of the function is ignored + const isXSimpleEqualY = simpleEqual(x, y) + if (isXSimpleEqualY !== undefined) return isXSimpleEqualY + + if (!(x instanceof Map) || !(y instanceof Map)) return objectEquality(x, y) + + const xMap = x as Map + const yMap = y as Map + + // At this point we are sure we have two instances of a Map + const xKeys = Array.from(xMap.keys()) + const yKeys = Array.from(yMap.keys()) + + // Keys from both maps are not equal, content has not been verified, yet + if (!equalsIgnoreOrder(xKeys, yKeys)) return false + + // Here we recursively check whether the value of xMap is equals to the value of yMap + return Array.from(xMap.entries()).every(([key, xVal]) => deepEquality(xVal, yMap.get(key))) +} + +/** + * @note This will only work for primitive array equality + */ +function equalsIgnoreOrder(a: Array, b: Array): boolean { + if (a.length !== b.length) return false + return a.every((k) => b.includes(k)) +} + +// We take any here as we have to check some properties, they will be undefined if they do not exist +// eslint-disable-next-line @typescript-eslint/no-explicit-any +function simpleEqual(x: any, y: any) { + // short circuit for easy equality + if (x === y) return true + + if ((x === null || x === undefined) && (y === null || y === undefined)) return x === y + + // after this just checking type of one would be enough + if (x.constructor !== y.constructor) return false + + // if they are functions, they should exactly refer to same one (because of closures) + if (x instanceof Function) return x === y + + // if they are regexps, they should exactly refer to same one (it is hard to better equality check on current ES) + if (x instanceof RegExp) return x === y + + if (x.valueOf && y.valueOf && x.valueOf() === y.valueOf()) return true + + // if they are dates, they must had equal valueOf + if (x instanceof Date || y instanceof Date) return false +} diff --git a/packages/core/src/utils/index.ts b/packages/core/src/utils/index.ts index cb4f5d92e7..c0dfd135df 100644 --- a/packages/core/src/utils/index.ts +++ b/packages/core/src/utils/index.ts @@ -12,3 +12,5 @@ export * from './Hasher' export * from './validators' export * from './type' export * from './indyIdentifiers' +export * from './deepEquality' +export * from './objectEquality' diff --git a/packages/core/src/utils/objEqual.ts b/packages/core/src/utils/objEqual.ts deleted file mode 100644 index 2909b5c450..0000000000 --- a/packages/core/src/utils/objEqual.ts +++ /dev/null @@ -1,23 +0,0 @@ -export function deepEqual(a: any, b: any): boolean { - if (typeof a == 'object' && a != null && typeof b == 'object' && b != null) { - const count = [0, 0] - for (const key in a) count[0]++ - for (const key in b) count[1]++ - if (count[0] - count[1] != 0) { - return false - } - for (const key in a) { - if (!(key in b) || !deepEqual(a[key], b[key])) { - return false - } - } - for (const key in b) { - if (!(key in a) || !deepEqual(b[key], a[key])) { - return false - } - } - return true - } else { - return a === b - } -} diff --git a/packages/core/src/utils/objectCheck.ts b/packages/core/src/utils/objectCheck.ts deleted file mode 100644 index a511b28df6..0000000000 --- a/packages/core/src/utils/objectCheck.ts +++ /dev/null @@ -1,70 +0,0 @@ -export function objectEquals(x: Map, y: Map): boolean { - if (x === null || x === undefined || y === null || y === undefined) { - return x === y - } - - // after this just checking type of one would be enough - if (x.constructor !== y.constructor) { - return false - } - // if they are functions, they should exactly refer to same one (because of closures) - if (x instanceof Function) { - return x === y - } - // if they are regexps, they should exactly refer to same one (it is hard to better equality check on current ES) - if (x instanceof RegExp) { - return x === y - } - if (x === y || x.valueOf() === y.valueOf()) { - return true - } - - // if they are dates, they must had equal valueOf - if (x instanceof Date) { - return false - } - - // if they are strictly equal, they both need to be object at least - if (!(x instanceof Object)) { - return false - } - if (!(y instanceof Object)) { - return false - } - - const xkeys = Array.from(x.keys()) - const ykeys = Array.from(y.keys()) - if (!equalsIgnoreOrder(xkeys, ykeys)) { - return false - } - return ( - xkeys.every(function (i) { - return xkeys.indexOf(i) !== -1 - }) && - xkeys.every(function (xkey) { - // get the x map entries for this key - - const xval: any = x.get(xkey) - const yval: any = y.get(xkey) - - const a: Map = new Map([[xkey, xval]]) - if (a.size === 1) { - return xval === yval - } - // get the y map entries for this key - const b: Map = new Map([[xkey, yval]]) - return objectEquals(a, b) - }) - ) -} - -function equalsIgnoreOrder(a: string[], b: string[]): boolean { - if (a.length !== b.length) return false - const uniqueValues = new Set([...a, ...b]) - for (const v of uniqueValues) { - const aCount = a.filter((e) => e === v).length - const bCount = b.filter((e) => e === v).length - if (aCount !== bCount) return false - } - return true -} diff --git a/packages/core/src/utils/objectEquality.ts b/packages/core/src/utils/objectEquality.ts new file mode 100644 index 0000000000..9d0a18e0b3 --- /dev/null +++ b/packages/core/src/utils/objectEquality.ts @@ -0,0 +1,19 @@ +// eslint-disable-next-line @typescript-eslint/no-explicit-any +export function objectEquality(a: any, b: any): boolean { + if (typeof a == 'object' && a != null && typeof b == 'object' && b != null) { + if (Object.keys(a).length !== Object.keys(b).length) return false + for (const key in a) { + if (!(key in b) || !objectEquality(a[key], b[key])) { + return false + } + } + for (const key in b) { + if (!(key in a) || !objectEquality(b[key], a[key])) { + return false + } + } + return true + } else { + return a === b + } +} diff --git a/packages/core/tests/v1-proofs-auto-accept.test.ts b/packages/core/tests/v1-proofs-auto-accept.test.ts index 6542979fa3..6c222e70e9 100644 --- a/packages/core/tests/v1-proofs-auto-accept.test.ts +++ b/packages/core/tests/v1-proofs-auto-accept.test.ts @@ -13,8 +13,6 @@ import { import { setupProofsTest, waitForProofExchangeRecord } from './helpers' import testLogger from './logger' -jest.setTimeout(120000) - describe('Auto accept present proof', () => { let faberAgent: Agent let aliceAgent: Agent @@ -42,14 +40,6 @@ describe('Auto accept present proof', () => { test('Alice starts with proof proposal to Faber, both with autoAcceptProof on `always`', async () => { testLogger.test('Alice sends presentation proposal to Faber') - const faberProofExchangeRecordPromise = waitForProofExchangeRecord(faberAgent, { - state: ProofState.Done, - }) - - const aliceProofExchangeRecordPromise = waitForProofExchangeRecord(aliceAgent, { - state: ProofState.Done, - }) - await aliceAgent.proofs.proposeProof({ connectionId: aliceConnection.id, protocolVersion: 'v1', @@ -65,10 +55,11 @@ describe('Auto accept present proof', () => { }) testLogger.test('Faber waits for presentation from Alice') - await faberProofExchangeRecordPromise - testLogger.test('Alice waits till it receives presentation ack') - await aliceProofExchangeRecordPromise + await Promise.all([ + waitForProofExchangeRecord(faberAgent, { state: ProofState.Done }), + waitForProofExchangeRecord(aliceAgent, { state: ProofState.Done }), + ]) }) test('Faber starts with proof requests to Alice, both with autoAcceptProof on `always`', async () => { @@ -96,14 +87,6 @@ describe('Auto accept present proof', () => { }), } - const faberProofExchangeRecordPromise = waitForProofExchangeRecord(faberAgent, { - state: ProofState.Done, - }) - - const aliceProofExchangeRecordPromise = waitForProofExchangeRecord(aliceAgent, { - state: ProofState.Done, - }) - await faberAgent.proofs.requestProof({ protocolVersion: 'v1', connectionId: faberConnection.id, @@ -119,11 +102,10 @@ describe('Auto accept present proof', () => { }) testLogger.test('Faber waits for presentation from Alice') - - await faberProofExchangeRecordPromise - - // Alice waits till it receives presentation ack - await aliceProofExchangeRecordPromise + await Promise.all([ + waitForProofExchangeRecord(faberAgent, { state: ProofState.Done }), + waitForProofExchangeRecord(aliceAgent, { state: ProofState.Done }), + ]) }) }) @@ -171,15 +153,10 @@ describe('Auto accept present proof', () => { testLogger.test('Faber accepts presentation proposal from Alice') await faberAgent.proofs.acceptProposal({ proofRecordId: faberProofExchangeRecord.id }) - await waitForProofExchangeRecord(aliceAgent, { - threadId: aliceProofExchangeRecord.threadId, - state: ProofState.Done, - }) - - await waitForProofExchangeRecord(faberAgent, { - threadId: faberProofExchangeRecord.threadId, - state: ProofState.Done, - }) + await Promise.all([ + waitForProofExchangeRecord(aliceAgent, { state: ProofState.Done }), + waitForProofExchangeRecord(faberAgent, { state: ProofState.Done }), + ]) }) test('Faber starts with proof requests to Alice, both with autoacceptproof on `contentApproved`', async () => { @@ -229,14 +206,10 @@ describe('Auto accept present proof', () => { const { proofFormats } = await aliceAgent.proofs.autoSelectCredentialsForProofRequest({ proofRecordId }) await aliceAgent.proofs.acceptRequest({ proofRecordId, proofFormats }) - // Alice waits till it receives presentation ack - await waitForProofExchangeRecord(aliceAgent, { - state: ProofState.Done, - }) - // Faber waits till it receives presentation ack - await waitForProofExchangeRecord(faberAgent, { - state: ProofState.Done, - }) + await Promise.all([ + waitForProofExchangeRecord(aliceAgent, { state: ProofState.Done }), + waitForProofExchangeRecord(faberAgent, { state: ProofState.Done }), + ]) }) }) }) diff --git a/packages/core/tests/v2-proofs-auto-accept.test.ts b/packages/core/tests/v2-proofs-auto-accept.test.ts index ccdf49473e..0ab3d81e13 100644 --- a/packages/core/tests/v2-proofs-auto-accept.test.ts +++ b/packages/core/tests/v2-proofs-auto-accept.test.ts @@ -13,8 +13,6 @@ import { import { setupProofsTest, waitForProofExchangeRecord } from './helpers' import testLogger from './logger' -jest.setTimeout(120000) - describe('Auto accept present proof', () => { let faberAgent: Agent let aliceAgent: Agent @@ -42,14 +40,6 @@ describe('Auto accept present proof', () => { test('Alice starts with proof proposal to Faber, both with autoAcceptProof on `always`', async () => { testLogger.test('Alice sends presentation proposal to Faber') - const faberProofExchangeRecordPromise = waitForProofExchangeRecord(faberAgent, { - state: ProofState.Done, - }) - - const aliceProofExchangeRecordPromise = waitForProofExchangeRecord(aliceAgent, { - state: ProofState.Done, - }) - await aliceAgent.proofs.proposeProof({ connectionId: aliceConnection.id, protocolVersion: 'v2', @@ -65,10 +55,11 @@ describe('Auto accept present proof', () => { }) testLogger.test('Faber waits for presentation from Alice') - await faberProofExchangeRecordPromise - testLogger.test('Alice waits till it receives presentation ack') - await aliceProofExchangeRecordPromise + await Promise.all([ + waitForProofExchangeRecord(faberAgent, { state: ProofState.Done }), + waitForProofExchangeRecord(aliceAgent, { state: ProofState.Done }), + ]) }) test('Faber starts with proof requests to Alice, both with autoAcceptProof on `always`', async () => { @@ -96,14 +87,6 @@ describe('Auto accept present proof', () => { }), } - const faberProofExchangeRecordPromise = waitForProofExchangeRecord(faberAgent, { - state: ProofState.Done, - }) - - const aliceProofExchangeRecordPromise = waitForProofExchangeRecord(aliceAgent, { - state: ProofState.Done, - }) - await faberAgent.proofs.requestProof({ protocolVersion: 'v2', connectionId: faberConnection.id, @@ -118,10 +101,12 @@ describe('Auto accept present proof', () => { }, }) - testLogger.test('Faber waits for presentation from Alice') - await faberProofExchangeRecordPromise - // Alice waits till it receives presentation ack - await aliceProofExchangeRecordPromise + testLogger.test('Alice waits for presentation from Faber') + testLogger.test('Faber waits till it receives presentation ack') + await Promise.all([ + waitForProofExchangeRecord(faberAgent, { state: ProofState.Done }), + waitForProofExchangeRecord(aliceAgent, { state: ProofState.Done }), + ]) }) }) @@ -146,11 +131,7 @@ describe('Auto accept present proof', () => { test('Alice starts with proof proposal to Faber, both with autoAcceptProof on `contentApproved`', async () => { testLogger.test('Alice sends presentation proposal to Faber') - let faberProofExchangeRecordPromise = waitForProofExchangeRecord(faberAgent, { - state: ProofState.ProposalReceived, - }) - - const aliceProofExchangeRecord = await aliceAgent.proofs.proposeProof({ + await aliceAgent.proofs.proposeProof({ connectionId: aliceConnection.id, protocolVersion: 'v2', proofFormats: { @@ -164,25 +145,17 @@ describe('Auto accept present proof', () => { }, }) - testLogger.test('Faber accepts presentation proposal from Alice') - const { id: proofRecordId } = await faberProofExchangeRecordPromise - await faberAgent.proofs.acceptProposal({ proofRecordId }) - - faberProofExchangeRecordPromise = waitForProofExchangeRecord(faberAgent, { - threadId: aliceProofExchangeRecord.threadId, - state: ProofState.Done, - }) - - const aliceProofExchangeRecordPromise = waitForProofExchangeRecord(aliceAgent, { - threadId: aliceProofExchangeRecord.threadId, - state: ProofState.Done, + const { id: proofRecordId } = await waitForProofExchangeRecord(faberAgent, { + state: ProofState.ProposalReceived, }) - testLogger.test('Faber waits for presentation from Alice') + testLogger.test('Faber accepts presentation proposal from Alice') + await faberAgent.proofs.acceptProposal({ proofRecordId }) - await faberProofExchangeRecordPromise - // Alice waits till it receives presentation ack - await aliceProofExchangeRecordPromise + await Promise.all([ + waitForProofExchangeRecord(aliceAgent, { state: ProofState.Done }), + waitForProofExchangeRecord(faberAgent, { state: ProofState.Done }), + ]) }) test('Faber starts with proof requests to Alice, both with autoAcceptProof on `contentApproved`', async () => { @@ -231,11 +204,10 @@ describe('Auto accept present proof', () => { const { proofFormats } = await aliceAgent.proofs.autoSelectCredentialsForProofRequest({ proofRecordId }) await aliceAgent.proofs.acceptRequest({ proofRecordId, proofFormats }) - // Faber waits till it receives presentation ack - await waitForProofExchangeRecord(faberAgent, { state: ProofState.Done }) - - // Alice waits till it receives presentation ack - await waitForProofExchangeRecord(aliceAgent, { state: ProofState.Done }) + await Promise.all([ + waitForProofExchangeRecord(faberAgent, { state: ProofState.Done }), + waitForProofExchangeRecord(aliceAgent, { state: ProofState.Done }), + ]) }) }) }) From 42b75957afd22f7e1a21443be5ed6075ef2cdfdd Mon Sep 17 00:00:00 2001 From: blu3beri Date: Thu, 1 Dec 2022 14:47:15 +0100 Subject: [PATCH 8/9] chore: remove 'foo' variable Signed-off-by: blu3beri --- .../core/src/modules/proofs/protocol/v2/V2ProofService.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/core/src/modules/proofs/protocol/v2/V2ProofService.ts b/packages/core/src/modules/proofs/protocol/v2/V2ProofService.ts index 400675e328..7ef492d4bb 100644 --- a/packages/core/src/modules/proofs/protocol/v2/V2ProofService.ts +++ b/packages/core/src/modules/proofs/protocol/v2/V2ProofService.ts @@ -736,9 +736,7 @@ export class V2ProofService extends P equalityResults.push(service?.proposalAndRequestAreEqual(proposalAttachments, requestAttachments)) } - const foo = equalityResults.every((x) => x === true) - - return foo + return equalityResults.every((x) => x === true) } public async shouldAutoRespondToPresentation( From fe412659ce9020cd16e1dd32ed3a71104a1573ff Mon Sep 17 00:00:00 2001 From: blu3beri Date: Thu, 1 Dec 2022 14:51:24 +0100 Subject: [PATCH 9/9] removed some missused awaits Signed-off-by: blu3beri --- .../proofs/formats/indy/IndyProofFormatService.ts | 8 ++++---- .../src/modules/proofs/protocol/v1/V1ProofService.ts | 10 +++------- packages/core/src/utils/MessageValidator.ts | 6 +++--- packages/core/src/utils/__tests__/shortenedUrl.test.ts | 4 ++-- packages/core/src/utils/parseInvitation.ts | 4 ++-- 5 files changed, 14 insertions(+), 18 deletions(-) diff --git a/packages/core/src/modules/proofs/formats/indy/IndyProofFormatService.ts b/packages/core/src/modules/proofs/formats/indy/IndyProofFormatService.ts index d93fc7a27d..1aec763e65 100644 --- a/packages/core/src/modules/proofs/formats/indy/IndyProofFormatService.ts +++ b/packages/core/src/modules/proofs/formats/indy/IndyProofFormatService.ts @@ -120,7 +120,7 @@ export class IndyProofFormatService extends ProofFormatService { }) const request = new ProofRequest(options.proofProposalOptions) - await MessageValidator.validateSync(request) + MessageValidator.validateSync(request) const attachment = new Attachment({ id: options.id, @@ -156,7 +156,7 @@ export class IndyProofFormatService extends ProofFormatService { const proposalMessage = JsonTransformer.fromJSON(proofProposalJson, ProofRequest) - await MessageValidator.validateSync(proposalMessage) + MessageValidator.validateSync(proposalMessage) } public async createRequestAsResponse( @@ -212,7 +212,7 @@ export class IndyProofFormatService extends ProofFormatService { `Missing required base64 or json encoded attachment data for presentation request with thread id ${options.record?.threadId}` ) } - await MessageValidator.validateSync(proofRequest) + MessageValidator.validateSync(proofRequest) // Assert attribute and predicate (group) names do not match checkProofRequestForDuplicates(proofRequest) @@ -605,7 +605,7 @@ export class IndyProofFormatService extends ProofFormatService { if (!proofRequest) { throw new AriesFrameworkError(`Missing required base64 or json encoded attachment data for presentation request.`) } - await MessageValidator.validateSync(proofRequest) + MessageValidator.validateSync(proofRequest) // Assert attribute and predicate (group) names do not match checkProofRequestForDuplicates(proofRequest) diff --git a/packages/core/src/modules/proofs/protocol/v1/V1ProofService.ts b/packages/core/src/modules/proofs/protocol/v1/V1ProofService.ts index dc572f431b..97f49bb572 100644 --- a/packages/core/src/modules/proofs/protocol/v1/V1ProofService.ts +++ b/packages/core/src/modules/proofs/protocol/v1/V1ProofService.ts @@ -764,10 +764,8 @@ export class V1ProofService extends ProofService<[IndyProofFormat]> { messageClass: V1ProposePresentationMessage, }) - if (!proposal) { - return false - } - await MessageValidator.validateSync(proposal) + if (!proposal) return false + MessageValidator.validateSync(proposal) // check the proposal against a possible previous request const request = await this.didCommMessageRepository.findAgentMessage(agentContext, { @@ -775,9 +773,7 @@ export class V1ProofService extends ProofService<[IndyProofFormat]> { messageClass: V1RequestPresentationMessage, }) - if (!request) { - return false - } + if (!request) return false const proofRequest = request.indyProofRequest diff --git a/packages/core/src/utils/MessageValidator.ts b/packages/core/src/utils/MessageValidator.ts index 82e1afc408..386cb49377 100644 --- a/packages/core/src/utils/MessageValidator.ts +++ b/packages/core/src/utils/MessageValidator.ts @@ -7,13 +7,13 @@ export class MessageValidator { /** * * @param classInstance the class instance to validate - * @returns nothing + * @returns void * @throws array of validation errors {@link ValidationError} */ // eslint-disable-next-line @typescript-eslint/ban-types public static validateSync(classInstance: T & {}) { - // NOTE: validateSync (strangely) return an Array of errors so we - // have to transform that into an error of choice and throw that. + // NOTE: validateSync returns an Array of errors. + // We have to transform that into an error of choice and throw that. const errors = validateSync(classInstance) if (isValidationErrorArray(errors)) { throw new ClassValidationError('Failed to validate class.', { diff --git a/packages/core/src/utils/__tests__/shortenedUrl.test.ts b/packages/core/src/utils/__tests__/shortenedUrl.test.ts index 5e79621e96..55180a141d 100644 --- a/packages/core/src/utils/__tests__/shortenedUrl.test.ts +++ b/packages/core/src/utils/__tests__/shortenedUrl.test.ts @@ -81,9 +81,9 @@ let connectionInvitationToNew: OutOfBandInvitation beforeAll(async () => { outOfBandInvitationMock = await JsonTransformer.fromJSON(mockOobInvite, OutOfBandInvitation) - await MessageValidator.validateSync(outOfBandInvitationMock) + MessageValidator.validateSync(outOfBandInvitationMock) connectionInvitationMock = await JsonTransformer.fromJSON(mockConnectionInvite, ConnectionInvitationMessage) - await MessageValidator.validateSync(connectionInvitationMock) + MessageValidator.validateSync(connectionInvitationMock) connectionInvitationToNew = convertToNewInvitation(connectionInvitationMock) }) diff --git a/packages/core/src/utils/parseInvitation.ts b/packages/core/src/utils/parseInvitation.ts index 6f3c9e8f3b..6af07072e2 100644 --- a/packages/core/src/utils/parseInvitation.ts +++ b/packages/core/src/utils/parseInvitation.ts @@ -61,11 +61,11 @@ export const oobInvitationFromShortUrl = async (response: Response): Promise