@didit-protocol/sdk-web 0.1.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.
- package/LICENSE +21 -0
- package/README.md +410 -0
- package/dist/didit-sdk.cjs.js +738 -0
- package/dist/didit-sdk.cjs.js.map +1 -0
- package/dist/didit-sdk.esm.js +732 -0
- package/dist/didit-sdk.esm.js.map +1 -0
- package/dist/didit-sdk.umd.js +744 -0
- package/dist/didit-sdk.umd.js.map +1 -0
- package/dist/didit-sdk.umd.min.js +7 -0
- package/dist/didit-sdk.umd.min.js.map +1 -0
- package/dist/index.d.ts +95 -0
- package/package.json +60 -0
|
@@ -0,0 +1,738 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Didit SDK for Web v0.1.0
|
|
3
|
+
* (c) 2026 Didit
|
|
4
|
+
* @license MIT
|
|
5
|
+
*/
|
|
6
|
+
'use strict';
|
|
7
|
+
|
|
8
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
9
|
+
|
|
10
|
+
const DEFAULT_CONFIG = {
|
|
11
|
+
zIndex: 9999,
|
|
12
|
+
showCloseButton: true,
|
|
13
|
+
showExitConfirmation: true,
|
|
14
|
+
loggingEnabled: false,
|
|
15
|
+
};
|
|
16
|
+
const CSS_CLASSES = {
|
|
17
|
+
overlay: "didit-modal-overlay",
|
|
18
|
+
container: "didit-modal-container",
|
|
19
|
+
iframe: "didit-verification-iframe",
|
|
20
|
+
closeButton: "didit-close-button",
|
|
21
|
+
loading: "didit-loading",
|
|
22
|
+
confirmOverlay: "didit-confirm-overlay",
|
|
23
|
+
confirmBox: "didit-confirm-box"
|
|
24
|
+
};
|
|
25
|
+
const SDK_VERSION = "0.1.0";
|
|
26
|
+
|
|
27
|
+
class SDKLogger {
|
|
28
|
+
static get isEnabled() {
|
|
29
|
+
return this._enabled;
|
|
30
|
+
}
|
|
31
|
+
static set isEnabled(value) {
|
|
32
|
+
this._enabled = value;
|
|
33
|
+
}
|
|
34
|
+
static log(...args) {
|
|
35
|
+
if (this._enabled) {
|
|
36
|
+
console.log("[DiditSDK]", ...args);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
static warn(...args) {
|
|
40
|
+
if (this._enabled) {
|
|
41
|
+
console.warn("[DiditSDK]", ...args);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
static error(...args) {
|
|
45
|
+
if (this._enabled) {
|
|
46
|
+
console.error("[DiditSDK]", ...args);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
SDKLogger._enabled = false;
|
|
51
|
+
function generateModalId() {
|
|
52
|
+
return `didit-modal-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
|
53
|
+
}
|
|
54
|
+
function isAllowedOrigin(origin) {
|
|
55
|
+
try {
|
|
56
|
+
const url = new URL(origin);
|
|
57
|
+
return url.hostname.endsWith('.didit.me');
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
function createVerificationError(type, customMessage) {
|
|
64
|
+
const messages = {
|
|
65
|
+
sessionExpired: "Your verification session has expired.",
|
|
66
|
+
networkError: "A network error occurred. Please try again.",
|
|
67
|
+
cameraAccessDenied: "Camera access is required for verification.",
|
|
68
|
+
unknown: customMessage || "An unknown error occurred."
|
|
69
|
+
};
|
|
70
|
+
return {
|
|
71
|
+
type,
|
|
72
|
+
message: customMessage || messages[type]
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
class VerificationModal {
|
|
77
|
+
constructor(configuration, callbacks) {
|
|
78
|
+
this.state = {
|
|
79
|
+
isOpen: false,
|
|
80
|
+
isLoading: true,
|
|
81
|
+
showConfirmation: false
|
|
82
|
+
};
|
|
83
|
+
this.overlay = null;
|
|
84
|
+
this.container = null;
|
|
85
|
+
this.iframe = null;
|
|
86
|
+
this.loadingEl = null;
|
|
87
|
+
this.confirmOverlay = null;
|
|
88
|
+
this.boundHandleMessage = null;
|
|
89
|
+
this.boundHandleKeydown = null;
|
|
90
|
+
this.modalId = generateModalId();
|
|
91
|
+
this.config = {
|
|
92
|
+
zIndex: configuration?.zIndex ?? DEFAULT_CONFIG.zIndex,
|
|
93
|
+
showCloseButton: configuration?.showCloseButton ?? DEFAULT_CONFIG.showCloseButton,
|
|
94
|
+
showExitConfirmation: configuration?.showExitConfirmation ?? DEFAULT_CONFIG.showExitConfirmation
|
|
95
|
+
};
|
|
96
|
+
this.callbacks = callbacks;
|
|
97
|
+
this.containerElement = configuration?.containerElement ?? document.body;
|
|
98
|
+
}
|
|
99
|
+
injectStyles() {
|
|
100
|
+
const styleId = "didit-sdk-styles";
|
|
101
|
+
if (document.getElementById(styleId)) {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
const styles = document.createElement("style");
|
|
105
|
+
styles.id = styleId;
|
|
106
|
+
styles.textContent = `
|
|
107
|
+
.${CSS_CLASSES.overlay} {
|
|
108
|
+
display: none;
|
|
109
|
+
position: fixed;
|
|
110
|
+
inset: 0;
|
|
111
|
+
background: rgba(0, 0, 0, 0.6);
|
|
112
|
+
z-index: ${this.config.zIndex};
|
|
113
|
+
justify-content: center;
|
|
114
|
+
align-items: center;
|
|
115
|
+
padding: 1rem;
|
|
116
|
+
opacity: 0;
|
|
117
|
+
transition: opacity 0.2s ease-out;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
.${CSS_CLASSES.overlay}.active {
|
|
121
|
+
display: flex;
|
|
122
|
+
opacity: 1;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
.${CSS_CLASSES.container} {
|
|
126
|
+
position: relative;
|
|
127
|
+
width: 100%;
|
|
128
|
+
max-width: 500px;
|
|
129
|
+
max-height: 90vh;
|
|
130
|
+
border-radius: 16px;
|
|
131
|
+
overflow: hidden;
|
|
132
|
+
background: transparent;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
.${CSS_CLASSES.overlay}.active .${CSS_CLASSES.container} {
|
|
136
|
+
transform: scale(1);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
.${CSS_CLASSES.iframe} {
|
|
140
|
+
width: 100%;
|
|
141
|
+
height: 700px;
|
|
142
|
+
border: none;
|
|
143
|
+
display: block;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
.${CSS_CLASSES.closeButton} {
|
|
147
|
+
position: absolute;
|
|
148
|
+
top: 12px;
|
|
149
|
+
right: 12px;
|
|
150
|
+
width: 24px;
|
|
151
|
+
height: 24px;
|
|
152
|
+
background: transparent;
|
|
153
|
+
border: none;
|
|
154
|
+
cursor: pointer;
|
|
155
|
+
padding: 0;
|
|
156
|
+
z-index: 10;
|
|
157
|
+
outline: none;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
.${CSS_CLASSES.closeButton}:hover,
|
|
161
|
+
.${CSS_CLASSES.closeButton}:focus {
|
|
162
|
+
background: transparent;
|
|
163
|
+
opacity: 0.5;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
.${CSS_CLASSES.closeButton} svg {
|
|
167
|
+
stroke: #666;
|
|
168
|
+
stroke-width: 2;
|
|
169
|
+
stroke-linecap: round;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
.${CSS_CLASSES.loading} {
|
|
173
|
+
position: absolute;
|
|
174
|
+
inset: 0;
|
|
175
|
+
display: flex;
|
|
176
|
+
align-items: center;
|
|
177
|
+
justify-content: center;
|
|
178
|
+
background: #fafafa;
|
|
179
|
+
z-index: 5;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
.${CSS_CLASSES.loading}.hidden {
|
|
183
|
+
display: none;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
.${CSS_CLASSES.loading} svg {
|
|
187
|
+
width: 48px;
|
|
188
|
+
height: 48px;
|
|
189
|
+
animation: didit-spin 1s linear infinite;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
.${CSS_CLASSES.loading} circle {
|
|
193
|
+
stroke: #e5e5e5;
|
|
194
|
+
stroke-width: 3;
|
|
195
|
+
fill: none;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
.${CSS_CLASSES.loading} path {
|
|
199
|
+
stroke: #525252;
|
|
200
|
+
stroke-width: 3;
|
|
201
|
+
stroke-linecap: round;
|
|
202
|
+
fill: none;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
@keyframes didit-spin {
|
|
206
|
+
from { transform: rotate(0deg); }
|
|
207
|
+
to { transform: rotate(360deg); }
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
.${CSS_CLASSES.confirmOverlay} {
|
|
211
|
+
display: none;
|
|
212
|
+
position: absolute;
|
|
213
|
+
inset: 0;
|
|
214
|
+
background: rgba(0, 0, 0, 0.5);
|
|
215
|
+
z-index: 20;
|
|
216
|
+
justify-content: center;
|
|
217
|
+
align-items: center;
|
|
218
|
+
opacity: 0;
|
|
219
|
+
transition: opacity 0.15s ease-out;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
.${CSS_CLASSES.confirmOverlay}.active {
|
|
223
|
+
display: flex;
|
|
224
|
+
opacity: 1;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
.${CSS_CLASSES.confirmBox} {
|
|
228
|
+
background: #fff;
|
|
229
|
+
border-radius: 12px;
|
|
230
|
+
padding: 1.5rem;
|
|
231
|
+
text-align: center;
|
|
232
|
+
max-width: 300px;
|
|
233
|
+
margin: 1rem;
|
|
234
|
+
transform: scale(0.95);
|
|
235
|
+
transition: transform 0.15s ease-out;
|
|
236
|
+
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
.${CSS_CLASSES.confirmOverlay}.active .${CSS_CLASSES.confirmBox} {
|
|
240
|
+
transform: scale(1);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
.${CSS_CLASSES.confirmBox} h3 {
|
|
244
|
+
color: #1a1a2e;
|
|
245
|
+
margin: 0 0 0.5rem 0;
|
|
246
|
+
font-size: 1.125rem;
|
|
247
|
+
font-weight: 600;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
.${CSS_CLASSES.confirmBox} p {
|
|
251
|
+
color: #666;
|
|
252
|
+
font-size: 0.875rem;
|
|
253
|
+
margin: 0 0 1.25rem 0;
|
|
254
|
+
line-height: 1.5;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
.didit-confirm-actions {
|
|
258
|
+
display: flex;
|
|
259
|
+
align-items: center;
|
|
260
|
+
justify-content: center;
|
|
261
|
+
gap: 1rem;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
.didit-confirm-actions button {
|
|
265
|
+
background: #2563eb;
|
|
266
|
+
color: #fff;
|
|
267
|
+
border: none;
|
|
268
|
+
padding: 0.625rem 1.25rem;
|
|
269
|
+
border-radius: 8px;
|
|
270
|
+
font-size: 0.875rem;
|
|
271
|
+
font-weight: 500;
|
|
272
|
+
cursor: pointer;
|
|
273
|
+
transition: background 0.15s ease;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
.didit-confirm-actions button:hover {
|
|
277
|
+
background: #1d4ed8;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
.didit-confirm-actions span {
|
|
281
|
+
color: #666;
|
|
282
|
+
font-size: 0.875rem;
|
|
283
|
+
cursor: pointer;
|
|
284
|
+
padding: 0.625rem;
|
|
285
|
+
transition: color 0.15s ease;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
.didit-confirm-actions span:hover {
|
|
289
|
+
color: #1a1a2e;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
@media (max-width: 540px) {
|
|
293
|
+
.${CSS_CLASSES.overlay} {
|
|
294
|
+
padding: 0;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
.${CSS_CLASSES.container} {
|
|
298
|
+
max-width: 100%;
|
|
299
|
+
max-height: 100vh;
|
|
300
|
+
border-radius: 0;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
.${CSS_CLASSES.iframe} {
|
|
304
|
+
height: 100vh;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
`;
|
|
308
|
+
document.head.appendChild(styles);
|
|
309
|
+
}
|
|
310
|
+
createDOM() {
|
|
311
|
+
this.injectStyles();
|
|
312
|
+
this.overlay = document.createElement("div");
|
|
313
|
+
this.overlay.id = this.modalId;
|
|
314
|
+
this.overlay.className = CSS_CLASSES.overlay;
|
|
315
|
+
this.overlay.setAttribute("role", "dialog");
|
|
316
|
+
this.overlay.setAttribute("aria-modal", "true");
|
|
317
|
+
this.overlay.setAttribute("aria-label", "Didit Verification");
|
|
318
|
+
this.container = document.createElement("div");
|
|
319
|
+
this.container.className = CSS_CLASSES.container;
|
|
320
|
+
this.loadingEl = document.createElement("div");
|
|
321
|
+
this.loadingEl.className = CSS_CLASSES.loading;
|
|
322
|
+
this.loadingEl.innerHTML = `
|
|
323
|
+
<svg viewBox="0 0 24 24">
|
|
324
|
+
<circle cx="12" cy="12" r="10" />
|
|
325
|
+
<path d="M12 2a10 10 0 0 1 10 10" />
|
|
326
|
+
</svg>
|
|
327
|
+
`;
|
|
328
|
+
if (this.config.showCloseButton) {
|
|
329
|
+
const closeBtn = document.createElement("button");
|
|
330
|
+
closeBtn.className = CSS_CLASSES.closeButton;
|
|
331
|
+
closeBtn.setAttribute("aria-label", "Close verification");
|
|
332
|
+
closeBtn.innerHTML = `
|
|
333
|
+
<svg width="14" height="14" viewBox="0 0 14 14">
|
|
334
|
+
<line x1="1" y1="1" x2="13" y2="13" />
|
|
335
|
+
<line x1="13" y1="1" x2="1" y2="13" />
|
|
336
|
+
</svg>
|
|
337
|
+
`;
|
|
338
|
+
closeBtn.addEventListener("click", () => this.handleCloseRequest());
|
|
339
|
+
this.container.appendChild(closeBtn);
|
|
340
|
+
}
|
|
341
|
+
this.iframe = document.createElement("iframe");
|
|
342
|
+
this.iframe.className = CSS_CLASSES.iframe;
|
|
343
|
+
this.iframe.setAttribute("allow", "camera; microphone; fullscreen; autoplay; encrypted-media; geolocation");
|
|
344
|
+
this.iframe.setAttribute("title", "Didit Verification");
|
|
345
|
+
this.iframe.addEventListener("load", () => this.handleIframeLoad());
|
|
346
|
+
this.confirmOverlay = document.createElement("div");
|
|
347
|
+
this.confirmOverlay.className = CSS_CLASSES.confirmOverlay;
|
|
348
|
+
this.confirmOverlay.innerHTML = `
|
|
349
|
+
<div class="${CSS_CLASSES.confirmBox}">
|
|
350
|
+
<h3>Exit verification?</h3>
|
|
351
|
+
<p>Exiting will end your verification process. Are you sure?</p>
|
|
352
|
+
<div class="didit-confirm-actions">
|
|
353
|
+
<button type="button" data-action="continue">Continue</button>
|
|
354
|
+
<span data-action="exit">Exit</span>
|
|
355
|
+
</div>
|
|
356
|
+
</div>
|
|
357
|
+
`;
|
|
358
|
+
this.confirmOverlay.querySelector('[data-action="continue"]')?.addEventListener("click", () => {
|
|
359
|
+
this.hideConfirmation();
|
|
360
|
+
});
|
|
361
|
+
this.confirmOverlay.querySelector('[data-action="exit"]')?.addEventListener("click", () => {
|
|
362
|
+
this.confirmExit();
|
|
363
|
+
});
|
|
364
|
+
this.container.appendChild(this.loadingEl);
|
|
365
|
+
this.container.appendChild(this.iframe);
|
|
366
|
+
this.container.appendChild(this.confirmOverlay);
|
|
367
|
+
this.overlay.appendChild(this.container);
|
|
368
|
+
this.overlay.addEventListener("click", (e) => {
|
|
369
|
+
if (e.target === this.overlay) {
|
|
370
|
+
this.handleCloseRequest();
|
|
371
|
+
}
|
|
372
|
+
});
|
|
373
|
+
this.containerElement.appendChild(this.overlay);
|
|
374
|
+
}
|
|
375
|
+
setupEventListeners() {
|
|
376
|
+
this.boundHandleMessage = this.handleMessage.bind(this);
|
|
377
|
+
window.addEventListener("message", this.boundHandleMessage);
|
|
378
|
+
this.boundHandleKeydown = this.handleKeydown.bind(this);
|
|
379
|
+
document.addEventListener("keydown", this.boundHandleKeydown);
|
|
380
|
+
}
|
|
381
|
+
removeEventListeners() {
|
|
382
|
+
if (this.boundHandleMessage) {
|
|
383
|
+
window.removeEventListener("message", this.boundHandleMessage);
|
|
384
|
+
this.boundHandleMessage = null;
|
|
385
|
+
}
|
|
386
|
+
if (this.boundHandleKeydown) {
|
|
387
|
+
document.removeEventListener("keydown", this.boundHandleKeydown);
|
|
388
|
+
this.boundHandleKeydown = null;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
handleMessage(event) {
|
|
392
|
+
if (!isAllowedOrigin(event.origin)) {
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
SDKLogger.log("Received postMessage:", event.data);
|
|
396
|
+
let messageData;
|
|
397
|
+
try {
|
|
398
|
+
if (typeof event.data === "string") {
|
|
399
|
+
messageData = JSON.parse(event.data);
|
|
400
|
+
}
|
|
401
|
+
else {
|
|
402
|
+
messageData = event.data;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
catch {
|
|
406
|
+
SDKLogger.warn("Failed to parse postMessage:", event.data);
|
|
407
|
+
return;
|
|
408
|
+
}
|
|
409
|
+
if (messageData.type === "didit:close_request") {
|
|
410
|
+
this.handleCloseRequest();
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
this.callbacks.onMessage(messageData);
|
|
414
|
+
}
|
|
415
|
+
handleKeydown(event) {
|
|
416
|
+
if (!this.state.isOpen)
|
|
417
|
+
return;
|
|
418
|
+
if (event.key === "Escape") {
|
|
419
|
+
event.preventDefault();
|
|
420
|
+
if (this.state.showConfirmation) {
|
|
421
|
+
this.hideConfirmation();
|
|
422
|
+
}
|
|
423
|
+
else {
|
|
424
|
+
this.handleCloseRequest();
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
handleIframeLoad() {
|
|
429
|
+
if (this.iframe?.src && this.iframe.src !== "about:blank") {
|
|
430
|
+
this.state.isLoading = false;
|
|
431
|
+
this.loadingEl?.classList.add("hidden");
|
|
432
|
+
this.callbacks.onIframeLoad();
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
handleCloseRequest() {
|
|
436
|
+
if (this.config.showExitConfirmation) {
|
|
437
|
+
this.showConfirmation();
|
|
438
|
+
}
|
|
439
|
+
else {
|
|
440
|
+
this.callbacks.onCloseConfirmed();
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
showConfirmation() {
|
|
444
|
+
this.state.showConfirmation = true;
|
|
445
|
+
this.confirmOverlay?.classList.add("active");
|
|
446
|
+
this.callbacks.onClose();
|
|
447
|
+
}
|
|
448
|
+
hideConfirmation() {
|
|
449
|
+
this.state.showConfirmation = false;
|
|
450
|
+
this.confirmOverlay?.classList.remove("active");
|
|
451
|
+
}
|
|
452
|
+
confirmExit() {
|
|
453
|
+
this.hideConfirmation();
|
|
454
|
+
this.callbacks.onCloseConfirmed();
|
|
455
|
+
}
|
|
456
|
+
open(verificationUrl) {
|
|
457
|
+
if (!this.overlay) {
|
|
458
|
+
this.createDOM();
|
|
459
|
+
this.setupEventListeners();
|
|
460
|
+
}
|
|
461
|
+
SDKLogger.log("Opening modal with URL:", verificationUrl);
|
|
462
|
+
this.state.isLoading = true;
|
|
463
|
+
this.state.showConfirmation = false;
|
|
464
|
+
this.loadingEl?.classList.remove("hidden");
|
|
465
|
+
this.confirmOverlay?.classList.remove("active");
|
|
466
|
+
if (this.iframe) {
|
|
467
|
+
this.iframe.src = verificationUrl;
|
|
468
|
+
}
|
|
469
|
+
this.state.isOpen = true;
|
|
470
|
+
this.overlay?.classList.add("active");
|
|
471
|
+
document.body.style.overflow = "hidden";
|
|
472
|
+
}
|
|
473
|
+
close() {
|
|
474
|
+
SDKLogger.log("Closing modal");
|
|
475
|
+
this.state.isOpen = false;
|
|
476
|
+
this.state.isLoading = true;
|
|
477
|
+
this.state.showConfirmation = false;
|
|
478
|
+
this.overlay?.classList.remove("active");
|
|
479
|
+
if (this.iframe) {
|
|
480
|
+
this.iframe.src = "about:blank";
|
|
481
|
+
}
|
|
482
|
+
document.body.style.overflow = "";
|
|
483
|
+
}
|
|
484
|
+
destroy() {
|
|
485
|
+
SDKLogger.log("Destroying modal");
|
|
486
|
+
this.close();
|
|
487
|
+
this.removeEventListeners();
|
|
488
|
+
if (this.overlay && this.overlay.parentNode) {
|
|
489
|
+
this.overlay.parentNode.removeChild(this.overlay);
|
|
490
|
+
}
|
|
491
|
+
this.overlay = null;
|
|
492
|
+
this.container = null;
|
|
493
|
+
this.iframe = null;
|
|
494
|
+
this.loadingEl = null;
|
|
495
|
+
this.confirmOverlay = null;
|
|
496
|
+
}
|
|
497
|
+
isOpen() {
|
|
498
|
+
return this.state.isOpen;
|
|
499
|
+
}
|
|
500
|
+
isLoading() {
|
|
501
|
+
return this.state.isLoading;
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
class DiditSdk {
|
|
506
|
+
static get shared() {
|
|
507
|
+
if (!DiditSdk._instance) {
|
|
508
|
+
DiditSdk._instance = new DiditSdk();
|
|
509
|
+
}
|
|
510
|
+
return DiditSdk._instance;
|
|
511
|
+
}
|
|
512
|
+
static reset() {
|
|
513
|
+
if (DiditSdk._instance) {
|
|
514
|
+
DiditSdk._instance.destroy();
|
|
515
|
+
DiditSdk._instance = null;
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
get state() {
|
|
519
|
+
return this._state;
|
|
520
|
+
}
|
|
521
|
+
get configuration() {
|
|
522
|
+
return this._configuration;
|
|
523
|
+
}
|
|
524
|
+
get isPresented() {
|
|
525
|
+
return this._modal?.isOpen() ?? false;
|
|
526
|
+
}
|
|
527
|
+
get errorMessage() {
|
|
528
|
+
return this._errorMessage;
|
|
529
|
+
}
|
|
530
|
+
constructor() {
|
|
531
|
+
this._state = "idle";
|
|
532
|
+
this._modal = null;
|
|
533
|
+
SDKLogger.log("DiditSdk initialized");
|
|
534
|
+
}
|
|
535
|
+
async startVerification(options) {
|
|
536
|
+
const config = options.configuration;
|
|
537
|
+
this._configuration = config;
|
|
538
|
+
SDKLogger.isEnabled = config?.loggingEnabled ?? DEFAULT_CONFIG.loggingEnabled;
|
|
539
|
+
SDKLogger.log("Starting verification with options:", options);
|
|
540
|
+
this._modal = new VerificationModal(config, {
|
|
541
|
+
onClose: () => this.handleModalClose(),
|
|
542
|
+
onCloseConfirmed: () => this.handleModalCloseConfirmed(),
|
|
543
|
+
onMessage: (event) => this.handleVerificationEvent(event),
|
|
544
|
+
onIframeLoad: () => this.handleIframeLoad()
|
|
545
|
+
});
|
|
546
|
+
try {
|
|
547
|
+
const { url } = options;
|
|
548
|
+
if (!url || typeof url !== "string") {
|
|
549
|
+
throw new Error("Invalid options: url is required");
|
|
550
|
+
}
|
|
551
|
+
this._url = url;
|
|
552
|
+
this.setState("loading");
|
|
553
|
+
this.emitInternalEvent("didit:started", {});
|
|
554
|
+
this._modal?.open(this._url);
|
|
555
|
+
this.setState("ready");
|
|
556
|
+
}
|
|
557
|
+
catch (error) {
|
|
558
|
+
this.handleError(error);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
close() {
|
|
562
|
+
SDKLogger.log("Closing verification programmatically");
|
|
563
|
+
this.handleModalCloseConfirmed();
|
|
564
|
+
}
|
|
565
|
+
destroy() {
|
|
566
|
+
SDKLogger.log("Destroying SDK instance");
|
|
567
|
+
this._modal?.destroy();
|
|
568
|
+
this._modal = null;
|
|
569
|
+
this.reset();
|
|
570
|
+
}
|
|
571
|
+
handleModalClose() {
|
|
572
|
+
SDKLogger.log("Modal close requested");
|
|
573
|
+
}
|
|
574
|
+
handleModalCloseConfirmed() {
|
|
575
|
+
SDKLogger.log("Modal close confirmed");
|
|
576
|
+
const sessionData = this.buildSessionData();
|
|
577
|
+
this._modal?.close();
|
|
578
|
+
this.reset();
|
|
579
|
+
const result = {
|
|
580
|
+
type: "cancelled",
|
|
581
|
+
session: sessionData
|
|
582
|
+
};
|
|
583
|
+
this.onComplete?.(result);
|
|
584
|
+
}
|
|
585
|
+
handleIframeLoad() {
|
|
586
|
+
SDKLogger.log("Iframe loaded");
|
|
587
|
+
this.emitInternalEvent("didit:ready", {
|
|
588
|
+
sessionId: this._sessionId
|
|
589
|
+
});
|
|
590
|
+
}
|
|
591
|
+
emitInternalEvent(type, data) {
|
|
592
|
+
const event = {
|
|
593
|
+
type,
|
|
594
|
+
data,
|
|
595
|
+
timestamp: Date.now()
|
|
596
|
+
};
|
|
597
|
+
SDKLogger.log("Emitting internal event:", event);
|
|
598
|
+
this.onEvent?.(event);
|
|
599
|
+
}
|
|
600
|
+
handleVerificationEvent(event) {
|
|
601
|
+
SDKLogger.log("Verification event:", event);
|
|
602
|
+
this.onEvent?.(event);
|
|
603
|
+
switch (event.type) {
|
|
604
|
+
case "didit:ready":
|
|
605
|
+
SDKLogger.log("Verification iframe ready");
|
|
606
|
+
break;
|
|
607
|
+
case "didit:started":
|
|
608
|
+
SDKLogger.log("User started verification");
|
|
609
|
+
break;
|
|
610
|
+
case "didit:step_started":
|
|
611
|
+
SDKLogger.log("Step started:", event.data?.step);
|
|
612
|
+
break;
|
|
613
|
+
case "didit:step_completed":
|
|
614
|
+
SDKLogger.log("Step completed:", event.data?.step, "-> next:", event.data?.nextStep);
|
|
615
|
+
break;
|
|
616
|
+
case "didit:media_started":
|
|
617
|
+
SDKLogger.log("Media started:", event.data?.mediaType, "for step:", event.data?.step);
|
|
618
|
+
break;
|
|
619
|
+
case "didit:media_captured":
|
|
620
|
+
SDKLogger.log("Media captured for step:", event.data?.step, "isAuto:", event.data?.isAuto);
|
|
621
|
+
break;
|
|
622
|
+
case "didit:document_selected":
|
|
623
|
+
SDKLogger.log("Document selected:", event.data?.documentType, "country:", event.data?.country);
|
|
624
|
+
break;
|
|
625
|
+
case "didit:verification_submitted":
|
|
626
|
+
SDKLogger.log("Verification submitted for step:", event.data?.step);
|
|
627
|
+
break;
|
|
628
|
+
case "didit:code_sent":
|
|
629
|
+
SDKLogger.log("Code sent via:", event.data?.channel, "codeSize:", event.data?.codeSize);
|
|
630
|
+
break;
|
|
631
|
+
case "didit:code_verified":
|
|
632
|
+
SDKLogger.log("Code verified via:", event.data?.channel);
|
|
633
|
+
break;
|
|
634
|
+
case "didit:status_updated":
|
|
635
|
+
SDKLogger.log("Status updated:", event.data?.status, "step:", event.data?.step);
|
|
636
|
+
break;
|
|
637
|
+
case "didit:completed":
|
|
638
|
+
this.handleVerificationCompleted(event);
|
|
639
|
+
break;
|
|
640
|
+
case "didit:cancelled":
|
|
641
|
+
this.handleVerificationCancelled(event);
|
|
642
|
+
break;
|
|
643
|
+
case "didit:error":
|
|
644
|
+
this.handleVerificationError(event);
|
|
645
|
+
break;
|
|
646
|
+
case "didit:step_changed":
|
|
647
|
+
SDKLogger.log("Step changed:", event.data?.step);
|
|
648
|
+
break;
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
handleVerificationCompleted(event) {
|
|
652
|
+
SDKLogger.log("Verification completed:", event.data);
|
|
653
|
+
const sessionData = this.buildSessionData(event.data);
|
|
654
|
+
this._modal?.close();
|
|
655
|
+
this.reset();
|
|
656
|
+
const result = {
|
|
657
|
+
type: "completed",
|
|
658
|
+
session: sessionData
|
|
659
|
+
};
|
|
660
|
+
this.onComplete?.(result);
|
|
661
|
+
}
|
|
662
|
+
handleVerificationCancelled(event) {
|
|
663
|
+
SDKLogger.log("Verification cancelled:", event.data);
|
|
664
|
+
const sessionData = this.buildSessionData(event.data);
|
|
665
|
+
this._modal?.close();
|
|
666
|
+
this.reset();
|
|
667
|
+
const result = {
|
|
668
|
+
type: "cancelled",
|
|
669
|
+
session: sessionData
|
|
670
|
+
};
|
|
671
|
+
this.onComplete?.(result);
|
|
672
|
+
}
|
|
673
|
+
handleVerificationError(event) {
|
|
674
|
+
SDKLogger.log("Verification error:", event.data);
|
|
675
|
+
const sessionData = this.buildSessionData(event.data);
|
|
676
|
+
const error = createVerificationError("unknown", event.data?.error);
|
|
677
|
+
this._modal?.close();
|
|
678
|
+
this.reset();
|
|
679
|
+
const result = {
|
|
680
|
+
type: "failed",
|
|
681
|
+
session: sessionData,
|
|
682
|
+
error
|
|
683
|
+
};
|
|
684
|
+
this.onComplete?.(result);
|
|
685
|
+
}
|
|
686
|
+
handleError(error) {
|
|
687
|
+
SDKLogger.error("SDK error:", error);
|
|
688
|
+
let verificationError;
|
|
689
|
+
if (error instanceof Error) {
|
|
690
|
+
verificationError = createVerificationError("unknown", error.message);
|
|
691
|
+
}
|
|
692
|
+
else {
|
|
693
|
+
verificationError = createVerificationError("unknown", "An unknown error occurred");
|
|
694
|
+
}
|
|
695
|
+
this._errorMessage = verificationError.message;
|
|
696
|
+
this.setState("error");
|
|
697
|
+
this._modal?.close();
|
|
698
|
+
this.reset();
|
|
699
|
+
const result = {
|
|
700
|
+
type: "failed",
|
|
701
|
+
error: verificationError
|
|
702
|
+
};
|
|
703
|
+
this.onComplete?.(result);
|
|
704
|
+
}
|
|
705
|
+
setState(state) {
|
|
706
|
+
const previousState = this._state;
|
|
707
|
+
this._state = state;
|
|
708
|
+
if (previousState !== state) {
|
|
709
|
+
SDKLogger.log("State changed:", previousState, "->", state);
|
|
710
|
+
this.onStateChange?.(state, this._errorMessage);
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
reset() {
|
|
714
|
+
this._state = "idle";
|
|
715
|
+
this._sessionId = undefined;
|
|
716
|
+
this._url = undefined;
|
|
717
|
+
this._errorMessage = undefined;
|
|
718
|
+
this._configuration = undefined;
|
|
719
|
+
}
|
|
720
|
+
buildSessionData(eventData) {
|
|
721
|
+
const sessionId = eventData?.sessionId || this._sessionId;
|
|
722
|
+
if (!sessionId) {
|
|
723
|
+
return undefined;
|
|
724
|
+
}
|
|
725
|
+
return {
|
|
726
|
+
sessionId,
|
|
727
|
+
status: (eventData?.status || "Pending"),
|
|
728
|
+
country: eventData?.country,
|
|
729
|
+
documentType: eventData?.documentType
|
|
730
|
+
};
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
DiditSdk._instance = null;
|
|
734
|
+
|
|
735
|
+
exports.DiditSdk = DiditSdk;
|
|
736
|
+
exports.SDK_VERSION = SDK_VERSION;
|
|
737
|
+
exports.default = DiditSdk;
|
|
738
|
+
//# sourceMappingURL=didit-sdk.cjs.js.map
|