-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: issue where attributes and predicates match (#640)
Signed-off-by: annelein <[email protected]>
- Loading branch information
Showing
7 changed files
with
133 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
packages/core/src/utils/__tests__/indyProofRequest.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { checkProofRequestForDuplicates } from '..' | ||
|
||
import { | ||
AriesFrameworkError, | ||
AttributeFilter, | ||
PredicateType, | ||
ProofAttributeInfo, | ||
ProofPredicateInfo, | ||
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' | ||
}) | ||
|
||
test('attribute names match, same cred def filter', () => { | ||
const attributes = { | ||
name: new ProofAttributeInfo({ | ||
name: 'age', | ||
restrictions: [ | ||
new AttributeFilter({ | ||
credentialDefinitionId: credDefId, | ||
}), | ||
], | ||
}), | ||
age: new ProofAttributeInfo({ | ||
name: 'age', | ||
restrictions: [ | ||
new AttributeFilter({ | ||
credentialDefinitionId: credDefId, | ||
}), | ||
], | ||
}), | ||
} | ||
|
||
const nonce = 'testtesttest12345' | ||
|
||
const proofRequest = new ProofRequest({ | ||
name: 'proof-request', | ||
version: '1.0', | ||
nonce, | ||
requestedAttributes: attributes, | ||
}) | ||
|
||
expect(() => checkProofRequestForDuplicates(proofRequest)).toThrowError(AriesFrameworkError) | ||
}) | ||
|
||
test('attribute names match with predicates name, same cred def filter', () => { | ||
const attributes = { | ||
name: new ProofAttributeInfo({ | ||
name: 'age', | ||
restrictions: [ | ||
new AttributeFilter({ | ||
credentialDefinitionId: credDefId, | ||
}), | ||
], | ||
}), | ||
} | ||
|
||
const predicates = { | ||
age: new ProofPredicateInfo({ | ||
name: 'age', | ||
predicateType: PredicateType.GreaterThanOrEqualTo, | ||
predicateValue: 50, | ||
restrictions: [ | ||
new AttributeFilter({ | ||
credentialDefinitionId: credDefId, | ||
}), | ||
], | ||
}), | ||
} | ||
|
||
const nonce = 'testtesttest12345' | ||
|
||
const proofRequest = new ProofRequest({ | ||
name: 'proof-request', | ||
version: '1.0', | ||
nonce, | ||
requestedAttributes: attributes, | ||
requestedPredicates: predicates, | ||
}) | ||
|
||
expect(() => checkProofRequestForDuplicates(proofRequest)).toThrowError(AriesFrameworkError) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
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()}`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import type { ProofRequest } from '../modules/proofs/models/ProofRequest' | ||
|
||
import { assertNoDuplicatesInArray } from './assertNoDuplicates' | ||
|
||
export 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[]>( | ||
(names, a) => [...names, ...(a.name ? [a.name] : a.names ? a.names : [])], | ||
[] | ||
) | ||
} | ||
|
||
export function predicateNamesToArray(proofRequest: ProofRequest) { | ||
return Array.from(proofRequest.requestedPredicates.values()).map((a) => a.name) | ||
} | ||
|
||
export function checkProofRequestForDuplicates(proofRequest: ProofRequest) { | ||
const attributes = attributeNamesToArray(proofRequest) | ||
const predicates = predicateNamesToArray(proofRequest) | ||
assertNoDuplicatesInArray(attributes.concat(predicates)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters