Skip to content

Commit 50921b0

Browse files
Merge pull request #9 from animo/refactor/replace-mdoc-lib
2 parents 35ab83d + 982438a commit 50921b0

35 files changed

+445
-122
lines changed

lib/PEX.ts

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
import { Format, PresentationDefinitionV1, PresentationDefinitionV2, PresentationSubmission } from '@sphereon/pex-models';
22
import {
33
CompactSdJwtVc,
4-
CredentialMapper,
54
Hasher,
65
ICredential,
76
IPresentation,
87
IProof,
98
JwtDecodedVerifiableCredential,
10-
OriginalVerifiableCredential,
11-
OriginalVerifiablePresentation,
129
OrPromise,
1310
SdJwtDecodedVerifiableCredential,
1411
W3CVerifiableCredential,
1512
W3CVerifiablePresentation,
16-
WrappedVerifiableCredential,
17-
WrappedVerifiablePresentation,
1813
} from '@sphereon/ssi-types';
1914

2015
import { Status } from './ConstraintUtils';
@@ -30,6 +25,13 @@ import {
3025
VerifiablePresentationResult,
3126
} from './signing';
3227
import { DiscoveredVersion, IInternalPresentationDefinition, IPresentationDefinition, OrArray, PEVersion, SSITypesBuilder } from './types';
28+
import {
29+
OriginalVerifiableCredential,
30+
OriginalVerifiablePresentation,
31+
PexCredentialMapper,
32+
WrappedVerifiableCredential,
33+
WrappedVerifiablePresentation,
34+
} from './types/PexCredentialMapper';
3335
import { calculateSdHash, definitionVersionDiscovery, formatValidationErrors, getSubjectIdsAsString } from './utils';
3436
import { PresentationDefinitionV1VB, PresentationDefinitionV2VB, PresentationSubmissionVB, Validated, ValidationEngine } from './validation';
3537

@@ -38,7 +40,7 @@ export interface PEXOptions {
3840
* Hasher implementation, can be used for tasks such as decoding a compact SD-JWT VC to it's encoded variant.
3941
* When decoding SD-JWT credentials the hasher must be provided. The hasher implementation must be sync. When using
4042
* an async hasher implementation, you must manually decode the credential or presentation first according to the
41-
* `SdJwtDecodedVerifiableCredential` interface. You can use the `CredentialMapper.decodeSdJwtAsync` method for
43+
* `SdJwtDecodedVerifiableCredential` interface. You can use the `PexCredentialMapper.decodeSdJwtAsync` method for
4244
* this from the `@sphereon/ssi-types` package. NOTE that this is only needed when using an async hasher, and
4345
* that for sync hashers providing it here is enough for the decoding to be done automatically.
4446
*/
@@ -106,15 +108,15 @@ export class PEX {
106108
let presentationSubmission = opts?.presentationSubmission;
107109
let presentationSubmissionLocation =
108110
opts?.presentationSubmissionLocation ??
109-
((Array.isArray(presentations) && presentations.length > 1) || !CredentialMapper.isW3cPresentation(wrappedPresentations[0].presentation)
111+
((Array.isArray(presentations) && presentations.length > 1) || !PexCredentialMapper.isW3cPresentation(wrappedPresentations[0].presentation)
110112
? PresentationSubmissionLocation.EXTERNAL
111113
: PresentationSubmissionLocation.PRESENTATION);
112114

113115
// When only one presentation, we also allow it to be present in the VP
114116
if (
115117
!presentationSubmission &&
116118
presentationsArray.length === 1 &&
117-
CredentialMapper.isW3cPresentation(wrappedPresentations[0].presentation) &&
119+
PexCredentialMapper.isW3cPresentation(wrappedPresentations[0].presentation) &&
118120
!generatePresentationSubmission
119121
) {
120122
const decoded = wrappedPresentations[0].decoded;
@@ -138,7 +140,7 @@ export class PEX {
138140
// `wrappedPresentation.original.compactKbJwt`, but as HAIP doesn't use dids, we'll leave it for now.
139141
const holderDIDs = wrappedPresentations
140142
.map((p) => {
141-
return CredentialMapper.isW3cPresentation(p.presentation) && p.presentation.holder ? p.presentation.holder : undefined;
143+
return PexCredentialMapper.isW3cPresentation(p.presentation) && p.presentation.holder ? p.presentation.holder : undefined;
142144
})
143145
.filter((d): d is string => d !== undefined);
144146

@@ -269,7 +271,9 @@ export class PEX {
269271
opts?: PresentationFromOpts,
270272
): PresentationResult {
271273
const presentationSubmission = this.presentationSubmissionFrom(presentationDefinition, selectedCredentials, opts);
272-
const hasSdJwtCredentials = selectedCredentials.some((c) => CredentialMapper.isSdJwtDecodedCredential(c) || CredentialMapper.isSdJwtEncoded(c));
274+
const hasSdJwtCredentials = selectedCredentials.some(
275+
(c) => PexCredentialMapper.isSdJwtDecodedCredential(c) || PexCredentialMapper.isSdJwtEncoded(c),
276+
);
273277

274278
// We could include it in the KB-JWT? Not sure if we want that
275279
if (opts?.presentationSubmissionLocation === PresentationSubmissionLocation.PRESENTATION && hasSdJwtCredentials) {
@@ -310,7 +314,7 @@ export class PEX {
310314
}
311315
const verifiableCredential = (Array.isArray(selectedCredentials) ? selectedCredentials : [selectedCredentials]) as W3CVerifiableCredential[];
312316

313-
const wVCs = verifiableCredential.map((vc) => CredentialMapper.toWrappedVerifiableCredential(vc));
317+
const wVCs = verifiableCredential.map((vc) => PexCredentialMapper.toWrappedVerifiableCredential(vc));
314318
const holders = Array.from(new Set(wVCs.flatMap((wvc) => getSubjectIdsAsString(wvc.credential as ICredential))));
315319
const holder = opts?.holderDID ?? (holders.length === 1 ? holders[0] : undefined);
316320

@@ -352,10 +356,10 @@ export class PEX {
352356
});
353357
} else {
354358
verifiableCredential.forEach((vc) => {
355-
if (CredentialMapper.isSdJwtDecodedCredential(vc)) {
359+
if (PexCredentialMapper.isSdJwtDecodedCredential(vc)) {
356360
result.push(vc as PartialSdJwtDecodedVerifiableCredential);
357-
} else if (CredentialMapper.isSdJwtEncoded(vc)) {
358-
const decoded = CredentialMapper.decodeVerifiableCredential(vc);
361+
} else if (PexCredentialMapper.isSdJwtEncoded(vc)) {
362+
const decoded = PexCredentialMapper.decodeVerifiableCredential(vc);
359363
result.push(decoded as PartialSdJwtDecodedVerifiableCredential);
360364
} else {
361365
// This should be jwt or json-ld
@@ -377,15 +381,15 @@ export class PEX {
377381
TODO SDK-37 refinement needed
378382
*/
379383
private static allowMultipleVCsPerPresentation(verifiableCredentials: Array<OriginalVerifiableCredential>): boolean {
380-
const jwtCredentials = verifiableCredentials.filter((c) => CredentialMapper.isJwtEncoded(c) || CredentialMapper.isJwtDecodedCredential(c));
384+
const jwtCredentials = verifiableCredentials.filter((c) => PexCredentialMapper.isJwtEncoded(c) || PexCredentialMapper.isJwtDecodedCredential(c));
381385

382386
if (jwtCredentials.length > 0) {
383387
const subjects = new Set<string>();
384388
const verificationMethods = new Set<string>();
385389

386390
for (const credential of jwtCredentials) {
387-
const decodedCredential = CredentialMapper.isJwtEncoded(credential)
388-
? (CredentialMapper.decodeVerifiableCredential(credential) as JwtDecodedVerifiableCredential)
391+
const decodedCredential = PexCredentialMapper.isJwtEncoded(credential)
392+
? (PexCredentialMapper.decodeVerifiableCredential(credential) as JwtDecodedVerifiableCredential)
389393
: (credential as JwtDecodedVerifiableCredential);
390394

391395
const subject =
@@ -406,7 +410,7 @@ export class PEX {
406410
}
407411
}
408412

409-
if (verifiableCredentials.some((c) => CredentialMapper.isSdJwtEncoded(c) || CredentialMapper.isSdJwtDecodedCredential(c))) {
413+
if (verifiableCredentials.some((c) => PexCredentialMapper.isSdJwtEncoded(c) || PexCredentialMapper.isSdJwtDecodedCredential(c))) {
410414
return false;
411415
}
412416

@@ -568,7 +572,7 @@ export class PEX {
568572
) {
569573
presentations.forEach((presentation, index) => {
570574
// Select type without kbJwt as isSdJwtDecodedCredential and won't accept the partial sdvc type
571-
if (CredentialMapper.isSdJwtDecodedCredential(presentation as SdJwtDecodedVerifiableCredential)) {
575+
if (PexCredentialMapper.isSdJwtDecodedCredential(presentation as SdJwtDecodedVerifiableCredential)) {
572576
const sdJwtCredential = presentation as SdJwtDecodedVerifiableCredential;
573577

574578
// extract sd_alg or default to sha-256

lib/PEXv1.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { Format, PresentationDefinitionV1, PresentationSubmission } from '@sphereon/pex-models';
2-
import { CredentialMapper, IPresentation, OriginalVerifiableCredential, OriginalVerifiablePresentation } from '@sphereon/ssi-types';
2+
import { IPresentation } from '@sphereon/ssi-types';
33

44
import { PEX } from './PEX';
55
import { EvaluationClientWrapper, EvaluationResults, PresentationEvaluationResults, SelectResults } from './evaluation';
66
import { PresentationFromOpts, PresentationResult, PresentationSubmissionLocation } from './signing';
77
import { SSITypesBuilder } from './types';
8+
import { OriginalVerifiableCredential, OriginalVerifiablePresentation, PexCredentialMapper } from './types/PexCredentialMapper';
89
import { PresentationDefinitionV1VB, Validated, ValidationEngine } from './validation';
910

1011
/**
@@ -113,7 +114,9 @@ export class PEXv1 extends PEX {
113114
opts,
114115
);
115116

116-
const hasSdJwtCredentials = selectedCredentials.some((c) => CredentialMapper.isSdJwtDecodedCredential(c) || CredentialMapper.isSdJwtEncoded(c));
117+
const hasSdJwtCredentials = selectedCredentials.some(
118+
(c) => PexCredentialMapper.isSdJwtDecodedCredential(c) || PexCredentialMapper.isSdJwtEncoded(c),
119+
);
117120

118121
// We could include it in the KB-JWT? Not sure if we want that
119122
if (opts?.presentationSubmissionLocation === PresentationSubmissionLocation.PRESENTATION && hasSdJwtCredentials) {

lib/PEXv2.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { Format, PresentationDefinitionV2, PresentationSubmission } from '@sphereon/pex-models';
2-
import { CredentialMapper, IPresentation, OriginalVerifiableCredential, OriginalVerifiablePresentation } from '@sphereon/ssi-types';
2+
import { IPresentation } from '@sphereon/ssi-types';
33

44
import { PEX } from './PEX';
55
import { EvaluationClientWrapper, EvaluationResults, PresentationEvaluationResults, SelectResults } from './evaluation';
66
import { PresentationFromOpts, PresentationResult, PresentationSubmissionLocation } from './signing';
77
import { SSITypesBuilder } from './types';
8+
import { OriginalVerifiableCredential, OriginalVerifiablePresentation, PexCredentialMapper } from './types/PexCredentialMapper';
89
import { PresentationDefinitionV2VB, Validated, ValidationEngine } from './validation';
910

1011
/**
@@ -109,7 +110,9 @@ export class PEXv2 extends PEX {
109110
SSITypesBuilder.mapExternalVerifiableCredentialsToWrappedVcs(selectedCredentials),
110111
opts,
111112
);
112-
const hasSdJwtCredentials = selectedCredentials.some((c) => CredentialMapper.isSdJwtDecodedCredential(c) || CredentialMapper.isSdJwtEncoded(c));
113+
const hasSdJwtCredentials = selectedCredentials.some(
114+
(c) => PexCredentialMapper.isSdJwtDecodedCredential(c) || PexCredentialMapper.isSdJwtEncoded(c),
115+
);
113116

114117
// We could include it in the KB-JWT? Not sure if we want that
115118
if (opts?.presentationSubmissionLocation === PresentationSubmissionLocation.PRESENTATION && hasSdJwtCredentials) {

lib/evaluation/core/evaluationResults.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { PresentationSubmission } from '@sphereon/pex-models';
2-
import { IPresentation, IVerifiableCredential, OriginalVerifiablePresentation, SdJwtDecodedVerifiableCredential } from '@sphereon/ssi-types';
2+
import { IPresentation, IVerifiableCredential, SdJwtDecodedVerifiableCredential } from '@sphereon/ssi-types';
33

44
import { Checked, Status } from '../../ConstraintUtils';
5+
import { OriginalVerifiablePresentation } from '../../types/PexCredentialMapper';
56

67
export interface PresentationEvaluationResults extends Omit<EvaluationResults, 'verifiableCredential'> {
78
presentations: Array<OriginalVerifiablePresentation | IPresentation>;

lib/evaluation/core/selectResults.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { OriginalVerifiableCredential } from '@sphereon/ssi-types';
2-
31
import { Checked, Status } from '../../ConstraintUtils';
2+
import { OriginalVerifiableCredential } from '../../types/PexCredentialMapper';
43

54
import { SubmissionRequirementMatch } from './submissionRequirementMatch';
65

lib/evaluation/evaluationClient.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { Format, PresentationSubmission } from '@sphereon/pex-models';
2-
import { IProofType, WrappedVerifiableCredential } from '@sphereon/ssi-types';
2+
import { IProofType } from '@sphereon/ssi-types';
33

44
import { Status } from '../ConstraintUtils';
55
import { IInternalPresentationDefinition } from '../types';
66
import PexMessages from '../types/Messages';
7+
import { WrappedVerifiableCredential } from '../types/PexCredentialMapper';
78
import { filterToRestrictedDIDs, uniformDIDMethods } from '../utils';
89

910
import { HandlerCheckResult } from './core';

lib/evaluation/evaluationClientWrapper.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
import { JSONPath as jp } from '@astronautlabs/jsonpath';
22
import { Descriptor, Format, InputDescriptorV1, InputDescriptorV2, PresentationSubmission, Rules } from '@sphereon/pex-models';
33
import type { SubmissionRequirement } from '@sphereon/pex-models';
4-
import {
5-
CredentialMapper,
6-
IVerifiableCredential,
7-
IVerifiablePresentation,
8-
OriginalVerifiableCredential,
9-
SdJwtDecodedVerifiableCredential,
10-
WrappedMdocCredential,
11-
WrappedVerifiableCredential,
12-
WrappedVerifiablePresentation,
13-
} from '@sphereon/ssi-types';
4+
import { IVerifiableCredential, IVerifiablePresentation, SdJwtDecodedVerifiableCredential } from '@sphereon/ssi-types';
145

156
import { Checked, Status } from '../ConstraintUtils';
167
import { PresentationSubmissionLocation } from '../signing';
@@ -21,6 +12,13 @@ import {
2112
IPresentationDefinition,
2213
OrArray,
2314
} from '../types';
15+
import {
16+
OriginalVerifiableCredential,
17+
PexCredentialMapper,
18+
WrappedMdocCredential,
19+
WrappedVerifiableCredential,
20+
WrappedVerifiablePresentation,
21+
} from '../types/PexCredentialMapper';
2422
import { JsonPathUtils } from '../utils';
2523
import { getVpFormatForVcFormat } from '../utils/formatMap';
2624

@@ -468,7 +466,7 @@ export class EvaluationClientWrapper {
468466
}
469467

470468
// FIXME figure out possible types, can't see that in debug mode...
471-
const isCredential = CredentialMapper.isCredential(vcResult.value as OriginalVerifiableCredential);
469+
const isCredential = PexCredentialMapper.isCredential(vcResult.value as OriginalVerifiableCredential);
472470
if (
473471
!vcResult.value ||
474472
(typeof vcResult.value === 'string' && !isCredential) ||
@@ -505,7 +503,7 @@ export class EvaluationClientWrapper {
505503
}
506504

507505
// Find the corresponding Wrapped Verifiable Credential (wvc) based on the original VC
508-
const wvc = wvp.vcs.find((wrappedVc) => CredentialMapper.areOriginalVerifiableCredentialsEqual(wrappedVc.original, originalVc));
506+
const wvc = wvp.vcs.find((wrappedVc) => PexCredentialMapper.areOriginalVerifiableCredentialsEqual(wrappedVc.original, originalVc));
509507

510508
if (!wvc) {
511509
return {
@@ -555,7 +553,7 @@ export class EvaluationClientWrapper {
555553
// If only a single VP is passed that is not w3c and no presentationSubmissionLocation, we set the default location to presentation. Otherwise we assume it's external
556554
const presentationSubmissionLocation =
557555
opts?.presentationSubmissionLocation ??
558-
(Array.isArray(wvps) || !CredentialMapper.isW3cPresentation(Array.isArray(wvps) ? wvps[0].presentation : wvps.presentation)
556+
(Array.isArray(wvps) || !PexCredentialMapper.isW3cPresentation(Array.isArray(wvps) ? wvps[0].presentation : wvps.presentation)
559557
? PresentationSubmissionLocation.EXTERNAL
560558
: PresentationSubmissionLocation.PRESENTATION);
561559

@@ -651,10 +649,10 @@ export class EvaluationClientWrapper {
651649
const vcs = matchingVp.vcs as WrappedMdocCredential[];
652650
vcPath += ` with nested mdoc with doctype ${descriptor.id}`;
653651

654-
const matchingVc = vcs.find((vc) => descriptor.id === vc.credential.docType.asStr);
652+
const matchingVc = vcs.find((vc) => descriptor.id === vc.credential.docType);
655653

656654
if (!matchingVc) {
657-
const allDoctypes = vcs.map((vc) => `'${vc.credential.docType.asStr}'`).join(', ');
655+
const allDoctypes = vcs.map((vc) => `'${vc.credential.docType}'`).join(', ');
658656
result.areRequiredCredentialsPresent = Status.ERROR;
659657
result.errors?.push({
660658
status: Status.ERROR,
@@ -691,7 +689,7 @@ export class EvaluationClientWrapper {
691689

692690
// Determine holder DIDs
693691
const holderDIDs =
694-
CredentialMapper.isW3cPresentation(matchingVp.presentation) && matchingVp.presentation.holder
692+
PexCredentialMapper.isW3cPresentation(matchingVp.presentation) && matchingVp.presentation.holder
695693
? [matchingVp.presentation.holder]
696694
: opts?.holderDIDs || [];
697695

lib/evaluation/handlers/abstractEvaluationHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { JSONPath as jp } from '@astronautlabs/jsonpath';
22
import { InputDescriptorV1, InputDescriptorV2, PresentationSubmission } from '@sphereon/pex-models';
3-
import { WrappedVerifiableCredential } from '@sphereon/ssi-types';
43

54
import { Status } from '../../ConstraintUtils';
65
import { IInternalPresentationDefinition } from '../../types';
6+
import { WrappedVerifiableCredential } from '../../types/PexCredentialMapper';
77
import { HandlerCheckResult } from '../core';
88
import { EvaluationClient } from '../evaluationClient';
99

lib/evaluation/handlers/didRestrictionEvaluationHandler.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { CredentialMapper, WrappedVerifiableCredential } from '@sphereon/ssi-types';
2-
31
import { Status } from '../../ConstraintUtils';
42
import { IInternalPresentationDefinition, InternalPresentationDefinitionV1, InternalPresentationDefinitionV2 } from '../../types';
53
import PexMessages from '../../types/Messages';
4+
import { PexCredentialMapper, WrappedVerifiableCredential } from '../../types/PexCredentialMapper';
65
import { isRestrictedDID } from '../../utils';
76
import { HandlerCheckResult } from '../core';
87
import { EvaluationClient } from '../evaluationClient';
@@ -39,13 +38,13 @@ export class DIDRestrictionEvaluationHandler extends AbstractEvaluationHandler {
3938
}
4039

4140
private getIssuerIdFromWrappedVerifiableCredential(wrappedVc: WrappedVerifiableCredential) {
42-
if (CredentialMapper.isW3cCredential(wrappedVc.credential)) {
41+
if (PexCredentialMapper.isW3cCredential(wrappedVc.credential)) {
4342
return typeof wrappedVc.credential.issuer === 'object' ? wrappedVc.credential.issuer.id : wrappedVc.credential.issuer;
44-
} else if (CredentialMapper.isSdJwtDecodedCredential(wrappedVc.credential)) {
43+
} else if (PexCredentialMapper.isSdJwtDecodedCredential(wrappedVc.credential)) {
4544
return wrappedVc.credential.decodedPayload.iss;
4645
}
4746
// mdoc is not bound to did
48-
else if (CredentialMapper.isWrappedMdocCredential(wrappedVc)) {
47+
else if (PexCredentialMapper.isWrappedMdocCredential(wrappedVc)) {
4948
return undefined;
5049
}
5150
throw new Error('Unsupported credential type');

lib/evaluation/handlers/evaluationHandler.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { WrappedVerifiableCredential } from '@sphereon/ssi-types';
2-
31
import { IInternalPresentationDefinition } from '../../types/Internal.types';
2+
import { WrappedVerifiableCredential } from '../../types/PexCredentialMapper';
43
import { EvaluationClient } from '../evaluationClient';
54

65
export interface EvaluationHandler {

0 commit comments

Comments
 (0)