@sphereon/ssi-sdk.sd-jwt 0.34.1-feature.FIDES.1.274 → 0.34.1-feature.IDK.11.49

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/dist/index.cjs CHANGED
@@ -35,22 +35,18 @@ __export(index_exports, {
35
35
  assertValidTypeMetadata: () => assertValidTypeMetadata,
36
36
  contextHasSDJwtPlugin: () => contextHasSDJwtPlugin,
37
37
  createIntegrity: () => createIntegrity,
38
- defaultGenerateDigest: () => defaultGenerateDigest,
39
38
  extractHashFromIntegrity: () => extractHashFromIntegrity,
40
39
  fetchUrlWithErrorHandling: () => fetchUrlWithErrorHandling,
41
- getIssuerFromSdJwt: () => getIssuerFromSdJwt,
42
- isSdjwtVcPayload: () => isSdjwtVcPayload,
43
- isVcdm2SdJwt: () => isVcdm2SdJwt,
44
- isVcdm2SdJwtPayload: () => isVcdm2SdJwtPayload,
45
40
  sdJwtPluginContextMethods: () => sdJwtPluginContextMethods,
46
41
  validateIntegrity: () => validateIntegrity
47
42
  });
48
43
  module.exports = __toCommonJS(index_exports);
49
44
 
50
45
  // src/action-handler.ts
51
- var import_core2 = require("@sd-jwt/core");
52
- var import_sd_jwt_vc2 = require("@sd-jwt/sd-jwt-vc");
46
+ var import_core = require("@sd-jwt/core");
47
+ var import_sd_jwt_vc = require("@sd-jwt/sd-jwt-vc");
53
48
  var import_ssi_sdk_ext2 = require("@sphereon/ssi-sdk-ext.key-utils");
49
+ var import_utils = require("@veramo/utils");
54
50
  var import_debug = __toESM(require("debug"), 1);
55
51
 
56
52
  // src/defaultCallbacks.ts
@@ -126,189 +122,8 @@ function assertValidTypeMetadata(metadata, vct) {
126
122
  }
127
123
  }
128
124
  __name(assertValidTypeMetadata, "assertValidTypeMetadata");
129
- function isVcdm2SdJwtPayload(payload) {
130
- return "type" in payload && Array.isArray(payload.type) && payload.type.includes("VerifiableCredential") && "@context" in payload && (typeof payload["@context"] === "string" && payload["@context"].length > 0 || Array.isArray(payload["@context"]) && payload["@context"].length > 0 && payload["@context"].includes("https://www.w3.org/ns/credentials/v2"));
131
- }
132
- __name(isVcdm2SdJwtPayload, "isVcdm2SdJwtPayload");
133
- function isSdjwtVcPayload(payload) {
134
- return !isVcdm2SdJwtPayload(payload) && "vct" in payload && typeof payload.vct === "string";
135
- }
136
- __name(isSdjwtVcPayload, "isSdjwtVcPayload");
137
- function getIssuerFromSdJwt(payload) {
138
- let issuer;
139
- if (isSdjwtVcPayload(payload) || "iss" in payload) {
140
- issuer = payload.iss;
141
- } else if (isVcdm2SdJwtPayload(payload) || "issuer" in payload && payload.issuer) {
142
- issuer = typeof payload.issuer === "string" ? payload.issuer : payload.issuer?.id;
143
- }
144
- if (!issuer) {
145
- throw new Error("No issuer (iss or VCDM 2 issuer) found in SD-JWT or no VCDM2 SD-JWT or SD-JWT VC");
146
- }
147
- return issuer;
148
- }
149
- __name(getIssuerFromSdJwt, "getIssuerFromSdJwt");
150
-
151
- // src/sdJwtVcdm2Instance.ts
152
- var import_core = require("@sd-jwt/core");
153
- var import_utils = require("@sd-jwt/utils");
154
- var import_sd_jwt_vc = require("@sd-jwt/sd-jwt-vc");
155
-
156
- // src/types.ts
157
- var import_ssi_sdk = require("@sphereon/ssi-sdk.agent-config");
158
- var sdJwtPluginContextMethods = [
159
- "createSdJwtVc",
160
- "createSdJwtPresentation",
161
- "verifySdJwtVc",
162
- "verifySdJwtPresentation"
163
- ];
164
- function contextHasSDJwtPlugin(context) {
165
- return (0, import_ssi_sdk.contextHasPlugin)(context, "verifySdJwtVc");
166
- }
167
- __name(contextHasSDJwtPlugin, "contextHasSDJwtPlugin");
168
- function isVcdm2SdJwt(type) {
169
- return type === "vc+sd-jwt" || type === "vp+sd-jwt";
170
- }
171
- __name(isVcdm2SdJwt, "isVcdm2SdJwt");
172
-
173
- // src/sdJwtVcdm2Instance.ts
174
- var SDJwtVcdmInstanceFactory = class {
175
- static {
176
- __name(this, "SDJwtVcdmInstanceFactory");
177
- }
178
- static create(type, config) {
179
- if (isVcdm2SdJwt(type)) {
180
- return new SDJwtVcdm2Instance(config);
181
- }
182
- return new import_sd_jwt_vc.SDJwtVcInstance(config);
183
- }
184
- };
185
- var SDJwtVcdm2Instance = class extends import_core.SDJwtInstance {
186
- static {
187
- __name(this, "SDJwtVcdm2Instance");
188
- }
189
- /**
190
- * The type of the SD-JWT VCDM2 set in the header.typ field.
191
- */
192
- static type = "vc+sd-jwt";
193
- userConfig = {};
194
- constructor(userConfig) {
195
- super(userConfig);
196
- if (userConfig) {
197
- this.userConfig = userConfig;
198
- }
199
- }
200
- /**
201
- * Validates if the disclosureFrame contains any reserved fields. If so it will throw an error.
202
- * @param disclosureFrame
203
- */
204
- validateReservedFields(disclosureFrame) {
205
- if (disclosureFrame?._sd && Array.isArray(disclosureFrame._sd) && disclosureFrame._sd.length > 0) {
206
- const reservedNames = [
207
- "iss",
208
- "nbf",
209
- "exp",
210
- "cnf",
211
- "@context",
212
- "type",
213
- "credentialStatus",
214
- "credentialSchema",
215
- "relatedResource"
216
- ];
217
- const reservedNamesInDisclosureFrame = disclosureFrame._sd.filter((key) => reservedNames.includes(key));
218
- if (reservedNamesInDisclosureFrame.length > 0) {
219
- throw new import_utils.SDJWTException(`Cannot disclose protected field(s): ${reservedNamesInDisclosureFrame.join(", ")}`);
220
- }
221
- }
222
- }
223
- /**
224
- * Verifies the SD-JWT-VC. It will validate the signature, the keybindings when required, the status, and the VCT.
225
- * @param encodedSDJwt
226
- * @param options
227
- */
228
- async verify(encodedSDJwt, options) {
229
- const result = await super.verify(encodedSDJwt, options).then((res) => {
230
- return {
231
- payload: res.payload,
232
- header: res.header,
233
- kb: res.kb
234
- };
235
- });
236
- return result;
237
- }
238
- /**
239
- * Validates the integrity of the response if the integrity is passed. If the integrity does not match, an error is thrown.
240
- * @param integrity
241
- * @param response
242
- */
243
- async validateIntegrity(response, url, integrity) {
244
- if (integrity) {
245
- const arrayBuffer = await response.arrayBuffer();
246
- const alg = integrity.split("-")[0];
247
- const hashBuffer = await this.userConfig.hasher(arrayBuffer, alg);
248
- const integrityHash = integrity.split("-")[1];
249
- const hash = Array.from(new Uint8Array(hashBuffer)).map((byte) => byte.toString(16).padStart(2, "0")).join("");
250
- if (hash !== integrityHash) {
251
- throw new Error(`Integrity check for ${url} failed: is ${hash}, but expected ${integrityHash}`);
252
- }
253
- }
254
- }
255
- /**
256
- * Fetches the content from the url with a timeout of 10 seconds.
257
- * @param url
258
- * @param integrity
259
- * @returns
260
- */
261
- async fetch(url, integrity) {
262
- try {
263
- const response = await fetch(url, {
264
- signal: AbortSignal.timeout(this.userConfig.timeout ?? 1e4)
265
- });
266
- if (!response.ok) {
267
- const errorText = await response.text();
268
- return Promise.reject(new Error(`Error fetching ${url}: ${response.status} ${response.statusText} - ${errorText}`));
269
- }
270
- await this.validateIntegrity(response.clone(), url, integrity);
271
- return response.json();
272
- } catch (error) {
273
- if (error.name === "TimeoutError") {
274
- throw new Error(`Request to ${url} timed out`);
275
- }
276
- throw error;
277
- }
278
- }
279
- async issue(payload, disclosureFrame, options) {
280
- if (payload.iss && !payload.issuer) {
281
- payload.issuer = {
282
- id: payload.iss
283
- };
284
- delete payload.iss;
285
- }
286
- if (payload.nbf && !payload.validFrom) {
287
- payload.validFrom = toVcdm2Date(payload.nbf);
288
- delete payload.nbf;
289
- }
290
- if (payload.exp && !payload.validUntil) {
291
- payload.validUntil = toVcdm2Date(payload.exp);
292
- delete payload.exp;
293
- }
294
- if (payload.sub && !Array.isArray(payload.credentialSubject) && !payload.credentialSubject.id) {
295
- payload.credentialSubject.id = payload.sub;
296
- delete payload.sub;
297
- }
298
- return super.issue(payload, disclosureFrame, options);
299
- }
300
- };
301
- function toVcdm2Date(value) {
302
- const num = typeof value === "string" ? Number(value) : value;
303
- if (!Number.isFinite(num)) {
304
- throw new import_utils.SDJWTException(`Invalid numeric date: ${value}`);
305
- }
306
- return new Date(num * 1e3).toISOString();
307
- }
308
- __name(toVcdm2Date, "toVcdm2Date");
309
125
 
310
126
  // src/action-handler.ts
311
- var u8a = __toESM(require("uint8arrays"), 1);
312
127
  var debug = (0, import_debug.default)("@sphereon/ssi-sdk.sd-jwt");
313
128
  var SDJwtPlugin = class {
314
129
  static {
@@ -378,11 +193,7 @@ var SDJwtPlugin = class {
378
193
  * @returns A signed SD-JWT credential.
379
194
  */
380
195
  async createSdJwtVc(args, context) {
381
- const payload = args.credentialPayload;
382
- const isVcdm2 = isVcdm2SdJwtPayload(payload);
383
- const isSdJwtVc = isSdjwtVcPayload(payload);
384
- const type = args.type ?? (isVcdm2 ? "vc+sd-jwt" : "dc+sd-jwt");
385
- const issuer = getIssuerFromSdJwt(args.credentialPayload);
196
+ const issuer = args.credentialPayload.iss;
386
197
  if (!issuer) {
387
198
  throw new Error("credential.issuer must not be empty");
388
199
  }
@@ -390,46 +201,24 @@ var SDJwtPlugin = class {
390
201
  identifier: issuer,
391
202
  resolution: args.resolution
392
203
  }, context);
393
- const signAlg = alg ?? signingKey?.alg ?? "ES256";
394
- const hashAlg = /(\d{3})$/.test(signAlg) ? `sha-${signAlg.slice(-3)}` : "sha-256";
395
- const sdjwt = SDJwtVcdmInstanceFactory.create(type, {
396
- omitTyp: true,
204
+ const sdjwt = new import_sd_jwt_vc.SDJwtVcInstance({
397
205
  signer,
398
206
  hasher: this.registeredImplementations.hasher,
399
207
  saltGenerator: this.registeredImplementations.saltGenerator,
400
- signAlg,
401
- hashAlg
208
+ signAlg: alg ?? "ES256",
209
+ hashAlg: "sha-256"
402
210
  });
403
- const header = {
404
- ...signingKey?.key.kid !== void 0 && {
405
- kid: signingKey.key.kid
406
- },
407
- ...signingKey?.key.x5c !== void 0 && {
408
- x5c: signingKey.key.x5c
409
- },
410
- ...type && {
411
- typ: type
412
- }
413
- };
414
- let credential;
415
- if (isVcdm2) {
416
- credential = await sdjwt.issue(
417
- payload,
418
- // @ts-ignore
419
- args.disclosureFrame,
420
- {
421
- header
211
+ const credential = await sdjwt.issue(args.credentialPayload, args.disclosureFrame, {
212
+ header: {
213
+ ...signingKey?.key.kid !== void 0 && {
214
+ kid: signingKey.key.kid
215
+ },
216
+ ...signingKey?.key.x5c !== void 0 && {
217
+ x5c: signingKey.key.x5c
422
218
  }
423
- );
424
- } else if (isSdJwtVc) {
425
- credential = await sdjwt.issue(payload, args.disclosureFrame, {
426
- header
427
- });
428
- } else {
429
- return Promise.reject(new Error(`invalid_argument: credential '${type}' type is not supported`));
430
- }
219
+ }
220
+ });
431
221
  return {
432
- type,
433
222
  credential
434
223
  };
435
224
  }
@@ -555,8 +344,7 @@ var SDJwtPlugin = class {
555
344
  * @returns A signed SD-JWT presentation.
556
345
  */
557
346
  async createSdJwtPresentation(args, context) {
558
- const type = args.type ?? "dc+sd-jwt";
559
- const cred = await import_core2.SDJwt.fromEncode(args.presentation, this.registeredImplementations.hasher);
347
+ const cred = await import_core.SDJwt.fromEncode(args.presentation, this.registeredImplementations.hasher);
560
348
  const claims = await cred.getClaims(this.registeredImplementations.hasher);
561
349
  let holder;
562
350
  if (args.holder) {
@@ -576,9 +364,8 @@ var SDJwtPlugin = class {
576
364
  const { alg, signer } = await this.getSignerForIdentifier({
577
365
  identifier: holder
578
366
  }, context);
579
- const sdjwt = SDJwtVcdmInstanceFactory.create(type, {
580
- omitTyp: true,
581
- hasher: this.registeredImplementations.hasher,
367
+ const sdjwt = new import_sd_jwt_vc.SDJwtVcInstance({
368
+ hasher: this.registeredImplementations.hasher ?? defaultGenerateDigest,
582
369
  saltGenerator: this.registeredImplementations.saltGenerator,
583
370
  kbSigner: signer,
584
371
  kbSignAlg: alg ?? "ES256"
@@ -587,7 +374,6 @@ var SDJwtPlugin = class {
587
374
  kb: args.kb
588
375
  });
589
376
  return {
590
- type,
591
377
  presentation
592
378
  };
593
379
  }
@@ -598,18 +384,13 @@ var SDJwtPlugin = class {
598
384
  * @returns
599
385
  */
600
386
  async verifySdJwtVc(args, context) {
601
- const verifier = /* @__PURE__ */ __name(async (data, signature) => this.verifyCallbackImpl(sdjwt, context, data, signature), "verifier");
602
- const cred = await import_core2.SDJwt.fromEncode(args.credential, this.registeredImplementations.hasher);
603
- const type = isVcdm2SdJwtPayload(cred.jwt?.payload) ? "vc+sd-jwt" : "dc+sd-jwt";
604
- const sdjwt = SDJwtVcdmInstanceFactory.create(type, {
387
+ const verifier = /* @__PURE__ */ __name(async (data, signature) => this.verify(sdjwt, context, data, signature), "verifier");
388
+ const sdjwt = new import_sd_jwt_vc.SDJwtVcInstance({
605
389
  verifier,
606
390
  hasher: this.registeredImplementations.hasher ?? defaultGenerateDigest
607
391
  });
608
- const { header = {}, payload, kb } = await sdjwt.verify(args.credential, {
609
- skewSeconds: 60 * 60 * 24 * 5
610
- });
392
+ const { header = {}, payload, kb } = await sdjwt.verify(args.credential);
611
393
  return {
612
- type,
613
394
  header,
614
395
  payload,
615
396
  kb
@@ -624,7 +405,7 @@ var SDJwtPlugin = class {
624
405
  * @param payload - The payload of the SD-JWT
625
406
  * @returns
626
407
  */
627
- verifyKb(context, data, signature, payload) {
408
+ verifyKb(sdjwt, context, data, signature, payload) {
628
409
  if (!payload.cnf) {
629
410
  throw Error("other method than cnf is not supported yet");
630
411
  }
@@ -638,10 +419,9 @@ var SDJwtPlugin = class {
638
419
  * @param signature - The signature
639
420
  * @returns
640
421
  */
641
- async verifyCallbackImpl(sdjwt, context, data, signature, opts) {
422
+ async verify(sdjwt, context, data, signature, opts) {
642
423
  const decodedVC = await sdjwt.decode(`${data}.${signature}`);
643
- const payload = decodedVC.jwt.payload;
644
- const issuer = getIssuerFromSdJwt(payload);
424
+ const issuer = decodedVC.jwt.payload.iss;
645
425
  const header = decodedVC.jwt.header;
646
426
  const x5c = header?.x5c;
647
427
  let jwk = header.jwk;
@@ -707,18 +487,14 @@ var SDJwtPlugin = class {
707
487
  */
708
488
  async verifySdJwtPresentation(args, context) {
709
489
  let sdjwt;
710
- const verifier = /* @__PURE__ */ __name(async (data, signature) => this.verifyCallbackImpl(sdjwt, context, data, signature), "verifier");
711
- const verifierKb = /* @__PURE__ */ __name(async (data, signature, payload) => this.verifyKb(context, data, signature, payload), "verifierKb");
712
- sdjwt = new import_sd_jwt_vc2.SDJwtVcInstance({
490
+ const verifier = /* @__PURE__ */ __name(async (data, signature) => this.verify(sdjwt, context, data, signature), "verifier");
491
+ const verifierKb = /* @__PURE__ */ __name(async (data, signature, payload) => this.verifyKb(sdjwt, context, data, signature, payload), "verifierKb");
492
+ sdjwt = new import_sd_jwt_vc.SDJwtVcInstance({
713
493
  verifier,
714
494
  hasher: this.registeredImplementations.hasher,
715
495
  kbVerifier: verifierKb
716
496
  });
717
- const verifierOpts = {
718
- requiredClaimKeys: args.requiredClaimKeys,
719
- keyBindingNonce: args.keyBindingNonce
720
- };
721
- return sdjwt.verify(args.presentation, verifierOpts);
497
+ return sdjwt.verify(args.presentation, args.requiredClaimKeys, args.kb);
722
498
  }
723
499
  /**
724
500
  * Fetch and validate Type Metadata.
@@ -789,7 +565,7 @@ var SDJwtPlugin = class {
789
565
  return payload.cnf.jwk;
790
566
  } else if (payload.cnf !== void 0 && "kid" in payload.cnf && typeof payload.cnf.kid === "string" && payload.cnf.kid.startsWith("did:jwk:")) {
791
567
  const encoded = this.extractBase64FromDIDJwk(payload.cnf.kid);
792
- const decoded = u8a.toString(u8a.fromString(encoded, "base64url"), "utf-8");
568
+ const decoded = (0, import_utils.decodeBase64url)(encoded);
793
569
  const jwt = JSON.parse(decoded);
794
570
  return jwt;
795
571
  }
@@ -803,4 +579,17 @@ var SDJwtPlugin = class {
803
579
  return parts[2].split("#")[0];
804
580
  }
805
581
  };
582
+
583
+ // src/types.ts
584
+ var import_ssi_sdk = require("@sphereon/ssi-sdk.agent-config");
585
+ var sdJwtPluginContextMethods = [
586
+ "createSdJwtVc",
587
+ "createSdJwtPresentation",
588
+ "verifySdJwtVc",
589
+ "verifySdJwtPresentation"
590
+ ];
591
+ function contextHasSDJwtPlugin(context) {
592
+ return (0, import_ssi_sdk.contextHasPlugin)(context, "verifySdJwtVc");
593
+ }
594
+ __name(contextHasSDJwtPlugin, "contextHasSDJwtPlugin");
806
595
  //# sourceMappingURL=index.cjs.map