-
Notifications
You must be signed in to change notification settings - Fork 207
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: issue where attributes and predicates match #640
Merged
TimoGlastra
merged 9 commits into
openwallet-foundation:main
from
animo:fix/attributes_predicates
Mar 3, 2022
Merged
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
3b0794a
fix: issue where attributes and predicates match
Annelein 8174b1f
redundant checks
Annelein 0a57b2e
moved test to util tests
Annelein 028f70c
wrong import
Annelein daf2053
fixed creddefid
Annelein 0237061
removed async
Annelein f610203
predicateNamesToArray
Annelein 4255975
remove unused function
Annelein e50b32e
Merge branch 'main' into fix/attributes_predicates
TimoGlastra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@TimoGlastra I created an issue for this, #646. Is this solution fine or do we need to expose the generateNonce function in a more accessible way?