@sphereon/ssi-sdk.sd-jwt 0.34.1-feature.SSISDK.45.93 → 0.34.1-feature.SSISDK.46.40

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