@sphereon/ssi-types 0.18.2-next.9 → 0.18.2-unstable.13
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.
- package/LICENSE +201 -201
- package/README.md +49 -49
- package/dist/index.d.ts +3 -3
- package/dist/index.js +19 -19
- package/dist/mapper/credential-mapper.d.ts +107 -107
- package/dist/mapper/credential-mapper.js +556 -556
- package/dist/mapper/index.d.ts +1 -1
- package/dist/mapper/index.js +17 -17
- package/dist/types/did.d.ts +35 -35
- package/dist/types/did.js +75 -75
- package/dist/types/generic.d.ts +7 -7
- package/dist/types/generic.js +2 -2
- package/dist/types/index.d.ts +6 -6
- package/dist/types/index.js +22 -22
- package/dist/types/pex.d.ts +35 -35
- package/dist/types/pex.js +2 -2
- package/dist/types/sd-jwt-vc.d.ts +170 -170
- package/dist/types/sd-jwt-vc.js +71 -71
- package/dist/types/vc.d.ts +23 -23
- package/dist/types/vc.js +23 -23
- package/dist/types/w3c-vc.d.ts +247 -247
- package/dist/types/w3c-vc.js +29 -29
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.js +17 -17
- package/dist/utils/object.d.ts +6 -6
- package/dist/utils/object.js +20 -20
- package/package.json +2 -2
- package/src/index.ts +3 -3
- package/src/mapper/credential-mapper.ts +686 -686
- package/src/mapper/index.ts +1 -1
- package/src/types/generic.ts +8 -8
- package/src/types/index.ts +6 -6
- package/src/types/pex.ts +36 -36
- package/src/types/sd-jwt-vc.ts +232 -232
- package/src/types/vc.ts +62 -62
- package/src/types/w3c-vc.ts +300 -300
- package/src/utils/index.ts +1 -1
- package/src/utils/object.ts +19 -19
|
@@ -1,557 +1,557 @@
|
|
|
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
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
|
-
};
|
|
16
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.CredentialMapper = void 0;
|
|
18
|
-
const jwt_decode_1 = __importDefault(require("jwt-decode"));
|
|
19
|
-
const types_1 = require("../types");
|
|
20
|
-
const utils_1 = require("../utils");
|
|
21
|
-
class CredentialMapper {
|
|
22
|
-
/**
|
|
23
|
-
* 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
|
|
24
|
-
* with the other decode methods.
|
|
25
|
-
*/
|
|
26
|
-
static decodeSdJwtVcAsync(compactSdJwtVc, hasher) {
|
|
27
|
-
return (0, types_1.decodeSdJwtVcAsync)(compactSdJwtVc, hasher);
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Decodes a Verifiable Presentation to a uniform format.
|
|
31
|
-
*
|
|
32
|
-
* When decoding SD-JWT credentials, a hasher implementation must be provided. The hasher implementation must be sync. When using
|
|
33
|
-
* an async hasher implementation, use the decodeSdJwtVcAsync method instead and you can provide the decoded payload to methods
|
|
34
|
-
* instead of the compact SD-JWT.
|
|
35
|
-
*
|
|
36
|
-
* @param hasher Hasher implementation to use for SD-JWT decoding.
|
|
37
|
-
*/
|
|
38
|
-
static decodeVerifiablePresentation(presentation, hasher) {
|
|
39
|
-
var _a;
|
|
40
|
-
if (CredentialMapper.isJwtEncoded(presentation)) {
|
|
41
|
-
const payload = (0, jwt_decode_1.default)(presentation);
|
|
42
|
-
const header = (0, jwt_decode_1.default)(presentation, { header: true });
|
|
43
|
-
payload.vp.proof = {
|
|
44
|
-
type: types_1.IProofType.JwtProof2020,
|
|
45
|
-
created: payload.nbf,
|
|
46
|
-
proofPurpose: types_1.IProofPurpose.authentication,
|
|
47
|
-
verificationMethod: (_a = header['kid']) !== null && _a !== void 0 ? _a : payload.iss,
|
|
48
|
-
jwt: presentation,
|
|
49
|
-
};
|
|
50
|
-
return payload;
|
|
51
|
-
}
|
|
52
|
-
else if (CredentialMapper.isJwtDecodedPresentation(presentation)) {
|
|
53
|
-
return presentation;
|
|
54
|
-
}
|
|
55
|
-
else if (CredentialMapper.isSdJwtEncoded(presentation)) {
|
|
56
|
-
if (!hasher) {
|
|
57
|
-
throw new Error('Hasher implementation is required to decode SD-JWT');
|
|
58
|
-
}
|
|
59
|
-
return (0, types_1.decodeSdJwtVc)(presentation, hasher);
|
|
60
|
-
}
|
|
61
|
-
else if (CredentialMapper.isSdJwtDecodedCredential(presentation)) {
|
|
62
|
-
return presentation;
|
|
63
|
-
}
|
|
64
|
-
else if (CredentialMapper.isJsonLdAsString(presentation)) {
|
|
65
|
-
return JSON.parse(presentation);
|
|
66
|
-
}
|
|
67
|
-
else {
|
|
68
|
-
return presentation;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Decodes a Verifiable Credential to a uniform format.
|
|
73
|
-
*
|
|
74
|
-
* When decoding SD-JWT credentials, a hasher implementation must be provided. The hasher implementation must be sync. When using
|
|
75
|
-
* an async hasher implementation, use the decodeSdJwtVcAsync method instead and you can provide the decoded payload to methods
|
|
76
|
-
* instead of the compact SD-JWT.
|
|
77
|
-
*
|
|
78
|
-
* @param hasher Hasher implementation to use for SD-JWT decoding
|
|
79
|
-
*/
|
|
80
|
-
static decodeVerifiableCredential(credential, hasher) {
|
|
81
|
-
var _a;
|
|
82
|
-
if (CredentialMapper.isJwtEncoded(credential)) {
|
|
83
|
-
const payload = (0, jwt_decode_1.default)(credential);
|
|
84
|
-
const header = (0, jwt_decode_1.default)(credential, { header: true });
|
|
85
|
-
payload.vc.proof = {
|
|
86
|
-
type: types_1.IProofType.JwtProof2020,
|
|
87
|
-
created: payload.nbf,
|
|
88
|
-
proofPurpose: types_1.IProofPurpose.authentication,
|
|
89
|
-
verificationMethod: (_a = header['kid']) !== null && _a !== void 0 ? _a : payload.iss,
|
|
90
|
-
jwt: credential,
|
|
91
|
-
};
|
|
92
|
-
return payload;
|
|
93
|
-
}
|
|
94
|
-
else if (CredentialMapper.isJwtDecodedCredential(credential)) {
|
|
95
|
-
return credential;
|
|
96
|
-
}
|
|
97
|
-
else if (CredentialMapper.isJsonLdAsString(credential)) {
|
|
98
|
-
return JSON.parse(credential);
|
|
99
|
-
}
|
|
100
|
-
else if (CredentialMapper.isSdJwtEncoded(credential)) {
|
|
101
|
-
if (!hasher) {
|
|
102
|
-
throw new Error('Hasher implementation is required to decode SD-JWT');
|
|
103
|
-
}
|
|
104
|
-
return (0, types_1.decodeSdJwtVc)(credential, hasher);
|
|
105
|
-
}
|
|
106
|
-
else if (CredentialMapper.isSdJwtDecodedCredential(credential)) {
|
|
107
|
-
return credential;
|
|
108
|
-
}
|
|
109
|
-
else {
|
|
110
|
-
return credential;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* Converts a presentation to a wrapped presentation.
|
|
115
|
-
*
|
|
116
|
-
* When decoding SD-JWT credentials, a hasher implementation must be provided. The hasher implementation must be sync. When using
|
|
117
|
-
* an async hasher implementation, use the decodeSdJwtVcAsync method instead and you can provide the decoded payload to methods
|
|
118
|
-
* instead of the compact SD-JWT.
|
|
119
|
-
*
|
|
120
|
-
* @param hasher Hasher implementation to use for SD-JWT decoding
|
|
121
|
-
*/
|
|
122
|
-
static toWrappedVerifiablePresentation(originalPresentation, opts) {
|
|
123
|
-
// SD-JWT
|
|
124
|
-
if (CredentialMapper.isSdJwtDecodedCredential(originalPresentation) || CredentialMapper.isSdJwtEncoded(originalPresentation)) {
|
|
125
|
-
let decodedPresentation;
|
|
126
|
-
if (CredentialMapper.isSdJwtEncoded(originalPresentation)) {
|
|
127
|
-
if (!(opts === null || opts === void 0 ? void 0 : opts.hasher)) {
|
|
128
|
-
throw new Error('Hasher implementation is required to decode SD-JWT');
|
|
129
|
-
}
|
|
130
|
-
decodedPresentation = (0, types_1.decodeSdJwtVc)(originalPresentation, opts.hasher);
|
|
131
|
-
}
|
|
132
|
-
else {
|
|
133
|
-
decodedPresentation = originalPresentation;
|
|
134
|
-
}
|
|
135
|
-
return {
|
|
136
|
-
type: CredentialMapper.isSdJwtDecodedCredential(originalPresentation) ? types_1.OriginalType.SD_JWT_VC_DECODED : types_1.OriginalType.SD_JWT_VC_ENCODED,
|
|
137
|
-
format: 'vc+sd-jwt',
|
|
138
|
-
original: originalPresentation,
|
|
139
|
-
presentation: decodedPresentation,
|
|
140
|
-
decoded: decodedPresentation.decodedPayload,
|
|
141
|
-
// NOTE: we also include the SD-JWT VC as the VC, as the SD-JWT acts as both the VC and the VP
|
|
142
|
-
vcs: [CredentialMapper.toWrappedVerifiableCredential(originalPresentation, opts)],
|
|
143
|
-
};
|
|
144
|
-
}
|
|
145
|
-
// If the VP is not an encoded/decoded SD-JWT, we assume it will be a W3C VC
|
|
146
|
-
const proof = CredentialMapper.getFirstProof(originalPresentation);
|
|
147
|
-
const original = typeof originalPresentation !== 'string' && CredentialMapper.hasJWTProofType(originalPresentation) ? proof === null || proof === void 0 ? void 0 : proof.jwt : originalPresentation;
|
|
148
|
-
if (!original) {
|
|
149
|
-
throw Error('Could not determine original presentation, probably it was a converted JWT presentation, that is now missing the JWT value in the proof');
|
|
150
|
-
}
|
|
151
|
-
const decoded = CredentialMapper.decodeVerifiablePresentation(original);
|
|
152
|
-
const isJwtEncoded = CredentialMapper.isJwtEncoded(original);
|
|
153
|
-
const isJwtDecoded = CredentialMapper.isJwtDecodedPresentation(original);
|
|
154
|
-
const type = isJwtEncoded ? types_1.OriginalType.JWT_ENCODED : isJwtDecoded ? types_1.OriginalType.JWT_DECODED : types_1.OriginalType.JSONLD;
|
|
155
|
-
const format = isJwtDecoded || isJwtEncoded ? 'jwt_vp' : 'ldp_vp';
|
|
156
|
-
let vp;
|
|
157
|
-
if (isJwtEncoded || isJwtDecoded) {
|
|
158
|
-
vp = CredentialMapper.jwtDecodedPresentationToUniformPresentation(decoded, false, opts);
|
|
159
|
-
}
|
|
160
|
-
else {
|
|
161
|
-
vp = decoded;
|
|
162
|
-
}
|
|
163
|
-
if (!vp || !('verifiableCredential' in vp) || !vp.verifiableCredential || vp.verifiableCredential.length === 0) {
|
|
164
|
-
throw Error(`VP needs to have at least one verifiable credential at this point`);
|
|
165
|
-
}
|
|
166
|
-
const vcs = CredentialMapper.toWrappedVerifiableCredentials(vp.verifiableCredential /*.map(value => value.original)*/, opts);
|
|
167
|
-
const presentation = Object.assign(Object.assign({}, vp), { verifiableCredential: vcs });
|
|
168
|
-
return {
|
|
169
|
-
type,
|
|
170
|
-
format,
|
|
171
|
-
original,
|
|
172
|
-
decoded,
|
|
173
|
-
presentation,
|
|
174
|
-
vcs,
|
|
175
|
-
};
|
|
176
|
-
}
|
|
177
|
-
/**
|
|
178
|
-
* Converts a list of credentials to a list of wrapped credentials.
|
|
179
|
-
*
|
|
180
|
-
* When decoding SD-JWT credentials, a hasher implementation must be provided. The hasher implementation must be sync. When using
|
|
181
|
-
* an async hasher implementation, use the decodeSdJwtVcAsync method instead and you can provide the decoded payload to methods
|
|
182
|
-
* instead of the compact SD-JWT.
|
|
183
|
-
*
|
|
184
|
-
* @param hasher Hasher implementation to use for SD-JWT decoding
|
|
185
|
-
*/
|
|
186
|
-
static toWrappedVerifiableCredentials(verifiableCredentials, opts) {
|
|
187
|
-
return verifiableCredentials.map((vc) => CredentialMapper.toWrappedVerifiableCredential(vc, opts));
|
|
188
|
-
}
|
|
189
|
-
/**
|
|
190
|
-
* Converts a credential to a wrapped credential.
|
|
191
|
-
*
|
|
192
|
-
* When decoding SD-JWT credentials, a hasher implementation must be provided. The hasher implementation must be sync. When using
|
|
193
|
-
* an async hasher implementation, use the decodeSdJwtVcAsync method instead and you can provide the decoded payload to methods
|
|
194
|
-
* instead of the compact SD-JWT.
|
|
195
|
-
*
|
|
196
|
-
* @param hasher Hasher implementation to use for SD-JWT decoding
|
|
197
|
-
*/
|
|
198
|
-
static toWrappedVerifiableCredential(verifiableCredential, opts) {
|
|
199
|
-
var _a;
|
|
200
|
-
// SD-JWT
|
|
201
|
-
if (CredentialMapper.isSdJwtDecodedCredential(verifiableCredential) || CredentialMapper.isSdJwtEncoded(verifiableCredential)) {
|
|
202
|
-
let decodedCredential;
|
|
203
|
-
if (CredentialMapper.isSdJwtEncoded(verifiableCredential)) {
|
|
204
|
-
if (!(opts === null || opts === void 0 ? void 0 : opts.hasher)) {
|
|
205
|
-
throw new Error('Hasher implementation is required to decode SD-JWT');
|
|
206
|
-
}
|
|
207
|
-
decodedCredential = (0, types_1.decodeSdJwtVc)(verifiableCredential, opts.hasher);
|
|
208
|
-
}
|
|
209
|
-
else {
|
|
210
|
-
decodedCredential = verifiableCredential;
|
|
211
|
-
}
|
|
212
|
-
return {
|
|
213
|
-
type: CredentialMapper.isSdJwtDecodedCredential(verifiableCredential) ? types_1.OriginalType.SD_JWT_VC_DECODED : types_1.OriginalType.SD_JWT_VC_ENCODED,
|
|
214
|
-
format: 'vc+sd-jwt',
|
|
215
|
-
original: verifiableCredential,
|
|
216
|
-
credential: decodedCredential,
|
|
217
|
-
decoded: decodedCredential.decodedPayload,
|
|
218
|
-
};
|
|
219
|
-
}
|
|
220
|
-
// If the VC is not an encoded/decoded SD-JWT, we assume it will be a W3C VC
|
|
221
|
-
const proof = CredentialMapper.getFirstProof(verifiableCredential);
|
|
222
|
-
const original = CredentialMapper.hasJWTProofType(verifiableCredential) && proof ? (_a = proof.jwt) !== null && _a !== void 0 ? _a : verifiableCredential : verifiableCredential;
|
|
223
|
-
if (!original) {
|
|
224
|
-
throw Error('Could not determine original credential, probably it was a converted JWT credential, that is now missing the JWT value in the proof');
|
|
225
|
-
}
|
|
226
|
-
const decoded = CredentialMapper.decodeVerifiableCredential(original);
|
|
227
|
-
const isJwtEncoded = CredentialMapper.isJwtEncoded(original);
|
|
228
|
-
const isJwtDecoded = CredentialMapper.isJwtDecodedCredential(original);
|
|
229
|
-
const type = isJwtEncoded ? types_1.OriginalType.JWT_ENCODED : isJwtDecoded ? types_1.OriginalType.JWT_DECODED : types_1.OriginalType.JSONLD;
|
|
230
|
-
const credential = isJwtEncoded || isJwtDecoded
|
|
231
|
-
? CredentialMapper.jwtDecodedCredentialToUniformCredential(decoded, opts)
|
|
232
|
-
: decoded;
|
|
233
|
-
const format = isJwtEncoded || isJwtDecoded ? 'jwt_vc' : 'ldp_vc';
|
|
234
|
-
return {
|
|
235
|
-
original,
|
|
236
|
-
decoded,
|
|
237
|
-
format,
|
|
238
|
-
type,
|
|
239
|
-
credential,
|
|
240
|
-
};
|
|
241
|
-
}
|
|
242
|
-
static isJwtEncoded(original) {
|
|
243
|
-
return utils_1.ObjectUtils.isString(original) && original.startsWith('ey') && !original.includes('~');
|
|
244
|
-
}
|
|
245
|
-
static isSdJwtEncoded(original) {
|
|
246
|
-
return utils_1.ObjectUtils.isString(original) && original.startsWith('ey') && original.includes('~');
|
|
247
|
-
}
|
|
248
|
-
static isW3cCredential(credential) {
|
|
249
|
-
var _a;
|
|
250
|
-
return '@context' in credential && (((_a = credential.type) === null || _a === void 0 ? void 0 : _a.includes('VerifiableCredential')) || false);
|
|
251
|
-
}
|
|
252
|
-
static isW3cPresentation(presentation) {
|
|
253
|
-
var _a;
|
|
254
|
-
return '@context' in presentation && (((_a = presentation.type) === null || _a === void 0 ? void 0 : _a.includes('VerifiablePresentation')) || false);
|
|
255
|
-
}
|
|
256
|
-
static isSdJwtDecodedCredentialPayload(credential) {
|
|
257
|
-
return 'vct' in credential;
|
|
258
|
-
}
|
|
259
|
-
static areOriginalVerifiableCredentialsEqual(firstOriginal, secondOriginal) {
|
|
260
|
-
// String (e.g. encoded jwt or SD-JWT)
|
|
261
|
-
if (typeof firstOriginal === 'string' || typeof secondOriginal === 'string') {
|
|
262
|
-
return firstOriginal === secondOriginal;
|
|
263
|
-
}
|
|
264
|
-
else if (CredentialMapper.isSdJwtDecodedCredential(firstOriginal) || CredentialMapper.isSdJwtDecodedCredential(secondOriginal)) {
|
|
265
|
-
return firstOriginal.compactSdJwtVc === secondOriginal.compactSdJwtVc;
|
|
266
|
-
}
|
|
267
|
-
else {
|
|
268
|
-
// JSON-LD or decoded JWT. (should we compare the signatures instead?)
|
|
269
|
-
return JSON.stringify(secondOriginal.proof) === JSON.stringify(firstOriginal.proof);
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
static isJsonLdAsString(original) {
|
|
273
|
-
return utils_1.ObjectUtils.isString(original) && original.includes('@context');
|
|
274
|
-
}
|
|
275
|
-
static isSdJwtDecodedCredential(original) {
|
|
276
|
-
return original.compactSdJwtVc !== undefined;
|
|
277
|
-
}
|
|
278
|
-
static isJwtDecodedCredential(original) {
|
|
279
|
-
return original.vc !== undefined && original.iss !== undefined;
|
|
280
|
-
}
|
|
281
|
-
static isJwtDecodedPresentation(original) {
|
|
282
|
-
return original.vp !== undefined && original.iss !== undefined;
|
|
283
|
-
}
|
|
284
|
-
static jwtEncodedPresentationToUniformPresentation(jwt, makeCredentialsUniform = true, opts) {
|
|
285
|
-
return CredentialMapper.jwtDecodedPresentationToUniformPresentation((0, jwt_decode_1.default)(jwt), makeCredentialsUniform, opts);
|
|
286
|
-
}
|
|
287
|
-
static jwtDecodedPresentationToUniformPresentation(decoded, makeCredentialsUniform = true, opts) {
|
|
288
|
-
const { iss, aud, jti, vp } = decoded, rest = __rest(decoded, ["iss", "aud", "jti", "vp"]);
|
|
289
|
-
const presentation = Object.assign(Object.assign({}, rest), vp);
|
|
290
|
-
if (makeCredentialsUniform) {
|
|
291
|
-
if (!vp.verifiableCredential) {
|
|
292
|
-
throw Error('Verifiable Presentation should have a verifiable credential at this point');
|
|
293
|
-
}
|
|
294
|
-
presentation.verifiableCredential = vp.verifiableCredential.map((vc) => CredentialMapper.toUniformCredential(vc, opts));
|
|
295
|
-
}
|
|
296
|
-
if (iss) {
|
|
297
|
-
const holder = presentation.holder;
|
|
298
|
-
if (holder) {
|
|
299
|
-
if (holder !== iss) {
|
|
300
|
-
throw new Error(`Inconsistent holders between JWT claim (${iss}) and VC value (${holder})`);
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
presentation.holder = iss;
|
|
304
|
-
}
|
|
305
|
-
if (aud) {
|
|
306
|
-
const verifier = presentation.verifier;
|
|
307
|
-
if (verifier) {
|
|
308
|
-
if (verifier !== aud) {
|
|
309
|
-
throw new Error(`Inconsistent holders between JWT claim (${aud}) and VC value (${verifier})`);
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
presentation.verifier = aud;
|
|
313
|
-
}
|
|
314
|
-
if (jti) {
|
|
315
|
-
const id = presentation.id;
|
|
316
|
-
if (id && id !== jti) {
|
|
317
|
-
throw new Error(`Inconsistent VP ids between JWT claim (${jti}) and VP value (${id})`);
|
|
318
|
-
}
|
|
319
|
-
presentation.id = jti;
|
|
320
|
-
}
|
|
321
|
-
return presentation;
|
|
322
|
-
}
|
|
323
|
-
static toUniformCredential(verifiableCredential, opts) {
|
|
324
|
-
var _a;
|
|
325
|
-
if (CredentialMapper.isSdJwtDecodedCredential(verifiableCredential)) {
|
|
326
|
-
throw new Error('Converting SD-JWT VC to uniform VC is not supported.');
|
|
327
|
-
}
|
|
328
|
-
const original = typeof verifiableCredential !== 'string' && CredentialMapper.hasJWTProofType(verifiableCredential)
|
|
329
|
-
? (_a = CredentialMapper.getFirstProof(verifiableCredential)) === null || _a === void 0 ? void 0 : _a.jwt
|
|
330
|
-
: verifiableCredential;
|
|
331
|
-
if (!original) {
|
|
332
|
-
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');
|
|
333
|
-
}
|
|
334
|
-
const decoded = CredentialMapper.decodeVerifiableCredential(original);
|
|
335
|
-
const isJwtEncoded = CredentialMapper.isJwtEncoded(original);
|
|
336
|
-
const isJwtDecoded = CredentialMapper.isJwtDecodedCredential(original);
|
|
337
|
-
if (isJwtDecoded || isJwtEncoded) {
|
|
338
|
-
return CredentialMapper.jwtDecodedCredentialToUniformCredential(decoded, opts);
|
|
339
|
-
}
|
|
340
|
-
else {
|
|
341
|
-
return decoded;
|
|
342
|
-
}
|
|
343
|
-
}
|
|
344
|
-
static toUniformPresentation(presentation, opts) {
|
|
345
|
-
var _a;
|
|
346
|
-
if (CredentialMapper.isSdJwtDecodedCredential(presentation)) {
|
|
347
|
-
throw new Error('Converting SD-JWT VC to uniform VP is not supported.');
|
|
348
|
-
}
|
|
349
|
-
const proof = CredentialMapper.getFirstProof(presentation);
|
|
350
|
-
const original = typeof presentation !== 'string' && CredentialMapper.hasJWTProofType(presentation) ? proof === null || proof === void 0 ? void 0 : proof.jwt : presentation;
|
|
351
|
-
if (!original) {
|
|
352
|
-
throw Error('Could not determine original presentation, probably it was a converted JWT presentation, that is now missing the JWT value in the proof');
|
|
353
|
-
}
|
|
354
|
-
const decoded = CredentialMapper.decodeVerifiablePresentation(original);
|
|
355
|
-
const isJwtEncoded = CredentialMapper.isJwtEncoded(original);
|
|
356
|
-
const isJwtDecoded = CredentialMapper.isJwtDecodedPresentation(original);
|
|
357
|
-
const uniformPresentation = isJwtEncoded || isJwtDecoded
|
|
358
|
-
? CredentialMapper.jwtDecodedPresentationToUniformPresentation(decoded, false)
|
|
359
|
-
: decoded;
|
|
360
|
-
// At time of writing Velocity Networks does not conform to specification. Adding bare minimum @context section to stop parsers from crashing and whatnot
|
|
361
|
-
if ((opts === null || opts === void 0 ? void 0 : opts.addContextIfMissing) && !uniformPresentation['@context']) {
|
|
362
|
-
uniformPresentation['@context'] = ['https://www.w3.org/2018/credentials/v1'];
|
|
363
|
-
}
|
|
364
|
-
uniformPresentation.verifiableCredential = (_a = uniformPresentation.verifiableCredential) === null || _a === void 0 ? void 0 : _a.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)
|
|
365
|
-
return uniformPresentation;
|
|
366
|
-
}
|
|
367
|
-
static jwtEncodedCredentialToUniformCredential(jwt, opts) {
|
|
368
|
-
return CredentialMapper.jwtDecodedCredentialToUniformCredential((0, jwt_decode_1.default)(jwt), opts);
|
|
369
|
-
}
|
|
370
|
-
static jwtDecodedCredentialToUniformCredential(decoded, opts) {
|
|
371
|
-
const { exp, nbf, iss, vc, sub, jti } = decoded, rest = __rest(decoded, ["exp", "nbf", "iss", "vc", "sub", "jti"]);
|
|
372
|
-
const credential = Object.assign(Object.assign({}, rest), vc);
|
|
373
|
-
const maxSkewInMS = (opts === null || opts === void 0 ? void 0 : opts.maxTimeSkewInMS) !== undefined ? opts.maxTimeSkewInMS : 999;
|
|
374
|
-
if (exp) {
|
|
375
|
-
const expDate = credential.expirationDate;
|
|
376
|
-
const jwtExp = parseInt(exp.toString());
|
|
377
|
-
// fix seconds to millisecond for the date
|
|
378
|
-
const expDateAsStr = jwtExp < 9999999999 ? new Date(jwtExp * 1000).toISOString().replace(/\.000Z/, 'Z') : new Date(jwtExp).toISOString();
|
|
379
|
-
if (expDate && expDate !== expDateAsStr) {
|
|
380
|
-
const diff = Math.abs(new Date(expDateAsStr).getTime() - new Date(expDate).getTime());
|
|
381
|
-
if (!maxSkewInMS || diff > maxSkewInMS) {
|
|
382
|
-
throw new Error(`Inconsistent expiration dates between JWT claim (${expDateAsStr}) and VC value (${expDate})`);
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
credential.expirationDate = expDateAsStr;
|
|
386
|
-
}
|
|
387
|
-
if (nbf) {
|
|
388
|
-
const issuanceDate = credential.issuanceDate;
|
|
389
|
-
const jwtNbf = parseInt(nbf.toString());
|
|
390
|
-
// fix seconds to millisecs for the date
|
|
391
|
-
const nbfDateAsStr = jwtNbf < 9999999999 ? new Date(jwtNbf * 1000).toISOString().replace(/\.000Z/, 'Z') : new Date(jwtNbf).toISOString();
|
|
392
|
-
if (issuanceDate && issuanceDate !== nbfDateAsStr) {
|
|
393
|
-
const diff = Math.abs(new Date(nbfDateAsStr).getTime() - new Date(issuanceDate).getTime());
|
|
394
|
-
if (!maxSkewInMS || diff > maxSkewInMS) {
|
|
395
|
-
throw new Error(`Inconsistent issuance dates between JWT claim (${nbfDateAsStr}) and VC value (${issuanceDate})`);
|
|
396
|
-
}
|
|
397
|
-
}
|
|
398
|
-
credential.issuanceDate = nbfDateAsStr;
|
|
399
|
-
}
|
|
400
|
-
if (iss) {
|
|
401
|
-
const issuer = credential.issuer;
|
|
402
|
-
if (issuer) {
|
|
403
|
-
if (typeof issuer === 'string') {
|
|
404
|
-
if (issuer !== iss) {
|
|
405
|
-
throw new Error(`Inconsistent issuers between JWT claim (${iss}) and VC value (${issuer})`);
|
|
406
|
-
}
|
|
407
|
-
}
|
|
408
|
-
else {
|
|
409
|
-
if (!issuer.id && Object.keys(issuer).length > 0) {
|
|
410
|
-
// We have an issuer object with more than 1 property but without an issuer id. Set it,
|
|
411
|
-
// because the default behaviour of did-jwt-vc is to remove the id value when creating JWTs
|
|
412
|
-
issuer.id = iss;
|
|
413
|
-
}
|
|
414
|
-
if (issuer.id !== iss) {
|
|
415
|
-
throw new Error(`Inconsistent issuers between JWT claim (${iss}) and VC value (${issuer.id})`);
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
}
|
|
419
|
-
else {
|
|
420
|
-
credential.issuer = iss;
|
|
421
|
-
}
|
|
422
|
-
}
|
|
423
|
-
if (sub) {
|
|
424
|
-
const subjects = Array.isArray(credential.credentialSubject) ? credential.credentialSubject : [credential.credentialSubject];
|
|
425
|
-
for (let i = 0; i < subjects.length; i++) {
|
|
426
|
-
const csId = subjects[i].id;
|
|
427
|
-
if (csId && csId !== sub) {
|
|
428
|
-
throw new Error(`Inconsistent credential subject ids between JWT claim (${sub}) and VC value (${csId})`);
|
|
429
|
-
}
|
|
430
|
-
Array.isArray(credential.credentialSubject) ? (credential.credentialSubject[i].id = sub) : (credential.credentialSubject.id = sub);
|
|
431
|
-
}
|
|
432
|
-
}
|
|
433
|
-
if (jti) {
|
|
434
|
-
const id = credential.id;
|
|
435
|
-
if (id && id !== jti) {
|
|
436
|
-
throw new Error(`Inconsistent credential ids between JWT claim (${jti}) and VC value (${id})`);
|
|
437
|
-
}
|
|
438
|
-
credential.id = jti;
|
|
439
|
-
}
|
|
440
|
-
return credential;
|
|
441
|
-
}
|
|
442
|
-
static toExternalVerifiableCredential(verifiableCredential) {
|
|
443
|
-
let proof;
|
|
444
|
-
if (verifiableCredential.proof) {
|
|
445
|
-
if (!verifiableCredential.proof.type) {
|
|
446
|
-
throw new Error('Verifiable credential proof is missing a type');
|
|
447
|
-
}
|
|
448
|
-
if (!verifiableCredential.proof.created) {
|
|
449
|
-
throw new Error('Verifiable credential proof is missing a created date');
|
|
450
|
-
}
|
|
451
|
-
if (!verifiableCredential.proof.proofPurpose) {
|
|
452
|
-
throw new Error('Verifiable credential proof is missing a proof purpose');
|
|
453
|
-
}
|
|
454
|
-
if (!verifiableCredential.proof.verificationMethod) {
|
|
455
|
-
throw new Error('Verifiable credential proof is missing a verification method');
|
|
456
|
-
}
|
|
457
|
-
proof = Object.assign(Object.assign({}, verifiableCredential.proof), { type: verifiableCredential.proof.type, created: verifiableCredential.proof.created, proofPurpose: verifiableCredential.proof.proofPurpose, verificationMethod: verifiableCredential.proof.verificationMethod });
|
|
458
|
-
}
|
|
459
|
-
return Object.assign(Object.assign({}, verifiableCredential), { type: verifiableCredential.type
|
|
460
|
-
? typeof verifiableCredential.type === 'string'
|
|
461
|
-
? [verifiableCredential.type]
|
|
462
|
-
: verifiableCredential.type
|
|
463
|
-
: ['VerifiableCredential'], proof });
|
|
464
|
-
}
|
|
465
|
-
static storedCredentialToOriginalFormat(credential) {
|
|
466
|
-
const type = CredentialMapper.detectDocumentType(credential);
|
|
467
|
-
if (typeof credential === 'string') {
|
|
468
|
-
if (type === 0 /* DocumentFormat.JWT */) {
|
|
469
|
-
return CredentialMapper.toCompactJWT(credential);
|
|
470
|
-
}
|
|
471
|
-
else if (type === 1 /* DocumentFormat.JSONLD */) {
|
|
472
|
-
return JSON.parse(credential);
|
|
473
|
-
}
|
|
474
|
-
}
|
|
475
|
-
else if (type === 0 /* DocumentFormat.JWT */ && 'vc' in credential) {
|
|
476
|
-
return CredentialMapper.toCompactJWT(credential);
|
|
477
|
-
}
|
|
478
|
-
return credential;
|
|
479
|
-
}
|
|
480
|
-
static storedPresentationToOriginalFormat(presentation) {
|
|
481
|
-
const type = CredentialMapper.detectDocumentType(presentation);
|
|
482
|
-
if (typeof presentation === 'string') {
|
|
483
|
-
if (type === 0 /* DocumentFormat.JWT */) {
|
|
484
|
-
return CredentialMapper.toCompactJWT(presentation);
|
|
485
|
-
}
|
|
486
|
-
else if (type === 1 /* DocumentFormat.JSONLD */) {
|
|
487
|
-
return JSON.parse(presentation);
|
|
488
|
-
}
|
|
489
|
-
}
|
|
490
|
-
else if (type === 0 /* DocumentFormat.JWT */ && 'vp' in presentation) {
|
|
491
|
-
return CredentialMapper.toCompactJWT(presentation);
|
|
492
|
-
}
|
|
493
|
-
return presentation;
|
|
494
|
-
}
|
|
495
|
-
static toCompactJWT(jwtDocument) {
|
|
496
|
-
if (!jwtDocument || CredentialMapper.detectDocumentType(jwtDocument) !== 0 /* DocumentFormat.JWT */) {
|
|
497
|
-
throw Error('Cannot convert non JWT credential to JWT');
|
|
498
|
-
}
|
|
499
|
-
if (typeof jwtDocument === 'string') {
|
|
500
|
-
return jwtDocument;
|
|
501
|
-
}
|
|
502
|
-
let proof;
|
|
503
|
-
if ('vp' in jwtDocument) {
|
|
504
|
-
proof = jwtDocument.vp.proof;
|
|
505
|
-
}
|
|
506
|
-
else if ('vc' in jwtDocument) {
|
|
507
|
-
proof = jwtDocument.vc.proof;
|
|
508
|
-
}
|
|
509
|
-
else {
|
|
510
|
-
proof = Array.isArray(jwtDocument.proof) ? jwtDocument.proof[0].jwt : jwtDocument.proof.jwt;
|
|
511
|
-
}
|
|
512
|
-
if (!proof) {
|
|
513
|
-
throw Error(`Could not get JWT from supplied document`);
|
|
514
|
-
}
|
|
515
|
-
return proof;
|
|
516
|
-
}
|
|
517
|
-
static detectDocumentType(document) {
|
|
518
|
-
if (this.isJsonLdAsString(document)) {
|
|
519
|
-
return 1 /* DocumentFormat.JSONLD */;
|
|
520
|
-
}
|
|
521
|
-
else if (this.isJwtEncoded(document)) {
|
|
522
|
-
return 0 /* DocumentFormat.JWT */;
|
|
523
|
-
}
|
|
524
|
-
else if (this.isSdJwtEncoded(document) || this.isSdJwtDecodedCredential(document)) {
|
|
525
|
-
return 2 /* DocumentFormat.SD_JWT_VC */;
|
|
526
|
-
}
|
|
527
|
-
const proofs = 'vc' in document ? document.vc.proof : 'vp' in document ? document.vp.proof : document.proof;
|
|
528
|
-
const proof = Array.isArray(proofs) ? proofs[0] : proofs;
|
|
529
|
-
if (proof === null || proof === void 0 ? void 0 : proof.jwt) {
|
|
530
|
-
return 0 /* DocumentFormat.JWT */;
|
|
531
|
-
}
|
|
532
|
-
else if ((proof === null || proof === void 0 ? void 0 : proof.type) === 'EthereumEip712Signature2021') {
|
|
533
|
-
return 3 /* DocumentFormat.EIP712 */;
|
|
534
|
-
}
|
|
535
|
-
return 1 /* DocumentFormat.JSONLD */;
|
|
536
|
-
}
|
|
537
|
-
static hasJWTProofType(document) {
|
|
538
|
-
var _a;
|
|
539
|
-
if (typeof document === 'string') {
|
|
540
|
-
return false;
|
|
541
|
-
}
|
|
542
|
-
return !!((_a = CredentialMapper.getFirstProof(document)) === null || _a === void 0 ? void 0 : _a.jwt);
|
|
543
|
-
}
|
|
544
|
-
static getFirstProof(document) {
|
|
545
|
-
if (!document || typeof document === 'string') {
|
|
546
|
-
return undefined;
|
|
547
|
-
}
|
|
548
|
-
const proofs = 'vc' in document ? document.vc.proof : 'vp' in document ? document.vp.proof : document.proof;
|
|
549
|
-
return Array.isArray(proofs) ? proofs[0] : proofs;
|
|
550
|
-
}
|
|
551
|
-
}
|
|
552
|
-
exports.CredentialMapper = CredentialMapper;
|
|
553
|
-
CredentialMapper.isWrappedSdJwtVerifiableCredential = types_1.isWrappedSdJwtVerifiableCredential;
|
|
554
|
-
CredentialMapper.isWrappedSdJwtVerifiablePresentation = types_1.isWrappedSdJwtVerifiablePresentation;
|
|
555
|
-
CredentialMapper.isWrappedW3CVerifiableCredential = types_1.isWrappedW3CVerifiableCredential;
|
|
556
|
-
CredentialMapper.isWrappedW3CVerifiablePresentation = types_1.isWrappedW3CVerifiablePresentation;
|
|
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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.CredentialMapper = void 0;
|
|
18
|
+
const jwt_decode_1 = __importDefault(require("jwt-decode"));
|
|
19
|
+
const types_1 = require("../types");
|
|
20
|
+
const utils_1 = require("../utils");
|
|
21
|
+
class CredentialMapper {
|
|
22
|
+
/**
|
|
23
|
+
* 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
|
|
24
|
+
* with the other decode methods.
|
|
25
|
+
*/
|
|
26
|
+
static decodeSdJwtVcAsync(compactSdJwtVc, hasher) {
|
|
27
|
+
return (0, types_1.decodeSdJwtVcAsync)(compactSdJwtVc, hasher);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Decodes a Verifiable Presentation to a uniform format.
|
|
31
|
+
*
|
|
32
|
+
* When decoding SD-JWT credentials, a hasher implementation must be provided. The hasher implementation must be sync. When using
|
|
33
|
+
* an async hasher implementation, use the decodeSdJwtVcAsync method instead and you can provide the decoded payload to methods
|
|
34
|
+
* instead of the compact SD-JWT.
|
|
35
|
+
*
|
|
36
|
+
* @param hasher Hasher implementation to use for SD-JWT decoding.
|
|
37
|
+
*/
|
|
38
|
+
static decodeVerifiablePresentation(presentation, hasher) {
|
|
39
|
+
var _a;
|
|
40
|
+
if (CredentialMapper.isJwtEncoded(presentation)) {
|
|
41
|
+
const payload = (0, jwt_decode_1.default)(presentation);
|
|
42
|
+
const header = (0, jwt_decode_1.default)(presentation, { header: true });
|
|
43
|
+
payload.vp.proof = {
|
|
44
|
+
type: types_1.IProofType.JwtProof2020,
|
|
45
|
+
created: payload.nbf,
|
|
46
|
+
proofPurpose: types_1.IProofPurpose.authentication,
|
|
47
|
+
verificationMethod: (_a = header['kid']) !== null && _a !== void 0 ? _a : payload.iss,
|
|
48
|
+
jwt: presentation,
|
|
49
|
+
};
|
|
50
|
+
return payload;
|
|
51
|
+
}
|
|
52
|
+
else if (CredentialMapper.isJwtDecodedPresentation(presentation)) {
|
|
53
|
+
return presentation;
|
|
54
|
+
}
|
|
55
|
+
else if (CredentialMapper.isSdJwtEncoded(presentation)) {
|
|
56
|
+
if (!hasher) {
|
|
57
|
+
throw new Error('Hasher implementation is required to decode SD-JWT');
|
|
58
|
+
}
|
|
59
|
+
return (0, types_1.decodeSdJwtVc)(presentation, hasher);
|
|
60
|
+
}
|
|
61
|
+
else if (CredentialMapper.isSdJwtDecodedCredential(presentation)) {
|
|
62
|
+
return presentation;
|
|
63
|
+
}
|
|
64
|
+
else if (CredentialMapper.isJsonLdAsString(presentation)) {
|
|
65
|
+
return JSON.parse(presentation);
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
return presentation;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Decodes a Verifiable Credential to a uniform format.
|
|
73
|
+
*
|
|
74
|
+
* When decoding SD-JWT credentials, a hasher implementation must be provided. The hasher implementation must be sync. When using
|
|
75
|
+
* an async hasher implementation, use the decodeSdJwtVcAsync method instead and you can provide the decoded payload to methods
|
|
76
|
+
* instead of the compact SD-JWT.
|
|
77
|
+
*
|
|
78
|
+
* @param hasher Hasher implementation to use for SD-JWT decoding
|
|
79
|
+
*/
|
|
80
|
+
static decodeVerifiableCredential(credential, hasher) {
|
|
81
|
+
var _a;
|
|
82
|
+
if (CredentialMapper.isJwtEncoded(credential)) {
|
|
83
|
+
const payload = (0, jwt_decode_1.default)(credential);
|
|
84
|
+
const header = (0, jwt_decode_1.default)(credential, { header: true });
|
|
85
|
+
payload.vc.proof = {
|
|
86
|
+
type: types_1.IProofType.JwtProof2020,
|
|
87
|
+
created: payload.nbf,
|
|
88
|
+
proofPurpose: types_1.IProofPurpose.authentication,
|
|
89
|
+
verificationMethod: (_a = header['kid']) !== null && _a !== void 0 ? _a : payload.iss,
|
|
90
|
+
jwt: credential,
|
|
91
|
+
};
|
|
92
|
+
return payload;
|
|
93
|
+
}
|
|
94
|
+
else if (CredentialMapper.isJwtDecodedCredential(credential)) {
|
|
95
|
+
return credential;
|
|
96
|
+
}
|
|
97
|
+
else if (CredentialMapper.isJsonLdAsString(credential)) {
|
|
98
|
+
return JSON.parse(credential);
|
|
99
|
+
}
|
|
100
|
+
else if (CredentialMapper.isSdJwtEncoded(credential)) {
|
|
101
|
+
if (!hasher) {
|
|
102
|
+
throw new Error('Hasher implementation is required to decode SD-JWT');
|
|
103
|
+
}
|
|
104
|
+
return (0, types_1.decodeSdJwtVc)(credential, hasher);
|
|
105
|
+
}
|
|
106
|
+
else if (CredentialMapper.isSdJwtDecodedCredential(credential)) {
|
|
107
|
+
return credential;
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
return credential;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Converts a presentation to a wrapped presentation.
|
|
115
|
+
*
|
|
116
|
+
* When decoding SD-JWT credentials, a hasher implementation must be provided. The hasher implementation must be sync. When using
|
|
117
|
+
* an async hasher implementation, use the decodeSdJwtVcAsync method instead and you can provide the decoded payload to methods
|
|
118
|
+
* instead of the compact SD-JWT.
|
|
119
|
+
*
|
|
120
|
+
* @param hasher Hasher implementation to use for SD-JWT decoding
|
|
121
|
+
*/
|
|
122
|
+
static toWrappedVerifiablePresentation(originalPresentation, opts) {
|
|
123
|
+
// SD-JWT
|
|
124
|
+
if (CredentialMapper.isSdJwtDecodedCredential(originalPresentation) || CredentialMapper.isSdJwtEncoded(originalPresentation)) {
|
|
125
|
+
let decodedPresentation;
|
|
126
|
+
if (CredentialMapper.isSdJwtEncoded(originalPresentation)) {
|
|
127
|
+
if (!(opts === null || opts === void 0 ? void 0 : opts.hasher)) {
|
|
128
|
+
throw new Error('Hasher implementation is required to decode SD-JWT');
|
|
129
|
+
}
|
|
130
|
+
decodedPresentation = (0, types_1.decodeSdJwtVc)(originalPresentation, opts.hasher);
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
decodedPresentation = originalPresentation;
|
|
134
|
+
}
|
|
135
|
+
return {
|
|
136
|
+
type: CredentialMapper.isSdJwtDecodedCredential(originalPresentation) ? types_1.OriginalType.SD_JWT_VC_DECODED : types_1.OriginalType.SD_JWT_VC_ENCODED,
|
|
137
|
+
format: 'vc+sd-jwt',
|
|
138
|
+
original: originalPresentation,
|
|
139
|
+
presentation: decodedPresentation,
|
|
140
|
+
decoded: decodedPresentation.decodedPayload,
|
|
141
|
+
// NOTE: we also include the SD-JWT VC as the VC, as the SD-JWT acts as both the VC and the VP
|
|
142
|
+
vcs: [CredentialMapper.toWrappedVerifiableCredential(originalPresentation, opts)],
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
// If the VP is not an encoded/decoded SD-JWT, we assume it will be a W3C VC
|
|
146
|
+
const proof = CredentialMapper.getFirstProof(originalPresentation);
|
|
147
|
+
const original = typeof originalPresentation !== 'string' && CredentialMapper.hasJWTProofType(originalPresentation) ? proof === null || proof === void 0 ? void 0 : proof.jwt : originalPresentation;
|
|
148
|
+
if (!original) {
|
|
149
|
+
throw Error('Could not determine original presentation, probably it was a converted JWT presentation, that is now missing the JWT value in the proof');
|
|
150
|
+
}
|
|
151
|
+
const decoded = CredentialMapper.decodeVerifiablePresentation(original);
|
|
152
|
+
const isJwtEncoded = CredentialMapper.isJwtEncoded(original);
|
|
153
|
+
const isJwtDecoded = CredentialMapper.isJwtDecodedPresentation(original);
|
|
154
|
+
const type = isJwtEncoded ? types_1.OriginalType.JWT_ENCODED : isJwtDecoded ? types_1.OriginalType.JWT_DECODED : types_1.OriginalType.JSONLD;
|
|
155
|
+
const format = isJwtDecoded || isJwtEncoded ? 'jwt_vp' : 'ldp_vp';
|
|
156
|
+
let vp;
|
|
157
|
+
if (isJwtEncoded || isJwtDecoded) {
|
|
158
|
+
vp = CredentialMapper.jwtDecodedPresentationToUniformPresentation(decoded, false, opts);
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
vp = decoded;
|
|
162
|
+
}
|
|
163
|
+
if (!vp || !('verifiableCredential' in vp) || !vp.verifiableCredential || vp.verifiableCredential.length === 0) {
|
|
164
|
+
throw Error(`VP needs to have at least one verifiable credential at this point`);
|
|
165
|
+
}
|
|
166
|
+
const vcs = CredentialMapper.toWrappedVerifiableCredentials(vp.verifiableCredential /*.map(value => value.original)*/, opts);
|
|
167
|
+
const presentation = Object.assign(Object.assign({}, vp), { verifiableCredential: vcs });
|
|
168
|
+
return {
|
|
169
|
+
type,
|
|
170
|
+
format,
|
|
171
|
+
original,
|
|
172
|
+
decoded,
|
|
173
|
+
presentation,
|
|
174
|
+
vcs,
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* Converts a list of credentials to a list of wrapped credentials.
|
|
179
|
+
*
|
|
180
|
+
* When decoding SD-JWT credentials, a hasher implementation must be provided. The hasher implementation must be sync. When using
|
|
181
|
+
* an async hasher implementation, use the decodeSdJwtVcAsync method instead and you can provide the decoded payload to methods
|
|
182
|
+
* instead of the compact SD-JWT.
|
|
183
|
+
*
|
|
184
|
+
* @param hasher Hasher implementation to use for SD-JWT decoding
|
|
185
|
+
*/
|
|
186
|
+
static toWrappedVerifiableCredentials(verifiableCredentials, opts) {
|
|
187
|
+
return verifiableCredentials.map((vc) => CredentialMapper.toWrappedVerifiableCredential(vc, opts));
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Converts a credential to a wrapped credential.
|
|
191
|
+
*
|
|
192
|
+
* When decoding SD-JWT credentials, a hasher implementation must be provided. The hasher implementation must be sync. When using
|
|
193
|
+
* an async hasher implementation, use the decodeSdJwtVcAsync method instead and you can provide the decoded payload to methods
|
|
194
|
+
* instead of the compact SD-JWT.
|
|
195
|
+
*
|
|
196
|
+
* @param hasher Hasher implementation to use for SD-JWT decoding
|
|
197
|
+
*/
|
|
198
|
+
static toWrappedVerifiableCredential(verifiableCredential, opts) {
|
|
199
|
+
var _a;
|
|
200
|
+
// SD-JWT
|
|
201
|
+
if (CredentialMapper.isSdJwtDecodedCredential(verifiableCredential) || CredentialMapper.isSdJwtEncoded(verifiableCredential)) {
|
|
202
|
+
let decodedCredential;
|
|
203
|
+
if (CredentialMapper.isSdJwtEncoded(verifiableCredential)) {
|
|
204
|
+
if (!(opts === null || opts === void 0 ? void 0 : opts.hasher)) {
|
|
205
|
+
throw new Error('Hasher implementation is required to decode SD-JWT');
|
|
206
|
+
}
|
|
207
|
+
decodedCredential = (0, types_1.decodeSdJwtVc)(verifiableCredential, opts.hasher);
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
decodedCredential = verifiableCredential;
|
|
211
|
+
}
|
|
212
|
+
return {
|
|
213
|
+
type: CredentialMapper.isSdJwtDecodedCredential(verifiableCredential) ? types_1.OriginalType.SD_JWT_VC_DECODED : types_1.OriginalType.SD_JWT_VC_ENCODED,
|
|
214
|
+
format: 'vc+sd-jwt',
|
|
215
|
+
original: verifiableCredential,
|
|
216
|
+
credential: decodedCredential,
|
|
217
|
+
decoded: decodedCredential.decodedPayload,
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
// If the VC is not an encoded/decoded SD-JWT, we assume it will be a W3C VC
|
|
221
|
+
const proof = CredentialMapper.getFirstProof(verifiableCredential);
|
|
222
|
+
const original = CredentialMapper.hasJWTProofType(verifiableCredential) && proof ? (_a = proof.jwt) !== null && _a !== void 0 ? _a : verifiableCredential : verifiableCredential;
|
|
223
|
+
if (!original) {
|
|
224
|
+
throw Error('Could not determine original credential, probably it was a converted JWT credential, that is now missing the JWT value in the proof');
|
|
225
|
+
}
|
|
226
|
+
const decoded = CredentialMapper.decodeVerifiableCredential(original);
|
|
227
|
+
const isJwtEncoded = CredentialMapper.isJwtEncoded(original);
|
|
228
|
+
const isJwtDecoded = CredentialMapper.isJwtDecodedCredential(original);
|
|
229
|
+
const type = isJwtEncoded ? types_1.OriginalType.JWT_ENCODED : isJwtDecoded ? types_1.OriginalType.JWT_DECODED : types_1.OriginalType.JSONLD;
|
|
230
|
+
const credential = isJwtEncoded || isJwtDecoded
|
|
231
|
+
? CredentialMapper.jwtDecodedCredentialToUniformCredential(decoded, opts)
|
|
232
|
+
: decoded;
|
|
233
|
+
const format = isJwtEncoded || isJwtDecoded ? 'jwt_vc' : 'ldp_vc';
|
|
234
|
+
return {
|
|
235
|
+
original,
|
|
236
|
+
decoded,
|
|
237
|
+
format,
|
|
238
|
+
type,
|
|
239
|
+
credential,
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
static isJwtEncoded(original) {
|
|
243
|
+
return utils_1.ObjectUtils.isString(original) && original.startsWith('ey') && !original.includes('~');
|
|
244
|
+
}
|
|
245
|
+
static isSdJwtEncoded(original) {
|
|
246
|
+
return utils_1.ObjectUtils.isString(original) && original.startsWith('ey') && original.includes('~');
|
|
247
|
+
}
|
|
248
|
+
static isW3cCredential(credential) {
|
|
249
|
+
var _a;
|
|
250
|
+
return '@context' in credential && (((_a = credential.type) === null || _a === void 0 ? void 0 : _a.includes('VerifiableCredential')) || false);
|
|
251
|
+
}
|
|
252
|
+
static isW3cPresentation(presentation) {
|
|
253
|
+
var _a;
|
|
254
|
+
return '@context' in presentation && (((_a = presentation.type) === null || _a === void 0 ? void 0 : _a.includes('VerifiablePresentation')) || false);
|
|
255
|
+
}
|
|
256
|
+
static isSdJwtDecodedCredentialPayload(credential) {
|
|
257
|
+
return 'vct' in credential;
|
|
258
|
+
}
|
|
259
|
+
static areOriginalVerifiableCredentialsEqual(firstOriginal, secondOriginal) {
|
|
260
|
+
// String (e.g. encoded jwt or SD-JWT)
|
|
261
|
+
if (typeof firstOriginal === 'string' || typeof secondOriginal === 'string') {
|
|
262
|
+
return firstOriginal === secondOriginal;
|
|
263
|
+
}
|
|
264
|
+
else if (CredentialMapper.isSdJwtDecodedCredential(firstOriginal) || CredentialMapper.isSdJwtDecodedCredential(secondOriginal)) {
|
|
265
|
+
return firstOriginal.compactSdJwtVc === secondOriginal.compactSdJwtVc;
|
|
266
|
+
}
|
|
267
|
+
else {
|
|
268
|
+
// JSON-LD or decoded JWT. (should we compare the signatures instead?)
|
|
269
|
+
return JSON.stringify(secondOriginal.proof) === JSON.stringify(firstOriginal.proof);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
static isJsonLdAsString(original) {
|
|
273
|
+
return utils_1.ObjectUtils.isString(original) && original.includes('@context');
|
|
274
|
+
}
|
|
275
|
+
static isSdJwtDecodedCredential(original) {
|
|
276
|
+
return original.compactSdJwtVc !== undefined;
|
|
277
|
+
}
|
|
278
|
+
static isJwtDecodedCredential(original) {
|
|
279
|
+
return original.vc !== undefined && original.iss !== undefined;
|
|
280
|
+
}
|
|
281
|
+
static isJwtDecodedPresentation(original) {
|
|
282
|
+
return original.vp !== undefined && original.iss !== undefined;
|
|
283
|
+
}
|
|
284
|
+
static jwtEncodedPresentationToUniformPresentation(jwt, makeCredentialsUniform = true, opts) {
|
|
285
|
+
return CredentialMapper.jwtDecodedPresentationToUniformPresentation((0, jwt_decode_1.default)(jwt), makeCredentialsUniform, opts);
|
|
286
|
+
}
|
|
287
|
+
static jwtDecodedPresentationToUniformPresentation(decoded, makeCredentialsUniform = true, opts) {
|
|
288
|
+
const { iss, aud, jti, vp } = decoded, rest = __rest(decoded, ["iss", "aud", "jti", "vp"]);
|
|
289
|
+
const presentation = Object.assign(Object.assign({}, rest), vp);
|
|
290
|
+
if (makeCredentialsUniform) {
|
|
291
|
+
if (!vp.verifiableCredential) {
|
|
292
|
+
throw Error('Verifiable Presentation should have a verifiable credential at this point');
|
|
293
|
+
}
|
|
294
|
+
presentation.verifiableCredential = vp.verifiableCredential.map((vc) => CredentialMapper.toUniformCredential(vc, opts));
|
|
295
|
+
}
|
|
296
|
+
if (iss) {
|
|
297
|
+
const holder = presentation.holder;
|
|
298
|
+
if (holder) {
|
|
299
|
+
if (holder !== iss) {
|
|
300
|
+
throw new Error(`Inconsistent holders between JWT claim (${iss}) and VC value (${holder})`);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
presentation.holder = iss;
|
|
304
|
+
}
|
|
305
|
+
if (aud) {
|
|
306
|
+
const verifier = presentation.verifier;
|
|
307
|
+
if (verifier) {
|
|
308
|
+
if (verifier !== aud) {
|
|
309
|
+
throw new Error(`Inconsistent holders between JWT claim (${aud}) and VC value (${verifier})`);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
presentation.verifier = aud;
|
|
313
|
+
}
|
|
314
|
+
if (jti) {
|
|
315
|
+
const id = presentation.id;
|
|
316
|
+
if (id && id !== jti) {
|
|
317
|
+
throw new Error(`Inconsistent VP ids between JWT claim (${jti}) and VP value (${id})`);
|
|
318
|
+
}
|
|
319
|
+
presentation.id = jti;
|
|
320
|
+
}
|
|
321
|
+
return presentation;
|
|
322
|
+
}
|
|
323
|
+
static toUniformCredential(verifiableCredential, opts) {
|
|
324
|
+
var _a;
|
|
325
|
+
if (CredentialMapper.isSdJwtDecodedCredential(verifiableCredential)) {
|
|
326
|
+
throw new Error('Converting SD-JWT VC to uniform VC is not supported.');
|
|
327
|
+
}
|
|
328
|
+
const original = typeof verifiableCredential !== 'string' && CredentialMapper.hasJWTProofType(verifiableCredential)
|
|
329
|
+
? (_a = CredentialMapper.getFirstProof(verifiableCredential)) === null || _a === void 0 ? void 0 : _a.jwt
|
|
330
|
+
: verifiableCredential;
|
|
331
|
+
if (!original) {
|
|
332
|
+
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');
|
|
333
|
+
}
|
|
334
|
+
const decoded = CredentialMapper.decodeVerifiableCredential(original);
|
|
335
|
+
const isJwtEncoded = CredentialMapper.isJwtEncoded(original);
|
|
336
|
+
const isJwtDecoded = CredentialMapper.isJwtDecodedCredential(original);
|
|
337
|
+
if (isJwtDecoded || isJwtEncoded) {
|
|
338
|
+
return CredentialMapper.jwtDecodedCredentialToUniformCredential(decoded, opts);
|
|
339
|
+
}
|
|
340
|
+
else {
|
|
341
|
+
return decoded;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
static toUniformPresentation(presentation, opts) {
|
|
345
|
+
var _a;
|
|
346
|
+
if (CredentialMapper.isSdJwtDecodedCredential(presentation)) {
|
|
347
|
+
throw new Error('Converting SD-JWT VC to uniform VP is not supported.');
|
|
348
|
+
}
|
|
349
|
+
const proof = CredentialMapper.getFirstProof(presentation);
|
|
350
|
+
const original = typeof presentation !== 'string' && CredentialMapper.hasJWTProofType(presentation) ? proof === null || proof === void 0 ? void 0 : proof.jwt : presentation;
|
|
351
|
+
if (!original) {
|
|
352
|
+
throw Error('Could not determine original presentation, probably it was a converted JWT presentation, that is now missing the JWT value in the proof');
|
|
353
|
+
}
|
|
354
|
+
const decoded = CredentialMapper.decodeVerifiablePresentation(original);
|
|
355
|
+
const isJwtEncoded = CredentialMapper.isJwtEncoded(original);
|
|
356
|
+
const isJwtDecoded = CredentialMapper.isJwtDecodedPresentation(original);
|
|
357
|
+
const uniformPresentation = isJwtEncoded || isJwtDecoded
|
|
358
|
+
? CredentialMapper.jwtDecodedPresentationToUniformPresentation(decoded, false)
|
|
359
|
+
: decoded;
|
|
360
|
+
// At time of writing Velocity Networks does not conform to specification. Adding bare minimum @context section to stop parsers from crashing and whatnot
|
|
361
|
+
if ((opts === null || opts === void 0 ? void 0 : opts.addContextIfMissing) && !uniformPresentation['@context']) {
|
|
362
|
+
uniformPresentation['@context'] = ['https://www.w3.org/2018/credentials/v1'];
|
|
363
|
+
}
|
|
364
|
+
uniformPresentation.verifiableCredential = (_a = uniformPresentation.verifiableCredential) === null || _a === void 0 ? void 0 : _a.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)
|
|
365
|
+
return uniformPresentation;
|
|
366
|
+
}
|
|
367
|
+
static jwtEncodedCredentialToUniformCredential(jwt, opts) {
|
|
368
|
+
return CredentialMapper.jwtDecodedCredentialToUniformCredential((0, jwt_decode_1.default)(jwt), opts);
|
|
369
|
+
}
|
|
370
|
+
static jwtDecodedCredentialToUniformCredential(decoded, opts) {
|
|
371
|
+
const { exp, nbf, iss, vc, sub, jti } = decoded, rest = __rest(decoded, ["exp", "nbf", "iss", "vc", "sub", "jti"]);
|
|
372
|
+
const credential = Object.assign(Object.assign({}, rest), vc);
|
|
373
|
+
const maxSkewInMS = (opts === null || opts === void 0 ? void 0 : opts.maxTimeSkewInMS) !== undefined ? opts.maxTimeSkewInMS : 999;
|
|
374
|
+
if (exp) {
|
|
375
|
+
const expDate = credential.expirationDate;
|
|
376
|
+
const jwtExp = parseInt(exp.toString());
|
|
377
|
+
// fix seconds to millisecond for the date
|
|
378
|
+
const expDateAsStr = jwtExp < 9999999999 ? new Date(jwtExp * 1000).toISOString().replace(/\.000Z/, 'Z') : new Date(jwtExp).toISOString();
|
|
379
|
+
if (expDate && expDate !== expDateAsStr) {
|
|
380
|
+
const diff = Math.abs(new Date(expDateAsStr).getTime() - new Date(expDate).getTime());
|
|
381
|
+
if (!maxSkewInMS || diff > maxSkewInMS) {
|
|
382
|
+
throw new Error(`Inconsistent expiration dates between JWT claim (${expDateAsStr}) and VC value (${expDate})`);
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
credential.expirationDate = expDateAsStr;
|
|
386
|
+
}
|
|
387
|
+
if (nbf) {
|
|
388
|
+
const issuanceDate = credential.issuanceDate;
|
|
389
|
+
const jwtNbf = parseInt(nbf.toString());
|
|
390
|
+
// fix seconds to millisecs for the date
|
|
391
|
+
const nbfDateAsStr = jwtNbf < 9999999999 ? new Date(jwtNbf * 1000).toISOString().replace(/\.000Z/, 'Z') : new Date(jwtNbf).toISOString();
|
|
392
|
+
if (issuanceDate && issuanceDate !== nbfDateAsStr) {
|
|
393
|
+
const diff = Math.abs(new Date(nbfDateAsStr).getTime() - new Date(issuanceDate).getTime());
|
|
394
|
+
if (!maxSkewInMS || diff > maxSkewInMS) {
|
|
395
|
+
throw new Error(`Inconsistent issuance dates between JWT claim (${nbfDateAsStr}) and VC value (${issuanceDate})`);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
credential.issuanceDate = nbfDateAsStr;
|
|
399
|
+
}
|
|
400
|
+
if (iss) {
|
|
401
|
+
const issuer = credential.issuer;
|
|
402
|
+
if (issuer) {
|
|
403
|
+
if (typeof issuer === 'string') {
|
|
404
|
+
if (issuer !== iss) {
|
|
405
|
+
throw new Error(`Inconsistent issuers between JWT claim (${iss}) and VC value (${issuer})`);
|
|
406
|
+
}
|
|
407
|
+
}
|
|
408
|
+
else {
|
|
409
|
+
if (!issuer.id && Object.keys(issuer).length > 0) {
|
|
410
|
+
// We have an issuer object with more than 1 property but without an issuer id. Set it,
|
|
411
|
+
// because the default behaviour of did-jwt-vc is to remove the id value when creating JWTs
|
|
412
|
+
issuer.id = iss;
|
|
413
|
+
}
|
|
414
|
+
if (issuer.id !== iss) {
|
|
415
|
+
throw new Error(`Inconsistent issuers between JWT claim (${iss}) and VC value (${issuer.id})`);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
else {
|
|
420
|
+
credential.issuer = iss;
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
if (sub) {
|
|
424
|
+
const subjects = Array.isArray(credential.credentialSubject) ? credential.credentialSubject : [credential.credentialSubject];
|
|
425
|
+
for (let i = 0; i < subjects.length; i++) {
|
|
426
|
+
const csId = subjects[i].id;
|
|
427
|
+
if (csId && csId !== sub) {
|
|
428
|
+
throw new Error(`Inconsistent credential subject ids between JWT claim (${sub}) and VC value (${csId})`);
|
|
429
|
+
}
|
|
430
|
+
Array.isArray(credential.credentialSubject) ? (credential.credentialSubject[i].id = sub) : (credential.credentialSubject.id = sub);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
if (jti) {
|
|
434
|
+
const id = credential.id;
|
|
435
|
+
if (id && id !== jti) {
|
|
436
|
+
throw new Error(`Inconsistent credential ids between JWT claim (${jti}) and VC value (${id})`);
|
|
437
|
+
}
|
|
438
|
+
credential.id = jti;
|
|
439
|
+
}
|
|
440
|
+
return credential;
|
|
441
|
+
}
|
|
442
|
+
static toExternalVerifiableCredential(verifiableCredential) {
|
|
443
|
+
let proof;
|
|
444
|
+
if (verifiableCredential.proof) {
|
|
445
|
+
if (!verifiableCredential.proof.type) {
|
|
446
|
+
throw new Error('Verifiable credential proof is missing a type');
|
|
447
|
+
}
|
|
448
|
+
if (!verifiableCredential.proof.created) {
|
|
449
|
+
throw new Error('Verifiable credential proof is missing a created date');
|
|
450
|
+
}
|
|
451
|
+
if (!verifiableCredential.proof.proofPurpose) {
|
|
452
|
+
throw new Error('Verifiable credential proof is missing a proof purpose');
|
|
453
|
+
}
|
|
454
|
+
if (!verifiableCredential.proof.verificationMethod) {
|
|
455
|
+
throw new Error('Verifiable credential proof is missing a verification method');
|
|
456
|
+
}
|
|
457
|
+
proof = Object.assign(Object.assign({}, verifiableCredential.proof), { type: verifiableCredential.proof.type, created: verifiableCredential.proof.created, proofPurpose: verifiableCredential.proof.proofPurpose, verificationMethod: verifiableCredential.proof.verificationMethod });
|
|
458
|
+
}
|
|
459
|
+
return Object.assign(Object.assign({}, verifiableCredential), { type: verifiableCredential.type
|
|
460
|
+
? typeof verifiableCredential.type === 'string'
|
|
461
|
+
? [verifiableCredential.type]
|
|
462
|
+
: verifiableCredential.type
|
|
463
|
+
: ['VerifiableCredential'], proof });
|
|
464
|
+
}
|
|
465
|
+
static storedCredentialToOriginalFormat(credential) {
|
|
466
|
+
const type = CredentialMapper.detectDocumentType(credential);
|
|
467
|
+
if (typeof credential === 'string') {
|
|
468
|
+
if (type === 0 /* DocumentFormat.JWT */) {
|
|
469
|
+
return CredentialMapper.toCompactJWT(credential);
|
|
470
|
+
}
|
|
471
|
+
else if (type === 1 /* DocumentFormat.JSONLD */) {
|
|
472
|
+
return JSON.parse(credential);
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
else if (type === 0 /* DocumentFormat.JWT */ && 'vc' in credential) {
|
|
476
|
+
return CredentialMapper.toCompactJWT(credential);
|
|
477
|
+
}
|
|
478
|
+
return credential;
|
|
479
|
+
}
|
|
480
|
+
static storedPresentationToOriginalFormat(presentation) {
|
|
481
|
+
const type = CredentialMapper.detectDocumentType(presentation);
|
|
482
|
+
if (typeof presentation === 'string') {
|
|
483
|
+
if (type === 0 /* DocumentFormat.JWT */) {
|
|
484
|
+
return CredentialMapper.toCompactJWT(presentation);
|
|
485
|
+
}
|
|
486
|
+
else if (type === 1 /* DocumentFormat.JSONLD */) {
|
|
487
|
+
return JSON.parse(presentation);
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
else if (type === 0 /* DocumentFormat.JWT */ && 'vp' in presentation) {
|
|
491
|
+
return CredentialMapper.toCompactJWT(presentation);
|
|
492
|
+
}
|
|
493
|
+
return presentation;
|
|
494
|
+
}
|
|
495
|
+
static toCompactJWT(jwtDocument) {
|
|
496
|
+
if (!jwtDocument || CredentialMapper.detectDocumentType(jwtDocument) !== 0 /* DocumentFormat.JWT */) {
|
|
497
|
+
throw Error('Cannot convert non JWT credential to JWT');
|
|
498
|
+
}
|
|
499
|
+
if (typeof jwtDocument === 'string') {
|
|
500
|
+
return jwtDocument;
|
|
501
|
+
}
|
|
502
|
+
let proof;
|
|
503
|
+
if ('vp' in jwtDocument) {
|
|
504
|
+
proof = jwtDocument.vp.proof;
|
|
505
|
+
}
|
|
506
|
+
else if ('vc' in jwtDocument) {
|
|
507
|
+
proof = jwtDocument.vc.proof;
|
|
508
|
+
}
|
|
509
|
+
else {
|
|
510
|
+
proof = Array.isArray(jwtDocument.proof) ? jwtDocument.proof[0].jwt : jwtDocument.proof.jwt;
|
|
511
|
+
}
|
|
512
|
+
if (!proof) {
|
|
513
|
+
throw Error(`Could not get JWT from supplied document`);
|
|
514
|
+
}
|
|
515
|
+
return proof;
|
|
516
|
+
}
|
|
517
|
+
static detectDocumentType(document) {
|
|
518
|
+
if (this.isJsonLdAsString(document)) {
|
|
519
|
+
return 1 /* DocumentFormat.JSONLD */;
|
|
520
|
+
}
|
|
521
|
+
else if (this.isJwtEncoded(document)) {
|
|
522
|
+
return 0 /* DocumentFormat.JWT */;
|
|
523
|
+
}
|
|
524
|
+
else if (this.isSdJwtEncoded(document) || this.isSdJwtDecodedCredential(document)) {
|
|
525
|
+
return 2 /* DocumentFormat.SD_JWT_VC */;
|
|
526
|
+
}
|
|
527
|
+
const proofs = 'vc' in document ? document.vc.proof : 'vp' in document ? document.vp.proof : document.proof;
|
|
528
|
+
const proof = Array.isArray(proofs) ? proofs[0] : proofs;
|
|
529
|
+
if (proof === null || proof === void 0 ? void 0 : proof.jwt) {
|
|
530
|
+
return 0 /* DocumentFormat.JWT */;
|
|
531
|
+
}
|
|
532
|
+
else if ((proof === null || proof === void 0 ? void 0 : proof.type) === 'EthereumEip712Signature2021') {
|
|
533
|
+
return 3 /* DocumentFormat.EIP712 */;
|
|
534
|
+
}
|
|
535
|
+
return 1 /* DocumentFormat.JSONLD */;
|
|
536
|
+
}
|
|
537
|
+
static hasJWTProofType(document) {
|
|
538
|
+
var _a;
|
|
539
|
+
if (typeof document === 'string') {
|
|
540
|
+
return false;
|
|
541
|
+
}
|
|
542
|
+
return !!((_a = CredentialMapper.getFirstProof(document)) === null || _a === void 0 ? void 0 : _a.jwt);
|
|
543
|
+
}
|
|
544
|
+
static getFirstProof(document) {
|
|
545
|
+
if (!document || typeof document === 'string') {
|
|
546
|
+
return undefined;
|
|
547
|
+
}
|
|
548
|
+
const proofs = 'vc' in document ? document.vc.proof : 'vp' in document ? document.vp.proof : document.proof;
|
|
549
|
+
return Array.isArray(proofs) ? proofs[0] : proofs;
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
exports.CredentialMapper = CredentialMapper;
|
|
553
|
+
CredentialMapper.isWrappedSdJwtVerifiableCredential = types_1.isWrappedSdJwtVerifiableCredential;
|
|
554
|
+
CredentialMapper.isWrappedSdJwtVerifiablePresentation = types_1.isWrappedSdJwtVerifiablePresentation;
|
|
555
|
+
CredentialMapper.isWrappedW3CVerifiableCredential = types_1.isWrappedW3CVerifiableCredential;
|
|
556
|
+
CredentialMapper.isWrappedW3CVerifiablePresentation = types_1.isWrappedW3CVerifiablePresentation;
|
|
557
557
|
//# sourceMappingURL=credential-mapper.js.map
|