@dwn-protocol/id-sdk 0.2.5 → 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (99) hide show
  1. package/package.json +2 -3
  2. package/src/agent/app-data-store.ts +0 -365
  3. package/src/agent/did-manager.ts +0 -393
  4. package/src/agent/dwn-manager.ts +0 -548
  5. package/src/agent/identity-manager.ts +0 -165
  6. package/src/agent/index.ts +0 -19
  7. package/src/agent/json-rpc.ts +0 -107
  8. package/src/agent/key-manager.ts +0 -302
  9. package/src/agent/kms-local.ts +0 -412
  10. package/src/agent/outbox.ts +0 -128
  11. package/src/agent/rpc-client.ts +0 -223
  12. package/src/agent/store-managed-did.ts +0 -295
  13. package/src/agent/store-managed-identity.ts +0 -243
  14. package/src/agent/store-managed-key.ts +0 -754
  15. package/src/agent/sync-manager.ts +0 -631
  16. package/src/agent/test-managed-agent.ts +0 -299
  17. package/src/agent/types/agent.ts +0 -145
  18. package/src/agent/types/managed-key.ts +0 -442
  19. package/src/agent/utils.ts +0 -190
  20. package/src/common/convert.ts +0 -424
  21. package/src/common/index.ts +0 -9
  22. package/src/common/multicodec.ts +0 -176
  23. package/src/common/object.ts +0 -43
  24. package/src/common/stores.ts +0 -125
  25. package/src/common/stream-node.ts +0 -381
  26. package/src/common/stream.ts +0 -406
  27. package/src/common/type-utils.ts +0 -117
  28. package/src/common/types.ts +0 -48
  29. package/src/credentials/credential-bbs.ts +0 -419
  30. package/src/credentials/credential.ts +0 -324
  31. package/src/credentials/index.ts +0 -5
  32. package/src/credentials/presentation.ts +0 -182
  33. package/src/credentials/status-list.ts +0 -365
  34. package/src/credentials/utils.ts +0 -58
  35. package/src/credentials/validators.ts +0 -52
  36. package/src/crypto/algorithms-api/aes/base.ts +0 -49
  37. package/src/crypto/algorithms-api/aes/ctr.ts +0 -51
  38. package/src/crypto/algorithms-api/aes/index.ts +0 -2
  39. package/src/crypto/algorithms-api/crypto-algorithm.ts +0 -127
  40. package/src/crypto/algorithms-api/crypto-key.ts +0 -56
  41. package/src/crypto/algorithms-api/ec/base.ts +0 -39
  42. package/src/crypto/algorithms-api/ec/ecdh.ts +0 -53
  43. package/src/crypto/algorithms-api/ec/ecdsa.ts +0 -37
  44. package/src/crypto/algorithms-api/ec/eddsa.ts +0 -30
  45. package/src/crypto/algorithms-api/ec/index.ts +0 -4
  46. package/src/crypto/algorithms-api/errors.ts +0 -29
  47. package/src/crypto/algorithms-api/index.ts +0 -6
  48. package/src/crypto/algorithms-api/pbkdf/index.ts +0 -1
  49. package/src/crypto/algorithms-api/pbkdf/pbkdf2.ts +0 -91
  50. package/src/crypto/crypto-algorithms/aes-ctr.ts +0 -70
  51. package/src/crypto/crypto-algorithms/bbs.ts +0 -110
  52. package/src/crypto/crypto-algorithms/ecdh.ts +0 -115
  53. package/src/crypto/crypto-algorithms/ecdsa.ts +0 -111
  54. package/src/crypto/crypto-algorithms/eddsa.ts +0 -110
  55. package/src/crypto/crypto-algorithms/index.ts +0 -6
  56. package/src/crypto/crypto-algorithms/pbkdf2.ts +0 -54
  57. package/src/crypto/crypto-primitives/aes-ctr.ts +0 -131
  58. package/src/crypto/crypto-primitives/aes-gcm.ts +0 -138
  59. package/src/crypto/crypto-primitives/bbs.ts +0 -183
  60. package/src/crypto/crypto-primitives/concat-kdf.ts +0 -207
  61. package/src/crypto/crypto-primitives/ed25519.ts +0 -201
  62. package/src/crypto/crypto-primitives/index.ts +0 -10
  63. package/src/crypto/crypto-primitives/pbkdf2.ts +0 -78
  64. package/src/crypto/crypto-primitives/secp256k1.ts +0 -322
  65. package/src/crypto/crypto-primitives/x25519.ts +0 -101
  66. package/src/crypto/crypto-primitives/xchacha20-poly1305.ts +0 -46
  67. package/src/crypto/crypto-primitives/xchacha20.ts +0 -34
  68. package/src/crypto/index.ts +0 -8
  69. package/src/crypto/jose.ts +0 -948
  70. package/src/crypto/types/crypto-key.ts +0 -4
  71. package/src/crypto/types/iddwn-crypto.ts +0 -119
  72. package/src/crypto/utils.ts +0 -200
  73. package/src/did-api.ts +0 -72
  74. package/src/dids/dht.ts +0 -412
  75. package/src/dids/did-dht.ts +0 -436
  76. package/src/dids/did-ion.ts +0 -613
  77. package/src/dids/did-key.ts +0 -791
  78. package/src/dids/did-resolver.ts +0 -107
  79. package/src/dids/index.ts +0 -9
  80. package/src/dids/resolver-cache-level.ts +0 -82
  81. package/src/dids/resolver-cache-noop.ts +0 -25
  82. package/src/dids/types.ts +0 -278
  83. package/src/dids/utils.ts +0 -129
  84. package/src/dwn-api.ts +0 -584
  85. package/src/iddwn.ts +0 -241
  86. package/src/identity-agent/index.ts +0 -270
  87. package/src/index.ts +0 -26
  88. package/src/interfaces/metadata.ts +0 -163
  89. package/src/interfaces/queue.ts +0 -108
  90. package/src/interfaces/services.ts +0 -122
  91. package/src/interfaces/transactions.ts +0 -220
  92. package/src/protocol.ts +0 -68
  93. package/src/proxy-agent/index.ts +0 -255
  94. package/src/record.ts +0 -521
  95. package/src/service-options.ts +0 -62
  96. package/src/typings/decentralized-identity__ion-pow-sdk.d.ts +0 -7
  97. package/src/user-agent/index.ts +0 -295
  98. package/src/utils.ts +0 -29
  99. package/src/vc-api.ts +0 -505
@@ -1,324 +0,0 @@
1
- import type { Resolvable, DIDResolutionResult} from 'did-resolver';
2
- import type {
3
- ICredential,
4
- ICredentialSubject,
5
- JwtDecodedVerifiableCredential,
6
- } from '@sphereon/ssi-types';
7
-
8
- import { v4 as uuidv4 } from 'uuid';
9
- import { getCurrentXmlSchema112Timestamp } from './utils.js';
10
- import { Convert } from '../common/index.js';
11
- import { verifyJWT } from 'did-jwt';
12
- import { DidDhtMethod, DidIonMethod, DidKeyMethod, DidResolver } from '../dids/index.js';
13
- import { VcValidator } from './validators.js';
14
-
15
- export const DEFAULT_CONTEXT = 'https://www.w3.org/2018/credentials/v1';
16
- export const DEFAULT_VC_TYPE = 'VerifiableCredential';
17
- export const VC_DATA_FORMAT = 'application/vc+jwt';
18
-
19
- /**
20
- * A Verifiable Credential is a set of one or more claims made by the same entity.
21
- *
22
- * @see {@link https://www.w3.org/TR/vc-data-model/#credentials | VC Data Model}
23
- */
24
- export type VcDataModel = ICredential;
25
-
26
- /**
27
- * @param type Optional. The type of the credential, can be a string or an array of strings.
28
- * @param issuer The issuer URI of the credential, as a string.
29
- * @param subject The subject URI of the credential, as a string.
30
- * @param data The credential data, as a generic type any.
31
- * @param issuanceDate Optional. The issuance date of the credential, as a string.
32
- * Defaults to the current date if not specified.
33
- * @param expirationDate Optional. The expiration date of the credential, as a string.
34
- * @param revocable Optional. Whether the credential should be revocable (adds credentialStatus).
35
- * @param suspendable Optional. Whether the credential should be suspendable (adds credentialStatus).
36
- * @param statusListCredentialId Optional. ID of existing status list credential to link to.
37
- */
38
- export type VerifiableCredentialCreateOptions = {
39
- type?: string | string[];
40
- issuer: string;
41
- subject: string;
42
- data: any;
43
- issuanceDate?: string;
44
- expirationDate?: string;
45
- revocable?: boolean;
46
- suspendable?: boolean;
47
- statusListCredentialId?: string;
48
- };
49
-
50
- export type SignOptions = {
51
- kid: string;
52
- issuerDid: string;
53
- subjectDid: string;
54
- signer: Signer,
55
- }
56
-
57
- export type Signer = (data: Uint8Array) => Promise<Uint8Array>;
58
-
59
- type CredentialSubject = ICredentialSubject;
60
-
61
- type JwtHeaderParams = {
62
- alg: string;
63
- typ: 'JWT'
64
- kid: string;
65
- };
66
-
67
- export type DecodedVcJwt = {
68
- header: JwtHeaderParams
69
- payload: JwtDecodedVerifiableCredential,
70
- signature: string
71
- }
72
-
73
- const didResolver = new DidResolver({ didResolvers: [DidIonMethod, DidKeyMethod, DidDhtMethod] });
74
-
75
- class DwnResolver implements Resolvable {
76
- async resolve(didUrl: string): Promise<DIDResolutionResult> {
77
- return await didResolver.resolve(didUrl) as any; // DIDResolutionResult
78
- }
79
- }
80
-
81
- const dwnResolver = new DwnResolver();
82
-
83
- /**
84
- * `VerifiableCredential` represents a digital verifiable credential according to the
85
- * [W3C Verifiable Credentials Data Model](https://www.w3.org/TR/vc-data-model/).
86
- *
87
- * It provides functionalities to sign, verify, and create credentials, offering a concise API to
88
- * work with JWT representations of verifiable credentials and ensuring that the signatures
89
- * and claims within those JWTs can be validated.
90
- *
91
- * @property vcDataModel The [VcDataModel] instance representing the core data model of a verifiable credential.
92
- */
93
- export class VerifiableCredential {
94
- constructor(public vcDataModel: VcDataModel) {}
95
-
96
- get type(): string {
97
- return this.vcDataModel.type[this.vcDataModel.type.length - 1];
98
- }
99
-
100
- get issuer(): string {
101
- return this.vcDataModel.issuer.toString();
102
- }
103
-
104
- get subject(): string {
105
- if (Array.isArray(this.vcDataModel.credentialSubject)) {
106
- return this.vcDataModel.credentialSubject[0].id!;
107
- } else {
108
- return this.vcDataModel.credentialSubject.id!;
109
- }
110
- }
111
-
112
- /**
113
- * Sign a verifiable credential using [signOptions]
114
- *
115
- *
116
- * @param signOptions The sign options used to sign the credential.
117
- * @return The JWT representing the signed verifiable credential.
118
- *
119
- * Example:
120
- * ```
121
- * const signedVc = verifiableCredential.sign(signOptions)
122
- * ```
123
- */
124
- // TODO: Refactor to look like: sign(did: Did, assertionMethodId?: string)
125
- public async sign(signOptions: SignOptions): Promise<string> {
126
- const vcJwt: string = await createJwt({ vc: this.vcDataModel }, signOptions);
127
- return vcJwt;
128
- }
129
-
130
- /**
131
- * Converts the current object to its JSON representation.
132
- *
133
- * @return The JSON representation of the object.
134
- */
135
- public toString(): string {
136
- return JSON.stringify(this.vcDataModel);
137
- }
138
-
139
- /**
140
- * Create a [VerifiableCredential] based on the provided parameters.
141
- *
142
- * @param vcCreateOptions The options to use when creating the Verifiable Credential.
143
- * @return A [VerifiableCredential] instance.
144
- *
145
- * Example:
146
- * ```
147
- * const vc = VerifiableCredential.create({
148
- * issuer: 'did:ex:issuer',
149
- * subject: 'did:ex:subject',
150
- * data: { 'arbitrary': 'data' }
151
- * type: 'ChiaCredential',
152
- * })
153
- * ```
154
- */
155
- public static create(vcCreateOptions: VerifiableCredentialCreateOptions): VerifiableCredential {
156
- const { type, issuer, subject, data, issuanceDate, expirationDate, revocable, suspendable, statusListCredentialId } = vcCreateOptions;
157
-
158
- const jsonData = JSON.parse(JSON.stringify(data));
159
-
160
- if (typeof jsonData !== 'object') {
161
- throw new Error('Expected data to be parseable into a JSON object');
162
- }
163
-
164
- if(!issuer || !subject) {
165
- throw new Error('Issuer and subject must be defined');
166
- }
167
-
168
- const credentialSubject: CredentialSubject = {
169
- id: subject,
170
- ...jsonData
171
- };
172
-
173
- const vcDataModel: VcDataModel = {
174
- '@context' : [DEFAULT_CONTEXT],
175
- type : Array.isArray(type)
176
- ? [DEFAULT_VC_TYPE, ...type]
177
- : (type ? [DEFAULT_VC_TYPE, type] : [DEFAULT_VC_TYPE]),
178
- id : `urn:uuid:${uuidv4()}`,
179
- issuer : issuer,
180
- issuanceDate : issuanceDate || getCurrentXmlSchema112Timestamp(), // use default if undefined
181
- credentialSubject : credentialSubject,
182
- ...(expirationDate && { expirationDate }), // optional property
183
- };
184
-
185
- // Note: credentialStatus is added by StatusListManager.addCredentialStatus()
186
- // This is handled at a higher level when creating revocable credentials
187
- // The revocable/suspendable flags are stored here for reference but
188
- // actual credentialStatus linking happens via VcApi.createRevocableCredential()
189
-
190
- validatePayload(vcDataModel);
191
- return new VerifiableCredential(vcDataModel);
192
- }
193
-
194
- /**
195
- * Verifies the integrity and authenticity of a Verifiable Credential (VC) encoded as a JSON Web Token (JWT).
196
- *
197
- * This function performs several crucial validation steps to ensure the trustworthiness of the provided VC:
198
- * - Parses and validates the structure of the JWT.
199
- * - Ensures the presence of critical header elements `alg` and `kid` in the JWT header.
200
- * - Resolves the Decentralized Identifier (DID) and retrieves the associated DID Document.
201
- * - Validates the DID and establishes a set of valid verification method IDs.
202
- * - Identifies the correct Verification Method from the DID Document based on the `kid` parameter.
203
- * - Verifies the JWT's signature using the public key associated with the Verification Method.
204
- *
205
- * If any of these steps fail, the function will throw a [Error] with a message indicating the nature of the failure.
206
- *
207
- * @param vcJwt The Verifiable Credential in JWT format as a [string].
208
- * @throws Error if the verification fails at any step, providing a message with failure details.
209
- * @throws Error if critical JWT header elements are absent.
210
- *
211
- * ### Example:
212
- * ```
213
- * try {
214
- * VerifiableCredential.verify(signedVcJwt)
215
- * console.log("VC Verification successful!")
216
- * } catch (e: Error) {
217
- * console.log("VC Verification failed: ${e.message}")
218
- * }
219
- * ```
220
- */
221
- public static async verify(vcJwt: string): Promise<void> {
222
- const jwt = decodeJwt(vcJwt); // Parse and validate JWT
223
-
224
- // Ensure the presence of critical header elements `alg` and `kid`
225
- if (!jwt.header.alg || !jwt.header.kid) {
226
- throw new Error('Signature verification failed: Expected JWS header to contain alg and kid');
227
- }
228
-
229
- const verificationResponse = await verifyJWT(vcJwt, {
230
- resolver: dwnResolver
231
- });
232
-
233
- if (!verificationResponse.verified) {
234
- throw new Error('VC JWT could not be verified. Reason: ' + JSON.stringify(verificationResponse));
235
- }
236
- }
237
-
238
- /**
239
- * Parses a JWT into a [VerifiableCredential] instance.
240
- *
241
- * @param vcJwt The verifiable credential JWT as a [String].
242
- * @return A [VerifiableCredential] instance derived from the JWT.
243
- *
244
- * Example:
245
- * ```
246
- * val vc = VerifiableCredential.parseJwt(signedVcJwt)
247
- * ```
248
- */
249
- public static parseJwt(vcJwt: string): VerifiableCredential {
250
- const decodedVcJwt: DecodedVcJwt = decodeJwt(vcJwt);
251
- const vcDataModel: VcDataModel = decodedVcJwt.payload.vc;
252
-
253
- if(!vcDataModel) {
254
- throw Error('Jwt payload missing vc property');
255
- }
256
-
257
- return new VerifiableCredential(vcDataModel);
258
- }
259
- }
260
-
261
- /**
262
- * Validates the structure and integrity of a Verifiable Credential payload.
263
- *
264
- * @param vc - The Verifiable Credential object to validate.
265
- * @throws Error if any validation check fails.
266
- */
267
- export function validatePayload(vc: VcDataModel): void {
268
- VcValidator.validateContext(vc['@context']);
269
- VcValidator.validateVcType(vc.type);
270
- VcValidator.validateCredentialSubject(vc.credentialSubject);
271
- if (vc.issuanceDate) VcValidator.validateTimestamp(vc.issuanceDate);
272
- if (vc.expirationDate) VcValidator.validateTimestamp(vc.expirationDate);
273
- }
274
-
275
- /**
276
- * Decodes a VC JWT into its constituent parts: header, payload, and signature.
277
- *
278
- * @param jwt - The JWT string to decode.
279
- * @returns An object containing the decoded header, payload, and signature.
280
- */
281
- export function decodeJwt(jwt: string): DecodedVcJwt {
282
- const [encodedHeader, encodedPayload, encodedSignature] = jwt.split('.');
283
-
284
- if (!encodedHeader || !encodedPayload || !encodedSignature) {
285
- throw Error('Not a valid jwt');
286
- }
287
-
288
- return {
289
- header : Convert.base64Url(encodedHeader).toObject() as JwtHeaderParams,
290
- payload : Convert.base64Url(encodedPayload).toObject() as JwtDecodedVerifiableCredential,
291
- signature : encodedSignature
292
- };
293
- }
294
-
295
- /**
296
- * Create a VC JWT.
297
- *
298
- * @param payload - the payload for the JWT.
299
- * @param signOptions The sign options used to sign the credential.
300
- * @return The JWT representing the signed verifiable credential.
301
- */
302
- export async function createJwt(payload: any, signOptions: SignOptions) {
303
- const { issuerDid, subjectDid, signer, kid } = signOptions;
304
-
305
- const header: JwtHeaderParams = { alg: 'EdDSA', typ: 'JWT', kid: kid };
306
-
307
- const jwtPayload = {
308
- iss : issuerDid,
309
- sub : subjectDid,
310
- ...payload,
311
- };
312
-
313
- const encodedHeader = Convert.object(header).toBase64Url();
314
- const encodedPayload = Convert.object(jwtPayload).toBase64Url();
315
- const message = encodedHeader + '.' + encodedPayload;
316
- const messageBytes = Convert.string(message).toUint8Array();
317
-
318
- const signature = await signer(messageBytes);
319
-
320
- const encodedSignature = Convert.uint8Array(signature).toBase64Url();
321
- const jwt = message + '.' + encodedSignature;
322
-
323
- return jwt;
324
- }
@@ -1,5 +0,0 @@
1
- export * from './credential.js';
2
- export * from './credential-bbs.js';
3
- export * from './presentation.js';
4
- export * from './status-list.js';
5
- export * as utils from './utils.js';
@@ -1,182 +0,0 @@
1
- import type { EvaluationResults, PresentationResult, SelectResults, Validated as PexValidated } from '@sphereon/pex';
2
- import { PEX } from '@sphereon/pex';
3
-
4
- import type { PresentationDefinitionV2 as PexPresDefV2 } from '@sphereon/pex-models';
5
-
6
- import type {
7
- IPresentation,
8
- PresentationSubmission,
9
- JwtDecodedVerifiablePresentation
10
-
11
- } from '@sphereon/ssi-types';
12
-
13
- export type Validated = PexValidated;
14
- export type PresentationDefinitionV2 = PexPresDefV2
15
-
16
- export type JwtHeaderParams = {
17
- alg: string;
18
- typ: 'JWT'
19
- kid: string;
20
- };
21
-
22
- export type DecodedVpJwt = {
23
- header: JwtHeaderParams
24
- payload: JwtDecodedVerifiablePresentation,
25
- signature: string
26
- }
27
-
28
- export class PresentationExchange {
29
- /**
30
- * The Presentation Exchange (PEX) Library implements the functionality described in the DIF Presentation Exchange specification
31
- */
32
- private static pex: PEX = new PEX();
33
-
34
- /**
35
- * Selects credentials that satisfy a given presentation definition.
36
- *
37
- * @param {string[]} vcJwts The list of Verifiable Credentials to select from.
38
- * @param {PresentationDefinitionV2} presentationDefinition The Presentation Definition to match against.
39
- * @return {string[]} selectedVcJwts A list of Verifiable Credentials that satisfy the Presentation Definition.
40
- */
41
- public static selectCredentials(
42
- vcJwts: string[],
43
- presentationDefinition: PresentationDefinitionV2
44
- ): string[] {
45
- this.resetPex();
46
- const selectResults: SelectResults = this.pex.selectFrom(presentationDefinition, vcJwts);
47
- return selectResults.verifiableCredential as string[] ?? [];
48
- }
49
-
50
- /**
51
- * Validates if a list of VC JWTs satisfies the given presentation definition.
52
- *
53
- * @param {string[]} vcJwts An array of VC JWTs as strings.
54
- * @param {PresentationDefinitionV2} presentationDefinition The criteria to validate against.
55
- * @throws {Error} If the evaluation results in warnings or errors.
56
- */
57
- public static satisfiesPresentationDefinition(
58
- vcJwts: string[],
59
- presentationDefinition: PresentationDefinitionV2
60
- ): void {
61
- this.resetPex();
62
- const evaluationResults: EvaluationResults = this.pex.evaluateCredentials(presentationDefinition, vcJwts);
63
- if (evaluationResults.warnings?.length) {
64
- console.warn('Warnings were generated during the evaluation process: ' + JSON.stringify(evaluationResults.warnings));
65
- }
66
-
67
- if (evaluationResults.areRequiredCredentialsPresent.toString() !== 'info' || evaluationResults.errors?.length) {
68
- let errorMessage = 'Failed to create Verifiable Presentation JWT due to: ';
69
- if(evaluationResults.areRequiredCredentialsPresent) {
70
- errorMessage += 'Required Credentials Not Present: ' + JSON.stringify(evaluationResults.areRequiredCredentialsPresent);
71
- }
72
-
73
- if (evaluationResults.errors?.length) {
74
- errorMessage += 'Errors: ' + JSON.stringify(evaluationResults.errors);
75
- }
76
-
77
- throw new Error(errorMessage);
78
- }
79
- }
80
-
81
- /**
82
- * Creates a presentation from a list of Verifiable Credentials that satisfy a given presentation definition.
83
- * This function initializes the Presentation Exchange (PEX) process, validates the presentation definition,
84
- * evaluates the credentials against the definition, and finally constructs the presentation result if the
85
- * evaluation is successful.
86
- *
87
- * @param {string[]} vcJwts The list of Verifiable Credentials (VCs) in JWT format to be evaluated.
88
- * @param {PresentationDefinitionV2} presentationDefinition The Presentation Definition V2 to match the VCs against.
89
- * @returns {PresentationResult} The result of the presentation creation process, containing a presentation submission
90
- * that satisfies the presentation definition criteria.
91
- * @throws {Error} If the evaluation results in warnings or errors, or if the required credentials are not present,
92
- * an error is thrown with a descriptive message.
93
- */
94
- public static createPresentationFromCredentials(
95
- vcJwts: string[],
96
- presentationDefinition: PresentationDefinitionV2
97
- ): PresentationResult {
98
- this.resetPex();
99
-
100
- const pdValidated: Validated = PEX.validateDefinition(presentationDefinition);
101
- isValid(pdValidated);
102
-
103
- const evaluationResults: EvaluationResults = this.pex.evaluateCredentials(presentationDefinition, vcJwts);
104
-
105
- if (evaluationResults.warnings?.length) {
106
- console.warn('Warnings were generated during the evaluation process: ' + JSON.stringify(evaluationResults.warnings));
107
- }
108
-
109
- if (evaluationResults.areRequiredCredentialsPresent.toString() !== 'info' || evaluationResults.errors?.length) {
110
- let errorMessage = 'Failed to create Verifiable Presentation JWT due to: ';
111
- if(evaluationResults.areRequiredCredentialsPresent) {
112
- errorMessage += 'Required Credentials Not Present: ' + JSON.stringify(evaluationResults.areRequiredCredentialsPresent);
113
- }
114
-
115
- if (evaluationResults.errors?.length) {
116
- errorMessage += 'Errors: ' + JSON.stringify(evaluationResults.errors);
117
- }
118
-
119
- throw new Error(errorMessage);
120
- }
121
-
122
- const presentationResult: PresentationResult = this.pex.presentationFrom(presentationDefinition, vcJwts);
123
-
124
- const submissionValidated: Validated = PEX.validateSubmission(presentationResult.presentationSubmission);
125
- isValid(submissionValidated);
126
-
127
- return presentationResult;
128
- }
129
-
130
- /**
131
- * This method validates whether an object is usable as a presentation definition or not.
132
- *
133
- * @param {PresentationDefinitionV2} presentationDefinition: presentationDefinition to be validated.
134
- *
135
- * @return {Validated} the validation results to reveal what is acceptable/unacceptable about the passed object to be considered a valid presentation definition
136
- */
137
- public static validateDefinition(presentationDefinition: PresentationDefinitionV2): Validated {
138
- return PEX.validateDefinition(presentationDefinition);
139
- }
140
-
141
- /**
142
- * This method validates whether an object is usable as a presentation submission or not.
143
- *
144
- * @param {PresentationSubmission} presentationSubmission the object to be validated.
145
- *
146
- * @return {Validated} the validation results to reveal what is acceptable/unacceptable about the passed object to be considered a valid presentation submission
147
- */
148
- public static validateSubmission(presentationSubmission: PresentationSubmission): Validated {
149
- return PEX.validateSubmission(presentationSubmission);
150
- }
151
-
152
- /**
153
- * Evaluates a presentation against a presentation definition.
154
- * @returns {EvaluationResults} The result of the evaluation process.
155
- */
156
- public static evaluatePresentation(
157
- presentationDefinition: PresentationDefinitionV2,
158
- presentation: IPresentation
159
- ): EvaluationResults {
160
- this.resetPex();
161
- return this.pex.evaluatePresentation(presentationDefinition, presentation);
162
- }
163
-
164
- private static resetPex() {
165
- this.pex = new PEX();
166
- }
167
- }
168
-
169
- function isValid(validated: Validated) {
170
- let errorMessage = 'Failed to pass validation check due to: ';
171
- if (Array.isArray(validated)) {
172
- if (!validated.every(item => item.status === 'info')) {
173
- errorMessage += 'Validation Errors: ' + JSON.stringify(validated);
174
- throw new Error(errorMessage);
175
- }
176
- } else {
177
- if (validated.status !== 'info') {
178
- errorMessage += 'Validation Errors: ' + JSON.stringify(validated);
179
- throw new Error(errorMessage);
180
- }
181
- }
182
- }