@mattrglobal/verifier-sdk-web 2.2.1-unstable.76 → 2.2.1-unstable.78

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/README.md CHANGED
@@ -21,7 +21,7 @@
21
21
  - [Define wallet identifiers](#define-wallet-identifiers)
22
22
  - [Configure redirectUri](#configure-redirecturi)
23
23
  - [Correlate sessions with your own records using state](#correlate-sessions-with-your-own-records-using-state)
24
- - [Run logic on session creation using onSessionCreate](#run-logic-on-session-creation-using-onsessioncreate)
24
+ - [React to presentation requests using onPresentationRequest](#react-to-presentation-requests-using-onpresentationrequest)
25
25
  - [Request credentials using the DC API](#request-credentials-using-the-dc-api)
26
26
  - [Request credentials examples](#request-credentials-examples)
27
27
  - [Request credentials with automatic flow selection](#request-credentials-with-automatic-flow-selection)
@@ -114,8 +114,8 @@ the following configurations and settings:
114
114
  same-device flows).
115
115
  - Optionally pass a `state` string to correlate the session with a record in your own system (see
116
116
  [Correlate sessions with your own records using state](#correlate-sessions-with-your-own-records-using-state)).
117
- - Optionally pass an `onSessionCreate` hook to run your own logic once the session has been created (see
118
- [Run logic on session creation using onSessionCreate](#run-logic-on-session-creation-using-onsessioncreate)).
117
+ - Optionally pass an `onPresentationRequest` hook to run your own logic each time a presentation request is issued (see
118
+ [React to presentation requests using onPresentationRequest](#react-to-presentation-requests-using-onpresentationrequest)).
119
119
 
120
120
  ## Initialize the SDK
121
121
 
@@ -341,15 +341,16 @@ MATTR VII platform and the SDK, see the
341
341
  [Correlating verification sessions with your system](https://learn.mattr.global/docs/verification/remote-web-verifiers/guides/correlating-verification-sessions)
342
342
  guide on MATTR Learn.
343
343
 
344
- ## Run logic on session creation using `onSessionCreate`
344
+ ## React to presentation requests using `onPresentationRequest`
345
345
 
346
- `requestCredentials()` accepts an optional `onSessionCreate` hook that the SDK invokes immediately after the
347
- presentation session has been created on the MATTR VII verifier tenant, but before it continues with the rest of the
348
- presentation flow (rendering the QR code for cross-device flows or redirecting to the wallet for same-device flows). Use
349
- it to run your own logic against the freshly created session, for example persisting the `sessionId` in your backend,
350
- starting a timer, or emitting analytics.
346
+ `requestCredentials()` accepts an optional `onPresentationRequest` hook that the SDK invokes once immediately after the
347
+ presentation session has been created on the MATTR VII verifier tenant, and again every time the OpenID4VP auth-request
348
+ URI is regenerated for cross-device sessions this happens whenever the QR code shown in the iframe is refreshed.
349
+ Same-device and Digital Credentials API sessions only ever invoke the hook once, since there is no refreshable request
350
+ URI in those flows. Use it to run your own logic against the current request, for example mirroring the `authRequestUri`
351
+ into your own system, starting a timer, or emitting analytics.
351
352
 
352
- The hook receives the session creation result and may be synchronous or asynchronous; the SDK awaits it before
353
+ The hook receives the current presentation request and may be synchronous or asynchronous; the SDK awaits it before
353
354
  continuing:
354
355
 
355
356
  ```typescript
@@ -360,24 +361,46 @@ const options: MATTRVerifierSDK.RequestCredentialsOptions = {
360
361
  redirectUri: window.location.origin,
361
362
  walletProviderId,
362
363
  },
363
- onSessionCreate: async (session) => {
364
- // e.g. record the session against your own system before the flow continues
365
- await fetch("/api/sessions", {
366
- method: "POST",
367
- headers: { "Content-Type": "application/json" },
368
- body: JSON.stringify({ sessionId: session.sessionId }),
369
- });
364
+ onPresentationRequest: async (request) => {
365
+ // e.g. record the current auth-request URI against your own system
366
+ if (request.type === "openid4vp") {
367
+ await fetch("/api/presentation-requests", {
368
+ method: "POST",
369
+ headers: { "Content-Type": "application/json" },
370
+ body: JSON.stringify({ sessionId: request.sessionId, authRequestUri: request.authRequestUri }),
371
+ });
372
+ }
370
373
  },
371
374
  };
372
375
 
373
376
  const results = await MATTRVerifierSDK.requestCredentials(options);
374
377
  ```
375
378
 
376
- - The hook is supported in both same-device and cross-device sessions.
377
- - The `session` argument exposes the identifiers for the created session, including its `sessionId`.
378
- - Any error thrown or promise rejection from the hook is intentionally ignored so it cannot disrupt the presentation
379
- flow. If a step is critical, handle its errors inside the hook rather than relying on `requestCredentials()` to
380
- surface them.
379
+ - The hook fires once for same-device and Digital Credentials API sessions, and once immediately plus again on every QR
380
+ refresh for cross-device sessions.
381
+ - The `request` argument is a discriminated union on `type`: for OpenID4VP sessions (`type: "openid4vp"`) it exposes
382
+ `sessionId` and `authRequestUri`; for Digital Credentials API sessions (`type: "digital-credentials-api"`) it exposes
383
+ only `sessionId`.
384
+ - If the hook throws or its returned promise rejects on any firing, the in-progress presentation is aborted — the
385
+ session is aborted server-side and, for cross-device flows, the iframe is closed — and `requestCredentials()` resolves
386
+ with an `OnPresentationRequestFailed` error (see `RequestCredentialsErrorType`). This applies consistently whether the
387
+ failing firing was the initial one or a later cross-device refresh. If you want the presentation flow to continue
388
+ regardless of an error in the hook, catch and handle the error yourself inside the hook, for example:
389
+
390
+ ```typescript
391
+ onPresentationRequest: async (request) => {
392
+ try {
393
+ await fetch("/api/presentation-requests", {
394
+ method: "POST",
395
+ headers: { "Content-Type": "application/json" },
396
+ body: JSON.stringify(request),
397
+ });
398
+ } catch (error) {
399
+ // Swallow the error here so requestCredentials() continues with the presentation flow
400
+ console.error("Failed to record presentation request", error);
401
+ }
402
+ },
403
+ ```
381
404
 
382
405
  ## Request credentials using the DC API
383
406
 
@@ -435,9 +458,9 @@ const result = await MATTRVerifierSDK.requestCredentials({
435
458
  walletProviderId, // Define the wallet identifier
436
459
  redirectUri, // Define the redirect URI (not required for cross-device only requests)
437
460
  },
438
- onSessionCreate: (session) => {
439
- // Optional: run your own logic once the session is created, before the flow continues
440
- console.info("Session created", session.sessionId);
461
+ onPresentationRequest: (request) => {
462
+ // Optional: run your own logic each time a presentation request is issued
463
+ console.info("Presentation request", request.sessionId);
441
464
  },
442
465
  });
443
466
 
@@ -463,10 +486,11 @@ if (result.isErr()) {
463
486
  wallet-facing request URI and stored in session records. Returned in the response from `requestCredentials()`
464
487
  (cross-device) or `handleRedirectCallback()` (same-device). See
465
488
  [Correlate sessions with your own records using state](#correlate-sessions-with-your-own-records-using-state).
466
- - `onSessionCreate` (optional): A hook invoked with the session creation result once the presentation session has been
467
- created, before the flow continues. Supported in both same-device and cross-device flows. Errors thrown by the hook
468
- are ignored. See
469
- [Run logic on session creation using onSessionCreate](#run-logic-on-session-creation-using-onsessioncreate).
489
+ - `onPresentationRequest` (optional): A hook invoked once with the current presentation request after the session is
490
+ created, and again on every cross-device QR refresh. If the hook throws or rejects on any firing, the in-progress
491
+ presentation is aborted and `requestCredentials()` resolves with an `OnPresentationRequestFailed` error; catch and
492
+ handle errors inside the hook yourself if you want the flow to continue regardless. See
493
+ [React to presentation requests using onPresentationRequest](#react-to-presentation-requests-using-onpresentationrequest).
470
494
  - `mode`: When omitted, the SDK defaults to automatically selecting a flow based on the browser's user agent (set to
471
495
  `undefined` in the example for clarity).
472
496
  - `redirectUri`: Replace with a URI that matches one of the values in the
@@ -7,7 +7,7 @@
7
7
  * Do Not Translate or Localize
8
8
  *
9
9
  * Bundle of @mattrglobal/verifier-sdk-web
10
- * Generated: 2026-07-31
10
+ * Generated: 2026-08-02
11
11
  * Version: 2.2.0
12
12
  * Dependencies:
13
13
  */
@@ -221,6 +221,7 @@ const CreateSessionOpenId4vpResponseValidator = v__namespace.object({
221
221
  sessionId: v__namespace.string(),
222
222
  sessionKey: v__namespace.string(),
223
223
  sessionUrl: v__namespace.string(),
224
+ authRequestUri: v__namespace.string(),
224
225
  state: v__namespace.optional(v__namespace.string())
225
226
  });
226
227
 
@@ -262,6 +263,7 @@ var MessageEventDataType;
262
263
  MessageEventDataType["PresentationCompleted"] = "PresentationCompleted";
263
264
  MessageEventDataType["PresentationTimeout"] = "PresentationTimeout";
264
265
  MessageEventDataType["PresentationAbort"] = "PresentationAbort";
266
+ MessageEventDataType["PresentationRequestUpdated"] = "PresentationRequestUpdated";
265
267
  })(MessageEventDataType || (MessageEventDataType = {}));
266
268
 
267
269
  const OpenId4vpConfigSameDeviceOptionsValidator = v__namespace.object({
@@ -294,6 +296,7 @@ exports.RequestCredentialsErrorType = void 0;
294
296
  RequestCredentialsErrorType["RequestCredentialsFailed"] = "RequestCredentialsFailed";
295
297
  RequestCredentialsErrorType["Timeout"] = "Timeout";
296
298
  RequestCredentialsErrorType["Abort"] = "Abort";
299
+ RequestCredentialsErrorType["OnPresentationRequestFailed"] = "OnPresentationRequestFailed";
297
300
  })(exports.RequestCredentialsErrorType || (exports.RequestCredentialsErrorType = {}));
298
301
 
299
302
  var RequestCredentialsErrorMessage;
@@ -309,6 +312,7 @@ var RequestCredentialsErrorMessage;
309
312
  RequestCredentialsErrorMessage["Abort"] = "User aborted the session";
310
313
  RequestCredentialsErrorMessage["Timeout"] = "User session timeout";
311
314
  RequestCredentialsErrorMessage["StateMismatch"] = "State mismatch between requested session and back-channel result";
315
+ RequestCredentialsErrorMessage["OnPresentationRequestFailed"] = "The onPresentationRequest hook threw an error or rejected";
312
316
  })(RequestCredentialsErrorMessage || (RequestCredentialsErrorMessage = {}));
313
317
 
314
318
  exports.AbortSessionErrorType = void 0;
@@ -628,71 +632,111 @@ const closeCrossDeviceModal = options => {
628
632
  removeWindowMessageEventListener();
629
633
  };
630
634
 
631
- const receiveMessageHandler = options => async event => {
632
- const {onComplete: onComplete, onFailure: onFailure, container: container, sessionId: sessionId, apiBaseUrl: apiBaseUrl, challenge: challenge, state: state} = options;
633
- if (event.origin !== apiBaseUrl) {
634
- return;
635
- }
636
- if (event.data.type === MessageEventDataType.PresentationCompleted && event.data.responseCode && event.data.sessionId) {
637
- const responseCode = event.data.responseCode;
638
- const result = await exchangeSessionResult({
639
- challenge: challenge,
640
- responseCode: responseCode,
641
- sessionId: sessionId,
642
- apiBaseUrl: apiBaseUrl
643
- });
644
- if (result.isErr()) {
635
+ const receiveMessageHandler = options => {
636
+ let queue = Promise.resolve();
637
+ const processEvent = async event => {
638
+ const {onComplete: onComplete, onFailure: onFailure, container: container, sessionId: sessionId, sessionKey: sessionKey, apiBaseUrl: apiBaseUrl, challenge: challenge, state: state, onPresentationRequest: onPresentationRequest, isSettled: isSettled} = options;
639
+ if (isSettled() || event.origin !== apiBaseUrl) {
640
+ return;
641
+ }
642
+ if (event.data.type === MessageEventDataType.PresentationRequestUpdated && event.data.authRequestUri) {
643
+ if (onPresentationRequest) {
644
+ try {
645
+ await onPresentationRequest({
646
+ type: SessionType.Openid4vp,
647
+ sessionId: sessionId,
648
+ authRequestUri: event.data.authRequestUri
649
+ });
650
+ } catch (cause) {
651
+ if (isSettled()) {
652
+ return;
653
+ }
654
+ onFailure({
655
+ type: exports.RequestCredentialsErrorType.OnPresentationRequestFailed,
656
+ message: RequestCredentialsErrorMessage.OnPresentationRequestFailed,
657
+ cause: cause
658
+ });
659
+ closeCrossDeviceModal({
660
+ container: container
661
+ });
662
+ await abortSession({
663
+ apiBaseUrl: apiBaseUrl,
664
+ sessionId: sessionId,
665
+ sessionKey: sessionKey
666
+ });
667
+ }
668
+ }
669
+ return;
670
+ }
671
+ if (event.data.type === MessageEventDataType.PresentationCompleted && event.data.responseCode && event.data.sessionId) {
672
+ const responseCode = event.data.responseCode;
673
+ const result = await exchangeSessionResult({
674
+ challenge: challenge,
675
+ responseCode: responseCode,
676
+ sessionId: sessionId,
677
+ apiBaseUrl: apiBaseUrl
678
+ });
679
+ if (isSettled()) {
680
+ return;
681
+ }
682
+ if (result.isErr()) {
683
+ onFailure({
684
+ type: exports.RequestCredentialsErrorType.RequestCredentialsFailed,
685
+ message: RequestCredentialsErrorMessage.FailedToGetSessionResult
686
+ });
687
+ closeCrossDeviceModal({
688
+ container: container
689
+ });
690
+ return;
691
+ }
692
+ const resultState = "challenge" in result.value ? result.value.state : undefined;
693
+ if (state !== undefined && resultState !== undefined && state !== resultState) {
694
+ onFailure({
695
+ type: exports.RequestCredentialsErrorType.RequestCredentialsFailed,
696
+ message: RequestCredentialsErrorMessage.StateMismatch
697
+ });
698
+ closeCrossDeviceModal({
699
+ container: container
700
+ });
701
+ return;
702
+ }
703
+ onComplete({
704
+ result: "challenge" in result.value ? result.value : undefined,
705
+ sessionId: result.value.sessionId,
706
+ sessionCompletedInRedirect: false,
707
+ state: resultState !== null && resultState !== void 0 ? resultState : state
708
+ });
709
+ closeCrossDeviceModal({
710
+ container: container
711
+ });
712
+ return;
713
+ }
714
+ if (event.data.type === MessageEventDataType.PresentationTimeout) {
645
715
  onFailure({
646
- type: exports.RequestCredentialsErrorType.RequestCredentialsFailed,
647
- message: RequestCredentialsErrorMessage.FailedToGetSessionResult
716
+ type: exports.RequestCredentialsErrorType.Timeout,
717
+ message: RequestCredentialsErrorMessage.Timeout
648
718
  });
649
719
  closeCrossDeviceModal({
650
720
  container: container
651
721
  });
652
722
  return;
653
723
  }
654
- const resultState = "challenge" in result.value ? result.value.state : undefined;
655
- if (state !== undefined && resultState !== undefined && state !== resultState) {
724
+ if (event.data.type === MessageEventDataType.PresentationAbort) {
656
725
  onFailure({
657
- type: exports.RequestCredentialsErrorType.RequestCredentialsFailed,
658
- message: RequestCredentialsErrorMessage.StateMismatch
726
+ type: exports.RequestCredentialsErrorType.Abort,
727
+ message: RequestCredentialsErrorMessage.Abort
659
728
  });
660
729
  closeCrossDeviceModal({
661
730
  container: container
662
731
  });
663
732
  return;
664
733
  }
665
- onComplete({
666
- result: "challenge" in result.value ? result.value : undefined,
667
- sessionId: result.value.sessionId,
668
- sessionCompletedInRedirect: false,
669
- state: resultState !== null && resultState !== void 0 ? resultState : state
670
- });
671
- closeCrossDeviceModal({
672
- container: container
673
- });
674
- return;
675
- }
676
- if (event.data.type === MessageEventDataType.PresentationTimeout) {
677
- onFailure({
678
- type: exports.RequestCredentialsErrorType.Timeout,
679
- message: RequestCredentialsErrorMessage.Timeout
680
- });
681
- closeCrossDeviceModal({
682
- container: container
683
- });
684
- return;
685
- }
686
- if (event.data.type === MessageEventDataType.PresentationAbort) {
687
- onFailure({
688
- type: exports.RequestCredentialsErrorType.Abort,
689
- message: RequestCredentialsErrorMessage.Abort
690
- });
691
- closeCrossDeviceModal({
692
- container: container
693
- });
694
- return;
695
- }
734
+ };
735
+ return event => {
736
+ const next = queue.then((() => processEvent(event)));
737
+ queue = next.catch((() => undefined));
738
+ return next;
739
+ };
696
740
  };
697
741
 
698
742
  const openCrossDeviceModal = options => {
@@ -712,16 +756,18 @@ const openCrossDeviceModal = options => {
712
756
  };
713
757
 
714
758
  const requestCredentialsWithCrossDevice = async options => {
715
- const {challenge: challenge, apiBaseUrl: apiBaseUrl, sessionUrl: sessionUrl, sessionId: sessionId, sessionKey: sessionKey, state: state} = options;
759
+ const {challenge: challenge, apiBaseUrl: apiBaseUrl, sessionUrl: sessionUrl, sessionId: sessionId, sessionKey: sessionKey, state: state, onPresentationRequest: onPresentationRequest} = options;
716
760
  const container = openCrossDeviceModal({
717
761
  sessionUrl: sessionUrl
718
762
  });
763
+ let settled = false;
719
764
  return await new Promise((resolve => {
720
765
  const abortController = setActiveSession({
721
766
  sessionId: sessionId,
722
767
  sessionKey: sessionKey
723
768
  });
724
769
  abortController.signal.addEventListener("abort", (() => {
770
+ settled = true;
725
771
  closeCrossDeviceModal({
726
772
  container: container
727
773
  });
@@ -734,11 +780,20 @@ const requestCredentialsWithCrossDevice = async options => {
734
780
  listener = receiveMessageHandler({
735
781
  container: container,
736
782
  sessionId: sessionId,
783
+ sessionKey: sessionKey,
737
784
  apiBaseUrl: apiBaseUrl,
738
785
  challenge: challenge,
739
786
  state: state,
740
- onComplete: data => resolve(neverthrow.ok(data)),
741
- onFailure: error => resolve(neverthrow.err(error))
787
+ onPresentationRequest: onPresentationRequest,
788
+ isSettled: () => settled,
789
+ onComplete: data => {
790
+ settled = true;
791
+ resolve(neverthrow.ok(data));
792
+ },
793
+ onFailure: error => {
794
+ settled = true;
795
+ resolve(neverthrow.err(error));
796
+ }
742
797
  });
743
798
  window.addEventListener(WindowEventListenerType.message, listener, false);
744
799
  }));
@@ -986,7 +1041,7 @@ const requestCredentials = async options => {
986
1041
  }
987
1042
  assertType(RequestCredentialsOptionsValidator, "Invalid request credential options")(options);
988
1043
  const {apiBaseUrl: apiBaseUrl, applicationId: applicationId} = initializeOptions;
989
- const {challenge: challenge = generateChallenge(), credentialQuery: credentialQuery, openid4vpConfiguration: openid4vpConfiguration, state: state, onSessionCreate: onSessionCreate} = options;
1044
+ const {challenge: challenge = generateChallenge(), credentialQuery: credentialQuery, openid4vpConfiguration: openid4vpConfiguration, state: state, onPresentationRequest: onPresentationRequest} = options;
990
1045
  const dcApiSupported = isDigitalCredentialsApiSupported();
991
1046
  const openId4VpRedirectUri = deriveOpenId4vpRedirectUri(openid4vpConfiguration);
992
1047
  const createSessionResult = await createSession({
@@ -1008,10 +1063,29 @@ const requestCredentials = async options => {
1008
1063
  }
1009
1064
  const session = createSessionResult.value;
1010
1065
  const {sessionKey: sessionKey, sessionId: sessionId} = session;
1011
- if (onSessionCreate) {
1066
+ if (onPresentationRequest) {
1067
+ const presentationRequest = session.type === SessionType.DigitalCredentialsApi ? {
1068
+ type: SessionType.DigitalCredentialsApi,
1069
+ sessionId: sessionId
1070
+ } : {
1071
+ type: SessionType.Openid4vp,
1072
+ sessionId: sessionId,
1073
+ authRequestUri: session.authRequestUri
1074
+ };
1012
1075
  try {
1013
- await onSessionCreate(session);
1014
- } catch (_b) {}
1076
+ await onPresentationRequest(presentationRequest);
1077
+ } catch (cause) {
1078
+ await abortSession({
1079
+ apiBaseUrl: apiBaseUrl,
1080
+ sessionId: sessionId,
1081
+ sessionKey: sessionKey
1082
+ });
1083
+ return neverthrow.err({
1084
+ type: exports.RequestCredentialsErrorType.OnPresentationRequestFailed,
1085
+ message: RequestCredentialsErrorMessage.OnPresentationRequestFailed,
1086
+ cause: cause
1087
+ });
1088
+ }
1015
1089
  }
1016
1090
  if (session.type === SessionType.DigitalCredentialsApi) {
1017
1091
  const {request: request, sessionTtl: sessionTtl} = session;
@@ -1048,7 +1122,8 @@ const requestCredentials = async options => {
1048
1122
  sessionUrl: sessionUrl,
1049
1123
  sessionKey: sessionKey,
1050
1124
  sessionId: sessionId,
1051
- state: state
1125
+ state: state,
1126
+ onPresentationRequest: onPresentationRequest
1052
1127
  });
1053
1128
  };
1054
1129