Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/develop' into feat/sd-jwt
Browse files Browse the repository at this point in the history
  • Loading branch information
TimoGlastra committed Dec 5, 2023
2 parents eda9a9c + dbea36c commit 86c8ae5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 15 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# Release Notes

## v2.2.2 - 2023-11-28

- Updated:
- Updated pex-model deps
- Added more logic to deduce holder(s) for a VP
- Fixed:
- Logic when to generate a presentation submission was incorrect

## v2.2.1 - 2023-10-23

- Updated:
- Updated logic to return holder DID when constructing presentation
- Fixed:
- Issue with determining whether to generate submission data or not.

## v2.2.0 - 2023-10-12

- Updated:
Expand Down
37 changes: 26 additions & 11 deletions lib/PEX.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
CompactSdJwtVc,
CredentialMapper,
Hasher,
ICredential,
IPresentation,
IProof,
OriginalVerifiableCredential,
Expand All @@ -28,7 +29,7 @@ import {
VerifiablePresentationResult,
} from './signing';
import { DiscoveredVersion, IInternalPresentationDefinition, IPresentationDefinition, PEVersion, SSITypesBuilder } from './types';
import { calculateSdHash, definitionVersionDiscovery } from './utils';
import { calculateSdHash, definitionVersionDiscovery, getSubjectIdsAsString } from './utils';
import { PresentationDefinitionV1VB, PresentationDefinitionV2VB, PresentationSubmissionVB, Validated, ValidationEngine } from './validation';

export interface PEXOptions {
Expand Down Expand Up @@ -80,7 +81,7 @@ export class PEX {
},
): EvaluationResults {
const generatePresentationSubmission =
opts?.generatePresentationSubmission !== undefined ? opts.generatePresentationSubmission : opts?.presentationSubmission !== undefined;
opts?.generatePresentationSubmission !== undefined ? opts.generatePresentationSubmission : opts?.presentationSubmission === undefined;
const pd: IInternalPresentationDefinition = SSITypesBuilder.toInternalPresentationDefinition(presentationDefinition);
const presentationCopy: OriginalVerifiablePresentation = JSON.parse(JSON.stringify(presentation));
const wrappedPresentation: WrappedVerifiablePresentation = SSITypesBuilder.mapExternalVerifiablePresentationToWrappedVP(
Expand Down Expand Up @@ -242,6 +243,7 @@ export class PEX {

const presentation = PEX.constructPresentation(selectedCredentials, {
...opts,
// We only pass in the submission in case it needs to be included in the presentation
presentationSubmission: presentationSubmissionLocation === PresentationSubmissionLocation.PRESENTATION ? presentationSubmission : undefined,
hasher: this.options?.hasher,
});
Expand Down Expand Up @@ -317,12 +319,28 @@ export class PEX {
kbJwt,
};
} else {
const holder = opts?.holderDID;
const type = Array.isArray(opts?.basePresentationPayload?.type)
? opts?.basePresentationPayload?.type || []
: opts?.basePresentationPayload?.type
? [opts.basePresentationPayload.type]
if (!selectedCredentials) {
throw Error(`At least a verifiable credential needs to be passed in to create a presentation`);
}
const verifiableCredential = (Array.isArray(selectedCredentials) ? selectedCredentials : [selectedCredentials]) as W3CVerifiableCredential[];
const wVCs = verifiableCredential.map((vc) => CredentialMapper.toWrappedVerifiableCredential(vc));
const holders = Array.from(new Set(wVCs.flatMap((wvc) => getSubjectIdsAsString(wvc.credential as ICredential))));
if (holders.length !== 1 && !opts?.holderDID) {
console.log(
`We deduced ${holders.length} subject from ${wVCs.length} Verifiable Credentials, and no holder property was given. This might lead to undesired results`,
);
}
const holder = opts?.holderDID ?? (holders.length === 1 ? holders[0] : undefined);

const type = opts?.basePresentationPayload?.type
? Array.isArray(opts.basePresentationPayload.type)
? opts.basePresentationPayload.type
: [opts.basePresentationPayload.type]
: [];
if (!type.includes('VerifiablePresentation')) {
type.push('VerifiablePresentation');
}

const context = opts?.basePresentationPayload?.['@context']
? Array.isArray(opts.basePresentationPayload['@context'])
? opts.basePresentationPayload['@context']
Expand All @@ -332,9 +350,6 @@ export class PEX {
context.push('https://www.w3.org/2018/credentials/v1');
}

if (!type.includes('VerifiablePresentation')) {
type.push('VerifiablePresentation');
}
if (opts?.presentationSubmission) {
if (!type.includes('PresentationSubmission')) {
type.push('PresentationSubmission');
Expand All @@ -349,7 +364,7 @@ export class PEX {
type,
holder,
...(!!opts?.presentationSubmission && { presentation_submission: opts.presentationSubmission }),
verifiableCredential: (Array.isArray(selectedCredentials) ? selectedCredentials : [selectedCredentials]) as W3CVerifiableCredential[],
verifiableCredential,
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sphereon/pex",
"version": "2.2.1-unstable.0",
"version": "2.2.3-unstable.0",
"description": "A Typescript implementation of the v1 and v2 DIF Presentation Exchange specification",
"main": "dist/main/index.js",
"module": "dist/module/index.js",
Expand Down
9 changes: 6 additions & 3 deletions test/PEX.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,12 @@ describe('evaluate', () => {
// Delete the submission to trigger an error
delete vpSimple.presentation_submission;
const pex: PEX = new PEX();
expect(() => pex.evaluatePresentation(pdSchema, vpSimple, { limitDisclosureSignatureSuites: LIMIT_DISCLOSURE_SIGNATURE_SUITES })).toThrowError(
'Either a presentation submission as part of the VP or provided separately was expected',
);
expect(() =>
pex.evaluatePresentation(pdSchema, vpSimple, {
limitDisclosureSignatureSuites: LIMIT_DISCLOSURE_SIGNATURE_SUITES,
generatePresentationSubmission: false,
}),
).toThrowError('Either a presentation submission as part of the VP or provided separately was expected');
});

it('Evaluate case without any error 2', () => {
Expand Down

0 comments on commit 86c8ae5

Please sign in to comment.