@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,686 +1,686 @@
|
|
|
1
|
-
import jwt_decode from 'jwt-decode'
|
|
2
|
-
import {
|
|
3
|
-
DocumentFormat,
|
|
4
|
-
IPresentation,
|
|
5
|
-
IProof,
|
|
6
|
-
IProofPurpose,
|
|
7
|
-
IProofType,
|
|
8
|
-
IVerifiableCredential,
|
|
9
|
-
IVerifiablePresentation,
|
|
10
|
-
JwtDecodedVerifiableCredential,
|
|
11
|
-
JwtDecodedVerifiablePresentation,
|
|
12
|
-
OriginalType,
|
|
13
|
-
OriginalVerifiableCredential,
|
|
14
|
-
OriginalVerifiablePresentation,
|
|
15
|
-
UniformVerifiablePresentation,
|
|
16
|
-
W3CVerifiableCredential,
|
|
17
|
-
W3CVerifiablePresentation,
|
|
18
|
-
WrappedVerifiableCredential,
|
|
19
|
-
WrappedVerifiablePresentation,
|
|
20
|
-
SdJwtDecodedVerifiableCredential,
|
|
21
|
-
SdJwtDecodedVerifiableCredentialPayload,
|
|
22
|
-
ICredential,
|
|
23
|
-
WrappedSdJwtVerifiableCredential,
|
|
24
|
-
WrappedW3CVerifiableCredential,
|
|
25
|
-
isWrappedSdJwtVerifiableCredential,
|
|
26
|
-
isWrappedSdJwtVerifiablePresentation,
|
|
27
|
-
isWrappedW3CVerifiableCredential,
|
|
28
|
-
isWrappedW3CVerifiablePresentation,
|
|
29
|
-
Hasher,
|
|
30
|
-
decodeSdJwtVc,
|
|
31
|
-
decodeSdJwtVcAsync,
|
|
32
|
-
AsyncHasher,
|
|
33
|
-
} from '../types'
|
|
34
|
-
import { ObjectUtils } from '../utils'
|
|
35
|
-
|
|
36
|
-
export class CredentialMapper {
|
|
37
|
-
/**
|
|
38
|
-
* 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
|
|
39
|
-
* with the other decode methods.
|
|
40
|
-
*/
|
|
41
|
-
static decodeSdJwtVcAsync(compactSdJwtVc: string, hasher: AsyncHasher) {
|
|
42
|
-
return decodeSdJwtVcAsync(compactSdJwtVc, hasher)
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Decodes a Verifiable Presentation to a uniform format.
|
|
47
|
-
*
|
|
48
|
-
* When decoding SD-JWT credentials, a hasher implementation must be provided. The hasher implementation must be sync. When using
|
|
49
|
-
* an async hasher implementation, use the decodeSdJwtVcAsync method instead and you can provide the decoded payload to methods
|
|
50
|
-
* instead of the compact SD-JWT.
|
|
51
|
-
*
|
|
52
|
-
* @param hasher Hasher implementation to use for SD-JWT decoding.
|
|
53
|
-
*/
|
|
54
|
-
static decodeVerifiablePresentation(
|
|
55
|
-
presentation: OriginalVerifiablePresentation,
|
|
56
|
-
hasher?: Hasher
|
|
57
|
-
): JwtDecodedVerifiablePresentation | IVerifiablePresentation | SdJwtDecodedVerifiableCredential {
|
|
58
|
-
if (CredentialMapper.isJwtEncoded(presentation)) {
|
|
59
|
-
const payload = jwt_decode(presentation as string) as JwtDecodedVerifiablePresentation
|
|
60
|
-
const header = jwt_decode(presentation as string, { header: true }) as Record<string, any>
|
|
61
|
-
|
|
62
|
-
payload.vp.proof = {
|
|
63
|
-
type: IProofType.JwtProof2020,
|
|
64
|
-
created: payload.nbf,
|
|
65
|
-
proofPurpose: IProofPurpose.authentication,
|
|
66
|
-
verificationMethod: header['kid'] ?? payload.iss,
|
|
67
|
-
jwt: presentation as string,
|
|
68
|
-
}
|
|
69
|
-
return payload
|
|
70
|
-
} else if (CredentialMapper.isJwtDecodedPresentation(presentation)) {
|
|
71
|
-
return presentation as JwtDecodedVerifiablePresentation
|
|
72
|
-
} else if (CredentialMapper.isSdJwtEncoded(presentation)) {
|
|
73
|
-
if (!hasher) {
|
|
74
|
-
throw new Error('Hasher implementation is required to decode SD-JWT')
|
|
75
|
-
}
|
|
76
|
-
return decodeSdJwtVc(presentation, hasher)
|
|
77
|
-
} else if (CredentialMapper.isSdJwtDecodedCredential(presentation)) {
|
|
78
|
-
return presentation as SdJwtDecodedVerifiableCredential
|
|
79
|
-
} else if (CredentialMapper.isJsonLdAsString(presentation)) {
|
|
80
|
-
return JSON.parse(presentation as string) as IVerifiablePresentation
|
|
81
|
-
} else {
|
|
82
|
-
return presentation as IVerifiablePresentation
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Decodes a Verifiable Credential to a uniform format.
|
|
88
|
-
*
|
|
89
|
-
* When decoding SD-JWT credentials, a hasher implementation must be provided. The hasher implementation must be sync. When using
|
|
90
|
-
* an async hasher implementation, use the decodeSdJwtVcAsync method instead and you can provide the decoded payload to methods
|
|
91
|
-
* instead of the compact SD-JWT.
|
|
92
|
-
*
|
|
93
|
-
* @param hasher Hasher implementation to use for SD-JWT decoding
|
|
94
|
-
*/
|
|
95
|
-
static decodeVerifiableCredential(
|
|
96
|
-
credential: OriginalVerifiableCredential,
|
|
97
|
-
hasher?: Hasher
|
|
98
|
-
): JwtDecodedVerifiableCredential | IVerifiableCredential | SdJwtDecodedVerifiableCredential {
|
|
99
|
-
if (CredentialMapper.isJwtEncoded(credential)) {
|
|
100
|
-
const payload = jwt_decode(credential as string) as JwtDecodedVerifiableCredential
|
|
101
|
-
const header = jwt_decode(credential as string, { header: true }) as Record<string, any>
|
|
102
|
-
payload.vc.proof = {
|
|
103
|
-
type: IProofType.JwtProof2020,
|
|
104
|
-
created: payload.nbf,
|
|
105
|
-
proofPurpose: IProofPurpose.authentication,
|
|
106
|
-
verificationMethod: header['kid'] ?? payload.iss,
|
|
107
|
-
jwt: credential as string,
|
|
108
|
-
}
|
|
109
|
-
return payload
|
|
110
|
-
} else if (CredentialMapper.isJwtDecodedCredential(credential)) {
|
|
111
|
-
return credential
|
|
112
|
-
} else if (CredentialMapper.isJsonLdAsString(credential)) {
|
|
113
|
-
return JSON.parse(credential as string) as IVerifiableCredential
|
|
114
|
-
} else if (CredentialMapper.isSdJwtEncoded(credential)) {
|
|
115
|
-
if (!hasher) {
|
|
116
|
-
throw new Error('Hasher implementation is required to decode SD-JWT')
|
|
117
|
-
}
|
|
118
|
-
return decodeSdJwtVc(credential, hasher)
|
|
119
|
-
} else if (CredentialMapper.isSdJwtDecodedCredential(credential)) {
|
|
120
|
-
return credential
|
|
121
|
-
} else {
|
|
122
|
-
return credential as IVerifiableCredential
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/**
|
|
127
|
-
* Converts a presentation to a wrapped presentation.
|
|
128
|
-
*
|
|
129
|
-
* When decoding SD-JWT credentials, a hasher implementation must be provided. The hasher implementation must be sync. When using
|
|
130
|
-
* an async hasher implementation, use the decodeSdJwtVcAsync method instead and you can provide the decoded payload to methods
|
|
131
|
-
* instead of the compact SD-JWT.
|
|
132
|
-
*
|
|
133
|
-
* @param hasher Hasher implementation to use for SD-JWT decoding
|
|
134
|
-
*/
|
|
135
|
-
static toWrappedVerifiablePresentation(
|
|
136
|
-
originalPresentation: OriginalVerifiablePresentation,
|
|
137
|
-
opts?: { maxTimeSkewInMS?: number; hasher?: Hasher }
|
|
138
|
-
): WrappedVerifiablePresentation {
|
|
139
|
-
// SD-JWT
|
|
140
|
-
if (CredentialMapper.isSdJwtDecodedCredential(originalPresentation) || CredentialMapper.isSdJwtEncoded(originalPresentation)) {
|
|
141
|
-
let decodedPresentation: SdJwtDecodedVerifiableCredential
|
|
142
|
-
if (CredentialMapper.isSdJwtEncoded(originalPresentation)) {
|
|
143
|
-
if (!opts?.hasher) {
|
|
144
|
-
throw new Error('Hasher implementation is required to decode SD-JWT')
|
|
145
|
-
}
|
|
146
|
-
decodedPresentation = decodeSdJwtVc(originalPresentation, opts.hasher)
|
|
147
|
-
} else {
|
|
148
|
-
decodedPresentation = originalPresentation
|
|
149
|
-
}
|
|
150
|
-
return {
|
|
151
|
-
type: CredentialMapper.isSdJwtDecodedCredential(originalPresentation) ? OriginalType.SD_JWT_VC_DECODED : OriginalType.SD_JWT_VC_ENCODED,
|
|
152
|
-
format: 'vc+sd-jwt',
|
|
153
|
-
original: originalPresentation,
|
|
154
|
-
presentation: decodedPresentation,
|
|
155
|
-
decoded: decodedPresentation.decodedPayload,
|
|
156
|
-
// NOTE: we also include the SD-JWT VC as the VC, as the SD-JWT acts as both the VC and the VP
|
|
157
|
-
vcs: [CredentialMapper.toWrappedVerifiableCredential(originalPresentation, opts) as WrappedSdJwtVerifiableCredential],
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
// If the VP is not an encoded/decoded SD-JWT, we assume it will be a W3C VC
|
|
162
|
-
const proof = CredentialMapper.getFirstProof(originalPresentation)
|
|
163
|
-
const original =
|
|
164
|
-
typeof originalPresentation !== 'string' && CredentialMapper.hasJWTProofType(originalPresentation) ? proof?.jwt : originalPresentation
|
|
165
|
-
if (!original) {
|
|
166
|
-
throw Error(
|
|
167
|
-
'Could not determine original presentation, probably it was a converted JWT presentation, that is now missing the JWT value in the proof'
|
|
168
|
-
)
|
|
169
|
-
}
|
|
170
|
-
const decoded = CredentialMapper.decodeVerifiablePresentation(original) as IVerifiablePresentation | JwtDecodedVerifiablePresentation
|
|
171
|
-
const isJwtEncoded: boolean = CredentialMapper.isJwtEncoded(original)
|
|
172
|
-
const isJwtDecoded: boolean = CredentialMapper.isJwtDecodedPresentation(original)
|
|
173
|
-
|
|
174
|
-
const type = isJwtEncoded ? OriginalType.JWT_ENCODED : isJwtDecoded ? OriginalType.JWT_DECODED : OriginalType.JSONLD
|
|
175
|
-
const format = isJwtDecoded || isJwtEncoded ? 'jwt_vp' : ('ldp_vp' as const)
|
|
176
|
-
|
|
177
|
-
let vp: OriginalVerifiablePresentation
|
|
178
|
-
if (isJwtEncoded || isJwtDecoded) {
|
|
179
|
-
vp = CredentialMapper.jwtDecodedPresentationToUniformPresentation(decoded as JwtDecodedVerifiablePresentation, false, opts)
|
|
180
|
-
} else {
|
|
181
|
-
vp = decoded as IVerifiablePresentation
|
|
182
|
-
}
|
|
183
|
-
if (!vp || !('verifiableCredential' in vp) || !vp.verifiableCredential || vp.verifiableCredential.length === 0) {
|
|
184
|
-
throw Error(`VP needs to have at least one verifiable credential at this point`)
|
|
185
|
-
}
|
|
186
|
-
const vcs = CredentialMapper.toWrappedVerifiableCredentials(
|
|
187
|
-
vp.verifiableCredential /*.map(value => value.original)*/,
|
|
188
|
-
opts
|
|
189
|
-
) as WrappedW3CVerifiableCredential[]
|
|
190
|
-
|
|
191
|
-
const presentation = {
|
|
192
|
-
...vp,
|
|
193
|
-
verifiableCredential: vcs, // We overwrite the verifiableCredentials with wrapped versions, making it an InternalVerifiablePresentation. Note: we keep the singular key name of the vc data model
|
|
194
|
-
} as UniformVerifiablePresentation
|
|
195
|
-
return {
|
|
196
|
-
type,
|
|
197
|
-
format,
|
|
198
|
-
original,
|
|
199
|
-
decoded,
|
|
200
|
-
presentation,
|
|
201
|
-
vcs,
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* Converts a list of credentials to a list of wrapped credentials.
|
|
207
|
-
*
|
|
208
|
-
* When decoding SD-JWT credentials, a hasher implementation must be provided. The hasher implementation must be sync. When using
|
|
209
|
-
* an async hasher implementation, use the decodeSdJwtVcAsync method instead and you can provide the decoded payload to methods
|
|
210
|
-
* instead of the compact SD-JWT.
|
|
211
|
-
*
|
|
212
|
-
* @param hasher Hasher implementation to use for SD-JWT decoding
|
|
213
|
-
*/
|
|
214
|
-
static toWrappedVerifiableCredentials(
|
|
215
|
-
verifiableCredentials: OriginalVerifiableCredential[],
|
|
216
|
-
opts?: { maxTimeSkewInMS?: number; hasher?: Hasher }
|
|
217
|
-
): WrappedVerifiableCredential[] {
|
|
218
|
-
return verifiableCredentials.map((vc) => CredentialMapper.toWrappedVerifiableCredential(vc, opts))
|
|
219
|
-
}
|
|
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(
|
|
231
|
-
verifiableCredential: OriginalVerifiableCredential,
|
|
232
|
-
opts?: { maxTimeSkewInMS?: number; hasher?: Hasher }
|
|
233
|
-
): WrappedVerifiableCredential {
|
|
234
|
-
// SD-JWT
|
|
235
|
-
if (CredentialMapper.isSdJwtDecodedCredential(verifiableCredential) || CredentialMapper.isSdJwtEncoded(verifiableCredential)) {
|
|
236
|
-
let decodedCredential: SdJwtDecodedVerifiableCredential
|
|
237
|
-
if (CredentialMapper.isSdJwtEncoded(verifiableCredential)) {
|
|
238
|
-
if (!opts?.hasher) {
|
|
239
|
-
throw new Error('Hasher implementation is required to decode SD-JWT')
|
|
240
|
-
}
|
|
241
|
-
decodedCredential = decodeSdJwtVc(verifiableCredential, opts.hasher)
|
|
242
|
-
} else {
|
|
243
|
-
decodedCredential = verifiableCredential
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
return {
|
|
247
|
-
type: CredentialMapper.isSdJwtDecodedCredential(verifiableCredential) ? OriginalType.SD_JWT_VC_DECODED : OriginalType.SD_JWT_VC_ENCODED,
|
|
248
|
-
format: 'vc+sd-jwt',
|
|
249
|
-
original: verifiableCredential,
|
|
250
|
-
credential: decodedCredential,
|
|
251
|
-
decoded: decodedCredential.decodedPayload,
|
|
252
|
-
}
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
// If the VC is not an encoded/decoded SD-JWT, we assume it will be a W3C VC
|
|
256
|
-
const proof = CredentialMapper.getFirstProof(verifiableCredential)
|
|
257
|
-
const original = CredentialMapper.hasJWTProofType(verifiableCredential) && proof ? proof.jwt ?? verifiableCredential : verifiableCredential
|
|
258
|
-
if (!original) {
|
|
259
|
-
throw Error(
|
|
260
|
-
'Could not determine original credential, probably it was a converted JWT credential, that is now missing the JWT value in the proof'
|
|
261
|
-
)
|
|
262
|
-
}
|
|
263
|
-
const decoded = CredentialMapper.decodeVerifiableCredential(original) as JwtDecodedVerifiableCredential | IVerifiableCredential
|
|
264
|
-
|
|
265
|
-
const isJwtEncoded = CredentialMapper.isJwtEncoded(original)
|
|
266
|
-
const isJwtDecoded = CredentialMapper.isJwtDecodedCredential(original)
|
|
267
|
-
const type = isJwtEncoded ? OriginalType.JWT_ENCODED : isJwtDecoded ? OriginalType.JWT_DECODED : OriginalType.JSONLD
|
|
268
|
-
|
|
269
|
-
const credential =
|
|
270
|
-
isJwtEncoded || isJwtDecoded
|
|
271
|
-
? CredentialMapper.jwtDecodedCredentialToUniformCredential(decoded as JwtDecodedVerifiableCredential, opts)
|
|
272
|
-
: (decoded as IVerifiableCredential)
|
|
273
|
-
|
|
274
|
-
const format = isJwtEncoded || isJwtDecoded ? ('jwt_vc' as const) : ('ldp_vc' as const)
|
|
275
|
-
return {
|
|
276
|
-
original,
|
|
277
|
-
decoded,
|
|
278
|
-
format,
|
|
279
|
-
type,
|
|
280
|
-
credential,
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
public static isJwtEncoded(original: OriginalVerifiableCredential | OriginalVerifiablePresentation): original is string {
|
|
285
|
-
return ObjectUtils.isString(original) && original.startsWith('ey') && !original.includes('~')
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
public static isSdJwtEncoded(original: OriginalVerifiableCredential | OriginalVerifiablePresentation): original is string {
|
|
289
|
-
return ObjectUtils.isString(original) && original.startsWith('ey') && original.includes('~')
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
public static isW3cCredential(credential: ICredential | SdJwtDecodedVerifiableCredential): credential is ICredential {
|
|
293
|
-
return '@context' in credential && ((credential as ICredential).type?.includes('VerifiableCredential') || false)
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
public static isW3cPresentation(
|
|
297
|
-
presentation: UniformVerifiablePresentation | IPresentation | SdJwtDecodedVerifiableCredential
|
|
298
|
-
): presentation is IPresentation {
|
|
299
|
-
return '@context' in presentation && ((presentation as IPresentation).type?.includes('VerifiablePresentation') || false)
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
public static isSdJwtDecodedCredentialPayload(
|
|
303
|
-
credential: ICredential | SdJwtDecodedVerifiableCredentialPayload
|
|
304
|
-
): credential is SdJwtDecodedVerifiableCredentialPayload {
|
|
305
|
-
return 'vct' in credential
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
public static areOriginalVerifiableCredentialsEqual(firstOriginal: OriginalVerifiableCredential, secondOriginal: OriginalVerifiableCredential) {
|
|
309
|
-
// String (e.g. encoded jwt or SD-JWT)
|
|
310
|
-
if (typeof firstOriginal === 'string' || typeof secondOriginal === 'string') {
|
|
311
|
-
return firstOriginal === secondOriginal
|
|
312
|
-
} else if (CredentialMapper.isSdJwtDecodedCredential(firstOriginal) || CredentialMapper.isSdJwtDecodedCredential(secondOriginal)) {
|
|
313
|
-
return firstOriginal.compactSdJwtVc === secondOriginal.compactSdJwtVc
|
|
314
|
-
} else {
|
|
315
|
-
// JSON-LD or decoded JWT. (should we compare the signatures instead?)
|
|
316
|
-
return JSON.stringify(secondOriginal.proof) === JSON.stringify(firstOriginal.proof)
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
private static isJsonLdAsString(original: OriginalVerifiableCredential | OriginalVerifiablePresentation): original is string {
|
|
321
|
-
return ObjectUtils.isString(original) && original.includes('@context')
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
public static isSdJwtDecodedCredential(
|
|
325
|
-
original: OriginalVerifiableCredential | OriginalVerifiablePresentation | ICredential | IPresentation
|
|
326
|
-
): original is SdJwtDecodedVerifiableCredential {
|
|
327
|
-
return (<SdJwtDecodedVerifiableCredential>original).compactSdJwtVc !== undefined
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
public static isJwtDecodedCredential(original: OriginalVerifiableCredential): original is JwtDecodedVerifiableCredential {
|
|
331
|
-
return (<JwtDecodedVerifiableCredential>original).vc !== undefined && (<JwtDecodedVerifiableCredential>original).iss !== undefined
|
|
332
|
-
}
|
|
333
|
-
|
|
334
|
-
public static isJwtDecodedPresentation(original: OriginalVerifiablePresentation): original is JwtDecodedVerifiablePresentation {
|
|
335
|
-
return (<JwtDecodedVerifiablePresentation>original).vp !== undefined && (<JwtDecodedVerifiablePresentation>original).iss !== undefined
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
public static isWrappedSdJwtVerifiableCredential = isWrappedSdJwtVerifiableCredential
|
|
339
|
-
public static isWrappedSdJwtVerifiablePresentation = isWrappedSdJwtVerifiablePresentation
|
|
340
|
-
public static isWrappedW3CVerifiableCredential = isWrappedW3CVerifiableCredential
|
|
341
|
-
public static isWrappedW3CVerifiablePresentation = isWrappedW3CVerifiablePresentation
|
|
342
|
-
|
|
343
|
-
static jwtEncodedPresentationToUniformPresentation(
|
|
344
|
-
jwt: string,
|
|
345
|
-
makeCredentialsUniform: boolean = true,
|
|
346
|
-
opts?: { maxTimeSkewInMS?: number }
|
|
347
|
-
): IPresentation {
|
|
348
|
-
return CredentialMapper.jwtDecodedPresentationToUniformPresentation(jwt_decode(jwt), makeCredentialsUniform, opts)
|
|
349
|
-
}
|
|
350
|
-
|
|
351
|
-
static jwtDecodedPresentationToUniformPresentation(
|
|
352
|
-
decoded: JwtDecodedVerifiablePresentation,
|
|
353
|
-
makeCredentialsUniform: boolean = true,
|
|
354
|
-
opts?: { maxTimeSkewInMS?: number }
|
|
355
|
-
): IVerifiablePresentation {
|
|
356
|
-
const { iss, aud, jti, vp, ...rest } = decoded
|
|
357
|
-
|
|
358
|
-
const presentation: IVerifiablePresentation = {
|
|
359
|
-
...rest,
|
|
360
|
-
...vp,
|
|
361
|
-
}
|
|
362
|
-
if (makeCredentialsUniform) {
|
|
363
|
-
if (!vp.verifiableCredential) {
|
|
364
|
-
throw Error('Verifiable Presentation should have a verifiable credential at this point')
|
|
365
|
-
}
|
|
366
|
-
presentation.verifiableCredential = vp.verifiableCredential.map((vc) => CredentialMapper.toUniformCredential(vc, opts))
|
|
367
|
-
}
|
|
368
|
-
if (iss) {
|
|
369
|
-
const holder = presentation.holder
|
|
370
|
-
if (holder) {
|
|
371
|
-
if (holder !== iss) {
|
|
372
|
-
throw new Error(`Inconsistent holders between JWT claim (${iss}) and VC value (${holder})`)
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
presentation.holder = iss
|
|
376
|
-
}
|
|
377
|
-
if (aud) {
|
|
378
|
-
const verifier = presentation.verifier
|
|
379
|
-
if (verifier) {
|
|
380
|
-
if (verifier !== aud) {
|
|
381
|
-
throw new Error(`Inconsistent holders between JWT claim (${aud}) and VC value (${verifier})`)
|
|
382
|
-
}
|
|
383
|
-
}
|
|
384
|
-
presentation.verifier = aud
|
|
385
|
-
}
|
|
386
|
-
if (jti) {
|
|
387
|
-
const id = presentation.id
|
|
388
|
-
if (id && id !== jti) {
|
|
389
|
-
throw new Error(`Inconsistent VP ids between JWT claim (${jti}) and VP value (${id})`)
|
|
390
|
-
}
|
|
391
|
-
presentation.id = jti
|
|
392
|
-
}
|
|
393
|
-
return presentation
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
static toUniformCredential(
|
|
397
|
-
verifiableCredential: OriginalVerifiableCredential,
|
|
398
|
-
opts?: {
|
|
399
|
-
maxTimeSkewInMS?: number
|
|
400
|
-
}
|
|
401
|
-
): IVerifiableCredential {
|
|
402
|
-
if (CredentialMapper.isSdJwtDecodedCredential(verifiableCredential)) {
|
|
403
|
-
throw new Error('Converting SD-JWT VC to uniform VC is not supported.')
|
|
404
|
-
}
|
|
405
|
-
const original =
|
|
406
|
-
typeof verifiableCredential !== 'string' && CredentialMapper.hasJWTProofType(verifiableCredential)
|
|
407
|
-
? CredentialMapper.getFirstProof(verifiableCredential)?.jwt
|
|
408
|
-
: verifiableCredential
|
|
409
|
-
if (!original) {
|
|
410
|
-
throw Error(
|
|
411
|
-
'Could not determine original credential from passed in credential. Probably because a JWT proof type was present, but now is not available anymore'
|
|
412
|
-
)
|
|
413
|
-
}
|
|
414
|
-
const decoded = CredentialMapper.decodeVerifiableCredential(original)
|
|
415
|
-
|
|
416
|
-
const isJwtEncoded: boolean = CredentialMapper.isJwtEncoded(original)
|
|
417
|
-
const isJwtDecoded: boolean = CredentialMapper.isJwtDecodedCredential(original)
|
|
418
|
-
|
|
419
|
-
if (isJwtDecoded || isJwtEncoded) {
|
|
420
|
-
return CredentialMapper.jwtDecodedCredentialToUniformCredential(decoded as JwtDecodedVerifiableCredential, opts)
|
|
421
|
-
} else {
|
|
422
|
-
return decoded as IVerifiableCredential
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
|
|
426
|
-
static toUniformPresentation(
|
|
427
|
-
presentation: OriginalVerifiablePresentation,
|
|
428
|
-
opts?: { maxTimeSkewInMS?: number; addContextIfMissing?: boolean }
|
|
429
|
-
): IVerifiablePresentation {
|
|
430
|
-
if (CredentialMapper.isSdJwtDecodedCredential(presentation)) {
|
|
431
|
-
throw new Error('Converting SD-JWT VC to uniform VP is not supported.')
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
const proof = CredentialMapper.getFirstProof(presentation)
|
|
435
|
-
const original = typeof presentation !== 'string' && CredentialMapper.hasJWTProofType(presentation) ? proof?.jwt : presentation
|
|
436
|
-
if (!original) {
|
|
437
|
-
throw Error(
|
|
438
|
-
'Could not determine original presentation, probably it was a converted JWT presentation, that is now missing the JWT value in the proof'
|
|
439
|
-
)
|
|
440
|
-
}
|
|
441
|
-
const decoded = CredentialMapper.decodeVerifiablePresentation(original)
|
|
442
|
-
const isJwtEncoded: boolean = CredentialMapper.isJwtEncoded(original)
|
|
443
|
-
const isJwtDecoded: boolean = CredentialMapper.isJwtDecodedPresentation(original)
|
|
444
|
-
const uniformPresentation =
|
|
445
|
-
isJwtEncoded || isJwtDecoded
|
|
446
|
-
? CredentialMapper.jwtDecodedPresentationToUniformPresentation(decoded as JwtDecodedVerifiablePresentation, false)
|
|
447
|
-
: (decoded as IVerifiablePresentation)
|
|
448
|
-
|
|
449
|
-
// At time of writing Velocity Networks does not conform to specification. Adding bare minimum @context section to stop parsers from crashing and whatnot
|
|
450
|
-
if (opts?.addContextIfMissing && !uniformPresentation['@context']) {
|
|
451
|
-
uniformPresentation['@context'] = ['https://www.w3.org/2018/credentials/v1']
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
uniformPresentation.verifiableCredential = uniformPresentation.verifiableCredential?.map((vc) =>
|
|
455
|
-
CredentialMapper.toUniformCredential(vc, opts)
|
|
456
|
-
) as IVerifiableCredential[] // We cast it because we IPresentation needs a VC. The internal Credential doesn't have the required Proof anymore (that is intended)
|
|
457
|
-
return uniformPresentation
|
|
458
|
-
}
|
|
459
|
-
|
|
460
|
-
static jwtEncodedCredentialToUniformCredential(
|
|
461
|
-
jwt: string,
|
|
462
|
-
opts?: {
|
|
463
|
-
maxTimeSkewInMS?: number
|
|
464
|
-
}
|
|
465
|
-
): IVerifiableCredential {
|
|
466
|
-
return CredentialMapper.jwtDecodedCredentialToUniformCredential(jwt_decode(jwt), opts)
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
static jwtDecodedCredentialToUniformCredential(
|
|
470
|
-
decoded: JwtDecodedVerifiableCredential,
|
|
471
|
-
opts?: { maxTimeSkewInMS?: number }
|
|
472
|
-
): IVerifiableCredential {
|
|
473
|
-
const { exp, nbf, iss, vc, sub, jti, ...rest } = decoded
|
|
474
|
-
const credential: IVerifiableCredential = {
|
|
475
|
-
...rest,
|
|
476
|
-
...vc,
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
const maxSkewInMS = opts?.maxTimeSkewInMS !== undefined ? opts.maxTimeSkewInMS : 999
|
|
480
|
-
|
|
481
|
-
if (exp) {
|
|
482
|
-
const expDate = credential.expirationDate
|
|
483
|
-
const jwtExp = parseInt(exp.toString())
|
|
484
|
-
// fix seconds to millisecond for the date
|
|
485
|
-
const expDateAsStr = jwtExp < 9999999999 ? new Date(jwtExp * 1000).toISOString().replace(/\.000Z/, 'Z') : new Date(jwtExp).toISOString()
|
|
486
|
-
if (expDate && expDate !== expDateAsStr) {
|
|
487
|
-
const diff = Math.abs(new Date(expDateAsStr).getTime() - new Date(expDate).getTime())
|
|
488
|
-
if (!maxSkewInMS || diff > maxSkewInMS) {
|
|
489
|
-
throw new Error(`Inconsistent expiration dates between JWT claim (${expDateAsStr}) and VC value (${expDate})`)
|
|
490
|
-
}
|
|
491
|
-
}
|
|
492
|
-
credential.expirationDate = expDateAsStr
|
|
493
|
-
}
|
|
494
|
-
|
|
495
|
-
if (nbf) {
|
|
496
|
-
const issuanceDate = credential.issuanceDate
|
|
497
|
-
const jwtNbf = parseInt(nbf.toString())
|
|
498
|
-
// fix seconds to millisecs for the date
|
|
499
|
-
const nbfDateAsStr = jwtNbf < 9999999999 ? new Date(jwtNbf * 1000).toISOString().replace(/\.000Z/, 'Z') : new Date(jwtNbf).toISOString()
|
|
500
|
-
if (issuanceDate && issuanceDate !== nbfDateAsStr) {
|
|
501
|
-
const diff = Math.abs(new Date(nbfDateAsStr).getTime() - new Date(issuanceDate).getTime())
|
|
502
|
-
if (!maxSkewInMS || diff > maxSkewInMS) {
|
|
503
|
-
throw new Error(`Inconsistent issuance dates between JWT claim (${nbfDateAsStr}) and VC value (${issuanceDate})`)
|
|
504
|
-
}
|
|
505
|
-
}
|
|
506
|
-
credential.issuanceDate = nbfDateAsStr
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
if (iss) {
|
|
510
|
-
const issuer = credential.issuer
|
|
511
|
-
if (issuer) {
|
|
512
|
-
if (typeof issuer === 'string') {
|
|
513
|
-
if (issuer !== iss) {
|
|
514
|
-
throw new Error(`Inconsistent issuers between JWT claim (${iss}) and VC value (${issuer})`)
|
|
515
|
-
}
|
|
516
|
-
} else {
|
|
517
|
-
if (!issuer.id && Object.keys(issuer).length > 0) {
|
|
518
|
-
// We have an issuer object with more than 1 property but without an issuer id. Set it,
|
|
519
|
-
// because the default behaviour of did-jwt-vc is to remove the id value when creating JWTs
|
|
520
|
-
issuer.id = iss
|
|
521
|
-
}
|
|
522
|
-
if (issuer.id !== iss) {
|
|
523
|
-
throw new Error(`Inconsistent issuers between JWT claim (${iss}) and VC value (${issuer.id})`)
|
|
524
|
-
}
|
|
525
|
-
}
|
|
526
|
-
} else {
|
|
527
|
-
credential.issuer = iss
|
|
528
|
-
}
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
if (sub) {
|
|
532
|
-
const subjects = Array.isArray(credential.credentialSubject) ? credential.credentialSubject : [credential.credentialSubject]
|
|
533
|
-
for (let i = 0; i < subjects.length; i++) {
|
|
534
|
-
const csId = subjects[i].id
|
|
535
|
-
if (csId && csId !== sub) {
|
|
536
|
-
throw new Error(`Inconsistent credential subject ids between JWT claim (${sub}) and VC value (${csId})`)
|
|
537
|
-
}
|
|
538
|
-
Array.isArray(credential.credentialSubject) ? (credential.credentialSubject[i].id = sub) : (credential.credentialSubject.id = sub)
|
|
539
|
-
}
|
|
540
|
-
}
|
|
541
|
-
if (jti) {
|
|
542
|
-
const id = credential.id
|
|
543
|
-
if (id && id !== jti) {
|
|
544
|
-
throw new Error(`Inconsistent credential ids between JWT claim (${jti}) and VC value (${id})`)
|
|
545
|
-
}
|
|
546
|
-
credential.id = jti
|
|
547
|
-
}
|
|
548
|
-
|
|
549
|
-
return credential
|
|
550
|
-
}
|
|
551
|
-
|
|
552
|
-
static toExternalVerifiableCredential(verifiableCredential: any): IVerifiableCredential {
|
|
553
|
-
let proof
|
|
554
|
-
if (verifiableCredential.proof) {
|
|
555
|
-
if (!verifiableCredential.proof.type) {
|
|
556
|
-
throw new Error('Verifiable credential proof is missing a type')
|
|
557
|
-
}
|
|
558
|
-
|
|
559
|
-
if (!verifiableCredential.proof.created) {
|
|
560
|
-
throw new Error('Verifiable credential proof is missing a created date')
|
|
561
|
-
}
|
|
562
|
-
|
|
563
|
-
if (!verifiableCredential.proof.proofPurpose) {
|
|
564
|
-
throw new Error('Verifiable credential proof is missing a proof purpose')
|
|
565
|
-
}
|
|
566
|
-
|
|
567
|
-
if (!verifiableCredential.proof.verificationMethod) {
|
|
568
|
-
throw new Error('Verifiable credential proof is missing a verification method')
|
|
569
|
-
}
|
|
570
|
-
proof = {
|
|
571
|
-
...verifiableCredential.proof,
|
|
572
|
-
type: verifiableCredential.proof.type,
|
|
573
|
-
created: verifiableCredential.proof.created,
|
|
574
|
-
proofPurpose: verifiableCredential.proof.proofPurpose,
|
|
575
|
-
verificationMethod: verifiableCredential.proof.verificationMethod,
|
|
576
|
-
}
|
|
577
|
-
}
|
|
578
|
-
|
|
579
|
-
return {
|
|
580
|
-
...verifiableCredential,
|
|
581
|
-
type: verifiableCredential.type
|
|
582
|
-
? typeof verifiableCredential.type === 'string'
|
|
583
|
-
? [verifiableCredential.type]
|
|
584
|
-
: verifiableCredential.type
|
|
585
|
-
: ['VerifiableCredential'],
|
|
586
|
-
proof,
|
|
587
|
-
}
|
|
588
|
-
}
|
|
589
|
-
|
|
590
|
-
static storedCredentialToOriginalFormat(credential: OriginalVerifiableCredential): W3CVerifiableCredential {
|
|
591
|
-
const type: DocumentFormat = CredentialMapper.detectDocumentType(credential)
|
|
592
|
-
if (typeof credential === 'string') {
|
|
593
|
-
if (type === DocumentFormat.JWT) {
|
|
594
|
-
return CredentialMapper.toCompactJWT(credential)
|
|
595
|
-
} else if (type === DocumentFormat.JSONLD) {
|
|
596
|
-
return JSON.parse(credential)
|
|
597
|
-
}
|
|
598
|
-
} else if (type === DocumentFormat.JWT && 'vc' in credential) {
|
|
599
|
-
return CredentialMapper.toCompactJWT(credential)
|
|
600
|
-
}
|
|
601
|
-
return credential as W3CVerifiableCredential
|
|
602
|
-
}
|
|
603
|
-
|
|
604
|
-
static storedPresentationToOriginalFormat(presentation: OriginalVerifiablePresentation): W3CVerifiablePresentation {
|
|
605
|
-
const type: DocumentFormat = CredentialMapper.detectDocumentType(presentation)
|
|
606
|
-
if (typeof presentation === 'string') {
|
|
607
|
-
if (type === DocumentFormat.JWT) {
|
|
608
|
-
return CredentialMapper.toCompactJWT(presentation)
|
|
609
|
-
} else if (type === DocumentFormat.JSONLD) {
|
|
610
|
-
return JSON.parse(presentation)
|
|
611
|
-
}
|
|
612
|
-
} else if (type === DocumentFormat.JWT && 'vp' in presentation) {
|
|
613
|
-
return CredentialMapper.toCompactJWT(presentation)
|
|
614
|
-
}
|
|
615
|
-
return presentation as W3CVerifiablePresentation
|
|
616
|
-
}
|
|
617
|
-
|
|
618
|
-
static toCompactJWT(
|
|
619
|
-
jwtDocument: W3CVerifiableCredential | JwtDecodedVerifiableCredential | W3CVerifiablePresentation | JwtDecodedVerifiablePresentation | string
|
|
620
|
-
): string {
|
|
621
|
-
if (!jwtDocument || CredentialMapper.detectDocumentType(jwtDocument) !== DocumentFormat.JWT) {
|
|
622
|
-
throw Error('Cannot convert non JWT credential to JWT')
|
|
623
|
-
}
|
|
624
|
-
if (typeof jwtDocument === 'string') {
|
|
625
|
-
return jwtDocument
|
|
626
|
-
}
|
|
627
|
-
let proof: string | undefined
|
|
628
|
-
if ('vp' in jwtDocument) {
|
|
629
|
-
proof = jwtDocument.vp.proof
|
|
630
|
-
} else if ('vc' in jwtDocument) {
|
|
631
|
-
proof = jwtDocument.vc.proof
|
|
632
|
-
} else {
|
|
633
|
-
proof = Array.isArray(jwtDocument.proof) ? jwtDocument.proof[0].jwt : jwtDocument.proof.jwt
|
|
634
|
-
}
|
|
635
|
-
if (!proof) {
|
|
636
|
-
throw Error(`Could not get JWT from supplied document`)
|
|
637
|
-
}
|
|
638
|
-
return proof
|
|
639
|
-
}
|
|
640
|
-
|
|
641
|
-
static detectDocumentType(
|
|
642
|
-
document:
|
|
643
|
-
| W3CVerifiableCredential
|
|
644
|
-
| W3CVerifiablePresentation
|
|
645
|
-
| JwtDecodedVerifiableCredential
|
|
646
|
-
| JwtDecodedVerifiablePresentation
|
|
647
|
-
| SdJwtDecodedVerifiableCredential
|
|
648
|
-
): DocumentFormat {
|
|
649
|
-
if (this.isJsonLdAsString(document)) {
|
|
650
|
-
return DocumentFormat.JSONLD
|
|
651
|
-
} else if (this.isJwtEncoded(document)) {
|
|
652
|
-
return DocumentFormat.JWT
|
|
653
|
-
} else if (this.isSdJwtEncoded(document) || this.isSdJwtDecodedCredential(document as any)) {
|
|
654
|
-
return DocumentFormat.SD_JWT_VC
|
|
655
|
-
}
|
|
656
|
-
|
|
657
|
-
const proofs = 'vc' in document ? document.vc.proof : 'vp' in document ? document.vp.proof : (<IVerifiableCredential>document).proof
|
|
658
|
-
const proof: IProof = Array.isArray(proofs) ? proofs[0] : proofs
|
|
659
|
-
|
|
660
|
-
if (proof?.jwt) {
|
|
661
|
-
return DocumentFormat.JWT
|
|
662
|
-
} else if (proof?.type === 'EthereumEip712Signature2021') {
|
|
663
|
-
return DocumentFormat.EIP712
|
|
664
|
-
}
|
|
665
|
-
return DocumentFormat.JSONLD
|
|
666
|
-
}
|
|
667
|
-
|
|
668
|
-
private static hasJWTProofType(
|
|
669
|
-
document: W3CVerifiableCredential | W3CVerifiablePresentation | JwtDecodedVerifiableCredential | JwtDecodedVerifiablePresentation
|
|
670
|
-
): boolean {
|
|
671
|
-
if (typeof document === 'string') {
|
|
672
|
-
return false
|
|
673
|
-
}
|
|
674
|
-
return !!CredentialMapper.getFirstProof(document)?.jwt
|
|
675
|
-
}
|
|
676
|
-
|
|
677
|
-
private static getFirstProof(
|
|
678
|
-
document: W3CVerifiableCredential | W3CVerifiablePresentation | JwtDecodedVerifiableCredential | JwtDecodedVerifiablePresentation
|
|
679
|
-
): IProof | undefined {
|
|
680
|
-
if (!document || typeof document === 'string') {
|
|
681
|
-
return undefined
|
|
682
|
-
}
|
|
683
|
-
const proofs = 'vc' in document ? document.vc.proof : 'vp' in document ? document.vp.proof : (<IVerifiableCredential>document).proof
|
|
684
|
-
return Array.isArray(proofs) ? proofs[0] : proofs
|
|
685
|
-
}
|
|
686
|
-
}
|
|
1
|
+
import jwt_decode from 'jwt-decode'
|
|
2
|
+
import {
|
|
3
|
+
DocumentFormat,
|
|
4
|
+
IPresentation,
|
|
5
|
+
IProof,
|
|
6
|
+
IProofPurpose,
|
|
7
|
+
IProofType,
|
|
8
|
+
IVerifiableCredential,
|
|
9
|
+
IVerifiablePresentation,
|
|
10
|
+
JwtDecodedVerifiableCredential,
|
|
11
|
+
JwtDecodedVerifiablePresentation,
|
|
12
|
+
OriginalType,
|
|
13
|
+
OriginalVerifiableCredential,
|
|
14
|
+
OriginalVerifiablePresentation,
|
|
15
|
+
UniformVerifiablePresentation,
|
|
16
|
+
W3CVerifiableCredential,
|
|
17
|
+
W3CVerifiablePresentation,
|
|
18
|
+
WrappedVerifiableCredential,
|
|
19
|
+
WrappedVerifiablePresentation,
|
|
20
|
+
SdJwtDecodedVerifiableCredential,
|
|
21
|
+
SdJwtDecodedVerifiableCredentialPayload,
|
|
22
|
+
ICredential,
|
|
23
|
+
WrappedSdJwtVerifiableCredential,
|
|
24
|
+
WrappedW3CVerifiableCredential,
|
|
25
|
+
isWrappedSdJwtVerifiableCredential,
|
|
26
|
+
isWrappedSdJwtVerifiablePresentation,
|
|
27
|
+
isWrappedW3CVerifiableCredential,
|
|
28
|
+
isWrappedW3CVerifiablePresentation,
|
|
29
|
+
Hasher,
|
|
30
|
+
decodeSdJwtVc,
|
|
31
|
+
decodeSdJwtVcAsync,
|
|
32
|
+
AsyncHasher,
|
|
33
|
+
} from '../types'
|
|
34
|
+
import { ObjectUtils } from '../utils'
|
|
35
|
+
|
|
36
|
+
export class CredentialMapper {
|
|
37
|
+
/**
|
|
38
|
+
* 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
|
|
39
|
+
* with the other decode methods.
|
|
40
|
+
*/
|
|
41
|
+
static decodeSdJwtVcAsync(compactSdJwtVc: string, hasher: AsyncHasher) {
|
|
42
|
+
return decodeSdJwtVcAsync(compactSdJwtVc, hasher)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Decodes a Verifiable Presentation to a uniform format.
|
|
47
|
+
*
|
|
48
|
+
* When decoding SD-JWT credentials, a hasher implementation must be provided. The hasher implementation must be sync. When using
|
|
49
|
+
* an async hasher implementation, use the decodeSdJwtVcAsync method instead and you can provide the decoded payload to methods
|
|
50
|
+
* instead of the compact SD-JWT.
|
|
51
|
+
*
|
|
52
|
+
* @param hasher Hasher implementation to use for SD-JWT decoding.
|
|
53
|
+
*/
|
|
54
|
+
static decodeVerifiablePresentation(
|
|
55
|
+
presentation: OriginalVerifiablePresentation,
|
|
56
|
+
hasher?: Hasher
|
|
57
|
+
): JwtDecodedVerifiablePresentation | IVerifiablePresentation | SdJwtDecodedVerifiableCredential {
|
|
58
|
+
if (CredentialMapper.isJwtEncoded(presentation)) {
|
|
59
|
+
const payload = jwt_decode(presentation as string) as JwtDecodedVerifiablePresentation
|
|
60
|
+
const header = jwt_decode(presentation as string, { header: true }) as Record<string, any>
|
|
61
|
+
|
|
62
|
+
payload.vp.proof = {
|
|
63
|
+
type: IProofType.JwtProof2020,
|
|
64
|
+
created: payload.nbf,
|
|
65
|
+
proofPurpose: IProofPurpose.authentication,
|
|
66
|
+
verificationMethod: header['kid'] ?? payload.iss,
|
|
67
|
+
jwt: presentation as string,
|
|
68
|
+
}
|
|
69
|
+
return payload
|
|
70
|
+
} else if (CredentialMapper.isJwtDecodedPresentation(presentation)) {
|
|
71
|
+
return presentation as JwtDecodedVerifiablePresentation
|
|
72
|
+
} else if (CredentialMapper.isSdJwtEncoded(presentation)) {
|
|
73
|
+
if (!hasher) {
|
|
74
|
+
throw new Error('Hasher implementation is required to decode SD-JWT')
|
|
75
|
+
}
|
|
76
|
+
return decodeSdJwtVc(presentation, hasher)
|
|
77
|
+
} else if (CredentialMapper.isSdJwtDecodedCredential(presentation)) {
|
|
78
|
+
return presentation as SdJwtDecodedVerifiableCredential
|
|
79
|
+
} else if (CredentialMapper.isJsonLdAsString(presentation)) {
|
|
80
|
+
return JSON.parse(presentation as string) as IVerifiablePresentation
|
|
81
|
+
} else {
|
|
82
|
+
return presentation as IVerifiablePresentation
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Decodes a Verifiable Credential to a uniform format.
|
|
88
|
+
*
|
|
89
|
+
* When decoding SD-JWT credentials, a hasher implementation must be provided. The hasher implementation must be sync. When using
|
|
90
|
+
* an async hasher implementation, use the decodeSdJwtVcAsync method instead and you can provide the decoded payload to methods
|
|
91
|
+
* instead of the compact SD-JWT.
|
|
92
|
+
*
|
|
93
|
+
* @param hasher Hasher implementation to use for SD-JWT decoding
|
|
94
|
+
*/
|
|
95
|
+
static decodeVerifiableCredential(
|
|
96
|
+
credential: OriginalVerifiableCredential,
|
|
97
|
+
hasher?: Hasher
|
|
98
|
+
): JwtDecodedVerifiableCredential | IVerifiableCredential | SdJwtDecodedVerifiableCredential {
|
|
99
|
+
if (CredentialMapper.isJwtEncoded(credential)) {
|
|
100
|
+
const payload = jwt_decode(credential as string) as JwtDecodedVerifiableCredential
|
|
101
|
+
const header = jwt_decode(credential as string, { header: true }) as Record<string, any>
|
|
102
|
+
payload.vc.proof = {
|
|
103
|
+
type: IProofType.JwtProof2020,
|
|
104
|
+
created: payload.nbf,
|
|
105
|
+
proofPurpose: IProofPurpose.authentication,
|
|
106
|
+
verificationMethod: header['kid'] ?? payload.iss,
|
|
107
|
+
jwt: credential as string,
|
|
108
|
+
}
|
|
109
|
+
return payload
|
|
110
|
+
} else if (CredentialMapper.isJwtDecodedCredential(credential)) {
|
|
111
|
+
return credential
|
|
112
|
+
} else if (CredentialMapper.isJsonLdAsString(credential)) {
|
|
113
|
+
return JSON.parse(credential as string) as IVerifiableCredential
|
|
114
|
+
} else if (CredentialMapper.isSdJwtEncoded(credential)) {
|
|
115
|
+
if (!hasher) {
|
|
116
|
+
throw new Error('Hasher implementation is required to decode SD-JWT')
|
|
117
|
+
}
|
|
118
|
+
return decodeSdJwtVc(credential, hasher)
|
|
119
|
+
} else if (CredentialMapper.isSdJwtDecodedCredential(credential)) {
|
|
120
|
+
return credential
|
|
121
|
+
} else {
|
|
122
|
+
return credential as IVerifiableCredential
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Converts a presentation to a wrapped presentation.
|
|
128
|
+
*
|
|
129
|
+
* When decoding SD-JWT credentials, a hasher implementation must be provided. The hasher implementation must be sync. When using
|
|
130
|
+
* an async hasher implementation, use the decodeSdJwtVcAsync method instead and you can provide the decoded payload to methods
|
|
131
|
+
* instead of the compact SD-JWT.
|
|
132
|
+
*
|
|
133
|
+
* @param hasher Hasher implementation to use for SD-JWT decoding
|
|
134
|
+
*/
|
|
135
|
+
static toWrappedVerifiablePresentation(
|
|
136
|
+
originalPresentation: OriginalVerifiablePresentation,
|
|
137
|
+
opts?: { maxTimeSkewInMS?: number; hasher?: Hasher }
|
|
138
|
+
): WrappedVerifiablePresentation {
|
|
139
|
+
// SD-JWT
|
|
140
|
+
if (CredentialMapper.isSdJwtDecodedCredential(originalPresentation) || CredentialMapper.isSdJwtEncoded(originalPresentation)) {
|
|
141
|
+
let decodedPresentation: SdJwtDecodedVerifiableCredential
|
|
142
|
+
if (CredentialMapper.isSdJwtEncoded(originalPresentation)) {
|
|
143
|
+
if (!opts?.hasher) {
|
|
144
|
+
throw new Error('Hasher implementation is required to decode SD-JWT')
|
|
145
|
+
}
|
|
146
|
+
decodedPresentation = decodeSdJwtVc(originalPresentation, opts.hasher)
|
|
147
|
+
} else {
|
|
148
|
+
decodedPresentation = originalPresentation
|
|
149
|
+
}
|
|
150
|
+
return {
|
|
151
|
+
type: CredentialMapper.isSdJwtDecodedCredential(originalPresentation) ? OriginalType.SD_JWT_VC_DECODED : OriginalType.SD_JWT_VC_ENCODED,
|
|
152
|
+
format: 'vc+sd-jwt',
|
|
153
|
+
original: originalPresentation,
|
|
154
|
+
presentation: decodedPresentation,
|
|
155
|
+
decoded: decodedPresentation.decodedPayload,
|
|
156
|
+
// NOTE: we also include the SD-JWT VC as the VC, as the SD-JWT acts as both the VC and the VP
|
|
157
|
+
vcs: [CredentialMapper.toWrappedVerifiableCredential(originalPresentation, opts) as WrappedSdJwtVerifiableCredential],
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// If the VP is not an encoded/decoded SD-JWT, we assume it will be a W3C VC
|
|
162
|
+
const proof = CredentialMapper.getFirstProof(originalPresentation)
|
|
163
|
+
const original =
|
|
164
|
+
typeof originalPresentation !== 'string' && CredentialMapper.hasJWTProofType(originalPresentation) ? proof?.jwt : originalPresentation
|
|
165
|
+
if (!original) {
|
|
166
|
+
throw Error(
|
|
167
|
+
'Could not determine original presentation, probably it was a converted JWT presentation, that is now missing the JWT value in the proof'
|
|
168
|
+
)
|
|
169
|
+
}
|
|
170
|
+
const decoded = CredentialMapper.decodeVerifiablePresentation(original) as IVerifiablePresentation | JwtDecodedVerifiablePresentation
|
|
171
|
+
const isJwtEncoded: boolean = CredentialMapper.isJwtEncoded(original)
|
|
172
|
+
const isJwtDecoded: boolean = CredentialMapper.isJwtDecodedPresentation(original)
|
|
173
|
+
|
|
174
|
+
const type = isJwtEncoded ? OriginalType.JWT_ENCODED : isJwtDecoded ? OriginalType.JWT_DECODED : OriginalType.JSONLD
|
|
175
|
+
const format = isJwtDecoded || isJwtEncoded ? 'jwt_vp' : ('ldp_vp' as const)
|
|
176
|
+
|
|
177
|
+
let vp: OriginalVerifiablePresentation
|
|
178
|
+
if (isJwtEncoded || isJwtDecoded) {
|
|
179
|
+
vp = CredentialMapper.jwtDecodedPresentationToUniformPresentation(decoded as JwtDecodedVerifiablePresentation, false, opts)
|
|
180
|
+
} else {
|
|
181
|
+
vp = decoded as IVerifiablePresentation
|
|
182
|
+
}
|
|
183
|
+
if (!vp || !('verifiableCredential' in vp) || !vp.verifiableCredential || vp.verifiableCredential.length === 0) {
|
|
184
|
+
throw Error(`VP needs to have at least one verifiable credential at this point`)
|
|
185
|
+
}
|
|
186
|
+
const vcs = CredentialMapper.toWrappedVerifiableCredentials(
|
|
187
|
+
vp.verifiableCredential /*.map(value => value.original)*/,
|
|
188
|
+
opts
|
|
189
|
+
) as WrappedW3CVerifiableCredential[]
|
|
190
|
+
|
|
191
|
+
const presentation = {
|
|
192
|
+
...vp,
|
|
193
|
+
verifiableCredential: vcs, // We overwrite the verifiableCredentials with wrapped versions, making it an InternalVerifiablePresentation. Note: we keep the singular key name of the vc data model
|
|
194
|
+
} as UniformVerifiablePresentation
|
|
195
|
+
return {
|
|
196
|
+
type,
|
|
197
|
+
format,
|
|
198
|
+
original,
|
|
199
|
+
decoded,
|
|
200
|
+
presentation,
|
|
201
|
+
vcs,
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Converts a list of credentials to a list of wrapped credentials.
|
|
207
|
+
*
|
|
208
|
+
* When decoding SD-JWT credentials, a hasher implementation must be provided. The hasher implementation must be sync. When using
|
|
209
|
+
* an async hasher implementation, use the decodeSdJwtVcAsync method instead and you can provide the decoded payload to methods
|
|
210
|
+
* instead of the compact SD-JWT.
|
|
211
|
+
*
|
|
212
|
+
* @param hasher Hasher implementation to use for SD-JWT decoding
|
|
213
|
+
*/
|
|
214
|
+
static toWrappedVerifiableCredentials(
|
|
215
|
+
verifiableCredentials: OriginalVerifiableCredential[],
|
|
216
|
+
opts?: { maxTimeSkewInMS?: number; hasher?: Hasher }
|
|
217
|
+
): WrappedVerifiableCredential[] {
|
|
218
|
+
return verifiableCredentials.map((vc) => CredentialMapper.toWrappedVerifiableCredential(vc, opts))
|
|
219
|
+
}
|
|
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(
|
|
231
|
+
verifiableCredential: OriginalVerifiableCredential,
|
|
232
|
+
opts?: { maxTimeSkewInMS?: number; hasher?: Hasher }
|
|
233
|
+
): WrappedVerifiableCredential {
|
|
234
|
+
// SD-JWT
|
|
235
|
+
if (CredentialMapper.isSdJwtDecodedCredential(verifiableCredential) || CredentialMapper.isSdJwtEncoded(verifiableCredential)) {
|
|
236
|
+
let decodedCredential: SdJwtDecodedVerifiableCredential
|
|
237
|
+
if (CredentialMapper.isSdJwtEncoded(verifiableCredential)) {
|
|
238
|
+
if (!opts?.hasher) {
|
|
239
|
+
throw new Error('Hasher implementation is required to decode SD-JWT')
|
|
240
|
+
}
|
|
241
|
+
decodedCredential = decodeSdJwtVc(verifiableCredential, opts.hasher)
|
|
242
|
+
} else {
|
|
243
|
+
decodedCredential = verifiableCredential
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
return {
|
|
247
|
+
type: CredentialMapper.isSdJwtDecodedCredential(verifiableCredential) ? OriginalType.SD_JWT_VC_DECODED : OriginalType.SD_JWT_VC_ENCODED,
|
|
248
|
+
format: 'vc+sd-jwt',
|
|
249
|
+
original: verifiableCredential,
|
|
250
|
+
credential: decodedCredential,
|
|
251
|
+
decoded: decodedCredential.decodedPayload,
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// If the VC is not an encoded/decoded SD-JWT, we assume it will be a W3C VC
|
|
256
|
+
const proof = CredentialMapper.getFirstProof(verifiableCredential)
|
|
257
|
+
const original = CredentialMapper.hasJWTProofType(verifiableCredential) && proof ? proof.jwt ?? verifiableCredential : verifiableCredential
|
|
258
|
+
if (!original) {
|
|
259
|
+
throw Error(
|
|
260
|
+
'Could not determine original credential, probably it was a converted JWT credential, that is now missing the JWT value in the proof'
|
|
261
|
+
)
|
|
262
|
+
}
|
|
263
|
+
const decoded = CredentialMapper.decodeVerifiableCredential(original) as JwtDecodedVerifiableCredential | IVerifiableCredential
|
|
264
|
+
|
|
265
|
+
const isJwtEncoded = CredentialMapper.isJwtEncoded(original)
|
|
266
|
+
const isJwtDecoded = CredentialMapper.isJwtDecodedCredential(original)
|
|
267
|
+
const type = isJwtEncoded ? OriginalType.JWT_ENCODED : isJwtDecoded ? OriginalType.JWT_DECODED : OriginalType.JSONLD
|
|
268
|
+
|
|
269
|
+
const credential =
|
|
270
|
+
isJwtEncoded || isJwtDecoded
|
|
271
|
+
? CredentialMapper.jwtDecodedCredentialToUniformCredential(decoded as JwtDecodedVerifiableCredential, opts)
|
|
272
|
+
: (decoded as IVerifiableCredential)
|
|
273
|
+
|
|
274
|
+
const format = isJwtEncoded || isJwtDecoded ? ('jwt_vc' as const) : ('ldp_vc' as const)
|
|
275
|
+
return {
|
|
276
|
+
original,
|
|
277
|
+
decoded,
|
|
278
|
+
format,
|
|
279
|
+
type,
|
|
280
|
+
credential,
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
public static isJwtEncoded(original: OriginalVerifiableCredential | OriginalVerifiablePresentation): original is string {
|
|
285
|
+
return ObjectUtils.isString(original) && original.startsWith('ey') && !original.includes('~')
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
public static isSdJwtEncoded(original: OriginalVerifiableCredential | OriginalVerifiablePresentation): original is string {
|
|
289
|
+
return ObjectUtils.isString(original) && original.startsWith('ey') && original.includes('~')
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
public static isW3cCredential(credential: ICredential | SdJwtDecodedVerifiableCredential): credential is ICredential {
|
|
293
|
+
return '@context' in credential && ((credential as ICredential).type?.includes('VerifiableCredential') || false)
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
public static isW3cPresentation(
|
|
297
|
+
presentation: UniformVerifiablePresentation | IPresentation | SdJwtDecodedVerifiableCredential
|
|
298
|
+
): presentation is IPresentation {
|
|
299
|
+
return '@context' in presentation && ((presentation as IPresentation).type?.includes('VerifiablePresentation') || false)
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
public static isSdJwtDecodedCredentialPayload(
|
|
303
|
+
credential: ICredential | SdJwtDecodedVerifiableCredentialPayload
|
|
304
|
+
): credential is SdJwtDecodedVerifiableCredentialPayload {
|
|
305
|
+
return 'vct' in credential
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
public static areOriginalVerifiableCredentialsEqual(firstOriginal: OriginalVerifiableCredential, secondOriginal: OriginalVerifiableCredential) {
|
|
309
|
+
// String (e.g. encoded jwt or SD-JWT)
|
|
310
|
+
if (typeof firstOriginal === 'string' || typeof secondOriginal === 'string') {
|
|
311
|
+
return firstOriginal === secondOriginal
|
|
312
|
+
} else if (CredentialMapper.isSdJwtDecodedCredential(firstOriginal) || CredentialMapper.isSdJwtDecodedCredential(secondOriginal)) {
|
|
313
|
+
return firstOriginal.compactSdJwtVc === secondOriginal.compactSdJwtVc
|
|
314
|
+
} else {
|
|
315
|
+
// JSON-LD or decoded JWT. (should we compare the signatures instead?)
|
|
316
|
+
return JSON.stringify(secondOriginal.proof) === JSON.stringify(firstOriginal.proof)
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
private static isJsonLdAsString(original: OriginalVerifiableCredential | OriginalVerifiablePresentation): original is string {
|
|
321
|
+
return ObjectUtils.isString(original) && original.includes('@context')
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
public static isSdJwtDecodedCredential(
|
|
325
|
+
original: OriginalVerifiableCredential | OriginalVerifiablePresentation | ICredential | IPresentation
|
|
326
|
+
): original is SdJwtDecodedVerifiableCredential {
|
|
327
|
+
return (<SdJwtDecodedVerifiableCredential>original).compactSdJwtVc !== undefined
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
public static isJwtDecodedCredential(original: OriginalVerifiableCredential): original is JwtDecodedVerifiableCredential {
|
|
331
|
+
return (<JwtDecodedVerifiableCredential>original).vc !== undefined && (<JwtDecodedVerifiableCredential>original).iss !== undefined
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
public static isJwtDecodedPresentation(original: OriginalVerifiablePresentation): original is JwtDecodedVerifiablePresentation {
|
|
335
|
+
return (<JwtDecodedVerifiablePresentation>original).vp !== undefined && (<JwtDecodedVerifiablePresentation>original).iss !== undefined
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
public static isWrappedSdJwtVerifiableCredential = isWrappedSdJwtVerifiableCredential
|
|
339
|
+
public static isWrappedSdJwtVerifiablePresentation = isWrappedSdJwtVerifiablePresentation
|
|
340
|
+
public static isWrappedW3CVerifiableCredential = isWrappedW3CVerifiableCredential
|
|
341
|
+
public static isWrappedW3CVerifiablePresentation = isWrappedW3CVerifiablePresentation
|
|
342
|
+
|
|
343
|
+
static jwtEncodedPresentationToUniformPresentation(
|
|
344
|
+
jwt: string,
|
|
345
|
+
makeCredentialsUniform: boolean = true,
|
|
346
|
+
opts?: { maxTimeSkewInMS?: number }
|
|
347
|
+
): IPresentation {
|
|
348
|
+
return CredentialMapper.jwtDecodedPresentationToUniformPresentation(jwt_decode(jwt), makeCredentialsUniform, opts)
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
static jwtDecodedPresentationToUniformPresentation(
|
|
352
|
+
decoded: JwtDecodedVerifiablePresentation,
|
|
353
|
+
makeCredentialsUniform: boolean = true,
|
|
354
|
+
opts?: { maxTimeSkewInMS?: number }
|
|
355
|
+
): IVerifiablePresentation {
|
|
356
|
+
const { iss, aud, jti, vp, ...rest } = decoded
|
|
357
|
+
|
|
358
|
+
const presentation: IVerifiablePresentation = {
|
|
359
|
+
...rest,
|
|
360
|
+
...vp,
|
|
361
|
+
}
|
|
362
|
+
if (makeCredentialsUniform) {
|
|
363
|
+
if (!vp.verifiableCredential) {
|
|
364
|
+
throw Error('Verifiable Presentation should have a verifiable credential at this point')
|
|
365
|
+
}
|
|
366
|
+
presentation.verifiableCredential = vp.verifiableCredential.map((vc) => CredentialMapper.toUniformCredential(vc, opts))
|
|
367
|
+
}
|
|
368
|
+
if (iss) {
|
|
369
|
+
const holder = presentation.holder
|
|
370
|
+
if (holder) {
|
|
371
|
+
if (holder !== iss) {
|
|
372
|
+
throw new Error(`Inconsistent holders between JWT claim (${iss}) and VC value (${holder})`)
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
presentation.holder = iss
|
|
376
|
+
}
|
|
377
|
+
if (aud) {
|
|
378
|
+
const verifier = presentation.verifier
|
|
379
|
+
if (verifier) {
|
|
380
|
+
if (verifier !== aud) {
|
|
381
|
+
throw new Error(`Inconsistent holders between JWT claim (${aud}) and VC value (${verifier})`)
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
presentation.verifier = aud
|
|
385
|
+
}
|
|
386
|
+
if (jti) {
|
|
387
|
+
const id = presentation.id
|
|
388
|
+
if (id && id !== jti) {
|
|
389
|
+
throw new Error(`Inconsistent VP ids between JWT claim (${jti}) and VP value (${id})`)
|
|
390
|
+
}
|
|
391
|
+
presentation.id = jti
|
|
392
|
+
}
|
|
393
|
+
return presentation
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
static toUniformCredential(
|
|
397
|
+
verifiableCredential: OriginalVerifiableCredential,
|
|
398
|
+
opts?: {
|
|
399
|
+
maxTimeSkewInMS?: number
|
|
400
|
+
}
|
|
401
|
+
): IVerifiableCredential {
|
|
402
|
+
if (CredentialMapper.isSdJwtDecodedCredential(verifiableCredential)) {
|
|
403
|
+
throw new Error('Converting SD-JWT VC to uniform VC is not supported.')
|
|
404
|
+
}
|
|
405
|
+
const original =
|
|
406
|
+
typeof verifiableCredential !== 'string' && CredentialMapper.hasJWTProofType(verifiableCredential)
|
|
407
|
+
? CredentialMapper.getFirstProof(verifiableCredential)?.jwt
|
|
408
|
+
: verifiableCredential
|
|
409
|
+
if (!original) {
|
|
410
|
+
throw Error(
|
|
411
|
+
'Could not determine original credential from passed in credential. Probably because a JWT proof type was present, but now is not available anymore'
|
|
412
|
+
)
|
|
413
|
+
}
|
|
414
|
+
const decoded = CredentialMapper.decodeVerifiableCredential(original)
|
|
415
|
+
|
|
416
|
+
const isJwtEncoded: boolean = CredentialMapper.isJwtEncoded(original)
|
|
417
|
+
const isJwtDecoded: boolean = CredentialMapper.isJwtDecodedCredential(original)
|
|
418
|
+
|
|
419
|
+
if (isJwtDecoded || isJwtEncoded) {
|
|
420
|
+
return CredentialMapper.jwtDecodedCredentialToUniformCredential(decoded as JwtDecodedVerifiableCredential, opts)
|
|
421
|
+
} else {
|
|
422
|
+
return decoded as IVerifiableCredential
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
static toUniformPresentation(
|
|
427
|
+
presentation: OriginalVerifiablePresentation,
|
|
428
|
+
opts?: { maxTimeSkewInMS?: number; addContextIfMissing?: boolean }
|
|
429
|
+
): IVerifiablePresentation {
|
|
430
|
+
if (CredentialMapper.isSdJwtDecodedCredential(presentation)) {
|
|
431
|
+
throw new Error('Converting SD-JWT VC to uniform VP is not supported.')
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
const proof = CredentialMapper.getFirstProof(presentation)
|
|
435
|
+
const original = typeof presentation !== 'string' && CredentialMapper.hasJWTProofType(presentation) ? proof?.jwt : presentation
|
|
436
|
+
if (!original) {
|
|
437
|
+
throw Error(
|
|
438
|
+
'Could not determine original presentation, probably it was a converted JWT presentation, that is now missing the JWT value in the proof'
|
|
439
|
+
)
|
|
440
|
+
}
|
|
441
|
+
const decoded = CredentialMapper.decodeVerifiablePresentation(original)
|
|
442
|
+
const isJwtEncoded: boolean = CredentialMapper.isJwtEncoded(original)
|
|
443
|
+
const isJwtDecoded: boolean = CredentialMapper.isJwtDecodedPresentation(original)
|
|
444
|
+
const uniformPresentation =
|
|
445
|
+
isJwtEncoded || isJwtDecoded
|
|
446
|
+
? CredentialMapper.jwtDecodedPresentationToUniformPresentation(decoded as JwtDecodedVerifiablePresentation, false)
|
|
447
|
+
: (decoded as IVerifiablePresentation)
|
|
448
|
+
|
|
449
|
+
// At time of writing Velocity Networks does not conform to specification. Adding bare minimum @context section to stop parsers from crashing and whatnot
|
|
450
|
+
if (opts?.addContextIfMissing && !uniformPresentation['@context']) {
|
|
451
|
+
uniformPresentation['@context'] = ['https://www.w3.org/2018/credentials/v1']
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
uniformPresentation.verifiableCredential = uniformPresentation.verifiableCredential?.map((vc) =>
|
|
455
|
+
CredentialMapper.toUniformCredential(vc, opts)
|
|
456
|
+
) as IVerifiableCredential[] // We cast it because we IPresentation needs a VC. The internal Credential doesn't have the required Proof anymore (that is intended)
|
|
457
|
+
return uniformPresentation
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
static jwtEncodedCredentialToUniformCredential(
|
|
461
|
+
jwt: string,
|
|
462
|
+
opts?: {
|
|
463
|
+
maxTimeSkewInMS?: number
|
|
464
|
+
}
|
|
465
|
+
): IVerifiableCredential {
|
|
466
|
+
return CredentialMapper.jwtDecodedCredentialToUniformCredential(jwt_decode(jwt), opts)
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
static jwtDecodedCredentialToUniformCredential(
|
|
470
|
+
decoded: JwtDecodedVerifiableCredential,
|
|
471
|
+
opts?: { maxTimeSkewInMS?: number }
|
|
472
|
+
): IVerifiableCredential {
|
|
473
|
+
const { exp, nbf, iss, vc, sub, jti, ...rest } = decoded
|
|
474
|
+
const credential: IVerifiableCredential = {
|
|
475
|
+
...rest,
|
|
476
|
+
...vc,
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
const maxSkewInMS = opts?.maxTimeSkewInMS !== undefined ? opts.maxTimeSkewInMS : 999
|
|
480
|
+
|
|
481
|
+
if (exp) {
|
|
482
|
+
const expDate = credential.expirationDate
|
|
483
|
+
const jwtExp = parseInt(exp.toString())
|
|
484
|
+
// fix seconds to millisecond for the date
|
|
485
|
+
const expDateAsStr = jwtExp < 9999999999 ? new Date(jwtExp * 1000).toISOString().replace(/\.000Z/, 'Z') : new Date(jwtExp).toISOString()
|
|
486
|
+
if (expDate && expDate !== expDateAsStr) {
|
|
487
|
+
const diff = Math.abs(new Date(expDateAsStr).getTime() - new Date(expDate).getTime())
|
|
488
|
+
if (!maxSkewInMS || diff > maxSkewInMS) {
|
|
489
|
+
throw new Error(`Inconsistent expiration dates between JWT claim (${expDateAsStr}) and VC value (${expDate})`)
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
credential.expirationDate = expDateAsStr
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
if (nbf) {
|
|
496
|
+
const issuanceDate = credential.issuanceDate
|
|
497
|
+
const jwtNbf = parseInt(nbf.toString())
|
|
498
|
+
// fix seconds to millisecs for the date
|
|
499
|
+
const nbfDateAsStr = jwtNbf < 9999999999 ? new Date(jwtNbf * 1000).toISOString().replace(/\.000Z/, 'Z') : new Date(jwtNbf).toISOString()
|
|
500
|
+
if (issuanceDate && issuanceDate !== nbfDateAsStr) {
|
|
501
|
+
const diff = Math.abs(new Date(nbfDateAsStr).getTime() - new Date(issuanceDate).getTime())
|
|
502
|
+
if (!maxSkewInMS || diff > maxSkewInMS) {
|
|
503
|
+
throw new Error(`Inconsistent issuance dates between JWT claim (${nbfDateAsStr}) and VC value (${issuanceDate})`)
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
credential.issuanceDate = nbfDateAsStr
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
if (iss) {
|
|
510
|
+
const issuer = credential.issuer
|
|
511
|
+
if (issuer) {
|
|
512
|
+
if (typeof issuer === 'string') {
|
|
513
|
+
if (issuer !== iss) {
|
|
514
|
+
throw new Error(`Inconsistent issuers between JWT claim (${iss}) and VC value (${issuer})`)
|
|
515
|
+
}
|
|
516
|
+
} else {
|
|
517
|
+
if (!issuer.id && Object.keys(issuer).length > 0) {
|
|
518
|
+
// We have an issuer object with more than 1 property but without an issuer id. Set it,
|
|
519
|
+
// because the default behaviour of did-jwt-vc is to remove the id value when creating JWTs
|
|
520
|
+
issuer.id = iss
|
|
521
|
+
}
|
|
522
|
+
if (issuer.id !== iss) {
|
|
523
|
+
throw new Error(`Inconsistent issuers between JWT claim (${iss}) and VC value (${issuer.id})`)
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
} else {
|
|
527
|
+
credential.issuer = iss
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
if (sub) {
|
|
532
|
+
const subjects = Array.isArray(credential.credentialSubject) ? credential.credentialSubject : [credential.credentialSubject]
|
|
533
|
+
for (let i = 0; i < subjects.length; i++) {
|
|
534
|
+
const csId = subjects[i].id
|
|
535
|
+
if (csId && csId !== sub) {
|
|
536
|
+
throw new Error(`Inconsistent credential subject ids between JWT claim (${sub}) and VC value (${csId})`)
|
|
537
|
+
}
|
|
538
|
+
Array.isArray(credential.credentialSubject) ? (credential.credentialSubject[i].id = sub) : (credential.credentialSubject.id = sub)
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
if (jti) {
|
|
542
|
+
const id = credential.id
|
|
543
|
+
if (id && id !== jti) {
|
|
544
|
+
throw new Error(`Inconsistent credential ids between JWT claim (${jti}) and VC value (${id})`)
|
|
545
|
+
}
|
|
546
|
+
credential.id = jti
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
return credential
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
static toExternalVerifiableCredential(verifiableCredential: any): IVerifiableCredential {
|
|
553
|
+
let proof
|
|
554
|
+
if (verifiableCredential.proof) {
|
|
555
|
+
if (!verifiableCredential.proof.type) {
|
|
556
|
+
throw new Error('Verifiable credential proof is missing a type')
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
if (!verifiableCredential.proof.created) {
|
|
560
|
+
throw new Error('Verifiable credential proof is missing a created date')
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
if (!verifiableCredential.proof.proofPurpose) {
|
|
564
|
+
throw new Error('Verifiable credential proof is missing a proof purpose')
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
if (!verifiableCredential.proof.verificationMethod) {
|
|
568
|
+
throw new Error('Verifiable credential proof is missing a verification method')
|
|
569
|
+
}
|
|
570
|
+
proof = {
|
|
571
|
+
...verifiableCredential.proof,
|
|
572
|
+
type: verifiableCredential.proof.type,
|
|
573
|
+
created: verifiableCredential.proof.created,
|
|
574
|
+
proofPurpose: verifiableCredential.proof.proofPurpose,
|
|
575
|
+
verificationMethod: verifiableCredential.proof.verificationMethod,
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
return {
|
|
580
|
+
...verifiableCredential,
|
|
581
|
+
type: verifiableCredential.type
|
|
582
|
+
? typeof verifiableCredential.type === 'string'
|
|
583
|
+
? [verifiableCredential.type]
|
|
584
|
+
: verifiableCredential.type
|
|
585
|
+
: ['VerifiableCredential'],
|
|
586
|
+
proof,
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
static storedCredentialToOriginalFormat(credential: OriginalVerifiableCredential): W3CVerifiableCredential {
|
|
591
|
+
const type: DocumentFormat = CredentialMapper.detectDocumentType(credential)
|
|
592
|
+
if (typeof credential === 'string') {
|
|
593
|
+
if (type === DocumentFormat.JWT) {
|
|
594
|
+
return CredentialMapper.toCompactJWT(credential)
|
|
595
|
+
} else if (type === DocumentFormat.JSONLD) {
|
|
596
|
+
return JSON.parse(credential)
|
|
597
|
+
}
|
|
598
|
+
} else if (type === DocumentFormat.JWT && 'vc' in credential) {
|
|
599
|
+
return CredentialMapper.toCompactJWT(credential)
|
|
600
|
+
}
|
|
601
|
+
return credential as W3CVerifiableCredential
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
static storedPresentationToOriginalFormat(presentation: OriginalVerifiablePresentation): W3CVerifiablePresentation {
|
|
605
|
+
const type: DocumentFormat = CredentialMapper.detectDocumentType(presentation)
|
|
606
|
+
if (typeof presentation === 'string') {
|
|
607
|
+
if (type === DocumentFormat.JWT) {
|
|
608
|
+
return CredentialMapper.toCompactJWT(presentation)
|
|
609
|
+
} else if (type === DocumentFormat.JSONLD) {
|
|
610
|
+
return JSON.parse(presentation)
|
|
611
|
+
}
|
|
612
|
+
} else if (type === DocumentFormat.JWT && 'vp' in presentation) {
|
|
613
|
+
return CredentialMapper.toCompactJWT(presentation)
|
|
614
|
+
}
|
|
615
|
+
return presentation as W3CVerifiablePresentation
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
static toCompactJWT(
|
|
619
|
+
jwtDocument: W3CVerifiableCredential | JwtDecodedVerifiableCredential | W3CVerifiablePresentation | JwtDecodedVerifiablePresentation | string
|
|
620
|
+
): string {
|
|
621
|
+
if (!jwtDocument || CredentialMapper.detectDocumentType(jwtDocument) !== DocumentFormat.JWT) {
|
|
622
|
+
throw Error('Cannot convert non JWT credential to JWT')
|
|
623
|
+
}
|
|
624
|
+
if (typeof jwtDocument === 'string') {
|
|
625
|
+
return jwtDocument
|
|
626
|
+
}
|
|
627
|
+
let proof: string | undefined
|
|
628
|
+
if ('vp' in jwtDocument) {
|
|
629
|
+
proof = jwtDocument.vp.proof
|
|
630
|
+
} else if ('vc' in jwtDocument) {
|
|
631
|
+
proof = jwtDocument.vc.proof
|
|
632
|
+
} else {
|
|
633
|
+
proof = Array.isArray(jwtDocument.proof) ? jwtDocument.proof[0].jwt : jwtDocument.proof.jwt
|
|
634
|
+
}
|
|
635
|
+
if (!proof) {
|
|
636
|
+
throw Error(`Could not get JWT from supplied document`)
|
|
637
|
+
}
|
|
638
|
+
return proof
|
|
639
|
+
}
|
|
640
|
+
|
|
641
|
+
static detectDocumentType(
|
|
642
|
+
document:
|
|
643
|
+
| W3CVerifiableCredential
|
|
644
|
+
| W3CVerifiablePresentation
|
|
645
|
+
| JwtDecodedVerifiableCredential
|
|
646
|
+
| JwtDecodedVerifiablePresentation
|
|
647
|
+
| SdJwtDecodedVerifiableCredential
|
|
648
|
+
): DocumentFormat {
|
|
649
|
+
if (this.isJsonLdAsString(document)) {
|
|
650
|
+
return DocumentFormat.JSONLD
|
|
651
|
+
} else if (this.isJwtEncoded(document)) {
|
|
652
|
+
return DocumentFormat.JWT
|
|
653
|
+
} else if (this.isSdJwtEncoded(document) || this.isSdJwtDecodedCredential(document as any)) {
|
|
654
|
+
return DocumentFormat.SD_JWT_VC
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
const proofs = 'vc' in document ? document.vc.proof : 'vp' in document ? document.vp.proof : (<IVerifiableCredential>document).proof
|
|
658
|
+
const proof: IProof = Array.isArray(proofs) ? proofs[0] : proofs
|
|
659
|
+
|
|
660
|
+
if (proof?.jwt) {
|
|
661
|
+
return DocumentFormat.JWT
|
|
662
|
+
} else if (proof?.type === 'EthereumEip712Signature2021') {
|
|
663
|
+
return DocumentFormat.EIP712
|
|
664
|
+
}
|
|
665
|
+
return DocumentFormat.JSONLD
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
private static hasJWTProofType(
|
|
669
|
+
document: W3CVerifiableCredential | W3CVerifiablePresentation | JwtDecodedVerifiableCredential | JwtDecodedVerifiablePresentation
|
|
670
|
+
): boolean {
|
|
671
|
+
if (typeof document === 'string') {
|
|
672
|
+
return false
|
|
673
|
+
}
|
|
674
|
+
return !!CredentialMapper.getFirstProof(document)?.jwt
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
private static getFirstProof(
|
|
678
|
+
document: W3CVerifiableCredential | W3CVerifiablePresentation | JwtDecodedVerifiableCredential | JwtDecodedVerifiablePresentation
|
|
679
|
+
): IProof | undefined {
|
|
680
|
+
if (!document || typeof document === 'string') {
|
|
681
|
+
return undefined
|
|
682
|
+
}
|
|
683
|
+
const proofs = 'vc' in document ? document.vc.proof : 'vp' in document ? document.vp.proof : (<IVerifiableCredential>document).proof
|
|
684
|
+
return Array.isArray(proofs) ? proofs[0] : proofs
|
|
685
|
+
}
|
|
686
|
+
}
|