@kwik-id/sdk-react-native 0.0.0-stage → 0.1.0-alpha.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.
Files changed (269) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +81 -2
  3. package/app.plugin.js +40 -0
  4. package/lib/commonjs/api/api-client.js +213 -0
  5. package/lib/commonjs/api/api-client.js.map +1 -0
  6. package/lib/commonjs/api/api-error.js +29 -0
  7. package/lib/commonjs/api/api-error.js.map +1 -0
  8. package/lib/commonjs/api/index.js +33 -0
  9. package/lib/commonjs/api/index.js.map +1 -0
  10. package/lib/commonjs/api/upload.js +74 -0
  11. package/lib/commonjs/api/upload.js.map +1 -0
  12. package/lib/commonjs/camera/camera-view.js +227 -0
  13. package/lib/commonjs/camera/camera-view.js.map +1 -0
  14. package/lib/commonjs/camera/index.js +20 -0
  15. package/lib/commonjs/camera/index.js.map +1 -0
  16. package/lib/commonjs/camera/photo-capture.js +50 -0
  17. package/lib/commonjs/camera/photo-capture.js.map +1 -0
  18. package/lib/commonjs/crypto/fingerprint.js +43 -0
  19. package/lib/commonjs/crypto/fingerprint.js.map +1 -0
  20. package/lib/commonjs/crypto/hmac.js +150 -0
  21. package/lib/commonjs/crypto/hmac.js.map +1 -0
  22. package/lib/commonjs/crypto/index.js +39 -0
  23. package/lib/commonjs/crypto/index.js.map +1 -0
  24. package/lib/commonjs/crypto/session-manager.js +173 -0
  25. package/lib/commonjs/crypto/session-manager.js.map +1 -0
  26. package/lib/commonjs/i18n/en.js +297 -0
  27. package/lib/commonjs/i18n/en.js.map +1 -0
  28. package/lib/commonjs/i18n/fr.js +294 -0
  29. package/lib/commonjs/i18n/fr.js.map +1 -0
  30. package/lib/commonjs/i18n/index.js +34 -0
  31. package/lib/commonjs/i18n/index.js.map +1 -0
  32. package/lib/commonjs/i18n/locale.js +20 -0
  33. package/lib/commonjs/i18n/locale.js.map +1 -0
  34. package/lib/commonjs/i18n/translator.js +37 -0
  35. package/lib/commonjs/i18n/translator.js.map +1 -0
  36. package/lib/commonjs/index.js +187 -0
  37. package/lib/commonjs/index.js.map +1 -0
  38. package/lib/commonjs/lib/device-detector.js +34 -0
  39. package/lib/commonjs/lib/device-detector.js.map +1 -0
  40. package/lib/commonjs/lib/event-emitter.js +85 -0
  41. package/lib/commonjs/lib/event-emitter.js.map +1 -0
  42. package/lib/commonjs/lib/index.js +63 -0
  43. package/lib/commonjs/lib/index.js.map +1 -0
  44. package/lib/commonjs/lib/kwik-id-rn-sdk.js +156 -0
  45. package/lib/commonjs/lib/kwik-id-rn-sdk.js.map +1 -0
  46. package/lib/commonjs/lib/verification-machine.js +276 -0
  47. package/lib/commonjs/lib/verification-machine.js.map +1 -0
  48. package/lib/commonjs/package.json +1 -0
  49. package/lib/commonjs/types/events.js +6 -0
  50. package/lib/commonjs/types/events.js.map +1 -0
  51. package/lib/commonjs/types/index.js +2 -0
  52. package/lib/commonjs/types/index.js.map +1 -0
  53. package/lib/commonjs/ui/components/KwikIDVerificationModal.js +407 -0
  54. package/lib/commonjs/ui/components/KwikIDVerificationModal.js.map +1 -0
  55. package/lib/commonjs/ui/components/screens.js +338 -0
  56. package/lib/commonjs/ui/components/screens.js.map +1 -0
  57. package/lib/commonjs/ui/context/kwik-id-provider.js +52 -0
  58. package/lib/commonjs/ui/context/kwik-id-provider.js.map +1 -0
  59. package/lib/commonjs/ui/hooks/use-kwik-verification.js +77 -0
  60. package/lib/commonjs/ui/hooks/use-kwik-verification.js.map +1 -0
  61. package/lib/commonjs/ui/index.js +33 -0
  62. package/lib/commonjs/ui/index.js.map +1 -0
  63. package/lib/module/api/api-client.js +208 -0
  64. package/lib/module/api/api-client.js.map +1 -0
  65. package/lib/module/api/api-error.js +24 -0
  66. package/lib/module/api/api-error.js.map +1 -0
  67. package/lib/module/api/index.js +6 -0
  68. package/lib/module/api/index.js.map +1 -0
  69. package/lib/module/api/upload.js +68 -0
  70. package/lib/module/api/upload.js.map +1 -0
  71. package/lib/module/camera/camera-view.js +222 -0
  72. package/lib/module/camera/camera-view.js.map +1 -0
  73. package/lib/module/camera/index.js +5 -0
  74. package/lib/module/camera/index.js.map +1 -0
  75. package/lib/module/camera/photo-capture.js +45 -0
  76. package/lib/module/camera/photo-capture.js.map +1 -0
  77. package/lib/module/crypto/fingerprint.js +37 -0
  78. package/lib/module/crypto/fingerprint.js.map +1 -0
  79. package/lib/module/crypto/hmac.js +143 -0
  80. package/lib/module/crypto/hmac.js.map +1 -0
  81. package/lib/module/crypto/index.js +6 -0
  82. package/lib/module/crypto/index.js.map +1 -0
  83. package/lib/module/crypto/session-manager.js +169 -0
  84. package/lib/module/crypto/session-manager.js.map +1 -0
  85. package/lib/module/i18n/en.js +293 -0
  86. package/lib/module/i18n/en.js.map +1 -0
  87. package/lib/module/i18n/fr.js +290 -0
  88. package/lib/module/i18n/fr.js.map +1 -0
  89. package/lib/module/i18n/index.js +11 -0
  90. package/lib/module/i18n/index.js.map +1 -0
  91. package/lib/module/i18n/locale.js +15 -0
  92. package/lib/module/i18n/locale.js.map +1 -0
  93. package/lib/module/i18n/translator.js +34 -0
  94. package/lib/module/i18n/translator.js.map +1 -0
  95. package/lib/module/index.js +30 -0
  96. package/lib/module/index.js.map +1 -0
  97. package/lib/module/lib/device-detector.js +26 -0
  98. package/lib/module/lib/device-detector.js.map +1 -0
  99. package/lib/module/lib/event-emitter.js +80 -0
  100. package/lib/module/lib/event-emitter.js.map +1 -0
  101. package/lib/module/lib/index.js +6 -0
  102. package/lib/module/lib/index.js.map +1 -0
  103. package/lib/module/lib/kwik-id-rn-sdk.js +150 -0
  104. package/lib/module/lib/kwik-id-rn-sdk.js.map +1 -0
  105. package/lib/module/lib/verification-machine.js +268 -0
  106. package/lib/module/lib/verification-machine.js.map +1 -0
  107. package/lib/module/package.json +1 -0
  108. package/lib/module/types/events.js +4 -0
  109. package/lib/module/types/events.js.map +1 -0
  110. package/lib/module/types/index.js +2 -0
  111. package/lib/module/types/index.js.map +1 -0
  112. package/lib/module/ui/components/KwikIDVerificationModal.js +402 -0
  113. package/lib/module/ui/components/KwikIDVerificationModal.js.map +1 -0
  114. package/lib/module/ui/components/screens.js +328 -0
  115. package/lib/module/ui/components/screens.js.map +1 -0
  116. package/lib/module/ui/context/kwik-id-provider.js +46 -0
  117. package/lib/module/ui/context/kwik-id-provider.js.map +1 -0
  118. package/lib/module/ui/hooks/use-kwik-verification.js +72 -0
  119. package/lib/module/ui/hooks/use-kwik-verification.js.map +1 -0
  120. package/lib/module/ui/index.js +6 -0
  121. package/lib/module/ui/index.js.map +1 -0
  122. package/lib/typescript/commonjs/api/api-client.d.ts +82 -0
  123. package/lib/typescript/commonjs/api/api-client.d.ts.map +1 -0
  124. package/lib/typescript/commonjs/api/api-error.d.ts +13 -0
  125. package/lib/typescript/commonjs/api/api-error.d.ts.map +1 -0
  126. package/lib/typescript/commonjs/api/index.d.ts +6 -0
  127. package/lib/typescript/commonjs/api/index.d.ts.map +1 -0
  128. package/lib/typescript/commonjs/api/upload.d.ts +19 -0
  129. package/lib/typescript/commonjs/api/upload.d.ts.map +1 -0
  130. package/lib/typescript/commonjs/camera/camera-view.d.ts +25 -0
  131. package/lib/typescript/commonjs/camera/camera-view.d.ts.map +1 -0
  132. package/lib/typescript/commonjs/camera/index.d.ts +5 -0
  133. package/lib/typescript/commonjs/camera/index.d.ts.map +1 -0
  134. package/lib/typescript/commonjs/camera/photo-capture.d.ts +9 -0
  135. package/lib/typescript/commonjs/camera/photo-capture.d.ts.map +1 -0
  136. package/lib/typescript/commonjs/crypto/fingerprint.d.ts +3 -0
  137. package/lib/typescript/commonjs/crypto/fingerprint.d.ts.map +1 -0
  138. package/lib/typescript/commonjs/crypto/hmac.d.ts +18 -0
  139. package/lib/typescript/commonjs/crypto/hmac.d.ts.map +1 -0
  140. package/lib/typescript/commonjs/crypto/index.d.ts +4 -0
  141. package/lib/typescript/commonjs/crypto/index.d.ts.map +1 -0
  142. package/lib/typescript/commonjs/crypto/session-manager.d.ts +27 -0
  143. package/lib/typescript/commonjs/crypto/session-manager.d.ts.map +1 -0
  144. package/lib/typescript/commonjs/i18n/en.d.ts +291 -0
  145. package/lib/typescript/commonjs/i18n/en.d.ts.map +1 -0
  146. package/lib/typescript/commonjs/i18n/fr.d.ts +6 -0
  147. package/lib/typescript/commonjs/i18n/fr.d.ts.map +1 -0
  148. package/lib/typescript/commonjs/i18n/index.d.ts +10 -0
  149. package/lib/typescript/commonjs/i18n/index.d.ts.map +1 -0
  150. package/lib/typescript/commonjs/i18n/locale.d.ts +4 -0
  151. package/lib/typescript/commonjs/i18n/locale.d.ts.map +1 -0
  152. package/lib/typescript/commonjs/i18n/translator.d.ts +15 -0
  153. package/lib/typescript/commonjs/i18n/translator.d.ts.map +1 -0
  154. package/lib/typescript/commonjs/index.d.ts +24 -0
  155. package/lib/typescript/commonjs/index.d.ts.map +1 -0
  156. package/lib/typescript/commonjs/lib/device-detector.d.ts +5 -0
  157. package/lib/typescript/commonjs/lib/device-detector.d.ts.map +1 -0
  158. package/lib/typescript/commonjs/lib/event-emitter.d.ts +19 -0
  159. package/lib/typescript/commonjs/lib/event-emitter.d.ts.map +1 -0
  160. package/lib/typescript/commonjs/lib/index.d.ts +5 -0
  161. package/lib/typescript/commonjs/lib/index.d.ts.map +1 -0
  162. package/lib/typescript/commonjs/lib/kwik-id-rn-sdk.d.ts +43 -0
  163. package/lib/typescript/commonjs/lib/kwik-id-rn-sdk.d.ts.map +1 -0
  164. package/lib/typescript/commonjs/lib/verification-machine.d.ts +78 -0
  165. package/lib/typescript/commonjs/lib/verification-machine.d.ts.map +1 -0
  166. package/lib/typescript/commonjs/package.json +1 -0
  167. package/lib/typescript/commonjs/types/events.d.ts +94 -0
  168. package/lib/typescript/commonjs/types/events.d.ts.map +1 -0
  169. package/lib/typescript/commonjs/types/index.d.ts +242 -0
  170. package/lib/typescript/commonjs/types/index.d.ts.map +1 -0
  171. package/lib/typescript/commonjs/ui/components/KwikIDVerificationModal.d.ts +21 -0
  172. package/lib/typescript/commonjs/ui/components/KwikIDVerificationModal.d.ts.map +1 -0
  173. package/lib/typescript/commonjs/ui/components/screens.d.ts +46 -0
  174. package/lib/typescript/commonjs/ui/components/screens.d.ts.map +1 -0
  175. package/lib/typescript/commonjs/ui/context/kwik-id-provider.d.ts +19 -0
  176. package/lib/typescript/commonjs/ui/context/kwik-id-provider.d.ts.map +1 -0
  177. package/lib/typescript/commonjs/ui/hooks/use-kwik-verification.d.ts +29 -0
  178. package/lib/typescript/commonjs/ui/hooks/use-kwik-verification.d.ts.map +1 -0
  179. package/lib/typescript/commonjs/ui/index.d.ts +7 -0
  180. package/lib/typescript/commonjs/ui/index.d.ts.map +1 -0
  181. package/lib/typescript/module/api/api-client.d.ts +82 -0
  182. package/lib/typescript/module/api/api-client.d.ts.map +1 -0
  183. package/lib/typescript/module/api/api-error.d.ts +13 -0
  184. package/lib/typescript/module/api/api-error.d.ts.map +1 -0
  185. package/lib/typescript/module/api/index.d.ts +6 -0
  186. package/lib/typescript/module/api/index.d.ts.map +1 -0
  187. package/lib/typescript/module/api/upload.d.ts +19 -0
  188. package/lib/typescript/module/api/upload.d.ts.map +1 -0
  189. package/lib/typescript/module/camera/camera-view.d.ts +25 -0
  190. package/lib/typescript/module/camera/camera-view.d.ts.map +1 -0
  191. package/lib/typescript/module/camera/index.d.ts +5 -0
  192. package/lib/typescript/module/camera/index.d.ts.map +1 -0
  193. package/lib/typescript/module/camera/photo-capture.d.ts +9 -0
  194. package/lib/typescript/module/camera/photo-capture.d.ts.map +1 -0
  195. package/lib/typescript/module/crypto/fingerprint.d.ts +3 -0
  196. package/lib/typescript/module/crypto/fingerprint.d.ts.map +1 -0
  197. package/lib/typescript/module/crypto/hmac.d.ts +18 -0
  198. package/lib/typescript/module/crypto/hmac.d.ts.map +1 -0
  199. package/lib/typescript/module/crypto/index.d.ts +4 -0
  200. package/lib/typescript/module/crypto/index.d.ts.map +1 -0
  201. package/lib/typescript/module/crypto/session-manager.d.ts +27 -0
  202. package/lib/typescript/module/crypto/session-manager.d.ts.map +1 -0
  203. package/lib/typescript/module/i18n/en.d.ts +291 -0
  204. package/lib/typescript/module/i18n/en.d.ts.map +1 -0
  205. package/lib/typescript/module/i18n/fr.d.ts +6 -0
  206. package/lib/typescript/module/i18n/fr.d.ts.map +1 -0
  207. package/lib/typescript/module/i18n/index.d.ts +10 -0
  208. package/lib/typescript/module/i18n/index.d.ts.map +1 -0
  209. package/lib/typescript/module/i18n/locale.d.ts +4 -0
  210. package/lib/typescript/module/i18n/locale.d.ts.map +1 -0
  211. package/lib/typescript/module/i18n/translator.d.ts +15 -0
  212. package/lib/typescript/module/i18n/translator.d.ts.map +1 -0
  213. package/lib/typescript/module/index.d.ts +24 -0
  214. package/lib/typescript/module/index.d.ts.map +1 -0
  215. package/lib/typescript/module/lib/device-detector.d.ts +5 -0
  216. package/lib/typescript/module/lib/device-detector.d.ts.map +1 -0
  217. package/lib/typescript/module/lib/event-emitter.d.ts +19 -0
  218. package/lib/typescript/module/lib/event-emitter.d.ts.map +1 -0
  219. package/lib/typescript/module/lib/index.d.ts +5 -0
  220. package/lib/typescript/module/lib/index.d.ts.map +1 -0
  221. package/lib/typescript/module/lib/kwik-id-rn-sdk.d.ts +43 -0
  222. package/lib/typescript/module/lib/kwik-id-rn-sdk.d.ts.map +1 -0
  223. package/lib/typescript/module/lib/verification-machine.d.ts +78 -0
  224. package/lib/typescript/module/lib/verification-machine.d.ts.map +1 -0
  225. package/lib/typescript/module/package.json +1 -0
  226. package/lib/typescript/module/types/events.d.ts +94 -0
  227. package/lib/typescript/module/types/events.d.ts.map +1 -0
  228. package/lib/typescript/module/types/index.d.ts +242 -0
  229. package/lib/typescript/module/types/index.d.ts.map +1 -0
  230. package/lib/typescript/module/ui/components/KwikIDVerificationModal.d.ts +21 -0
  231. package/lib/typescript/module/ui/components/KwikIDVerificationModal.d.ts.map +1 -0
  232. package/lib/typescript/module/ui/components/screens.d.ts +46 -0
  233. package/lib/typescript/module/ui/components/screens.d.ts.map +1 -0
  234. package/lib/typescript/module/ui/context/kwik-id-provider.d.ts +19 -0
  235. package/lib/typescript/module/ui/context/kwik-id-provider.d.ts.map +1 -0
  236. package/lib/typescript/module/ui/hooks/use-kwik-verification.d.ts +29 -0
  237. package/lib/typescript/module/ui/hooks/use-kwik-verification.d.ts.map +1 -0
  238. package/lib/typescript/module/ui/index.d.ts +7 -0
  239. package/lib/typescript/module/ui/index.d.ts.map +1 -0
  240. package/package.json +121 -3
  241. package/src/api/api-client.ts +319 -0
  242. package/src/api/api-error.ts +28 -0
  243. package/src/api/index.ts +12 -0
  244. package/src/api/upload.ts +87 -0
  245. package/src/camera/camera-view.tsx +217 -0
  246. package/src/camera/index.ts +4 -0
  247. package/src/camera/photo-capture.ts +58 -0
  248. package/src/crypto/fingerprint.ts +46 -0
  249. package/src/crypto/hmac.ts +172 -0
  250. package/src/crypto/index.ts +3 -0
  251. package/src/crypto/session-manager.ts +200 -0
  252. package/src/i18n/en.ts +315 -0
  253. package/src/i18n/fr.ts +325 -0
  254. package/src/i18n/index.ts +9 -0
  255. package/src/i18n/locale.ts +15 -0
  256. package/src/i18n/translator.ts +37 -0
  257. package/src/index.ts +102 -0
  258. package/src/lib/device-detector.ts +32 -0
  259. package/src/lib/event-emitter.ts +90 -0
  260. package/src/lib/index.ts +15 -0
  261. package/src/lib/kwik-id-rn-sdk.ts +160 -0
  262. package/src/lib/verification-machine.ts +273 -0
  263. package/src/types/events.ts +75 -0
  264. package/src/types/index.ts +309 -0
  265. package/src/ui/components/KwikIDVerificationModal.tsx +450 -0
  266. package/src/ui/components/screens.tsx +283 -0
  267. package/src/ui/context/kwik-id-provider.tsx +72 -0
  268. package/src/ui/hooks/use-kwik-verification.ts +121 -0
  269. package/src/ui/index.ts +9 -0
@@ -0,0 +1,160 @@
1
+ /**
2
+ * Headless orchestrator for the RN SDK.
3
+ *
4
+ * Wraps SessionManager + ApiClient + EventEmitter so the UI (and advanced
5
+ * integrators) drive a single object. No camera/UI concerns live here.
6
+ */
7
+ import { ApiClient, type StatusResponse } from "../api/api-client";
8
+ import { SessionManager } from "../crypto/session-manager";
9
+ import type {
10
+ CaptureAsset,
11
+ CaptureType,
12
+ DocumentType,
13
+ KwikIdConfig,
14
+ KwikIdEventHandler,
15
+ KwikIdEventName,
16
+ LivenessResult,
17
+ QualityResult,
18
+ SessionInfo,
19
+ } from "../types";
20
+ import { assertSupportedRuntime } from "./device-detector";
21
+ import { EventEmitter } from "./event-emitter";
22
+
23
+ export interface KwikIdRNSDKOptions extends KwikIdConfig {
24
+ /** Skip the Expo Go runtime guard (tests only). */
25
+ skipRuntimeCheck?: boolean;
26
+ }
27
+
28
+ export class KwikIdRNSDK {
29
+ private readonly events = new EventEmitter();
30
+ private readonly sessionManager: SessionManager;
31
+ private readonly api: ApiClient;
32
+ private readonly options: KwikIdRNSDKOptions;
33
+ private sessionInfo: SessionInfo | null = null;
34
+ private initialized = false;
35
+
36
+ constructor(options: KwikIdRNSDKOptions) {
37
+ this.options = options;
38
+ this.sessionManager = new SessionManager();
39
+ this.api = new ApiClient(this.sessionManager, {
40
+ baseUrl: options.baseUrl,
41
+ timeout: options.timeout,
42
+ debug: options.debug,
43
+ });
44
+ }
45
+
46
+ /** Validate the runtime + session token. Returns the org/session config. */
47
+ async initialize(): Promise<SessionInfo> {
48
+ if (!this.options.skipRuntimeCheck) {
49
+ assertSupportedRuntime();
50
+ }
51
+ try {
52
+ this.sessionInfo = await this.api.validateSession(this.options.clientSecret);
53
+ this.initialized = true;
54
+ const session = this.sessionManager.getSession();
55
+ if (session) this.events.emit("session:created", session);
56
+ this.events.emit("ready", undefined);
57
+ return this.sessionInfo;
58
+ } catch (error) {
59
+ this.events.emit("error", {
60
+ code: "INIT_FAILED",
61
+ message: (error as Error).message,
62
+ recoverable: false,
63
+ });
64
+ throw error;
65
+ }
66
+ }
67
+
68
+ /** Redeem a desktop→mobile handoff code, swapping in the mobile session token. */
69
+ async redeemHandoff(code: string): Promise<void> {
70
+ try {
71
+ const { sessionToken } = await this.api.redeemHandoff(code);
72
+ this.sessionInfo = await this.api.validateSession(sessionToken);
73
+ this.initialized = true;
74
+ const session = this.sessionManager.getSession();
75
+ this.events.emit("handoff:redeemed", { jobId: session?.jobId ?? "" });
76
+ } catch (error) {
77
+ this.events.emit("handoff:error", { error: error as Error });
78
+ throw error;
79
+ }
80
+ }
81
+
82
+ async uploadCapture(
83
+ type: CaptureType,
84
+ asset: CaptureAsset,
85
+ onProgress?: (progress: number) => void
86
+ ): Promise<{ uploadId: string }> {
87
+ this.assertReady();
88
+ this.events.emit("upload:starting", { type });
89
+ try {
90
+ const res = await this.api.uploadCapture(type, asset, (p) => {
91
+ this.events.emit("upload:progress", { type, progress: p });
92
+ onProgress?.(p);
93
+ });
94
+ this.events.emit("upload:complete", { type, uploadId: res.uploadId });
95
+ return { uploadId: res.uploadId };
96
+ } catch (error) {
97
+ const retryable =
98
+ typeof (error as { isRetryable?: () => boolean }).isRetryable === "function"
99
+ ? (error as { isRetryable: () => boolean }).isRetryable()
100
+ : false;
101
+ this.events.emit("upload:failed", { type, error: error as Error, retryable });
102
+ throw error;
103
+ }
104
+ }
105
+
106
+ async submit(
107
+ documentType: DocumentType,
108
+ analysis?: { quality?: QualityResult; liveness?: LivenessResult }
109
+ ): Promise<{ jobId: string }> {
110
+ this.assertReady();
111
+ this.events.emit("verification:submitting", undefined);
112
+ try {
113
+ const res = await this.api.submitVerification(documentType, {
114
+ qualityScores: analysis?.quality,
115
+ livenessResult: analysis?.liveness,
116
+ });
117
+ const jobId = res.jobId ?? this.sessionManager.getSession()?.jobId ?? "";
118
+ this.events.emit("verification:submitted", { jobId });
119
+ return { jobId };
120
+ } catch (error) {
121
+ this.events.emit("verification:failed", { error: error as Error });
122
+ throw error;
123
+ }
124
+ }
125
+
126
+ getStatus(): Promise<StatusResponse> {
127
+ this.assertReady();
128
+ return this.api.getStatus();
129
+ }
130
+
131
+ getSessionInfo(): SessionInfo | null {
132
+ return this.sessionInfo;
133
+ }
134
+
135
+ /** True when the session token is within ~2 min of expiry. */
136
+ needsRefresh(): boolean {
137
+ return this.sessionManager.needsRefresh();
138
+ }
139
+
140
+ on<E extends KwikIdEventName>(event: E, handler: KwikIdEventHandler<E>): () => void {
141
+ return this.events.on(event, handler);
142
+ }
143
+
144
+ reset(): void {
145
+ this.sessionManager.clearSession();
146
+ this.sessionInfo = null;
147
+ this.initialized = false;
148
+ }
149
+
150
+ destroy(): void {
151
+ this.reset();
152
+ this.events.removeAllListeners();
153
+ }
154
+
155
+ private assertReady(): void {
156
+ if (!this.initialized) {
157
+ throw new Error("KwikIdRNSDK not initialized — call initialize() first");
158
+ }
159
+ }
160
+ }
@@ -0,0 +1,273 @@
1
+ /**
2
+ * Pure reducer for the RN verification flow.
3
+ *
4
+ * Adapted from the web SDK's KYCModalFlow SubStep machine. No React / no native
5
+ * dependency, so it is fully unit-testable. The selfie branch folds the web's
6
+ * `camera-selfie` into a single `liveness` step (align → passive/active → capture).
7
+ */
8
+ import type {
9
+ CaptureAsset,
10
+ CaptureSlot,
11
+ CaptureType,
12
+ DocumentType,
13
+ LivenessResult,
14
+ VerificationOutcome,
15
+ VerificationSubStep,
16
+ } from "../types";
17
+
18
+ export interface MachineContext {
19
+ documentType: DocumentType | null;
20
+ front?: CaptureAsset;
21
+ back?: CaptureAsset;
22
+ selfie?: CaptureAsset;
23
+ livenessResult?: LivenessResult;
24
+ /** 0..100 upload/submit progress shown on the processing screen. */
25
+ uploadProgress: number;
26
+ uploadStage: CaptureType | "submitting" | null;
27
+ outcome?: VerificationOutcome;
28
+ error?: string;
29
+ }
30
+
31
+ export interface MachineState {
32
+ step: VerificationSubStep;
33
+ context: MachineContext;
34
+ }
35
+
36
+ export interface MachineOptions {
37
+ /** When launched as the mobile redeemer of a desktop handoff. */
38
+ hasHandoffCode?: boolean;
39
+ }
40
+
41
+ export type MachineEvent =
42
+ | { t: "NEXT" }
43
+ | { t: "BACK" }
44
+ | { t: "SELECT_DOC"; doc: DocumentType }
45
+ | { t: "CAPTURED"; slot: CaptureSlot; asset: CaptureAsset }
46
+ | { t: "RETAKE"; slot: CaptureSlot }
47
+ | { t: "HANDOFF_REDEEMED" }
48
+ /** `result` is omitted until on-device liveness ships (Phase 2). */
49
+ | { t: "LIVENESS_DONE"; result?: LivenessResult; selfie: CaptureAsset }
50
+ | { t: "PROGRESS"; stage: MachineContext["uploadStage"]; value: number }
51
+ | { t: "SUBMITTED"; jobId: string }
52
+ | { t: "CANCEL" }
53
+ | { t: "FAILED"; error: string }
54
+ /** Back to a fresh intro, e.g. when the modal closes so the next run starts clean. */
55
+ | { t: "RESET" };
56
+
57
+ export function needsBackImage(doc: DocumentType | null): boolean {
58
+ return doc === "id_card" || doc === "drivers_license" || doc === "voters_card";
59
+ }
60
+
61
+ export function initialState(): MachineState {
62
+ return {
63
+ step: "intro",
64
+ context: {
65
+ documentType: null,
66
+ uploadProgress: 0,
67
+ uploadStage: null,
68
+ },
69
+ };
70
+ }
71
+
72
+ /** Steps that contribute to the progress bar, in order (mirrors web getProgress). */
73
+ function progressSteps(ctx: MachineContext): VerificationSubStep[] {
74
+ const steps: VerificationSubStep[] = [
75
+ "document-type",
76
+ "prepare-front",
77
+ "camera-front",
78
+ "confirm-front",
79
+ ];
80
+ if (needsBackImage(ctx.documentType)) {
81
+ steps.push("prepare-back", "camera-back", "confirm-back");
82
+ }
83
+ steps.push("prepare-selfie", "liveness", "confirm-selfie");
84
+ return steps;
85
+ }
86
+
87
+ /** Progress-bar percentage (0..100) for the current step. */
88
+ export function getStepProgress(state: MachineState): number {
89
+ const hidden: VerificationSubStep[] = ["intro", "handoff-redeem", "processing", "result"];
90
+ if (hidden.includes(state.step)) return 0;
91
+ const steps = progressSteps(state.context);
92
+ const idx = steps.indexOf(state.step);
93
+ if (idx < 0) return 0;
94
+ return ((idx + 1) / steps.length) * 100;
95
+ }
96
+
97
+ /**
98
+ * Upload/submit progress banding (matches web performVerification):
99
+ * front 0–30, back 30–45, selfie 45–70 (or 30–55 without back), submit 80, done 100.
100
+ */
101
+ export function computeUploadProgress(
102
+ stage: MachineContext["uploadStage"],
103
+ pct: number,
104
+ hasBack: boolean
105
+ ): number {
106
+ switch (stage) {
107
+ case "document_front":
108
+ return Math.round(pct * 0.3);
109
+ case "document_back":
110
+ return 30 + Math.round(pct * 0.15);
111
+ case "selfie": {
112
+ const base = hasBack ? 45 : 30;
113
+ return base + Math.round(pct * 0.25);
114
+ }
115
+ case "submitting":
116
+ return Math.max(80, Math.round(pct));
117
+ default:
118
+ return 0;
119
+ }
120
+ }
121
+
122
+ const FORWARD_TAP: Partial<Record<VerificationSubStep, VerificationSubStep>> = {
123
+ "prepare-front": "camera-front",
124
+ "prepare-back": "camera-back",
125
+ "prepare-selfie": "liveness",
126
+ // 'confirm-front' is resolved dynamically (needs back?) in the reducer.
127
+ "confirm-back": "prepare-selfie",
128
+ "confirm-selfie": "processing",
129
+ };
130
+
131
+ const BACK_FLOW: Partial<Record<VerificationSubStep, VerificationSubStep>> = {
132
+ "document-type": "intro",
133
+ "prepare-front": "document-type",
134
+ "camera-front": "prepare-front",
135
+ "confirm-front": "camera-front",
136
+ "prepare-back": "confirm-front",
137
+ "camera-back": "prepare-back",
138
+ "confirm-back": "camera-back",
139
+ "confirm-selfie": "prepare-selfie",
140
+ };
141
+
142
+ export function reducer(
143
+ state: MachineState,
144
+ event: MachineEvent,
145
+ options: MachineOptions = {}
146
+ ): MachineState {
147
+ const { step, context } = state;
148
+
149
+ switch (event.t) {
150
+ case "NEXT": {
151
+ if (step === "intro") {
152
+ return {
153
+ ...state,
154
+ step: options.hasHandoffCode ? "handoff-redeem" : "document-type",
155
+ };
156
+ }
157
+ if (step === "confirm-front") {
158
+ return {
159
+ ...state,
160
+ step: needsBackImage(context.documentType) ? "prepare-back" : "prepare-selfie",
161
+ };
162
+ }
163
+ const next = FORWARD_TAP[step];
164
+ return next ? { ...state, step: next } : state;
165
+ }
166
+
167
+ case "BACK": {
168
+ // Terminal / non-reversible steps.
169
+ if (step === "processing" || step === "result" || step === "liveness") return state;
170
+ if (step === "prepare-selfie") {
171
+ return {
172
+ ...state,
173
+ step: needsBackImage(context.documentType) ? "confirm-back" : "confirm-front",
174
+ };
175
+ }
176
+ const prev = BACK_FLOW[step];
177
+ return prev ? { ...state, step: prev } : state;
178
+ }
179
+
180
+ case "SELECT_DOC": {
181
+ if (step !== "document-type") return state;
182
+ return {
183
+ step: "prepare-front",
184
+ context: { ...context, documentType: event.doc },
185
+ };
186
+ }
187
+
188
+ case "HANDOFF_REDEEMED": {
189
+ if (step !== "handoff-redeem") return state;
190
+ return { ...state, step: "document-type" };
191
+ }
192
+
193
+ case "CAPTURED": {
194
+ const ctx = { ...context, [event.slot]: event.asset };
195
+ if (event.slot === "front" && step === "camera-front") {
196
+ return { step: "confirm-front", context: ctx };
197
+ }
198
+ if (event.slot === "back" && step === "camera-back") {
199
+ return { step: "confirm-back", context: ctx };
200
+ }
201
+ return { ...state, context: ctx };
202
+ }
203
+
204
+ case "RETAKE": {
205
+ if (event.slot === "front") {
206
+ return { step: "camera-front", context: { ...context, front: undefined } };
207
+ }
208
+ if (event.slot === "back") {
209
+ return { step: "camera-back", context: { ...context, back: undefined } };
210
+ }
211
+ return {
212
+ step: "liveness",
213
+ context: { ...context, selfie: undefined, livenessResult: undefined },
214
+ };
215
+ }
216
+
217
+ case "LIVENESS_DONE": {
218
+ if (step !== "liveness") return state;
219
+ return {
220
+ step: "confirm-selfie",
221
+ context: { ...context, selfie: event.selfie, livenessResult: event.result },
222
+ };
223
+ }
224
+
225
+ case "PROGRESS": {
226
+ const hasBack = !!context.back;
227
+ return {
228
+ ...state,
229
+ context: {
230
+ ...context,
231
+ uploadStage: event.stage,
232
+ uploadProgress: computeUploadProgress(event.stage, event.value, hasBack),
233
+ },
234
+ };
235
+ }
236
+
237
+ case "SUBMITTED": {
238
+ return {
239
+ step: "result",
240
+ context: {
241
+ ...context,
242
+ uploadProgress: 100,
243
+ uploadStage: null,
244
+ outcome: { status: "submitted", jobId: event.jobId },
245
+ },
246
+ };
247
+ }
248
+
249
+ case "FAILED": {
250
+ return {
251
+ step: "result",
252
+ context: {
253
+ ...context,
254
+ error: event.error,
255
+ outcome: { status: "error", error: new Error(event.error) },
256
+ },
257
+ };
258
+ }
259
+
260
+ case "CANCEL": {
261
+ return {
262
+ ...state,
263
+ context: { ...context, outcome: { status: "cancelled" } },
264
+ };
265
+ }
266
+
267
+ case "RESET":
268
+ return initialState();
269
+
270
+ default:
271
+ return state;
272
+ }
273
+ }
@@ -0,0 +1,75 @@
1
+ /**
2
+ * Event vocabulary for @kwik-id/sdk-react-native.
3
+ *
4
+ * Mirrors the portable subset of the web SDK's KwikIdEvents — DOM/Blob/MediaStream
5
+ * payloads are replaced with RN-native equivalents (CaptureAsset, FaceSignals).
6
+ */
7
+ import type {
8
+ CaptureAsset,
9
+ CaptureType,
10
+ Challenge,
11
+ ChallengeResult,
12
+ FaceSignals,
13
+ LivenessMode,
14
+ LivenessResult,
15
+ PassiveLivenessResult,
16
+ Session,
17
+ VerificationResult,
18
+ } from "./index";
19
+
20
+ export interface KwikIdEvents {
21
+ // Session
22
+ "session:created": Session;
23
+ "session:expired": { sessionId: string };
24
+ "session:refreshed": Session;
25
+
26
+ // Camera
27
+ "camera:ready": void;
28
+ "camera:error": { error: Error; recoverable: boolean };
29
+ "camera:permission_denied": { canAskAgain: boolean };
30
+
31
+ // Face / alignment
32
+ "face:detected": FaceSignals;
33
+ "face:lost": void;
34
+ "face:aligned": void;
35
+ "face:misaligned": { reason: string };
36
+
37
+ // Liveness
38
+ "liveness:starting": { mode: LivenessMode };
39
+ "liveness:challenge": Challenge;
40
+ "liveness:challenge_result": ChallengeResult;
41
+ "liveness:passive_result": PassiveLivenessResult;
42
+ "liveness:complete": LivenessResult;
43
+ "liveness:failed": { reason: string };
44
+
45
+ // Capture
46
+ "capture:starting": { type: CaptureType };
47
+ "capture:complete": { type: CaptureType; asset: CaptureAsset };
48
+ "capture:failed": { type: CaptureType; error: Error };
49
+
50
+ // Upload
51
+ "upload:starting": { type: CaptureType };
52
+ "upload:progress": { type: CaptureType; progress: number };
53
+ "upload:complete": { type: CaptureType; uploadId: string };
54
+ "upload:failed": { type: CaptureType; error: Error; retryable: boolean };
55
+
56
+ // Verification
57
+ "verification:submitting": void;
58
+ "verification:submitted": { jobId: string };
59
+ "verification:processing": { jobId: string; progress?: number };
60
+ "verification:complete": VerificationResult;
61
+ "verification:failed": { error: Error; jobId?: string };
62
+
63
+ // Handoff (mobile is the redeemer)
64
+ "handoff:redeemed": { jobId: string };
65
+ "handoff:error": { error: Error };
66
+
67
+ // General
68
+ ready: void;
69
+ error: { code: string; message: string; recoverable: boolean };
70
+ destroy: void;
71
+ }
72
+
73
+ export type KwikIdEventName = keyof KwikIdEvents;
74
+
75
+ export type KwikIdEventHandler<T extends KwikIdEventName> = (data: KwikIdEvents[T]) => void;