Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(proofs): allow duplicates in proof attributes #848

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 9 additions & 18 deletions packages/core/src/utils/__tests__/indyProofRequest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,21 @@ 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({
credentialDefinitionId: credDefId,
}),
],
}),
age: new ProofAttributeInfo({
age2: new ProofAttributeInfo({
name: 'age',
restrictions: [
new AttributeFilter({
Expand All @@ -38,21 +33,19 @@ describe('Present Proof', () => {
}),
}

const nonce = 'testtesttest12345'

const proofRequest = new ProofRequest({
name: 'proof-request',
version: '1.0',
nonce,
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({
Expand All @@ -63,7 +56,7 @@ describe('Present Proof', () => {
}

const predicates = {
age: new ProofPredicateInfo({
predicate: new ProofPredicateInfo({
name: 'age',
predicateType: PredicateType.GreaterThanOrEqualTo,
predicateValue: 50,
Expand All @@ -75,8 +68,6 @@ describe('Present Proof', () => {
}),
}

const nonce = 'testtesttest12345'

const proofRequest = new ProofRequest({
name: 'proof-request',
version: '1.0',
Expand Down
11 changes: 0 additions & 11 deletions packages/core/src/utils/assertNoDuplicates.ts

This file was deleted.

20 changes: 15 additions & 5 deletions packages/core/src/utils/indyProofRequest.ts
Original file line number Diff line number Diff line change
@@ -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<string[]>(
Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion packages/core/tests/proofs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
})
})