@functionland/react-native-fula 0.4.2 → 1.0.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 (51) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +133 -100
  3. package/android/.gradle/7.5.1/checksums/checksums.lock +0 -0
  4. package/android/.gradle/7.5.1/checksums/md5-checksums.bin +0 -0
  5. package/android/.gradle/7.5.1/checksums/sha1-checksums.bin +0 -0
  6. package/android/.gradle/7.5.1/dependencies-accessors/dependencies-accessors.lock +0 -0
  7. package/android/.gradle/7.5.1/dependencies-accessors/gc.properties +0 -0
  8. package/android/.gradle/7.5.1/executionHistory/executionHistory.lock +0 -0
  9. package/android/.gradle/7.5.1/fileChanges/last-build.bin +0 -0
  10. package/android/.gradle/7.5.1/fileHashes/fileHashes.bin +0 -0
  11. package/android/.gradle/7.5.1/fileHashes/fileHashes.lock +0 -0
  12. package/android/.gradle/7.5.1/fileHashes/resourceHashesCache.bin +0 -0
  13. package/android/.gradle/7.5.1/gc.properties +0 -0
  14. package/android/.gradle/buildOutputCleanup/buildOutputCleanup.lock +0 -0
  15. package/android/.gradle/buildOutputCleanup/cache.properties +2 -2
  16. package/android/.gradle/buildOutputCleanup/outputFiles.bin +0 -0
  17. package/android/.gradle/vcs-1/gc.properties +0 -0
  18. package/android/.idea/compiler.xml +5 -5
  19. package/android/.idea/gradle.xml +17 -18
  20. package/android/.idea/jarRepositories.xml +44 -49
  21. package/android/.idea/misc.xml +9 -9
  22. package/android/.idea/vcs.xml +5 -5
  23. package/android/build.gradle +69 -67
  24. package/android/gradle/wrapper/gradle-wrapper.properties +1 -0
  25. package/android/gradlew +375 -240
  26. package/android/local.properties +8 -8
  27. package/android/src/main/AndroidManifest.xml +4 -4
  28. package/android/src/main/java/land/fx/fula/ConfigRef.java +7 -7
  29. package/android/src/main/java/land/fx/fula/Cryptography.java +47 -47
  30. package/android/src/main/java/land/fx/fula/FulaModule.java +701 -480
  31. package/android/src/main/java/land/fx/fula/FulaPackage.java +32 -32
  32. package/android/src/main/java/land/fx/fula/SharedPreferenceHelper.java +65 -65
  33. package/android/src/main/java/land/fx/fula/StaticHelper.java +13 -13
  34. package/android/src/main/java/land/fx/fula/ThreadUtils.java +42 -42
  35. package/ios/FulaModule.h +10 -10
  36. package/ios/FulaModule.m +149 -149
  37. package/lib/commonjs/interfaces/fulaNativeModule.js.map +1 -1
  38. package/lib/commonjs/protocols/fula.js +127 -70
  39. package/lib/commonjs/protocols/fula.js.map +1 -1
  40. package/lib/module/interfaces/fulaNativeModule.js.map +1 -1
  41. package/lib/module/protocols/fula.js +122 -68
  42. package/lib/module/protocols/fula.js.map +1 -1
  43. package/lib/typescript/index.d.ts +1 -1
  44. package/lib/typescript/interfaces/fulaNativeModule.d.ts +28 -20
  45. package/lib/typescript/protocols/fula.d.ts +99 -77
  46. package/package.json +149 -152
  47. package/react-native-fula.podspec +19 -19
  48. package/src/interfaces/fulaNativeModule.ts +52 -42
  49. package/src/protocols/fula.ts +212 -138
  50. package/android/.gradle/7.5.1/executionHistory/executionHistory.bin +0 -0
  51. package/android/.gradle/file-system.probe +0 -0
@@ -1,480 +1,701 @@
1
- package land.fx.fula;
2
-
3
- import android.util.Log;
4
-
5
- import androidx.annotation.NonNull;
6
-
7
- import com.facebook.react.bridge.Promise;
8
- import com.facebook.react.bridge.ReactApplicationContext;
9
- import com.facebook.react.bridge.ReactContextBaseJavaModule;
10
- import com.facebook.react.bridge.ReactMethod;
11
- import com.facebook.react.module.annotations.ReactModule;
12
-
13
- import org.jetbrains.annotations.Contract;
14
-
15
- import java.io.File;
16
- import java.nio.charset.StandardCharsets;
17
- import java.util.Arrays;
18
-
19
- import javax.crypto.SecretKey;
20
-
21
- import fulamobile.Config;
22
- import fulamobile.Fulamobile;
23
-
24
- import land.fx.wnfslib.LibKt;
25
-
26
- @ReactModule(name = FulaModule.NAME)
27
- public class FulaModule extends ReactContextBaseJavaModule {
28
- public static final String NAME = "FulaModule";
29
- fulamobile.Client fula;
30
- Client client;
31
- String appDir;
32
- String fulaStorePath;
33
- String privateForest;
34
- land.fx.wnfslib.Config rootConfig;
35
- SharedPreferenceHelper sharedPref;
36
- static String PRIVATE_KEY_STORE_ID = "PRIVATE_KEY";
37
-
38
- public class Client implements land.fx.wnfslib.Client {
39
-
40
- private fulamobile.Client internalClient;
41
-
42
- Client(fulamobile.Client clientInput) {
43
- internalClient = clientInput;
44
- }
45
-
46
- @NonNull
47
- @Override
48
- public byte[] get(@NonNull byte[] cid) {
49
- try {
50
- internalClient.get(cid);
51
- } catch (Exception e) {
52
- e.printStackTrace();
53
- }
54
- return null;
55
- }
56
-
57
- @NonNull
58
- @Override
59
- public byte[] put(@NonNull byte[] data, long codec) {
60
- try {
61
- return client.put(data, codec);
62
- } catch (Exception e) {
63
- e.printStackTrace();
64
- }
65
- return null;
66
- }
67
- }
68
-
69
- public FulaModule(ReactApplicationContext reactContext) {
70
- super(reactContext);
71
- appDir = reactContext.getFilesDir().toString();
72
- fulaStorePath = appDir + "/fula";
73
- File storeDir = new File(fulaStorePath);
74
- sharedPref = SharedPreferenceHelper.getInstance(reactContext.getApplicationContext());
75
- boolean success = true;
76
- if (!storeDir.exists()) {
77
- success = storeDir.mkdirs();
78
- }
79
- if (success) {
80
- Log.d(NAME, "Fula store folder created");
81
- } else {
82
- Log.d(NAME, "Unable to create fula store folder!");
83
- }
84
- }
85
-
86
- @Override
87
- @NonNull
88
- public String getName() {
89
- return NAME;
90
- }
91
-
92
-
93
- private byte[] toByte(@NonNull String input) {
94
- return input.getBytes(StandardCharsets.UTF_8);
95
- }
96
-
97
- @NonNull
98
- @Contract("_ -> new")
99
- private String toString(byte[] input) {
100
- return new String(input, StandardCharsets.UTF_8);
101
- }
102
-
103
- @NonNull
104
- private static int[] stringArrToIntArr(@NonNull String[] s) {
105
- int[] result = new int[s.length];
106
- for (int i = 0; i < s.length; i++) {
107
- result[i] = Integer.parseInt(s[i]);
108
- }
109
- return result;
110
- }
111
-
112
- @NonNull
113
- @Contract(pure = true)
114
- private static byte[] convertIntToByte(@NonNull int[] input) {
115
- byte[] result = new byte[input.length];
116
- for (int i = 0; i < input.length; i++) {
117
- byte b = (byte) input[i];
118
- result[i] = b;
119
- }
120
- return result;
121
- }
122
-
123
- @NonNull
124
- private static byte[] convertStringToByte(@NonNull String data) {
125
- String[] keyInt_S = data.split(",");
126
- int[] keyInt = stringArrToIntArr(keyInt_S);
127
-
128
- return convertIntToByte(keyInt);
129
- }
130
-
131
- @ReactMethod
132
- public void init(String identityString, String storePath, String bloxAddr, String exchange, Promise promise) {
133
- Log.d("ReactNative", "init started");
134
- ThreadUtils.runOnExecutor(() -> {
135
- try {
136
- Log.d("ReactNative", "init storePath= " + storePath);
137
- byte[] identity = toByte(identityString);
138
- Log.d("ReactNative", "init identity= " + identityString);
139
- String[] obj = initInternal(identity, storePath, bloxAddr, exchange);
140
- Log.d("ReactNative", "init object created: [ " + obj[0] + ", " + obj[1] + ", " + obj[2] + " ]");
141
- promise.resolve(obj);
142
- } catch (Exception e) {
143
- Log.d("ReactNative", "init failed with Error: " + e.getMessage());
144
- promise.reject(e);
145
- }
146
- });
147
- }
148
-
149
- @NonNull
150
- private byte[] createPeerIdentity(byte[] privateKey) throws Exception {
151
- try {
152
- // 1: First: create public key from provided private key
153
- // 2: Should read the local keychain store (if it is key-value, key is public key above,
154
- // 3: if found, decrypt using the private key
155
- // 4: If not found or decryption not successful, generate an identity
156
- // 5: then encrypt and store in keychain
157
-
158
- String encryptedKey = sharedPref.getValue(PRIVATE_KEY_STORE_ID);
159
- SecretKey secretKey = Cryptography.generateKey(privateKey);
160
- if (encryptedKey == null) {
161
- byte[] autoGeneratedIdentity = Fulamobile.generateEd25519Key();
162
- encryptedKey = Cryptography.encryptMsg(StaticHelper.bytesToBase64(autoGeneratedIdentity), secretKey);
163
- sharedPref.add(PRIVATE_KEY_STORE_ID, encryptedKey);
164
- }
165
- return StaticHelper.base64ToBytes(Cryptography.decryptMsg(encryptedKey, secretKey));
166
-
167
- } catch (Exception e) {
168
- Log.d("ReactNative", "createPeerIdentity failed with Error: " + e.getMessage());
169
- throw (e);
170
- }
171
- }
172
-
173
- @NonNull
174
- private String[] initInternal(byte[] identity, String storePath, String bloxAddr, String exchange) throws Exception {
175
- try {
176
- Config config_ext = new Config();
177
- if (storePath == null || storePath.trim().isEmpty()) {
178
- config_ext.setStorePath(fulaStorePath);
179
- } else {
180
- config_ext.setStorePath(storePath);
181
- }
182
- Log.d("ReactNative", "storePath is set: " + config_ext.getStorePath());
183
-
184
- byte[] peerIdentity = createPeerIdentity(identity);
185
- config_ext.setIdentity(peerIdentity);
186
- Log.d("ReactNative", "peerIdentity is set: " + toString(config_ext.getIdentity()));
187
- config_ext.setBloxAddr(bloxAddr);
188
- Log.d("ReactNative", "bloxAddr is set: " + config_ext.getBloxAddr());
189
- config_ext.setExchange(exchange);
190
- this.fula = Fulamobile.newClient(config_ext);
191
- this.client = new Client(this.fula);
192
- Log.d("ReactNative", "fula initialized: " + this.fula.id());
193
- if (this.rootConfig == null) {
194
- Log.d("ReactNative", "creating rootConfig");
195
- this.privateForest = LibKt.createPrivateForest(this.client);
196
- Log.d("ReactNative", "privateForest is created: " + this.privateForest);
197
- this.rootConfig = LibKt.createRootDir(this.client, this.privateForest);
198
- Log.d("ReactNative", "rootConfig is created: " + this.rootConfig.getCid());
199
- } else {
200
- Log.d("ReactNative", "rootConfig existed: " + this.rootConfig.getCid());
201
- }
202
- String peerId = this.fula.id();
203
- String[] obj = new String[3];
204
- obj[0] = peerId;
205
- obj[1] = this.rootConfig.getCid();
206
- obj[2] = this.rootConfig.getPrivate_ref();
207
- Log.d("ReactNative", "initInternal is completed successfully");
208
- return obj;
209
- } catch (Exception e) {
210
- Log.d("ReactNative", "init internal failed with Error: " + e.getMessage());
211
- throw (e);
212
- }
213
- }
214
-
215
- @ReactMethod
216
- public void mkdir(String path, Promise promise) {
217
- ThreadUtils.runOnExecutor(() -> {
218
- Log.d("ReactNative", "mkdir: path = " + path);
219
- try {
220
- land.fx.wnfslib.Config config = LibKt.mkdir(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), path);
221
- this.rootConfig = config;
222
- promise.resolve(config.getCid());
223
- } catch (Exception e) {
224
- Log.d("get", e.getMessage());
225
- promise.reject(e);
226
- }
227
- });
228
- }
229
-
230
- @ReactMethod
231
- public void writeFile(String fulaTargetFilename, String localFilename, Promise promise) {
232
- /*
233
- // reads content of the file form localFilename (should include full absolute path to local file with read permission
234
- // writes content to the specified location by fulaTargetFilename in Fula filesystem
235
- // fulaTargetFilename: a string including full path and filename of target file on Fula (e.g. root/pictures/cat.jpg)
236
- // localFilename: a string containing full path and filename of local file on hte device (e.g /usr/bin/cat.jpg)
237
- // Returns: new cid of the root after this file is placed in the tree
238
- */
239
- ThreadUtils.runOnExecutor(() -> {
240
- Log.d("ReactNative", "writeFile to : path = " + fulaTargetFilename + ", from: " + localFilename);
241
- try {
242
- land.fx.wnfslib.Config config = LibKt.writeFileFromPath(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), fulaTargetFilename, localFilename);
243
- this.rootConfig = config;
244
- promise.resolve(config.getCid());
245
- } catch (Exception e) {
246
- Log.d("get", e.getMessage());
247
- promise.reject(e);
248
- }
249
- });
250
- }
251
-
252
- @ReactMethod
253
- public void writeFileContent(String path, String contentString, Promise promise) {
254
- ThreadUtils.runOnExecutor(() -> {
255
- Log.d("ReactNative", "writeFile: contentString = " + contentString);
256
- Log.d("ReactNative", "writeFile: path = " + path);
257
- try {
258
- byte[] content = convertStringToByte(contentString);
259
- land.fx.wnfslib.Config config = LibKt.writeFile(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), path, content);
260
- this.rootConfig = config;
261
- promise.resolve(config.getCid());
262
- } catch (Exception e) {
263
- Log.d("get", e.getMessage());
264
- promise.reject(e);
265
- }
266
- });
267
- }
268
-
269
- @ReactMethod
270
- public void ls(String path, Promise promise) {
271
- ThreadUtils.runOnExecutor(() -> {
272
- Log.d("ReactNative", "ls: path = " + path);
273
- try {
274
- String res = LibKt.ls(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), path);
275
- promise.resolve(res);
276
- } catch (Exception e) {
277
- Log.d("get", e.getMessage());
278
- promise.reject(e);
279
- }
280
- });
281
- }
282
-
283
- @ReactMethod
284
- public void rm(String path, Promise promise) {
285
- ThreadUtils.runOnExecutor(() -> {
286
- Log.d("ReactNative", "rm: path = " + path);
287
- try {
288
- land.fx.wnfslib.Config config = LibKt.rm(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), path);
289
- this.rootConfig = config;
290
- promise.resolve(config.getCid());
291
- } catch (Exception e) {
292
- Log.d("get", e.getMessage());
293
- promise.reject(e);
294
- }
295
- });
296
- }
297
-
298
- @ReactMethod
299
- public void readFile(String fulaTargetFilename, String localFilename, Promise promise) {
300
- /*
301
- // reads content of the file form localFilename (should include full absolute path to local file with read permission
302
- // writes content to the specified location by fulaTargetFilename in Fula filesystem
303
- // fulaTargetFilename: a string including full path and filename of target file on Fula (e.g. root/pictures/cat.jpg)
304
- // localFilename: a string containing full path and filename of local file on hte device (e.g /usr/bin/cat.jpg)
305
- // Returns: new cid of the root after this file is placed in the tree
306
- */
307
- ThreadUtils.runOnExecutor(() -> {
308
- Log.d("ReactNative", "readFile: fulaTargetFilename = " + fulaTargetFilename);
309
- try {
310
- String path = LibKt.readFileToPath(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), fulaTargetFilename, localFilename);
311
- promise.resolve(path);
312
- } catch (Exception e) {
313
- Log.d("get", e.getMessage());
314
- promise.reject(e);
315
- }
316
- });
317
- }
318
-
319
- @ReactMethod
320
- public void readFileContent(String path, Promise promise) {
321
- ThreadUtils.runOnExecutor(() -> {
322
- Log.d("ReactNative", "readFileContent: path = " + path);
323
- try {
324
- byte[] res = LibKt.readFile(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), path);
325
- String resString = toString(res);
326
- promise.resolve(resString);
327
- } catch (Exception e) {
328
- Log.d("get", e.getMessage());
329
- promise.reject(e);
330
- }
331
- });
332
- }
333
-
334
- @ReactMethod
335
- public void get(String keyString, Promise promise) {
336
- ThreadUtils.runOnExecutor(() -> {
337
- Log.d("ReactNative", "get: keyString = " + keyString);
338
- try {
339
- byte[] key = convertStringToByte(keyString);
340
- byte[] value = getInternal(key);
341
- String valueString = toString(value);
342
- promise.resolve(valueString);
343
- } catch (Exception e) {
344
- Log.d("get", e.getMessage());
345
- promise.reject(e);
346
- }
347
- });
348
- }
349
-
350
- @NonNull
351
- private byte[] getInternal(byte[] key) throws Exception {
352
- try {
353
- Log.d("ReactNative", "getInternal: key.toString() = " + toString(key));
354
- Log.d("ReactNative", "getInternal: key.toString().bytes = " + Arrays.toString(key));
355
- byte[] value = this.fula.get(key);
356
- Log.d("ReactNative", "getInternal: value.toString() = " + toString(value));
357
- return value;
358
- } catch (Exception e) {
359
- Log.d("ReactNative", "getInternal: error = " + e.getMessage());
360
- Log.d("getInternal", e.getMessage());
361
- throw (e);
362
- }
363
- }
364
-
365
- @ReactMethod
366
- public void has(String keyString, Promise promise) {
367
- ThreadUtils.runOnExecutor(() -> {
368
- Log.d("ReactNative", "has: keyString = " + keyString);
369
- try {
370
- byte[] key = convertStringToByte(keyString);
371
- boolean result = hasInternal(key);
372
- promise.resolve(result);
373
- } catch (Exception e) {
374
- Log.d("get", e.getMessage());
375
- promise.reject(e);
376
- }
377
- });
378
- }
379
-
380
- @NonNull
381
- private boolean hasInternal(byte[] key) throws Exception {
382
- try {
383
- boolean res = this.fula.has(key);
384
- return res;
385
- } catch (Exception e) {
386
- Log.d("hasInternal", e.getMessage());
387
- throw (e);
388
- }
389
- }
390
-
391
- @NonNull
392
- private void pullInternal(byte[] key) throws Exception {
393
- try {
394
- this.fula.pull(key);
395
- } catch (Exception e) {
396
- Log.d("pullInternal", e.getMessage());
397
- throw (e);
398
- }
399
- }
400
-
401
- @ReactMethod
402
- public void push(Promise promise) {
403
- ThreadUtils.runOnExecutor(() -> {
404
- Log.d("ReactNative", "push started");
405
- try {
406
- pushInternal(convertStringToByte(this.rootConfig.getCid()));
407
- promise.resolve(this.rootConfig.getCid());
408
- } catch (Exception e) {
409
- Log.d("get", e.getMessage());
410
- promise.reject(e);
411
- }
412
- });
413
- }
414
-
415
- @NonNull
416
- private void pushInternal(byte[] key) throws Exception {
417
- try {
418
- if (this.fula.has(key)) {
419
- this.fula.push(key);
420
- } else {
421
- Log.d("pushInternal", "error: key wasn't found");
422
- throw new Exception("key wasn't found in local storage");
423
- }
424
- } catch (Exception e) {
425
- Log.d("pushInternal", e.getMessage());
426
- throw (e);
427
- }
428
- }
429
-
430
- @ReactMethod
431
- public void put(String valueString, String codecString, Promise promise) {
432
- ThreadUtils.runOnExecutor(() -> {
433
- Log.d("ReactNative", "put: codecString = " + codecString);
434
- Log.d("ReactNative", "put: valueString = " + valueString);
435
- try {
436
- //byte[] codec = convertStringToByte(CodecString);
437
- long codec = Long.parseLong(codecString);
438
-
439
-
440
- Log.d("ReactNative", "put: codec = " + codec);
441
- byte[] value = toByte(valueString);
442
-
443
- Log.d("ReactNative", "put: value.toString() = " + toString(value));
444
- byte[] key = putInternal(value, codec);
445
- Log.d("ReactNative", "put: key.toString() = " + toString(key));
446
- promise.resolve(toString(key));
447
- } catch (Exception e) {
448
- Log.d("ReactNative", "put: error = " + e.getMessage());
449
- Log.d("put", e.getMessage());
450
- promise.reject(e);
451
- }
452
- });
453
- }
454
-
455
- @NonNull
456
- private byte[] putInternal(byte[] value, long codec) throws Exception {
457
- try {
458
- byte[] key = this.fula.put(value, codec);
459
- return key;
460
- } catch (Exception e) {
461
- Log.d("putInternal", e.getMessage());
462
- throw (e);
463
- }
464
- }
465
-
466
- @ReactMethod
467
- public void shutdown(Promise promise) {
468
- ThreadUtils.runOnExecutor(() -> {
469
- try {
470
- fula.shutdown();
471
- promise.resolve(true);
472
- } catch (Exception e) {
473
- promise.reject(e);
474
- Log.d("shutdown", e.getMessage());
475
- }
476
- });
477
-
478
- }
479
-
480
- }
1
+ package land.fx.fula;
2
+
3
+ import android.util.Log;
4
+
5
+ import androidx.annotation.NonNull;
6
+
7
+ import com.facebook.react.bridge.Promise;
8
+ import com.facebook.react.bridge.ReactApplicationContext;
9
+ import com.facebook.react.bridge.ReactContextBaseJavaModule;
10
+ import com.facebook.react.bridge.ReactMethod;
11
+ import com.facebook.react.bridge.WritableMap;
12
+ import com.facebook.react.bridge.WritableNativeMap;
13
+ import com.facebook.react.module.annotations.ReactModule;
14
+
15
+ import org.apache.commons.io.FileUtils;
16
+ import org.jetbrains.annotations.Contract;
17
+
18
+ import java.io.File;
19
+ import java.nio.charset.StandardCharsets;
20
+ import java.util.Arrays;
21
+
22
+ import javax.crypto.SecretKey;
23
+
24
+ import org.json.JSONObject;
25
+ import org.json.JSONArray;
26
+
27
+ import fulamobile.Config;
28
+ import fulamobile.Fulamobile;
29
+
30
+ import land.fx.wnfslib.Fs;
31
+
32
+ @ReactModule(name = FulaModule.NAME)
33
+ public class FulaModule extends ReactContextBaseJavaModule {
34
+
35
+
36
+ @Override
37
+ public void initialize() {
38
+ System.loadLibrary("wnfslib");
39
+ System.loadLibrary("gojni");
40
+ }
41
+
42
+
43
+ public static final String NAME = "FulaModule";
44
+ fulamobile.Client fula;
45
+ Client client;
46
+ String appDir;
47
+ String fulaStorePath;
48
+ String privateForest;
49
+ land.fx.wnfslib.Config rootConfig;
50
+ SharedPreferenceHelper sharedPref;
51
+ SecretKey secretKeyGlobal;
52
+ String identityEncryptedGlobal;
53
+ static String PRIVATE_KEY_STORE_ID = "PRIVATE_KEY";
54
+
55
+ public static class Client implements land.fx.wnfslib.Datastore {
56
+
57
+ private final fulamobile.Client internalClient;
58
+
59
+ Client(fulamobile.Client clientInput) {
60
+ this.internalClient = clientInput;
61
+ }
62
+
63
+ @NonNull
64
+ @Override
65
+ public byte[] get(@NonNull byte[] cid) {
66
+ try {
67
+ Log.d("ReactNative", Arrays.toString(cid));
68
+ return this.internalClient.get(cid);
69
+ } catch (Exception e) {
70
+ e.printStackTrace();
71
+ }
72
+ Log.d("ReactNative","Error get");
73
+ return cid;
74
+ }
75
+
76
+ @NonNull
77
+ @Override
78
+ public byte[] put(@NonNull byte[] data, long codec) {
79
+ try {
80
+ //Log.d("ReactNative", "data="+ Arrays.toString(data) +" ;codec="+codec);
81
+ return this.internalClient.put(data, codec);
82
+ } catch (Exception e) {
83
+ Log.d("ReactNative", "put Error="+e.getMessage());
84
+ e.printStackTrace();
85
+ }
86
+ Log.d("ReactNative","Error put");
87
+ return data;
88
+ }
89
+ }
90
+
91
+ public FulaModule(ReactApplicationContext reactContext) {
92
+ super(reactContext);
93
+ appDir = reactContext.getFilesDir().toString();
94
+ fulaStorePath = appDir + "/fula";
95
+ File storeDir = new File(fulaStorePath);
96
+ sharedPref = SharedPreferenceHelper.getInstance(reactContext.getApplicationContext());
97
+ boolean success = true;
98
+ if (!storeDir.exists()) {
99
+ success = storeDir.mkdirs();
100
+ }
101
+ if (success) {
102
+ Log.d(NAME, "Fula store folder created");
103
+ } else {
104
+ Log.d(NAME, "Unable to create fula store folder!");
105
+ }
106
+ }
107
+
108
+ @Override
109
+ @NonNull
110
+ public java.lang.String getName() {
111
+ return NAME;
112
+ }
113
+
114
+
115
+ private byte[] toByte(@NonNull String input) {
116
+ return input.getBytes(StandardCharsets.UTF_8);
117
+ }
118
+
119
+ @NonNull
120
+ @Contract("_ -> new")
121
+ private String toString(byte[] input) {
122
+ return new String(input, StandardCharsets.UTF_8);
123
+ }
124
+
125
+ @NonNull
126
+ private static int[] stringArrToIntArr(@NonNull String[] s) {
127
+ int[] result = new int[s.length];
128
+ for (int i = 0; i < s.length; i++) {
129
+ result[i] = Integer.parseInt(s[i]);
130
+ }
131
+ return result;
132
+ }
133
+
134
+ @NonNull
135
+ @Contract(pure = true)
136
+ private static byte[] convertIntToByte(@NonNull int[] input) {
137
+ byte[] result = new byte[input.length];
138
+ for (int i = 0; i < input.length; i++) {
139
+ byte b = (byte) input[i];
140
+ result[i] = b;
141
+ }
142
+ return result;
143
+ }
144
+
145
+ @NonNull
146
+ private static byte[] convertStringToByte(@NonNull String data) {
147
+ String[] keyInt_S = data.split(",");
148
+ int[] keyInt = stringArrToIntArr(keyInt_S);
149
+
150
+ return convertIntToByte(keyInt);
151
+ }
152
+
153
+ @ReactMethod
154
+ public void init(String identityString, String storePath, String bloxAddr, String exchange, String rootConfig, Promise promise) {
155
+ Log.d("ReactNative", "init started");
156
+ ThreadUtils.runOnExecutor(() -> {
157
+ try {
158
+ WritableMap resultData = new WritableNativeMap();
159
+ Log.d("ReactNative", "init storePath= " + storePath);
160
+ byte[] identity = toByte(identityString);
161
+ Log.d("ReactNative", "init identity= " + identityString);
162
+ String[] obj = initInternal(identity, storePath, bloxAddr, exchange, rootConfig);
163
+ Log.d("ReactNative", "init object created: [ " + obj[0] + ", " + obj[1] + ", " + obj[2] + " ]");
164
+ resultData.putString("peerId", obj[0]);
165
+ resultData.putString("rootCid", obj[1]);
166
+ resultData.putString("private_ref", obj[2]);
167
+ promise.resolve(resultData);
168
+ } catch (Exception e) {
169
+ Log.d("ReactNative", "init failed with Error: " + e.getMessage());
170
+ promise.reject("Error", e.getMessage());
171
+ }
172
+ });
173
+ }
174
+
175
+ @ReactMethod
176
+ public void logout(String identityString, String storePath, Promise promise) {
177
+ Log.d("ReactNative", "logout started");
178
+ ThreadUtils.runOnExecutor(() -> {
179
+ try {
180
+ byte[] identity = toByte(identityString);
181
+ boolean obj = logoutInternal(identity, storePath);
182
+ Log.d("ReactNative", "logout completed");
183
+ promise.resolve(obj);
184
+ } catch (Exception e) {
185
+ Log.d("ReactNative", "logout failed with Error: " + e.getMessage());
186
+ promise.reject("Error", e.getMessage());
187
+ }
188
+ });
189
+ }
190
+
191
+ @NonNull
192
+ private byte[] createPeerIdentity(byte[] privateKey) throws Exception {
193
+ try {
194
+ // 1: First: create public key from provided private key
195
+ // 2: Should read the local keychain store (if it is key-value, key is public key above,
196
+ // 3: if found, decrypt using the private key
197
+ // 4: If not found or decryption not successful, generate an identity
198
+ // 5: then encrypt and store in keychain
199
+
200
+ String encryptedKey = sharedPref.getValue(PRIVATE_KEY_STORE_ID);
201
+ SecretKey secretKey = Cryptography.generateKey(privateKey);
202
+ if (encryptedKey == null) {
203
+ byte[] autoGeneratedIdentity = Fulamobile.generateEd25519Key();
204
+ encryptedKey = Cryptography.encryptMsg(StaticHelper.bytesToBase64(autoGeneratedIdentity), secretKey);
205
+ sharedPref.add(PRIVATE_KEY_STORE_ID, encryptedKey);
206
+ }
207
+ return StaticHelper.base64ToBytes(Cryptography.decryptMsg(encryptedKey, secretKey));
208
+
209
+ } catch (Exception e) {
210
+ Log.d("ReactNative", "createPeerIdentity failed with Error: " + e.getMessage());
211
+ throw (e);
212
+ }
213
+ }
214
+
215
+ private void loadForestInternal(String privateRef, String cid) throws Exception {
216
+ try {
217
+ this.privateForest = Fs.createPrivateForest(this.client);
218
+ } catch (Exception e) {
219
+ Log.d("ReactNative", "loadForestInternal failed with Error: " + e.getMessage());
220
+ throw (e);
221
+ }
222
+ }
223
+
224
+ private void createNewRootConfig(FulaModule.Client iClient, byte[] identity) throws Exception {
225
+ this.privateForest = Fs.createPrivateForest(iClient);
226
+ Log.d("ReactNative", "privateForest is created: " + this.privateForest);
227
+ this.rootConfig = Fs.createRootDir(iClient, this.privateForest, identity);
228
+ Log.d("ReactNative", "new rootConfig is created: cid=" + this.rootConfig.getCid()+" & private_ref="+this.rootConfig.getPrivate_ref());
229
+
230
+ encrypt_and_store_config();
231
+ }
232
+
233
+ private String getPrivateRef(FulaModule.Client iClient, byte[] wnfsKey, String rootCid) throws Exception {
234
+ Log.d("ReactNative", "getPrivateRef called: rootCid=" + rootCid);
235
+ String privateRef = Fs.getPrivateRef(iClient, wnfsKey, rootCid);
236
+ Log.d("ReactNative", "getPrivateRef completed: privateRef=" + privateRef);
237
+ return privateRef;
238
+ }
239
+
240
+ private boolean encrypt_and_store_config() throws Exception {
241
+ try {
242
+ if(!this.identityEncryptedGlobal.isEmpty() && !this.identityEncryptedGlobal.isEmpty()) {
243
+ String cid_encrypted = Cryptography.encryptMsg(this.rootConfig.getCid(), this.secretKeyGlobal);
244
+ String private_ref_encrypted = Cryptography.encryptMsg(this.rootConfig.getPrivate_ref(), this.secretKeyGlobal);
245
+
246
+ sharedPref.add("cid_encrypted_" + this.identityEncryptedGlobal, cid_encrypted);
247
+ sharedPref.add("private_ref_encrypted_" + this.identityEncryptedGlobal, private_ref_encrypted);
248
+ return true;
249
+ } else {
250
+ return false;
251
+ }
252
+ } catch (Exception e) {
253
+ Log.d("ReactNative", "encrypt_and_store_config failed with Error: " + e.getMessage());
254
+ throw (e);
255
+ }
256
+ }
257
+
258
+ @NonNull
259
+ private boolean logoutInternal(byte[] identity, String storePath) throws Exception {
260
+ try {
261
+ SecretKey secretKey = Cryptography.generateKey(identity);
262
+ String identity_encrypted = Cryptography.encryptMsg(Arrays.toString(identity), secretKey);
263
+ sharedPref.remove("cid_encrypted_"+ identity_encrypted);
264
+ sharedPref.remove("private_ref_encrypted_"+identity_encrypted);
265
+
266
+ //TODO: Should also remove peerid @Mahdi
267
+
268
+ sharedPref.remove("cid_encrypted_"+ identity_encrypted);
269
+ sharedPref.remove("private_ref_encrypted_"+ identity_encrypted);
270
+
271
+ this.rootConfig = null;
272
+ this.secretKeyGlobal = null;
273
+ this.identityEncryptedGlobal = null;
274
+
275
+ if (storePath == null || storePath.trim().isEmpty()) {
276
+ storePath = this.fulaStorePath;
277
+ }
278
+
279
+ File file = new File(storePath);
280
+ FileUtils.deleteDirectory(file);
281
+ return true;
282
+
283
+ } catch (Exception e) {
284
+ Log.d("ReactNative", "logout internal failed with Error: " + e.getMessage());
285
+ throw (e);
286
+ }
287
+ }
288
+
289
+ @NonNull
290
+ private String[] initInternal(byte[] identity, String storePath, String bloxAddr, String exchange, String rootCid) throws Exception {
291
+ try {
292
+ Config config_ext = new Config();
293
+ if (storePath == null || storePath.trim().isEmpty()) {
294
+ config_ext.setStorePath(this.fulaStorePath);
295
+ } else {
296
+ config_ext.setStorePath(storePath);
297
+ }
298
+ Log.d("ReactNative", "storePath is set: " + config_ext.getStorePath());
299
+
300
+ byte[] peerIdentity = createPeerIdentity(identity);
301
+ config_ext.setIdentity(peerIdentity);
302
+ Log.d("ReactNative", "peerIdentity is set: " + toString(config_ext.getIdentity()));
303
+ config_ext.setBloxAddr(bloxAddr);
304
+ Log.d("ReactNative", "bloxAddr is set: " + config_ext.getBloxAddr());
305
+ config_ext.setExchange(exchange);
306
+ this.fula = Fulamobile.newClient(config_ext);
307
+ this.client = new Client(this.fula);
308
+ Log.d("ReactNative", "fula initialized: " + this.fula.id());
309
+
310
+ SecretKey secretKey = Cryptography.generateKey(identity);
311
+ String identity_encrypted =Cryptography.encryptMsg(Arrays.toString(identity), secretKey);
312
+ this.identityEncryptedGlobal = identity_encrypted;
313
+ this.secretKeyGlobal = secretKey;
314
+
315
+ if (this.rootConfig == null || this.rootConfig.getCid().isEmpty() || this.rootConfig.getPrivate_ref().isEmpty()) {
316
+ Log.d("ReactNative", "this.rootCid is empty.");
317
+ //Load from keystore
318
+
319
+ String cid_encrypted_fetched = sharedPref.getValue("cid_encrypted_"+ identity_encrypted);
320
+ String private_ref_encrypted_fetched = sharedPref.getValue("private_ref_encrypted_"+identity_encrypted);
321
+ Log.d("ReactNative", "Here1");
322
+ String cid = "";
323
+ String private_ref = "";
324
+ if(cid_encrypted_fetched != null && !cid_encrypted_fetched.isEmpty()) {
325
+ Log.d("ReactNative", "decrypting cid="+cid_encrypted_fetched+" with secret="+secretKey.toString());
326
+ cid = Cryptography.decryptMsg(cid_encrypted_fetched, secretKey);
327
+ }
328
+ if(private_ref_encrypted_fetched != null && !private_ref_encrypted_fetched.isEmpty()) {
329
+ Log.d("ReactNative", "decrypting private_ref="+private_ref_encrypted_fetched+" with secret="+secretKey.toString());
330
+ private_ref = Cryptography.decryptMsg(private_ref_encrypted_fetched, secretKey);
331
+ }
332
+ Log.d("ReactNative", "Here2");
333
+ //Log.d("ReactNative", "Attempted to fetch cid from keystore; cid="+cid+" & private_ref="+private_ref);
334
+ if((cid == null || cid.isEmpty()) || (private_ref == null || private_ref.isEmpty()) ){
335
+ Log.d("ReactNative", "cid or PrivateRef was not found");
336
+ if(rootCid !=null && !rootCid.isEmpty()){
337
+ Log.d("ReactNative", "Re-setting cid from input: "+rootCid);
338
+ cid = rootCid;
339
+ }
340
+ if((private_ref == null || private_ref.isEmpty()) && (cid != null && !cid.isEmpty())){
341
+ Log.d("ReactNative", "Re-fetching privateRef from wnfs: cid="+cid);
342
+ private_ref = getPrivateRef(this.client, identity, cid);
343
+ Log.d("ReactNative", "Re-fetching privateRef from wnfs: "+private_ref);
344
+ }
345
+ if(cid == null || cid.isEmpty() || private_ref == null || private_ref.isEmpty()) {
346
+ Log.d("ReactNative", "Tried to recover cid and privateRef but was not successful. Creating new ones");
347
+ createNewRootConfig(this.client, identity);
348
+ } else {
349
+ Log.d("ReactNative", "Tried to recover cid and privateRef and was successful. cid:"+cid+" & private_ref="+private_ref);
350
+ this.rootConfig = new land.fx.wnfslib.Config(cid, private_ref);
351
+ encrypt_and_store_config();
352
+ }
353
+ } else if(cid != null && !cid.isEmpty() && private_ref != null && !private_ref.isEmpty()) {
354
+ Log.d("ReactNative", "Found cid and private ref in keychain store");
355
+ if(cid != null && !cid.isEmpty() && private_ref!=null && !private_ref.isEmpty()) {
356
+ Log.d("ReactNative", "Recovered cid and private ref from keychain store. cid="+cid+" & private_ref="+private_ref);
357
+ this.rootConfig = new land.fx.wnfslib.Config(cid, private_ref);
358
+ } else{
359
+ Log.d("ReactNative", "Found but Could not recover cid and private_ref from keychain store");
360
+ createNewRootConfig(this.client, identity);
361
+ }
362
+ } else{
363
+ Log.d("ReactNative", "This cid and private_ref generation should never happen!!!");
364
+ //Create new root and store cid and private_ref
365
+ createNewRootConfig(this.client, identity);
366
+ }
367
+
368
+
369
+ Log.d("ReactNative", "creating rootConfig completed");
370
+
371
+ /*
372
+ byte[] testbyte = convertStringToByte("-104,40,24,-93,24,100,24,114,24,111,24,111,24,116,24,-126,24,-126,0,0,24,-128,24,103,24,118,24,101,24,114,24,115,24,105,24,111,24,110,24,101,24,48,24,46,24,49,24,46,24,48,24,105,24,115,24,116,24,114,24,117,24,99,24,116,24,117,24,114,24,101,24,100,24,104,24,97,24,109,24,116");
373
+ long testcodec = 85;
374
+ byte[] testputcid = this.client.put(testbyte, testcodec);
375
+ Log.d("ReactNative", "client.put test done"+ Arrays.toString(testputcid));
376
+ byte[] testfetchedcid = convertStringToByte("1,113,18,32,-6,-63,-128,79,-102,-89,57,77,-8,67,-98,8,-81,40,-87,123,122,29,-52,-124,-60,-53,100,105,125,123,-5,-99,41,106,-124,-64");
377
+ byte[] testfetchedbytes = this.client.get(testfetchedcid);
378
+ Log.d("ReactNative", "client.get test done"+ Arrays.toString(testfetchedbytes));
379
+ */
380
+
381
+
382
+ Log.d("ReactNative", "rootConfig is created: cid=" + this.rootConfig.getCid()+ "& private_ref="+this
383
+ .rootConfig.getPrivate_ref());
384
+ } else {
385
+ Log.d("ReactNative", "rootConfig existed: cid=" + this.rootConfig.getCid()+ " & private_ref="+this.rootConfig.getPrivate_ref());
386
+ }
387
+ String peerId = this.fula.id();
388
+ String[] obj = new String[3];
389
+ obj[0] = peerId;
390
+ obj[1] = this.rootConfig.getCid();
391
+ obj[2] = this.rootConfig.getPrivate_ref();
392
+ Log.d("ReactNative", "initInternal is completed successfully");
393
+ return obj;
394
+ } catch (Exception e) {
395
+ Log.d("ReactNative", "init internal failed with Error: " + e.getMessage());
396
+ throw (e);
397
+ }
398
+ }
399
+
400
+ @ReactMethod
401
+ public void mkdir(String path, Promise promise) {
402
+ ThreadUtils.runOnExecutor(() -> {
403
+ Log.d("ReactNative", "mkdir: path = " + path);
404
+ try {
405
+ land.fx.wnfslib.Config config = Fs.mkdir(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), path);
406
+ this.rootConfig = config;
407
+ promise.resolve(config.getCid());
408
+ } catch (Exception e) {
409
+ Log.d("get", e.getMessage());
410
+ promise.reject(e);
411
+ }
412
+ });
413
+ }
414
+
415
+ @ReactMethod
416
+ public void writeFile(String fulaTargetFilename, String localFilename, Promise promise) {
417
+ /*
418
+ // reads content of the file form localFilename (should include full absolute path to local file with read permission
419
+ // writes content to the specified location by fulaTargetFilename in Fula filesystem
420
+ // fulaTargetFilename: a string including full path and filename of target file on Fula (e.g. root/pictures/cat.jpg)
421
+ // localFilename: a string containing full path and filename of local file on hte device (e.g /usr/bin/cat.jpg)
422
+ // Returns: new cid of the root after this file is placed in the tree
423
+ */
424
+ ThreadUtils.runOnExecutor(() -> {
425
+ Log.d("ReactNative", "writeFile to : path = " + fulaTargetFilename + ", from: " + localFilename);
426
+ try {
427
+ land.fx.wnfslib.Config config = Fs.writeFileFromPath(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), fulaTargetFilename, localFilename);
428
+ this.rootConfig = config;
429
+ encrypt_and_store_config();
430
+ promise.resolve(config.getCid());
431
+ } catch (Exception e) {
432
+ Log.d("get", e.getMessage());
433
+ promise.reject(e);
434
+ }
435
+ });
436
+ }
437
+
438
+ @ReactMethod
439
+ public void writeFileContent(String path, String contentString, Promise promise) {
440
+ ThreadUtils.runOnExecutor(() -> {
441
+ Log.d("ReactNative", "writeFile: contentString = " + contentString);
442
+ Log.d("ReactNative", "writeFile: path = " + path);
443
+ try {
444
+ byte[] content = convertStringToByte(contentString);
445
+ land.fx.wnfslib.Config config = Fs.writeFile(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), path, content);
446
+ this.rootConfig = config;
447
+ encrypt_and_store_config();
448
+ promise.resolve(config.getCid());
449
+ } catch (Exception e) {
450
+ Log.d("get", e.getMessage());
451
+ promise.reject(e);
452
+ }
453
+ });
454
+ }
455
+
456
+ @ReactMethod
457
+ public void ls(String path, Promise promise) {
458
+ ThreadUtils.runOnExecutor(() -> {
459
+ Log.d("ReactNative", "ls: path = " + path);
460
+ try {
461
+ byte[] res = Fs.ls(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), path);
462
+
463
+ //JSONArray jsonArray = new JSONArray(res);
464
+ String s = new String(res, StandardCharsets.UTF_8);
465
+ Log.d("ReactNative", "ls: res = " + s);
466
+ promise.resolve(s);
467
+ } catch (Exception e) {
468
+ Log.d("get", e.getMessage());
469
+ promise.reject(e);
470
+ }
471
+ });
472
+ }
473
+
474
+ @ReactMethod
475
+ public void rm(String path, Promise promise) {
476
+ ThreadUtils.runOnExecutor(() -> {
477
+ Log.d("ReactNative", "rm: path = " + path);
478
+ try {
479
+ land.fx.wnfslib.Config config = Fs.rm(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), path);
480
+ this.rootConfig = config;
481
+ encrypt_and_store_config();
482
+ promise.resolve(config.getCid());
483
+ } catch (Exception e) {
484
+ Log.d("get", e.getMessage());
485
+ promise.reject(e);
486
+ }
487
+ });
488
+ }
489
+
490
+ @ReactMethod
491
+ public void cp(String sourcePath, String targetPath, Promise promise) {
492
+ ThreadUtils.runOnExecutor(() -> {
493
+ Log.d("ReactNative", "rm: sourcePath = " + sourcePath);
494
+ try {
495
+ land.fx.wnfslib.Config config = Fs.cp(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), sourcePath, targetPath);
496
+ this.rootConfig = config;
497
+ encrypt_and_store_config();
498
+ promise.resolve(config.getCid());
499
+ } catch (Exception e) {
500
+ Log.d("get", e.getMessage());
501
+ promise.reject(e);
502
+ }
503
+ });
504
+ }
505
+
506
+ @ReactMethod
507
+ public void mv(String sourcePath, String targetPath, Promise promise) {
508
+ ThreadUtils.runOnExecutor(() -> {
509
+ Log.d("ReactNative", "rm: sourcePath = " + sourcePath);
510
+ try {
511
+ land.fx.wnfslib.Config config = Fs.mv(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), sourcePath, targetPath);
512
+ this.rootConfig = config;
513
+ encrypt_and_store_config();
514
+ promise.resolve(config.getCid());
515
+ } catch (Exception e) {
516
+ Log.d("get", e.getMessage());
517
+ promise.reject(e);
518
+ }
519
+ });
520
+ }
521
+
522
+ @ReactMethod
523
+ public void readFile(String fulaTargetFilename, String localFilename, Promise promise) {
524
+ /*
525
+ // reads content of the file form localFilename (should include full absolute path to local file with read permission
526
+ // writes content to the specified location by fulaTargetFilename in Fula filesystem
527
+ // fulaTargetFilename: a string including full path and filename of target file on Fula (e.g. root/pictures/cat.jpg)
528
+ // localFilename: a string containing full path and filename of local file on hte device (e.g /usr/bin/cat.jpg)
529
+ // Returns: new cid of the root after this file is placed in the tree
530
+ */
531
+ ThreadUtils.runOnExecutor(() -> {
532
+ Log.d("ReactNative", "readFile: fulaTargetFilename = " + fulaTargetFilename);
533
+ try {
534
+ String path = Fs.readFilestreamToPath(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), fulaTargetFilename, localFilename);
535
+ promise.resolve(path);
536
+ } catch (Exception e) {
537
+ Log.d("get", e.getMessage());
538
+ promise.reject(e);
539
+ }
540
+ });
541
+ }
542
+
543
+ @ReactMethod
544
+ public void readFileContent(String path, Promise promise) {
545
+ ThreadUtils.runOnExecutor(() -> {
546
+ Log.d("ReactNative", "readFileContent: path = " + path);
547
+ try {
548
+ byte[] res = Fs.readFile(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), path);
549
+ String resString = toString(res);
550
+ promise.resolve(resString);
551
+ } catch (Exception e) {
552
+ Log.d("get", e.getMessage());
553
+ promise.reject(e);
554
+ }
555
+ });
556
+ }
557
+
558
+ @ReactMethod
559
+ public void get(String keyString, Promise promise) {
560
+ ThreadUtils.runOnExecutor(() -> {
561
+ Log.d("ReactNative", "get: keyString = " + keyString);
562
+ try {
563
+ byte[] key = convertStringToByte(keyString);
564
+ byte[] value = getInternal(key);
565
+ String valueString = toString(value);
566
+ promise.resolve(valueString);
567
+ } catch (Exception e) {
568
+ Log.d("get", e.getMessage());
569
+ promise.reject(e);
570
+ }
571
+ });
572
+ }
573
+
574
+ @NonNull
575
+ private byte[] getInternal(byte[] key) throws Exception {
576
+ try {
577
+ Log.d("ReactNative", "getInternal: key.toString() = " + toString(key));
578
+ Log.d("ReactNative", "getInternal: key.toString().bytes = " + Arrays.toString(key));
579
+ byte[] value = this.fula.get(key);
580
+ Log.d("ReactNative", "getInternal: value.toString() = " + toString(value));
581
+ return value;
582
+ } catch (Exception e) {
583
+ Log.d("ReactNative", "getInternal: error = " + e.getMessage());
584
+ Log.d("getInternal", e.getMessage());
585
+ throw (e);
586
+ }
587
+ }
588
+
589
+ @ReactMethod
590
+ public void has(String keyString, Promise promise) {
591
+ ThreadUtils.runOnExecutor(() -> {
592
+ Log.d("ReactNative", "has: keyString = " + keyString);
593
+ try {
594
+ byte[] key = convertStringToByte(keyString);
595
+ boolean result = hasInternal(key);
596
+ promise.resolve(result);
597
+ } catch (Exception e) {
598
+ Log.d("get", e.getMessage());
599
+ promise.reject(e);
600
+ }
601
+ });
602
+ }
603
+
604
+ private boolean hasInternal(byte[] key) throws Exception {
605
+ try {
606
+ boolean res = this.fula.has(key);
607
+ return res;
608
+ } catch (Exception e) {
609
+ Log.d("hasInternal", e.getMessage());
610
+ throw (e);
611
+ }
612
+ }
613
+
614
+ private void pullInternal(byte[] key) throws Exception {
615
+ try {
616
+ this.fula.pull(key);
617
+ } catch (Exception e) {
618
+ Log.d("pullInternal", e.getMessage());
619
+ throw (e);
620
+ }
621
+ }
622
+
623
+ @ReactMethod
624
+ public void push(Promise promise) {
625
+ ThreadUtils.runOnExecutor(() -> {
626
+ Log.d("ReactNative", "push started");
627
+ try {
628
+ pushInternal(convertStringToByte(this.rootConfig.getCid()));
629
+ promise.resolve(this.rootConfig.getCid());
630
+ } catch (Exception e) {
631
+ Log.d("get", e.getMessage());
632
+ promise.reject(e);
633
+ }
634
+ });
635
+ }
636
+
637
+ private void pushInternal(byte[] key) throws Exception {
638
+ try {
639
+ if (this.fula.has(key)) {
640
+ this.fula.push(key);
641
+ } else {
642
+ Log.d("pushInternal", "error: key wasn't found");
643
+ throw new Exception("key wasn't found in local storage");
644
+ }
645
+ } catch (Exception e) {
646
+ Log.d("pushInternal", e.getMessage());
647
+ throw (e);
648
+ }
649
+ }
650
+
651
+ @ReactMethod
652
+ public void put(String valueString, String codecString, Promise promise) {
653
+ ThreadUtils.runOnExecutor(() -> {
654
+ Log.d("ReactNative", "put: codecString = " + codecString);
655
+ Log.d("ReactNative", "put: valueString = " + valueString);
656
+ try {
657
+ //byte[] codec = convertStringToByte(CodecString);
658
+ long codec = Long.parseLong(codecString);
659
+
660
+
661
+ Log.d("ReactNative", "put: codec = " + codec);
662
+ byte[] value = toByte(valueString);
663
+
664
+ Log.d("ReactNative", "put: value.toString() = " + toString(value));
665
+ byte[] key = putInternal(value, codec);
666
+ Log.d("ReactNative", "put: key.toString() = " + toString(key));
667
+ promise.resolve(toString(key));
668
+ } catch (Exception e) {
669
+ Log.d("ReactNative", "put: error = " + e.getMessage());
670
+ Log.d("put", e.getMessage());
671
+ promise.reject(e);
672
+ }
673
+ });
674
+ }
675
+
676
+ @NonNull
677
+ private byte[] putInternal(byte[] value, long codec) throws Exception {
678
+ try {
679
+ byte[] key = this.fula.put(value, codec);
680
+ return key;
681
+ } catch (Exception e) {
682
+ Log.d("putInternal", e.getMessage());
683
+ throw (e);
684
+ }
685
+ }
686
+
687
+ @ReactMethod
688
+ public void shutdown(Promise promise) {
689
+ ThreadUtils.runOnExecutor(() -> {
690
+ try {
691
+ fula.shutdown();
692
+ promise.resolve(true);
693
+ } catch (Exception e) {
694
+ promise.reject(e);
695
+ Log.d("shutdown", e.getMessage());
696
+ }
697
+ });
698
+
699
+ }
700
+
701
+ }