-
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
Signed-off-by: annelein <[email protected]>
- Loading branch information
Showing
7 changed files
with
233 additions
and
11 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
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,24 @@ | ||
import type { ProofRequest } from '../modules/proofs/models/ProofRequest' | ||
|
||
import { assertNoDuplicatesInArray } from './assertNoDuplicates' | ||
|
||
export function attributesToArray(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 predicatesToArray(proofRequest: ProofRequest) { | ||
return Array.from(proofRequest.requestedPredicates.values()).map((a) => a.name) | ||
} | ||
|
||
export function checkProofRequestForDuplicates(proofRequest: ProofRequest) { | ||
const attributes = attributesToArray(proofRequest) | ||
const predicates = predicatesToArray(proofRequest) | ||
assertNoDuplicatesInArray(attributes) | ||
assertNoDuplicatesInArray(predicates) | ||
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
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