@sd-jwt/core 0.3.0 → 2.0.4-next.58

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