Skip to content

Commit ca6c1ce

Browse files
authored
fix(proofs): allow duplicates in proof attributes (#848)
Signed-off-by: Pavel Zarecky <zarecky@procivis.ch>
1 parent 8055652 commit ca6c1ce

File tree

4 files changed

+25
-35
lines changed

4 files changed

+25
-35
lines changed

packages/core/src/utils/__tests__/indyProofRequest.test.ts

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,21 @@ import {
99
ProofRequest,
1010
} from '@aries-framework/core'
1111

12-
export const INDY_CREDENTIAL_OFFER_ATTACHMENT_ID = 'libindy-cred-offer-0'
13-
1412
describe('Present Proof', () => {
15-
let credDefId: string
16-
17-
beforeAll(() => {
18-
credDefId = '9vPXgSpQJPkJEALbLXueBp:3:CL:57753:tag1'
19-
})
13+
const credDefId = '9vPXgSpQJPkJEALbLXueBp:3:CL:57753:tag1'
14+
const nonce = 'testtesttest12345'
2015

21-
test('attribute names match, same cred def filter', () => {
16+
test('attribute names match', () => {
2217
const attributes = {
23-
name: new ProofAttributeInfo({
18+
age1: new ProofAttributeInfo({
2419
name: 'age',
2520
restrictions: [
2621
new AttributeFilter({
2722
credentialDefinitionId: credDefId,
2823
}),
2924
],
3025
}),
31-
age: new ProofAttributeInfo({
26+
age2: new ProofAttributeInfo({
3227
name: 'age',
3328
restrictions: [
3429
new AttributeFilter({
@@ -38,21 +33,19 @@ describe('Present Proof', () => {
3833
}),
3934
}
4035

41-
const nonce = 'testtesttest12345'
42-
4336
const proofRequest = new ProofRequest({
4437
name: 'proof-request',
4538
version: '1.0',
4639
nonce,
4740
requestedAttributes: attributes,
4841
})
4942

50-
expect(() => checkProofRequestForDuplicates(proofRequest)).toThrowError(AriesFrameworkError)
43+
expect(() => checkProofRequestForDuplicates(proofRequest)).not.toThrow()
5144
})
5245

53-
test('attribute names match with predicates name, same cred def filter', () => {
46+
test('attribute names match with predicates name', () => {
5447
const attributes = {
55-
name: new ProofAttributeInfo({
48+
attrib: new ProofAttributeInfo({
5649
name: 'age',
5750
restrictions: [
5851
new AttributeFilter({
@@ -63,7 +56,7 @@ describe('Present Proof', () => {
6356
}
6457

6558
const predicates = {
66-
age: new ProofPredicateInfo({
59+
predicate: new ProofPredicateInfo({
6760
name: 'age',
6861
predicateType: PredicateType.GreaterThanOrEqualTo,
6962
predicateValue: 50,
@@ -75,8 +68,6 @@ describe('Present Proof', () => {
7568
}),
7669
}
7770

78-
const nonce = 'testtesttest12345'
79-
8071
const proofRequest = new ProofRequest({
8172
name: 'proof-request',
8273
version: '1.0',

packages/core/src/utils/assertNoDuplicates.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { ProofRequest } from '../modules/proofs/models/ProofRequest'
22

3-
import { assertNoDuplicatesInArray } from './assertNoDuplicates'
3+
import { AriesFrameworkError } from '../error/AriesFrameworkError'
44

5-
export function attributeNamesToArray(proofRequest: ProofRequest) {
5+
function attributeNamesToArray(proofRequest: ProofRequest) {
66
// Attributes can contain either a `name` string value or an `names` string array. We reduce it to a single array
77
// containing all attribute names from the requested attributes.
88
return Array.from(proofRequest.requestedAttributes.values()).reduce<string[]>(
@@ -11,12 +11,22 @@ export function attributeNamesToArray(proofRequest: ProofRequest) {
1111
)
1212
}
1313

14-
export function predicateNamesToArray(proofRequest: ProofRequest) {
15-
return Array.from(proofRequest.requestedPredicates.values()).map((a) => a.name)
14+
function predicateNamesToArray(proofRequest: ProofRequest) {
15+
return Array.from(new Set(Array.from(proofRequest.requestedPredicates.values()).map((a) => a.name)))
1616
}
1717

18+
function assertNoDuplicates(predicates: string[], attributeNames: string[]) {
19+
const duplicates = predicates.filter((item) => attributeNames.indexOf(item) !== -1)
20+
if (duplicates.length > 0) {
21+
throw new AriesFrameworkError(
22+
`The proof request contains duplicate predicates and attributes: ${duplicates.toString()}`
23+
)
24+
}
25+
}
26+
27+
// TODO: This is still not ideal. The requested groups can specify different credentials using restrictions.
1828
export function checkProofRequestForDuplicates(proofRequest: ProofRequest) {
1929
const attributes = attributeNamesToArray(proofRequest)
2030
const predicates = predicateNamesToArray(proofRequest)
21-
assertNoDuplicatesInArray(attributes.concat(predicates))
31+
assertNoDuplicates(predicates, attributes)
2232
}

packages/core/tests/proofs.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,6 @@ describe('Present Proof', () => {
365365
requestedAttributes: attributes,
366366
requestedPredicates: predicates,
367367
})
368-
).rejects.toThrowError(`The proof request contains duplicate items: age`)
368+
).rejects.toThrowError('The proof request contains duplicate predicates and attributes: age')
369369
})
370370
})

0 commit comments

Comments
 (0)