@functionland/react-native-fula 1.0.0 → 1.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +190 -133
- package/android/.gradle/7.5.1/checksums/checksums.lock +0 -0
- package/android/.gradle/7.5.1/checksums/md5-checksums.bin +0 -0
- package/android/.gradle/7.5.1/checksums/sha1-checksums.bin +0 -0
- package/android/.gradle/7.5.1/dependencies-accessors/dependencies-accessors.lock +0 -0
- package/android/.gradle/7.5.1/dependencies-accessors/gc.properties +0 -0
- package/android/.gradle/7.5.1/executionHistory/executionHistory.bin +0 -0
- package/android/.gradle/7.5.1/executionHistory/executionHistory.lock +0 -0
- package/android/.gradle/7.5.1/fileChanges/last-build.bin +0 -0
- package/android/.gradle/7.5.1/fileHashes/fileHashes.bin +0 -0
- package/android/.gradle/7.5.1/fileHashes/fileHashes.lock +0 -0
- package/android/.gradle/7.5.1/fileHashes/resourceHashesCache.bin +0 -0
- package/android/.gradle/7.5.1/gc.properties +0 -0
- package/android/.gradle/buildOutputCleanup/buildOutputCleanup.lock +0 -0
- package/android/.gradle/buildOutputCleanup/cache.properties +2 -2
- package/android/.gradle/buildOutputCleanup/outputFiles.bin +0 -0
- package/android/.gradle/file-system.probe +0 -0
- package/android/.gradle/vcs-1/gc.properties +0 -0
- package/android/.idea/compiler.xml +5 -5
- package/android/.idea/gradle.xml +18 -17
- package/android/.idea/jarRepositories.xml +49 -44
- package/android/.idea/misc.xml +9 -9
- package/android/.idea/sonarlint/issuestore/index.pb +0 -0
- package/android/.idea/vcs.xml +5 -5
- package/android/build.gradle +69 -69
- package/android/gradle/wrapper/gradle-wrapper.properties +7 -7
- package/android/gradle.properties +1 -1
- package/android/gradlew +291 -375
- package/android/local.properties +8 -8
- package/android/src/main/AndroidManifest.xml +4 -4
- package/android/src/main/java/land/fx/fula/ConfigRef.java +7 -7
- package/android/src/main/java/land/fx/fula/Cryptography.java +47 -47
- package/android/src/main/java/land/fx/fula/FulaModule.java +1248 -701
- package/android/src/main/java/land/fx/fula/FulaPackage.java +2 -2
- package/android/src/main/java/land/fx/fula/SharedPreferenceHelper.java +65 -65
- package/android/src/main/java/land/fx/fula/StaticHelper.java +13 -13
- package/android/src/main/java/land/fx/fula/ThreadUtils.java +42 -42
- package/ios/FulaModule.h +10 -10
- package/ios/FulaModule.m +149 -149
- package/lib/commonjs/index.js +5 -1
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/interfaces/api-lookup.js +1653 -0
- package/lib/commonjs/interfaces/api-lookup.js.map +1 -0
- package/lib/commonjs/interfaces/fulaNativeModule.js +1 -1
- package/lib/commonjs/interfaces/fulaNativeModule.js.map +1 -1
- package/lib/commonjs/protocols/blockchain.js +404 -0
- package/lib/commonjs/protocols/blockchain.js.map +1 -0
- package/lib/commonjs/protocols/chain-api.js +87 -0
- package/lib/commonjs/protocols/chain-api.js.map +1 -0
- package/lib/commonjs/protocols/fula.js +138 -82
- package/lib/commonjs/protocols/fula.js.map +1 -1
- package/lib/commonjs/types/blockchain.js +2 -0
- package/lib/commonjs/types/blockchain.js.map +1 -0
- package/lib/module/index.js +4 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/interfaces/api-lookup.js +1648 -0
- package/lib/module/interfaces/api-lookup.js.map +1 -0
- package/lib/module/interfaces/fulaNativeModule.js +1 -1
- package/lib/module/interfaces/fulaNativeModule.js.map +1 -1
- package/lib/module/protocols/blockchain.js +383 -0
- package/lib/module/protocols/blockchain.js.map +1 -0
- package/lib/module/protocols/chain-api.js +76 -0
- package/lib/module/protocols/chain-api.js.map +1 -0
- package/lib/module/protocols/fula.js +133 -81
- package/lib/module/protocols/fula.js.map +1 -1
- package/lib/module/types/blockchain.js +2 -0
- package/lib/module/types/blockchain.js.map +1 -0
- package/lib/typescript/index.d.ts +3 -1
- package/lib/typescript/interfaces/api-lookup.d.ts +1643 -0
- package/lib/typescript/interfaces/fulaNativeModule.d.ts +57 -28
- package/lib/typescript/protocols/blockchain.d.ts +17 -0
- package/lib/typescript/protocols/chain-api.d.ts +6 -0
- package/lib/typescript/protocols/fula.d.ts +127 -99
- package/lib/typescript/types/blockchain.d.ts +68 -0
- package/package.json +157 -149
- package/react-native-fula.podspec +19 -19
- package/src/index.tsx +3 -1
- package/src/interfaces/api-lookup.ts +1647 -0
- package/src/interfaces/fulaNativeModule.ts +85 -52
- package/src/protocols/blockchain.ts +460 -0
- package/src/protocols/chain-api.ts +80 -0
- package/src/protocols/fula.ts +283 -212
- package/src/types/blockchain.ts +81 -0
|
@@ -1,701 +1,1248 @@
|
|
|
1
|
-
package land.fx.fula;
|
|
2
|
-
|
|
3
|
-
import android.util.Log;
|
|
4
|
-
|
|
5
|
-
import androidx.annotation.NonNull;
|
|
6
|
-
|
|
7
|
-
import com.facebook.react.bridge.Promise;
|
|
8
|
-
import com.facebook.react.bridge.ReactApplicationContext;
|
|
9
|
-
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
|
10
|
-
import com.facebook.react.bridge.ReactMethod;
|
|
11
|
-
import com.facebook.react.bridge.WritableMap;
|
|
12
|
-
import com.facebook.react.bridge.WritableNativeMap;
|
|
13
|
-
import com.facebook.react.module.annotations.ReactModule;
|
|
14
|
-
|
|
15
|
-
import org.apache.commons.io.FileUtils;
|
|
16
|
-
import org.jetbrains.annotations.Contract;
|
|
17
|
-
|
|
18
|
-
import java.io.File;
|
|
19
|
-
import java.nio.charset.StandardCharsets;
|
|
20
|
-
import java.util.Arrays;
|
|
21
|
-
|
|
22
|
-
import javax.crypto.SecretKey;
|
|
23
|
-
|
|
24
|
-
import org.json.JSONObject;
|
|
25
|
-
import org.json.JSONArray;
|
|
26
|
-
|
|
27
|
-
import fulamobile.Config;
|
|
28
|
-
import fulamobile.Fulamobile;
|
|
29
|
-
|
|
30
|
-
import land.fx.wnfslib.Fs;
|
|
31
|
-
|
|
32
|
-
@ReactModule(name = FulaModule.NAME)
|
|
33
|
-
public class FulaModule extends ReactContextBaseJavaModule {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
@Override
|
|
37
|
-
public void initialize() {
|
|
38
|
-
System.loadLibrary("wnfslib");
|
|
39
|
-
System.loadLibrary("gojni");
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
public static final String NAME = "FulaModule";
|
|
44
|
-
fulamobile.Client fula;
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
String
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
Log.d(NAME, "
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
promise.resolve(
|
|
184
|
-
} catch (Exception e) {
|
|
185
|
-
Log.d("ReactNative", "
|
|
186
|
-
promise.reject("Error", e.getMessage());
|
|
187
|
-
}
|
|
188
|
-
});
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
@
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
}
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
Log.d("ReactNative", "
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
}
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
String
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
this.
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
}
|
|
598
|
-
Log.d("
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
}
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
1
|
+
package land.fx.fula;
|
|
2
|
+
|
|
3
|
+
import android.util.Log;
|
|
4
|
+
|
|
5
|
+
import androidx.annotation.NonNull;
|
|
6
|
+
|
|
7
|
+
import com.facebook.react.bridge.Promise;
|
|
8
|
+
import com.facebook.react.bridge.ReactApplicationContext;
|
|
9
|
+
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
|
10
|
+
import com.facebook.react.bridge.ReactMethod;
|
|
11
|
+
import com.facebook.react.bridge.WritableMap;
|
|
12
|
+
import com.facebook.react.bridge.WritableNativeMap;
|
|
13
|
+
import com.facebook.react.module.annotations.ReactModule;
|
|
14
|
+
|
|
15
|
+
import org.apache.commons.io.FileUtils;
|
|
16
|
+
import org.jetbrains.annotations.Contract;
|
|
17
|
+
|
|
18
|
+
import java.io.File;
|
|
19
|
+
import java.nio.charset.StandardCharsets;
|
|
20
|
+
import java.util.Arrays;
|
|
21
|
+
|
|
22
|
+
import javax.crypto.SecretKey;
|
|
23
|
+
|
|
24
|
+
import org.json.JSONObject;
|
|
25
|
+
import org.json.JSONArray;
|
|
26
|
+
|
|
27
|
+
import fulamobile.Config;
|
|
28
|
+
import fulamobile.Fulamobile;
|
|
29
|
+
|
|
30
|
+
import land.fx.wnfslib.Fs;
|
|
31
|
+
|
|
32
|
+
@ReactModule(name = FulaModule.NAME)
|
|
33
|
+
public class FulaModule extends ReactContextBaseJavaModule {
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@Override
|
|
37
|
+
public void initialize() {
|
|
38
|
+
System.loadLibrary("wnfslib");
|
|
39
|
+
System.loadLibrary("gojni");
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
public static final String NAME = "FulaModule";
|
|
44
|
+
fulamobile.Client fula;
|
|
45
|
+
|
|
46
|
+
Client client;
|
|
47
|
+
Config fulaConfig;
|
|
48
|
+
String appDir;
|
|
49
|
+
String fulaStorePath;
|
|
50
|
+
String privateForest;
|
|
51
|
+
land.fx.wnfslib.Config rootConfig;
|
|
52
|
+
SharedPreferenceHelper sharedPref;
|
|
53
|
+
SecretKey secretKeyGlobal;
|
|
54
|
+
String identityEncryptedGlobal;
|
|
55
|
+
static String PRIVATE_KEY_STORE_ID = "PRIVATE_KEY";
|
|
56
|
+
|
|
57
|
+
public static class Client implements land.fx.wnfslib.Datastore {
|
|
58
|
+
|
|
59
|
+
private final fulamobile.Client internalClient;
|
|
60
|
+
|
|
61
|
+
Client(fulamobile.Client clientInput) {
|
|
62
|
+
this.internalClient = clientInput;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
@NonNull
|
|
66
|
+
@Override
|
|
67
|
+
public byte[] get(@NonNull byte[] cid) {
|
|
68
|
+
try {
|
|
69
|
+
Log.d("ReactNative", Arrays.toString(cid));
|
|
70
|
+
return this.internalClient.get(cid);
|
|
71
|
+
} catch (Exception e) {
|
|
72
|
+
e.printStackTrace();
|
|
73
|
+
}
|
|
74
|
+
Log.d("ReactNative","Error get");
|
|
75
|
+
return cid;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
@NonNull
|
|
79
|
+
@Override
|
|
80
|
+
public byte[] put(@NonNull byte[] data, long codec) {
|
|
81
|
+
try {
|
|
82
|
+
//Log.d("ReactNative", "data="+ Arrays.toString(data) +" ;codec="+codec);
|
|
83
|
+
return this.internalClient.put(data, codec);
|
|
84
|
+
} catch (Exception e) {
|
|
85
|
+
Log.d("ReactNative", "put Error="+e.getMessage());
|
|
86
|
+
e.printStackTrace();
|
|
87
|
+
}
|
|
88
|
+
Log.d("ReactNative","Error put");
|
|
89
|
+
return data;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
public FulaModule(ReactApplicationContext reactContext) {
|
|
94
|
+
super(reactContext);
|
|
95
|
+
appDir = reactContext.getFilesDir().toString();
|
|
96
|
+
fulaStorePath = appDir + "/fula";
|
|
97
|
+
File storeDir = new File(fulaStorePath);
|
|
98
|
+
sharedPref = SharedPreferenceHelper.getInstance(reactContext.getApplicationContext());
|
|
99
|
+
boolean success = true;
|
|
100
|
+
if (!storeDir.exists()) {
|
|
101
|
+
success = storeDir.mkdirs();
|
|
102
|
+
}
|
|
103
|
+
if (success) {
|
|
104
|
+
Log.d(NAME, "Fula store folder created");
|
|
105
|
+
} else {
|
|
106
|
+
Log.d(NAME, "Unable to create fula store folder!");
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
@Override
|
|
111
|
+
@NonNull
|
|
112
|
+
public java.lang.String getName() {
|
|
113
|
+
return NAME;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
private byte[] toByte(@NonNull String input) {
|
|
118
|
+
return input.getBytes(StandardCharsets.UTF_8);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
@NonNull
|
|
122
|
+
@Contract("_ -> new")
|
|
123
|
+
public String toString(byte[] input) {
|
|
124
|
+
return new String(input, StandardCharsets.UTF_8);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
@NonNull
|
|
128
|
+
private static int[] stringArrToIntArr(@NonNull String[] s) {
|
|
129
|
+
int[] result = new int[s.length];
|
|
130
|
+
for (int i = 0; i < s.length; i++) {
|
|
131
|
+
result[i] = Integer.parseInt(s[i]);
|
|
132
|
+
}
|
|
133
|
+
return result;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
@NonNull
|
|
137
|
+
@Contract(pure = true)
|
|
138
|
+
private static byte[] convertIntToByte(@NonNull int[] input) {
|
|
139
|
+
byte[] result = new byte[input.length];
|
|
140
|
+
for (int i = 0; i < input.length; i++) {
|
|
141
|
+
byte b = (byte) input[i];
|
|
142
|
+
result[i] = b;
|
|
143
|
+
}
|
|
144
|
+
return result;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
@NonNull
|
|
148
|
+
private byte[] convertStringToByte(@NonNull String data) {
|
|
149
|
+
String[] keyInt_S = data.split(",");
|
|
150
|
+
int[] keyInt = stringArrToIntArr(keyInt_S);
|
|
151
|
+
|
|
152
|
+
return convertIntToByte(keyInt);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
@ReactMethod
|
|
156
|
+
public void checkConnection(Promise promise) {
|
|
157
|
+
Log.d("ReactNative", "checkConnection started");
|
|
158
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
159
|
+
if (this.fula != null) {
|
|
160
|
+
try {
|
|
161
|
+
boolean connectionStatus = this.checkConnectionInternal();
|
|
162
|
+
promise.resolve(connectionStatus);
|
|
163
|
+
}
|
|
164
|
+
catch (Exception e) {
|
|
165
|
+
Log.d("ReactNative", "checkConnection failed with Error: " + e.getMessage());
|
|
166
|
+
promise.resolve(false);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
@ReactMethod
|
|
173
|
+
public void newClient(String identityString, String storePath, String bloxAddr, String exchange, boolean autoFlush, boolean useRelay, Promise promise) {
|
|
174
|
+
Log.d("ReactNative", "newClient started");
|
|
175
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
176
|
+
try {
|
|
177
|
+
Log.d("ReactNative", "newClient storePath= " + storePath);
|
|
178
|
+
byte[] identity = toByte(identityString);
|
|
179
|
+
Log.d("ReactNative", "newClient identity= " + identityString);
|
|
180
|
+
this.newClientInternal(identity, storePath, bloxAddr, exchange, autoFlush, useRelay);
|
|
181
|
+
//String objString = Arrays.toString(obj);
|
|
182
|
+
String peerId = this.fula.id();
|
|
183
|
+
promise.resolve(peerId);
|
|
184
|
+
} catch (Exception e) {
|
|
185
|
+
Log.d("ReactNative", "newClient failed with Error: " + e.getMessage());
|
|
186
|
+
promise.reject("Error", e.getMessage());
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
@ReactMethod
|
|
192
|
+
public void isReady(boolean filesystemCheck, Promise promise) {
|
|
193
|
+
Log.d("ReactNative", "isReady started");
|
|
194
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
195
|
+
boolean initialized = false;
|
|
196
|
+
try {
|
|
197
|
+
if (this.fula != null && this.fula.id() != null) {
|
|
198
|
+
if (filesystemCheck) {
|
|
199
|
+
if (this.client != null && this.rootConfig != null && !this.rootConfig.getCid().isEmpty()) {
|
|
200
|
+
initialized = true;
|
|
201
|
+
}
|
|
202
|
+
} else {
|
|
203
|
+
initialized = true;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
promise.resolve(initialized);
|
|
207
|
+
} catch (Exception e) {
|
|
208
|
+
Log.d("ReactNative", "isReady failed with Error: " + e.getMessage());
|
|
209
|
+
promise.reject("Error", e.getMessage());
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
@ReactMethod
|
|
215
|
+
public void init(String identityString, String storePath, String bloxAddr, String exchange, boolean autoFlush, String rootConfig, boolean useRelay, boolean refresh, Promise promise) {
|
|
216
|
+
Log.d("ReactNative", "init started");
|
|
217
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
218
|
+
try {
|
|
219
|
+
WritableMap resultData = new WritableNativeMap();
|
|
220
|
+
Log.d("ReactNative", "init storePath= " + storePath);
|
|
221
|
+
byte[] identity = toByte(identityString);
|
|
222
|
+
Log.d("ReactNative", "init identity= " + identityString);
|
|
223
|
+
String[] obj = this.initInternal(identity, storePath, bloxAddr, exchange, autoFlush, rootConfig, useRelay, refresh);
|
|
224
|
+
Log.d("ReactNative", "init object created: [ " + obj[0] + ", " + obj[1] + ", " + obj[2] + " ]");
|
|
225
|
+
resultData.putString("peerId", obj[0]);
|
|
226
|
+
resultData.putString("rootCid", obj[1]);
|
|
227
|
+
resultData.putString("private_ref", obj[2]);
|
|
228
|
+
promise.resolve(resultData);
|
|
229
|
+
} catch (Exception e) {
|
|
230
|
+
Log.d("ReactNative", "init failed with Error: " + e.getMessage());
|
|
231
|
+
promise.reject("Error", e.getMessage());
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
@ReactMethod
|
|
237
|
+
public void logout(String identityString, String storePath, Promise promise) {
|
|
238
|
+
Log.d("ReactNative", "logout started");
|
|
239
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
240
|
+
try {
|
|
241
|
+
byte[] identity = toByte(identityString);
|
|
242
|
+
boolean obj = this.logoutInternal(identity, storePath);
|
|
243
|
+
Log.d("ReactNative", "logout completed");
|
|
244
|
+
promise.resolve(obj);
|
|
245
|
+
} catch (Exception e) {
|
|
246
|
+
Log.d("ReactNative", "logout failed with Error: " + e.getMessage());
|
|
247
|
+
promise.reject("Error", e.getMessage());
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
private boolean checkConnectionInternal() throws Exception {
|
|
253
|
+
try {
|
|
254
|
+
Log.d("ReactNative", "checkConnectionInternal started");
|
|
255
|
+
if (this.fula != null) {
|
|
256
|
+
try {
|
|
257
|
+
Log.d("ReactNative", "connectToBlox started");
|
|
258
|
+
this.fula.connectToBlox();
|
|
259
|
+
return true;
|
|
260
|
+
}
|
|
261
|
+
catch (Exception e) {
|
|
262
|
+
Log.d("ReactNative", "checkConnectionInternal failed with Error: " + e.getMessage());
|
|
263
|
+
return false;
|
|
264
|
+
}
|
|
265
|
+
} else {
|
|
266
|
+
Log.d("ReactNative", "checkConnectionInternal failed because fula is not initialized ");
|
|
267
|
+
return false;
|
|
268
|
+
}
|
|
269
|
+
} catch (Exception e) {
|
|
270
|
+
Log.d("ReactNative", "checkConnectionInternal failed with Error: " + e.getMessage());
|
|
271
|
+
throw (e);
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
@ReactMethod
|
|
276
|
+
private void checkFailedActions(boolean retry, Promise promise) throws Exception {
|
|
277
|
+
try {
|
|
278
|
+
if (this.fula != null) {
|
|
279
|
+
if (!retry) {
|
|
280
|
+
Log.d("ReactNative", "checkFailedActions without retry");
|
|
281
|
+
fulamobile.LinkIterator failedLinks = this.fula.listFailedPushes();
|
|
282
|
+
if (failedLinks.hasNext()) {
|
|
283
|
+
Log.d("ReactNative", "checkFailedActions found: "+Arrays.toString(failedLinks.next()));
|
|
284
|
+
promise.resolve(true);
|
|
285
|
+
} else {
|
|
286
|
+
promise.resolve(false);
|
|
287
|
+
}
|
|
288
|
+
} else {
|
|
289
|
+
Log.d("ReactNative", "checkFailedActions with retry");
|
|
290
|
+
boolean retryResults = this.retryFailedActionsInternal();
|
|
291
|
+
promise.resolve(!retryResults);
|
|
292
|
+
}
|
|
293
|
+
} else {
|
|
294
|
+
throw new Exception("Fula is not initialized");
|
|
295
|
+
}
|
|
296
|
+
} catch (Exception e) {
|
|
297
|
+
Log.d("ReactNative", "checkFailedActions failed with Error: " + e.getMessage());
|
|
298
|
+
throw (e);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
private boolean retryFailedActionsInternal() throws Exception {
|
|
303
|
+
try {
|
|
304
|
+
Log.d("ReactNative", "retryFailedActionsInternal started");
|
|
305
|
+
if (this.fula != null) {
|
|
306
|
+
//Fula is initialized
|
|
307
|
+
try {
|
|
308
|
+
boolean connectionCheck = this.checkConnectionInternal();
|
|
309
|
+
if(connectionCheck) {
|
|
310
|
+
try {
|
|
311
|
+
Log.d("ReactNative", "retryFailedPushes started");
|
|
312
|
+
this.fula.retryFailedPushes();
|
|
313
|
+
Log.d("ReactNative", "flush started");
|
|
314
|
+
this.fula.flush();
|
|
315
|
+
return true;
|
|
316
|
+
}
|
|
317
|
+
catch (Exception e) {
|
|
318
|
+
this.fula.flush();
|
|
319
|
+
Log.d("ReactNative", "retryFailedActionsInternal failed with Error: " + e.getMessage());
|
|
320
|
+
return false;
|
|
321
|
+
}
|
|
322
|
+
//Blox online
|
|
323
|
+
/*fulamobile.LinkIterator failedLinks = this.fula.listFailedPushes();
|
|
324
|
+
if (failedLinks.hasNext()) {
|
|
325
|
+
Log.d("ReactNative", "Failed links");
|
|
326
|
+
//Failed list is not empty. iterate in the list
|
|
327
|
+
while (failedLinks.hasNext()) {
|
|
328
|
+
//Get the missing key
|
|
329
|
+
byte[] failedNode = failedLinks.next();
|
|
330
|
+
try {
|
|
331
|
+
//Push to Blox
|
|
332
|
+
Log.d("ReactNative", "Pushing Failed links "+Arrays.toString(failedNode));
|
|
333
|
+
this.pushInternal(failedNode);
|
|
334
|
+
Log.d("ReactNative", "Failed links pushed");
|
|
335
|
+
}
|
|
336
|
+
catch (Exception e) {
|
|
337
|
+
Log.d("ReactNative", "retryFailedActionsInternal failed with Error: " + e.getMessage());
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
//check if list is empty now and all are pushed
|
|
341
|
+
Log.d("ReactNative", "Pushing finished");
|
|
342
|
+
fulamobile.LinkIterator failedLinks_after = this.fula.listFailedPushes();
|
|
343
|
+
if(failedLinks_after.hasNext()) {
|
|
344
|
+
//Some pushes failed
|
|
345
|
+
byte[] first_failed = failedLinks_after.next();
|
|
346
|
+
Log.d("ReactNative", "Failed links are not empty "+Arrays.toString(first_failed));
|
|
347
|
+
return false;
|
|
348
|
+
} else {
|
|
349
|
+
//All pushes successful
|
|
350
|
+
return true;
|
|
351
|
+
}
|
|
352
|
+
} else {
|
|
353
|
+
Log.d("ReactNative", "No Failed links");
|
|
354
|
+
//Failed list is empty
|
|
355
|
+
return true;
|
|
356
|
+
}*/
|
|
357
|
+
} else {
|
|
358
|
+
Log.d("ReactNative", "retryFailedActions failed because blox is offline");
|
|
359
|
+
//Blox Offline
|
|
360
|
+
return false;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
catch (Exception e) {
|
|
364
|
+
Log.d("ReactNative", "retryFailedActions failed with Error: " + e.getMessage());
|
|
365
|
+
return false;
|
|
366
|
+
}
|
|
367
|
+
} else {
|
|
368
|
+
Log.d("ReactNative", "retryFailedActions failed because fula is not initialized");
|
|
369
|
+
//Fula is not initialized
|
|
370
|
+
return false;
|
|
371
|
+
}
|
|
372
|
+
} catch (Exception e) {
|
|
373
|
+
Log.d("ReactNative", "retryFailedActions failed with Error: " + e.getMessage());
|
|
374
|
+
throw (e);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
@NonNull
|
|
379
|
+
private byte[] createPeerIdentity(byte[] privateKey) throws Exception {
|
|
380
|
+
try {
|
|
381
|
+
// 1: First: create public key from provided private key
|
|
382
|
+
// 2: Should read the local keychain store (if it is key-value, key is public key above,
|
|
383
|
+
// 3: if found, decrypt using the private key
|
|
384
|
+
// 4: If not found or decryption not successful, generate an identity
|
|
385
|
+
// 5: then encrypt and store in keychain
|
|
386
|
+
|
|
387
|
+
String encryptedKey = sharedPref.getValue(PRIVATE_KEY_STORE_ID);
|
|
388
|
+
SecretKey secretKey = Cryptography.generateKey(privateKey);
|
|
389
|
+
if (encryptedKey == null) {
|
|
390
|
+
byte[] autoGeneratedIdentity = Fulamobile.generateEd25519Key();
|
|
391
|
+
encryptedKey = Cryptography.encryptMsg(StaticHelper.bytesToBase64(autoGeneratedIdentity), secretKey);
|
|
392
|
+
sharedPref.add(PRIVATE_KEY_STORE_ID, encryptedKey);
|
|
393
|
+
}
|
|
394
|
+
return StaticHelper.base64ToBytes(Cryptography.decryptMsg(encryptedKey, secretKey));
|
|
395
|
+
|
|
396
|
+
} catch (Exception e) {
|
|
397
|
+
Log.d("ReactNative", "createPeerIdentity failed with Error: " + e.getMessage());
|
|
398
|
+
throw (e);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
private void createNewRootConfig(FulaModule.Client iClient, byte[] identity) throws Exception {
|
|
403
|
+
this.privateForest = Fs.createPrivateForest(iClient);
|
|
404
|
+
Log.d("ReactNative", "privateForest is created: " + this.privateForest);
|
|
405
|
+
this.rootConfig = Fs.createRootDir(iClient, this.privateForest, identity);
|
|
406
|
+
if (this.fula != null) {
|
|
407
|
+
this.fula.flush();
|
|
408
|
+
}
|
|
409
|
+
Log.d("ReactNative", "new rootConfig is created: cid=" + this.rootConfig.getCid()+" & private_ref="+this.rootConfig.getPrivate_ref());
|
|
410
|
+
|
|
411
|
+
this.encrypt_and_store_config();
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
private String getPrivateRef(FulaModule.Client iClient, byte[] wnfsKey, String rootCid) throws Exception {
|
|
415
|
+
Log.d("ReactNative", "getPrivateRef called: rootCid=" + rootCid);
|
|
416
|
+
String privateRef = Fs.getPrivateRef(iClient, wnfsKey, rootCid);
|
|
417
|
+
Log.d("ReactNative", "getPrivateRef completed: privateRef=" + privateRef);
|
|
418
|
+
return privateRef;
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
private boolean encrypt_and_store_config() throws Exception {
|
|
422
|
+
try {
|
|
423
|
+
if(this.identityEncryptedGlobal != null && !this.identityEncryptedGlobal.isEmpty()) {
|
|
424
|
+
String cid_encrypted = Cryptography.encryptMsg(this.rootConfig.getCid(), this.secretKeyGlobal);
|
|
425
|
+
String private_ref_encrypted = Cryptography.encryptMsg(this.rootConfig.getPrivate_ref(), this.secretKeyGlobal);
|
|
426
|
+
|
|
427
|
+
sharedPref.add("cid_encrypted_" + this.identityEncryptedGlobal, cid_encrypted);
|
|
428
|
+
sharedPref.add("private_ref_encrypted_" + this.identityEncryptedGlobal, private_ref_encrypted);
|
|
429
|
+
return true;
|
|
430
|
+
} else {
|
|
431
|
+
return false;
|
|
432
|
+
}
|
|
433
|
+
} catch (Exception e) {
|
|
434
|
+
Log.d("ReactNative", "encrypt_and_store_config failed with Error: " + e.getMessage());
|
|
435
|
+
throw (e);
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
private boolean logoutInternal(byte[] identity, String storePath) throws Exception {
|
|
440
|
+
try {
|
|
441
|
+
if (this.fula != null) {
|
|
442
|
+
this.fula.flush();
|
|
443
|
+
}
|
|
444
|
+
SecretKey secretKey = Cryptography.generateKey(identity);
|
|
445
|
+
String identity_encrypted = Cryptography.encryptMsg(Arrays.toString(identity), secretKey);
|
|
446
|
+
sharedPref.remove("cid_encrypted_"+ identity_encrypted);
|
|
447
|
+
sharedPref.remove("private_ref_encrypted_"+identity_encrypted);
|
|
448
|
+
|
|
449
|
+
//TODO: Should also remove peerid @Mahdi
|
|
450
|
+
|
|
451
|
+
sharedPref.remove("cid_encrypted_"+ identity_encrypted);
|
|
452
|
+
sharedPref.remove("private_ref_encrypted_"+ identity_encrypted);
|
|
453
|
+
|
|
454
|
+
this.rootConfig = null;
|
|
455
|
+
this.secretKeyGlobal = null;
|
|
456
|
+
this.identityEncryptedGlobal = null;
|
|
457
|
+
|
|
458
|
+
if (storePath == null || storePath.trim().isEmpty()) {
|
|
459
|
+
storePath = this.fulaStorePath;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
File file = new File(storePath);
|
|
463
|
+
FileUtils.deleteDirectory(file);
|
|
464
|
+
return true;
|
|
465
|
+
|
|
466
|
+
} catch (Exception e) {
|
|
467
|
+
Log.d("ReactNative", "logout internal failed with Error: " + e.getMessage());
|
|
468
|
+
throw (e);
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
public fulamobile.Client getFulaClient() {
|
|
473
|
+
return this.fula;
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
@NonNull
|
|
477
|
+
private byte[] newClientInternal(byte[] identity, String storePath, String bloxAddr, String exchange, boolean autoFlush, boolean useRelay) throws Exception {
|
|
478
|
+
try {
|
|
479
|
+
fulaConfig = new Config();
|
|
480
|
+
if (storePath == null || storePath.trim().isEmpty()) {
|
|
481
|
+
fulaConfig.setStorePath(this.fulaStorePath);
|
|
482
|
+
} else {
|
|
483
|
+
fulaConfig.setStorePath(storePath);
|
|
484
|
+
}
|
|
485
|
+
Log.d("ReactNative", "storePath is set: " + fulaConfig.getStorePath());
|
|
486
|
+
|
|
487
|
+
byte[] peerIdentity = this.createPeerIdentity(identity);
|
|
488
|
+
fulaConfig.setIdentity(peerIdentity);
|
|
489
|
+
Log.d("ReactNative", "peerIdentity is set: " + toString(fulaConfig.getIdentity()));
|
|
490
|
+
fulaConfig.setBloxAddr(bloxAddr);
|
|
491
|
+
Log.d("ReactNative", "bloxAddr is set: " + fulaConfig.getBloxAddr());
|
|
492
|
+
fulaConfig.setExchange(exchange);
|
|
493
|
+
fulaConfig.setSyncWrites(autoFlush);
|
|
494
|
+
if (useRelay) {
|
|
495
|
+
fulaConfig.setAllowTransientConnection(true);
|
|
496
|
+
fulaConfig.setForceReachabilityPrivate(true);
|
|
497
|
+
}
|
|
498
|
+
if (this.fula == null) {
|
|
499
|
+
Log.d("ReactNative", "Creating a new Fula instance");
|
|
500
|
+
this.fula = Fulamobile.newClient(fulaConfig);
|
|
501
|
+
}
|
|
502
|
+
if (this.fula != null) {
|
|
503
|
+
this.fula.flush();
|
|
504
|
+
}
|
|
505
|
+
return peerIdentity;
|
|
506
|
+
} catch (Exception e) {
|
|
507
|
+
Log.d("ReactNative", "newclientInternal failed with Error: " + e.getMessage());
|
|
508
|
+
throw (e);
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
@NonNull
|
|
513
|
+
private String[] initInternal(byte[] identity, String storePath, String bloxAddr, String exchange, boolean autoFlush, String rootCid, boolean useRelay, boolean refresh) throws Exception {
|
|
514
|
+
try {
|
|
515
|
+
if (this.fula == null || refresh) {
|
|
516
|
+
this.newClientInternal(identity, storePath, bloxAddr, exchange, autoFlush, useRelay);
|
|
517
|
+
}
|
|
518
|
+
if(this.client == null || refresh) {
|
|
519
|
+
this.client = new Client(this.fula);
|
|
520
|
+
Log.d("ReactNative", "fula initialized: " + this.fula.id());
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
SecretKey secretKey = Cryptography.generateKey(identity);
|
|
524
|
+
String identity_encrypted =Cryptography.encryptMsg(Arrays.toString(identity), secretKey);
|
|
525
|
+
this.identityEncryptedGlobal = identity_encrypted;
|
|
526
|
+
this.secretKeyGlobal = secretKey;
|
|
527
|
+
|
|
528
|
+
if (this.rootConfig == null || this.rootConfig.getCid().isEmpty() || this.rootConfig.getPrivate_ref().isEmpty()) {
|
|
529
|
+
Log.d("ReactNative", "this.rootCid is empty.");
|
|
530
|
+
//Load from keystore
|
|
531
|
+
|
|
532
|
+
String cid_encrypted_fetched = sharedPref.getValue("cid_encrypted_"+ identity_encrypted);
|
|
533
|
+
String private_ref_encrypted_fetched = sharedPref.getValue("private_ref_encrypted_"+identity_encrypted);
|
|
534
|
+
Log.d("ReactNative", "Here1");
|
|
535
|
+
String cid = "";
|
|
536
|
+
String private_ref = "";
|
|
537
|
+
if(cid_encrypted_fetched != null && !cid_encrypted_fetched.isEmpty()) {
|
|
538
|
+
Log.d("ReactNative", "decrypting cid="+cid_encrypted_fetched+" with secret="+secretKey.toString());
|
|
539
|
+
cid = Cryptography.decryptMsg(cid_encrypted_fetched, secretKey);
|
|
540
|
+
}
|
|
541
|
+
if(private_ref_encrypted_fetched != null && !private_ref_encrypted_fetched.isEmpty()) {
|
|
542
|
+
Log.d("ReactNative", "decrypting private_ref="+private_ref_encrypted_fetched+" with secret="+secretKey.toString());
|
|
543
|
+
private_ref = Cryptography.decryptMsg(private_ref_encrypted_fetched, secretKey);
|
|
544
|
+
}
|
|
545
|
+
Log.d("ReactNative", "Here2");
|
|
546
|
+
//Log.d("ReactNative", "Attempted to fetch cid from keystore; cid="+cid+" & private_ref="+private_ref);
|
|
547
|
+
if((cid == null || cid.isEmpty()) || (private_ref == null || private_ref.isEmpty()) ){
|
|
548
|
+
Log.d("ReactNative", "cid or PrivateRef was not found");
|
|
549
|
+
if(rootCid != null && !rootCid.isEmpty()){
|
|
550
|
+
Log.d("ReactNative", "Re-setting cid from input: "+rootCid);
|
|
551
|
+
cid = rootCid;
|
|
552
|
+
}
|
|
553
|
+
if((private_ref == null || private_ref.isEmpty()) && (cid != null && !cid.isEmpty())){
|
|
554
|
+
Log.d("ReactNative", "Re-fetching privateRef from wnfs: cid="+cid);
|
|
555
|
+
private_ref = this.getPrivateRef(this.client, identity, cid);
|
|
556
|
+
Log.d("ReactNative", "Re-fetching privateRef from wnfs: "+private_ref);
|
|
557
|
+
}
|
|
558
|
+
if(cid == null || cid.isEmpty() || private_ref == null || private_ref.isEmpty()) {
|
|
559
|
+
Log.d("ReactNative", "Tried to recover cid and privateRef but was not successful. Creating new ones");
|
|
560
|
+
this.createNewRootConfig(this.client, identity);
|
|
561
|
+
} else {
|
|
562
|
+
Log.d("ReactNative", "Tried to recover cid and privateRef and was successful. cid:"+cid+" & private_ref="+private_ref);
|
|
563
|
+
this.rootConfig = new land.fx.wnfslib.Config(cid, private_ref);
|
|
564
|
+
this.encrypt_and_store_config();
|
|
565
|
+
}
|
|
566
|
+
} else if(cid != null && !cid.isEmpty() && private_ref != null && !private_ref.isEmpty()) {
|
|
567
|
+
Log.d("ReactNative", "Found cid and private ref in keychain store");
|
|
568
|
+
if(cid != null && !cid.isEmpty() && private_ref != null && !private_ref.isEmpty()) {
|
|
569
|
+
Log.d("ReactNative", "Recovered cid and private ref from keychain store. cid="+cid+" & private_ref="+private_ref);
|
|
570
|
+
this.rootConfig = new land.fx.wnfslib.Config(cid, private_ref);
|
|
571
|
+
} else{
|
|
572
|
+
Log.d("ReactNative", "Found but Could not recover cid and private_ref from keychain store");
|
|
573
|
+
this.createNewRootConfig(this.client, identity);
|
|
574
|
+
}
|
|
575
|
+
} else{
|
|
576
|
+
Log.d("ReactNative", "This cid and private_ref generation should never happen!!!");
|
|
577
|
+
//Create new root and store cid and private_ref
|
|
578
|
+
this.createNewRootConfig(this.client, identity);
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
|
|
582
|
+
Log.d("ReactNative", "creating rootConfig completed");
|
|
583
|
+
|
|
584
|
+
/*
|
|
585
|
+
byte[] testbyte = convertStringToByte("-104,40,24,-93,24,100,24,114,24,111,24,111,24,116,24,-126,24,-126,0,0,24,-128,24,103,24,118,24,101,24,114,24,115,24,105,24,111,24,110,24,101,24,48,24,46,24,49,24,46,24,48,24,105,24,115,24,116,24,114,24,117,24,99,24,116,24,117,24,114,24,101,24,100,24,104,24,97,24,109,24,116");
|
|
586
|
+
long testcodec = 85;
|
|
587
|
+
byte[] testputcid = this.client.put(testbyte, testcodec);
|
|
588
|
+
Log.d("ReactNative", "client.put test done"+ Arrays.toString(testputcid));
|
|
589
|
+
byte[] testfetchedcid = convertStringToByte("1,113,18,32,-6,-63,-128,79,-102,-89,57,77,-8,67,-98,8,-81,40,-87,123,122,29,-52,-124,-60,-53,100,105,125,123,-5,-99,41,106,-124,-64");
|
|
590
|
+
byte[] testfetchedbytes = this.client.get(testfetchedcid);
|
|
591
|
+
Log.d("ReactNative", "client.get test done"+ Arrays.toString(testfetchedbytes));
|
|
592
|
+
*/
|
|
593
|
+
|
|
594
|
+
|
|
595
|
+
Log.d("ReactNative", "rootConfig is created: cid=" + this.rootConfig.getCid()+ "& private_ref="+this
|
|
596
|
+
.rootConfig.getPrivate_ref());
|
|
597
|
+
} else {
|
|
598
|
+
Log.d("ReactNative", "rootConfig existed: cid=" + this.rootConfig.getCid()+ " & private_ref="+this.rootConfig.getPrivate_ref());
|
|
599
|
+
}
|
|
600
|
+
String peerId = this.fula.id();
|
|
601
|
+
String[] obj = new String[3];
|
|
602
|
+
obj[0] = peerId;
|
|
603
|
+
obj[1] = this.rootConfig.getCid();
|
|
604
|
+
obj[2] = this.rootConfig.getPrivate_ref();
|
|
605
|
+
Log.d("ReactNative", "initInternal is completed successfully");
|
|
606
|
+
if (this.fula != null) {
|
|
607
|
+
this.fula.flush();
|
|
608
|
+
}
|
|
609
|
+
return obj;
|
|
610
|
+
} catch (Exception e) {
|
|
611
|
+
Log.d("ReactNative", "init internal failed with Error: " + e.getMessage());
|
|
612
|
+
throw (e);
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
@ReactMethod
|
|
617
|
+
public void mkdir(String path, Promise promise) {
|
|
618
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
619
|
+
Log.d("ReactNative", "mkdir: path = " + path);
|
|
620
|
+
try {
|
|
621
|
+
land.fx.wnfslib.Config config = Fs.mkdir(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), path);
|
|
622
|
+
if(config != null) {
|
|
623
|
+
this.rootConfig = config;
|
|
624
|
+
this.encrypt_and_store_config();
|
|
625
|
+
if (this.fula != null) {
|
|
626
|
+
this.fula.flush();
|
|
627
|
+
}
|
|
628
|
+
promise.resolve(config.getCid());
|
|
629
|
+
} else {
|
|
630
|
+
Log.d("ReactNative", "mkdir Error: config is null");
|
|
631
|
+
promise.reject(new Exception("mkdir Error: config is null"));
|
|
632
|
+
}
|
|
633
|
+
} catch (Exception e) {
|
|
634
|
+
Log.d("get", e.getMessage());
|
|
635
|
+
promise.reject(e);
|
|
636
|
+
}
|
|
637
|
+
});
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
@ReactMethod
|
|
641
|
+
public void writeFile(String fulaTargetFilename, String localFilename, Promise promise) {
|
|
642
|
+
/*
|
|
643
|
+
// reads content of the file form localFilename (should include full absolute path to local file with read permission
|
|
644
|
+
// writes content to the specified location by fulaTargetFilename in Fula filesystem
|
|
645
|
+
// fulaTargetFilename: a string including full path and filename of target file on Fula (e.g. root/pictures/cat.jpg)
|
|
646
|
+
// localFilename: a string containing full path and filename of local file on hte device (e.g /usr/bin/cat.jpg)
|
|
647
|
+
// Returns: new cid of the root after this file is placed in the tree
|
|
648
|
+
*/
|
|
649
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
650
|
+
Log.d("ReactNative", "writeFile to : path = " + fulaTargetFilename + ", from: " + localFilename);
|
|
651
|
+
try {
|
|
652
|
+
land.fx.wnfslib.Config config = Fs.writeFileFromPath(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), fulaTargetFilename, localFilename);
|
|
653
|
+
if(config != null) {
|
|
654
|
+
this.rootConfig = config;
|
|
655
|
+
this.encrypt_and_store_config();
|
|
656
|
+
if (this.fula != null) {
|
|
657
|
+
this.fula.flush();
|
|
658
|
+
}
|
|
659
|
+
promise.resolve(config.getCid());
|
|
660
|
+
} else {
|
|
661
|
+
Log.d("ReactNative", "writeFile Error: config is null");
|
|
662
|
+
promise.reject(new Exception("writeFile Error: config is null"));
|
|
663
|
+
}
|
|
664
|
+
} catch (Exception e) {
|
|
665
|
+
Log.d("get", e.getMessage());
|
|
666
|
+
promise.reject(e);
|
|
667
|
+
}
|
|
668
|
+
});
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
@ReactMethod
|
|
672
|
+
public void writeFileContent(String path, String contentString, Promise promise) {
|
|
673
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
674
|
+
Log.d("ReactNative", "writeFile: contentString = " + contentString);
|
|
675
|
+
Log.d("ReactNative", "writeFile: path = " + path);
|
|
676
|
+
try {
|
|
677
|
+
byte[] content = this.convertStringToByte(contentString);
|
|
678
|
+
land.fx.wnfslib.Config config = Fs.writeFile(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), path, content);
|
|
679
|
+
this.rootConfig = config;
|
|
680
|
+
this.encrypt_and_store_config();
|
|
681
|
+
if (this.fula != null) {
|
|
682
|
+
this.fula.flush();
|
|
683
|
+
}
|
|
684
|
+
promise.resolve(config.getCid());
|
|
685
|
+
} catch (Exception e) {
|
|
686
|
+
Log.d("get", e.getMessage());
|
|
687
|
+
promise.reject(e);
|
|
688
|
+
}
|
|
689
|
+
});
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
@ReactMethod
|
|
693
|
+
public void ls(String path, Promise promise) {
|
|
694
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
695
|
+
Log.d("ReactNative", "ls: path = " + path);
|
|
696
|
+
try {
|
|
697
|
+
byte[] res = Fs.ls(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), path);
|
|
698
|
+
|
|
699
|
+
//JSONArray jsonArray = new JSONArray(res);
|
|
700
|
+
String s = new String(res, StandardCharsets.UTF_8);
|
|
701
|
+
Log.d("ReactNative", "ls: res = " + s);
|
|
702
|
+
promise.resolve(s);
|
|
703
|
+
} catch (Exception e) {
|
|
704
|
+
Log.d("get", e.getMessage());
|
|
705
|
+
promise.reject(e);
|
|
706
|
+
}
|
|
707
|
+
});
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
@ReactMethod
|
|
711
|
+
public void rm(String path, Promise promise) {
|
|
712
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
713
|
+
Log.d("ReactNative", "rm: path = " + path);
|
|
714
|
+
try {
|
|
715
|
+
land.fx.wnfslib.Config config = Fs.rm(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), path);
|
|
716
|
+
if(config != null) {
|
|
717
|
+
this.rootConfig = config;
|
|
718
|
+
this.encrypt_and_store_config();
|
|
719
|
+
if (this.fula != null) {
|
|
720
|
+
this.fula.flush();
|
|
721
|
+
}
|
|
722
|
+
promise.resolve(config.getCid());
|
|
723
|
+
} else {
|
|
724
|
+
Log.d("ReactNative", "rm Error: config is null");
|
|
725
|
+
promise.reject(new Exception("rm Error: config is null"));
|
|
726
|
+
}
|
|
727
|
+
} catch (Exception e) {
|
|
728
|
+
Log.d("get", e.getMessage());
|
|
729
|
+
promise.reject(e);
|
|
730
|
+
}
|
|
731
|
+
});
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
@ReactMethod
|
|
735
|
+
public void cp(String sourcePath, String targetPath, Promise promise) {
|
|
736
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
737
|
+
Log.d("ReactNative", "rm: sourcePath = " + sourcePath);
|
|
738
|
+
try {
|
|
739
|
+
land.fx.wnfslib.Config config = Fs.cp(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), sourcePath, targetPath);
|
|
740
|
+
if(config != null) {
|
|
741
|
+
this.rootConfig = config;
|
|
742
|
+
this.encrypt_and_store_config();
|
|
743
|
+
if (this.fula != null) {
|
|
744
|
+
this.fula.flush();
|
|
745
|
+
}
|
|
746
|
+
promise.resolve(config.getCid());
|
|
747
|
+
} else {
|
|
748
|
+
Log.d("ReactNative", "cp Error: config is null");
|
|
749
|
+
promise.reject(new Exception("cp Error: config is null"));
|
|
750
|
+
}
|
|
751
|
+
} catch (Exception e) {
|
|
752
|
+
Log.d("get", e.getMessage());
|
|
753
|
+
promise.reject(e);
|
|
754
|
+
}
|
|
755
|
+
});
|
|
756
|
+
}
|
|
757
|
+
|
|
758
|
+
@ReactMethod
|
|
759
|
+
public void mv(String sourcePath, String targetPath, Promise promise) {
|
|
760
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
761
|
+
Log.d("ReactNative", "rm: sourcePath = " + sourcePath);
|
|
762
|
+
try {
|
|
763
|
+
land.fx.wnfslib.Config config = Fs.mv(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), sourcePath, targetPath);
|
|
764
|
+
if(config != null) {
|
|
765
|
+
this.rootConfig = config;
|
|
766
|
+
this.encrypt_and_store_config();
|
|
767
|
+
if (this.fula != null) {
|
|
768
|
+
this.fula.flush();
|
|
769
|
+
}
|
|
770
|
+
promise.resolve(config.getCid());
|
|
771
|
+
} else {
|
|
772
|
+
Log.d("ReactNative", "mv Error: config is null");
|
|
773
|
+
promise.reject(new Exception("mv Error: config is null"));
|
|
774
|
+
}
|
|
775
|
+
} catch (Exception e) {
|
|
776
|
+
Log.d("get", e.getMessage());
|
|
777
|
+
promise.reject(e);
|
|
778
|
+
}
|
|
779
|
+
});
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
@ReactMethod
|
|
783
|
+
public void readFile(String fulaTargetFilename, String localFilename, Promise promise) {
|
|
784
|
+
/*
|
|
785
|
+
// reads content of the file form localFilename (should include full absolute path to local file with read permission
|
|
786
|
+
// writes content to the specified location by fulaTargetFilename in Fula filesystem
|
|
787
|
+
// fulaTargetFilename: a string including full path and filename of target file on Fula (e.g. root/pictures/cat.jpg)
|
|
788
|
+
// localFilename: a string containing full path and filename of local file on hte device (e.g /usr/bin/cat.jpg)
|
|
789
|
+
// Returns: new cid of the root after this file is placed in the tree
|
|
790
|
+
*/
|
|
791
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
792
|
+
Log.d("ReactNative", "readFile: fulaTargetFilename = " + fulaTargetFilename);
|
|
793
|
+
try {
|
|
794
|
+
String path = Fs.readFilestreamToPath(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), fulaTargetFilename, localFilename);
|
|
795
|
+
promise.resolve(path);
|
|
796
|
+
} catch (Exception e) {
|
|
797
|
+
Log.d("get", e.getMessage());
|
|
798
|
+
promise.reject(e);
|
|
799
|
+
}
|
|
800
|
+
});
|
|
801
|
+
}
|
|
802
|
+
|
|
803
|
+
@ReactMethod
|
|
804
|
+
public void readFileContent(String path, Promise promise) {
|
|
805
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
806
|
+
Log.d("ReactNative", "readFileContent: path = " + path);
|
|
807
|
+
try {
|
|
808
|
+
byte[] res = Fs.readFile(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), path);
|
|
809
|
+
String resString = toString(res);
|
|
810
|
+
promise.resolve(resString);
|
|
811
|
+
} catch (Exception e) {
|
|
812
|
+
Log.d("get", e.getMessage());
|
|
813
|
+
promise.reject(e);
|
|
814
|
+
}
|
|
815
|
+
});
|
|
816
|
+
}
|
|
817
|
+
|
|
818
|
+
@ReactMethod
|
|
819
|
+
public void get(String keyString, Promise promise) {
|
|
820
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
821
|
+
Log.d("ReactNative", "get: keyString = " + keyString);
|
|
822
|
+
try {
|
|
823
|
+
byte[] key = this.convertStringToByte(keyString);
|
|
824
|
+
byte[] value = this.getInternal(key);
|
|
825
|
+
String valueString = toString(value);
|
|
826
|
+
promise.resolve(valueString);
|
|
827
|
+
} catch (Exception e) {
|
|
828
|
+
Log.d("get", e.getMessage());
|
|
829
|
+
promise.reject(e);
|
|
830
|
+
}
|
|
831
|
+
});
|
|
832
|
+
}
|
|
833
|
+
|
|
834
|
+
@NonNull
|
|
835
|
+
private byte[] getInternal(byte[] key) throws Exception {
|
|
836
|
+
try {
|
|
837
|
+
Log.d("ReactNative", "getInternal: key.toString() = " + toString(key));
|
|
838
|
+
Log.d("ReactNative", "getInternal: key.toString().bytes = " + Arrays.toString(key));
|
|
839
|
+
byte[] value = this.fula.get(key);
|
|
840
|
+
Log.d("ReactNative", "getInternal: value.toString() = " + toString(value));
|
|
841
|
+
return value;
|
|
842
|
+
} catch (Exception e) {
|
|
843
|
+
Log.d("ReactNative", "getInternal: error = " + e.getMessage());
|
|
844
|
+
Log.d("getInternal", e.getMessage());
|
|
845
|
+
throw (e);
|
|
846
|
+
}
|
|
847
|
+
}
|
|
848
|
+
|
|
849
|
+
@ReactMethod
|
|
850
|
+
public void has(String keyString, Promise promise) {
|
|
851
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
852
|
+
Log.d("ReactNative", "has: keyString = " + keyString);
|
|
853
|
+
try {
|
|
854
|
+
byte[] key = this.convertStringToByte(keyString);
|
|
855
|
+
boolean result = this.hasInternal(key);
|
|
856
|
+
promise.resolve(result);
|
|
857
|
+
} catch (Exception e) {
|
|
858
|
+
Log.d("get", e.getMessage());
|
|
859
|
+
promise.reject(e);
|
|
860
|
+
}
|
|
861
|
+
});
|
|
862
|
+
}
|
|
863
|
+
|
|
864
|
+
private boolean hasInternal(byte[] key) throws Exception {
|
|
865
|
+
try {
|
|
866
|
+
boolean res = this.fula.has(key);
|
|
867
|
+
return res;
|
|
868
|
+
} catch (Exception e) {
|
|
869
|
+
Log.d("hasInternal", e.getMessage());
|
|
870
|
+
throw (e);
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
private void pullInternal(byte[] key) throws Exception {
|
|
875
|
+
try {
|
|
876
|
+
this.fula.pull(key);
|
|
877
|
+
} catch (Exception e) {
|
|
878
|
+
Log.d("pullInternal", e.getMessage());
|
|
879
|
+
throw (e);
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
|
|
883
|
+
@ReactMethod
|
|
884
|
+
public void push(Promise promise) {
|
|
885
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
886
|
+
Log.d("ReactNative", "push started");
|
|
887
|
+
try {
|
|
888
|
+
this.pushInternal(this.convertStringToByte(this.rootConfig.getCid()));
|
|
889
|
+
promise.resolve(this.rootConfig.getCid());
|
|
890
|
+
} catch (Exception e) {
|
|
891
|
+
Log.d("get", e.getMessage());
|
|
892
|
+
promise.reject(e);
|
|
893
|
+
}
|
|
894
|
+
});
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
private void pushInternal(byte[] key) throws Exception {
|
|
898
|
+
try {
|
|
899
|
+
if (this.fula != null && this.fula.has(key)) {
|
|
900
|
+
this.fula.push(key);
|
|
901
|
+
this.fula.flush();
|
|
902
|
+
} else {
|
|
903
|
+
Log.d("ReactNative", "pushInternal error: key wasn't found or fula is not initialized");
|
|
904
|
+
throw new Exception("key wasn't found in local storage");
|
|
905
|
+
}
|
|
906
|
+
} catch (Exception e) {
|
|
907
|
+
Log.d("ReactNative", "pushInternal"+ e.getMessage());
|
|
908
|
+
throw (e);
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
@ReactMethod
|
|
913
|
+
public void put(String valueString, String codecString, Promise promise) {
|
|
914
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
915
|
+
Log.d("ReactNative", "put: codecString = " + codecString);
|
|
916
|
+
Log.d("ReactNative", "put: valueString = " + valueString);
|
|
917
|
+
try {
|
|
918
|
+
//byte[] codec = this.convertStringToByte(CodecString);
|
|
919
|
+
long codec = Long.parseLong(codecString);
|
|
920
|
+
|
|
921
|
+
|
|
922
|
+
Log.d("ReactNative", "put: codec = " + codec);
|
|
923
|
+
byte[] value = toByte(valueString);
|
|
924
|
+
|
|
925
|
+
Log.d("ReactNative", "put: value.toString() = " + toString(value));
|
|
926
|
+
byte[] key = this.putInternal(value, codec);
|
|
927
|
+
Log.d("ReactNative", "put: key.toString() = " + toString(key));
|
|
928
|
+
promise.resolve(toString(key));
|
|
929
|
+
} catch (Exception e) {
|
|
930
|
+
Log.d("ReactNative", "put: error = " + e.getMessage());
|
|
931
|
+
promise.reject(e);
|
|
932
|
+
}
|
|
933
|
+
});
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
@NonNull
|
|
937
|
+
private byte[] putInternal(byte[] value, long codec) throws Exception {
|
|
938
|
+
try {
|
|
939
|
+
if(this.fula != null) {
|
|
940
|
+
byte[] key = this.fula.put(value, codec);
|
|
941
|
+
this.fula.flush();
|
|
942
|
+
return key;
|
|
943
|
+
} else {
|
|
944
|
+
Log.d("ReactNative", "putInternal Error: fula is not initialized");
|
|
945
|
+
throw (new Exception("putInternal Error: fula is not initialized"));
|
|
946
|
+
}
|
|
947
|
+
} catch (Exception e) {
|
|
948
|
+
Log.d("ReactNative", "putInternal"+ e.getMessage());
|
|
949
|
+
throw (e);
|
|
950
|
+
}
|
|
951
|
+
}
|
|
952
|
+
|
|
953
|
+
@ReactMethod
|
|
954
|
+
public void setAuth(String peerIdString, boolean allow, Promise promise) {
|
|
955
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
956
|
+
Log.d("ReactNative", "setAuth: peerIdString = " + peerIdString);
|
|
957
|
+
try {
|
|
958
|
+
if (this.fula != null && this.fula.id() != null && this.fulaConfig != null && this.fulaConfig.getBloxAddr() != null) {
|
|
959
|
+
String bloxAddr = this.fulaConfig.getBloxAddr();
|
|
960
|
+
Log.d("ReactNative", "setAuth: bloxAddr = '" + bloxAddr+"'"+ " peerIdString = '" + peerIdString+"'");
|
|
961
|
+
int index = bloxAddr.lastIndexOf("/");
|
|
962
|
+
String bloxPeerId = bloxAddr.substring(index + 1);
|
|
963
|
+
this.fula.setAuth(bloxPeerId, peerIdString, allow);
|
|
964
|
+
promise.resolve(true);
|
|
965
|
+
} else {
|
|
966
|
+
Log.d("ReactNative", "setAuth error: fula is not initialized");
|
|
967
|
+
throw new Exception("fula is not initialized");
|
|
968
|
+
}
|
|
969
|
+
promise.resolve(false);
|
|
970
|
+
} catch (Exception e) {
|
|
971
|
+
Log.d("get", e.getMessage());
|
|
972
|
+
promise.reject(e);
|
|
973
|
+
}
|
|
974
|
+
});
|
|
975
|
+
}
|
|
976
|
+
|
|
977
|
+
@ReactMethod
|
|
978
|
+
public void shutdown(Promise promise) {
|
|
979
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
980
|
+
try {
|
|
981
|
+
if(this.fula != null) {
|
|
982
|
+
this.fula.shutdown();
|
|
983
|
+
this.fula = null;
|
|
984
|
+
this.client = null;
|
|
985
|
+
}
|
|
986
|
+
promise.resolve(true);
|
|
987
|
+
} catch (Exception e) {
|
|
988
|
+
promise.reject(e);
|
|
989
|
+
Log.d("ReactNative", "shutdown"+ e.getMessage());
|
|
990
|
+
}
|
|
991
|
+
});
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
///////////////////////////////////////////////////////////
|
|
995
|
+
///////////////////////////////////////////////////////////
|
|
996
|
+
///////////////////////////////////////////////////////////
|
|
997
|
+
///////////////////////////////////////////////////////////
|
|
998
|
+
//////////////////////ANYTHING BELOW IS FOR BLOCKCHAIN/////
|
|
999
|
+
///////////////////////////////////////////////////////////
|
|
1000
|
+
@ReactMethod
|
|
1001
|
+
public void createAccount(String seedString, Promise promise) {
|
|
1002
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
1003
|
+
Log.d("ReactNative", "createAccount: seedString = " + seedString);
|
|
1004
|
+
try {
|
|
1005
|
+
if (this.fula == null || this.fula.id() == null || this.fula.id().isEmpty()) {
|
|
1006
|
+
promise.reject(new Error("Fula client is not initialized"));
|
|
1007
|
+
} else {
|
|
1008
|
+
|
|
1009
|
+
if (!seedString.startsWith("/")) {
|
|
1010
|
+
promise.reject(new Error("seed should start with /"));
|
|
1011
|
+
}
|
|
1012
|
+
byte[] result = this.fula.seeded(seedString);
|
|
1013
|
+
String resultString = toString(result);
|
|
1014
|
+
promise.resolve(resultString);
|
|
1015
|
+
}
|
|
1016
|
+
} catch (Exception e) {
|
|
1017
|
+
Log.d("get", e.getMessage());
|
|
1018
|
+
promise.reject(e);
|
|
1019
|
+
}
|
|
1020
|
+
});
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
@ReactMethod
|
|
1024
|
+
public void checkAccountExists(String accountString, Promise promise) {
|
|
1025
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
1026
|
+
Log.d("ReactNative", "checkAccountExists: accountString = " + accountString);
|
|
1027
|
+
try {
|
|
1028
|
+
byte[] result = this.fula.accountExists(accountString);
|
|
1029
|
+
String resultString = toString(result);
|
|
1030
|
+
promise.resolve(resultString);
|
|
1031
|
+
} catch (Exception e) {
|
|
1032
|
+
Log.d("get", e.getMessage());
|
|
1033
|
+
promise.reject(e);
|
|
1034
|
+
}
|
|
1035
|
+
});
|
|
1036
|
+
}
|
|
1037
|
+
|
|
1038
|
+
@ReactMethod
|
|
1039
|
+
public void createPool(String seedString, String poolName, Promise promise) {
|
|
1040
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
1041
|
+
Log.d("ReactNative", "createPool: seedString = " + seedString + "; poolName = " + poolName);
|
|
1042
|
+
try {
|
|
1043
|
+
byte[] result = this.fula.poolCreate(seedString, poolName);
|
|
1044
|
+
String resultString = toString(result);
|
|
1045
|
+
promise.resolve(resultString);
|
|
1046
|
+
} catch (Exception e) {
|
|
1047
|
+
Log.d("get", e.getMessage());
|
|
1048
|
+
promise.reject(e);
|
|
1049
|
+
}
|
|
1050
|
+
});
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1053
|
+
@ReactMethod
|
|
1054
|
+
public void listPools(Promise promise) {
|
|
1055
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
1056
|
+
Log.d("ReactNative", "listPools");
|
|
1057
|
+
try {
|
|
1058
|
+
byte[] result = this.fula.poolList();
|
|
1059
|
+
String resultString = toString(result);
|
|
1060
|
+
promise.resolve(resultString);
|
|
1061
|
+
} catch (Exception e) {
|
|
1062
|
+
Log.d("get", e.getMessage());
|
|
1063
|
+
promise.reject(e);
|
|
1064
|
+
}
|
|
1065
|
+
});
|
|
1066
|
+
}
|
|
1067
|
+
|
|
1068
|
+
@ReactMethod
|
|
1069
|
+
public void joinPool(String seedString, long poolID, Promise promise) {
|
|
1070
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
1071
|
+
Log.d("ReactNative", "joinPool: seedString = " + seedString + "; poolID = " + poolID);
|
|
1072
|
+
try {
|
|
1073
|
+
byte[] result = this.fula.poolJoin(seedString, poolID);
|
|
1074
|
+
String resultString = toString(result);
|
|
1075
|
+
promise.resolve(resultString);
|
|
1076
|
+
} catch (Exception e) {
|
|
1077
|
+
Log.d("get", e.getMessage());
|
|
1078
|
+
promise.reject(e);
|
|
1079
|
+
}
|
|
1080
|
+
});
|
|
1081
|
+
}
|
|
1082
|
+
|
|
1083
|
+
@ReactMethod
|
|
1084
|
+
public void cancelPoolJoin(String seedString, long poolID, Promise promise) {
|
|
1085
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
1086
|
+
Log.d("ReactNative", "cancelPoolJoin: seedString = " + seedString + "; poolID = " + poolID);
|
|
1087
|
+
try {
|
|
1088
|
+
byte[] result = this.fula.poolCancelJoin(seedString, poolID);
|
|
1089
|
+
String resultString = toString(result);
|
|
1090
|
+
promise.resolve(resultString);
|
|
1091
|
+
} catch (Exception e) {
|
|
1092
|
+
Log.d("get", e.getMessage());
|
|
1093
|
+
promise.reject(e);
|
|
1094
|
+
}
|
|
1095
|
+
});
|
|
1096
|
+
}
|
|
1097
|
+
|
|
1098
|
+
@ReactMethod
|
|
1099
|
+
public void listPoolJoinRequests(long poolID, Promise promise) {
|
|
1100
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
1101
|
+
Log.d("ReactNative", "listPoolJoinRequests: poolID = " + poolID);
|
|
1102
|
+
try {
|
|
1103
|
+
byte[] result = this.fula.poolRequests(poolID);
|
|
1104
|
+
String resultString = toString(result);
|
|
1105
|
+
promise.resolve(resultString);
|
|
1106
|
+
} catch (Exception e) {
|
|
1107
|
+
Log.d("get", e.getMessage());
|
|
1108
|
+
promise.reject(e);
|
|
1109
|
+
}
|
|
1110
|
+
});
|
|
1111
|
+
}
|
|
1112
|
+
|
|
1113
|
+
@ReactMethod
|
|
1114
|
+
public void votePoolJoinRequest(String seedString, long poolID, String accountString, boolean accept, Promise promise) {
|
|
1115
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
1116
|
+
Log.d("ReactNative", "votePoolJoinRequest: seedString = " + seedString + "; poolID = " + poolID + "; accountString = " + accountString + "; accept = " + accept);
|
|
1117
|
+
try {
|
|
1118
|
+
byte[] result = this.fula.poolVote(seedString, poolID, accountString, accept);
|
|
1119
|
+
String resultString = toString(result);
|
|
1120
|
+
promise.resolve(resultString);
|
|
1121
|
+
} catch (Exception e) {
|
|
1122
|
+
Log.d("get", e.getMessage());
|
|
1123
|
+
promise.reject(e);
|
|
1124
|
+
}
|
|
1125
|
+
});
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
@ReactMethod
|
|
1129
|
+
public void leavePool(String seedString, long poolID, Promise promise) {
|
|
1130
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
1131
|
+
Log.d("ReactNative", "leavePool: seedString = " + seedString + "; poolID = " + poolID);
|
|
1132
|
+
try {
|
|
1133
|
+
byte[] result = this.fula.poolLeave(seedString, poolID);
|
|
1134
|
+
String resultString = toString(result);
|
|
1135
|
+
promise.resolve(resultString);
|
|
1136
|
+
} catch (Exception e) {
|
|
1137
|
+
Log.d("get", e.getMessage());
|
|
1138
|
+
promise.reject(e);
|
|
1139
|
+
}
|
|
1140
|
+
});
|
|
1141
|
+
}
|
|
1142
|
+
|
|
1143
|
+
@ReactMethod
|
|
1144
|
+
public void newReplicationRequest(String seedString, long poolID, long replicationFactor, String cid, Promise promise) {
|
|
1145
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
1146
|
+
Log.d("ReactNative", "newReplicationRequest: seedString = " + seedString + "; poolID = " + poolID + "; replicationFactor = " + replicationFactor + "; cid = " + cid);
|
|
1147
|
+
try {
|
|
1148
|
+
byte[] result = this.fula.manifestUpload(seedString, poolID, replicationFactor, cid);
|
|
1149
|
+
String resultString = toString(result);
|
|
1150
|
+
promise.resolve(resultString);
|
|
1151
|
+
} catch (Exception e) {
|
|
1152
|
+
Log.d("get", e.getMessage());
|
|
1153
|
+
promise.reject(e);
|
|
1154
|
+
}
|
|
1155
|
+
});
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1158
|
+
@ReactMethod
|
|
1159
|
+
public void newStoreRequest(String seedString, long poolID, String uploader, String cid, Promise promise) {
|
|
1160
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
1161
|
+
Log.d("ReactNative", "newStoreRequest: seedString = " + seedString + "; poolID = " + poolID + "; uploader = " + uploader + "; cid = " + cid);
|
|
1162
|
+
try {
|
|
1163
|
+
byte[] result = this.fula.manifestStore(seedString, poolID, uploader, cid);
|
|
1164
|
+
String resultString = toString(result);
|
|
1165
|
+
promise.resolve(resultString);
|
|
1166
|
+
} catch (Exception e) {
|
|
1167
|
+
Log.d("get", e.getMessage());
|
|
1168
|
+
promise.reject(e);
|
|
1169
|
+
}
|
|
1170
|
+
});
|
|
1171
|
+
}
|
|
1172
|
+
|
|
1173
|
+
@ReactMethod
|
|
1174
|
+
public void listAvailableReplicationRequests(long poolID, Promise promise) {
|
|
1175
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
1176
|
+
Log.d("ReactNative", "listAvailableReplicationRequests: poolID = " + poolID);
|
|
1177
|
+
try {
|
|
1178
|
+
byte[] result = this.fula.manifestAvailable(poolID);
|
|
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 removeReplicationRequest(String seedString, long poolID, String cid, Promise promise) {
|
|
1190
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
1191
|
+
Log.d("ReactNative", "newReplicationRequest: seedString = " + seedString + "; poolID = " + poolID + "; cid = " + cid);
|
|
1192
|
+
try {
|
|
1193
|
+
byte[] result = this.fula.manifestRemove(seedString, poolID, cid);
|
|
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 removeStorer(String seedString, String storage, long poolID, String cid, Promise promise) {
|
|
1205
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
1206
|
+
Log.d("ReactNative", "removeStorer: seedString = " + seedString + "; storage = " + storage + "; poolID = " + poolID + "; cid = " + cid);
|
|
1207
|
+
try {
|
|
1208
|
+
byte[] result = this.fula.manifestRemoveStorer(seedString, storage, poolID, cid);
|
|
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 removeStoredReplication(String seedString, String uploader, long poolID, String cid, Promise promise) {
|
|
1220
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
1221
|
+
Log.d("ReactNative", "removeStoredReplication: seedString = " + seedString + "; uploader = " + uploader + "; poolID = " + poolID + "; cid = " + cid);
|
|
1222
|
+
try {
|
|
1223
|
+
byte[] result = this.fula.manifestRemoveStored(seedString, uploader, poolID, cid);
|
|
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 bloxFreeSpace(Promise promise) {
|
|
1235
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
1236
|
+
Log.d("ReactNative", "bloxFreeSpace");
|
|
1237
|
+
try {
|
|
1238
|
+
byte[] result = this.fula.bloxFreeSpace();
|
|
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
|
+
}
|