@didit-protocol/sdk-web 0.5.0 → 0.5.1

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.
@@ -1,5 +1,5 @@
1
1
  /**
2
- * Didit SDK for Web v0.5.0
2
+ * Didit SDK for Web v0.5.1
3
3
  * (c) 2026 Didit
4
4
  * @license MIT
5
5
  */
@@ -726,6 +726,9 @@ function submitEvent(options) {
726
726
  if (!isRecord(event) || !event.category || typeof event.actionType !== "string" || !event.actionType.trim()) {
727
727
  throw new DiditEventError("validation", "event.category and event.actionType are required.");
728
728
  }
729
+ if (event.occurredAt instanceof Date && Number.isNaN(event.occurredAt.getTime())) {
730
+ throw new DiditEventError("validation", "event.occurredAt must be a valid date.");
731
+ }
729
732
  if (typeof fetch !== "function") {
730
733
  throw new DiditEventError("network", "fetch is not available in this environment.");
731
734
  }
@@ -789,13 +792,14 @@ const CSS_CLASSES = {
789
792
  confirmBox: "didit-confirm-box",
790
793
  embedded: "didit-embedded"
791
794
  };
792
- const SDK_VERSION = "0.5.0";
795
+ const SDK_VERSION = "0.5.1";
793
796
  /**
794
797
  * The permissions the verification iframe gets, in a modal and embedded:
795
- * camera and microphone for capture, and clipboard-write for the flow's copy
796
- * buttons.
798
+ * camera and microphone for capture, clipboard-write for the flow's copy
799
+ * buttons, and digital-credentials-get so the flow can ask a digital ID wallet
800
+ * (Google Wallet) through the W3C Digital Credentials API from inside the iframe.
797
801
  */
798
- const IFRAME_ALLOW = "camera; microphone; fullscreen; autoplay; encrypted-media; geolocation; clipboard-write";
802
+ const IFRAME_ALLOW = "camera; microphone; fullscreen; autoplay; encrypted-media; geolocation; clipboard-write; digital-credentials-get";
799
803
  /** The verification-URL query parameter each camera option is forwarded as. */
800
804
  const CAMERA_QUERY_PARAMS = {
801
805
  defaultDocumentCamera: "document_camera",
@@ -1810,6 +1814,9 @@ class VerificationModal {
1810
1814
  max-width: max(300px, 18.75rem);
1811
1815
  margin: auto;
1812
1816
  overflow-wrap: anywhere;
1817
+ /* Focusing the dialog scrolls it into view start-aligned; this keeps
1818
+ * the overlay's top padding visible so the title is never clipped. */
1819
+ scroll-margin-top: 1rem;
1813
1820
  }
1814
1821
 
1815
1822
  .${CSS_CLASSES.flowSized} .didit-confirm-actions {
@@ -2594,6 +2601,23 @@ async function pollTransactionAfterAction(args) {
2594
2601
  return latest;
2595
2602
  }
2596
2603
 
2604
+ /**
2605
+ * Embedded mode renders inside an element that already exists on the
2606
+ * integrator's page. Without it the modal never shows anything (embedded mode
2607
+ * never activates the overlay), so the launch fails with a message the
2608
+ * integrator can act on instead of silently presenting nothing.
2609
+ */
2610
+ function assertEmbeddedHostExists(configuration) {
2611
+ if (!configuration?.embedded)
2612
+ return;
2613
+ const containerId = configuration.embeddedContainerId;
2614
+ if (!containerId) {
2615
+ throw new Error("Embedded mode requires embeddedContainerId: the id of the element that hosts the verification.");
2616
+ }
2617
+ if (typeof document === "undefined" || !document.getElementById(containerId)) {
2618
+ throw new Error(`Embedded container "#${containerId}" was not found in the page.`);
2619
+ }
2620
+ }
2597
2621
  class DiditSdk {
2598
2622
  static get shared() {
2599
2623
  if (!DiditSdk._instance) {
@@ -2653,17 +2677,20 @@ class DiditSdk {
2653
2677
  this._modal.destroy();
2654
2678
  this._modal = null;
2655
2679
  }
2656
- this._modal = new VerificationModal(config, {
2657
- onClose: () => this.handleModalClose(),
2658
- onCloseConfirmed: () => this.handleModalCloseConfirmed(),
2659
- onMessage: (event) => this.handleVerificationEvent(event),
2660
- onIframeLoad: () => this.handleIframeLoad()
2661
- });
2680
+ // A new launch is not in an error state any more, whatever the last one did.
2681
+ this._errorMessage = undefined;
2662
2682
  try {
2663
2683
  const { url } = options;
2664
2684
  if (!url || typeof url !== "string") {
2665
2685
  throw new Error("Invalid options: url is required");
2666
2686
  }
2687
+ assertEmbeddedHostExists(config);
2688
+ this._modal = new VerificationModal(config, {
2689
+ onClose: () => this.handleModalClose(),
2690
+ onCloseConfirmed: () => this.handleModalCloseConfirmed(),
2691
+ onMessage: (event) => this.handleVerificationEvent(event),
2692
+ onIframeLoad: () => this.handleIframeLoad()
2693
+ });
2667
2694
  this._url = buildVerificationUrl(url, config);
2668
2695
  this.setState("loading");
2669
2696
  this.emitInternalEvent("didit:started", {});
@@ -2780,6 +2807,7 @@ class DiditSdk {
2780
2807
  this._modal?.destroy();
2781
2808
  this._modal = null;
2782
2809
  this.reset();
2810
+ this.setState("idle");
2783
2811
  }
2784
2812
  async runTransactionAction(baseUrl, transactionToken, submitResult, onActionCompleted) {
2785
2813
  const generation = this._actionGeneration;
@@ -2895,6 +2923,7 @@ class DiditSdk {
2895
2923
  const sessionData = this.buildSessionData();
2896
2924
  this._modal?.close();
2897
2925
  this.reset();
2926
+ this.setState("idle");
2898
2927
  const result = {
2899
2928
  type: "cancelled",
2900
2929
  session: sessionData
@@ -2915,6 +2944,11 @@ class DiditSdk {
2915
2944
  }
2916
2945
  handleVerificationEvent(event) {
2917
2946
  SDKLogger.log("Verification event:", event);
2947
+ // Keep the session the flow told us about so a modal the user closes by
2948
+ // hand still reports the session it belongs to in the cancelled result.
2949
+ if (event.data?.sessionId) {
2950
+ this._sessionId = event.data.sessionId;
2951
+ }
2918
2952
  this.onEvent?.(event);
2919
2953
  switch (event.type) {
2920
2954
  case "didit:ready":
@@ -2970,6 +3004,7 @@ class DiditSdk {
2970
3004
  if (this._configuration?.closeModalOnComplete) {
2971
3005
  this._modal?.close();
2972
3006
  this.reset();
3007
+ this.setState("idle");
2973
3008
  }
2974
3009
  const result = {
2975
3010
  type: "completed",
@@ -2982,6 +3017,7 @@ class DiditSdk {
2982
3017
  const sessionData = this.buildSessionData(event.data);
2983
3018
  this._modal?.close();
2984
3019
  this.reset();
3020
+ this.setState("idle");
2985
3021
  const result = {
2986
3022
  type: "cancelled",
2987
3023
  session: sessionData
@@ -3000,10 +3036,14 @@ class DiditSdk {
3000
3036
  else {
3001
3037
  verificationError = createVerificationError("unknown", "An unknown error occurred");
3002
3038
  }
3003
- this._errorMessage = verificationError.message;
3004
- this.setState("error");
3005
3039
  this._modal?.close();
3040
+ this._modal = null;
3041
+ // Clear what the failed launch left behind, then report the error as the
3042
+ // state the SDK is in: reset() alone would leave callers reading "idle"
3043
+ // with no error message right after onStateChange("error").
3006
3044
  this.reset();
3045
+ this._errorMessage = verificationError.message;
3046
+ this.setState("error");
3007
3047
  const result = {
3008
3048
  type: "failed",
3009
3049
  error: verificationError
@@ -3019,7 +3059,6 @@ class DiditSdk {
3019
3059
  }
3020
3060
  }
3021
3061
  reset() {
3022
- this._state = "idle";
3023
3062
  this._sessionId = undefined;
3024
3063
  this._url = undefined;
3025
3064
  this._errorMessage = undefined;