@didit-protocol/sdk-web 0.5.3 → 0.7.0

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,9 +1,9 @@
1
1
  /**
2
- * Didit SDK for Web v0.5.3
2
+ * Didit SDK for Web v0.7.0
3
3
  * (c) 2026 Didit
4
4
  * @license MIT
5
5
  */
6
- import { getDeviceFingerprintPayload, buildFingerprintHeaders, submitEvent } from '@didit-protocol/signals';
6
+ import { getDeviceFingerprintPayload, buildFingerprintHeaders, submitEvent, getStoredDeviceId } from '@didit-protocol/signals';
7
7
  export * from '@didit-protocol/signals';
8
8
 
9
9
  const DEFAULT_CONFIG = {
@@ -23,9 +23,10 @@ const CSS_CLASSES = {
23
23
  loading: "didit-loading",
24
24
  confirmOverlay: "didit-confirm-overlay",
25
25
  confirmBox: "didit-confirm-box",
26
- embedded: "didit-embedded"
26
+ embedded: "didit-embedded",
27
+ curtain: "didit-modal-curtain"
27
28
  };
28
- const SDK_VERSION = "0.5.3";
29
+ const SDK_VERSION = "0.7.0";
29
30
  /**
30
31
  * The permissions the verification iframe gets, in a modal and embedded:
31
32
  * camera and microphone for capture, clipboard-write for the flow's copy
@@ -51,6 +52,13 @@ const PRESENTATION_QUERY_PARAMS = {
51
52
  presentation: "sdk_presentation",
52
53
  closeButton: "sdk_close_button"
53
54
  };
55
+ /**
56
+ * The verification-URL query parameter that carries the device id this page
57
+ * already keeps for Didit, when it keeps one. A browser gives the flow in the
58
+ * frame storage of its own, apart from this page's, so without it a
59
+ * verification and this site's events never share a device id.
60
+ */
61
+ const DEVICE_ID_QUERY_PARAM = "sdk_device_id";
54
62
  /**
55
63
  * Posted by the hosted flow when it lays itself out for the modal, with
56
64
  * `data: { contentHeight, colorScheme?, dir?, kind? }` (`kind` is `handoff` or `step`, `step` when missing). The modal then grows to the
@@ -177,15 +185,21 @@ const CAMERA_SWITCH_OPTIONS = ["showDocumentCameraSwitchButton", "showLivenessCa
177
185
  * `showDocumentCameraSwitchButton` -> `document_camera_switch=true|false`,
178
186
  * `showLivenessCameraSwitchButton` -> `liveness_camera_switch=true|false`.
179
187
  * How the SDK presents the flow is always added: `sdk_presentation=modal|embedded`
180
- * and, in a modal, `sdk_close_button=true|false`.
188
+ * and, in a modal, `sdk_close_button=true|false`. `storedDeviceId`, when the
189
+ * page has one, is added as `sdk_device_id`.
181
190
  * Each parameter is set (not appended) so the configuration wins over a value
182
191
  * already on the URL; existing query parameters and the hash are kept. A
183
192
  * value of the wrong shape is ignored with a warning rather than forwarded,
184
193
  * and a URL the browser cannot parse is returned as it came (the modal
185
194
  * reports it when it loads).
186
195
  */
187
- function buildVerificationUrl(url, configuration = {}) {
196
+ function buildVerificationUrl(url, configuration = {}, storedDeviceId = null) {
188
197
  const params = presentationParams(configuration);
198
+ // The id this page already keeps for Didit (see getStoredDeviceId): the flow in the frame, whose
199
+ // own storage the browser keeps apart from this page's, can then be told apart as the same
200
+ // device as this site's events. Absent when the page keeps none; nothing is created for it.
201
+ if (storedDeviceId)
202
+ params.push([DEVICE_ID_QUERY_PARAM, storedDeviceId]);
189
203
  for (const option of CAMERA_LENS_OPTIONS) {
190
204
  const value = configuration[option];
191
205
  if (value === undefined)
@@ -691,6 +705,10 @@ function getTranslations(language) {
691
705
  const SIZE_ANIMATION_MS = 300;
692
706
  /** How long the modal waits for the first layout before it shows at step size, so it can never hang. */
693
707
  const FIRST_LAYOUT_TIMEOUT_MS = 3000;
708
+ /** A strip is a line or two of text: a taller value is not one, and would push the close button out of reach. */
709
+ const MAX_TOP_INSET = 160;
710
+ /** The flow's ground (site-ground in the design system), which a full-screen modal paints the page with. */
711
+ const FLOW_GROUND = { light: "#ffffff", dark: "#0b0b0d" };
694
712
  class VerificationModal {
695
713
  constructor(configuration, callbacks) {
696
714
  this.state = {
@@ -713,10 +731,9 @@ class VerificationModal {
713
731
  this.embedded = false;
714
732
  this.embeddedContainer = null;
715
733
  this.language = "en";
716
- /** The page's own background colours, kept while a full-screen modal paints over them. */
717
- this.pageBackground = null;
718
- /** body.style.overflow value observed right before this modal locked scroll, restored on close(). */
719
- this.previousBodyOverflow = null;
734
+ /** The ground below the layout viewport (see showCurtain), while this modal is open. */
735
+ this.curtain = null;
736
+ this.boundPlaceCurtain = null;
720
737
  this.modalId = generateModalId();
721
738
  this.config = {
722
739
  zIndex: configuration?.zIndex ?? DEFAULT_CONFIG.zIndex,
@@ -810,13 +827,14 @@ class VerificationModal {
810
827
  display: flex;
811
828
  align-items: center;
812
829
  justify-content: center;
813
- background: #fafafa;
830
+ /* The flow's ground, so a phone's loader meets the ground below the viewport without a seam. */
831
+ background: ${FLOW_GROUND.light};
814
832
  z-index: 5;
815
833
  }
816
834
 
817
835
  @media (prefers-color-scheme: dark) {
818
836
  .${CSS_CLASSES.loading} {
819
- background: #0b0b0d;
837
+ background: ${FLOW_GROUND.dark};
820
838
  }
821
839
  }
822
840
 
@@ -1006,6 +1024,40 @@ class VerificationModal {
1006
1024
  }
1007
1025
  }
1008
1026
 
1027
+ /*
1028
+ * The flow's ground right below the layout viewport, in the page itself (see showCurtain):
1029
+ * on a phone, iOS browsers with their toolbars collapsed show a band of the page there,
1030
+ * below every fixed element, the full-screen modal included.
1031
+ */
1032
+ .${CSS_CLASSES.curtain} {
1033
+ display: none;
1034
+ position: absolute;
1035
+ top: 0;
1036
+ left: 0;
1037
+ width: 100vw;
1038
+ height: 100vh;
1039
+ margin: 0;
1040
+ pointer-events: none;
1041
+ /* Under the modal where they meet for a frame while the toolbars move, above the page. */
1042
+ z-index: ${this.config.zIndex - 1};
1043
+ background: ${FLOW_GROUND.light};
1044
+ }
1045
+
1046
+ .${CSS_CLASSES.curtain}[data-didit-theme="dark"] {
1047
+ background: ${FLOW_GROUND.dark};
1048
+ }
1049
+
1050
+ /* The exit confirmation's dimming continues over the ground below the viewport. */
1051
+ .${CSS_CLASSES.curtain}[data-didit-dimmed] {
1052
+ background-image: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5));
1053
+ }
1054
+
1055
+ @media (max-width: 540px) {
1056
+ .${CSS_CLASSES.curtain} {
1057
+ display: block;
1058
+ }
1059
+ }
1060
+
1009
1061
  /*
1010
1062
  * The hosted flow's layout message (didit:layout) brings the modal's size
1011
1063
  * (see above), theme and direction. Colours are the design system tokens
@@ -1038,7 +1090,7 @@ class VerificationModal {
1038
1090
  }
1039
1091
 
1040
1092
  .${CSS_CLASSES.flowSized} .${CSS_CLASSES.closeButton} {
1041
- top: 8px;
1093
+ top: calc(8px + var(--didit-top-inset, 0px));
1042
1094
  right: 8px;
1043
1095
  width: 44px;
1044
1096
  height: 44px;
@@ -1339,11 +1391,12 @@ class VerificationModal {
1339
1391
  /**
1340
1392
  * Reads a didit:layout message's data. Without a positive contentHeight the
1341
1393
  * message is ignored; a missing or unknown colorScheme follows the device,
1342
- * and a missing or unknown dir follows the verification URL's language.
1394
+ * a missing or unknown dir follows the verification URL's language, and the
1395
+ * close button keeps its place without a plausible topInset.
1343
1396
  */
1344
1397
  readFlowLayout(data) {
1345
1398
  const fields = typeof data === "object" && data !== null ? data : {};
1346
- const { contentHeight, colorScheme, dir, kind } = fields;
1399
+ const { contentHeight, colorScheme, dir, kind, topInset } = fields;
1347
1400
  if (typeof contentHeight !== "number" || !Number.isFinite(contentHeight) || contentHeight <= 0) {
1348
1401
  return null;
1349
1402
  }
@@ -1352,7 +1405,8 @@ class VerificationModal {
1352
1405
  kind: kind === "handoff" ? "handoff" : "step",
1353
1406
  contentHeight: Math.ceil(contentHeight),
1354
1407
  colorScheme: colorScheme === "light" || colorScheme === "dark" ? colorScheme : prefersDark ? "dark" : "light",
1355
- dir: dir === "ltr" || dir === "rtl" ? dir : isRtlLanguage(this.language) ? "rtl" : "ltr"
1408
+ dir: dir === "ltr" || dir === "rtl" ? dir : isRtlLanguage(this.language) ? "rtl" : "ltr",
1409
+ topInset: typeof topInset === "number" && topInset > 0 && topInset <= MAX_TOP_INSET ? Math.ceil(topInset) : 0
1356
1410
  };
1357
1411
  }
1358
1412
  /** Embedded mode keeps filling the integrator's host element, whatever the flow reports. */
@@ -1366,7 +1420,11 @@ class VerificationModal {
1366
1420
  // The height the person sees now, before the reported content height can change it.
1367
1421
  const shownHeight = this.container.offsetHeight;
1368
1422
  this.container.style.setProperty("--didit-content-height", `${layout.contentHeight}px`);
1423
+ // The close button sits under the flow's strip, over its top bar.
1424
+ this.container.style.setProperty("--didit-top-inset", `${layout.topInset}px`);
1369
1425
  this.matchPageBackground(layout.colorScheme);
1426
+ if (this.curtain)
1427
+ this.curtain.dataset.diditTheme = layout.colorScheme;
1370
1428
  this.setModalKind(layout.kind, layout.kind === "handoff" && this.handoffHeight(layout) === shownHeight);
1371
1429
  }
1372
1430
  /** Whether the window is a computer-sized one: a phone is full screen and never waits for a layout. */
@@ -1437,18 +1495,125 @@ class VerificationModal {
1437
1495
  matchPageBackground(colorScheme) {
1438
1496
  if (!window.matchMedia?.("(max-width: 540px)").matches)
1439
1497
  return;
1440
- const root = document.documentElement;
1441
- this.pageBackground ?? (this.pageBackground = { root: root.style.backgroundColor, body: document.body.style.backgroundColor });
1442
- const ground = colorScheme === "dark" ? "#0b0b0d" : "#ffffff";
1443
- root.style.backgroundColor = ground;
1444
- document.body.style.backgroundColor = ground;
1498
+ VerificationModal.pageBackground ?? (VerificationModal.pageBackground = {
1499
+ saved: { root: document.documentElement.style.backgroundColor, body: document.body.style.backgroundColor },
1500
+ grounds: new Map()
1501
+ });
1502
+ VerificationModal.pageBackground.grounds.set(this, colorScheme);
1503
+ VerificationModal.paintPageBackground();
1445
1504
  }
1446
1505
  restorePageBackground() {
1447
- if (!this.pageBackground)
1506
+ const background = VerificationModal.pageBackground;
1507
+ if (!background?.grounds.delete(this))
1508
+ return;
1509
+ if (background.grounds.size > 0) {
1510
+ VerificationModal.paintPageBackground();
1448
1511
  return;
1449
- document.documentElement.style.backgroundColor = this.pageBackground.root;
1450
- document.body.style.backgroundColor = this.pageBackground.body;
1451
- this.pageBackground = null;
1512
+ }
1513
+ VerificationModal.pageBackground = null;
1514
+ document.documentElement.style.backgroundColor = background.saved.root;
1515
+ document.body.style.backgroundColor = background.saved.body;
1516
+ }
1517
+ /** The page shows the ground of the topmost open modal that lends one. */
1518
+ static paintPageBackground() {
1519
+ const background = VerificationModal.pageBackground;
1520
+ if (!background)
1521
+ return;
1522
+ const lender = [...VerificationModal.openModals].reverse().find((modal) => background.grounds.has(modal));
1523
+ const colorScheme = lender ? background.grounds.get(lender) : [...background.grounds.values()].pop();
1524
+ if (!colorScheme)
1525
+ return;
1526
+ document.documentElement.style.backgroundColor = FLOW_GROUND[colorScheme];
1527
+ document.body.style.backgroundColor = FLOW_GROUND[colorScheme];
1528
+ }
1529
+ /**
1530
+ * Stops the page scrolling under the modal. The lock is on the root element: Chrome for iOS keeps
1531
+ * scrolling a page whose body alone is `overflow: hidden` (a swipe over the modal scrolls the page
1532
+ * and toggles the browser's toolbars), and stops only when the root element is. A page that
1533
+ * scrolls its body instead of the window has its body locked too. The first modal to open keeps
1534
+ * the page's own values and the last one to close gives them back.
1535
+ */
1536
+ lockPageScroll() {
1537
+ const lock = VerificationModal.pageScrollLock;
1538
+ if (lock) {
1539
+ lock.holders.add(this);
1540
+ return;
1541
+ }
1542
+ const root = document.documentElement;
1543
+ const body = document.body;
1544
+ // The body's overflow is the viewport's only while the root's is visible; otherwise a body with
1545
+ // an overflow of its own is a scroll container. Locking a body that is not one would clip the page
1546
+ // to the body's box.
1547
+ const clips = (overflow) => overflow !== "" && overflow !== "visible";
1548
+ const bodyScrollsItself = clips(window.getComputedStyle(root).overflowY) && clips(window.getComputedStyle(body).overflowY);
1549
+ VerificationModal.pageScrollLock = {
1550
+ holders: new Set([this]),
1551
+ root: { overflow: root.style.overflow, overscrollBehavior: root.style.overscrollBehavior },
1552
+ body: bodyScrollsItself ? { overflow: body.style.overflow } : null
1553
+ };
1554
+ root.style.overflow = "hidden";
1555
+ root.style.overscrollBehavior = "none";
1556
+ if (bodyScrollsItself)
1557
+ body.style.overflow = "hidden";
1558
+ }
1559
+ unlockPageScroll() {
1560
+ const lock = VerificationModal.pageScrollLock;
1561
+ if (!lock || !lock.holders.delete(this) || lock.holders.size > 0)
1562
+ return;
1563
+ VerificationModal.pageScrollLock = null;
1564
+ const root = document.documentElement;
1565
+ root.style.overflow = lock.root.overflow;
1566
+ root.style.overscrollBehavior = lock.root.overscrollBehavior;
1567
+ if (lock.body)
1568
+ document.body.style.overflow = lock.body.overflow;
1569
+ }
1570
+ /**
1571
+ * On a phone, Chrome for iOS (and the other iOS browsers built on WebKit's web view) shows a band
1572
+ * of the page below the layout viewport while its toolbars are collapsed: the home indicator's
1573
+ * area, and more while the toolbars move. Fixed elements end at the layout viewport, so the
1574
+ * full-screen modal cannot cover it and the host page's own content showed under the flow. The
1575
+ * curtain is part of the page instead: right below the viewport, in the flow's ground, so that band
1576
+ * shows the modal's ground. The page cannot scroll while the modal is open (lockPageScroll), and the
1577
+ * curtain follows the viewport when it resizes. Only phone-sized windows draw it (see the styles).
1578
+ */
1579
+ showCurtain() {
1580
+ if (!this.curtain) {
1581
+ const curtain = document.createElement("div");
1582
+ curtain.className = CSS_CLASSES.curtain;
1583
+ curtain.setAttribute("aria-hidden", "true");
1584
+ document.body.appendChild(curtain);
1585
+ this.curtain = curtain;
1586
+ this.boundPlaceCurtain = () => this.placeCurtain();
1587
+ window.addEventListener("resize", this.boundPlaceCurtain);
1588
+ window.addEventListener("scroll", this.boundPlaceCurtain);
1589
+ window.visualViewport?.addEventListener("resize", this.boundPlaceCurtain);
1590
+ }
1591
+ // The device's theme until the newly loaded flow reports its own (applyFlowLayout).
1592
+ this.curtain.dataset.diditTheme = window.matchMedia?.("(prefers-color-scheme: dark)").matches ? "dark" : "light";
1593
+ this.placeCurtain();
1594
+ }
1595
+ /** Puts the curtain's top at the bottom of the layout viewport, whatever its containing block is. */
1596
+ placeCurtain() {
1597
+ const curtain = this.curtain;
1598
+ if (!curtain)
1599
+ return;
1600
+ curtain.style.top = "0px";
1601
+ curtain.style.left = "0px";
1602
+ // At 0, 0 the curtain's box starts at its containing block's corner (the page's, or a positioned
1603
+ // body's), in viewport coordinates: the offset that brings its top to the viewport's bottom edge.
1604
+ const origin = curtain.getBoundingClientRect();
1605
+ curtain.style.top = `${window.innerHeight - origin.top}px`;
1606
+ curtain.style.left = `${-origin.left}px`;
1607
+ }
1608
+ removeCurtain() {
1609
+ if (this.boundPlaceCurtain) {
1610
+ window.removeEventListener("resize", this.boundPlaceCurtain);
1611
+ window.removeEventListener("scroll", this.boundPlaceCurtain);
1612
+ window.visualViewport?.removeEventListener("resize", this.boundPlaceCurtain);
1613
+ this.boundPlaceCurtain = null;
1614
+ }
1615
+ this.curtain?.remove();
1616
+ this.curtain = null;
1452
1617
  }
1453
1618
  /** Back to the neutral look until the newly loaded flow reports its theme and direction. */
1454
1619
  resetFlowLayout() {
@@ -1460,6 +1625,7 @@ class VerificationModal {
1460
1625
  delete this.container.dataset.diditDir;
1461
1626
  delete this.container.dataset.diditKind;
1462
1627
  this.container.style.removeProperty("--didit-content-height");
1628
+ this.container.style.removeProperty("--didit-top-inset");
1463
1629
  if (this.firstLayoutTimeout !== null) {
1464
1630
  window.clearTimeout(this.firstLayoutTimeout);
1465
1631
  this.firstLayoutTimeout = null;
@@ -1526,6 +1692,7 @@ class VerificationModal {
1526
1692
  : this.container?.querySelector(`.${CSS_CLASSES.closeButton}`) ?? this.iframe);
1527
1693
  this.state.showConfirmation = true;
1528
1694
  this.confirmOverlay.classList.add("active");
1695
+ this.curtain?.setAttribute("data-didit-dimmed", "");
1529
1696
  if (this.iframe)
1530
1697
  this.iframe.inert = true;
1531
1698
  const close = this.container?.querySelector(`.${CSS_CLASSES.closeButton}`);
@@ -1549,6 +1716,7 @@ class VerificationModal {
1549
1716
  return;
1550
1717
  this.state.showConfirmation = false;
1551
1718
  this.confirmOverlay?.classList.remove("active");
1719
+ this.curtain?.removeAttribute("data-didit-dimmed");
1552
1720
  if (this.iframe)
1553
1721
  this.iframe.inert = false;
1554
1722
  const close = this.container?.querySelector(`.${CSS_CLASSES.closeButton}`);
@@ -1576,11 +1744,6 @@ class VerificationModal {
1576
1744
  this.callbacks.onCloseConfirmed();
1577
1745
  }
1578
1746
  open(verificationUrl) {
1579
- // Whether this modal already held its scroll lock before this call. Re-opening
1580
- // an already-open instance must not re-capture previousBodyOverflow, or it
1581
- // would record this modal's own "hidden" lock and close() could never restore
1582
- // the real pre-lock value.
1583
- const wasAlreadyOpen = this.state.isOpen;
1584
1747
  this.language = detectLanguageFromUrl(verificationUrl);
1585
1748
  if (!this.overlay && !this.container) {
1586
1749
  this.createDOM();
@@ -1605,16 +1768,10 @@ class VerificationModal {
1605
1768
  return;
1606
1769
  }
1607
1770
  this.overlay?.classList.add("active");
1608
- // Capture whatever overflow value is currently in effect (which may
1609
- // already be "hidden" because another modal is open) so close() can
1610
- // restore exactly that value instead of clobbering a scroll lock owned
1611
- // by a concurrently open modal. Skip the capture on a re-open: the value
1612
- // in effect is this modal's own lock, and previousBodyOverflow already
1613
- // holds the correct pre-lock value.
1614
- if (!wasAlreadyOpen) {
1615
- this.previousBodyOverflow = document.body.style.overflow;
1616
- }
1617
- document.body.style.overflow = "hidden";
1771
+ // Idempotent: a re-open of a modal that is still open keeps its one hold on the lock, so
1772
+ // close() still gives the page back the values it had before any modal opened.
1773
+ this.lockPageScroll();
1774
+ this.showCurtain();
1618
1775
  }
1619
1776
  close() {
1620
1777
  SDKLogger.log("Closing");
@@ -1632,8 +1789,8 @@ class VerificationModal {
1632
1789
  return;
1633
1790
  }
1634
1791
  this.overlay?.classList.remove("active");
1635
- document.body.style.overflow = this.previousBodyOverflow ?? "";
1636
- this.previousBodyOverflow = null;
1792
+ this.removeCurtain();
1793
+ this.unlockPageScroll();
1637
1794
  }
1638
1795
  destroy() {
1639
1796
  SDKLogger.log("Destroying");
@@ -1665,6 +1822,16 @@ class VerificationModal {
1665
1822
  * a transaction action modal stacked over the verification modal).
1666
1823
  */
1667
1824
  VerificationModal.openModals = [];
1825
+ /**
1826
+ * The page's own background colours, kept while full-screen modals lend it their ground, shared by
1827
+ * every open modal: the topmost one's ground shows and the last one to give it back restores them.
1828
+ */
1829
+ VerificationModal.pageBackground = null;
1830
+ /**
1831
+ * The page's scroll styles from before the first open modal locked them, shared by every open
1832
+ * modal: the last one to close gives them back.
1833
+ */
1834
+ VerificationModal.pageScrollLock = null;
1668
1835
 
1669
1836
  const DEFAULT_TRANSACTION_BASE_URL = "https://verification.didit.me";
1670
1837
  const ACTION_POLL_INTERVAL_MS = 1500;
@@ -1984,6 +2151,18 @@ function assertEmbeddedHostExists(configuration) {
1984
2151
  throw new Error(`Embedded container "#${containerId}" was not found in the page.`);
1985
2152
  }
1986
2153
  }
2154
+ /**
2155
+ * The device id this page already keeps for Didit (it has sent events, transactions or sign-ins),
2156
+ * or null. Reading it never creates one, and a browser that refuses the read is not an error.
2157
+ */
2158
+ function storedDeviceId() {
2159
+ try {
2160
+ return getStoredDeviceId();
2161
+ }
2162
+ catch {
2163
+ return null;
2164
+ }
2165
+ }
1987
2166
  class DiditSdk {
1988
2167
  static get shared() {
1989
2168
  if (!DiditSdk._instance) {
@@ -2057,7 +2236,7 @@ class DiditSdk {
2057
2236
  onMessage: (event) => this.handleVerificationEvent(event),
2058
2237
  onIframeLoad: () => this.handleIframeLoad()
2059
2238
  });
2060
- this._url = buildVerificationUrl(url, config);
2239
+ this._url = buildVerificationUrl(url, config, storedDeviceId());
2061
2240
  this.setState("loading");
2062
2241
  this.emitInternalEvent("didit:started", {});
2063
2242
  this._modal?.open(this._url);