react-native-shield-full-plugin 2.3.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 (30) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +237 -0
  3. package/android/build.gradle +109 -0
  4. package/android/gradle.properties +5 -0
  5. package/android/src/main/AndroidManifest.xml +5 -0
  6. package/android/src/main/java/com/shieldfraudplugin/ShieldFraudPluginModule.java +441 -0
  7. package/android/src/main/java/com/shieldfraudplugin/ShieldFraudPluginPackage.java +78 -0
  8. package/android/src/newarch/java/com/shieldfraudplugin/ShieldFraudPluginSpec.java +41 -0
  9. package/android/src/oldarch/java/com/shieldfraudplugin/ShieldFraudPluginSpec.java +36 -0
  10. package/ios/ShieldFraudPlugin.h +24 -0
  11. package/ios/ShieldFraudPlugin.mm +295 -0
  12. package/ios/ShieldFraudPlugin.xcodeproj/project.pbxproj +282 -0
  13. package/ios/ShieldFraudPlugin.xcodeproj/project.xcworkspace/contents.xcworkspacedata +4 -0
  14. package/lib/commonjs/NativeShieldFraudPlugin.js +15 -0
  15. package/lib/commonjs/NativeShieldFraudPlugin.js.map +1 -0
  16. package/lib/commonjs/index.js +256 -0
  17. package/lib/commonjs/index.js.map +1 -0
  18. package/lib/module/NativeShieldFraudPlugin.js +10 -0
  19. package/lib/module/NativeShieldFraudPlugin.js.map +1 -0
  20. package/lib/module/index.js +253 -0
  21. package/lib/module/index.js.map +1 -0
  22. package/lib/typescript/src/NativeShieldFraudPlugin.d.ts +53 -0
  23. package/lib/typescript/src/NativeShieldFraudPlugin.d.ts.map +1 -0
  24. package/lib/typescript/src/index.d.ts +143 -0
  25. package/lib/typescript/src/index.d.ts.map +1 -0
  26. package/package.json +169 -0
  27. package/react-native-shield-fraud-plugin.podspec +29 -0
  28. package/react-native-shield-full-plugin.podspec +29 -0
  29. package/src/NativeShieldFraudPlugin.ts +87 -0
  30. package/src/index.tsx +334 -0
package/src/index.tsx ADDED
@@ -0,0 +1,334 @@
1
+ import {
2
+ NativeModules,
3
+ NativeEventEmitter,
4
+ Platform,
5
+ TurboModuleRegistry,
6
+ } from 'react-native';
7
+ import packageJson from '../package.json';
8
+
9
+ /**
10
+ * Resolve the native module through TurboModuleRegistry (New Architecture)
11
+ * with a fallback to NativeModules bridge (Old Architecture).
12
+ */
13
+ const ShieldFraudPluginNativeModule =
14
+ TurboModuleRegistry.get<any>('ShieldFraudPlugin') ??
15
+ NativeModules.ShieldFraudPlugin;
16
+
17
+ /**
18
+ * Enum representing the log levels for ShieldFraud.
19
+ */
20
+ export enum LogLevel {
21
+ LogLevelVerbose = 4,
22
+ LogLevelDebug = 3,
23
+ LogLevelInfo = 2,
24
+ LogLevelNone = 1,
25
+ }
26
+
27
+ /**
28
+ * Enum representing the environment information for ShieldFraud.
29
+ */
30
+ export enum EnvironmentInfo {
31
+ EnvironmentProd = 0, // Environment.PROD
32
+ EnvironmentDev = 1, // Environment.DEV
33
+ EnvironmentStaging = 2, // Environment.STAGING
34
+ }
35
+
36
+ /**
37
+ * Configuration object for initializing ShieldFraud.
38
+ */
39
+ export interface Config {
40
+ siteID: string;
41
+ secretKey: string;
42
+ blockedDialog?: {
43
+ title: string;
44
+ body: string;
45
+ } | null;
46
+ logLevel?: LogLevel;
47
+ environmentInfo?: EnvironmentInfo;
48
+ /**
49
+ * Android-only: when true, the Shield SDK will block screen recording
50
+ * while it is active. Has no effect on iOS (the value is forwarded but
51
+ * the native side ignores it). Defaults to false when not provided.
52
+ */
53
+ blockScreenRecording?: boolean;
54
+ }
55
+
56
+ /**
57
+ * Type definition for the success callback function.
58
+ */
59
+ type SuccessCallback = (data: any) => void;
60
+
61
+ /**
62
+ * Type definition for the failure callback function.
63
+ */
64
+ type FailureCallback = (error: any) => void;
65
+
66
+ /**
67
+ * Type definition for the ShieldFraud callback functions.
68
+ */
69
+ export type ShieldCallback = {
70
+ onSuccess?: SuccessCallback;
71
+ onFailure?: FailureCallback;
72
+ };
73
+
74
+ /**
75
+ * The `ShieldFraud` class is a wrapper for the ShieldFraudPlugin native module in React Native.
76
+ * It provides methods for initializing ShieldFraud, retrieving session ID, checking the SDK readiness,
77
+ * sending attributes, and getting the latest device result.
78
+ */
79
+ class ShieldFraud {
80
+ /**
81
+ * The native module for accessing the ShieldFraudPlugin.
82
+ * Resolved via TurboModuleRegistry on New Architecture, with
83
+ * a NativeModules bridge fallback for Old Architecture.
84
+ */
85
+ private static PlatformWrapper = ShieldFraudPluginNativeModule;
86
+
87
+ /**
88
+ * The event emitter for listening to success and error events.
89
+ */
90
+ private static eventEmitter = new NativeEventEmitter(
91
+ ShieldFraud.PlatformWrapper
92
+ );
93
+
94
+ private static callbackSubscriptions: {
95
+ success?: { remove: () => void };
96
+ error?: { remove: () => void };
97
+ } = {};
98
+
99
+ /**
100
+ * Initializes the ShieldFraud plugin with the provided configuration.
101
+ *
102
+ * @param config - The configuration object containing the required properties.
103
+ * @param callbacks - (Optional) The callback functions for success and failure events.
104
+ */
105
+ public static async initShield(
106
+ config: Config,
107
+ callbacks?: ShieldCallback
108
+ ): Promise<void> {
109
+ const isOptimizedListener = !!callbacks;
110
+
111
+ // Set default values if logLevel is not provided
112
+ const logLevel = config.logLevel || LogLevel.LogLevelNone;
113
+
114
+ // Set default values if environmentInfo is not provided
115
+ const environmentInfo =
116
+ config.environmentInfo || EnvironmentInfo.EnvironmentProd;
117
+
118
+ // Set cross-platform parameters internally (React Native and version from package.json)
119
+ ShieldFraud.setCrossPlatformParameters();
120
+
121
+ // blockScreenRecording is Android-only; iOS native ignores the value.
122
+ // Always send a real boolean — the Old Arch bridge cannot handle null
123
+ // for boolean parameters (causes NPE in ReadableNativeArray.getBoolean).
124
+ const blockScreenRecording =
125
+ Platform.OS === 'android' ? config.blockScreenRecording ?? false : false;
126
+
127
+ // Call the native method to initialize ShieldFraud with the provided configuration.
128
+ await ShieldFraud.PlatformWrapper.initShield(
129
+ config.siteID,
130
+ config.secretKey,
131
+ isOptimizedListener,
132
+ config.blockedDialog ?? null,
133
+ logLevel,
134
+ environmentInfo,
135
+ blockScreenRecording
136
+ );
137
+
138
+ if (isOptimizedListener) {
139
+ // Set up listeners for success and error events if callbacks are provided.
140
+ ShieldFraud.listeners(callbacks);
141
+ }
142
+ }
143
+
144
+ /**
145
+ * Private method to set cross-platform parameters.
146
+ * The cross-platform name and the version is fetched from package.json.
147
+ */
148
+ private static setCrossPlatformParameters(): void {
149
+ const crossPlatformName = packageJson.name;
150
+ const crossPlatformVersion = packageJson.version;
151
+
152
+ ShieldFraud.PlatformWrapper.setCrossPlatformParameters(
153
+ crossPlatformName,
154
+ crossPlatformVersion
155
+ );
156
+ }
157
+
158
+ /**
159
+ * Sets up event listeners for success and error events.
160
+ *
161
+ * @param callbacks - The callback functions for success and failure events.
162
+ */
163
+ private static listeners(callbacks?: ShieldCallback): void {
164
+ ShieldFraud.callbackSubscriptions.success?.remove();
165
+ ShieldFraud.callbackSubscriptions.error?.remove();
166
+
167
+ // Listen for success events and invoke the onSuccess callback if provided.
168
+ ShieldFraud.callbackSubscriptions.success =
169
+ ShieldFraud.eventEmitter.addListener('success', (data) => {
170
+ if (callbacks?.onSuccess) {
171
+ callbacks.onSuccess(data);
172
+ }
173
+ });
174
+
175
+ // Listen for error events and invoke the onFailure callback if provided.
176
+ ShieldFraud.callbackSubscriptions.error =
177
+ ShieldFraud.eventEmitter.addListener('error', (error) => {
178
+ if (callbacks?.onFailure) {
179
+ callbacks.onFailure(error);
180
+ }
181
+ });
182
+ }
183
+
184
+ /**
185
+ * Retrieves the session ID from the ShieldFraud plugin.
186
+ *
187
+ * @returns The current session ID.
188
+ */
189
+ public static getSessionId(): string {
190
+ return ShieldFraud.PlatformWrapper.getSessionId();
191
+ }
192
+
193
+ /**
194
+ * Checks whether the ShieldFraud plugin is initialized.
195
+ *
196
+ * @returns Whether the ShieldFraud plugin is initialized.
197
+ */
198
+ public static isShieldInitialized(): boolean {
199
+ return ShieldFraud.PlatformWrapper.isShieldInitialized();
200
+ }
201
+
202
+ /**
203
+ * Checks if the ShieldFraud SDK is ready and invokes the provided callback
204
+ * with the readiness state.
205
+ *
206
+ * On Android, readiness is approximated by the native initialization state
207
+ * to preserve backward compatibility without reporting ready before init.
208
+ *
209
+ * @param callback - A callback function to be invoked with the readiness state.
210
+ * - `isReady` (boolean): true when the SDK has produced a device result.
211
+ */
212
+ public static async isSDKready(
213
+ callback: (isReady: boolean) => void
214
+ ): Promise<void> {
215
+ if (Platform.OS === 'android') {
216
+ const isInitialized = await this.isShieldInitialized();
217
+ callback(isInitialized);
218
+ return;
219
+ }
220
+
221
+ try {
222
+ // Adding a timeout of 100 milliseconds
223
+ await new Promise((resolve) => setTimeout(resolve, 100));
224
+
225
+ const isInitialized = await this.isShieldInitialized();
226
+
227
+ if (!isInitialized) {
228
+ console.log('Shield SDK not initialized:');
229
+ callback(false);
230
+ return;
231
+ }
232
+
233
+ const deviceResultListener = (event: { status: string }) => {
234
+ if (event.status === 'isSDKReady') {
235
+ ShieldFraud.eventEmitter.removeAllListeners('device_result_state');
236
+ callback(true);
237
+ }
238
+ };
239
+
240
+ ShieldFraud.eventEmitter.addListener(
241
+ 'device_result_state',
242
+ deviceResultListener
243
+ );
244
+
245
+ // Trigger the SDK subscription — iOS 1.x requires this explicit call.
246
+ ShieldFraud.PlatformWrapper.setDeviceResultStateListener();
247
+ } catch (error) {
248
+ console.error('An error occurred:', error);
249
+ callback(false);
250
+ }
251
+ }
252
+
253
+ /**
254
+ * Sends attributes to the ShieldFraud plugin for a specific screen.
255
+ *
256
+ * @param screenName - The name of the screen.
257
+ * @param data - The attribute data object.
258
+ */
259
+ public static sendAttributes(screenName: string, data: object): void {
260
+ ShieldFraud.PlatformWrapper.sendAttributes(screenName, data);
261
+ }
262
+
263
+ /**
264
+ * Sends attributes and resolves with `true` when the native SDK confirms success.
265
+ *
266
+ * @param screenName - The name of the screen.
267
+ * @param data - The attribute data object.
268
+ * @returns A Promise that resolves to true on success and rejects on error.
269
+ */
270
+ public static sendAttributesWithCallback(
271
+ screenName: string,
272
+ data: object
273
+ ): Promise<boolean> {
274
+ return new Promise((resolve, reject) => {
275
+ ShieldFraud.PlatformWrapper.sendAttributesWithCallback(
276
+ screenName,
277
+ data,
278
+ (result: boolean) => {
279
+ resolve(!!result);
280
+ },
281
+ (error: object) => {
282
+ reject(error);
283
+ }
284
+ );
285
+ });
286
+ }
287
+
288
+ /**
289
+ * Retrieves the latest device result from the ShieldFraud plugin.
290
+ *
291
+ * @returns A Promise that resolves with the latest device result object.
292
+ */
293
+ public static getLatestDeviceResult(): Promise<object> {
294
+ return new Promise((resolve, reject) => {
295
+ ShieldFraud.PlatformWrapper.getLatestDeviceResult(
296
+ (result: object) => {
297
+ resolve(result);
298
+ },
299
+ (error: object) => {
300
+ reject(error);
301
+ }
302
+ );
303
+ });
304
+ }
305
+
306
+ /**
307
+ * Triggers a device signature computation for a given screen name.
308
+ * On success, resolves with the latest device result object.
309
+ * On failure, rejects with the error message.
310
+ *
311
+ * @param screenName - The name of the screen triggering the signature.
312
+ * @param userId - (Optional) A user ID to associate with the device signature.
313
+ * @returns A Promise that resolves with the device result or rejects with an error.
314
+ */
315
+ public static sendDeviceSignature(
316
+ screenName: string,
317
+ userId?: string
318
+ ): Promise<object> {
319
+ return new Promise((resolve, reject) => {
320
+ ShieldFraud.PlatformWrapper.sendDeviceSignature(
321
+ screenName,
322
+ userId ?? null,
323
+ (result: object) => {
324
+ resolve(result);
325
+ },
326
+ (error: string) => {
327
+ reject(error);
328
+ }
329
+ );
330
+ });
331
+ }
332
+ }
333
+
334
+ export default ShieldFraud;