@functionland/react-native-fula 1.14.1 → 1.14.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,1466 +1,1466 @@
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
- import com.facebook.react.bridge.Arguments;
15
- import com.facebook.react.bridge.WritableArray;
16
- import com.facebook.react.bridge.ReadableArray;
17
-
18
-
19
- import org.apache.commons.io.FileUtils;
20
- import org.jetbrains.annotations.Contract;
21
-
22
- import java.io.File;
23
- import java.io.IOException;
24
- import java.nio.charset.StandardCharsets;
25
- import java.security.GeneralSecurityException;
26
- import java.security.InvalidAlgorithmParameterException;
27
- import java.security.InvalidKeyException;
28
- import java.security.NoSuchAlgorithmException;
29
- import java.security.MessageDigest;
30
- import java.util.Arrays;
31
- import java.util.ArrayList;
32
-
33
- import javax.crypto.BadPaddingException;
34
- import javax.crypto.IllegalBlockSizeException;
35
- import javax.crypto.NoSuchPaddingException;
36
- import javax.crypto.SecretKey;
37
-
38
- import java.util.concurrent.Executors;
39
- import java.util.concurrent.ScheduledExecutorService;
40
- import java.util.concurrent.TimeUnit;
41
- import java.util.concurrent.atomic.AtomicBoolean;
42
- import java.util.concurrent.Future;
43
- import java.util.concurrent.TimeoutException;
44
-
45
- import fulamobile.Config;
46
- import fulamobile.Fulamobile;
47
-
48
- import land.fx.wnfslib.Fs;
49
-
50
- @ReactModule(name = FulaModule.NAME)
51
- public class FulaModule extends ReactContextBaseJavaModule {
52
-
53
-
54
- @Override
55
- public void initialize() {
56
- System.loadLibrary("wnfslib");
57
- System.loadLibrary("gojni");
58
- }
59
-
60
-
61
- public static final String NAME = "FulaModule";
62
- fulamobile.Client fula;
63
-
64
- Client client;
65
- Config fulaConfig;
66
-
67
- String appName;
68
- String appDir;
69
- String fulaStorePath;
70
- land.fx.wnfslib.Config rootConfig;
71
- SharedPreferenceHelper sharedPref;
72
- SecretKey secretKeyGlobal;
73
- String identityEncryptedGlobal;
74
- static String PRIVATE_KEY_STORE_PEERID = "PRIVATE_KEY";
75
-
76
- public static class Client implements land.fx.wnfslib.Datastore {
77
-
78
- private final fulamobile.Client internalClient;
79
-
80
- Client(fulamobile.Client clientInput) {
81
- this.internalClient = clientInput;
82
- }
83
-
84
- @NonNull
85
- @Override
86
- public byte[] get(@NonNull byte[] cid) {
87
- try {
88
- Log.d("ReactNative", Arrays.toString(cid));
89
- return this.internalClient.get(cid);
90
- } catch (Exception e) {
91
- e.printStackTrace();
92
- }
93
- Log.d("ReactNative","Error get");
94
- return cid;
95
- }
96
-
97
- @NonNull
98
- @Override
99
- public byte[] put(@NonNull byte[] cid, byte[] data) {
100
- try {
101
- long codec = (long)cid[1] & 0xFF;
102
- byte[] put_cid = this.internalClient.put(data, codec);
103
- //Log.d("ReactNative", "data="+ Arrays.toString(data) +" ;codec="+codec);
104
- return put_cid;
105
- } catch (Exception e) {
106
- Log.d("ReactNative", "put Error="+e.getMessage());
107
- e.printStackTrace();
108
- }
109
- Log.d("ReactNative","Error put");
110
- return data;
111
- }
112
- }
113
-
114
- public FulaModule(ReactApplicationContext reactContext) {
115
- super(reactContext);
116
- appName = reactContext.getPackageName();
117
- appDir = reactContext.getFilesDir().toString();
118
- fulaStorePath = appDir + "/fula";
119
- File storeDir = new File(fulaStorePath);
120
- sharedPref = SharedPreferenceHelper.getInstance(reactContext.getApplicationContext());
121
- boolean success = true;
122
- if (!storeDir.exists()) {
123
- success = storeDir.mkdirs();
124
- }
125
- if (success) {
126
- Log.d("ReactNative", "Fula store folder created for " + appName + " at " + fulaStorePath);
127
- } else {
128
- Log.d("ReactNative", "Unable to create fula store folder for " + appName + " at " + fulaStorePath);
129
- }
130
- }
131
-
132
- @Override
133
- @NonNull
134
- public java.lang.String getName() {
135
- return NAME;
136
- }
137
-
138
-
139
- private byte[] toByte(@NonNull String input) {
140
- return input.getBytes(StandardCharsets.UTF_8);
141
- }
142
-
143
- private byte[] decToByte(@NonNull String input) {
144
- String[] parts = input.split(",");
145
- byte[] output = new byte[parts.length];
146
- for (int i = 0; i < parts.length; i++) {
147
- output[i] = Byte.parseByte(parts[i]);
148
- }
149
- return output;
150
- }
151
-
152
- @NonNull
153
- @Contract("_ -> new")
154
- public String toString(byte[] input) {
155
- return new String(input, StandardCharsets.UTF_8);
156
- }
157
-
158
- @NonNull
159
- private static int[] stringArrToIntArr(@NonNull String[] s) {
160
- int[] result = new int[s.length];
161
- for (int i = 0; i < s.length; i++) {
162
- result[i] = Integer.parseInt(s[i]);
163
- }
164
- return result;
165
- }
166
-
167
- @NonNull
168
- @Contract(pure = true)
169
- private static byte[] convertIntToByte(@NonNull int[] input) {
170
- byte[] result = new byte[input.length];
171
- for (int i = 0; i < input.length; i++) {
172
- byte b = (byte) input[i];
173
- result[i] = b;
174
- }
175
- return result;
176
- }
177
-
178
- @NonNull
179
- private byte[] convertStringToByte(@NonNull String data) {
180
- String[] keyInt_S = data.split(",");
181
- int[] keyInt = stringArrToIntArr(keyInt_S);
182
-
183
- return convertIntToByte(keyInt);
184
- }
185
-
186
- @ReactMethod
187
- public void checkConnection(int timeout, Promise promise) {
188
- Log.d("ReactNative", "checkConnection started");
189
- ThreadUtils.runOnExecutor(() -> {
190
- if (this.fula != null) {
191
- try {
192
- boolean connectionStatus = this.checkConnectionInternal(timeout);
193
- Log.d("ReactNative", "checkConnection ended " + connectionStatus);
194
- promise.resolve(connectionStatus);
195
- }
196
- catch (Exception e) {
197
- Log.d("ReactNative", "checkConnection failed with Error: " + e.getMessage());
198
- promise.resolve(false);
199
- }
200
- } else {
201
- Log.d("ReactNative", "checkConnection failed with Error: " + "fula is null");
202
- promise.resolve(false);
203
- }
204
- });
205
- }
206
-
207
- @ReactMethod
208
- public void newClient(String identityString, String storePath, String bloxAddr, String exchange, boolean autoFlush, boolean useRelay, boolean refresh, Promise promise) {
209
- Log.d("ReactNative", "newClient started");
210
- ThreadUtils.runOnExecutor(() -> {
211
- try {
212
- Log.d("ReactNative", "newClient storePath= " + storePath + " bloxAddr= " + bloxAddr + " exchange= " + exchange + " autoFlush= " + autoFlush + " useRelay= " + useRelay + " refresh= " + refresh);
213
- byte[] identity = toByte(identityString);
214
- Log.d("ReactNative", "newClient identity= " + identityString);
215
- this.newClientInternal(identity, storePath, bloxAddr, exchange, autoFlush, useRelay, refresh);
216
- //String objString = Arrays.toString(obj);
217
- String peerId = this.fula.id();
218
- Log.d("ReactNative", "newClient peerId= " + peerId);
219
- promise.resolve(peerId);
220
- } catch (Exception e) {
221
- Log.d("ReactNative", "newClient failed with Error: " + e.getMessage());
222
- promise.reject("Error", e.getMessage());
223
- }
224
- });
225
- }
226
-
227
- @ReactMethod
228
- public void isReady(boolean filesystemCheck, Promise promise) {
229
- Log.d("ReactNative", "isReady started");
230
- ThreadUtils.runOnExecutor(() -> {
231
- boolean initialized = false;
232
- try {
233
- if (this.fula != null && this.fula.id() != null) {
234
- if (filesystemCheck) {
235
- if (this.client != null && this.rootConfig != null && !this.rootConfig.getCid().isEmpty()) {
236
- initialized = true;
237
- Log.d("ReactNative", "isReady is true with filesystem check");
238
- }
239
- } else {
240
- Log.d("ReactNative", "isReady is true without filesystem check");
241
- initialized = true;
242
- }
243
- }
244
- promise.resolve(initialized);
245
- } catch (Exception e) {
246
- Log.d("ReactNative", "isReady failed with Error: " + e.getMessage());
247
- promise.reject("Error", e.getMessage());
248
- }
249
- });
250
- }
251
-
252
- @ReactMethod
253
- public void init(String identityString, String storePath, String bloxAddr, String exchange, boolean autoFlush, String rootConfig, boolean useRelay, boolean refresh, Promise promise) {
254
- Log.d("ReactNative", "init started");
255
- ThreadUtils.runOnExecutor(() -> {
256
- try {
257
- WritableMap resultData = new WritableNativeMap();
258
- Log.d("ReactNative", "init storePath= " + storePath);
259
- byte[] identity = toByte(identityString);
260
- Log.d("ReactNative", "init identity= " + identityString);
261
- String[] obj = this.initInternal(identity, storePath, bloxAddr, exchange, autoFlush, rootConfig, useRelay, refresh);
262
- Log.d("ReactNative", "init object created: [ " + obj[0] + ", " + obj[1] + " ]");
263
- resultData.putString("peerId", obj[0]);
264
- resultData.putString("rootCid", obj[1]);
265
- promise.resolve(resultData);
266
- } catch (Exception e) {
267
- Log.d("ReactNative", "init failed with Error: " + e.getMessage());
268
- promise.reject("Error", e.getMessage());
269
- }
270
- });
271
- }
272
-
273
- @ReactMethod
274
- public void logout(String identityString, String storePath, Promise promise) {
275
- Log.d("ReactNative", "logout started");
276
- ThreadUtils.runOnExecutor(() -> {
277
- try {
278
- byte[] identity = toByte(identityString);
279
- boolean obj = this.logoutInternal(identity, storePath);
280
- Log.d("ReactNative", "logout completed");
281
- promise.resolve(obj);
282
- } catch (Exception e) {
283
- Log.d("ReactNative", "logout failed with Error: " + e.getMessage());
284
- promise.reject("Error", e.getMessage());
285
- }
286
- });
287
- }
288
-
289
- private boolean checkConnectionInternal(int timeout) throws Exception {
290
- try {
291
- Log.d("ReactNative", "checkConnectionInternal started");
292
- if (this.fula != null) {
293
- try {
294
- Log.d("ReactNative", "connectToBlox started");
295
-
296
- AtomicBoolean connectionStatus = new AtomicBoolean(false);
297
- ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
298
- Future<?> future = executor.submit(() -> {
299
- try {
300
- this.fula.connectToBlox();
301
- connectionStatus.set(true);
302
- Log.d("ReactNative", "checkConnectionInternal succeeded ");
303
- } catch (Exception e) {
304
- Log.d("ReactNative", "checkConnectionInternal failed with Error: " + e.getMessage());
305
- }
306
- });
307
-
308
- try {
309
- future.get(timeout, TimeUnit.SECONDS);
310
- } catch (TimeoutException te) {
311
- // If the timeout occurs, shut down the executor and return false
312
- executor.shutdownNow();
313
- return false;
314
- } finally {
315
- // If the future task is done, we can shut down the executor
316
- if (future.isDone()) {
317
- executor.shutdown();
318
- }
319
- }
320
-
321
- return connectionStatus.get();
322
- } catch (Exception e) {
323
- Log.d("ReactNative", "checkConnectionInternal failed with Error: " + e.getMessage());
324
- return false;
325
- }
326
- } else {
327
- Log.d("ReactNative", "checkConnectionInternal failed because fula is not initialized ");
328
- return false;
329
- }
330
- } catch (Exception e) {
331
- Log.d("ReactNative", "checkConnectionInternal failed with Error: " + e.getMessage());
332
- throw (e);
333
- }
334
- }
335
-
336
- @ReactMethod
337
- public void checkFailedActions(boolean retry, int timeout, Promise promise) throws Exception {
338
- try {
339
- if (this.fula != null) {
340
- if (!retry) {
341
- Log.d("ReactNative", "checkFailedActions without retry");
342
- fulamobile.LinkIterator failedLinks = this.fula.listFailedPushes();
343
- if (failedLinks.hasNext()) {
344
- Log.d("ReactNative", "checkFailedActions found: "+Arrays.toString(failedLinks.next()));
345
- promise.resolve(true);
346
- } else {
347
- promise.resolve(false);
348
- }
349
- } else {
350
- Log.d("ReactNative", "checkFailedActions with retry");
351
- boolean retryResults = this.retryFailedActionsInternal(timeout);
352
- promise.resolve(!retryResults);
353
- }
354
- } else {
355
- throw new Exception("Fula is not initialized");
356
- }
357
- } catch (Exception e) {
358
- Log.d("ReactNative", "checkFailedActions failed with Error: " + e.getMessage());
359
- throw (e);
360
- }
361
- }
362
-
363
- @ReactMethod
364
- private void listFailedActions(ReadableArray cids, Promise promise) throws Exception {
365
- try {
366
- if (this.fula != null) {
367
- Log.d("ReactNative", "listFailedActions");
368
- fulamobile.StringIterator failedLinks = this.fula.listFailedPushesAsString();
369
- ArrayList<String> failedLinksList = new ArrayList<>();
370
- while (failedLinks.hasNext()) {
371
- failedLinksList.add(failedLinks.next());
372
- }
373
- if (cids.size() > 0) {
374
- // If cids array is provided, filter the failedLinksList
375
- ArrayList<String> cidsList = new ArrayList<>();
376
- for (int i = 0; i < cids.size(); i++) {
377
- cidsList.add(cids.getString(i));
378
- }
379
- cidsList.retainAll(failedLinksList); // Keep only the elements in both cidsList and failedLinksList
380
- if (!cidsList.isEmpty()) {
381
- // If there are any matching cids, return them
382
- WritableArray cidsArray = Arguments.createArray();
383
- for (String cid : cidsList) {
384
- cidsArray.pushString(cid);
385
- }
386
- promise.resolve(cidsArray);
387
- } else {
388
- // If there are no matching cids, return false
389
- promise.resolve(false);
390
- }
391
- } else if (!failedLinksList.isEmpty()) {
392
- // If cids array is not provided, return the whole list
393
- Log.d("ReactNative", "listFailedActions found: "+ failedLinksList);
394
- WritableArray failedLinksArray = Arguments.createArray();
395
- for (String link : failedLinksList) {
396
- failedLinksArray.pushString(link);
397
- }
398
- promise.resolve(failedLinksArray);
399
- } else {
400
- promise.resolve(false);
401
- }
402
- } else {
403
- throw new Exception("listFailedActions: Fula is not initialized");
404
- }
405
- } catch (Exception e) {
406
- Log.d("ReactNative", "listFailedActions failed with Error: " + e.getMessage());
407
- throw (e);
408
- }
409
- }
410
-
411
-
412
-
413
-
414
- private boolean retryFailedActionsInternal(int timeout) throws Exception {
415
- try {
416
- Log.d("ReactNative", "retryFailedActionsInternal started");
417
- if (this.fula != null) {
418
- //Fula is initialized
419
- try {
420
- boolean connectionCheck = this.checkConnectionInternal(timeout);
421
- if(connectionCheck) {
422
- try {
423
- Log.d("ReactNative", "retryFailedPushes started");
424
- this.fula.retryFailedPushes();
425
- Log.d("ReactNative", "flush started");
426
- this.fula.flush();
427
- return true;
428
- }
429
- catch (Exception e) {
430
- this.fula.flush();
431
- Log.d("ReactNative", "retryFailedActionsInternal failed with Error: " + e.getMessage());
432
- return false;
433
- }
434
- //Blox online
435
- /*fulamobile.LinkIterator failedLinks = this.fula.listFailedPushes();
436
- if (failedLinks.hasNext()) {
437
- Log.d("ReactNative", "Failed links");
438
- //Failed list is not empty. iterate in the list
439
- while (failedLinks.hasNext()) {
440
- //Get the missing key
441
- byte[] failedNode = failedLinks.next();
442
- try {
443
- //Push to Blox
444
- Log.d("ReactNative", "Pushing Failed links "+Arrays.toString(failedNode));
445
- this.pushInternal(failedNode);
446
- Log.d("ReactNative", "Failed links pushed");
447
- }
448
- catch (Exception e) {
449
- Log.d("ReactNative", "retryFailedActionsInternal failed with Error: " + e.getMessage());
450
- }
451
- }
452
- //check if list is empty now and all are pushed
453
- Log.d("ReactNative", "Pushing finished");
454
- fulamobile.LinkIterator failedLinks_after = this.fula.listFailedPushes();
455
- if(failedLinks_after.hasNext()) {
456
- //Some pushes failed
457
- byte[] first_failed = failedLinks_after.next();
458
- Log.d("ReactNative", "Failed links are not empty "+Arrays.toString(first_failed));
459
- return false;
460
- } else {
461
- //All pushes successful
462
- return true;
463
- }
464
- } else {
465
- Log.d("ReactNative", "No Failed links");
466
- //Failed list is empty
467
- return true;
468
- }*/
469
- } else {
470
- Log.d("ReactNative", "retryFailedActionsInternal failed because blox is offline");
471
- //Blox Offline
472
- return false;
473
- }
474
- }
475
- catch (Exception e) {
476
- Log.d("ReactNative", "retryFailedActionsInternal failed with Error: " + e.getMessage());
477
- return false;
478
- }
479
- } else {
480
- Log.d("ReactNative", "retryFailedActionsInternal failed because fula is not initialized");
481
- //Fula is not initialized
482
- return false;
483
- }
484
- } catch (Exception e) {
485
- Log.d("ReactNative", "retryFailedActionsInternal failed with Error: " + e.getMessage());
486
- throw (e);
487
- }
488
- }
489
-
490
- @NonNull
491
- private byte[] createPeerIdentity(byte[] identity) throws GeneralSecurityException, IOException {
492
- try {
493
- // 1: First: create public key from provided private key
494
- // 2: Should read the local keychain store (if it is key-value, key is public key above,
495
- // 3: if found, decrypt using the private key
496
- // 4: If not found or decryption not successful, generate an identity
497
- // 5: then encrypt and store in keychain
498
- byte[] libp2pId;
499
- String encryptedLibp2pId = sharedPref.getValue(PRIVATE_KEY_STORE_PEERID);
500
- byte[] encryptionPair;
501
- SecretKey encryptionSecretKey;
502
- try {
503
- encryptionSecretKey = Cryptography.generateKey(identity);
504
- Log.d("ReactNative", "encryptionSecretKey generated from privateKey");
505
- } catch (Exception e) {
506
- Log.d("ReactNative", "Failed to generate key for encryption: " + e.getMessage());
507
- throw new GeneralSecurityException("Failed to generate key encryption", e);
508
- }
509
-
510
- if (encryptedLibp2pId == null || !encryptedLibp2pId.startsWith("FULA_" +
511
- "ENC_V4:")) {
512
- Log.d("ReactNative", "encryptedLibp2pId is not correct. creating new one " + encryptedLibp2pId);
513
-
514
- try {
515
- libp2pId = Fulamobile.generateEd25519KeyFromString(toString(identity));
516
- } catch (Exception e) {
517
- Log.d("ReactNative", "Failed to generate libp2pId: " + e.getMessage());
518
- throw new GeneralSecurityException("Failed to generate libp2pId", e);
519
- }
520
- encryptedLibp2pId = "FULA_ENC_V4:" + Cryptography.encryptMsg(StaticHelper.bytesToBase64(libp2pId), encryptionSecretKey, null);
521
- sharedPref.add(PRIVATE_KEY_STORE_PEERID, encryptedLibp2pId);
522
- } else {
523
- Log.d("ReactNative", "encryptedLibp2pId is correct. decrypting " + encryptedLibp2pId);
524
- }
525
-
526
- try {
527
- String decryptedLibp2pId = Cryptography.decryptMsg(encryptedLibp2pId.replace("FULA_ENC_V4:", ""), encryptionSecretKey);
528
-
529
- return StaticHelper.base64ToBytes(decryptedLibp2pId);
530
- } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException e) {
531
- Log.d("ReactNative", "createPeerIdentity decryptMsg failed with Error: " + e.getMessage());
532
- throw (e);
533
- }
534
-
535
- } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException e) {
536
- Log.d("ReactNative", "createPeerIdentity failed with Error: " + e.getMessage());
537
- throw (e);
538
- }
539
- }
540
-
541
- private void createNewRootConfig(FulaModule.Client iClient, byte[] identity) throws Exception {
542
- byte[] hash32 = getSHA256Hash(identity);
543
- this.rootConfig = Fs.init(iClient, hash32);
544
- Log.d("ReactNative", "rootConfig is created " + this.rootConfig.getCid());
545
- if (this.fula != null) {
546
- this.fula.flush();
547
- }
548
- this.encrypt_and_store_config();
549
- }
550
-
551
- public static byte[] getSHA256Hash(byte[] input) throws NoSuchAlgorithmException {
552
- MessageDigest md = MessageDigest.getInstance("SHA-256");
553
- return md.digest(input);
554
- }
555
-
556
- private static String bytesToHex(byte[] bytes) {
557
- StringBuilder result = new StringBuilder();
558
- for (byte b : bytes) {
559
- result.append(String.format("%02x", b));
560
- }
561
- return result.toString();
562
- }
563
-
564
- private void reloadFS(FulaModule.Client iClient, byte[] wnfsKey, String rootCid) throws Exception {
565
- Log.d("ReactNative", "reloadFS called: rootCid=" + rootCid);
566
- byte[] hash32 = getSHA256Hash(wnfsKey);
567
- Log.d("ReactNative", "wnfsKey=" + bytesToHex(wnfsKey) + "; hash32 = " + bytesToHex(hash32));
568
- Fs.loadWithWNFSKey(iClient, hash32, rootCid);
569
- Log.d("ReactNative", "reloadFS completed");
570
- }
571
-
572
- private boolean encrypt_and_store_config() throws Exception {
573
- try {
574
- if(this.identityEncryptedGlobal != null && !this.identityEncryptedGlobal.isEmpty()) {
575
- Log.d("ReactNative", "encrypt_and_store_config started");
576
-
577
- String cid_encrypted = Cryptography.encryptMsg(this.rootConfig.getCid(), this.secretKeyGlobal, null);
578
-
579
- sharedPref.add("FULA_ENC_V4:cid_encrypted_" + this.identityEncryptedGlobal, cid_encrypted);
580
- return true;
581
- } else {
582
- Log.d("ReactNative", "encrypt_and_store_config failed because identityEncryptedGlobal is empty");
583
- return false;
584
- }
585
- } catch (Exception e) {
586
- Log.d("ReactNative", "encrypt_and_store_config failed with Error: " + e.getMessage());
587
- throw (e);
588
- }
589
- }
590
-
591
- private boolean logoutInternal(byte[] identity, String storePath) throws Exception {
592
- try {
593
- if (this.fula != null) {
594
- this.fula.flush();
595
- }
596
- SecretKey secretKey = Cryptography.generateKey(identity);
597
- byte[] iv = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B };
598
- String identity_encrypted = Cryptography.encryptMsg(Arrays.toString(identity), secretKey, iv);
599
- sharedPref.remove("FULA_ENC_V4:cid_encrypted_"+ identity_encrypted);
600
-
601
- //TODO: Should also remove peerid @Mahdi
602
-
603
- sharedPref.remove("FULA_ENC_V4:cid_encrypted_"+ identity_encrypted);
604
-
605
- this.rootConfig = null;
606
- this.secretKeyGlobal = null;
607
- this.identityEncryptedGlobal = null;
608
-
609
- if (storePath == null || storePath.trim().isEmpty()) {
610
- storePath = this.fulaStorePath;
611
- }
612
-
613
- File file = new File(storePath);
614
- FileUtils.deleteDirectory(file);
615
- return true;
616
-
617
- } catch (Exception e) {
618
- Log.d("ReactNative", "logout internal failed with Error: " + e.getMessage());
619
- throw (e);
620
- }
621
- }
622
-
623
- public fulamobile.Client getFulaClient() {
624
- return this.fula;
625
- }
626
-
627
- @NonNull
628
- private byte[] newClientInternal(byte[] identity, String storePath, String bloxAddr, String exchange, boolean autoFlush, boolean useRelay, boolean refresh) throws GeneralSecurityException, IOException {
629
- byte[] peerIdentity = null;
630
- try {
631
- fulaConfig = new Config();
632
- if (storePath == null || storePath.trim().isEmpty()) {
633
- fulaConfig.setStorePath(this.fulaStorePath);
634
- } else {
635
- fulaConfig.setStorePath(storePath);
636
- }
637
- Log.d("ReactNative", "newClientInternal storePath is set: " + fulaConfig.getStorePath());
638
-
639
- peerIdentity = this.createPeerIdentity(identity);
640
- fulaConfig.setIdentity(peerIdentity);
641
- Log.d("ReactNative", "peerIdentity is set: " + toString(fulaConfig.getIdentity()));
642
- fulaConfig.setBloxAddr(bloxAddr);
643
- Log.d("ReactNative", "bloxAddr is set: " + fulaConfig.getBloxAddr());
644
- fulaConfig.setExchange(exchange);
645
- fulaConfig.setSyncWrites(autoFlush);
646
- if (useRelay) {
647
- fulaConfig.setAllowTransientConnection(true);
648
- fulaConfig.setForceReachabilityPrivate(true);
649
- }
650
- if (this.fula == null || refresh) {
651
- Log.d("ReactNative", "Creating a new Fula instance");
652
- try {
653
- shutdownInternal();
654
- this.fula = Fulamobile.newClient(fulaConfig);
655
- if (this.fula != null) {
656
- this.fula.flush();
657
- }
658
- } catch (Exception e) {
659
- Log.d("ReactNative", "Failed to create new Fula instance: " + e.getMessage());
660
- throw new IOException("Failed to create new Fula instance", e);
661
- }
662
- }
663
- } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException e) {
664
- Log.d("ReactNative", "newclientInternal failed with Error: " + e.getMessage());
665
- throw (e);
666
- }
667
- return peerIdentity;
668
- }
669
-
670
-
671
- @NonNull
672
- private String[] initInternal(byte[] identity, String storePath, String bloxAddr, String exchange, boolean autoFlush, String rootCid, boolean useRelay, boolean refresh) throws Exception {
673
- try {
674
- if (this.fula == null || refresh) {
675
- this.newClientInternal(identity, storePath, bloxAddr, exchange, autoFlush, useRelay, refresh);
676
- }
677
- if(this.client == null || refresh) {
678
- this.client = new Client(this.fula);
679
- Log.d("ReactNative", "fula initialized: " + this.fula.id());
680
- }
681
-
682
- SecretKey secretKey = Cryptography.generateKey(identity);
683
- Log.d("ReactNative", "secretKey generated: " + secretKey.toString());
684
- byte[] iv = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B };
685
- String identity_encrypted =Cryptography.encryptMsg(Arrays.toString(identity), secretKey, iv);
686
- Log.d("ReactNative", "identity_encrypted generated: " + identity_encrypted + " for identity: " + Arrays.toString(identity));
687
- this.identityEncryptedGlobal = identity_encrypted;
688
- this.secretKeyGlobal = secretKey;
689
-
690
- if ( this.rootConfig == null || this.rootConfig.getCid().isEmpty() ) {
691
- Log.d("ReactNative", "this.rootCid is empty.");
692
- //Load from keystore
693
-
694
- String cid_encrypted_fetched = sharedPref.getValue("FULA_ENC_V4:cid_encrypted_"+ identity_encrypted);
695
- Log.d("ReactNative", "Here1");
696
- String cid = "";
697
- if(cid_encrypted_fetched != null && !cid_encrypted_fetched.isEmpty()) {
698
- Log.d("ReactNative", "decrypting cid="+cid_encrypted_fetched+" with secret="+secretKey.toString());
699
- cid = Cryptography.decryptMsg(cid_encrypted_fetched, secretKey);
700
- }
701
-
702
- Log.d("ReactNative", "Here2");
703
- //Log.d("ReactNative", "Attempted to fetch cid from keystore; cid="+cid);
704
- if(cid == null || cid.isEmpty()) {
705
- Log.d("ReactNative", "cid was not found");
706
- if(rootCid != null && !rootCid.isEmpty()){
707
- Log.d("ReactNative", "Re-setting cid from input: "+rootCid);
708
- cid = rootCid;
709
- }
710
- if(cid == null || cid.isEmpty()) {
711
- Log.d("ReactNative", "Tried to recover cid but was not successful. Creating new ones");
712
- this.createNewRootConfig(this.client, identity);
713
- }
714
- } else {
715
- Log.d("ReactNative", "Recovered cid and private ref from keychain store. cid="+cid +" and cid from input was: "+rootCid);
716
- this.rootConfig = new land.fx.wnfslib.Config(cid);
717
- this.reloadFS(this.client, identity, cid);
718
- this.encrypt_and_store_config();
719
- }
720
-
721
- Log.d("ReactNative", "creating rootConfig completed");
722
-
723
- Log.d("ReactNative", "rootConfig is created: cid=" + this.rootConfig.getCid());
724
- } else {
725
- Log.d("ReactNative", "rootConfig existed: cid=" + this.rootConfig.getCid());
726
- }
727
- String peerId = this.fula.id();
728
- String[] obj = new String[2];
729
- obj[0] = peerId;
730
- obj[1] = this.rootConfig.getCid();
731
- Log.d("ReactNative", "initInternal is completed successfully");
732
- if (this.fula != null) {
733
- this.fula.flush();
734
- }
735
- return obj;
736
- } catch (Exception e) {
737
- Log.d("ReactNative", "init internal failed with Error: " + e.getMessage());
738
- throw (e);
739
- }
740
- }
741
-
742
- @ReactMethod
743
- public void mkdir(String path, Promise promise) {
744
- ThreadUtils.runOnExecutor(() -> {
745
- Log.d("ReactNative", "mkdir started with: path = " + path + " rootConfig.getCid() = " + this.rootConfig.getCid());
746
- try {
747
- land.fx.wnfslib.Config config = Fs.mkdir(this.client, this.rootConfig.getCid(), path);
748
- if(config != null) {
749
- this.rootConfig = config;
750
- this.encrypt_and_store_config();
751
- if (this.fula != null) {
752
- this.fula.flush();
753
- }
754
- String rootCid = this.rootConfig.getCid();
755
- Log.d("ReactNative", "mkdir completed successfully with rootCid = " + rootCid);
756
- promise.resolve(rootCid);
757
- } else {
758
- Log.d("ReactNative", "mkdir Error: config is null");
759
- promise.reject(new Exception("mkdir Error: config is null"));
760
- }
761
- } catch (Exception e) {
762
- Log.d("get", e.getMessage());
763
- promise.reject(e);
764
- }
765
- });
766
- }
767
-
768
- @ReactMethod
769
- public void writeFile(String fulaTargetFilename, String localFilename, Promise promise) {
770
- /*
771
- // reads content of the file form localFilename (should include full absolute path to local file with read permission
772
- // writes content to the specified location by fulaTargetFilename in Fula filesystem
773
- // fulaTargetFilename: a string including full path and filename of target file on Fula (e.g. root/pictures/cat.jpg)
774
- // localFilename: a string containing full path and filename of local file on hte device (e.g /usr/bin/cat.jpg)
775
- // Returns: new cid of the root after this file is placed in the tree
776
- */
777
- ThreadUtils.runOnExecutor(() -> {
778
- Log.d("ReactNative", "writeFile to : path = " + fulaTargetFilename + ", from: " + localFilename);
779
- try {
780
- if (this.client != null) {
781
- Log.d("ReactNative", "writeFileFromPath started: this.rootConfig.getCid=" + this.rootConfig.getCid()+ ", fulaTargetFilename="+fulaTargetFilename + ", localFilename="+localFilename);
782
- land.fx.wnfslib.Config config = Fs.writeFileStreamFromPath(this.client, this.rootConfig.getCid(), fulaTargetFilename, localFilename);
783
- if(config != null) {
784
- this.rootConfig = config;
785
- this.encrypt_and_store_config();
786
- if (this.fula != null) {
787
- this.fula.flush();
788
- String rootCid = this.rootConfig.getCid();
789
- Log.d("ReactNative", "writeFileFromPath completed: this.rootConfig.getCid=" + rootCid);
790
- promise.resolve(rootCid);
791
- } else {
792
- Log.d("ReactNative", "writeFile Error: fula is null");
793
- promise.reject(new Exception("writeFile Error: fula is null"));
794
- }
795
- } else {
796
- Log.d("ReactNative", "writeFile Error: config is null");
797
- promise.reject(new Exception("writeFile Error: config is null"));
798
- }
799
- } else {
800
- Log.d("ReactNative", "writeFile Error: client is null");
801
- promise.reject(new Exception("writeFile Error: client is null"));
802
- }
803
- } catch (Exception e) {
804
- Log.d("get", e.getMessage());
805
- promise.reject(e);
806
- }
807
- });
808
- }
809
-
810
- @ReactMethod
811
- public void writeFileContent(String path, String contentString, Promise promise) {
812
- ThreadUtils.runOnExecutor(() -> {
813
- Log.d("ReactNative", "writeFile: contentString = " + contentString);
814
- Log.d("ReactNative", "writeFile: path = " + path);
815
- try {
816
- byte[] content = this.convertStringToByte(contentString);
817
- land.fx.wnfslib.Config config = Fs.writeFile(this.client, this.rootConfig.getCid(), path, content);
818
- this.rootConfig = config;
819
- this.encrypt_and_store_config();
820
- if (this.fula != null) {
821
- this.fula.flush();
822
- }
823
- promise.resolve(config.getCid());
824
- } catch (Exception e) {
825
- Log.d("get", e.getMessage());
826
- promise.reject(e);
827
- }
828
- });
829
- }
830
-
831
- @ReactMethod
832
- public void ls(String path, Promise promise) {
833
- ThreadUtils.runOnExecutor(() -> {
834
- Log.d("ReactNative", "ls: path = " + path);
835
- try {
836
- byte[] res = Fs.ls(this.client, this.rootConfig.getCid(), path);
837
-
838
- String s = new String(res, StandardCharsets.UTF_8);
839
- Log.d("ReactNative", "ls: res = " + s);
840
- promise.resolve(s);
841
- } catch (Exception e) {
842
- Log.d("get", e.getMessage());
843
- promise.reject(e);
844
- }
845
- });
846
- }
847
-
848
- @ReactMethod
849
- public void rm(String path, Promise promise) {
850
- ThreadUtils.runOnExecutor(() -> {
851
- Log.d("ReactNative", "rm: path = " + path + ", beginning rootCid=" + this.rootConfig.getCid());
852
- try {
853
- land.fx.wnfslib.Config config = Fs.rm(this.client, this.rootConfig.getCid(), path);
854
- if(config != null) {
855
- this.rootConfig = config;
856
- this.encrypt_and_store_config();
857
- if (this.fula != null) {
858
- this.fula.flush();
859
- }
860
- String rootCid = config.getCid();
861
- Log.d("ReactNative", "rm: returned rootCid = " + rootCid);
862
- promise.resolve(rootCid);
863
- } else {
864
- Log.d("ReactNative", "rm Error: config is null");
865
- promise.reject(new Exception("rm Error: config is null"));
866
- }
867
- } catch (Exception e) {
868
- Log.d("get", e.getMessage());
869
- promise.reject(e);
870
- }
871
- });
872
- }
873
-
874
- @ReactMethod
875
- public void cp(String sourcePath, String targetPath, Promise promise) {
876
- ThreadUtils.runOnExecutor(() -> {
877
- Log.d("ReactNative", "rm: sourcePath = " + sourcePath);
878
- try {
879
- land.fx.wnfslib.Config config = Fs.cp(this.client, this.rootConfig.getCid(), sourcePath, targetPath);
880
- if(config != null) {
881
- this.rootConfig = config;
882
- this.encrypt_and_store_config();
883
- if (this.fula != null) {
884
- this.fula.flush();
885
- }
886
- promise.resolve(config.getCid());
887
- } else {
888
- Log.d("ReactNative", "cp Error: config is null");
889
- promise.reject(new Exception("cp Error: config is null"));
890
- }
891
- } catch (Exception e) {
892
- Log.d("get", e.getMessage());
893
- promise.reject(e);
894
- }
895
- });
896
- }
897
-
898
- @ReactMethod
899
- public void mv(String sourcePath, String targetPath, Promise promise) {
900
- ThreadUtils.runOnExecutor(() -> {
901
- Log.d("ReactNative", "rm: sourcePath = " + sourcePath);
902
- try {
903
- land.fx.wnfslib.Config config = Fs.mv(this.client, this.rootConfig.getCid(), sourcePath, targetPath);
904
- if(config != null) {
905
- this.rootConfig = config;
906
- this.encrypt_and_store_config();
907
- if (this.fula != null) {
908
- this.fula.flush();
909
- }
910
- promise.resolve(config.getCid());
911
- } else {
912
- Log.d("ReactNative", "mv Error: config is null");
913
- promise.reject(new Exception("mv Error: config is null"));
914
- }
915
- } catch (Exception e) {
916
- Log.d("get", e.getMessage());
917
- promise.reject(e);
918
- }
919
- });
920
- }
921
-
922
- @ReactMethod
923
- public void readFile(String fulaTargetFilename, String localFilename, Promise promise) {
924
- /*
925
- // reads content of the file form localFilename (should include full absolute path to local file with read permission
926
- // writes content to the specified location by fulaTargetFilename in Fula filesystem
927
- // fulaTargetFilename: a string including full path and filename of target file on Fula (e.g. root/pictures/cat.jpg)
928
- // localFilename: a string containing full path and filename of local file on hte device (e.g /usr/bin/cat.jpg)
929
- // Returns: new cid of the root after this file is placed in the tree
930
- */
931
- ThreadUtils.runOnExecutor(() -> {
932
- Log.d("ReactNative", "readFile: fulaTargetFilename = " + fulaTargetFilename);
933
- try {
934
- Log.d("ReactNative", "readFile: localFilename = " + localFilename + " fulaTargetFilename = " + fulaTargetFilename + " beginning rootCid = " + this.rootConfig.getCid());
935
- String path = Fs.readFilestreamToPath(this.client, this.rootConfig.getCid(), fulaTargetFilename, localFilename);
936
- promise.resolve(path);
937
- } catch (Exception e) {
938
- Log.d("get", e.getMessage());
939
- promise.reject(e);
940
- }
941
- });
942
- }
943
-
944
- @ReactMethod
945
- public void readFileContent(String path, Promise promise) {
946
- ThreadUtils.runOnExecutor(() -> {
947
- Log.d("ReactNative", "readFileContent: path = " + path);
948
- try {
949
- byte[] res = Fs.readFile(this.client, this.rootConfig.getCid(), path);
950
- String resString = toString(res);
951
- promise.resolve(resString);
952
- } catch (Exception e) {
953
- Log.d("get", e.getMessage());
954
- promise.reject(e);
955
- }
956
- });
957
- }
958
-
959
- @ReactMethod
960
- public void get(String keyString, Promise promise) {
961
- ThreadUtils.runOnExecutor(() -> {
962
- Log.d("ReactNative", "get: keyString = " + keyString);
963
- try {
964
- byte[] key = this.convertStringToByte(keyString);
965
- byte[] value = this.getInternal(key);
966
- String valueString = toString(value);
967
- promise.resolve(valueString);
968
- } catch (Exception e) {
969
- Log.d("get", e.getMessage());
970
- promise.reject(e);
971
- }
972
- });
973
- }
974
-
975
- @NonNull
976
- private byte[] getInternal(byte[] key) throws Exception {
977
- try {
978
- Log.d("ReactNative", "getInternal: key.toString() = " + toString(key));
979
- Log.d("ReactNative", "getInternal: key.toString().bytes = " + Arrays.toString(key));
980
- byte[] value = this.fula.get(key);
981
- Log.d("ReactNative", "getInternal: value.toString() = " + toString(value));
982
- return value;
983
- } catch (Exception e) {
984
- Log.d("ReactNative", "getInternal: error = " + e.getMessage());
985
- Log.d("getInternal", e.getMessage());
986
- throw (e);
987
- }
988
- }
989
-
990
- @ReactMethod
991
- public void has(String keyString, Promise promise) {
992
- ThreadUtils.runOnExecutor(() -> {
993
- Log.d("ReactNative", "has: keyString = " + keyString);
994
- try {
995
- byte[] key = this.convertStringToByte(keyString);
996
- boolean result = this.hasInternal(key);
997
- promise.resolve(result);
998
- } catch (Exception e) {
999
- Log.d("get", e.getMessage());
1000
- promise.reject(e);
1001
- }
1002
- });
1003
- }
1004
-
1005
- private boolean hasInternal(byte[] key) throws Exception {
1006
- try {
1007
- boolean res = this.fula.has(key);
1008
- return res;
1009
- } catch (Exception e) {
1010
- Log.d("hasInternal", e.getMessage());
1011
- throw (e);
1012
- }
1013
- }
1014
-
1015
- private void pullInternal(byte[] key) throws Exception {
1016
- try {
1017
- this.fula.pull(key);
1018
- } catch (Exception e) {
1019
- Log.d("pullInternal", e.getMessage());
1020
- throw (e);
1021
- }
1022
- }
1023
-
1024
- @ReactMethod
1025
- public void push(Promise promise) {
1026
- ThreadUtils.runOnExecutor(() -> {
1027
- Log.d("ReactNative", "push started");
1028
- try {
1029
- this.pushInternal(this.convertStringToByte(this.rootConfig.getCid()));
1030
- promise.resolve(this.rootConfig.getCid());
1031
- } catch (Exception e) {
1032
- Log.d("get", e.getMessage());
1033
- promise.reject(e);
1034
- }
1035
- });
1036
- }
1037
-
1038
- private void pushInternal(byte[] key) throws Exception {
1039
- try {
1040
- if (this.fula != null && this.fula.has(key)) {
1041
- this.fula.push(key);
1042
- this.fula.flush();
1043
- } else {
1044
- Log.d("ReactNative", "pushInternal error: key wasn't found or fula is not initialized");
1045
- throw new Exception("key wasn't found in local storage");
1046
- }
1047
- } catch (Exception e) {
1048
- Log.d("ReactNative", "pushInternal"+ e.getMessage());
1049
- throw (e);
1050
- }
1051
- }
1052
-
1053
- @ReactMethod
1054
- public void put(String valueString, String codecString, Promise promise) {
1055
- ThreadUtils.runOnExecutor(() -> {
1056
- Log.d("ReactNative", "put: codecString = " + codecString);
1057
- Log.d("ReactNative", "put: valueString = " + valueString);
1058
- try {
1059
- //byte[] codec = this.convertStringToByte(CodecString);
1060
- long codec = Long.parseLong(codecString);
1061
-
1062
-
1063
- Log.d("ReactNative", "put: codec = " + codec);
1064
- byte[] value = toByte(valueString);
1065
-
1066
- Log.d("ReactNative", "put: value.toString() = " + toString(value));
1067
- byte[] key = this.putInternal(value, codec);
1068
- Log.d("ReactNative", "put: key.toString() = " + toString(key));
1069
- promise.resolve(toString(key));
1070
- } catch (Exception e) {
1071
- Log.d("ReactNative", "put: error = " + e.getMessage());
1072
- promise.reject(e);
1073
- }
1074
- });
1075
- }
1076
-
1077
- @NonNull
1078
- private byte[] putInternal(byte[] value, long codec) throws Exception {
1079
- try {
1080
- if(this.fula != null) {
1081
- byte[] key = this.fula.put(value, codec);
1082
- this.fula.flush();
1083
- return key;
1084
- } else {
1085
- Log.d("ReactNative", "putInternal Error: fula is not initialized");
1086
- throw (new Exception("putInternal Error: fula is not initialized"));
1087
- }
1088
- } catch (Exception e) {
1089
- Log.d("ReactNative", "putInternal"+ e.getMessage());
1090
- throw (e);
1091
- }
1092
- }
1093
-
1094
- @ReactMethod
1095
- public void setAuth(String peerIdString, boolean allow, Promise promise) {
1096
- ThreadUtils.runOnExecutor(() -> {
1097
- Log.d("ReactNative", "setAuth: peerIdString = " + peerIdString);
1098
- try {
1099
- if (this.fula != null && this.fula.id() != null && this.fulaConfig != null && this.fulaConfig.getBloxAddr() != null) {
1100
- String bloxAddr = this.fulaConfig.getBloxAddr();
1101
- Log.d("ReactNative", "setAuth: bloxAddr = '" + bloxAddr+"'"+ " peerIdString = '" + peerIdString+"'");
1102
- int index = bloxAddr.lastIndexOf("/");
1103
- String bloxPeerId = bloxAddr.substring(index + 1);
1104
- this.fula.setAuth(bloxPeerId, peerIdString, allow);
1105
- promise.resolve(true);
1106
- } else {
1107
- Log.d("ReactNative", "setAuth error: fula is not initialized");
1108
- throw new Exception("fula is not initialized");
1109
- }
1110
- promise.resolve(false);
1111
- } catch (Exception e) {
1112
- Log.d("get", e.getMessage());
1113
- promise.reject(e);
1114
- }
1115
- });
1116
- }
1117
-
1118
- private void shutdownInternal() throws Exception {
1119
- try {
1120
- if(this.fula != null) {
1121
- this.fula.shutdown();
1122
- this.fula = null;
1123
- this.client = null;
1124
- }
1125
- } catch (Exception e) {
1126
- Log.d("ReactNative", "shutdownInternal"+ e.getMessage());
1127
- throw (e);
1128
- }
1129
- }
1130
-
1131
- @ReactMethod
1132
- public void shutdown(Promise promise) {
1133
- ThreadUtils.runOnExecutor(() -> {
1134
- try {
1135
- shutdownInternal();
1136
- promise.resolve(true);
1137
- } catch (Exception e) {
1138
- promise.reject(e);
1139
- Log.d("ReactNative", "shutdown"+ e.getMessage());
1140
- }
1141
- });
1142
- }
1143
-
1144
- ///////////////////////////////////////////////////////////
1145
- ///////////////////////////////////////////////////////////
1146
- ///////////////////////////////////////////////////////////
1147
- ///////////////////////////////////////////////////////////
1148
- //////////////////////ANYTHING BELOW IS FOR BLOCKCHAIN/////
1149
- ///////////////////////////////////////////////////////////
1150
- @ReactMethod
1151
- public void createAccount(String seedString, Promise promise) {
1152
- ThreadUtils.runOnExecutor(() -> {
1153
- Log.d("ReactNative", "createAccount: seedString = " + seedString);
1154
- try {
1155
- if (this.fula == null || this.fula.id() == null || this.fula.id().isEmpty()) {
1156
- promise.reject(new Error("Fula client is not initialized"));
1157
- } else {
1158
-
1159
- if (!seedString.startsWith("/")) {
1160
- promise.reject(new Error("seed should start with /"));
1161
- }
1162
- byte[] result = this.fula.seeded(seedString);
1163
- String resultString = toString(result);
1164
- promise.resolve(resultString);
1165
- }
1166
- } catch (Exception e) {
1167
- Log.d("get", e.getMessage());
1168
- promise.reject(e);
1169
- }
1170
- });
1171
- }
1172
-
1173
- @ReactMethod
1174
- public void checkAccountExists(String accountString, Promise promise) {
1175
- ThreadUtils.runOnExecutor(() -> {
1176
- Log.d("ReactNative", "checkAccountExists: accountString = " + accountString);
1177
- try {
1178
- byte[] result = this.fula.accountExists(accountString);
1179
- String resultString = toString(result);
1180
- promise.resolve(resultString);
1181
- } catch (Exception e) {
1182
- Log.d("get", e.getMessage());
1183
- promise.reject(e);
1184
- }
1185
- });
1186
- }
1187
-
1188
- @ReactMethod
1189
- public void createPool(String seedString, String poolName, Promise promise) {
1190
- ThreadUtils.runOnExecutor(() -> {
1191
- Log.d("ReactNative", "createPool: seedString = " + seedString + "; poolName = " + poolName);
1192
- try {
1193
- byte[] result = this.fula.poolCreate(seedString, poolName);
1194
- String resultString = toString(result);
1195
- promise.resolve(resultString);
1196
- } catch (Exception e) {
1197
- Log.d("get", e.getMessage());
1198
- promise.reject(e);
1199
- }
1200
- });
1201
- }
1202
-
1203
- @ReactMethod
1204
- public void listPools(Promise promise) {
1205
- ThreadUtils.runOnExecutor(() -> {
1206
- Log.d("ReactNative", "listPools");
1207
- try {
1208
- byte[] result = this.fula.poolList();
1209
- String resultString = toString(result);
1210
- promise.resolve(resultString);
1211
- } catch (Exception e) {
1212
- Log.d("get", e.getMessage());
1213
- promise.reject(e);
1214
- }
1215
- });
1216
- }
1217
-
1218
- @ReactMethod
1219
- public void joinPool(String seedString, long poolID, Promise promise) {
1220
- ThreadUtils.runOnExecutor(() -> {
1221
- Log.d("ReactNative", "joinPool: seedString = " + seedString + "; poolID = " + poolID);
1222
- try {
1223
- byte[] result = this.fula.poolJoin(seedString, poolID);
1224
- String resultString = toString(result);
1225
- promise.resolve(resultString);
1226
- } catch (Exception e) {
1227
- Log.d("get", e.getMessage());
1228
- promise.reject(e);
1229
- }
1230
- });
1231
- }
1232
-
1233
- @ReactMethod
1234
- public void cancelPoolJoin(String seedString, long poolID, Promise promise) {
1235
- ThreadUtils.runOnExecutor(() -> {
1236
- Log.d("ReactNative", "cancelPoolJoin: seedString = " + seedString + "; poolID = " + poolID);
1237
- try {
1238
- byte[] result = this.fula.poolCancelJoin(seedString, poolID);
1239
- String resultString = toString(result);
1240
- promise.resolve(resultString);
1241
- } catch (Exception e) {
1242
- Log.d("get", e.getMessage());
1243
- promise.reject(e);
1244
- }
1245
- });
1246
- }
1247
-
1248
- @ReactMethod
1249
- public void listPoolJoinRequests(long poolID, Promise promise) {
1250
- ThreadUtils.runOnExecutor(() -> {
1251
- Log.d("ReactNative", "listPoolJoinRequests: poolID = " + poolID);
1252
- try {
1253
- byte[] result = this.fula.poolRequests(poolID);
1254
- String resultString = toString(result);
1255
- promise.resolve(resultString);
1256
- } catch (Exception e) {
1257
- Log.d("get", e.getMessage());
1258
- promise.reject(e);
1259
- }
1260
- });
1261
- }
1262
-
1263
- @ReactMethod
1264
- public void votePoolJoinRequest(String seedString, long poolID, String accountString, boolean accept, Promise promise) {
1265
- ThreadUtils.runOnExecutor(() -> {
1266
- Log.d("ReactNative", "votePoolJoinRequest: seedString = " + seedString + "; poolID = " + poolID + "; accountString = " + accountString + "; accept = " + accept);
1267
- try {
1268
- byte[] result = this.fula.poolVote(seedString, poolID, accountString, accept);
1269
- String resultString = toString(result);
1270
- promise.resolve(resultString);
1271
- } catch (Exception e) {
1272
- Log.d("get", e.getMessage());
1273
- promise.reject(e);
1274
- }
1275
- });
1276
- }
1277
-
1278
- @ReactMethod
1279
- public void leavePool(String seedString, long poolID, Promise promise) {
1280
- ThreadUtils.runOnExecutor(() -> {
1281
- Log.d("ReactNative", "leavePool: seedString = " + seedString + "; poolID = " + poolID);
1282
- try {
1283
- byte[] result = this.fula.poolLeave(seedString, poolID);
1284
- String resultString = toString(result);
1285
- promise.resolve(resultString);
1286
- } catch (Exception e) {
1287
- Log.d("get", e.getMessage());
1288
- promise.reject(e);
1289
- }
1290
- });
1291
- }
1292
-
1293
- @ReactMethod
1294
- public void newReplicationRequest(String seedString, long poolID, long replicationFactor, String cid, Promise promise) {
1295
- ThreadUtils.runOnExecutor(() -> {
1296
- Log.d("ReactNative", "newReplicationRequest: seedString = " + seedString + "; poolID = " + poolID + "; replicationFactor = " + replicationFactor + "; cid = " + cid);
1297
- try {
1298
- byte[] result = this.fula.manifestUpload(seedString, poolID, replicationFactor, cid);
1299
- String resultString = toString(result);
1300
- promise.resolve(resultString);
1301
- } catch (Exception e) {
1302
- Log.d("get", e.getMessage());
1303
- promise.reject(e);
1304
- }
1305
- });
1306
- }
1307
-
1308
- @ReactMethod
1309
- public void newStoreRequest(String seedString, long poolID, String uploader, String cid, Promise promise) {
1310
- ThreadUtils.runOnExecutor(() -> {
1311
- Log.d("ReactNative", "newStoreRequest: seedString = " + seedString + "; poolID = " + poolID + "; uploader = " + uploader + "; cid = " + cid);
1312
- try {
1313
- byte[] result = this.fula.manifestStore(seedString, poolID, uploader, cid);
1314
- String resultString = toString(result);
1315
- promise.resolve(resultString);
1316
- } catch (Exception e) {
1317
- Log.d("get", e.getMessage());
1318
- promise.reject(e);
1319
- }
1320
- });
1321
- }
1322
-
1323
- @ReactMethod
1324
- public void listAvailableReplicationRequests(long poolID, Promise promise) {
1325
- ThreadUtils.runOnExecutor(() -> {
1326
- Log.d("ReactNative", "listAvailableReplicationRequests: poolID = " + poolID);
1327
- try {
1328
- byte[] result = this.fula.manifestAvailable(poolID);
1329
- String resultString = toString(result);
1330
- promise.resolve(resultString);
1331
- } catch (Exception e) {
1332
- Log.d("get", e.getMessage());
1333
- promise.reject(e);
1334
- }
1335
- });
1336
- }
1337
-
1338
- @ReactMethod
1339
- public void removeReplicationRequest(String seedString, long poolID, String cid, Promise promise) {
1340
- ThreadUtils.runOnExecutor(() -> {
1341
- Log.d("ReactNative", "newReplicationRequest: seedString = " + seedString + "; poolID = " + poolID + "; cid = " + cid);
1342
- try {
1343
- byte[] result = this.fula.manifestRemove(seedString, poolID, cid);
1344
- String resultString = toString(result);
1345
- promise.resolve(resultString);
1346
- } catch (Exception e) {
1347
- Log.d("get", e.getMessage());
1348
- promise.reject(e);
1349
- }
1350
- });
1351
- }
1352
-
1353
- @ReactMethod
1354
- public void removeStorer(String seedString, String storage, long poolID, String cid, Promise promise) {
1355
- ThreadUtils.runOnExecutor(() -> {
1356
- Log.d("ReactNative", "removeStorer: seedString = " + seedString + "; storage = " + storage + "; poolID = " + poolID + "; cid = " + cid);
1357
- try {
1358
- byte[] result = this.fula.manifestRemoveStorer(seedString, storage, poolID, cid);
1359
- String resultString = toString(result);
1360
- promise.resolve(resultString);
1361
- } catch (Exception e) {
1362
- Log.d("get", e.getMessage());
1363
- promise.reject(e);
1364
- }
1365
- });
1366
- }
1367
-
1368
- @ReactMethod
1369
- public void removeStoredReplication(String seedString, String uploader, long poolID, String cid, Promise promise) {
1370
- ThreadUtils.runOnExecutor(() -> {
1371
- Log.d("ReactNative", "removeStoredReplication: seedString = " + seedString + "; uploader = " + uploader + "; poolID = " + poolID + "; cid = " + cid);
1372
- try {
1373
- byte[] result = this.fula.manifestRemoveStored(seedString, uploader, poolID, cid);
1374
- String resultString = toString(result);
1375
- promise.resolve(resultString);
1376
- } catch (Exception e) {
1377
- Log.d("get", e.getMessage());
1378
- promise.reject(e);
1379
- }
1380
- });
1381
- }
1382
-
1383
- ////////////////////////////////////////////////////////////////
1384
- ///////////////// Blox Hardware Methods ////////////////////////
1385
- ////////////////////////////////////////////////////////////////
1386
-
1387
- @ReactMethod
1388
- public void bloxFreeSpace(Promise promise) {
1389
- ThreadUtils.runOnExecutor(() -> {
1390
- Log.d("ReactNative", "bloxFreeSpace");
1391
- try {
1392
- byte[] result = this.fula.bloxFreeSpace();
1393
- String resultString = toString(result);
1394
- Log.d("ReactNative", "result string="+resultString);
1395
- promise.resolve(resultString);
1396
- } catch (Exception e) {
1397
- Log.d("ReactNative", e.getMessage());
1398
- promise.reject(e);
1399
- }
1400
- });
1401
- }
1402
-
1403
- @ReactMethod
1404
- public void wifiRemoveall(Promise promise) {
1405
- ThreadUtils.runOnExecutor(() -> {
1406
- Log.d("ReactNative", "wifiRemoveall");
1407
- try {
1408
- byte[] result = this.fula.wifiRemoveall();
1409
- String resultString = toString(result);
1410
- Log.d("ReactNative", "result string="+resultString);
1411
- promise.resolve(resultString);
1412
- } catch (Exception e) {
1413
- Log.d("ReactNative", e.getMessage());
1414
- promise.reject(e);
1415
- }
1416
- });
1417
- }
1418
-
1419
- @ReactMethod
1420
- public void reboot(Promise promise) {
1421
- ThreadUtils.runOnExecutor(() -> {
1422
- Log.d("ReactNative", "reboot");
1423
- try {
1424
- byte[] result = this.fula.reboot();
1425
- String resultString = toString(result);
1426
- Log.d("ReactNative", "result string="+resultString);
1427
- promise.resolve(resultString);
1428
- } catch (Exception e) {
1429
- Log.d("ReactNative", e.getMessage());
1430
- promise.reject(e);
1431
- }
1432
- });
1433
- }
1434
-
1435
- @ReactMethod
1436
- public void testData(String identityString, String bloxAddr, Promise promise) {
1437
- try {
1438
- byte[] identity = toByte(identityString);
1439
- byte[] peerIdByte = this.newClientInternal(identity, "", bloxAddr, "", true, true, true);
1440
-
1441
- String peerIdReturned = Arrays.toString(peerIdByte);
1442
- Log.d("ReactNative", "newClient peerIdReturned= " + peerIdReturned);
1443
- String peerId = this.fula.id();
1444
- Log.d("ReactNative", "newClient peerId= " + peerId);
1445
- byte[] bytes = {1, 85, 18, 32, 11, -31, 75, -78, -109, 11, -111, 97, -47, -78, -22, 84, 39, -117, -64, -70, -91, 55, -23, -80, 116, -123, -97, -26, 126, -70, -76, 35, 54, -106, 55, -9};
1446
-
1447
- byte[] key = this.fula.put(bytes, 85);
1448
- Log.d("ReactNative", "testData put result string="+Arrays.toString(key));
1449
-
1450
- byte[] value = this.fula.get(key);
1451
- Log.d("ReactNative", "testData get result string="+Arrays.toString(value));
1452
-
1453
- this.fula.push(key);
1454
- //this.fula.flush();
1455
-
1456
- byte[] fetchedVal = this.fula.get(key);
1457
- this.fula.pull(key);
1458
-
1459
- promise.resolve(Arrays.toString(fetchedVal));
1460
- } catch (Exception e) {
1461
- Log.d("ReactNative", "ERROR:" + e.getMessage());
1462
- promise.reject(e);
1463
- }
1464
- }
1465
-
1466
- }
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
+ import com.facebook.react.bridge.Arguments;
15
+ import com.facebook.react.bridge.WritableArray;
16
+ import com.facebook.react.bridge.ReadableArray;
17
+
18
+
19
+ import org.apache.commons.io.FileUtils;
20
+ import org.jetbrains.annotations.Contract;
21
+
22
+ import java.io.File;
23
+ import java.io.IOException;
24
+ import java.nio.charset.StandardCharsets;
25
+ import java.security.GeneralSecurityException;
26
+ import java.security.InvalidAlgorithmParameterException;
27
+ import java.security.InvalidKeyException;
28
+ import java.security.NoSuchAlgorithmException;
29
+ import java.security.MessageDigest;
30
+ import java.util.Arrays;
31
+ import java.util.ArrayList;
32
+
33
+ import javax.crypto.BadPaddingException;
34
+ import javax.crypto.IllegalBlockSizeException;
35
+ import javax.crypto.NoSuchPaddingException;
36
+ import javax.crypto.SecretKey;
37
+
38
+ import java.util.concurrent.Executors;
39
+ import java.util.concurrent.ScheduledExecutorService;
40
+ import java.util.concurrent.TimeUnit;
41
+ import java.util.concurrent.atomic.AtomicBoolean;
42
+ import java.util.concurrent.Future;
43
+ import java.util.concurrent.TimeoutException;
44
+
45
+ import fulamobile.Config;
46
+ import fulamobile.Fulamobile;
47
+
48
+ import land.fx.wnfslib.Fs;
49
+
50
+ @ReactModule(name = FulaModule.NAME)
51
+ public class FulaModule extends ReactContextBaseJavaModule {
52
+
53
+
54
+ @Override
55
+ public void initialize() {
56
+ System.loadLibrary("wnfslib");
57
+ System.loadLibrary("gojni");
58
+ }
59
+
60
+
61
+ public static final String NAME = "FulaModule";
62
+ fulamobile.Client fula;
63
+
64
+ Client client;
65
+ Config fulaConfig;
66
+
67
+ String appName;
68
+ String appDir;
69
+ String fulaStorePath;
70
+ land.fx.wnfslib.Config rootConfig;
71
+ SharedPreferenceHelper sharedPref;
72
+ SecretKey secretKeyGlobal;
73
+ String identityEncryptedGlobal;
74
+ static String PRIVATE_KEY_STORE_PEERID = "PRIVATE_KEY";
75
+
76
+ public static class Client implements land.fx.wnfslib.Datastore {
77
+
78
+ private final fulamobile.Client internalClient;
79
+
80
+ Client(fulamobile.Client clientInput) {
81
+ this.internalClient = clientInput;
82
+ }
83
+
84
+ @NonNull
85
+ @Override
86
+ public byte[] get(@NonNull byte[] cid) {
87
+ try {
88
+ Log.d("ReactNative", Arrays.toString(cid));
89
+ return this.internalClient.get(cid);
90
+ } catch (Exception e) {
91
+ e.printStackTrace();
92
+ }
93
+ Log.d("ReactNative","Error get");
94
+ return cid;
95
+ }
96
+
97
+ @NonNull
98
+ @Override
99
+ public byte[] put(@NonNull byte[] cid, byte[] data) {
100
+ try {
101
+ long codec = (long)cid[1] & 0xFF;
102
+ byte[] put_cid = this.internalClient.put(data, codec);
103
+ //Log.d("ReactNative", "data="+ Arrays.toString(data) +" ;codec="+codec);
104
+ return put_cid;
105
+ } catch (Exception e) {
106
+ Log.d("ReactNative", "put Error="+e.getMessage());
107
+ e.printStackTrace();
108
+ }
109
+ Log.d("ReactNative","Error put");
110
+ return data;
111
+ }
112
+ }
113
+
114
+ public FulaModule(ReactApplicationContext reactContext) {
115
+ super(reactContext);
116
+ appName = reactContext.getPackageName();
117
+ appDir = reactContext.getFilesDir().toString();
118
+ fulaStorePath = appDir + "/fula";
119
+ File storeDir = new File(fulaStorePath);
120
+ sharedPref = SharedPreferenceHelper.getInstance(reactContext.getApplicationContext());
121
+ boolean success = true;
122
+ if (!storeDir.exists()) {
123
+ success = storeDir.mkdirs();
124
+ }
125
+ if (success) {
126
+ Log.d("ReactNative", "Fula store folder created for " + appName + " at " + fulaStorePath);
127
+ } else {
128
+ Log.d("ReactNative", "Unable to create fula store folder for " + appName + " at " + fulaStorePath);
129
+ }
130
+ }
131
+
132
+ @Override
133
+ @NonNull
134
+ public java.lang.String getName() {
135
+ return NAME;
136
+ }
137
+
138
+
139
+ private byte[] toByte(@NonNull String input) {
140
+ return input.getBytes(StandardCharsets.UTF_8);
141
+ }
142
+
143
+ private byte[] decToByte(@NonNull String input) {
144
+ String[] parts = input.split(",");
145
+ byte[] output = new byte[parts.length];
146
+ for (int i = 0; i < parts.length; i++) {
147
+ output[i] = Byte.parseByte(parts[i]);
148
+ }
149
+ return output;
150
+ }
151
+
152
+ @NonNull
153
+ @Contract("_ -> new")
154
+ public String toString(byte[] input) {
155
+ return new String(input, StandardCharsets.UTF_8);
156
+ }
157
+
158
+ @NonNull
159
+ private static int[] stringArrToIntArr(@NonNull String[] s) {
160
+ int[] result = new int[s.length];
161
+ for (int i = 0; i < s.length; i++) {
162
+ result[i] = Integer.parseInt(s[i]);
163
+ }
164
+ return result;
165
+ }
166
+
167
+ @NonNull
168
+ @Contract(pure = true)
169
+ private static byte[] convertIntToByte(@NonNull int[] input) {
170
+ byte[] result = new byte[input.length];
171
+ for (int i = 0; i < input.length; i++) {
172
+ byte b = (byte) input[i];
173
+ result[i] = b;
174
+ }
175
+ return result;
176
+ }
177
+
178
+ @NonNull
179
+ private byte[] convertStringToByte(@NonNull String data) {
180
+ String[] keyInt_S = data.split(",");
181
+ int[] keyInt = stringArrToIntArr(keyInt_S);
182
+
183
+ return convertIntToByte(keyInt);
184
+ }
185
+
186
+ @ReactMethod
187
+ public void checkConnection(int timeout, Promise promise) {
188
+ Log.d("ReactNative", "checkConnection started");
189
+ ThreadUtils.runOnExecutor(() -> {
190
+ if (this.fula != null) {
191
+ try {
192
+ boolean connectionStatus = this.checkConnectionInternal(timeout);
193
+ Log.d("ReactNative", "checkConnection ended " + connectionStatus);
194
+ promise.resolve(connectionStatus);
195
+ }
196
+ catch (Exception e) {
197
+ Log.d("ReactNative", "checkConnection failed with Error: " + e.getMessage());
198
+ promise.resolve(false);
199
+ }
200
+ } else {
201
+ Log.d("ReactNative", "checkConnection failed with Error: " + "fula is null");
202
+ promise.resolve(false);
203
+ }
204
+ });
205
+ }
206
+
207
+ @ReactMethod
208
+ public void newClient(String identityString, String storePath, String bloxAddr, String exchange, boolean autoFlush, boolean useRelay, boolean refresh, Promise promise) {
209
+ Log.d("ReactNative", "newClient started");
210
+ ThreadUtils.runOnExecutor(() -> {
211
+ try {
212
+ Log.d("ReactNative", "newClient storePath= " + storePath + " bloxAddr= " + bloxAddr + " exchange= " + exchange + " autoFlush= " + autoFlush + " useRelay= " + useRelay + " refresh= " + refresh);
213
+ byte[] identity = toByte(identityString);
214
+ Log.d("ReactNative", "newClient identity= " + identityString);
215
+ this.newClientInternal(identity, storePath, bloxAddr, exchange, autoFlush, useRelay, refresh);
216
+ //String objString = Arrays.toString(obj);
217
+ String peerId = this.fula.id();
218
+ Log.d("ReactNative", "newClient peerId= " + peerId);
219
+ promise.resolve(peerId);
220
+ } catch (Exception e) {
221
+ Log.d("ReactNative", "newClient failed with Error: " + e.getMessage());
222
+ promise.reject("Error", e.getMessage());
223
+ }
224
+ });
225
+ }
226
+
227
+ @ReactMethod
228
+ public void isReady(boolean filesystemCheck, Promise promise) {
229
+ Log.d("ReactNative", "isReady started");
230
+ ThreadUtils.runOnExecutor(() -> {
231
+ boolean initialized = false;
232
+ try {
233
+ if (this.fula != null && this.fula.id() != null) {
234
+ if (filesystemCheck) {
235
+ if (this.client != null && this.rootConfig != null && !this.rootConfig.getCid().isEmpty()) {
236
+ initialized = true;
237
+ Log.d("ReactNative", "isReady is true with filesystem check");
238
+ }
239
+ } else {
240
+ Log.d("ReactNative", "isReady is true without filesystem check");
241
+ initialized = true;
242
+ }
243
+ }
244
+ promise.resolve(initialized);
245
+ } catch (Exception e) {
246
+ Log.d("ReactNative", "isReady failed with Error: " + e.getMessage());
247
+ promise.reject("Error", e.getMessage());
248
+ }
249
+ });
250
+ }
251
+
252
+ @ReactMethod
253
+ public void init(String identityString, String storePath, String bloxAddr, String exchange, boolean autoFlush, String rootConfig, boolean useRelay, boolean refresh, Promise promise) {
254
+ Log.d("ReactNative", "init started");
255
+ ThreadUtils.runOnExecutor(() -> {
256
+ try {
257
+ WritableMap resultData = new WritableNativeMap();
258
+ Log.d("ReactNative", "init storePath= " + storePath);
259
+ byte[] identity = toByte(identityString);
260
+ Log.d("ReactNative", "init identity= " + identityString);
261
+ String[] obj = this.initInternal(identity, storePath, bloxAddr, exchange, autoFlush, rootConfig, useRelay, refresh);
262
+ Log.d("ReactNative", "init object created: [ " + obj[0] + ", " + obj[1] + " ]");
263
+ resultData.putString("peerId", obj[0]);
264
+ resultData.putString("rootCid", obj[1]);
265
+ promise.resolve(resultData);
266
+ } catch (Exception e) {
267
+ Log.d("ReactNative", "init failed with Error: " + e.getMessage());
268
+ promise.reject("Error", e.getMessage());
269
+ }
270
+ });
271
+ }
272
+
273
+ @ReactMethod
274
+ public void logout(String identityString, String storePath, Promise promise) {
275
+ Log.d("ReactNative", "logout started");
276
+ ThreadUtils.runOnExecutor(() -> {
277
+ try {
278
+ byte[] identity = toByte(identityString);
279
+ boolean obj = this.logoutInternal(identity, storePath);
280
+ Log.d("ReactNative", "logout completed");
281
+ promise.resolve(obj);
282
+ } catch (Exception e) {
283
+ Log.d("ReactNative", "logout failed with Error: " + e.getMessage());
284
+ promise.reject("Error", e.getMessage());
285
+ }
286
+ });
287
+ }
288
+
289
+ private boolean checkConnectionInternal(int timeout) throws Exception {
290
+ try {
291
+ Log.d("ReactNative", "checkConnectionInternal started");
292
+ if (this.fula != null) {
293
+ try {
294
+ Log.d("ReactNative", "connectToBlox started");
295
+
296
+ AtomicBoolean connectionStatus = new AtomicBoolean(false);
297
+ ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
298
+ Future<?> future = executor.submit(() -> {
299
+ try {
300
+ this.fula.connectToBlox();
301
+ connectionStatus.set(true);
302
+ Log.d("ReactNative", "checkConnectionInternal succeeded ");
303
+ } catch (Exception e) {
304
+ Log.d("ReactNative", "checkConnectionInternal failed with Error: " + e.getMessage());
305
+ }
306
+ });
307
+
308
+ try {
309
+ future.get(timeout, TimeUnit.SECONDS);
310
+ } catch (TimeoutException te) {
311
+ // If the timeout occurs, shut down the executor and return false
312
+ executor.shutdownNow();
313
+ return false;
314
+ } finally {
315
+ // If the future task is done, we can shut down the executor
316
+ if (future.isDone()) {
317
+ executor.shutdown();
318
+ }
319
+ }
320
+
321
+ return connectionStatus.get();
322
+ } catch (Exception e) {
323
+ Log.d("ReactNative", "checkConnectionInternal failed with Error: " + e.getMessage());
324
+ return false;
325
+ }
326
+ } else {
327
+ Log.d("ReactNative", "checkConnectionInternal failed because fula is not initialized ");
328
+ return false;
329
+ }
330
+ } catch (Exception e) {
331
+ Log.d("ReactNative", "checkConnectionInternal failed with Error: " + e.getMessage());
332
+ throw (e);
333
+ }
334
+ }
335
+
336
+ @ReactMethod
337
+ public void checkFailedActions(boolean retry, int timeout, Promise promise) throws Exception {
338
+ try {
339
+ if (this.fula != null) {
340
+ if (!retry) {
341
+ Log.d("ReactNative", "checkFailedActions without retry");
342
+ fulamobile.LinkIterator failedLinks = this.fula.listFailedPushes();
343
+ if (failedLinks.hasNext()) {
344
+ Log.d("ReactNative", "checkFailedActions found: "+Arrays.toString(failedLinks.next()));
345
+ promise.resolve(true);
346
+ } else {
347
+ promise.resolve(false);
348
+ }
349
+ } else {
350
+ Log.d("ReactNative", "checkFailedActions with retry");
351
+ boolean retryResults = this.retryFailedActionsInternal(timeout);
352
+ promise.resolve(!retryResults);
353
+ }
354
+ } else {
355
+ throw new Exception("Fula is not initialized");
356
+ }
357
+ } catch (Exception e) {
358
+ Log.d("ReactNative", "checkFailedActions failed with Error: " + e.getMessage());
359
+ throw (e);
360
+ }
361
+ }
362
+
363
+ @ReactMethod
364
+ private void listFailedActions(ReadableArray cids, Promise promise) throws Exception {
365
+ try {
366
+ if (this.fula != null) {
367
+ Log.d("ReactNative", "listFailedActions");
368
+ fulamobile.StringIterator failedLinks = this.fula.listFailedPushesAsString();
369
+ ArrayList<String> failedLinksList = new ArrayList<>();
370
+ while (failedLinks.hasNext()) {
371
+ failedLinksList.add(failedLinks.next());
372
+ }
373
+ if (cids.size() > 0) {
374
+ // If cids array is provided, filter the failedLinksList
375
+ ArrayList<String> cidsList = new ArrayList<>();
376
+ for (int i = 0; i < cids.size(); i++) {
377
+ cidsList.add(cids.getString(i));
378
+ }
379
+ cidsList.retainAll(failedLinksList); // Keep only the elements in both cidsList and failedLinksList
380
+ if (!cidsList.isEmpty()) {
381
+ // If there are any matching cids, return them
382
+ WritableArray cidsArray = Arguments.createArray();
383
+ for (String cid : cidsList) {
384
+ cidsArray.pushString(cid);
385
+ }
386
+ promise.resolve(cidsArray);
387
+ } else {
388
+ // If there are no matching cids, return false
389
+ promise.resolve(false);
390
+ }
391
+ } else if (!failedLinksList.isEmpty()) {
392
+ // If cids array is not provided, return the whole list
393
+ Log.d("ReactNative", "listFailedActions found: "+ failedLinksList);
394
+ WritableArray failedLinksArray = Arguments.createArray();
395
+ for (String link : failedLinksList) {
396
+ failedLinksArray.pushString(link);
397
+ }
398
+ promise.resolve(failedLinksArray);
399
+ } else {
400
+ promise.resolve(false);
401
+ }
402
+ } else {
403
+ throw new Exception("listFailedActions: Fula is not initialized");
404
+ }
405
+ } catch (Exception e) {
406
+ Log.d("ReactNative", "listFailedActions failed with Error: " + e.getMessage());
407
+ throw (e);
408
+ }
409
+ }
410
+
411
+
412
+
413
+
414
+ private boolean retryFailedActionsInternal(int timeout) throws Exception {
415
+ try {
416
+ Log.d("ReactNative", "retryFailedActionsInternal started");
417
+ if (this.fula != null) {
418
+ //Fula is initialized
419
+ try {
420
+ boolean connectionCheck = this.checkConnectionInternal(timeout);
421
+ if(connectionCheck) {
422
+ try {
423
+ Log.d("ReactNative", "retryFailedPushes started");
424
+ this.fula.retryFailedPushes();
425
+ Log.d("ReactNative", "flush started");
426
+ this.fula.flush();
427
+ return true;
428
+ }
429
+ catch (Exception e) {
430
+ this.fula.flush();
431
+ Log.d("ReactNative", "retryFailedActionsInternal failed with Error: " + e.getMessage());
432
+ return false;
433
+ }
434
+ //Blox online
435
+ /*fulamobile.LinkIterator failedLinks = this.fula.listFailedPushes();
436
+ if (failedLinks.hasNext()) {
437
+ Log.d("ReactNative", "Failed links");
438
+ //Failed list is not empty. iterate in the list
439
+ while (failedLinks.hasNext()) {
440
+ //Get the missing key
441
+ byte[] failedNode = failedLinks.next();
442
+ try {
443
+ //Push to Blox
444
+ Log.d("ReactNative", "Pushing Failed links "+Arrays.toString(failedNode));
445
+ this.pushInternal(failedNode);
446
+ Log.d("ReactNative", "Failed links pushed");
447
+ }
448
+ catch (Exception e) {
449
+ Log.d("ReactNative", "retryFailedActionsInternal failed with Error: " + e.getMessage());
450
+ }
451
+ }
452
+ //check if list is empty now and all are pushed
453
+ Log.d("ReactNative", "Pushing finished");
454
+ fulamobile.LinkIterator failedLinks_after = this.fula.listFailedPushes();
455
+ if(failedLinks_after.hasNext()) {
456
+ //Some pushes failed
457
+ byte[] first_failed = failedLinks_after.next();
458
+ Log.d("ReactNative", "Failed links are not empty "+Arrays.toString(first_failed));
459
+ return false;
460
+ } else {
461
+ //All pushes successful
462
+ return true;
463
+ }
464
+ } else {
465
+ Log.d("ReactNative", "No Failed links");
466
+ //Failed list is empty
467
+ return true;
468
+ }*/
469
+ } else {
470
+ Log.d("ReactNative", "retryFailedActionsInternal failed because blox is offline");
471
+ //Blox Offline
472
+ return false;
473
+ }
474
+ }
475
+ catch (Exception e) {
476
+ Log.d("ReactNative", "retryFailedActionsInternal failed with Error: " + e.getMessage());
477
+ return false;
478
+ }
479
+ } else {
480
+ Log.d("ReactNative", "retryFailedActionsInternal failed because fula is not initialized");
481
+ //Fula is not initialized
482
+ return false;
483
+ }
484
+ } catch (Exception e) {
485
+ Log.d("ReactNative", "retryFailedActionsInternal failed with Error: " + e.getMessage());
486
+ throw (e);
487
+ }
488
+ }
489
+
490
+ @NonNull
491
+ private byte[] createPeerIdentity(byte[] identity) throws GeneralSecurityException, IOException {
492
+ try {
493
+ // 1: First: create public key from provided private key
494
+ // 2: Should read the local keychain store (if it is key-value, key is public key above,
495
+ // 3: if found, decrypt using the private key
496
+ // 4: If not found or decryption not successful, generate an identity
497
+ // 5: then encrypt and store in keychain
498
+ byte[] libp2pId;
499
+ String encryptedLibp2pId = sharedPref.getValue(PRIVATE_KEY_STORE_PEERID);
500
+ byte[] encryptionPair;
501
+ SecretKey encryptionSecretKey;
502
+ try {
503
+ encryptionSecretKey = Cryptography.generateKey(identity);
504
+ Log.d("ReactNative", "encryptionSecretKey generated from privateKey");
505
+ } catch (Exception e) {
506
+ Log.d("ReactNative", "Failed to generate key for encryption: " + e.getMessage());
507
+ throw new GeneralSecurityException("Failed to generate key encryption", e);
508
+ }
509
+
510
+ if (encryptedLibp2pId == null || !encryptedLibp2pId.startsWith("FULA_" +
511
+ "ENC_V4:")) {
512
+ Log.d("ReactNative", "encryptedLibp2pId is not correct. creating new one " + encryptedLibp2pId);
513
+
514
+ try {
515
+ libp2pId = Fulamobile.generateEd25519KeyFromString(toString(identity));
516
+ } catch (Exception e) {
517
+ Log.d("ReactNative", "Failed to generate libp2pId: " + e.getMessage());
518
+ throw new GeneralSecurityException("Failed to generate libp2pId", e);
519
+ }
520
+ encryptedLibp2pId = "FULA_ENC_V4:" + Cryptography.encryptMsg(StaticHelper.bytesToBase64(libp2pId), encryptionSecretKey, null);
521
+ sharedPref.add(PRIVATE_KEY_STORE_PEERID, encryptedLibp2pId);
522
+ } else {
523
+ Log.d("ReactNative", "encryptedLibp2pId is correct. decrypting " + encryptedLibp2pId);
524
+ }
525
+
526
+ try {
527
+ String decryptedLibp2pId = Cryptography.decryptMsg(encryptedLibp2pId.replace("FULA_ENC_V4:", ""), encryptionSecretKey);
528
+
529
+ return StaticHelper.base64ToBytes(decryptedLibp2pId);
530
+ } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException e) {
531
+ Log.d("ReactNative", "createPeerIdentity decryptMsg failed with Error: " + e.getMessage());
532
+ throw (e);
533
+ }
534
+
535
+ } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException e) {
536
+ Log.d("ReactNative", "createPeerIdentity failed with Error: " + e.getMessage());
537
+ throw (e);
538
+ }
539
+ }
540
+
541
+ private void createNewRootConfig(FulaModule.Client iClient, byte[] identity) throws Exception {
542
+ byte[] hash32 = getSHA256Hash(identity);
543
+ this.rootConfig = Fs.init(iClient, hash32);
544
+ Log.d("ReactNative", "rootConfig is created " + this.rootConfig.getCid());
545
+ if (this.fula != null) {
546
+ this.fula.flush();
547
+ }
548
+ this.encrypt_and_store_config();
549
+ }
550
+
551
+ public static byte[] getSHA256Hash(byte[] input) throws NoSuchAlgorithmException {
552
+ MessageDigest md = MessageDigest.getInstance("SHA-256");
553
+ return md.digest(input);
554
+ }
555
+
556
+ private static String bytesToHex(byte[] bytes) {
557
+ StringBuilder result = new StringBuilder();
558
+ for (byte b : bytes) {
559
+ result.append(String.format("%02x", b));
560
+ }
561
+ return result.toString();
562
+ }
563
+
564
+ private void reloadFS(FulaModule.Client iClient, byte[] wnfsKey, String rootCid) throws Exception {
565
+ Log.d("ReactNative", "reloadFS called: rootCid=" + rootCid);
566
+ byte[] hash32 = getSHA256Hash(wnfsKey);
567
+ Log.d("ReactNative", "wnfsKey=" + bytesToHex(wnfsKey) + "; hash32 = " + bytesToHex(hash32));
568
+ Fs.loadWithWNFSKey(iClient, hash32, rootCid);
569
+ Log.d("ReactNative", "reloadFS completed");
570
+ }
571
+
572
+ private boolean encrypt_and_store_config() throws Exception {
573
+ try {
574
+ if(this.identityEncryptedGlobal != null && !this.identityEncryptedGlobal.isEmpty()) {
575
+ Log.d("ReactNative", "encrypt_and_store_config started");
576
+
577
+ String cid_encrypted = Cryptography.encryptMsg(this.rootConfig.getCid(), this.secretKeyGlobal, null);
578
+
579
+ sharedPref.add("FULA_ENC_V4:cid_encrypted_" + this.identityEncryptedGlobal, cid_encrypted);
580
+ return true;
581
+ } else {
582
+ Log.d("ReactNative", "encrypt_and_store_config failed because identityEncryptedGlobal is empty");
583
+ return false;
584
+ }
585
+ } catch (Exception e) {
586
+ Log.d("ReactNative", "encrypt_and_store_config failed with Error: " + e.getMessage());
587
+ throw (e);
588
+ }
589
+ }
590
+
591
+ private boolean logoutInternal(byte[] identity, String storePath) throws Exception {
592
+ try {
593
+ if (this.fula != null) {
594
+ this.fula.flush();
595
+ }
596
+ SecretKey secretKey = Cryptography.generateKey(identity);
597
+ byte[] iv = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B };
598
+ String identity_encrypted = Cryptography.encryptMsg(Arrays.toString(identity), secretKey, iv);
599
+ sharedPref.remove("FULA_ENC_V4:cid_encrypted_"+ identity_encrypted);
600
+
601
+ //TODO: Should also remove peerid @Mahdi
602
+
603
+ sharedPref.remove("FULA_ENC_V4:cid_encrypted_"+ identity_encrypted);
604
+
605
+ this.rootConfig = null;
606
+ this.secretKeyGlobal = null;
607
+ this.identityEncryptedGlobal = null;
608
+
609
+ if (storePath == null || storePath.trim().isEmpty()) {
610
+ storePath = this.fulaStorePath;
611
+ }
612
+
613
+ File file = new File(storePath);
614
+ FileUtils.deleteDirectory(file);
615
+ return true;
616
+
617
+ } catch (Exception e) {
618
+ Log.d("ReactNative", "logout internal failed with Error: " + e.getMessage());
619
+ throw (e);
620
+ }
621
+ }
622
+
623
+ public fulamobile.Client getFulaClient() {
624
+ return this.fula;
625
+ }
626
+
627
+ @NonNull
628
+ private byte[] newClientInternal(byte[] identity, String storePath, String bloxAddr, String exchange, boolean autoFlush, boolean useRelay, boolean refresh) throws GeneralSecurityException, IOException {
629
+ byte[] peerIdentity = null;
630
+ try {
631
+ fulaConfig = new Config();
632
+ if (storePath == null || storePath.trim().isEmpty()) {
633
+ fulaConfig.setStorePath(this.fulaStorePath);
634
+ } else {
635
+ fulaConfig.setStorePath(storePath);
636
+ }
637
+ Log.d("ReactNative", "newClientInternal storePath is set: " + fulaConfig.getStorePath());
638
+
639
+ peerIdentity = this.createPeerIdentity(identity);
640
+ fulaConfig.setIdentity(peerIdentity);
641
+ Log.d("ReactNative", "peerIdentity is set: " + toString(fulaConfig.getIdentity()));
642
+ fulaConfig.setBloxAddr(bloxAddr);
643
+ Log.d("ReactNative", "bloxAddr is set: " + fulaConfig.getBloxAddr());
644
+ fulaConfig.setExchange(exchange);
645
+ fulaConfig.setSyncWrites(autoFlush);
646
+ if (useRelay) {
647
+ fulaConfig.setAllowTransientConnection(true);
648
+ fulaConfig.setForceReachabilityPrivate(true);
649
+ }
650
+ if (this.fula == null || refresh) {
651
+ Log.d("ReactNative", "Creating a new Fula instance");
652
+ try {
653
+ shutdownInternal();
654
+ this.fula = Fulamobile.newClient(fulaConfig);
655
+ if (this.fula != null) {
656
+ this.fula.flush();
657
+ }
658
+ } catch (Exception e) {
659
+ Log.d("ReactNative", "Failed to create new Fula instance: " + e.getMessage());
660
+ throw new IOException("Failed to create new Fula instance", e);
661
+ }
662
+ }
663
+ } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException e) {
664
+ Log.d("ReactNative", "newclientInternal failed with Error: " + e.getMessage());
665
+ throw (e);
666
+ }
667
+ return peerIdentity;
668
+ }
669
+
670
+
671
+ @NonNull
672
+ private String[] initInternal(byte[] identity, String storePath, String bloxAddr, String exchange, boolean autoFlush, String rootCid, boolean useRelay, boolean refresh) throws Exception {
673
+ try {
674
+ if (this.fula == null || refresh) {
675
+ this.newClientInternal(identity, storePath, bloxAddr, exchange, autoFlush, useRelay, refresh);
676
+ }
677
+ if(this.client == null || refresh) {
678
+ this.client = new Client(this.fula);
679
+ Log.d("ReactNative", "fula initialized: " + this.fula.id());
680
+ }
681
+
682
+ SecretKey secretKey = Cryptography.generateKey(identity);
683
+ Log.d("ReactNative", "secretKey generated: " + secretKey.toString());
684
+ byte[] iv = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B };
685
+ String identity_encrypted =Cryptography.encryptMsg(Arrays.toString(identity), secretKey, iv);
686
+ Log.d("ReactNative", "identity_encrypted generated: " + identity_encrypted + " for identity: " + Arrays.toString(identity));
687
+ this.identityEncryptedGlobal = identity_encrypted;
688
+ this.secretKeyGlobal = secretKey;
689
+
690
+ if ( this.rootConfig == null || this.rootConfig.getCid().isEmpty() ) {
691
+ Log.d("ReactNative", "this.rootCid is empty.");
692
+ //Load from keystore
693
+
694
+ String cid_encrypted_fetched = sharedPref.getValue("FULA_ENC_V4:cid_encrypted_"+ identity_encrypted);
695
+ Log.d("ReactNative", "Here1");
696
+ String cid = "";
697
+ if(cid_encrypted_fetched != null && !cid_encrypted_fetched.isEmpty()) {
698
+ Log.d("ReactNative", "decrypting cid="+cid_encrypted_fetched+" with secret="+secretKey.toString());
699
+ cid = Cryptography.decryptMsg(cid_encrypted_fetched, secretKey);
700
+ }
701
+
702
+ Log.d("ReactNative", "Here2");
703
+ //Log.d("ReactNative", "Attempted to fetch cid from keystore; cid="+cid);
704
+ if(cid == null || cid.isEmpty()) {
705
+ Log.d("ReactNative", "cid was not found");
706
+ if(rootCid != null && !rootCid.isEmpty()){
707
+ Log.d("ReactNative", "Re-setting cid from input: "+rootCid);
708
+ cid = rootCid;
709
+ }
710
+ if(cid == null || cid.isEmpty()) {
711
+ Log.d("ReactNative", "Tried to recover cid but was not successful. Creating new ones");
712
+ this.createNewRootConfig(this.client, identity);
713
+ }
714
+ } else {
715
+ Log.d("ReactNative", "Recovered cid and private ref from keychain store. cid="+cid +" and cid from input was: "+rootCid);
716
+ this.rootConfig = new land.fx.wnfslib.Config(cid);
717
+ this.reloadFS(this.client, identity, cid);
718
+ this.encrypt_and_store_config();
719
+ }
720
+
721
+ Log.d("ReactNative", "creating rootConfig completed");
722
+
723
+ Log.d("ReactNative", "rootConfig is created: cid=" + this.rootConfig.getCid());
724
+ } else {
725
+ Log.d("ReactNative", "rootConfig existed: cid=" + this.rootConfig.getCid());
726
+ }
727
+ String peerId = this.fula.id();
728
+ String[] obj = new String[2];
729
+ obj[0] = peerId;
730
+ obj[1] = this.rootConfig.getCid();
731
+ Log.d("ReactNative", "initInternal is completed successfully");
732
+ if (this.fula != null) {
733
+ this.fula.flush();
734
+ }
735
+ return obj;
736
+ } catch (Exception e) {
737
+ Log.d("ReactNative", "init internal failed with Error: " + e.getMessage());
738
+ throw (e);
739
+ }
740
+ }
741
+
742
+ @ReactMethod
743
+ public void mkdir(String path, Promise promise) {
744
+ ThreadUtils.runOnExecutor(() -> {
745
+ Log.d("ReactNative", "mkdir started with: path = " + path + " rootConfig.getCid() = " + this.rootConfig.getCid());
746
+ try {
747
+ land.fx.wnfslib.Config config = Fs.mkdir(this.client, this.rootConfig.getCid(), path);
748
+ if(config != null) {
749
+ this.rootConfig = config;
750
+ this.encrypt_and_store_config();
751
+ if (this.fula != null) {
752
+ this.fula.flush();
753
+ }
754
+ String rootCid = this.rootConfig.getCid();
755
+ Log.d("ReactNative", "mkdir completed successfully with rootCid = " + rootCid);
756
+ promise.resolve(rootCid);
757
+ } else {
758
+ Log.d("ReactNative", "mkdir Error: config is null");
759
+ promise.reject(new Exception("mkdir Error: config is null"));
760
+ }
761
+ } catch (Exception e) {
762
+ Log.d("get", e.getMessage());
763
+ promise.reject(e);
764
+ }
765
+ });
766
+ }
767
+
768
+ @ReactMethod
769
+ public void writeFile(String fulaTargetFilename, String localFilename, Promise promise) {
770
+ /*
771
+ // reads content of the file form localFilename (should include full absolute path to local file with read permission
772
+ // writes content to the specified location by fulaTargetFilename in Fula filesystem
773
+ // fulaTargetFilename: a string including full path and filename of target file on Fula (e.g. root/pictures/cat.jpg)
774
+ // localFilename: a string containing full path and filename of local file on hte device (e.g /usr/bin/cat.jpg)
775
+ // Returns: new cid of the root after this file is placed in the tree
776
+ */
777
+ ThreadUtils.runOnExecutor(() -> {
778
+ Log.d("ReactNative", "writeFile to : path = " + fulaTargetFilename + ", from: " + localFilename);
779
+ try {
780
+ if (this.client != null) {
781
+ Log.d("ReactNative", "writeFileFromPath started: this.rootConfig.getCid=" + this.rootConfig.getCid()+ ", fulaTargetFilename="+fulaTargetFilename + ", localFilename="+localFilename);
782
+ land.fx.wnfslib.Config config = Fs.writeFileStreamFromPath(this.client, this.rootConfig.getCid(), fulaTargetFilename, localFilename);
783
+ if(config != null) {
784
+ this.rootConfig = config;
785
+ this.encrypt_and_store_config();
786
+ if (this.fula != null) {
787
+ this.fula.flush();
788
+ String rootCid = this.rootConfig.getCid();
789
+ Log.d("ReactNative", "writeFileFromPath completed: this.rootConfig.getCid=" + rootCid);
790
+ promise.resolve(rootCid);
791
+ } else {
792
+ Log.d("ReactNative", "writeFile Error: fula is null");
793
+ promise.reject(new Exception("writeFile Error: fula is null"));
794
+ }
795
+ } else {
796
+ Log.d("ReactNative", "writeFile Error: config is null");
797
+ promise.reject(new Exception("writeFile Error: config is null"));
798
+ }
799
+ } else {
800
+ Log.d("ReactNative", "writeFile Error: client is null");
801
+ promise.reject(new Exception("writeFile Error: client is null"));
802
+ }
803
+ } catch (Exception e) {
804
+ Log.d("get", e.getMessage());
805
+ promise.reject(e);
806
+ }
807
+ });
808
+ }
809
+
810
+ @ReactMethod
811
+ public void writeFileContent(String path, String contentString, Promise promise) {
812
+ ThreadUtils.runOnExecutor(() -> {
813
+ Log.d("ReactNative", "writeFile: contentString = " + contentString);
814
+ Log.d("ReactNative", "writeFile: path = " + path);
815
+ try {
816
+ byte[] content = this.convertStringToByte(contentString);
817
+ land.fx.wnfslib.Config config = Fs.writeFile(this.client, this.rootConfig.getCid(), path, content);
818
+ this.rootConfig = config;
819
+ this.encrypt_and_store_config();
820
+ if (this.fula != null) {
821
+ this.fula.flush();
822
+ }
823
+ promise.resolve(config.getCid());
824
+ } catch (Exception e) {
825
+ Log.d("get", e.getMessage());
826
+ promise.reject(e);
827
+ }
828
+ });
829
+ }
830
+
831
+ @ReactMethod
832
+ public void ls(String path, Promise promise) {
833
+ ThreadUtils.runOnExecutor(() -> {
834
+ Log.d("ReactNative", "ls: path = " + path);
835
+ try {
836
+ byte[] res = Fs.ls(this.client, this.rootConfig.getCid(), path);
837
+
838
+ String s = new String(res, StandardCharsets.UTF_8);
839
+ Log.d("ReactNative", "ls: res = " + s);
840
+ promise.resolve(s);
841
+ } catch (Exception e) {
842
+ Log.d("get", e.getMessage());
843
+ promise.reject(e);
844
+ }
845
+ });
846
+ }
847
+
848
+ @ReactMethod
849
+ public void rm(String path, Promise promise) {
850
+ ThreadUtils.runOnExecutor(() -> {
851
+ Log.d("ReactNative", "rm: path = " + path + ", beginning rootCid=" + this.rootConfig.getCid());
852
+ try {
853
+ land.fx.wnfslib.Config config = Fs.rm(this.client, this.rootConfig.getCid(), path);
854
+ if(config != null) {
855
+ this.rootConfig = config;
856
+ this.encrypt_and_store_config();
857
+ if (this.fula != null) {
858
+ this.fula.flush();
859
+ }
860
+ String rootCid = config.getCid();
861
+ Log.d("ReactNative", "rm: returned rootCid = " + rootCid);
862
+ promise.resolve(rootCid);
863
+ } else {
864
+ Log.d("ReactNative", "rm Error: config is null");
865
+ promise.reject(new Exception("rm Error: config is null"));
866
+ }
867
+ } catch (Exception e) {
868
+ Log.d("get", e.getMessage());
869
+ promise.reject(e);
870
+ }
871
+ });
872
+ }
873
+
874
+ @ReactMethod
875
+ public void cp(String sourcePath, String targetPath, Promise promise) {
876
+ ThreadUtils.runOnExecutor(() -> {
877
+ Log.d("ReactNative", "rm: sourcePath = " + sourcePath);
878
+ try {
879
+ land.fx.wnfslib.Config config = Fs.cp(this.client, this.rootConfig.getCid(), sourcePath, targetPath);
880
+ if(config != null) {
881
+ this.rootConfig = config;
882
+ this.encrypt_and_store_config();
883
+ if (this.fula != null) {
884
+ this.fula.flush();
885
+ }
886
+ promise.resolve(config.getCid());
887
+ } else {
888
+ Log.d("ReactNative", "cp Error: config is null");
889
+ promise.reject(new Exception("cp Error: config is null"));
890
+ }
891
+ } catch (Exception e) {
892
+ Log.d("get", e.getMessage());
893
+ promise.reject(e);
894
+ }
895
+ });
896
+ }
897
+
898
+ @ReactMethod
899
+ public void mv(String sourcePath, String targetPath, Promise promise) {
900
+ ThreadUtils.runOnExecutor(() -> {
901
+ Log.d("ReactNative", "rm: sourcePath = " + sourcePath);
902
+ try {
903
+ land.fx.wnfslib.Config config = Fs.mv(this.client, this.rootConfig.getCid(), sourcePath, targetPath);
904
+ if(config != null) {
905
+ this.rootConfig = config;
906
+ this.encrypt_and_store_config();
907
+ if (this.fula != null) {
908
+ this.fula.flush();
909
+ }
910
+ promise.resolve(config.getCid());
911
+ } else {
912
+ Log.d("ReactNative", "mv Error: config is null");
913
+ promise.reject(new Exception("mv Error: config is null"));
914
+ }
915
+ } catch (Exception e) {
916
+ Log.d("get", e.getMessage());
917
+ promise.reject(e);
918
+ }
919
+ });
920
+ }
921
+
922
+ @ReactMethod
923
+ public void readFile(String fulaTargetFilename, String localFilename, Promise promise) {
924
+ /*
925
+ // reads content of the file form localFilename (should include full absolute path to local file with read permission
926
+ // writes content to the specified location by fulaTargetFilename in Fula filesystem
927
+ // fulaTargetFilename: a string including full path and filename of target file on Fula (e.g. root/pictures/cat.jpg)
928
+ // localFilename: a string containing full path and filename of local file on hte device (e.g /usr/bin/cat.jpg)
929
+ // Returns: new cid of the root after this file is placed in the tree
930
+ */
931
+ ThreadUtils.runOnExecutor(() -> {
932
+ Log.d("ReactNative", "readFile: fulaTargetFilename = " + fulaTargetFilename);
933
+ try {
934
+ Log.d("ReactNative", "readFile: localFilename = " + localFilename + " fulaTargetFilename = " + fulaTargetFilename + " beginning rootCid = " + this.rootConfig.getCid());
935
+ String path = Fs.readFilestreamToPath(this.client, this.rootConfig.getCid(), fulaTargetFilename, localFilename);
936
+ promise.resolve(path);
937
+ } catch (Exception e) {
938
+ Log.d("get", e.getMessage());
939
+ promise.reject(e);
940
+ }
941
+ });
942
+ }
943
+
944
+ @ReactMethod
945
+ public void readFileContent(String path, Promise promise) {
946
+ ThreadUtils.runOnExecutor(() -> {
947
+ Log.d("ReactNative", "readFileContent: path = " + path);
948
+ try {
949
+ byte[] res = Fs.readFile(this.client, this.rootConfig.getCid(), path);
950
+ String resString = toString(res);
951
+ promise.resolve(resString);
952
+ } catch (Exception e) {
953
+ Log.d("get", e.getMessage());
954
+ promise.reject(e);
955
+ }
956
+ });
957
+ }
958
+
959
+ @ReactMethod
960
+ public void get(String keyString, Promise promise) {
961
+ ThreadUtils.runOnExecutor(() -> {
962
+ Log.d("ReactNative", "get: keyString = " + keyString);
963
+ try {
964
+ byte[] key = this.convertStringToByte(keyString);
965
+ byte[] value = this.getInternal(key);
966
+ String valueString = toString(value);
967
+ promise.resolve(valueString);
968
+ } catch (Exception e) {
969
+ Log.d("get", e.getMessage());
970
+ promise.reject(e);
971
+ }
972
+ });
973
+ }
974
+
975
+ @NonNull
976
+ private byte[] getInternal(byte[] key) throws Exception {
977
+ try {
978
+ Log.d("ReactNative", "getInternal: key.toString() = " + toString(key));
979
+ Log.d("ReactNative", "getInternal: key.toString().bytes = " + Arrays.toString(key));
980
+ byte[] value = this.fula.get(key);
981
+ Log.d("ReactNative", "getInternal: value.toString() = " + toString(value));
982
+ return value;
983
+ } catch (Exception e) {
984
+ Log.d("ReactNative", "getInternal: error = " + e.getMessage());
985
+ Log.d("getInternal", e.getMessage());
986
+ throw (e);
987
+ }
988
+ }
989
+
990
+ @ReactMethod
991
+ public void has(String keyString, Promise promise) {
992
+ ThreadUtils.runOnExecutor(() -> {
993
+ Log.d("ReactNative", "has: keyString = " + keyString);
994
+ try {
995
+ byte[] key = this.convertStringToByte(keyString);
996
+ boolean result = this.hasInternal(key);
997
+ promise.resolve(result);
998
+ } catch (Exception e) {
999
+ Log.d("get", e.getMessage());
1000
+ promise.reject(e);
1001
+ }
1002
+ });
1003
+ }
1004
+
1005
+ private boolean hasInternal(byte[] key) throws Exception {
1006
+ try {
1007
+ boolean res = this.fula.has(key);
1008
+ return res;
1009
+ } catch (Exception e) {
1010
+ Log.d("hasInternal", e.getMessage());
1011
+ throw (e);
1012
+ }
1013
+ }
1014
+
1015
+ private void pullInternal(byte[] key) throws Exception {
1016
+ try {
1017
+ this.fula.pull(key);
1018
+ } catch (Exception e) {
1019
+ Log.d("pullInternal", e.getMessage());
1020
+ throw (e);
1021
+ }
1022
+ }
1023
+
1024
+ @ReactMethod
1025
+ public void push(Promise promise) {
1026
+ ThreadUtils.runOnExecutor(() -> {
1027
+ Log.d("ReactNative", "push started");
1028
+ try {
1029
+ this.pushInternal(this.convertStringToByte(this.rootConfig.getCid()));
1030
+ promise.resolve(this.rootConfig.getCid());
1031
+ } catch (Exception e) {
1032
+ Log.d("get", e.getMessage());
1033
+ promise.reject(e);
1034
+ }
1035
+ });
1036
+ }
1037
+
1038
+ private void pushInternal(byte[] key) throws Exception {
1039
+ try {
1040
+ if (this.fula != null && this.fula.has(key)) {
1041
+ this.fula.push(key);
1042
+ this.fula.flush();
1043
+ } else {
1044
+ Log.d("ReactNative", "pushInternal error: key wasn't found or fula is not initialized");
1045
+ throw new Exception("key wasn't found in local storage");
1046
+ }
1047
+ } catch (Exception e) {
1048
+ Log.d("ReactNative", "pushInternal"+ e.getMessage());
1049
+ throw (e);
1050
+ }
1051
+ }
1052
+
1053
+ @ReactMethod
1054
+ public void put(String valueString, String codecString, Promise promise) {
1055
+ ThreadUtils.runOnExecutor(() -> {
1056
+ Log.d("ReactNative", "put: codecString = " + codecString);
1057
+ Log.d("ReactNative", "put: valueString = " + valueString);
1058
+ try {
1059
+ //byte[] codec = this.convertStringToByte(CodecString);
1060
+ long codec = Long.parseLong(codecString);
1061
+
1062
+
1063
+ Log.d("ReactNative", "put: codec = " + codec);
1064
+ byte[] value = toByte(valueString);
1065
+
1066
+ Log.d("ReactNative", "put: value.toString() = " + toString(value));
1067
+ byte[] key = this.putInternal(value, codec);
1068
+ Log.d("ReactNative", "put: key.toString() = " + toString(key));
1069
+ promise.resolve(toString(key));
1070
+ } catch (Exception e) {
1071
+ Log.d("ReactNative", "put: error = " + e.getMessage());
1072
+ promise.reject(e);
1073
+ }
1074
+ });
1075
+ }
1076
+
1077
+ @NonNull
1078
+ private byte[] putInternal(byte[] value, long codec) throws Exception {
1079
+ try {
1080
+ if(this.fula != null) {
1081
+ byte[] key = this.fula.put(value, codec);
1082
+ this.fula.flush();
1083
+ return key;
1084
+ } else {
1085
+ Log.d("ReactNative", "putInternal Error: fula is not initialized");
1086
+ throw (new Exception("putInternal Error: fula is not initialized"));
1087
+ }
1088
+ } catch (Exception e) {
1089
+ Log.d("ReactNative", "putInternal"+ e.getMessage());
1090
+ throw (e);
1091
+ }
1092
+ }
1093
+
1094
+ @ReactMethod
1095
+ public void setAuth(String peerIdString, boolean allow, Promise promise) {
1096
+ ThreadUtils.runOnExecutor(() -> {
1097
+ Log.d("ReactNative", "setAuth: peerIdString = " + peerIdString);
1098
+ try {
1099
+ if (this.fula != null && this.fula.id() != null && this.fulaConfig != null && this.fulaConfig.getBloxAddr() != null) {
1100
+ String bloxAddr = this.fulaConfig.getBloxAddr();
1101
+ Log.d("ReactNative", "setAuth: bloxAddr = '" + bloxAddr+"'"+ " peerIdString = '" + peerIdString+"'");
1102
+ int index = bloxAddr.lastIndexOf("/");
1103
+ String bloxPeerId = bloxAddr.substring(index + 1);
1104
+ this.fula.setAuth(bloxPeerId, peerIdString, allow);
1105
+ promise.resolve(true);
1106
+ } else {
1107
+ Log.d("ReactNative", "setAuth error: fula is not initialized");
1108
+ throw new Exception("fula is not initialized");
1109
+ }
1110
+ promise.resolve(false);
1111
+ } catch (Exception e) {
1112
+ Log.d("get", e.getMessage());
1113
+ promise.reject(e);
1114
+ }
1115
+ });
1116
+ }
1117
+
1118
+ private void shutdownInternal() throws Exception {
1119
+ try {
1120
+ if(this.fula != null) {
1121
+ this.fula.shutdown();
1122
+ this.fula = null;
1123
+ this.client = null;
1124
+ }
1125
+ } catch (Exception e) {
1126
+ Log.d("ReactNative", "shutdownInternal"+ e.getMessage());
1127
+ throw (e);
1128
+ }
1129
+ }
1130
+
1131
+ @ReactMethod
1132
+ public void shutdown(Promise promise) {
1133
+ ThreadUtils.runOnExecutor(() -> {
1134
+ try {
1135
+ shutdownInternal();
1136
+ promise.resolve(true);
1137
+ } catch (Exception e) {
1138
+ promise.reject(e);
1139
+ Log.d("ReactNative", "shutdown"+ e.getMessage());
1140
+ }
1141
+ });
1142
+ }
1143
+
1144
+ ///////////////////////////////////////////////////////////
1145
+ ///////////////////////////////////////////////////////////
1146
+ ///////////////////////////////////////////////////////////
1147
+ ///////////////////////////////////////////////////////////
1148
+ //////////////////////ANYTHING BELOW IS FOR BLOCKCHAIN/////
1149
+ ///////////////////////////////////////////////////////////
1150
+ @ReactMethod
1151
+ public void createAccount(String seedString, Promise promise) {
1152
+ ThreadUtils.runOnExecutor(() -> {
1153
+ Log.d("ReactNative", "createAccount: seedString = " + seedString);
1154
+ try {
1155
+ if (this.fula == null || this.fula.id() == null || this.fula.id().isEmpty()) {
1156
+ promise.reject(new Error("Fula client is not initialized"));
1157
+ } else {
1158
+
1159
+ if (!seedString.startsWith("/")) {
1160
+ promise.reject(new Error("seed should start with /"));
1161
+ }
1162
+ byte[] result = this.fula.seeded(seedString);
1163
+ String resultString = toString(result);
1164
+ promise.resolve(resultString);
1165
+ }
1166
+ } catch (Exception e) {
1167
+ Log.d("get", e.getMessage());
1168
+ promise.reject(e);
1169
+ }
1170
+ });
1171
+ }
1172
+
1173
+ @ReactMethod
1174
+ public void checkAccountExists(String accountString, Promise promise) {
1175
+ ThreadUtils.runOnExecutor(() -> {
1176
+ Log.d("ReactNative", "checkAccountExists: accountString = " + accountString);
1177
+ try {
1178
+ byte[] result = this.fula.accountExists(accountString);
1179
+ String resultString = toString(result);
1180
+ promise.resolve(resultString);
1181
+ } catch (Exception e) {
1182
+ Log.d("get", e.getMessage());
1183
+ promise.reject(e);
1184
+ }
1185
+ });
1186
+ }
1187
+
1188
+ @ReactMethod
1189
+ public void createPool(String seedString, String poolName, Promise promise) {
1190
+ ThreadUtils.runOnExecutor(() -> {
1191
+ Log.d("ReactNative", "createPool: seedString = " + seedString + "; poolName = " + poolName);
1192
+ try {
1193
+ byte[] result = this.fula.poolCreate(seedString, poolName);
1194
+ String resultString = toString(result);
1195
+ promise.resolve(resultString);
1196
+ } catch (Exception e) {
1197
+ Log.d("get", e.getMessage());
1198
+ promise.reject(e);
1199
+ }
1200
+ });
1201
+ }
1202
+
1203
+ @ReactMethod
1204
+ public void listPools(Promise promise) {
1205
+ ThreadUtils.runOnExecutor(() -> {
1206
+ Log.d("ReactNative", "listPools");
1207
+ try {
1208
+ byte[] result = this.fula.poolList();
1209
+ String resultString = toString(result);
1210
+ promise.resolve(resultString);
1211
+ } catch (Exception e) {
1212
+ Log.d("get", e.getMessage());
1213
+ promise.reject(e);
1214
+ }
1215
+ });
1216
+ }
1217
+
1218
+ @ReactMethod
1219
+ public void joinPool(String seedString, long poolID, Promise promise) {
1220
+ ThreadUtils.runOnExecutor(() -> {
1221
+ Log.d("ReactNative", "joinPool: seedString = " + seedString + "; poolID = " + poolID);
1222
+ try {
1223
+ byte[] result = this.fula.poolJoin(seedString, poolID);
1224
+ String resultString = toString(result);
1225
+ promise.resolve(resultString);
1226
+ } catch (Exception e) {
1227
+ Log.d("get", e.getMessage());
1228
+ promise.reject(e);
1229
+ }
1230
+ });
1231
+ }
1232
+
1233
+ @ReactMethod
1234
+ public void cancelPoolJoin(String seedString, long poolID, Promise promise) {
1235
+ ThreadUtils.runOnExecutor(() -> {
1236
+ Log.d("ReactNative", "cancelPoolJoin: seedString = " + seedString + "; poolID = " + poolID);
1237
+ try {
1238
+ byte[] result = this.fula.poolCancelJoin(seedString, poolID);
1239
+ String resultString = toString(result);
1240
+ promise.resolve(resultString);
1241
+ } catch (Exception e) {
1242
+ Log.d("get", e.getMessage());
1243
+ promise.reject(e);
1244
+ }
1245
+ });
1246
+ }
1247
+
1248
+ @ReactMethod
1249
+ public void listPoolJoinRequests(long poolID, Promise promise) {
1250
+ ThreadUtils.runOnExecutor(() -> {
1251
+ Log.d("ReactNative", "listPoolJoinRequests: poolID = " + poolID);
1252
+ try {
1253
+ byte[] result = this.fula.poolRequests(poolID);
1254
+ String resultString = toString(result);
1255
+ promise.resolve(resultString);
1256
+ } catch (Exception e) {
1257
+ Log.d("get", e.getMessage());
1258
+ promise.reject(e);
1259
+ }
1260
+ });
1261
+ }
1262
+
1263
+ @ReactMethod
1264
+ public void votePoolJoinRequest(String seedString, long poolID, String accountString, boolean accept, Promise promise) {
1265
+ ThreadUtils.runOnExecutor(() -> {
1266
+ Log.d("ReactNative", "votePoolJoinRequest: seedString = " + seedString + "; poolID = " + poolID + "; accountString = " + accountString + "; accept = " + accept);
1267
+ try {
1268
+ byte[] result = this.fula.poolVote(seedString, poolID, accountString, accept);
1269
+ String resultString = toString(result);
1270
+ promise.resolve(resultString);
1271
+ } catch (Exception e) {
1272
+ Log.d("get", e.getMessage());
1273
+ promise.reject(e);
1274
+ }
1275
+ });
1276
+ }
1277
+
1278
+ @ReactMethod
1279
+ public void leavePool(String seedString, long poolID, Promise promise) {
1280
+ ThreadUtils.runOnExecutor(() -> {
1281
+ Log.d("ReactNative", "leavePool: seedString = " + seedString + "; poolID = " + poolID);
1282
+ try {
1283
+ byte[] result = this.fula.poolLeave(seedString, poolID);
1284
+ String resultString = toString(result);
1285
+ promise.resolve(resultString);
1286
+ } catch (Exception e) {
1287
+ Log.d("get", e.getMessage());
1288
+ promise.reject(e);
1289
+ }
1290
+ });
1291
+ }
1292
+
1293
+ @ReactMethod
1294
+ public void newReplicationRequest(String seedString, long poolID, long replicationFactor, String cid, Promise promise) {
1295
+ ThreadUtils.runOnExecutor(() -> {
1296
+ Log.d("ReactNative", "newReplicationRequest: seedString = " + seedString + "; poolID = " + poolID + "; replicationFactor = " + replicationFactor + "; cid = " + cid);
1297
+ try {
1298
+ byte[] result = this.fula.manifestUpload(seedString, poolID, replicationFactor, cid);
1299
+ String resultString = toString(result);
1300
+ promise.resolve(resultString);
1301
+ } catch (Exception e) {
1302
+ Log.d("get", e.getMessage());
1303
+ promise.reject(e);
1304
+ }
1305
+ });
1306
+ }
1307
+
1308
+ @ReactMethod
1309
+ public void newStoreRequest(String seedString, long poolID, String uploader, String cid, Promise promise) {
1310
+ ThreadUtils.runOnExecutor(() -> {
1311
+ Log.d("ReactNative", "newStoreRequest: seedString = " + seedString + "; poolID = " + poolID + "; uploader = " + uploader + "; cid = " + cid);
1312
+ try {
1313
+ byte[] result = this.fula.manifestStore(seedString, poolID, uploader, cid);
1314
+ String resultString = toString(result);
1315
+ promise.resolve(resultString);
1316
+ } catch (Exception e) {
1317
+ Log.d("get", e.getMessage());
1318
+ promise.reject(e);
1319
+ }
1320
+ });
1321
+ }
1322
+
1323
+ @ReactMethod
1324
+ public void listAvailableReplicationRequests(long poolID, Promise promise) {
1325
+ ThreadUtils.runOnExecutor(() -> {
1326
+ Log.d("ReactNative", "listAvailableReplicationRequests: poolID = " + poolID);
1327
+ try {
1328
+ byte[] result = this.fula.manifestAvailable(poolID);
1329
+ String resultString = toString(result);
1330
+ promise.resolve(resultString);
1331
+ } catch (Exception e) {
1332
+ Log.d("get", e.getMessage());
1333
+ promise.reject(e);
1334
+ }
1335
+ });
1336
+ }
1337
+
1338
+ @ReactMethod
1339
+ public void removeReplicationRequest(String seedString, long poolID, String cid, Promise promise) {
1340
+ ThreadUtils.runOnExecutor(() -> {
1341
+ Log.d("ReactNative", "newReplicationRequest: seedString = " + seedString + "; poolID = " + poolID + "; cid = " + cid);
1342
+ try {
1343
+ byte[] result = this.fula.manifestRemove(seedString, poolID, cid);
1344
+ String resultString = toString(result);
1345
+ promise.resolve(resultString);
1346
+ } catch (Exception e) {
1347
+ Log.d("get", e.getMessage());
1348
+ promise.reject(e);
1349
+ }
1350
+ });
1351
+ }
1352
+
1353
+ @ReactMethod
1354
+ public void removeStorer(String seedString, String storage, long poolID, String cid, Promise promise) {
1355
+ ThreadUtils.runOnExecutor(() -> {
1356
+ Log.d("ReactNative", "removeStorer: seedString = " + seedString + "; storage = " + storage + "; poolID = " + poolID + "; cid = " + cid);
1357
+ try {
1358
+ byte[] result = this.fula.manifestRemoveStorer(seedString, storage, poolID, cid);
1359
+ String resultString = toString(result);
1360
+ promise.resolve(resultString);
1361
+ } catch (Exception e) {
1362
+ Log.d("get", e.getMessage());
1363
+ promise.reject(e);
1364
+ }
1365
+ });
1366
+ }
1367
+
1368
+ @ReactMethod
1369
+ public void removeStoredReplication(String seedString, String uploader, long poolID, String cid, Promise promise) {
1370
+ ThreadUtils.runOnExecutor(() -> {
1371
+ Log.d("ReactNative", "removeStoredReplication: seedString = " + seedString + "; uploader = " + uploader + "; poolID = " + poolID + "; cid = " + cid);
1372
+ try {
1373
+ byte[] result = this.fula.manifestRemoveStored(seedString, uploader, poolID, cid);
1374
+ String resultString = toString(result);
1375
+ promise.resolve(resultString);
1376
+ } catch (Exception e) {
1377
+ Log.d("get", e.getMessage());
1378
+ promise.reject(e);
1379
+ }
1380
+ });
1381
+ }
1382
+
1383
+ ////////////////////////////////////////////////////////////////
1384
+ ///////////////// Blox Hardware Methods ////////////////////////
1385
+ ////////////////////////////////////////////////////////////////
1386
+
1387
+ @ReactMethod
1388
+ public void bloxFreeSpace(Promise promise) {
1389
+ ThreadUtils.runOnExecutor(() -> {
1390
+ Log.d("ReactNative", "bloxFreeSpace");
1391
+ try {
1392
+ byte[] result = this.fula.bloxFreeSpace();
1393
+ String resultString = toString(result);
1394
+ Log.d("ReactNative", "result string="+resultString);
1395
+ promise.resolve(resultString);
1396
+ } catch (Exception e) {
1397
+ Log.d("ReactNative", e.getMessage());
1398
+ promise.reject(e);
1399
+ }
1400
+ });
1401
+ }
1402
+
1403
+ @ReactMethod
1404
+ public void wifiRemoveall(Promise promise) {
1405
+ ThreadUtils.runOnExecutor(() -> {
1406
+ Log.d("ReactNative", "wifiRemoveall");
1407
+ try {
1408
+ byte[] result = this.fula.wifiRemoveall();
1409
+ String resultString = toString(result);
1410
+ Log.d("ReactNative", "result string="+resultString);
1411
+ promise.resolve(resultString);
1412
+ } catch (Exception e) {
1413
+ Log.d("ReactNative", e.getMessage());
1414
+ promise.reject(e);
1415
+ }
1416
+ });
1417
+ }
1418
+
1419
+ @ReactMethod
1420
+ public void reboot(Promise promise) {
1421
+ ThreadUtils.runOnExecutor(() -> {
1422
+ Log.d("ReactNative", "reboot");
1423
+ try {
1424
+ byte[] result = this.fula.reboot();
1425
+ String resultString = toString(result);
1426
+ Log.d("ReactNative", "result string="+resultString);
1427
+ promise.resolve(resultString);
1428
+ } catch (Exception e) {
1429
+ Log.d("ReactNative", e.getMessage());
1430
+ promise.reject(e);
1431
+ }
1432
+ });
1433
+ }
1434
+
1435
+ @ReactMethod
1436
+ public void testData(String identityString, String bloxAddr, Promise promise) {
1437
+ try {
1438
+ byte[] identity = toByte(identityString);
1439
+ byte[] peerIdByte = this.newClientInternal(identity, "", bloxAddr, "", true, true, true);
1440
+
1441
+ String peerIdReturned = Arrays.toString(peerIdByte);
1442
+ Log.d("ReactNative", "newClient peerIdReturned= " + peerIdReturned);
1443
+ String peerId = this.fula.id();
1444
+ Log.d("ReactNative", "newClient peerId= " + peerId);
1445
+ byte[] bytes = {1, 85, 18, 32, 11, -31, 75, -78, -109, 11, -111, 97, -47, -78, -22, 84, 39, -117, -64, -70, -91, 55, -23, -80, 116, -123, -97, -26, 126, -70, -76, 35, 54, -106, 55, -9};
1446
+
1447
+ byte[] key = this.fula.put(bytes, 85);
1448
+ Log.d("ReactNative", "testData put result string="+Arrays.toString(key));
1449
+
1450
+ byte[] value = this.fula.get(key);
1451
+ Log.d("ReactNative", "testData get result string="+Arrays.toString(value));
1452
+
1453
+ this.fula.push(key);
1454
+ //this.fula.flush();
1455
+
1456
+ byte[] fetchedVal = this.fula.get(key);
1457
+ this.fula.pull(key);
1458
+
1459
+ promise.resolve(Arrays.toString(fetchedVal));
1460
+ } catch (Exception e) {
1461
+ Log.d("ReactNative", "ERROR:" + e.getMessage());
1462
+ promise.reject(e);
1463
+ }
1464
+ }
1465
+
1466
+ }