@sd-jwt/core 0.3.0 → 0.3.2-next.100

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +27 -82
  3. package/dist/index.d.mts +118 -0
  4. package/dist/index.d.ts +118 -0
  5. package/dist/index.js +675 -0
  6. package/dist/index.mjs +654 -0
  7. package/package.json +62 -48
  8. package/src/decoy.ts +15 -0
  9. package/src/index.ts +313 -0
  10. package/src/jwt.ts +107 -0
  11. package/src/kbjwt.ts +61 -0
  12. package/src/sdjwt.ts +337 -0
  13. package/src/test/decoy.spec.ts +30 -0
  14. package/src/test/index.spec.ts +528 -0
  15. package/src/test/jwt.spec.ts +141 -0
  16. package/src/test/kbjwt.spec.ts +341 -0
  17. package/src/test/pass.spec.ts +6 -0
  18. package/src/test/sdjwt.spec.ts +382 -0
  19. package/test/app-e2e.spec.ts +248 -0
  20. package/test/array_data_types.json +29 -0
  21. package/test/array_full_sd.json +21 -0
  22. package/test/array_in_sd.json +13 -0
  23. package/test/array_nested_in_plain.json +20 -0
  24. package/test/array_none_disclosed.json +17 -0
  25. package/test/array_of_nulls.json +15 -0
  26. package/test/array_of_objects.json +58 -0
  27. package/test/array_of_scalars.json +15 -0
  28. package/test/array_recursive_sd.json +35 -0
  29. package/test/array_recursive_sd_some_disclosed.json +55 -0
  30. package/test/complex.json +43 -0
  31. package/test/header_mod.json +44 -0
  32. package/test/json_serialization.json +44 -0
  33. package/test/key_binding.json +44 -0
  34. package/test/no_sd.json +36 -0
  35. package/test/object_data_types.json +60 -0
  36. package/test/recursions.json +98 -0
  37. package/tsconfig.json +7 -0
  38. package/vitest.config.mts +4 -0
  39. package/build/index.d.ts +0 -13
  40. package/build/index.js +0 -20
  41. package/build/index.js.map +0 -1
  42. package/build/jwt/error.d.ts +0 -2
  43. package/build/jwt/error.js +0 -7
  44. package/build/jwt/error.js.map +0 -1
  45. package/build/jwt/index.d.ts +0 -2
  46. package/build/jwt/index.js +0 -19
  47. package/build/jwt/index.js.map +0 -1
  48. package/build/jwt/jwt.d.ts +0 -208
  49. package/build/jwt/jwt.js +0 -325
  50. package/build/jwt/jwt.js.map +0 -1
  51. package/build/keyBinding/index.d.ts +0 -1
  52. package/build/keyBinding/index.js +0 -18
  53. package/build/keyBinding/index.js.map +0 -1
  54. package/build/keyBinding/keyBinding.d.ts +0 -64
  55. package/build/keyBinding/keyBinding.js +0 -119
  56. package/build/keyBinding/keyBinding.js.map +0 -1
  57. package/build/sdJwt/decoys.d.ts +0 -3
  58. package/build/sdJwt/decoys.js +0 -35
  59. package/build/sdJwt/decoys.js.map +0 -1
  60. package/build/sdJwt/disclosureFrame.d.ts +0 -8
  61. package/build/sdJwt/disclosureFrame.js +0 -87
  62. package/build/sdJwt/disclosureFrame.js.map +0 -1
  63. package/build/sdJwt/disclosures.d.ts +0 -33
  64. package/build/sdJwt/disclosures.js +0 -114
  65. package/build/sdJwt/disclosures.js.map +0 -1
  66. package/build/sdJwt/error.d.ts +0 -2
  67. package/build/sdJwt/error.js +0 -7
  68. package/build/sdJwt/error.js.map +0 -1
  69. package/build/sdJwt/index.d.ts +0 -6
  70. package/build/sdJwt/index.js +0 -23
  71. package/build/sdJwt/index.js.map +0 -1
  72. package/build/sdJwt/sdJwt.d.ts +0 -206
  73. package/build/sdJwt/sdJwt.js +0 -442
  74. package/build/sdJwt/sdJwt.js.map +0 -1
  75. package/build/sdJwt/types.d.ts +0 -5
  76. package/build/sdJwt/types.js +0 -3
  77. package/build/sdJwt/types.js.map +0 -1
  78. package/build/sdJwtVc/error.d.ts +0 -2
  79. package/build/sdJwtVc/error.js +0 -7
  80. package/build/sdJwtVc/error.js.map +0 -1
  81. package/build/sdJwtVc/index.d.ts +0 -2
  82. package/build/sdJwtVc/index.js +0 -19
  83. package/build/sdJwtVc/index.js.map +0 -1
  84. package/build/sdJwtVc/sdJwtVc.d.ts +0 -47
  85. package/build/sdJwtVc/sdJwtVc.js +0 -149
  86. package/build/sdJwtVc/sdJwtVc.js.map +0 -1
  87. package/build/signatureAndEncryptionAlgorithm.d.ts +0 -105
  88. package/build/signatureAndEncryptionAlgorithm.js +0 -110
  89. package/build/signatureAndEncryptionAlgorithm.js.map +0 -1
  90. package/build/types/disclosure.d.ts +0 -5
  91. package/build/types/disclosure.js +0 -3
  92. package/build/types/disclosure.js.map +0 -1
  93. package/build/types/index.d.ts +0 -5
  94. package/build/types/index.js +0 -22
  95. package/build/types/index.js.map +0 -1
  96. package/build/types/saltGenerator.d.ts +0 -17
  97. package/build/types/saltGenerator.js +0 -3
  98. package/build/types/saltGenerator.js.map +0 -1
  99. package/build/types/signer.d.ts +0 -2
  100. package/build/types/signer.js +0 -3
  101. package/build/types/signer.js.map +0 -1
  102. package/build/types/utils.d.ts +0 -2
  103. package/build/types/utils.js +0 -3
  104. package/build/types/utils.js.map +0 -1
  105. package/build/types/verifier.d.ts +0 -14
  106. package/build/types/verifier.js +0 -3
  107. package/build/types/verifier.js.map +0 -1
package/dist/index.mjs ADDED
@@ -0,0 +1,654 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defProps = Object.defineProperties;
3
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
+ var __spreadValues = (a, b) => {
9
+ for (var prop in b || (b = {}))
10
+ if (__hasOwnProp.call(b, prop))
11
+ __defNormalProp(a, prop, b[prop]);
12
+ if (__getOwnPropSymbols)
13
+ for (var prop of __getOwnPropSymbols(b)) {
14
+ if (__propIsEnum.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ }
17
+ return a;
18
+ };
19
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
+ var __async = (__this, __arguments, generator) => {
21
+ return new Promise((resolve, reject) => {
22
+ var fulfilled = (value) => {
23
+ try {
24
+ step(generator.next(value));
25
+ } catch (e) {
26
+ reject(e);
27
+ }
28
+ };
29
+ var rejected = (value) => {
30
+ try {
31
+ step(generator.throw(value));
32
+ } catch (e) {
33
+ reject(e);
34
+ }
35
+ };
36
+ var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
37
+ step((generator = generator.apply(__this, __arguments)).next());
38
+ });
39
+ };
40
+
41
+ // src/index.ts
42
+ import { SDJWTException as SDJWTException4, Uint8ArrayToBase64Url as Uint8ArrayToBase64Url2 } from "@sd-jwt/utils";
43
+
44
+ // src/jwt.ts
45
+ import { Base64urlEncode, SDJWTException } from "@sd-jwt/utils";
46
+ import { decodeJwt } from "@sd-jwt/decode";
47
+ var Jwt = class _Jwt {
48
+ constructor(data) {
49
+ this.header = data == null ? void 0 : data.header;
50
+ this.payload = data == null ? void 0 : data.payload;
51
+ this.signature = data == null ? void 0 : data.signature;
52
+ }
53
+ static decodeJWT(jwt) {
54
+ return decodeJwt(jwt);
55
+ }
56
+ static fromEncode(encodedJwt) {
57
+ const { header, payload, signature } = _Jwt.decodeJWT(
58
+ encodedJwt
59
+ );
60
+ const jwt = new _Jwt({
61
+ header,
62
+ payload,
63
+ signature
64
+ });
65
+ return jwt;
66
+ }
67
+ setHeader(header) {
68
+ this.header = header;
69
+ return this;
70
+ }
71
+ setPayload(payload) {
72
+ this.payload = payload;
73
+ return this;
74
+ }
75
+ sign(signer) {
76
+ return __async(this, null, function* () {
77
+ if (!this.header || !this.payload) {
78
+ throw new SDJWTException("Sign Error: Invalid JWT");
79
+ }
80
+ const header = Base64urlEncode(JSON.stringify(this.header));
81
+ const payload = Base64urlEncode(JSON.stringify(this.payload));
82
+ const data = `${header}.${payload}`;
83
+ this.signature = yield signer(data);
84
+ return this.encodeJwt();
85
+ });
86
+ }
87
+ encodeJwt() {
88
+ if (!this.header || !this.payload || !this.signature) {
89
+ throw new SDJWTException("Serialize Error: Invalid JWT");
90
+ }
91
+ const header = Base64urlEncode(JSON.stringify(this.header));
92
+ const payload = Base64urlEncode(JSON.stringify(this.payload));
93
+ const signature = this.signature;
94
+ const compact = `${header}.${payload}.${signature}`;
95
+ return compact;
96
+ }
97
+ verify(verifier) {
98
+ return __async(this, null, function* () {
99
+ if (!this.header || !this.payload || !this.signature) {
100
+ throw new SDJWTException("Verify Error: Invalid JWT");
101
+ }
102
+ const header = Base64urlEncode(JSON.stringify(this.header));
103
+ const payload = Base64urlEncode(JSON.stringify(this.payload));
104
+ const data = `${header}.${payload}`;
105
+ const verified = verifier(data, this.signature);
106
+ if (!verified) {
107
+ throw new SDJWTException("Verify Error: Invalid JWT Signature");
108
+ }
109
+ return { payload: this.payload, header: this.header };
110
+ });
111
+ }
112
+ };
113
+
114
+ // src/kbjwt.ts
115
+ import { Base64urlEncode as Base64urlEncode2, SDJWTException as SDJWTException2 } from "@sd-jwt/utils";
116
+ var KBJwt = class _KBJwt extends Jwt {
117
+ // Checking the validity of the key binding jwt
118
+ // the type unknown is not good, but we don't know at this point how to get the public key of the signer, this is defined in the kbVerifier
119
+ verifyKB(values) {
120
+ return __async(this, null, function* () {
121
+ var _a, _b, _c, _d, _e, _f;
122
+ if (!((_a = this.header) == null ? void 0 : _a.alg) || !this.header.typ || !((_b = this.payload) == null ? void 0 : _b.iat) || !((_c = this.payload) == null ? void 0 : _c.aud) || !((_d = this.payload) == null ? void 0 : _d.nonce) || // this is for backward compatibility with version 06
123
+ !(((_e = this.payload) == null ? void 0 : _e.sd_hash) || ((_f = this.payload) == null ? void 0 : _f._sd_hash))) {
124
+ throw new SDJWTException2("Invalid Key Binding Jwt");
125
+ }
126
+ if (!this.header || !this.payload || !this.signature) {
127
+ throw new SDJWTException2("Verify Error: Invalid JWT");
128
+ }
129
+ const header = Base64urlEncode2(JSON.stringify(this.header));
130
+ const payload = Base64urlEncode2(JSON.stringify(this.payload));
131
+ const data = `${header}.${payload}`;
132
+ const verified = yield values.verifier(
133
+ data,
134
+ this.signature,
135
+ values.payload
136
+ );
137
+ if (!verified) {
138
+ throw new SDJWTException2("Verify Error: Invalid JWT Signature");
139
+ }
140
+ return { payload: this.payload, header: this.header };
141
+ });
142
+ }
143
+ // This function is for creating KBJwt object for verify properly
144
+ static fromKBEncode(encodedJwt) {
145
+ const { header, payload, signature } = Jwt.decodeJWT(
146
+ encodedJwt
147
+ );
148
+ const jwt = new _KBJwt({
149
+ header,
150
+ payload,
151
+ signature
152
+ });
153
+ return jwt;
154
+ }
155
+ };
156
+
157
+ // src/decoy.ts
158
+ import { Uint8ArrayToBase64Url } from "@sd-jwt/utils";
159
+ var createDecoy = (hash, saltGenerator) => __async(void 0, null, function* () {
160
+ const { hasher, alg } = hash;
161
+ const salt = yield saltGenerator(16);
162
+ const decoy = yield hasher(salt, alg);
163
+ return Uint8ArrayToBase64Url(decoy);
164
+ });
165
+
166
+ // src/sdjwt.ts
167
+ import { SDJWTException as SDJWTException3, Disclosure } from "@sd-jwt/utils";
168
+ import {
169
+ SD_DECOY,
170
+ SD_DIGEST,
171
+ SD_LIST_KEY,
172
+ SD_SEPARATOR
173
+ } from "@sd-jwt/types";
174
+ import { createHashMapping, getSDAlgAndPayload, unpack } from "@sd-jwt/decode";
175
+ var SDJwt = class _SDJwt {
176
+ constructor(data) {
177
+ this.jwt = data == null ? void 0 : data.jwt;
178
+ this.disclosures = data == null ? void 0 : data.disclosures;
179
+ this.kbJwt = data == null ? void 0 : data.kbJwt;
180
+ }
181
+ static decodeSDJwt(sdjwt, hasher) {
182
+ return __async(this, null, function* () {
183
+ const [encodedJwt, ...encodedDisclosures] = sdjwt.split(SD_SEPARATOR);
184
+ const jwt = Jwt.fromEncode(encodedJwt);
185
+ if (!jwt.payload) {
186
+ throw new Error("Payload is undefined on the JWT. Invalid state reached");
187
+ }
188
+ if (encodedDisclosures.length === 0) {
189
+ return {
190
+ jwt,
191
+ disclosures: []
192
+ };
193
+ }
194
+ const encodedKeyBindingJwt = encodedDisclosures.pop();
195
+ const kbJwt = encodedKeyBindingJwt ? KBJwt.fromKBEncode(encodedKeyBindingJwt) : void 0;
196
+ const { _sd_alg } = getSDAlgAndPayload(jwt.payload);
197
+ const disclosures = yield Promise.all(
198
+ encodedDisclosures.map(
199
+ (ed) => Disclosure.fromEncode(ed, { alg: _sd_alg, hasher })
200
+ )
201
+ );
202
+ return {
203
+ jwt,
204
+ disclosures,
205
+ kbJwt
206
+ };
207
+ });
208
+ }
209
+ static fromEncode(encodedSdJwt, hasher) {
210
+ return __async(this, null, function* () {
211
+ const { jwt, disclosures, kbJwt } = yield _SDJwt.decodeSDJwt(encodedSdJwt, hasher);
212
+ return new _SDJwt({
213
+ jwt,
214
+ disclosures,
215
+ kbJwt
216
+ });
217
+ });
218
+ }
219
+ present(keys, hasher) {
220
+ return __async(this, null, function* () {
221
+ var _a;
222
+ if (!((_a = this.jwt) == null ? void 0 : _a.payload) || !this.disclosures) {
223
+ throw new SDJWTException3("Invalid sd-jwt: jwt or disclosures is missing");
224
+ }
225
+ const { _sd_alg: alg } = getSDAlgAndPayload(this.jwt.payload);
226
+ const hash = { alg, hasher };
227
+ const hashmap = yield createHashMapping(this.disclosures, hash);
228
+ const { disclosureKeymap } = yield unpack(
229
+ this.jwt.payload,
230
+ this.disclosures,
231
+ hasher
232
+ );
233
+ const presentableKeys = Object.keys(disclosureKeymap);
234
+ const missingKeys = keys.filter((k) => !presentableKeys.includes(k));
235
+ if (missingKeys.length > 0) {
236
+ throw new SDJWTException3(
237
+ `Invalid sd-jwt: invalid present keys: ${missingKeys.join(", ")}`
238
+ );
239
+ }
240
+ const disclosures = keys.map((k) => hashmap[disclosureKeymap[k]]);
241
+ const presentSDJwt = new _SDJwt({
242
+ jwt: this.jwt,
243
+ disclosures,
244
+ kbJwt: this.kbJwt
245
+ });
246
+ return presentSDJwt.encodeSDJwt();
247
+ });
248
+ }
249
+ encodeSDJwt() {
250
+ const data = [];
251
+ if (!this.jwt) {
252
+ throw new SDJWTException3("Invalid sd-jwt: jwt is missing");
253
+ }
254
+ const encodedJwt = this.jwt.encodeJwt();
255
+ data.push(encodedJwt);
256
+ if (this.disclosures && this.disclosures.length > 0) {
257
+ const encodeddisclosures = this.disclosures.map((dc) => dc.encode()).join(SD_SEPARATOR);
258
+ data.push(encodeddisclosures);
259
+ }
260
+ data.push(this.kbJwt ? this.kbJwt.encodeJwt() : "");
261
+ return data.join(SD_SEPARATOR);
262
+ }
263
+ keys(hasher) {
264
+ return __async(this, null, function* () {
265
+ return listKeys(yield this.getClaims(hasher)).sort();
266
+ });
267
+ }
268
+ presentableKeys(hasher) {
269
+ return __async(this, null, function* () {
270
+ var _a, _b;
271
+ if (!((_a = this.jwt) == null ? void 0 : _a.payload) || !this.disclosures) {
272
+ throw new SDJWTException3("Invalid sd-jwt: jwt or disclosures is missing");
273
+ }
274
+ const { disclosureKeymap } = yield unpack(
275
+ (_b = this.jwt) == null ? void 0 : _b.payload,
276
+ this.disclosures,
277
+ hasher
278
+ );
279
+ return Object.keys(disclosureKeymap).sort();
280
+ });
281
+ }
282
+ getClaims(hasher) {
283
+ return __async(this, null, function* () {
284
+ var _a;
285
+ if (!((_a = this.jwt) == null ? void 0 : _a.payload) || !this.disclosures) {
286
+ throw new SDJWTException3("Invalid sd-jwt: jwt or disclosures is missing");
287
+ }
288
+ const { unpackedObj } = yield unpack(
289
+ this.jwt.payload,
290
+ this.disclosures,
291
+ hasher
292
+ );
293
+ return unpackedObj;
294
+ });
295
+ }
296
+ };
297
+ var listKeys = (obj, prefix = "") => {
298
+ const keys = [];
299
+ for (const key in obj) {
300
+ if (obj[key] === void 0)
301
+ continue;
302
+ const newKey = prefix ? `${prefix}.${key}` : key;
303
+ keys.push(newKey);
304
+ if (obj[key] && typeof obj[key] === "object" && obj[key] !== null) {
305
+ keys.push(...listKeys(obj[key], newKey));
306
+ }
307
+ }
308
+ return keys;
309
+ };
310
+ var pack = (claims, disclosureFrame, hash, saltGenerator) => __async(void 0, null, function* () {
311
+ var _a, _b;
312
+ if (!disclosureFrame) {
313
+ return {
314
+ packedClaims: claims,
315
+ disclosures: []
316
+ };
317
+ }
318
+ const sd = (_a = disclosureFrame[SD_DIGEST]) != null ? _a : [];
319
+ const decoyCount = (_b = disclosureFrame[SD_DECOY]) != null ? _b : 0;
320
+ if (Array.isArray(claims)) {
321
+ const packedClaims2 = [];
322
+ const disclosures2 = [];
323
+ const recursivePackedClaims2 = {};
324
+ for (const key in disclosureFrame) {
325
+ if (key !== SD_DIGEST) {
326
+ const idx = parseInt(key);
327
+ const packed = yield pack(
328
+ claims[idx],
329
+ disclosureFrame[idx],
330
+ hash,
331
+ saltGenerator
332
+ );
333
+ recursivePackedClaims2[idx] = packed.packedClaims;
334
+ disclosures2.push(...packed.disclosures);
335
+ }
336
+ }
337
+ for (let i = 0; i < claims.length; i++) {
338
+ const claim = recursivePackedClaims2[i] ? recursivePackedClaims2[i] : claims[i];
339
+ if (sd.includes(i)) {
340
+ const salt = yield saltGenerator(16);
341
+ const disclosure = new Disclosure([salt, claim]);
342
+ const digest = yield disclosure.digest(hash);
343
+ packedClaims2.push({ [SD_LIST_KEY]: digest });
344
+ disclosures2.push(disclosure);
345
+ } else {
346
+ packedClaims2.push(claim);
347
+ }
348
+ }
349
+ for (let j = 0; j < decoyCount; j++) {
350
+ const decoyDigest = yield createDecoy(hash, saltGenerator);
351
+ packedClaims2.push({ [SD_LIST_KEY]: decoyDigest });
352
+ }
353
+ return { packedClaims: packedClaims2, disclosures: disclosures2 };
354
+ }
355
+ const packedClaims = {};
356
+ const disclosures = [];
357
+ const recursivePackedClaims = {};
358
+ for (const key in disclosureFrame) {
359
+ if (key !== SD_DIGEST) {
360
+ const packed = yield pack(
361
+ // @ts-ignore
362
+ claims[key],
363
+ disclosureFrame[key],
364
+ hash,
365
+ saltGenerator
366
+ );
367
+ recursivePackedClaims[key] = packed.packedClaims;
368
+ disclosures.push(...packed.disclosures);
369
+ }
370
+ }
371
+ const _sd = [];
372
+ for (const key in claims) {
373
+ const claim = recursivePackedClaims[key] ? recursivePackedClaims[key] : claims[key];
374
+ if (sd.includes(key)) {
375
+ const salt = yield saltGenerator(16);
376
+ const disclosure = new Disclosure([salt, key, claim]);
377
+ const digest = yield disclosure.digest(hash);
378
+ _sd.push(digest);
379
+ disclosures.push(disclosure);
380
+ } else {
381
+ packedClaims[key] = claim;
382
+ }
383
+ }
384
+ for (let j = 0; j < decoyCount; j++) {
385
+ const decoyDigest = yield createDecoy(hash, saltGenerator);
386
+ _sd.push(decoyDigest);
387
+ }
388
+ if (_sd.length > 0) {
389
+ packedClaims[SD_DIGEST] = _sd.sort();
390
+ }
391
+ return { packedClaims, disclosures };
392
+ });
393
+
394
+ // src/index.ts
395
+ import {
396
+ KB_JWT_TYP as KB_JWT_TYP2
397
+ } from "@sd-jwt/types";
398
+ import { getSDAlgAndPayload as getSDAlgAndPayload2 } from "@sd-jwt/decode";
399
+ var _SDJwtInstance = class _SDJwtInstance {
400
+ constructor(userConfig) {
401
+ this.userConfig = {};
402
+ if (userConfig) {
403
+ this.userConfig = userConfig;
404
+ }
405
+ }
406
+ createKBJwt(options, sdHash) {
407
+ return __async(this, null, function* () {
408
+ if (!this.userConfig.kbSigner) {
409
+ throw new SDJWTException4("Key Binding Signer not found");
410
+ }
411
+ if (!this.userConfig.kbSignAlg) {
412
+ throw new SDJWTException4("Key Binding sign algorithm not specified");
413
+ }
414
+ const { payload } = options;
415
+ const kbJwt = new KBJwt({
416
+ header: {
417
+ typ: KB_JWT_TYP2,
418
+ alg: this.userConfig.kbSignAlg
419
+ },
420
+ payload: __spreadProps(__spreadValues({}, payload), { sd_hash: sdHash })
421
+ });
422
+ yield kbJwt.sign(this.userConfig.kbSigner);
423
+ return kbJwt;
424
+ });
425
+ }
426
+ SignJwt(jwt) {
427
+ return __async(this, null, function* () {
428
+ if (!this.userConfig.signer) {
429
+ throw new SDJWTException4("Signer not found");
430
+ }
431
+ yield jwt.sign(this.userConfig.signer);
432
+ return jwt;
433
+ });
434
+ }
435
+ VerifyJwt(jwt) {
436
+ return __async(this, null, function* () {
437
+ if (!this.userConfig.verifier) {
438
+ throw new SDJWTException4("Verifier not found");
439
+ }
440
+ return jwt.verify(this.userConfig.verifier);
441
+ });
442
+ }
443
+ issue(payload, disclosureFrame, options) {
444
+ return __async(this, null, function* () {
445
+ var _a, _b;
446
+ if (!this.userConfig.hasher) {
447
+ throw new SDJWTException4("Hasher not found");
448
+ }
449
+ if (!this.userConfig.saltGenerator) {
450
+ throw new SDJWTException4("SaltGenerator not found");
451
+ }
452
+ if (!this.userConfig.signAlg) {
453
+ throw new SDJWTException4("sign alogrithm not specified");
454
+ }
455
+ if (disclosureFrame) {
456
+ this.validateReservedFields(disclosureFrame);
457
+ }
458
+ const hasher = this.userConfig.hasher;
459
+ const hashAlg = (_a = this.userConfig.hashAlg) != null ? _a : _SDJwtInstance.DEFAULT_hashAlg;
460
+ const { packedClaims, disclosures } = yield pack(
461
+ payload,
462
+ disclosureFrame,
463
+ { hasher, alg: hashAlg },
464
+ this.userConfig.saltGenerator
465
+ );
466
+ const alg = this.userConfig.signAlg;
467
+ const OptionHeader = (_b = options == null ? void 0 : options.header) != null ? _b : {};
468
+ const CustomHeader = this.userConfig.omitTyp ? OptionHeader : __spreadValues({ typ: this.type }, OptionHeader);
469
+ const header = __spreadProps(__spreadValues({}, CustomHeader), { alg });
470
+ const jwt = new Jwt({
471
+ header,
472
+ payload: __spreadProps(__spreadValues({}, packedClaims), {
473
+ _sd_alg: disclosureFrame ? hashAlg : void 0
474
+ })
475
+ });
476
+ yield this.SignJwt(jwt);
477
+ const sdJwt = new SDJwt({
478
+ jwt,
479
+ disclosures
480
+ });
481
+ return sdJwt.encodeSDJwt();
482
+ });
483
+ }
484
+ /**
485
+ * Validates if the disclosureFrame contains any reserved fields. If so it will throw an error.
486
+ * @param disclosureFrame
487
+ * @returns
488
+ */
489
+ validateReservedFields(disclosureFrame) {
490
+ return;
491
+ }
492
+ present(encodedSDJwt, presentationKeys, options) {
493
+ return __async(this, null, function* () {
494
+ var _a;
495
+ if (!presentationKeys)
496
+ return encodedSDJwt;
497
+ if (!this.userConfig.hasher) {
498
+ throw new SDJWTException4("Hasher not found");
499
+ }
500
+ const hasher = this.userConfig.hasher;
501
+ const sdjwt = yield SDJwt.fromEncode(encodedSDJwt, hasher);
502
+ if (!((_a = sdjwt.jwt) == null ? void 0 : _a.payload))
503
+ throw new SDJWTException4("Payload not found");
504
+ const presentSdJwtWithoutKb = yield sdjwt.present(
505
+ presentationKeys.sort(),
506
+ hasher
507
+ );
508
+ if (!(options == null ? void 0 : options.kb)) {
509
+ return presentSdJwtWithoutKb;
510
+ }
511
+ const sdHashStr = yield this.calculateSDHash(
512
+ presentSdJwtWithoutKb,
513
+ sdjwt,
514
+ hasher
515
+ );
516
+ sdjwt.kbJwt = yield this.createKBJwt(options.kb, sdHashStr);
517
+ return sdjwt.present(presentationKeys.sort(), hasher);
518
+ });
519
+ }
520
+ // This function is for verifying the SD JWT
521
+ // If requiredClaimKeys is provided, it will check if the required claim keys are presentation in the SD JWT
522
+ // If requireKeyBindings is true, it will check if the key binding JWT is presentation and verify it
523
+ verify(encodedSDJwt, requiredClaimKeys, requireKeyBindings) {
524
+ return __async(this, null, function* () {
525
+ if (!this.userConfig.hasher) {
526
+ throw new SDJWTException4("Hasher not found");
527
+ }
528
+ const hasher = this.userConfig.hasher;
529
+ const sdjwt = yield SDJwt.fromEncode(encodedSDJwt, hasher);
530
+ if (!sdjwt.jwt || !sdjwt.jwt.payload) {
531
+ throw new SDJWTException4("Invalid SD JWT");
532
+ }
533
+ const { payload, header } = yield this.validate(encodedSDJwt);
534
+ if (requiredClaimKeys) {
535
+ const keys = yield sdjwt.keys(hasher);
536
+ const missingKeys = requiredClaimKeys.filter((k) => !keys.includes(k));
537
+ if (missingKeys.length > 0) {
538
+ throw new SDJWTException4(
539
+ `Missing required claim keys: ${missingKeys.join(", ")}`
540
+ );
541
+ }
542
+ }
543
+ if (!requireKeyBindings) {
544
+ return { payload, header };
545
+ }
546
+ if (!sdjwt.kbJwt) {
547
+ throw new SDJWTException4("Key Binding JWT not exist");
548
+ }
549
+ if (!this.userConfig.kbVerifier) {
550
+ throw new SDJWTException4("Key Binding Verifier not found");
551
+ }
552
+ const kb = yield sdjwt.kbJwt.verifyKB({
553
+ verifier: this.userConfig.kbVerifier,
554
+ payload
555
+ });
556
+ if (!kb) {
557
+ throw new Error("signature is not valid");
558
+ }
559
+ const sdHashfromKb = kb.payload.sd_hash;
560
+ const sdjwtWithoutKb = new SDJwt({
561
+ jwt: sdjwt.jwt,
562
+ disclosures: sdjwt.disclosures
563
+ });
564
+ const presentSdJwtWithoutKb = sdjwtWithoutKb.encodeSDJwt();
565
+ const sdHashStr = yield this.calculateSDHash(
566
+ presentSdJwtWithoutKb,
567
+ sdjwt,
568
+ hasher
569
+ );
570
+ if (sdHashStr !== sdHashfromKb) {
571
+ throw new SDJWTException4("Invalid sd_hash in Key Binding JWT");
572
+ }
573
+ return { payload, header, kb };
574
+ });
575
+ }
576
+ calculateSDHash(presentSdJwtWithoutKb, sdjwt, hasher) {
577
+ return __async(this, null, function* () {
578
+ if (!sdjwt.jwt || !sdjwt.jwt.payload) {
579
+ throw new SDJWTException4("Invalid SD JWT");
580
+ }
581
+ const { _sd_alg } = getSDAlgAndPayload2(sdjwt.jwt.payload);
582
+ const sdHash = yield hasher(presentSdJwtWithoutKb, _sd_alg);
583
+ const sdHashStr = Uint8ArrayToBase64Url2(sdHash);
584
+ return sdHashStr;
585
+ });
586
+ }
587
+ // This function is for validating the SD JWT
588
+ // Just checking signature and return its the claims
589
+ validate(encodedSDJwt) {
590
+ return __async(this, null, function* () {
591
+ if (!this.userConfig.hasher) {
592
+ throw new SDJWTException4("Hasher not found");
593
+ }
594
+ const hasher = this.userConfig.hasher;
595
+ const sdjwt = yield SDJwt.fromEncode(encodedSDJwt, hasher);
596
+ if (!sdjwt.jwt) {
597
+ throw new SDJWTException4("Invalid SD JWT");
598
+ }
599
+ const verifiedPayloads = yield this.VerifyJwt(sdjwt.jwt);
600
+ const claims = yield sdjwt.getClaims(hasher);
601
+ return { payload: claims, header: verifiedPayloads.header };
602
+ });
603
+ }
604
+ config(newConfig) {
605
+ this.userConfig = __spreadValues(__spreadValues({}, this.userConfig), newConfig);
606
+ }
607
+ encode(sdJwt) {
608
+ return sdJwt.encodeSDJwt();
609
+ }
610
+ decode(endcodedSDJwt) {
611
+ if (!this.userConfig.hasher) {
612
+ throw new SDJWTException4("Hasher not found");
613
+ }
614
+ return SDJwt.fromEncode(endcodedSDJwt, this.userConfig.hasher);
615
+ }
616
+ keys(endcodedSDJwt) {
617
+ return __async(this, null, function* () {
618
+ if (!this.userConfig.hasher) {
619
+ throw new SDJWTException4("Hasher not found");
620
+ }
621
+ const sdjwt = yield SDJwt.fromEncode(endcodedSDJwt, this.userConfig.hasher);
622
+ return sdjwt.keys(this.userConfig.hasher);
623
+ });
624
+ }
625
+ presentableKeys(endcodedSDJwt) {
626
+ return __async(this, null, function* () {
627
+ if (!this.userConfig.hasher) {
628
+ throw new SDJWTException4("Hasher not found");
629
+ }
630
+ const sdjwt = yield SDJwt.fromEncode(endcodedSDJwt, this.userConfig.hasher);
631
+ return sdjwt.presentableKeys(this.userConfig.hasher);
632
+ });
633
+ }
634
+ getClaims(endcodedSDJwt) {
635
+ return __async(this, null, function* () {
636
+ if (!this.userConfig.hasher) {
637
+ throw new SDJWTException4("Hasher not found");
638
+ }
639
+ const sdjwt = yield SDJwt.fromEncode(endcodedSDJwt, this.userConfig.hasher);
640
+ return sdjwt.getClaims(this.userConfig.hasher);
641
+ });
642
+ }
643
+ };
644
+ _SDJwtInstance.DEFAULT_hashAlg = "sha-256";
645
+ var SDJwtInstance = _SDJwtInstance;
646
+ export {
647
+ Jwt,
648
+ KBJwt,
649
+ SDJwt,
650
+ SDJwtInstance,
651
+ createDecoy,
652
+ listKeys,
653
+ pack
654
+ };