Skip to content

Commit

Permalink
Filter by VPR QueryByExample example JSON-LD context.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlongley committed Oct 28, 2024
1 parent ac30d4f commit 4edeaf6
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# bedrock-web-wallet ChangeLog

## 14.6.2 - 2024-10-dd

### Fixed
- Filter VPR query matches by JSON-LD context, if one was provided
in the query by example `example`.

## 14.6.1 - 2024-10-02

### Fixed
Expand Down
33 changes: 33 additions & 0 deletions lib/presentations.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,9 @@ async function _getMatches({
for(const result of results) {
const {credentialQuery, matches} = result;

// remove any matches that do not include the requested context
result.matches = matches.filter(_matchContextFilter({credentialQuery}));

// apply local filters
// FIXME: write generalized matching instead
result.matches = matches.filter(_openBadgeFilter({credentialQuery}));
Expand Down Expand Up @@ -422,6 +425,36 @@ function _openBadgeFilter({credentialQuery}) {
};
}

function _matchContextFilter({credentialQuery}) {
// get expected context
let expectedContext = credentialQuery?.example?.['@context'];
if(expectedContext === undefined) {
// no context specified, allow any result
return () => true;
}
// normalize to an array
if(!Array.isArray(expectedContext)) {
expectedContext = [expectedContext];
}
// map to an array of strings for later comparison
expectedContext = expectedContext.map(
ctx => typeof ctx === 'string' ? ctx : JSON.stringify(ctx));
return ({record: {content}}) => {
// normalize record context to an array if present
let recordContext = content['@context'];
if(recordContext !== undefined && !Array.isArray(recordContext)) {
recordContext = [recordContext];
}
// map to an array of strings for later comparison
recordContext = recordContext.map(
ctx => typeof ctx === 'string' ? ctx : JSON.stringify(ctx));

// if every context value in `expectedContext` is present in
// `recordContext`, then accept the record
return expectedContext.every(c => recordContext.includes(c));
};
}

async function _matchQueryByExample({
verifiablePresentationRequest, query, credentialStore, matches
}) {
Expand Down

0 comments on commit 4edeaf6

Please sign in to comment.