Skip to content

Commit

Permalink
chore: temp version
Browse files Browse the repository at this point in the history
  • Loading branch information
sanderPostma committed Aug 29, 2024
1 parent a2e240e commit 443370e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 74 deletions.
110 changes: 46 additions & 64 deletions lib/PEX.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
Format,
PresentationDefinitionV1,
PresentationDefinitionV2,
PresentationSubmission
} from '@sphereon/pex-models';
import { Format, PresentationDefinitionV1, PresentationDefinitionV2, PresentationSubmission } from '@sphereon/pex-models';
import {
CompactSdJwtVc,
CredentialMapper,
Expand All @@ -18,7 +13,7 @@ import {
W3CVerifiableCredential,
W3CVerifiablePresentation,
WrappedVerifiableCredential,
WrappedVerifiablePresentation
WrappedVerifiablePresentation,
} from '@sphereon/ssi-types';

import { Status } from './ConstraintUtils';
Expand All @@ -32,24 +27,11 @@ import {
SdJwtDecodedVerifiableCredentialWithKbJwtInput,
SdJwtKbJwtInput,
VerifiablePresentationFromOpts,
VerifiablePresentationResult
VerifiablePresentationResult,
} from './signing';
import {
DiscoveredVersion,
IInternalPresentationDefinition,
IPresentationDefinition,
OrArray,
PEVersion,
SSITypesBuilder
} from './types';
import { DiscoveredVersion, IInternalPresentationDefinition, IPresentationDefinition, OrArray, PEVersion, SSITypesBuilder } from './types';
import { calculateSdHash, definitionVersionDiscovery, getSubjectIdsAsString } from './utils';
import {
PresentationDefinitionV1VB,
PresentationDefinitionV2VB,
PresentationSubmissionVB,
Validated,
ValidationEngine
} from './validation';
import { PresentationDefinitionV1VB, PresentationDefinitionV2VB, PresentationSubmissionVB, Validated, ValidationEngine } from './validation';

export interface PEXOptions {
/**
Expand Down Expand Up @@ -103,7 +85,7 @@ export class PEX {
*/
presentationSubmissionLocation?: PresentationSubmissionLocation;
generatePresentationSubmission?: boolean;
}
},
): PresentationEvaluationResults {
// We map it to an array for now to make processing on the presentations easier, but before checking against the submission
// we will transform it to the original structure (array vs single) so the references in the submission stay correct
Expand All @@ -118,7 +100,7 @@ export class PEX {
const presentationsCopy: OriginalVerifiablePresentation[] = JSON.parse(JSON.stringify(presentationsArray));

const wrappedPresentations: WrappedVerifiablePresentation[] = presentationsCopy.map((p) =>
SSITypesBuilder.mapExternalVerifiablePresentationToWrappedVP(p, this.options?.hasher)
SSITypesBuilder.mapExternalVerifiablePresentationToWrappedVP(p, this.options?.hasher),
);

let presentationSubmission = opts?.presentationSubmission;
Expand All @@ -141,22 +123,22 @@ export class PEX {
const holderDIDs = wrappedPresentations
.map((p) => {
// @ts-expect-error FIXME Funke - Add DeviceResponseCbor support
return (CredentialMapper.isW3cPresentation(p.presentation) && p.presentation.holder ? p.presentation.holder : undefined);
return CredentialMapper.isW3cPresentation(p.presentation) && p.presentation.holder ? p.presentation.holder : undefined;
})
.filter((d): d is string => d !== undefined);

const updatedOpts = {
...opts,
holderDIDs,
presentationSubmission,
generatePresentationSubmission
generatePresentationSubmission,
};

const allWvcs = wrappedPresentations.reduce((all, wvp) => [...all, ...wvp.vcs], [] as WrappedVerifiableCredential[]);
const result = this._evaluationClientWrapper.evaluatePresentations(
pd,
Array.isArray(presentations) ? wrappedPresentations : wrappedPresentations[0],
updatedOpts
updatedOpts,
);

if (result.areRequiredCredentialsPresent !== Status.ERROR) {
Expand Down Expand Up @@ -189,11 +171,11 @@ export class PEX {
limitDisclosureSignatureSuites?: string[];
restrictToFormats?: Format;
restrictToDIDMethods?: string[];
}
},
): EvaluationResults {
const wrappedVerifiableCredentials: WrappedVerifiableCredential[] = SSITypesBuilder.mapExternalVerifiableCredentialsToWrappedVcs(
verifiableCredentials,
this.options?.hasher
this.options?.hasher,
);

// TODO: So we have state in the form of this property which is set in the constructor, but we are overwriting it here. We need to retrhink how to instantiate PEX
Expand Down Expand Up @@ -230,7 +212,7 @@ export class PEX {
limitDisclosureSignatureSuites?: string[];
restrictToFormats?: Format;
restrictToDIDMethods?: string[];
}
},
): SelectResults {
const verifiableCredentialCopy = JSON.parse(JSON.stringify(verifiableCredentials));
const pd: IInternalPresentationDefinition = SSITypesBuilder.toInternalPresentationDefinition(presentationDefinition);
Expand All @@ -239,7 +221,7 @@ export class PEX {
return this._evaluationClientWrapper.selectFrom(
pd,
SSITypesBuilder.mapExternalVerifiableCredentialsToWrappedVcs(verifiableCredentialCopy, this.options?.hasher),
opts
opts,
);
}

Expand All @@ -254,13 +236,13 @@ export class PEX {
* or Presentation, which means it will become part of the VP
*/
presentationSubmissionLocation?: PresentationSubmissionLocation;
}
},
): PresentationSubmission {
const pd: IInternalPresentationDefinition = SSITypesBuilder.toInternalPresentationDefinition(presentationDefinition);
return this._evaluationClientWrapper.submissionFrom(
pd,
SSITypesBuilder.mapExternalVerifiableCredentialsToWrappedVcs(selectedCredentials, this.options?.hasher),
opts
opts,
);
}

Expand All @@ -278,7 +260,7 @@ export class PEX {
public presentationFrom(
presentationDefinition: IPresentationDefinition,
selectedCredentials: OriginalVerifiableCredential[],
opts?: PresentationFromOpts
opts?: PresentationFromOpts,
): PresentationResult {
const presentationSubmission = this.presentationSubmissionFrom(presentationDefinition, selectedCredentials, opts);
const hasSdJwtCredentials = selectedCredentials.some((c) => CredentialMapper.isSdJwtDecodedCredential(c) || CredentialMapper.isSdJwtEncoded(c));
Expand All @@ -296,13 +278,13 @@ export class PEX {
...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
hasher: this.options?.hasher,
});

return {
presentation,
presentationSubmissionLocation,
presentationSubmission
presentationSubmission,
};
}

Expand All @@ -316,7 +298,7 @@ export class PEX {
* Hasher to use when decoding an SD-JWT credential.
*/
hasher?: Hasher;
}
},
): IPresentation | SdJwtDecodedVerifiableCredentialWithKbJwtInput {
const credentials = Array.isArray(selectedCredentials) ? selectedCredentials : [selectedCredentials];

Expand Down Expand Up @@ -356,19 +338,19 @@ export class PEX {
const kbJwt = {
// alg MUST be set by the signer
header: {
typ: 'kb+jwt'
typ: 'kb+jwt',
},
// aud MUST be set by the signer or provided by e.g. SIOP/OpenID4VP lib
payload: {
iat: new Date().getTime(),
sd_hash: sdHash
}
sd_hash: sdHash,
},
} satisfies SdJwtKbJwtInput;

// @ts-expect-error FIXME Funke
return {
...decoded,
kbJwt
kbJwt,
};
} else {
if (!selectedCredentials) {
Expand All @@ -379,7 +361,7 @@ export class PEX {
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`
`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);
Expand Down Expand Up @@ -416,7 +398,7 @@ export class PEX {
type,
holder,
...(!!opts?.presentationSubmission && { presentation_submission: opts.presentationSubmission }),
verifiableCredential
verifiableCredential,
};
}
}
Expand All @@ -436,13 +418,13 @@ export class PEX {
const validators = [];
result.version === PEVersion.v1
? validators.push({
bundler: new PresentationDefinitionV1VB('root'),
target: SSITypesBuilder.modelEntityToInternalPresentationDefinitionV1(presentationDefinition as PresentationDefinitionV1)
})
bundler: new PresentationDefinitionV1VB('root'),
target: SSITypesBuilder.modelEntityToInternalPresentationDefinitionV1(presentationDefinition as PresentationDefinitionV1),
})
: validators.push({
bundler: new PresentationDefinitionV2VB('root'),
target: SSITypesBuilder.modelEntityInternalPresentationDefinitionV2(presentationDefinition as PresentationDefinitionV2)
});
bundler: new PresentationDefinitionV2VB('root'),
target: SSITypesBuilder.modelEntityInternalPresentationDefinitionV2(presentationDefinition as PresentationDefinitionV2),
});
return new ValidationEngine().validate(validators);
}

Expand All @@ -457,8 +439,8 @@ export class PEX {
return new ValidationEngine().validate([
{
bundler: new PresentationSubmissionVB('root'),
target: presentationSubmission
}
target: presentationSubmission,
},
]);
}

Expand All @@ -483,7 +465,7 @@ export class PEX {
presentationDefinition: IPresentationDefinition,
selectedCredentials: OriginalVerifiableCredential[],
signingCallBack: (callBackParams: PresentationSignCallBackParams) => OrPromise<W3CVerifiablePresentation | CompactSdJwtVc>,
opts: VerifiablePresentationFromOpts
opts: VerifiablePresentationFromOpts,
): Promise<VerifiablePresentationResult> {
const { holderDID, signatureOptions, proofOptions } = opts;

Expand All @@ -502,15 +484,15 @@ export class PEX {
const limitDisclosureSignatureSuites = limitedDisclosureSuites();
const evaluationResult = this.evaluateCredentials(presentationDefinition, selectedCredentials, {
holderDIDs,
limitDisclosureSignatureSuites
limitDisclosureSignatureSuites,
});

const presentationResult = this.presentationFrom(presentationDefinition, evaluationResult.verifiableCredential, opts);
const evaluationResults = this.evaluatePresentation(presentationDefinition, presentationResult.presentation, {
limitDisclosureSignatureSuites,
...(presentationResult.presentationSubmissionLocation === PresentationSubmissionLocation.EXTERNAL && {
presentationSubmission: presentationResult.presentationSubmission
})
presentationSubmission: presentationResult.presentationSubmission,
}),
});
if (!evaluationResults.value && selectedCredentials.length === 0) {
evaluationResults.value = presentationResult.presentationSubmission;
Expand All @@ -528,7 +510,7 @@ export class PEX {
jws: signatureOptions?.jws,
challenge: proofOptions?.challenge,
nonce: proofOptions?.nonce,
domain: proofOptions?.domain
domain: proofOptions?.domain,
};

let presentation = presentationResult.presentation;
Expand All @@ -545,41 +527,41 @@ export class PEX {
const kbJwt = {
// alg MUST be set by the signer
header: {
typ: 'kb+jwt'
typ: 'kb+jwt',
},
// aud MUST be set by the signer or provided by e.g. SIOP/OpenID4VP lib
payload: {
iat: new Date().getTime(),
nonce: proofOptions?.nonce,
sd_hash: sdHash
}
sd_hash: sdHash,
},
} satisfies SdJwtKbJwtInput;

// @ts-expect-error FIXME Funke
presentation = {
...presentation,
kbJwt
kbJwt,
};
}

const callBackParams: PresentationSignCallBackParams = {
options: {
...opts,
presentationSubmissionLocation: presentationResult.presentationSubmissionLocation
presentationSubmissionLocation: presentationResult.presentationSubmissionLocation,
},
presentation,
presentationDefinition,
selectedCredentials,
proof,
presentationSubmission: evaluationResults.value,
evaluationResults
evaluationResults,
};
const verifiablePresentation = await signingCallBack(callBackParams);

return {
verifiablePresentation,
presentationSubmissionLocation: presentationResult.presentationSubmissionLocation,
presentationSubmission: evaluationResults.value
presentationSubmission: evaluationResults.value,
};
}

Expand Down
16 changes: 6 additions & 10 deletions lib/evaluation/handlers/didRestrictionEvaluationHandler.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import { CredentialMapper, WrappedVerifiableCredential } from '@sphereon/ssi-types';

import { Status } from '../../ConstraintUtils';
import {
IInternalPresentationDefinition,
InternalPresentationDefinitionV1,
InternalPresentationDefinitionV2
} from '../../types';
import { IInternalPresentationDefinition, InternalPresentationDefinitionV1, InternalPresentationDefinitionV2 } from '../../types';
import PexMessages from '../../types/Messages';
import { isRestrictedDID } from '../../utils';
import { HandlerCheckResult } from '../core';
Expand Down Expand Up @@ -47,7 +43,7 @@ export class DIDRestrictionEvaluationHandler extends AbstractEvaluationHandler {
return typeof wrappedVc.credential.issuer === 'object' ? wrappedVc.credential.issuer.id : wrappedVc.credential.issuer;
} else if (CredentialMapper.isSdJwtDecodedCredential(wrappedVc.credential)) {
return wrappedVc.credential.decodedPayload.iss;
} else if (CredentialMapper.isMsoMdocOid4VPEncoded(wrappedVc.credential) && typeof (wrappedVc.decoded) === 'object') {
} else if (CredentialMapper.isMsoMdocOid4VPEncoded(wrappedVc.credential) && typeof wrappedVc.decoded === 'object') {
return wrappedVc.decoded.iss ?? '';
}
return ''; // FIXME Funke
Expand All @@ -62,8 +58,8 @@ export class DIDRestrictionEvaluationHandler extends AbstractEvaluationHandler {
message: PexMessages.FORMAT_RESTRICTION_DIDNT_PASS,
verifiable_credential_path: vcPath,
payload: {
format: wvc.format
}
format: wvc.format,
},
};
}

Expand All @@ -75,8 +71,8 @@ export class DIDRestrictionEvaluationHandler extends AbstractEvaluationHandler {
message: message ?? PexMessages.FORMAT_RESTRICTION_PASSED,
verifiable_credential_path: vcPath,
payload: {
format: wvc.format
}
format: wvc.format,
},
};
}
}

0 comments on commit 443370e

Please sign in to comment.