@sphereon/ssi-types 0.33.1-next.2 → 0.33.1-next.68

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 (122) hide show
  1. package/dist/index.cjs +1773 -0
  2. package/dist/index.cjs.map +1 -0
  3. package/dist/index.d.cts +1589 -0
  4. package/dist/index.d.ts +1589 -6
  5. package/dist/index.js +1741 -24
  6. package/dist/index.js.map +1 -1
  7. package/package.json +23 -14
  8. package/src/index.ts +3 -2
  9. package/src/logging/index.ts +6 -4
  10. package/src/mapper/credential-constraints.ts +14 -1
  11. package/src/mapper/credential-mapper.ts +61 -23
  12. package/src/mapper/index.ts +1 -0
  13. package/src/types/cose.ts +5 -51
  14. package/src/types/did.ts +0 -90
  15. package/src/types/index.ts +13 -13
  16. package/src/types/jose.ts +18 -65
  17. package/src/types/mso_mdoc.ts +10 -138
  18. package/src/types/sd-jwt-vc.ts +4 -178
  19. package/src/types/status-list.ts +1 -8
  20. package/src/types/vc.ts +7 -29
  21. package/src/types/w3c-vc.ts +3 -26
  22. package/src/utils/cose.ts +46 -0
  23. package/src/utils/did.ts +91 -0
  24. package/src/utils/hasher.ts +5 -2
  25. package/src/utils/index.ts +6 -0
  26. package/src/utils/jose.ts +47 -0
  27. package/src/utils/mdoc.ts +144 -0
  28. package/src/utils/sd-jwt.ts +179 -0
  29. package/src/utils/vc.ts +54 -0
  30. package/dist/events/index.d.ts +0 -116
  31. package/dist/events/index.d.ts.map +0 -1
  32. package/dist/events/index.js +0 -165
  33. package/dist/events/index.js.map +0 -1
  34. package/dist/index.d.ts.map +0 -1
  35. package/dist/logging/index.d.ts +0 -73
  36. package/dist/logging/index.d.ts.map +0 -1
  37. package/dist/logging/index.js +0 -186
  38. package/dist/logging/index.js.map +0 -1
  39. package/dist/mapper/credential-constraints.d.ts +0 -16
  40. package/dist/mapper/credential-constraints.d.ts.map +0 -1
  41. package/dist/mapper/credential-constraints.js +0 -3
  42. package/dist/mapper/credential-constraints.js.map +0 -1
  43. package/dist/mapper/credential-mapper.d.ts +0 -124
  44. package/dist/mapper/credential-mapper.d.ts.map +0 -1
  45. package/dist/mapper/credential-mapper.js +0 -774
  46. package/dist/mapper/credential-mapper.js.map +0 -1
  47. package/dist/mapper/index.d.ts +0 -3
  48. package/dist/mapper/index.d.ts.map +0 -1
  49. package/dist/mapper/index.js +0 -19
  50. package/dist/mapper/index.js.map +0 -1
  51. package/dist/mapper/jsonld-language-values.d.ts +0 -18
  52. package/dist/mapper/jsonld-language-values.d.ts.map +0 -1
  53. package/dist/mapper/jsonld-language-values.js +0 -79
  54. package/dist/mapper/jsonld-language-values.js.map +0 -1
  55. package/dist/types/cose.d.ts +0 -77
  56. package/dist/types/cose.d.ts.map +0 -1
  57. package/dist/types/cose.js +0 -51
  58. package/dist/types/cose.js.map +0 -1
  59. package/dist/types/dcql.d.ts +0 -33
  60. package/dist/types/dcql.d.ts.map +0 -1
  61. package/dist/types/dcql.js +0 -4
  62. package/dist/types/dcql.js.map +0 -1
  63. package/dist/types/did.d.ts +0 -186
  64. package/dist/types/did.d.ts.map +0 -1
  65. package/dist/types/did.js +0 -78
  66. package/dist/types/did.js.map +0 -1
  67. package/dist/types/generic.d.ts +0 -44
  68. package/dist/types/generic.d.ts.map +0 -1
  69. package/dist/types/generic.js +0 -3
  70. package/dist/types/generic.js.map +0 -1
  71. package/dist/types/index.d.ts +0 -14
  72. package/dist/types/index.d.ts.map +0 -1
  73. package/dist/types/index.js +0 -30
  74. package/dist/types/index.js.map +0 -1
  75. package/dist/types/jose.d.ts +0 -89
  76. package/dist/types/jose.d.ts.map +0 -1
  77. package/dist/types/jose.js +0 -55
  78. package/dist/types/jose.js.map +0 -1
  79. package/dist/types/metadata-types.d.ts +0 -5
  80. package/dist/types/metadata-types.d.ts.map +0 -1
  81. package/dist/types/metadata-types.js +0 -3
  82. package/dist/types/metadata-types.js.map +0 -1
  83. package/dist/types/mso_mdoc.d.ts +0 -94
  84. package/dist/types/mso_mdoc.d.ts.map +0 -1
  85. package/dist/types/mso_mdoc.js +0 -121
  86. package/dist/types/mso_mdoc.js.map +0 -1
  87. package/dist/types/pex.d.ts +0 -36
  88. package/dist/types/pex.d.ts.map +0 -1
  89. package/dist/types/pex.js +0 -3
  90. package/dist/types/pex.js.map +0 -1
  91. package/dist/types/sd-jwt-type-metadata.d.ts +0 -191
  92. package/dist/types/sd-jwt-type-metadata.d.ts.map +0 -1
  93. package/dist/types/sd-jwt-type-metadata.js +0 -3
  94. package/dist/types/sd-jwt-type-metadata.js.map +0 -1
  95. package/dist/types/sd-jwt-vc.d.ts +0 -196
  96. package/dist/types/sd-jwt-vc.d.ts.map +0 -1
  97. package/dist/types/sd-jwt-vc.js +0 -142
  98. package/dist/types/sd-jwt-vc.js.map +0 -1
  99. package/dist/types/status-list.d.ts +0 -10
  100. package/dist/types/status-list.d.ts.map +0 -1
  101. package/dist/types/status-list.js +0 -9
  102. package/dist/types/status-list.js.map +0 -1
  103. package/dist/types/vc.d.ts +0 -28
  104. package/dist/types/vc.d.ts.map +0 -1
  105. package/dist/types/vc.js +0 -28
  106. package/dist/types/vc.js.map +0 -1
  107. package/dist/types/w3c-vc.d.ts +0 -243
  108. package/dist/types/w3c-vc.d.ts.map +0 -1
  109. package/dist/types/w3c-vc.js +0 -26
  110. package/dist/types/w3c-vc.js.map +0 -1
  111. package/dist/utils/hasher.d.ts +0 -4
  112. package/dist/utils/hasher.d.ts.map +0 -1
  113. package/dist/utils/hasher.js +0 -40
  114. package/dist/utils/hasher.js.map +0 -1
  115. package/dist/utils/index.d.ts +0 -3
  116. package/dist/utils/index.d.ts.map +0 -1
  117. package/dist/utils/index.js +0 -19
  118. package/dist/utils/index.js.map +0 -1
  119. package/dist/utils/object.d.ts +0 -8
  120. package/dist/utils/object.d.ts.map +0 -1
  121. package/dist/utils/object.js +0 -28
  122. package/dist/utils/object.js.map +0 -1
@@ -1,774 +0,0 @@
1
- "use strict";
2
- var __rest = (this && this.__rest) || function (s, e) {
3
- var t = {};
4
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
5
- t[p] = s[p];
6
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
7
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
8
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
9
- t[p[i]] = s[p[i]];
10
- }
11
- return t;
12
- };
13
- Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.CredentialMapper = exports.sha256 = void 0;
15
- const types_1 = require("../types");
16
- const utils_1 = require("../utils");
17
- const jwt_decode_1 = require("jwt-decode");
18
- const sha256 = (data) => {
19
- return (0, utils_1.defaultHasher)(data, 'sha256');
20
- };
21
- exports.sha256 = sha256;
22
- class CredentialMapper {
23
- /**
24
- * Decodes a compact SD-JWT vc to it's decoded variant. This method can be used when the hasher implementation used is Async, and therefore not suitable for usage
25
- * with the other decode methods.
26
- */
27
- static decodeSdJwtVcAsync(compactSdJwtVc, hasher) {
28
- return (0, types_1.decodeSdJwtVcAsync)(compactSdJwtVc, hasher !== null && hasher !== void 0 ? hasher : exports.sha256);
29
- }
30
- /**
31
- * Decodes a Verifiable Presentation to a uniform format.
32
- *
33
- * When decoding SD-JWT credentials, a hasher implementation must be provided. The hasher implementation must be sync. When using
34
- * an async hasher implementation, use the decodeSdJwtVcAsync method instead and you can provide the decoded payload to methods
35
- * instead of the compact SD-JWT.
36
- *
37
- * @param presentation
38
- * @param hasher Hasher implementation to use for SD-JWT decoding.
39
- */
40
- static decodeVerifiablePresentation(presentation, hasher) {
41
- var _a;
42
- if (CredentialMapper.isJwtEncoded(presentation)) {
43
- const payload = (0, jwt_decode_1.jwtDecode)(presentation);
44
- const header = (0, jwt_decode_1.jwtDecode)(presentation, { header: true });
45
- payload.vp.proof = {
46
- type: types_1.IProofType.JwtProof2020,
47
- created: payload.nbf,
48
- proofPurpose: types_1.IProofPurpose.authentication,
49
- verificationMethod: (_a = header['kid']) !== null && _a !== void 0 ? _a : payload.iss,
50
- jwt: presentation,
51
- };
52
- return payload;
53
- }
54
- else if (CredentialMapper.isJwtDecodedPresentation(presentation)) {
55
- return presentation;
56
- }
57
- else if (CredentialMapper.isSdJwtEncoded(presentation)) {
58
- return (0, types_1.decodeSdJwtVc)(presentation, hasher !== null && hasher !== void 0 ? hasher : exports.sha256);
59
- }
60
- else if (CredentialMapper.isSdJwtDecodedCredential(presentation)) {
61
- return presentation;
62
- }
63
- else if (CredentialMapper.isMsoMdocOid4VPEncoded(presentation)) {
64
- return presentation;
65
- }
66
- else if (CredentialMapper.isMsoMdocDecodedPresentation(presentation)) {
67
- return presentation;
68
- }
69
- else if (CredentialMapper.isJsonLdAsString(presentation)) {
70
- return JSON.parse(presentation);
71
- }
72
- else {
73
- return presentation;
74
- }
75
- }
76
- /**
77
- * Decodes a Verifiable Credential to a uniform format.
78
- *
79
- * When decoding SD-JWT credentials, a hasher implementation must be provided. The hasher implementation must be sync. When using
80
- * an async hasher implementation, use the decodeSdJwtVcAsync method instead and you can provide the decoded payload to methods
81
- * instead of the compact SD-JWT.
82
- *
83
- * @param hasher Hasher implementation to use for SD-JWT decoding
84
- */
85
- static decodeVerifiableCredential(credential, hasher) {
86
- var _a;
87
- if (CredentialMapper.isJwtEncoded(credential)) {
88
- const payload = (0, jwt_decode_1.jwtDecode)(credential);
89
- const header = (0, jwt_decode_1.jwtDecode)(credential, { header: true });
90
- payload.vc.proof = {
91
- type: types_1.IProofType.JwtProof2020,
92
- created: payload.nbf,
93
- proofPurpose: types_1.IProofPurpose.authentication,
94
- verificationMethod: (_a = header['kid']) !== null && _a !== void 0 ? _a : payload.iss,
95
- jwt: credential,
96
- };
97
- return payload;
98
- }
99
- else if (CredentialMapper.isJwtDecodedCredential(credential)) {
100
- return credential;
101
- }
102
- else if (CredentialMapper.isJsonLdAsString(credential)) {
103
- return JSON.parse(credential);
104
- }
105
- else if (CredentialMapper.isSdJwtEncoded(credential)) {
106
- return (0, types_1.decodeSdJwtVc)(credential, hasher !== null && hasher !== void 0 ? hasher : exports.sha256);
107
- }
108
- else if (CredentialMapper.isSdJwtDecodedCredential(credential)) {
109
- return credential;
110
- }
111
- else {
112
- return credential;
113
- }
114
- }
115
- /**
116
- * Converts a presentation to a wrapped presentation.
117
- *
118
- * When decoding SD-JWT credentials, a hasher implementation must be provided. The hasher implementation must be sync. When using
119
- * an async hasher implementation, use the decodeSdJwtVcAsync method instead and you can provide the decoded payload to methods
120
- * instead of the compact SD-JWT.
121
- *
122
- * @param hasher Hasher implementation to use for SD-JWT decoding
123
- */
124
- static toWrappedVerifiablePresentation(originalPresentation, opts) {
125
- var _a, _b, _c;
126
- // MSO_MDOC
127
- if (CredentialMapper.isMsoMdocDecodedPresentation(originalPresentation) || CredentialMapper.isMsoMdocOid4VPEncoded(originalPresentation)) {
128
- let deviceResponse;
129
- let originalType;
130
- if (CredentialMapper.isMsoMdocOid4VPEncoded(originalPresentation)) {
131
- deviceResponse = (0, types_1.decodeMdocDeviceResponse)(originalPresentation);
132
- originalType = types_1.OriginalType.MSO_MDOC_ENCODED;
133
- }
134
- else {
135
- deviceResponse = originalPresentation;
136
- originalType = types_1.OriginalType.MSO_MDOC_DECODED;
137
- }
138
- const mdocCredentials = (_a = deviceResponse.documents) === null || _a === void 0 ? void 0 : _a.map((doc) => CredentialMapper.toWrappedVerifiableCredential(doc, opts));
139
- if (!mdocCredentials || mdocCredentials.length === 0) {
140
- throw new Error('could not extract any mdoc credentials from mdoc device response');
141
- }
142
- return {
143
- type: originalType,
144
- format: 'mso_mdoc',
145
- original: originalPresentation,
146
- presentation: deviceResponse,
147
- decoded: deviceResponse,
148
- vcs: mdocCredentials,
149
- };
150
- }
151
- // SD-JWT
152
- if (CredentialMapper.isSdJwtDecodedCredential(originalPresentation) || CredentialMapper.isSdJwtEncoded(originalPresentation)) {
153
- let decodedPresentation;
154
- if (CredentialMapper.isSdJwtEncoded(originalPresentation)) {
155
- decodedPresentation = (0, types_1.decodeSdJwtVc)(originalPresentation, (_b = opts === null || opts === void 0 ? void 0 : opts.hasher) !== null && _b !== void 0 ? _b : exports.sha256);
156
- }
157
- else {
158
- decodedPresentation = originalPresentation;
159
- }
160
- return {
161
- type: CredentialMapper.isSdJwtDecodedCredential(originalPresentation) ? types_1.OriginalType.SD_JWT_VC_DECODED : types_1.OriginalType.SD_JWT_VC_ENCODED,
162
- format: 'vc+sd-jwt',
163
- original: originalPresentation,
164
- presentation: decodedPresentation,
165
- decoded: decodedPresentation.decodedPayload,
166
- // NOTE: we also include the SD-JWT VC as the VC, as the SD-JWT acts as both the VC and the VP
167
- vcs: [CredentialMapper.toWrappedVerifiableCredential(originalPresentation, opts)],
168
- };
169
- }
170
- // If the VP is not an encoded/decoded SD-JWT, we assume it will be a W3C VC
171
- const proof = CredentialMapper.getFirstProof(originalPresentation);
172
- const original = typeof originalPresentation !== 'string' && CredentialMapper.hasJWTProofType(originalPresentation) ? proof === null || proof === void 0 ? void 0 : proof.jwt : originalPresentation;
173
- if (!original) {
174
- throw Error('Could not determine original presentation, probably it was a converted JWT presentation, that is now missing the JWT value in the proof');
175
- }
176
- const decoded = CredentialMapper.decodeVerifiablePresentation(original);
177
- const isJwtEncoded = CredentialMapper.isJwtEncoded(original);
178
- const isJwtDecoded = CredentialMapper.isJwtDecodedPresentation(original);
179
- const type = isJwtEncoded ? types_1.OriginalType.JWT_ENCODED : isJwtDecoded ? types_1.OriginalType.JWT_DECODED : types_1.OriginalType.JSONLD;
180
- const format = isJwtDecoded || isJwtEncoded ? 'jwt_vp' : 'ldp_vp';
181
- let vp;
182
- if (isJwtEncoded || isJwtDecoded) {
183
- vp = CredentialMapper.jwtDecodedPresentationToUniformPresentation(decoded, false, opts);
184
- }
185
- else {
186
- vp = decoded;
187
- }
188
- if (!vp) {
189
- throw Error(`VP key not found`);
190
- }
191
- const noVCs = !('verifiableCredential' in vp) || !vp.verifiableCredential || vp.verifiableCredential.length === 0;
192
- if (noVCs) {
193
- console.warn(`Presentation without verifiable credentials. That is rare! `);
194
- // throw Error(`VP needs to have at least one verifiable credential at this point`)
195
- }
196
- const vcs = noVCs
197
- ? []
198
- : CredentialMapper.toWrappedVerifiableCredentials((_c = vp.verifiableCredential) !== null && _c !== void 0 ? _c : [] /*.map(value => value.original)*/, opts);
199
- const presentation = Object.assign(Object.assign({}, vp), { verifiableCredential: vcs });
200
- return {
201
- type,
202
- format,
203
- original,
204
- decoded,
205
- presentation,
206
- vcs,
207
- };
208
- }
209
- /**
210
- * Converts a list of credentials to a list of wrapped credentials.
211
- *
212
- * When decoding SD-JWT credentials, a hasher implementation must be provided. The hasher implementation must be sync. When using
213
- * an async hasher implementation, use the decodeSdJwtVcAsync method instead and you can provide the decoded payload to methods
214
- * instead of the compact SD-JWT.
215
- *
216
- * @param hasher Hasher implementation to use for SD-JWT decoding
217
- */
218
- static toWrappedVerifiableCredentials(verifiableCredentials, opts) {
219
- return verifiableCredentials.map((vc) => CredentialMapper.toWrappedVerifiableCredential(vc, opts));
220
- }
221
- /**
222
- * Converts a credential to a wrapped credential.
223
- *
224
- * When decoding SD-JWT credentials, a hasher implementation must be provided. The hasher implementation must be sync. When using
225
- * an async hasher implementation, use the decodeSdJwtVcAsync method instead and you can provide the decoded payload to methods
226
- * instead of the compact SD-JWT.
227
- *
228
- * @param hasher Hasher implementation to use for SD-JWT decoding
229
- */
230
- static toWrappedVerifiableCredential(verifiableCredential, opts) {
231
- var _a, _b;
232
- // MSO_MDOC
233
- if (CredentialMapper.isMsoMdocDecodedCredential(verifiableCredential) || CredentialMapper.isMsoMdocOid4VPEncoded(verifiableCredential)) {
234
- let mdoc;
235
- if (CredentialMapper.isMsoMdocOid4VPEncoded(verifiableCredential)) {
236
- mdoc = (0, types_1.decodeMdocIssuerSigned)(verifiableCredential);
237
- }
238
- else {
239
- mdoc = verifiableCredential;
240
- }
241
- return {
242
- type: CredentialMapper.isMsoMdocDecodedCredential(verifiableCredential) ? types_1.OriginalType.MSO_MDOC_DECODED : types_1.OriginalType.MSO_MDOC_ENCODED,
243
- format: 'mso_mdoc',
244
- original: verifiableCredential,
245
- credential: mdoc,
246
- decoded: (0, types_1.getMdocDecodedPayload)(mdoc),
247
- };
248
- }
249
- // SD-JWT
250
- if (CredentialMapper.isSdJwtDecodedCredential(verifiableCredential) || CredentialMapper.isSdJwtEncoded(verifiableCredential)) {
251
- let decodedCredential;
252
- if (CredentialMapper.isSdJwtEncoded(verifiableCredential)) {
253
- const hasher = (_a = opts === null || opts === void 0 ? void 0 : opts.hasher) !== null && _a !== void 0 ? _a : exports.sha256;
254
- decodedCredential = (0, types_1.decodeSdJwtVc)(verifiableCredential, hasher);
255
- }
256
- else {
257
- decodedCredential = verifiableCredential;
258
- }
259
- return {
260
- type: CredentialMapper.isSdJwtDecodedCredential(verifiableCredential) ? types_1.OriginalType.SD_JWT_VC_DECODED : types_1.OriginalType.SD_JWT_VC_ENCODED,
261
- format: 'vc+sd-jwt',
262
- original: verifiableCredential,
263
- credential: decodedCredential,
264
- decoded: decodedCredential.decodedPayload,
265
- };
266
- }
267
- // If the VC is not an encoded/decoded SD-JWT, we assume it will be a W3C VC
268
- const proof = CredentialMapper.getFirstProof(verifiableCredential);
269
- const original = CredentialMapper.hasJWTProofType(verifiableCredential) && proof ? ((_b = proof.jwt) !== null && _b !== void 0 ? _b : verifiableCredential) : verifiableCredential;
270
- if (!original) {
271
- throw Error('Could not determine original credential, probably it was a converted JWT credential, that is now missing the JWT value in the proof');
272
- }
273
- const decoded = CredentialMapper.decodeVerifiableCredential(original);
274
- const isJwtEncoded = CredentialMapper.isJwtEncoded(original);
275
- const isJwtDecoded = CredentialMapper.isJwtDecodedCredential(original);
276
- const type = isJwtEncoded ? types_1.OriginalType.JWT_ENCODED : isJwtDecoded ? types_1.OriginalType.JWT_DECODED : types_1.OriginalType.JSONLD;
277
- const credential = isJwtEncoded || isJwtDecoded
278
- ? CredentialMapper.jwtDecodedCredentialToUniformCredential(decoded, opts)
279
- : decoded;
280
- const format = isJwtEncoded || isJwtDecoded ? 'jwt_vc' : 'ldp_vc';
281
- return {
282
- original,
283
- decoded,
284
- format,
285
- type,
286
- credential,
287
- };
288
- }
289
- static isJwtEncoded(original) {
290
- return utils_1.ObjectUtils.isString(original) && original.startsWith('ey') && !original.includes('~');
291
- }
292
- static isSdJwtEncoded(original) {
293
- return utils_1.ObjectUtils.isString(original) && original.startsWith('ey') && original.includes('~');
294
- }
295
- static isMsoMdocOid4VPEncoded(original) {
296
- return utils_1.ObjectUtils.isString(original) && !original.startsWith('ey') && utils_1.ObjectUtils.isBase64(original);
297
- }
298
- static isW3cCredential(credential) {
299
- var _a;
300
- return typeof credential === 'object' && '@context' in credential && (((_a = credential.type) === null || _a === void 0 ? void 0 : _a.includes('VerifiableCredential')) || false);
301
- }
302
- static isCredential(original) {
303
- try {
304
- if (CredentialMapper.isJwtEncoded(original)) {
305
- const vc = CredentialMapper.toUniformCredential(original);
306
- return CredentialMapper.isW3cCredential(vc);
307
- }
308
- else if (CredentialMapper.isSdJwtEncoded(original)) {
309
- return true;
310
- }
311
- else if (CredentialMapper.isMsoMdocDecodedCredential(original)) {
312
- return true;
313
- }
314
- else if (CredentialMapper.isMsoMdocOid4VPEncoded(original)) {
315
- return true;
316
- }
317
- return (CredentialMapper.isW3cCredential(original) ||
318
- CredentialMapper.isSdJwtDecodedCredentialPayload(original) ||
319
- CredentialMapper.isJwtDecodedCredential(original) ||
320
- CredentialMapper.isSdJwtDecodedCredential(original));
321
- }
322
- catch (e) {
323
- return false;
324
- }
325
- }
326
- static isPresentation(original) {
327
- try {
328
- if (CredentialMapper.isJwtEncoded(original)) {
329
- const vp = CredentialMapper.toUniformPresentation(original);
330
- return CredentialMapper.isW3cPresentation(vp);
331
- }
332
- else if (CredentialMapper.isSdJwtEncoded(original)) {
333
- return false;
334
- // @ts-expect-error
335
- }
336
- else if (CredentialMapper.isMsoMdocDecodedPresentation(original)) {
337
- return true;
338
- }
339
- else if (CredentialMapper.isMsoMdocOid4VPEncoded(original)) {
340
- return true;
341
- }
342
- return (CredentialMapper.isW3cPresentation(original) ||
343
- CredentialMapper.isSdJwtDecodedCredentialPayload(original) ||
344
- CredentialMapper.isJwtDecodedPresentation(original) ||
345
- CredentialMapper.isSdJwtDecodedCredential(original));
346
- }
347
- catch (e) {
348
- return false;
349
- }
350
- }
351
- static hasProof(original) {
352
- try {
353
- if (CredentialMapper.isMsoMdocOid4VPEncoded(original)) {
354
- return false;
355
- // @ts-ignore
356
- }
357
- else if (CredentialMapper.isMsoMdocDecodedCredential(original) || CredentialMapper.isMsoMdocDecodedPresentation(original)) {
358
- return true;
359
- }
360
- else if (CredentialMapper.isJwtEncoded(original) || CredentialMapper.isJwtDecodedCredential(original)) {
361
- return true;
362
- }
363
- else if (CredentialMapper.isSdJwtEncoded(original) || CredentialMapper.isSdJwtDecodedCredential(original)) {
364
- //todo: we might want to revisit this
365
- return true;
366
- }
367
- if (typeof original !== 'object') {
368
- return false;
369
- }
370
- if ('vc' in original && original.vc.proof) {
371
- return true;
372
- }
373
- if ('vp' in original && original.vp.proof) {
374
- return true;
375
- }
376
- return !!original.proof;
377
- }
378
- catch (e) {
379
- return false;
380
- }
381
- }
382
- static isW3cPresentation(presentation) {
383
- var _a;
384
- return (typeof presentation === 'object' &&
385
- '@context' in presentation &&
386
- (((_a = presentation.type) === null || _a === void 0 ? void 0 : _a.includes('VerifiablePresentation')) || false));
387
- }
388
- static isSdJwtDecodedCredentialPayload(credential) {
389
- return typeof credential === 'object' && 'vct' in credential;
390
- }
391
- static areOriginalVerifiableCredentialsEqual(firstOriginal, secondOriginal) {
392
- // String (e.g. encoded jwt or SD-JWT)
393
- if (typeof firstOriginal === 'string' || typeof secondOriginal === 'string') {
394
- return firstOriginal === secondOriginal;
395
- }
396
- else if (CredentialMapper.isMsoMdocDecodedCredential(firstOriginal) || CredentialMapper.isMsoMdocDecodedCredential(secondOriginal)) {
397
- if (!CredentialMapper.isMsoMdocDecodedCredential(firstOriginal) || !CredentialMapper.isMsoMdocDecodedCredential(secondOriginal)) {
398
- // We are doing this over here, as the rest of the logic around it would otherwise need to be adjusted substantially
399
- return false;
400
- }
401
- // FIXME: mdoc library fails on parsing the device signed, so for now we just check whether the issuerSigned
402
- // is equal, then we have a good chance it is the same credential. Once device signed parsing is fixed in mdl
403
- // library we can move the .equals() to the top-level object.
404
- return firstOriginal.issuerSigned.equals(secondOriginal.issuerSigned);
405
- }
406
- else if (CredentialMapper.isSdJwtDecodedCredential(firstOriginal) || CredentialMapper.isSdJwtDecodedCredential(secondOriginal)) {
407
- return firstOriginal.compactSdJwtVc === secondOriginal.compactSdJwtVc;
408
- }
409
- else {
410
- // JSON-LD or decoded JWT. (should we compare the signatures instead?)
411
- return JSON.stringify(secondOriginal.proof) === JSON.stringify(firstOriginal.proof);
412
- }
413
- }
414
- static isJsonLdAsString(original) {
415
- return utils_1.ObjectUtils.isString(original) && original.includes('@context');
416
- }
417
- static isSdJwtDecodedCredential(original) {
418
- return (typeof original === 'object' &&
419
- (original.compactSdJwtVc !== undefined || original.kbJwt !== undefined));
420
- }
421
- static isMsoMdocDecodedCredential(original) {
422
- return typeof original === 'object' && 'issuerSigned' in original && original.issuerSigned !== undefined;
423
- }
424
- static isMsoMdocDecodedPresentation(original) {
425
- return typeof original === 'object' && 'version' in original && original.version !== undefined;
426
- }
427
- static isJwtDecodedCredential(original) {
428
- return (typeof original === 'object' &&
429
- original.vc !== undefined &&
430
- original.iss !== undefined);
431
- }
432
- static isJwtDecodedPresentation(original) {
433
- return (typeof original === 'object' &&
434
- original.vp !== undefined &&
435
- original.iss !== undefined);
436
- }
437
- static jwtEncodedPresentationToUniformPresentation(jwt, makeCredentialsUniform = true, opts) {
438
- return CredentialMapper.jwtDecodedPresentationToUniformPresentation((0, jwt_decode_1.jwtDecode)(jwt), makeCredentialsUniform, opts);
439
- }
440
- static jwtDecodedPresentationToUniformPresentation(decoded, makeCredentialsUniform = true, opts) {
441
- const { iss, aud, jti, vp } = decoded, rest = __rest(decoded, ["iss", "aud", "jti", "vp"]);
442
- const presentation = Object.assign(Object.assign({}, rest), vp);
443
- if (makeCredentialsUniform) {
444
- if (!vp.verifiableCredential) {
445
- throw Error('Verifiable Presentation should have a verifiable credential at this point');
446
- }
447
- presentation.verifiableCredential = vp.verifiableCredential.map((vc) => CredentialMapper.toUniformCredential(vc, opts));
448
- }
449
- if (iss) {
450
- const holder = presentation.holder;
451
- if (holder) {
452
- if (holder !== iss) {
453
- throw new Error(`Inconsistent holders between JWT claim (${iss}) and VC value (${holder})`);
454
- }
455
- }
456
- presentation.holder = iss;
457
- }
458
- if (aud) {
459
- const verifier = presentation.verifier;
460
- if (verifier) {
461
- if (verifier !== aud) {
462
- throw new Error(`Inconsistent holders between JWT claim (${aud}) and VC value (${verifier})`);
463
- }
464
- }
465
- presentation.verifier = aud;
466
- }
467
- if (jti) {
468
- const id = presentation.id;
469
- if (id && id !== jti) {
470
- throw new Error(`Inconsistent VP ids between JWT claim (${jti}) and VP value (${id})`);
471
- }
472
- presentation.id = jti;
473
- }
474
- return presentation;
475
- }
476
- static toUniformCredential(verifiableCredential, opts) {
477
- var _a, _b;
478
- if (CredentialMapper.isMsoMdocDecodedCredential(verifiableCredential)) {
479
- return (0, types_1.mdocDecodedCredentialToUniformCredential)(verifiableCredential);
480
- }
481
- if (CredentialMapper.isSdJwtDecodedCredential(verifiableCredential)) {
482
- return (0, types_1.sdJwtDecodedCredentialToUniformCredential)(verifiableCredential, opts);
483
- }
484
- const original = typeof verifiableCredential !== 'string' && CredentialMapper.hasJWTProofType(verifiableCredential)
485
- ? (_a = CredentialMapper.getFirstProof(verifiableCredential)) === null || _a === void 0 ? void 0 : _a.jwt
486
- : verifiableCredential;
487
- if (!original) {
488
- throw Error('Could not determine original credential from passed in credential. Probably because a JWT proof type was present, but now is not available anymore');
489
- }
490
- const decoded = CredentialMapper.decodeVerifiableCredential(original, (_b = opts === null || opts === void 0 ? void 0 : opts.hasher) !== null && _b !== void 0 ? _b : exports.sha256);
491
- const isJwtEncoded = CredentialMapper.isJwtEncoded(original);
492
- const isJwtDecoded = CredentialMapper.isJwtDecodedCredential(original);
493
- const isSdJwtEncoded = CredentialMapper.isSdJwtEncoded(original);
494
- const isMdocEncoded = CredentialMapper.isMsoMdocOid4VPEncoded(original);
495
- if (isSdJwtEncoded) {
496
- return (0, types_1.sdJwtDecodedCredentialToUniformCredential)(decoded, opts);
497
- }
498
- else if (isMdocEncoded) {
499
- return (0, types_1.mdocDecodedCredentialToUniformCredential)((0, types_1.decodeMdocIssuerSigned)(original));
500
- }
501
- else if (isJwtDecoded || isJwtEncoded) {
502
- return CredentialMapper.jwtDecodedCredentialToUniformCredential(decoded, opts);
503
- }
504
- else {
505
- return decoded;
506
- }
507
- }
508
- static toUniformPresentation(presentation, opts) {
509
- var _a, _b;
510
- if (CredentialMapper.isSdJwtDecodedCredential(presentation)) {
511
- throw new Error('Converting SD-JWT VC to uniform VP is not supported.');
512
- }
513
- else if (CredentialMapper.isMsoMdocDecodedPresentation(presentation)) {
514
- throw new Error('Converting MSO_MDOC to uniform VP is not supported yet.');
515
- }
516
- const proof = CredentialMapper.getFirstProof(presentation);
517
- const original = typeof presentation !== 'string' && CredentialMapper.hasJWTProofType(presentation) ? proof === null || proof === void 0 ? void 0 : proof.jwt : presentation;
518
- if (!original) {
519
- throw Error('Could not determine original presentation, probably it was a converted JWT presentation, that is now missing the JWT value in the proof');
520
- }
521
- const decoded = CredentialMapper.decodeVerifiablePresentation(original, (_a = opts === null || opts === void 0 ? void 0 : opts.hasher) !== null && _a !== void 0 ? _a : exports.sha256);
522
- const isJwtEncoded = CredentialMapper.isJwtEncoded(original);
523
- const isJwtDecoded = CredentialMapper.isJwtDecodedPresentation(original);
524
- const uniformPresentation = isJwtEncoded || isJwtDecoded
525
- ? CredentialMapper.jwtDecodedPresentationToUniformPresentation(decoded, false)
526
- : decoded;
527
- // At time of writing Velocity Networks does not conform to specification. Adding bare minimum @context section to stop parsers from crashing and whatnot
528
- if ((opts === null || opts === void 0 ? void 0 : opts.addContextIfMissing) && !uniformPresentation['@context']) {
529
- uniformPresentation['@context'] = ['https://www.w3.org/2018/credentials/v1'];
530
- }
531
- uniformPresentation.verifiableCredential = (_b = uniformPresentation.verifiableCredential) === null || _b === void 0 ? void 0 : _b.map((vc) => CredentialMapper.toUniformCredential(vc, opts)); // We cast it because we IPresentation needs a VC. The internal Credential doesn't have the required Proof anymore (that is intended)
532
- return uniformPresentation;
533
- }
534
- static jwtEncodedCredentialToUniformCredential(jwt, opts) {
535
- return CredentialMapper.jwtDecodedCredentialToUniformCredential((0, jwt_decode_1.jwtDecode)(jwt), opts);
536
- }
537
- static jwtDecodedCredentialToUniformCredential(decoded, opts) {
538
- var _a;
539
- const { exp, nbf, iss, vc, sub, jti } = decoded, rest = __rest(decoded, ["exp", "nbf", "iss", "vc", "sub", "jti"]);
540
- const credential = Object.assign(Object.assign({}, rest), vc);
541
- const maxSkewInMS = (_a = opts === null || opts === void 0 ? void 0 : opts.maxTimeSkewInMS) !== null && _a !== void 0 ? _a : 1500;
542
- if (exp) {
543
- const expDate = credential.expirationDate;
544
- const jwtExp = parseInt(exp.toString());
545
- // fix seconds to millisecond for the date
546
- const expDateAsStr = jwtExp < 9999999999 ? new Date(jwtExp * 1000).toISOString().replace(/\.000Z/, 'Z') : new Date(jwtExp).toISOString();
547
- if (expDate && expDate !== expDateAsStr) {
548
- const diff = Math.abs(new Date(expDateAsStr).getTime() - new Date(expDate).getTime());
549
- if (!maxSkewInMS || diff > maxSkewInMS) {
550
- throw new Error(`Inconsistent expiration dates between JWT claim (${expDateAsStr}) and VC value (${expDate})`);
551
- }
552
- }
553
- credential.expirationDate = expDateAsStr;
554
- }
555
- if (nbf) {
556
- const issuanceDate = credential.issuanceDate;
557
- const jwtNbf = parseInt(nbf.toString());
558
- // fix seconds to millisecs for the date
559
- const nbfDateAsStr = jwtNbf < 9999999999 ? new Date(jwtNbf * 1000).toISOString().replace(/\.000Z/, 'Z') : new Date(jwtNbf).toISOString();
560
- if (issuanceDate && issuanceDate !== nbfDateAsStr) {
561
- const diff = Math.abs(new Date(nbfDateAsStr).getTime() - new Date(issuanceDate).getTime());
562
- if (!maxSkewInMS || diff > maxSkewInMS) {
563
- throw new Error(`Inconsistent issuance dates between JWT claim (${nbfDateAsStr}) and VC value (${issuanceDate})`);
564
- }
565
- }
566
- credential.issuanceDate = nbfDateAsStr;
567
- }
568
- if (iss) {
569
- const issuer = credential.issuer;
570
- if (issuer) {
571
- if (typeof issuer === 'string') {
572
- if (issuer !== iss) {
573
- throw new Error(`Inconsistent issuers between JWT claim (${iss}) and VC value (${issuer})`);
574
- }
575
- }
576
- else {
577
- if (!issuer.id && Object.keys(issuer).length > 0) {
578
- // We have an issuer object with more than 1 property but without an issuer id. Set it,
579
- // because the default behaviour of did-jwt-vc is to remove the id value when creating JWTs
580
- issuer.id = iss;
581
- }
582
- if (issuer.id !== iss) {
583
- throw new Error(`Inconsistent issuers between JWT claim (${iss}) and VC value (${issuer.id})`);
584
- }
585
- }
586
- }
587
- else {
588
- credential.issuer = iss;
589
- }
590
- }
591
- if (sub) {
592
- const subjects = Array.isArray(credential.credentialSubject) ? credential.credentialSubject : [credential.credentialSubject];
593
- for (let i = 0; i < subjects.length; i++) {
594
- const csId = subjects[i].id;
595
- if (csId && csId !== sub) {
596
- throw new Error(`Inconsistent credential subject ids between JWT claim (${sub}) and VC value (${csId})`);
597
- }
598
- Array.isArray(credential.credentialSubject) ? (credential.credentialSubject[i].id = sub) : (credential.credentialSubject.id = sub);
599
- }
600
- }
601
- if (jti) {
602
- const id = credential.id;
603
- if (id && id !== jti) {
604
- throw new Error(`Inconsistent credential ids between JWT claim (${jti}) and VC value (${id})`);
605
- }
606
- credential.id = jti;
607
- }
608
- return credential;
609
- }
610
- static toExternalVerifiableCredential(verifiableCredential) {
611
- let proof;
612
- if (verifiableCredential.proof) {
613
- if (!verifiableCredential.proof.type) {
614
- throw new Error('Verifiable credential proof is missing a type');
615
- }
616
- if (!verifiableCredential.proof.created) {
617
- throw new Error('Verifiable credential proof is missing a created date');
618
- }
619
- if (!verifiableCredential.proof.proofPurpose) {
620
- throw new Error('Verifiable credential proof is missing a proof purpose');
621
- }
622
- if (!verifiableCredential.proof.verificationMethod) {
623
- throw new Error('Verifiable credential proof is missing a verification method');
624
- }
625
- proof = Object.assign(Object.assign({}, verifiableCredential.proof), { type: verifiableCredential.proof.type, created: verifiableCredential.proof.created, proofPurpose: verifiableCredential.proof.proofPurpose, verificationMethod: verifiableCredential.proof.verificationMethod });
626
- }
627
- return Object.assign(Object.assign({}, verifiableCredential), { type: verifiableCredential.type
628
- ? typeof verifiableCredential.type === 'string'
629
- ? [verifiableCredential.type]
630
- : verifiableCredential.type
631
- : ['VerifiableCredential'], proof });
632
- }
633
- static storedCredentialToOriginalFormat(credential) {
634
- const type = CredentialMapper.detectDocumentType(credential);
635
- if (typeof credential === 'string') {
636
- if (type === 0 /* DocumentFormat.JWT */) {
637
- return CredentialMapper.toCompactJWT(credential);
638
- }
639
- else if (type === 1 /* DocumentFormat.JSONLD */) {
640
- return JSON.parse(credential);
641
- }
642
- else if (type === 2 /* DocumentFormat.SD_JWT_VC */) {
643
- return credential;
644
- }
645
- }
646
- else if (type === 0 /* DocumentFormat.JWT */ && utils_1.ObjectUtils.isObject(credential) && 'vc' in credential) {
647
- return CredentialMapper.toCompactJWT(credential);
648
- }
649
- else if (utils_1.ObjectUtils.isObject(credential) && 'proof' in credential && credential.proof.type === 'JwtProof2020' && credential.proof.jwt) {
650
- return credential.proof.jwt;
651
- }
652
- else if (utils_1.ObjectUtils.isObject(credential) &&
653
- 'proof' in credential &&
654
- credential.proof.type === types_1.IProofType.SdJwtProof2024 &&
655
- credential.proof.jwt) {
656
- return credential.proof.jwt;
657
- }
658
- else if (type === 2 /* DocumentFormat.SD_JWT_VC */ && this.isSdJwtDecodedCredential(credential)) {
659
- return credential.compactSdJwtVc;
660
- }
661
- return credential;
662
- }
663
- static storedPresentationToOriginalFormat(presentation) {
664
- const type = CredentialMapper.detectDocumentType(presentation);
665
- if (typeof presentation === 'string') {
666
- if (type === 0 /* DocumentFormat.JWT */) {
667
- return CredentialMapper.toCompactJWT(presentation);
668
- }
669
- else if (type === 1 /* DocumentFormat.JSONLD */) {
670
- return JSON.parse(presentation);
671
- }
672
- }
673
- else if (type === 0 /* DocumentFormat.JWT */ && utils_1.ObjectUtils.isObject(presentation) && 'vp' in presentation) {
674
- return CredentialMapper.toCompactJWT(presentation);
675
- }
676
- else if (utils_1.ObjectUtils.isObject(presentation) &&
677
- 'proof' in presentation &&
678
- presentation.proof.type === 'JwtProof2020' &&
679
- presentation.proof.jwt) {
680
- return presentation.proof.jwt;
681
- }
682
- return presentation;
683
- }
684
- static toCompactJWT(jwtDocument) {
685
- if (!jwtDocument || CredentialMapper.detectDocumentType(jwtDocument) !== 0 /* DocumentFormat.JWT */) {
686
- throw Error('Cannot convert non JWT credential to JWT');
687
- }
688
- if (typeof jwtDocument === 'string') {
689
- return jwtDocument;
690
- }
691
- let proof;
692
- if (utils_1.ObjectUtils.isObject(jwtDocument) && 'vp' in jwtDocument) {
693
- proof = 'jwt' in jwtDocument.vp.proof ? jwtDocument.vp.proof.jwt : jwtDocument.vp.proof;
694
- }
695
- else if (utils_1.ObjectUtils.isObject(jwtDocument) && 'vc' in jwtDocument) {
696
- proof = 'jwt' in jwtDocument.vc.proof ? jwtDocument.vc.proof.jwt : jwtDocument.vc.proof;
697
- }
698
- else {
699
- proof = Array.isArray(jwtDocument.proof) ? jwtDocument.proof[0].jwt : jwtDocument.proof.jwt;
700
- }
701
- if (!proof) {
702
- throw Error(`Could not get JWT from supplied document`);
703
- }
704
- return proof;
705
- }
706
- static detectDocumentType(document) {
707
- if (this.isMsoMdocOid4VPEncoded(document) || this.isMsoMdocDecodedCredential(document)) {
708
- return 4 /* DocumentFormat.MSO_MDOC */;
709
- }
710
- else if (this.isMsoMdocDecodedPresentation(document) || this.isMsoMdocDecodedPresentation(document)) {
711
- return 4 /* DocumentFormat.MSO_MDOC */;
712
- }
713
- else if (this.isJsonLdAsString(document)) {
714
- return 1 /* DocumentFormat.JSONLD */;
715
- }
716
- else if (this.isJwtEncoded(document)) {
717
- return 0 /* DocumentFormat.JWT */;
718
- }
719
- else if (this.isSdJwtEncoded(document) || this.isSdJwtDecodedCredential(document)) {
720
- return 2 /* DocumentFormat.SD_JWT_VC */;
721
- }
722
- const proofs = typeof document !== 'string' &&
723
- ('vc' in document ? document.vc.proof : 'vp' in document ? document.vp.proof : document.proof);
724
- const proof = Array.isArray(proofs) ? proofs[0] : proofs;
725
- if (proof === null || proof === void 0 ? void 0 : proof.jwt) {
726
- return 0 /* DocumentFormat.JWT */;
727
- }
728
- else if ((proof === null || proof === void 0 ? void 0 : proof.type) === 'EthereumEip712Signature2021') {
729
- return 3 /* DocumentFormat.EIP712 */;
730
- }
731
- return 1 /* DocumentFormat.JSONLD */;
732
- }
733
- static hasJWTProofType(document) {
734
- var _a;
735
- if (typeof document === 'string') {
736
- return false;
737
- }
738
- return !!((_a = CredentialMapper.getFirstProof(document)) === null || _a === void 0 ? void 0 : _a.jwt);
739
- }
740
- static getFirstProof(document) {
741
- if (!document || typeof document === 'string') {
742
- return undefined;
743
- }
744
- const proofs = 'vc' in document ? document.vc.proof : 'vp' in document ? document.vp.proof : document.proof;
745
- return Array.isArray(proofs) ? proofs[0] : proofs;
746
- }
747
- static issuerCorrelationIdFromIssuerType(issuer) {
748
- if (issuer === undefined) {
749
- throw new Error('Issuer type us undefined');
750
- }
751
- else if (typeof issuer === 'string') {
752
- return issuer;
753
- }
754
- else if (typeof issuer === 'object') {
755
- if ('id' in issuer) {
756
- return issuer.id;
757
- }
758
- else {
759
- throw new Error('Encountered an invalid issuer object: missing id property');
760
- }
761
- }
762
- else {
763
- throw new Error('Invalid issuer type');
764
- }
765
- }
766
- }
767
- exports.CredentialMapper = CredentialMapper;
768
- CredentialMapper.isWrappedSdJwtVerifiableCredential = types_1.isWrappedSdJwtVerifiableCredential;
769
- CredentialMapper.isWrappedSdJwtVerifiablePresentation = types_1.isWrappedSdJwtVerifiablePresentation;
770
- CredentialMapper.isWrappedW3CVerifiableCredential = types_1.isWrappedW3CVerifiableCredential;
771
- CredentialMapper.isWrappedW3CVerifiablePresentation = types_1.isWrappedW3CVerifiablePresentation;
772
- CredentialMapper.isWrappedMdocCredential = types_1.isWrappedMdocCredential;
773
- CredentialMapper.isWrappedMdocPresentation = types_1.isWrappedMdocPresentation;
774
- //# sourceMappingURL=credential-mapper.js.map