-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs(changeset): feat: presentation matches
- Loading branch information
1 parent
b44a33c
commit ba6c09b
Showing
9 changed files
with
200 additions
and
33 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'dcql': patch | ||
--- | ||
|
||
feat: presentation matches |
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
106 changes: 106 additions & 0 deletions
106
dcql/src/dcql-presentation/m-dcql-valid-dcql-query-result.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,106 @@ | ||
import * as v from 'valibot'; | ||
|
||
import { DcqlQueryResult } from '../dcql-query-result/m-dcql-query-result.js'; | ||
import { DcqlInvalidPresentationRecordError } from '../e-dcql.js'; | ||
import { idRegex } from '../u-dcql.js'; | ||
|
||
export namespace DcqlPresentationQueryResult { | ||
export const vModel = v.object({ | ||
...DcqlQueryResult.vModel.entries, | ||
canBeSatisfied: v.literal(true), | ||
presentation_matches: v.record( | ||
v.pipe(v.string(), v.regex(idRegex)), | ||
v.object({ | ||
...v.omit( | ||
DcqlQueryResult.vModel.entries.credential_matches.value.options[0], | ||
['all', 'issues', 'credential_index'] | ||
).entries, | ||
presentation_index: v.number(), | ||
}) | ||
), | ||
}); | ||
|
||
export type Input = v.InferInput<typeof vModel>; | ||
export type Output = v.InferOutput<typeof vModel>; | ||
|
||
export type UnknownResult = | ||
| (DcqlQueryResult & { | ||
canBeSatisfied: false; | ||
presentation_matches?: undefined; | ||
}) | ||
| DcqlPresentationQueryResult; | ||
|
||
export const parse = (input: Input | DcqlQueryResult) => { | ||
return v.parse(vModel, input); | ||
}; | ||
|
||
export const fromDcqlQueryResult = ( | ||
dcqlQueryResult: DcqlQueryResult | ||
): DcqlPresentationQueryResult => { | ||
const { canBeSatisfied } = dcqlQueryResult; | ||
if (!canBeSatisfied) { | ||
throw new DcqlInvalidPresentationRecordError({ | ||
message: 'Invalid Presentation record', | ||
cause: dcqlQueryResult, | ||
}); | ||
} | ||
|
||
const presentation_matches: DcqlPresentationQueryResult['presentation_matches'] = | ||
{}; | ||
|
||
if (!dcqlQueryResult.credential_sets) { | ||
for (const credentialQueryId of dcqlQueryResult.credentials.map( | ||
c => c.id | ||
)) { | ||
const match = dcqlQueryResult.credential_matches[credentialQueryId]; | ||
if (!match?.success) { | ||
throw new DcqlInvalidPresentationRecordError({ | ||
message: `Credential query ${credentialQueryId} is required but not satisfied.`, | ||
}); | ||
} | ||
|
||
const { all, issues, credential_index, ...rest } = match; | ||
presentation_matches[credentialQueryId] = { | ||
...rest, | ||
presentation_index: credential_index, | ||
}; | ||
} | ||
|
||
return { | ||
...dcqlQueryResult, | ||
canBeSatisfied, | ||
presentation_matches, | ||
}; | ||
} | ||
|
||
for (const credentialSet of dcqlQueryResult.credential_sets ?? []) { | ||
const matchingOption = credentialSet.matching_options?.find(Boolean); | ||
if (!matchingOption) throw new Error('Invalid matching option'); | ||
|
||
for (const credentialQueryId of matchingOption) { | ||
const match = dcqlQueryResult.credential_matches[credentialQueryId]; | ||
if (match?.success) { | ||
const { all, issues, credential_index, ...rest } = match; | ||
presentation_matches[credentialQueryId] = { | ||
...rest, | ||
presentation_index: credential_index, | ||
}; | ||
continue; | ||
} | ||
|
||
if (credentialSet.required) { | ||
throw new DcqlInvalidPresentationRecordError({ | ||
message: `Credential query ${credentialQueryId} is required but not satisfied.`, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
return { | ||
...dcqlQueryResult, | ||
canBeSatisfied, | ||
presentation_matches, | ||
}; | ||
}; | ||
} | ||
export type DcqlPresentationQueryResult = DcqlPresentationQueryResult.Output; |
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
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