From ca6c1ce82bb84a638f98977191b04a249633be76 Mon Sep 17 00:00:00 2001 From: Iskander508 Date: Tue, 14 Jun 2022 16:00:29 +0200 Subject: [PATCH] fix(proofs): allow duplicates in proof attributes (#848) Signed-off-by: Pavel Zarecky --- .../utils/__tests__/indyProofRequest.test.ts | 27 +++++++------------ packages/core/src/utils/assertNoDuplicates.ts | 11 -------- packages/core/src/utils/indyProofRequest.ts | 20 ++++++++++---- packages/core/tests/proofs.test.ts | 2 +- 4 files changed, 25 insertions(+), 35 deletions(-) delete mode 100644 packages/core/src/utils/assertNoDuplicates.ts diff --git a/packages/core/src/utils/__tests__/indyProofRequest.test.ts b/packages/core/src/utils/__tests__/indyProofRequest.test.ts index 5b08490d17..ef15b80f40 100644 --- a/packages/core/src/utils/__tests__/indyProofRequest.test.ts +++ b/packages/core/src/utils/__tests__/indyProofRequest.test.ts @@ -9,18 +9,13 @@ import { ProofRequest, } from '@aries-framework/core' -export const INDY_CREDENTIAL_OFFER_ATTACHMENT_ID = 'libindy-cred-offer-0' - describe('Present Proof', () => { - let credDefId: string - - beforeAll(() => { - credDefId = '9vPXgSpQJPkJEALbLXueBp:3:CL:57753:tag1' - }) + const credDefId = '9vPXgSpQJPkJEALbLXueBp:3:CL:57753:tag1' + const nonce = 'testtesttest12345' - test('attribute names match, same cred def filter', () => { + test('attribute names match', () => { const attributes = { - name: new ProofAttributeInfo({ + age1: new ProofAttributeInfo({ name: 'age', restrictions: [ new AttributeFilter({ @@ -28,7 +23,7 @@ describe('Present Proof', () => { }), ], }), - age: new ProofAttributeInfo({ + age2: new ProofAttributeInfo({ name: 'age', restrictions: [ new AttributeFilter({ @@ -38,8 +33,6 @@ describe('Present Proof', () => { }), } - const nonce = 'testtesttest12345' - const proofRequest = new ProofRequest({ name: 'proof-request', version: '1.0', @@ -47,12 +40,12 @@ describe('Present Proof', () => { requestedAttributes: attributes, }) - expect(() => checkProofRequestForDuplicates(proofRequest)).toThrowError(AriesFrameworkError) + expect(() => checkProofRequestForDuplicates(proofRequest)).not.toThrow() }) - test('attribute names match with predicates name, same cred def filter', () => { + test('attribute names match with predicates name', () => { const attributes = { - name: new ProofAttributeInfo({ + attrib: new ProofAttributeInfo({ name: 'age', restrictions: [ new AttributeFilter({ @@ -63,7 +56,7 @@ describe('Present Proof', () => { } const predicates = { - age: new ProofPredicateInfo({ + predicate: new ProofPredicateInfo({ name: 'age', predicateType: PredicateType.GreaterThanOrEqualTo, predicateValue: 50, @@ -75,8 +68,6 @@ describe('Present Proof', () => { }), } - const nonce = 'testtesttest12345' - const proofRequest = new ProofRequest({ name: 'proof-request', version: '1.0', diff --git a/packages/core/src/utils/assertNoDuplicates.ts b/packages/core/src/utils/assertNoDuplicates.ts deleted file mode 100644 index 841ba261f4..0000000000 --- a/packages/core/src/utils/assertNoDuplicates.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { AriesFrameworkError } from '../error/AriesFrameworkError' - -export function assertNoDuplicatesInArray(arr: string[]) { - const arrayLength = arr.length - const uniqueArrayLength = new Set(arr).size - - if (arrayLength === uniqueArrayLength) return - - const duplicates = arr.filter((item, index) => arr.indexOf(item) != index) - throw new AriesFrameworkError(`The proof request contains duplicate items: ${duplicates.toString()}`) -} diff --git a/packages/core/src/utils/indyProofRequest.ts b/packages/core/src/utils/indyProofRequest.ts index 7c24a4b05c..6f128b4849 100644 --- a/packages/core/src/utils/indyProofRequest.ts +++ b/packages/core/src/utils/indyProofRequest.ts @@ -1,8 +1,8 @@ import type { ProofRequest } from '../modules/proofs/models/ProofRequest' -import { assertNoDuplicatesInArray } from './assertNoDuplicates' +import { AriesFrameworkError } from '../error/AriesFrameworkError' -export function attributeNamesToArray(proofRequest: ProofRequest) { +function attributeNamesToArray(proofRequest: ProofRequest) { // Attributes can contain either a `name` string value or an `names` string array. We reduce it to a single array // containing all attribute names from the requested attributes. return Array.from(proofRequest.requestedAttributes.values()).reduce( @@ -11,12 +11,22 @@ export function attributeNamesToArray(proofRequest: ProofRequest) { ) } -export function predicateNamesToArray(proofRequest: ProofRequest) { - return Array.from(proofRequest.requestedPredicates.values()).map((a) => a.name) +function predicateNamesToArray(proofRequest: ProofRequest) { + return Array.from(new Set(Array.from(proofRequest.requestedPredicates.values()).map((a) => a.name))) } +function assertNoDuplicates(predicates: string[], attributeNames: string[]) { + const duplicates = predicates.filter((item) => attributeNames.indexOf(item) !== -1) + if (duplicates.length > 0) { + throw new AriesFrameworkError( + `The proof request contains duplicate predicates and attributes: ${duplicates.toString()}` + ) + } +} + +// TODO: This is still not ideal. The requested groups can specify different credentials using restrictions. export function checkProofRequestForDuplicates(proofRequest: ProofRequest) { const attributes = attributeNamesToArray(proofRequest) const predicates = predicateNamesToArray(proofRequest) - assertNoDuplicatesInArray(attributes.concat(predicates)) + assertNoDuplicates(predicates, attributes) } diff --git a/packages/core/tests/proofs.test.ts b/packages/core/tests/proofs.test.ts index 38c96c03ce..a09784167d 100644 --- a/packages/core/tests/proofs.test.ts +++ b/packages/core/tests/proofs.test.ts @@ -365,6 +365,6 @@ describe('Present Proof', () => { requestedAttributes: attributes, requestedPredicates: predicates, }) - ).rejects.toThrowError(`The proof request contains duplicate items: age`) + ).rejects.toThrowError('The proof request contains duplicate predicates and attributes: age') }) })