@sd-jwt/core 0.19.1-next.1 → 0.19.1-next.11

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/src/index.ts CHANGED
@@ -1,34 +1,42 @@
1
- import { getSDAlgAndPayload } from '@sd-jwt/decode';
1
+ import { getSDAlgAndPayload } from './decode';
2
+ import { FlattenJSON } from './flattenJSON';
3
+ import { GeneralJSON } from './generalJSON';
4
+ import { Jwt, type VerifierOptions } from './jwt';
5
+ import { KBJwt } from './kbjwt';
6
+ import { pack, SDJwt } from './sdjwt';
2
7
  import {
3
8
  type DisclosureFrame,
4
9
  type Hasher,
5
10
  IANA_HASH_ALGORITHMS,
6
- type JwtPayload,
7
11
  KB_JWT_TYP,
8
12
  type KBOptions,
9
13
  type PresentationFrame,
14
+ type SafeVerifyResult,
10
15
  type SDJWTCompact,
11
16
  type SDJWTConfig,
12
17
  type Signer,
13
- } from '@sd-jwt/types';
18
+ type VerificationError,
19
+ type VerificationErrorCode,
20
+ } from './types';
14
21
  import {
15
22
  base64urlDecode,
16
23
  base64urlEncode,
24
+ ensureError,
17
25
  SDJWTException,
18
26
  uint8ArrayToBase64Url,
19
- } from '@sd-jwt/utils';
20
- import { FlattenJSON } from './flattenJSON';
21
- import { GeneralJSON } from './generalJSON';
22
- import { Jwt, type VerifierOptions } from './jwt';
23
- import { KBJwt } from './kbjwt';
24
- import { pack, SDJwt } from './sdjwt';
27
+ } from './utils';
25
28
 
29
+ export * from './decode';
26
30
  export * from './decoy';
27
31
  export * from './flattenJSON';
28
32
  export * from './generalJSON';
29
33
  export * from './jwt';
30
34
  export * from './kbjwt';
35
+ export * from './present';
31
36
  export * from './sdjwt';
37
+ // Re-export all types, utils, decode, and present functionality
38
+ export * from './types';
39
+ export * from './utils';
32
40
 
33
41
  export type SdJwtPayload = Record<string, unknown>;
34
42
 
@@ -232,8 +240,9 @@ export class SDJwtInstance<ExtendedPayload extends SdJwtPayload, T = unknown> {
232
240
  }
233
241
  const kb = await sdjwt.kbJwt.verifyKB({
234
242
  verifier: this.userConfig.kbVerifier,
235
- payload: payload as JwtPayload,
243
+ payload,
236
244
  nonce: options.keyBindingNonce,
245
+ options,
237
246
  });
238
247
  if (!kb) {
239
248
  throw new Error('signature is not valid');
@@ -259,6 +268,212 @@ export class SDJwtInstance<ExtendedPayload extends SdJwtPayload, T = unknown> {
259
268
  return { payload, header, kb };
260
269
  }
261
270
 
271
+ /**
272
+ * Safe verification that collects all errors instead of failing fast.
273
+ * Returns a result object with either the verified data or an array of all errors.
274
+ *
275
+ * @param encodedSDJwt - The encoded SD-JWT to verify
276
+ * @param options - Verification options
277
+ * @returns A SafeVerifyResult containing either success data or collected errors
278
+ */
279
+ public async safeVerify(
280
+ encodedSDJwt: string,
281
+ options?: T & VerifierOptions,
282
+ ): Promise<
283
+ SafeVerifyResult<{
284
+ payload: ExtendedPayload;
285
+ header: Record<string, unknown> | undefined;
286
+ kb?: {
287
+ payload: Record<string, unknown>;
288
+ header: Record<string, unknown>;
289
+ };
290
+ }>
291
+ > {
292
+ const errors: VerificationError[] = [];
293
+
294
+ // Helper to add errors
295
+ const addError = (
296
+ code: VerificationErrorCode,
297
+ message: string,
298
+ details?: unknown,
299
+ ) => {
300
+ errors.push({ code, message, details });
301
+ };
302
+
303
+ // Helper to convert exception to error code
304
+ const exceptionToCode = (error: Error): VerificationErrorCode => {
305
+ const message = error.message.toLowerCase();
306
+ if (message.includes('hasher not found')) return 'HASHER_NOT_FOUND';
307
+ if (message.includes('verifier not found')) return 'VERIFIER_NOT_FOUND';
308
+ if (message.includes('invalid sd jwt') || message.includes('invalid jwt'))
309
+ return 'INVALID_SD_JWT';
310
+ if (message.includes('not yet valid')) return 'JWT_NOT_YET_VALID';
311
+ if (message.includes('expired')) return 'JWT_EXPIRED';
312
+ if (message.includes('signature')) return 'INVALID_JWT_SIGNATURE';
313
+ if (message.includes('missing required claim'))
314
+ return 'MISSING_REQUIRED_CLAIMS';
315
+ if (message.includes('key binding jwt not exist'))
316
+ return 'KEY_BINDING_JWT_MISSING';
317
+ if (message.includes('key binding verifier not found'))
318
+ return 'KEY_BINDING_VERIFIER_NOT_FOUND';
319
+ if (message.includes('sd_hash')) return 'KEY_BINDING_SD_HASH_INVALID';
320
+ return 'UNKNOWN_ERROR';
321
+ };
322
+
323
+ // Check basic configuration first
324
+ if (!this.userConfig.hasher) {
325
+ addError('HASHER_NOT_FOUND', 'Hasher not found');
326
+ }
327
+ if (!this.userConfig.verifier) {
328
+ addError('VERIFIER_NOT_FOUND', 'Verifier not found');
329
+ }
330
+
331
+ // If basic config is missing, return early
332
+ if (errors.length > 0) {
333
+ return { success: false, errors };
334
+ }
335
+
336
+ if (!this.userConfig.hasher) {
337
+ throw new SDJWTException('Hasher not found');
338
+ }
339
+
340
+ // hasher and verifier are guaranteed to be defined here
341
+ const hasher = this.userConfig.hasher;
342
+
343
+ // Try to decode and validate the SD-JWT
344
+ let sdjwt: SDJwt | undefined;
345
+ let payload: ExtendedPayload | undefined;
346
+ let header: Record<string, unknown> | undefined;
347
+
348
+ try {
349
+ sdjwt = await SDJwt.fromEncode(encodedSDJwt, hasher);
350
+ if (!sdjwt.jwt || !sdjwt.jwt.payload) {
351
+ addError('INVALID_SD_JWT', 'Invalid SD JWT: missing JWT or payload');
352
+ }
353
+ } catch (e) {
354
+ const error = ensureError(e);
355
+ addError(
356
+ 'INVALID_SD_JWT',
357
+ `Failed to decode SD-JWT: ${error.message}`,
358
+ error,
359
+ );
360
+ }
361
+
362
+ // Validate signature and claims
363
+ if (sdjwt?.jwt) {
364
+ try {
365
+ const result = await this.VerifyJwt(sdjwt.jwt, options);
366
+ header = result.header;
367
+ const claims = await sdjwt.getClaims(hasher);
368
+ payload = claims as ExtendedPayload;
369
+ } catch (e) {
370
+ const error = ensureError(e);
371
+ const code = exceptionToCode(error);
372
+ addError(code, error.message, error);
373
+ }
374
+ }
375
+
376
+ // Check required claim keys
377
+ if (sdjwt && options?.requiredClaimKeys) {
378
+ try {
379
+ const keys = await sdjwt.keys(hasher);
380
+ const missingKeys = options.requiredClaimKeys.filter(
381
+ (k) => !keys.includes(k),
382
+ );
383
+ if (missingKeys.length > 0) {
384
+ addError(
385
+ 'MISSING_REQUIRED_CLAIMS',
386
+ `Missing required claim keys: ${missingKeys.join(', ')}`,
387
+ { missingKeys },
388
+ );
389
+ }
390
+ } catch (e) {
391
+ const error = ensureError(e);
392
+ addError(
393
+ 'UNKNOWN_ERROR',
394
+ `Failed to check required claims: ${error.message}`,
395
+ error,
396
+ );
397
+ }
398
+ }
399
+
400
+ // Verify key binding if requested
401
+ let kb:
402
+ | { payload: Record<string, unknown>; header: Record<string, unknown> }
403
+ | undefined;
404
+ if (options?.keyBindingNonce && sdjwt) {
405
+ if (!sdjwt.kbJwt) {
406
+ addError('KEY_BINDING_JWT_MISSING', 'Key Binding JWT not exist');
407
+ } else if (!this.userConfig.kbVerifier) {
408
+ addError(
409
+ 'KEY_BINDING_VERIFIER_NOT_FOUND',
410
+ 'Key Binding Verifier not found',
411
+ );
412
+ } else if (payload) {
413
+ try {
414
+ const kbResult = await sdjwt.kbJwt.verifyKB({
415
+ verifier: this.userConfig.kbVerifier,
416
+ payload,
417
+ nonce: options.keyBindingNonce,
418
+ options,
419
+ });
420
+ if (!kbResult) {
421
+ addError(
422
+ 'KEY_BINDING_SIGNATURE_INVALID',
423
+ 'Key binding signature is not valid',
424
+ );
425
+ } else {
426
+ kb = kbResult;
427
+
428
+ // Verify sd_hash
429
+ const sdjwtWithoutKb = new SDJwt({
430
+ jwt: sdjwt.jwt,
431
+ disclosures: sdjwt.disclosures,
432
+ });
433
+ const presentSdJwtWithoutKb = sdjwtWithoutKb.encodeSDJwt();
434
+ const sdHashStr = await this.calculateSDHash(
435
+ presentSdJwtWithoutKb,
436
+ sdjwt,
437
+ hasher,
438
+ );
439
+
440
+ if (sdHashStr !== kbResult.payload.sd_hash) {
441
+ addError(
442
+ 'KEY_BINDING_SD_HASH_INVALID',
443
+ 'Invalid sd_hash in Key Binding JWT',
444
+ {
445
+ expected: sdHashStr,
446
+ received: kbResult.payload.sd_hash,
447
+ },
448
+ );
449
+ }
450
+ }
451
+ } catch (e) {
452
+ const error = ensureError(e);
453
+ addError(
454
+ 'KEY_BINDING_SIGNATURE_INVALID',
455
+ `Key binding verification failed: ${error.message}`,
456
+ error,
457
+ );
458
+ }
459
+ }
460
+ }
461
+
462
+ // Return result
463
+ if (errors.length > 0) {
464
+ return { success: false, errors };
465
+ }
466
+
467
+ return {
468
+ success: true,
469
+ data: {
470
+ payload: payload as ExtendedPayload,
471
+ header,
472
+ kb,
473
+ },
474
+ };
475
+ }
476
+
262
477
  private async calculateSDHash(
263
478
  presentSdJwtWithoutKb: string,
264
479
  sdjwt: SDJwt,
@@ -292,7 +507,7 @@ export class SDJwtInstance<ExtendedPayload extends SdJwtPayload, T = unknown> {
292
507
  }
293
508
 
294
509
  const verifiedPayloads = await this.VerifyJwt(sdjwt.jwt, options);
295
- const claims = await sdjwt.getClaims(hasher);
510
+ const claims = await sdjwt.getClaims<ExtendedPayload>(hasher);
296
511
  return { payload: claims, header: verifiedPayloads.header };
297
512
  }
298
513
 
@@ -559,8 +774,9 @@ export class SDJwtGeneralJSONInstance<ExtendedPayload extends SdJwtPayload> {
559
774
  }
560
775
  const kb = await sdjwt.kbJwt.verifyKB({
561
776
  verifier: this.userConfig.kbVerifier,
562
- payload: payload as JwtPayload,
563
- nonce: options.keyBindingNonce as string,
777
+ payload,
778
+ nonce: options.keyBindingNonce,
779
+ options,
564
780
  });
565
781
  if (!kb) {
566
782
  throw new Error('signature is not valid');
@@ -636,7 +852,7 @@ export class SDJwtGeneralJSONInstance<ExtendedPayload extends SdJwtPayload> {
636
852
  throw new SDJWTException('Invalid SD JWT');
637
853
  }
638
854
 
639
- const claims = await sdjwt.getClaims(hasher);
855
+ const claims = await sdjwt.getClaims<ExtendedPayload>(hasher);
640
856
  return { payload: claims, headers: results.map((r) => r.header) };
641
857
  }
642
858
 
package/src/jwt.ts CHANGED
@@ -1,6 +1,6 @@
1
- import { decodeJwt } from '@sd-jwt/decode';
2
- import type { Base64urlString, Signer, Verifier } from '@sd-jwt/types';
3
- import { base64urlEncode, SDJWTException } from '@sd-jwt/utils';
1
+ import { decodeJwt } from './decode';
2
+ import type { Base64urlString, Signer, Verifier } from './types';
3
+ import { base64urlEncode, SDJWTException } from './utils';
4
4
 
5
5
  export type JwtData<
6
6
  Header extends Record<string, unknown>,
@@ -154,23 +154,18 @@ export class Jwt<
154
154
  public async verify<T>(verifier: Verifier<T>, options?: T & VerifierOptions) {
155
155
  const skew = options?.skewSeconds ? options.skewSeconds : 0;
156
156
  const currentDate = options?.currentDate ?? Math.floor(Date.now() / 1000);
157
- if (
158
- this.payload?.iat &&
159
- (this.payload.iat as number) - skew > currentDate
160
- ) {
157
+ const iat = this.payload?.iat;
158
+ const nbf = this.payload?.nbf;
159
+ const exp = this.payload?.exp;
160
+
161
+ if (typeof iat === 'number' && iat - skew > currentDate) {
161
162
  throw new SDJWTException('Verify Error: JWT is not yet valid');
162
163
  }
163
164
 
164
- if (
165
- this.payload?.nbf &&
166
- (this.payload.nbf as number) - skew > currentDate
167
- ) {
165
+ if (typeof nbf === 'number' && nbf - skew > currentDate) {
168
166
  throw new SDJWTException('Verify Error: JWT is not yet valid');
169
167
  }
170
- if (
171
- this.payload?.exp &&
172
- (this.payload.exp as number) + skew < currentDate
173
- ) {
168
+ if (typeof exp === 'number' && exp + skew < currentDate) {
174
169
  throw new SDJWTException('Verify Error: JWT is expired');
175
170
  }
176
171
 
package/src/kbjwt.ts CHANGED
@@ -1,12 +1,11 @@
1
+ import { Jwt, type VerifierOptions } from './jwt';
1
2
  import {
2
- type JwtPayload,
3
3
  KB_JWT_TYP,
4
4
  type KbVerifier,
5
5
  type kbHeader,
6
6
  type kbPayload,
7
- } from '@sd-jwt/types';
8
- import { SDJWTException } from '@sd-jwt/utils';
9
- import { Jwt } from './jwt';
7
+ } from './types';
8
+ import { SDJWTException } from './utils';
10
9
 
11
10
  export class KBJwt<
12
11
  Header extends kbHeader = kbHeader,
@@ -16,8 +15,13 @@ export class KBJwt<
16
15
  // 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
17
16
  public async verifyKB(values: {
18
17
  verifier: KbVerifier;
19
- payload: JwtPayload;
18
+ payload: Record<string, unknown>;
20
19
  nonce: string;
20
+ /**
21
+ * Options forwarded to the common JWT verification, e.g. currentDate and
22
+ * skewSeconds used to validate the iat, nbf and exp claims.
23
+ */
24
+ options?: VerifierOptions;
21
25
  }) {
22
26
  if (!this.header || !this.payload || !this.signature) {
23
27
  throw new SDJWTException('Verify Error: Invalid JWT');
@@ -34,25 +38,25 @@ export class KBJwt<
34
38
  // this is for backward compatibility with version 06
35
39
  !(
36
40
  this.payload.sd_hash ||
37
- (this.payload as Record<string, unknown> | undefined)?._sd_hash
41
+ ('_sd_hash' in this.payload && this.payload._sd_hash)
38
42
  )
39
43
  ) {
40
44
  throw new SDJWTException('Invalid Key Binding Jwt');
41
45
  }
42
46
 
43
- const data = this.getUnsignedToken();
44
- const verified = await values.verifier(
45
- data,
46
- this.signature,
47
- values.payload,
48
- );
49
- if (!verified) {
50
- throw new SDJWTException('Verify Error: Invalid JWT Signature');
51
- }
52
47
  if (this.payload.nonce !== values.nonce) {
53
48
  throw new SDJWTException('Verify Error: Invalid Nonce');
54
49
  }
55
50
 
51
+ // Delegate signature verification and common JWT claim validation
52
+ // (iat, nbf, exp) to the shared Jwt.verify implementation. The kbVerifier
53
+ // needs the kb+jwt payload (e.g. the holder's cnf key), so we wrap it to
54
+ // forward values.payload instead of the base verifier's options argument.
55
+ await this.verify(
56
+ (data, sig) => values.verifier(data, sig, values.payload),
57
+ values.options,
58
+ );
59
+
56
60
  return { payload: this.payload, header: this.header };
57
61
  }
58
62
 
@@ -0,0 +1 @@
1
+ export * from './present';
@@ -0,0 +1,210 @@
1
+ import {
2
+ createHashMapping,
3
+ createHashMappingSync,
4
+ decodeSdJwt,
5
+ decodeSdJwtSync,
6
+ getSDAlgAndPayload,
7
+ splitSdJwt,
8
+ unpack,
9
+ unpackObj,
10
+ unpackSync,
11
+ } from '../decode';
12
+ import type { Extensible, HasherSync } from '../types';
13
+ import { type Hasher, type PresentationFrame, SD_SEPARATOR } from '../types';
14
+ import { Disclosure, SDJWTException } from '../utils';
15
+
16
+ // Presentable keys
17
+ // The presentable keys are the path of JSON object that are presentable in the SD JWT
18
+ // e.g. if the SD JWT has the following payload and set sd like this:
19
+ // {
20
+ // "foo": "bar", // sd
21
+ // "arr": [ // sd
22
+ // "1", // sd
23
+ // "2",
24
+ // {
25
+ // "a": "1" // sd
26
+ // }
27
+ // ],
28
+ // "test": {
29
+ // "zzz": "xxx" // sd
30
+ // }
31
+ // }
32
+ // The presentable keys are: ["arr", "arr.0", "arr.2.a", "foo", "test.zzz"]
33
+ export const presentableKeys = async (
34
+ rawPayload: Record<string, unknown>,
35
+ disclosures: Array<Disclosure>,
36
+ hasher: Hasher,
37
+ ): Promise<string[]> => {
38
+ const { disclosureKeymap } = await unpack(rawPayload, disclosures, hasher);
39
+ return Object.keys(disclosureKeymap).sort();
40
+ };
41
+
42
+ export const presentableKeysSync = (
43
+ rawPayload: Record<string, unknown>,
44
+ disclosures: Array<Disclosure>,
45
+ hasher: HasherSync,
46
+ ): string[] => {
47
+ const { disclosureKeymap } = unpackSync(rawPayload, disclosures, hasher);
48
+ return Object.keys(disclosureKeymap).sort();
49
+ };
50
+
51
+ export const present = async <T extends Record<string, unknown>>(
52
+ sdJwt: string,
53
+ presentFrame: PresentationFrame<T>,
54
+ hasher: Hasher,
55
+ ): Promise<string> => {
56
+ const { jwt, kbJwt } = splitSdJwt(sdJwt);
57
+ const {
58
+ jwt: { payload },
59
+ disclosures,
60
+ } = await decodeSdJwt(sdJwt, hasher);
61
+
62
+ const { _sd_alg: alg } = getSDAlgAndPayload(payload);
63
+ const hash = { alg, hasher };
64
+ const keys = transformPresentationFrame(presentFrame);
65
+
66
+ // hashmap: <digest> => <disclosure>
67
+ // to match the digest with the disclosure
68
+ const hashmap = await createHashMapping(disclosures, hash);
69
+ const { disclosureKeymap } = await unpack(payload, disclosures, hasher);
70
+ const presentedDisclosures = keys
71
+ .map((k) => hashmap[disclosureKeymap[k]])
72
+ .filter((d) => d !== undefined);
73
+
74
+ return [
75
+ jwt,
76
+ ...presentedDisclosures.map((d) => d.encode()),
77
+ kbJwt ?? '',
78
+ ].join(SD_SEPARATOR);
79
+ };
80
+
81
+ export const presentSync = <T extends Record<string, unknown>>(
82
+ sdJwt: string,
83
+ presentFrame: PresentationFrame<T>,
84
+ hasher: HasherSync,
85
+ ): string => {
86
+ const { jwt, kbJwt } = splitSdJwt(sdJwt);
87
+ const {
88
+ jwt: { payload },
89
+ disclosures,
90
+ } = decodeSdJwtSync(sdJwt, hasher);
91
+
92
+ const { _sd_alg: alg } = getSDAlgAndPayload(payload);
93
+ const hash = { alg, hasher };
94
+ const keys = transformPresentationFrame(presentFrame);
95
+
96
+ // hashmap: <digest> => <disclosure>
97
+ // to match the digest with the disclosure
98
+ const hashmap = createHashMappingSync(disclosures, hash);
99
+ const { disclosureKeymap } = unpackSync(payload, disclosures, hasher);
100
+
101
+ const presentedDisclosures = keys
102
+ .map((k) => hashmap[disclosureKeymap[k]])
103
+ .filter((d) => d !== undefined);
104
+
105
+ return [
106
+ jwt,
107
+ ...presentedDisclosures.map((d) => d.encode()),
108
+ kbJwt ?? '',
109
+ ].join(SD_SEPARATOR);
110
+ };
111
+
112
+ /**
113
+ * Transform the object keys into an array of strings. We are not sorting the array in any way.
114
+ * @param obj The object to transform
115
+ * @param prefix The prefix to add to the keys
116
+ * @returns
117
+ */
118
+ export const transformPresentationFrame = (
119
+ obj: PresentationFrame<Extensible>,
120
+ prefix = '',
121
+ ): string[] => {
122
+ return Object.entries(obj).reduce<string[]>((acc, [key, value]) => {
123
+ const newPrefix = prefix ? `${prefix}.${key}` : key;
124
+ if (typeof value === 'boolean') {
125
+ // only add it, when it's true
126
+ if (value) {
127
+ acc.push(newPrefix);
128
+ }
129
+ } else if (typeof value === 'object' && value !== null) {
130
+ acc.push(
131
+ newPrefix,
132
+ ...transformPresentationFrame(
133
+ value as PresentationFrame<Extensible>,
134
+ newPrefix,
135
+ ),
136
+ );
137
+ }
138
+ return acc;
139
+ }, []);
140
+ };
141
+
142
+ export type SerializedDisclosure = {
143
+ digest: string;
144
+ encoded: string;
145
+ salt: string;
146
+ key: string | undefined;
147
+ value: unknown;
148
+ };
149
+
150
+ export const createHashMappingForSerializedDisclosure = (
151
+ disclosures: SerializedDisclosure[],
152
+ ) => {
153
+ const map: Record<string, Disclosure> = {};
154
+ for (let i = 0; i < disclosures.length; i++) {
155
+ const disclosure = disclosures[i];
156
+ const { digest, encoded, key, salt, value } = disclosure;
157
+ // we made Disclosure to fit the interface of unpack
158
+ map[digest] = Disclosure.fromArray(
159
+ key ? [salt, key, value] : [salt, value],
160
+ { digest, encoded },
161
+ );
162
+ }
163
+ return map;
164
+ };
165
+
166
+ /**
167
+ * This function selects the serialized disclosures from the payload
168
+ * and array of serialized disclosure based on the presentation frame.
169
+ * If you want to know what is serialized disclosures, check type SerializedDisclosure.
170
+ * @param payload: Record<string, unknown>
171
+ * @param disclosures: SerializedDisclosure[]
172
+ * @param presentationFrame: PresentationFrame<T>
173
+ */
174
+ export const selectDisclosures = <T extends Record<string, unknown>>(
175
+ payload: Record<string, unknown>,
176
+ disclosures: SerializedDisclosure[],
177
+ presentationFrame: PresentationFrame<T>,
178
+ ) => {
179
+ if (disclosures.length === 0) {
180
+ return [];
181
+ }
182
+
183
+ const hashmap = createHashMappingForSerializedDisclosure(disclosures);
184
+ const { disclosureKeymap } = unpackObj(payload, hashmap);
185
+ const keys = transformPresentationFrame(presentationFrame);
186
+
187
+ const presentedDisclosures = keys
188
+ .map((k) => hashmap[disclosureKeymap[k]])
189
+ .filter((d) => d !== undefined);
190
+
191
+ const selectedDisclosures: SerializedDisclosure[] = presentedDisclosures.map(
192
+ (d) => {
193
+ const { salt, key, value, _digest } = d;
194
+ if (!_digest) {
195
+ throw new SDJWTException(
196
+ 'Implementation error: _digest is not defined',
197
+ );
198
+ }
199
+ return {
200
+ digest: _digest,
201
+ encoded: d.encode(),
202
+ salt,
203
+ key,
204
+ value,
205
+ };
206
+ },
207
+ );
208
+
209
+ return selectedDisclosures;
210
+ };
package/src/sdjwt.ts CHANGED
@@ -1,5 +1,8 @@
1
- import { createHashMapping, getSDAlgAndPayload, unpack } from '@sd-jwt/decode';
2
- import { transformPresentationFrame } from '@sd-jwt/present';
1
+ import { createHashMapping, getSDAlgAndPayload, unpack } from './decode';
2
+ import { createDecoy } from './decoy';
3
+ import { Jwt } from './jwt';
4
+ import { KBJwt } from './kbjwt';
5
+ import { transformPresentationFrame } from './present';
3
6
  import {
4
7
  type DisclosureFrame,
5
8
  type Hasher,
@@ -13,11 +16,8 @@ import {
13
16
  SD_LIST_KEY,
14
17
  SD_SEPARATOR,
15
18
  type SDJWTCompact,
16
- } from '@sd-jwt/types';
17
- import { Disclosure, SDJWTException } from '@sd-jwt/utils';
18
- import { createDecoy } from './decoy';
19
- import { Jwt } from './jwt';
20
- import { KBJwt } from './kbjwt';
19
+ } from './types';
20
+ import { Disclosure, SDJWTException } from './utils';
21
21
 
22
22
  export type SDJwtData<
23
23
  Header extends Record<string, unknown>,
@@ -81,7 +81,7 @@ export class SDJwt<
81
81
  const { _sd_alg } = getSDAlgAndPayload(jwt.payload);
82
82
 
83
83
  const disclosures = await Promise.all(
84
- (encodedDisclosures as Array<string>).map((ed) =>
84
+ encodedDisclosures.map((ed) =>
85
85
  Disclosure.fromEncode(ed, { alg: _sd_alg, hasher }),
86
86
  ),
87
87
  );
@@ -221,8 +221,9 @@ export const listKeys = (obj: Record<string, unknown>, prefix = '') => {
221
221
  const newKey = prefix ? `${prefix}.${key}` : key;
222
222
  keys.push(newKey);
223
223
 
224
- if (obj[key] && typeof obj[key] === 'object' && obj[key] !== null) {
225
- keys.push(...listKeys(obj[key] as Record<string, unknown>, newKey));
224
+ const value = obj[key];
225
+ if (value && typeof value === 'object') {
226
+ keys.push(...listKeys(value as Record<string, unknown>, newKey));
226
227
  }
227
228
  }
228
229
  return keys;