@functionland/react-native-fula 0.4.0 → 0.4.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.
- package/README.md +74 -9
- 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/executionHistory/executionHistory.bin +0 -0
- package/android/.gradle/7.5.1/executionHistory/executionHistory.lock +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/buildOutputCleanup/buildOutputCleanup.lock +0 -0
- package/android/.gradle/buildOutputCleanup/outputFiles.bin +0 -0
- package/android/.gradle/file-system.probe +0 -0
- package/android/build.gradle +67 -67
- package/android/src/main/java/land/fx/fula/FulaModule.java +480 -443
- package/lib/commonjs/interfaces/fulaNativeModule.js.map +1 -1
- package/lib/commonjs/protocols/fula.js +84 -61
- package/lib/commonjs/protocols/fula.js.map +1 -1
- package/lib/module/interfaces/fulaNativeModule.js.map +1 -1
- package/lib/module/protocols/fula.js +81 -60
- package/lib/module/protocols/fula.js.map +1 -1
- package/lib/typescript/interfaces/fulaNativeModule.d.ts +5 -2
- package/lib/typescript/protocols/fula.d.ts +9 -2
- package/package.json +152 -152
- package/src/interfaces/fulaNativeModule.ts +42 -39
- package/src/protocols/fula.ts +138 -116
|
@@ -1,443 +1,480 @@
|
|
|
1
|
-
package land.fx.fula;
|
|
2
|
-
|
|
3
|
-
import android.util.Log;
|
|
4
|
-
|
|
5
|
-
import androidx.annotation.NonNull;
|
|
6
|
-
|
|
7
|
-
import com.facebook.react.bridge.Promise;
|
|
8
|
-
import com.facebook.react.bridge.ReactApplicationContext;
|
|
9
|
-
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
|
10
|
-
import com.facebook.react.bridge.ReactMethod;
|
|
11
|
-
import com.facebook.react.module.annotations.ReactModule;
|
|
12
|
-
|
|
13
|
-
import org.jetbrains.annotations.Contract;
|
|
14
|
-
|
|
15
|
-
import java.io.File;
|
|
16
|
-
import java.nio.charset.StandardCharsets;
|
|
17
|
-
import java.util.Arrays;
|
|
18
|
-
|
|
19
|
-
import javax.crypto.SecretKey;
|
|
20
|
-
|
|
21
|
-
import fulamobile.Config;
|
|
22
|
-
import fulamobile.Fulamobile;
|
|
23
|
-
|
|
24
|
-
import land.fx.wnfslib.LibKt;
|
|
25
|
-
|
|
26
|
-
@ReactModule(name = FulaModule.NAME)
|
|
27
|
-
public class FulaModule extends ReactContextBaseJavaModule {
|
|
28
|
-
public static final String NAME = "FulaModule";
|
|
29
|
-
fulamobile.Client fula;
|
|
30
|
-
Client client;
|
|
31
|
-
String appDir;
|
|
32
|
-
String fulaStorePath;
|
|
33
|
-
String privateForest;
|
|
34
|
-
land.fx.wnfslib.Config rootConfig;
|
|
35
|
-
SharedPreferenceHelper sharedPref;
|
|
36
|
-
static String PRIVATE_KEY_STORE_ID = "PRIVATE_KEY";
|
|
37
|
-
|
|
38
|
-
public class Client implements land.fx.wnfslib.Client {
|
|
39
|
-
|
|
40
|
-
private fulamobile.Client internalClient;
|
|
41
|
-
|
|
42
|
-
Client(fulamobile.Client clientInput) {
|
|
43
|
-
internalClient = clientInput;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
@NonNull
|
|
47
|
-
@Override
|
|
48
|
-
public byte[] get(@NonNull byte[] cid) {
|
|
49
|
-
try {
|
|
50
|
-
internalClient.get(cid);
|
|
51
|
-
} catch (Exception e) {
|
|
52
|
-
e.printStackTrace();
|
|
53
|
-
}
|
|
54
|
-
return null;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
@NonNull
|
|
58
|
-
@Override
|
|
59
|
-
public byte[] put(@NonNull byte[] data, long codec) {
|
|
60
|
-
try {
|
|
61
|
-
return client.put(data, codec);
|
|
62
|
-
} catch (Exception e) {
|
|
63
|
-
e.printStackTrace();
|
|
64
|
-
}
|
|
65
|
-
return null;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
public FulaModule(ReactApplicationContext reactContext) {
|
|
70
|
-
super(reactContext);
|
|
71
|
-
appDir = reactContext.getFilesDir().toString();
|
|
72
|
-
fulaStorePath = appDir + "/fula";
|
|
73
|
-
File storeDir = new File(fulaStorePath);
|
|
74
|
-
sharedPref = SharedPreferenceHelper.getInstance(reactContext.getApplicationContext());
|
|
75
|
-
boolean success = true;
|
|
76
|
-
if (!storeDir.exists()) {
|
|
77
|
-
success = storeDir.mkdirs();
|
|
78
|
-
}
|
|
79
|
-
if (success) {
|
|
80
|
-
Log.d(NAME, "Fula store folder created");
|
|
81
|
-
} else {
|
|
82
|
-
Log.d(NAME, "Unable to create fula store folder!");
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
@Override
|
|
87
|
-
@NonNull
|
|
88
|
-
public String getName() {
|
|
89
|
-
return NAME;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
private byte[] toByte(@NonNull String input) {
|
|
94
|
-
return input.getBytes(StandardCharsets.UTF_8);
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
@NonNull
|
|
98
|
-
@Contract("_ -> new")
|
|
99
|
-
private String toString(byte[] input) {
|
|
100
|
-
return new String(input, StandardCharsets.UTF_8);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
@NonNull
|
|
104
|
-
private static int[] stringArrToIntArr(@NonNull String[] s) {
|
|
105
|
-
int[] result = new int[s.length];
|
|
106
|
-
for (int i = 0; i < s.length; i++) {
|
|
107
|
-
result[i] = Integer.parseInt(s[i]);
|
|
108
|
-
}
|
|
109
|
-
return result;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
@NonNull
|
|
113
|
-
@Contract(pure = true)
|
|
114
|
-
private static byte[] convertIntToByte(@NonNull int[] input) {
|
|
115
|
-
byte[] result = new byte[input.length];
|
|
116
|
-
for (int i = 0; i < input.length; i++) {
|
|
117
|
-
byte b = (byte) input[i];
|
|
118
|
-
result[i] = b;
|
|
119
|
-
}
|
|
120
|
-
return result;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
@NonNull
|
|
124
|
-
private static byte[] convertStringToByte(@NonNull String data) {
|
|
125
|
-
String[] keyInt_S = data.split(",");
|
|
126
|
-
int[] keyInt = stringArrToIntArr(keyInt_S);
|
|
127
|
-
|
|
128
|
-
return convertIntToByte(keyInt);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
@ReactMethod
|
|
132
|
-
public void init(String identityString, String storePath, String bloxAddr, Promise promise) {
|
|
133
|
-
Log.d("ReactNative", "init started");
|
|
134
|
-
ThreadUtils.runOnExecutor(() -> {
|
|
135
|
-
try {
|
|
136
|
-
Log.d("ReactNative", "init storePath= " + storePath);
|
|
137
|
-
byte[] identity = toByte(identityString);
|
|
138
|
-
Log.d("ReactNative", "init identity= " + identityString);
|
|
139
|
-
String[] obj = initInternal(identity, storePath, bloxAddr);
|
|
140
|
-
Log.d("ReactNative", "init object created: [ " + obj[0] + ", " + obj[1] + ", " + obj[2] + " ]");
|
|
141
|
-
promise.resolve(obj);
|
|
142
|
-
} catch (Exception e) {
|
|
143
|
-
Log.d("ReactNative", "init failed with Error: " + e.getMessage());
|
|
144
|
-
promise.reject(e);
|
|
145
|
-
}
|
|
146
|
-
});
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
@NonNull
|
|
150
|
-
private byte[] createPeerIdentity(byte[] privateKey) throws Exception {
|
|
151
|
-
try {
|
|
152
|
-
// 1: First: create public key from provided private key
|
|
153
|
-
// 2: Should read the local keychain store (if it is key-value, key is public key above,
|
|
154
|
-
// 3: if found, decrypt using the private key
|
|
155
|
-
// 4: If not found or decryption not successful, generate an identity
|
|
156
|
-
// 5: then encrypt and store in keychain
|
|
157
|
-
|
|
158
|
-
String encryptedKey = sharedPref.getValue(PRIVATE_KEY_STORE_ID);
|
|
159
|
-
SecretKey secretKey = Cryptography.generateKey(privateKey);
|
|
160
|
-
if (encryptedKey == null) {
|
|
161
|
-
byte[] autoGeneratedIdentity = Fulamobile.generateEd25519Key();
|
|
162
|
-
encryptedKey = Cryptography.encryptMsg(StaticHelper.bytesToBase64(autoGeneratedIdentity), secretKey);
|
|
163
|
-
sharedPref.add(PRIVATE_KEY_STORE_ID, encryptedKey);
|
|
164
|
-
}
|
|
165
|
-
return StaticHelper.base64ToBytes(Cryptography.decryptMsg(encryptedKey, secretKey));
|
|
166
|
-
|
|
167
|
-
} catch (Exception e) {
|
|
168
|
-
Log.d("ReactNative", "createPeerIdentity failed with Error: " + e.getMessage());
|
|
169
|
-
throw (e);
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
@NonNull
|
|
174
|
-
private String[] initInternal(byte[] identity, String storePath, String bloxAddr) throws Exception {
|
|
175
|
-
try {
|
|
176
|
-
Config config_ext = new Config();
|
|
177
|
-
if (storePath == null || storePath.trim().isEmpty()) {
|
|
178
|
-
config_ext.setStorePath(fulaStorePath);
|
|
179
|
-
} else {
|
|
180
|
-
config_ext.setStorePath(storePath);
|
|
181
|
-
}
|
|
182
|
-
Log.d("ReactNative", "storePath is set: " + config_ext.getStorePath());
|
|
183
|
-
|
|
184
|
-
byte[] peerIdentity = createPeerIdentity(identity);
|
|
185
|
-
config_ext.setIdentity(peerIdentity);
|
|
186
|
-
Log.d("ReactNative", "peerIdentity is set: " + toString(config_ext.getIdentity()));
|
|
187
|
-
config_ext.setBloxAddr(bloxAddr);
|
|
188
|
-
Log.d("ReactNative", "bloxAddr is set: " + config_ext.getBloxAddr());
|
|
189
|
-
|
|
190
|
-
this.
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
String
|
|
203
|
-
|
|
204
|
-
obj[
|
|
205
|
-
obj[
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
this.rootConfig
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
//
|
|
234
|
-
//
|
|
235
|
-
//
|
|
236
|
-
//
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
this.rootConfig
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
Log.d("ReactNative", "writeFile:
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
this.rootConfig
|
|
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
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
Log.d("ReactNative", "
|
|
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
|
-
} catch (Exception e) {
|
|
359
|
-
Log.d("
|
|
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
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
Log.d("
|
|
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
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
1
|
+
package land.fx.fula;
|
|
2
|
+
|
|
3
|
+
import android.util.Log;
|
|
4
|
+
|
|
5
|
+
import androidx.annotation.NonNull;
|
|
6
|
+
|
|
7
|
+
import com.facebook.react.bridge.Promise;
|
|
8
|
+
import com.facebook.react.bridge.ReactApplicationContext;
|
|
9
|
+
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
|
10
|
+
import com.facebook.react.bridge.ReactMethod;
|
|
11
|
+
import com.facebook.react.module.annotations.ReactModule;
|
|
12
|
+
|
|
13
|
+
import org.jetbrains.annotations.Contract;
|
|
14
|
+
|
|
15
|
+
import java.io.File;
|
|
16
|
+
import java.nio.charset.StandardCharsets;
|
|
17
|
+
import java.util.Arrays;
|
|
18
|
+
|
|
19
|
+
import javax.crypto.SecretKey;
|
|
20
|
+
|
|
21
|
+
import fulamobile.Config;
|
|
22
|
+
import fulamobile.Fulamobile;
|
|
23
|
+
|
|
24
|
+
import land.fx.wnfslib.LibKt;
|
|
25
|
+
|
|
26
|
+
@ReactModule(name = FulaModule.NAME)
|
|
27
|
+
public class FulaModule extends ReactContextBaseJavaModule {
|
|
28
|
+
public static final String NAME = "FulaModule";
|
|
29
|
+
fulamobile.Client fula;
|
|
30
|
+
Client client;
|
|
31
|
+
String appDir;
|
|
32
|
+
String fulaStorePath;
|
|
33
|
+
String privateForest;
|
|
34
|
+
land.fx.wnfslib.Config rootConfig;
|
|
35
|
+
SharedPreferenceHelper sharedPref;
|
|
36
|
+
static String PRIVATE_KEY_STORE_ID = "PRIVATE_KEY";
|
|
37
|
+
|
|
38
|
+
public class Client implements land.fx.wnfslib.Client {
|
|
39
|
+
|
|
40
|
+
private fulamobile.Client internalClient;
|
|
41
|
+
|
|
42
|
+
Client(fulamobile.Client clientInput) {
|
|
43
|
+
internalClient = clientInput;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
@NonNull
|
|
47
|
+
@Override
|
|
48
|
+
public byte[] get(@NonNull byte[] cid) {
|
|
49
|
+
try {
|
|
50
|
+
internalClient.get(cid);
|
|
51
|
+
} catch (Exception e) {
|
|
52
|
+
e.printStackTrace();
|
|
53
|
+
}
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
@NonNull
|
|
58
|
+
@Override
|
|
59
|
+
public byte[] put(@NonNull byte[] data, long codec) {
|
|
60
|
+
try {
|
|
61
|
+
return client.put(data, codec);
|
|
62
|
+
} catch (Exception e) {
|
|
63
|
+
e.printStackTrace();
|
|
64
|
+
}
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
public FulaModule(ReactApplicationContext reactContext) {
|
|
70
|
+
super(reactContext);
|
|
71
|
+
appDir = reactContext.getFilesDir().toString();
|
|
72
|
+
fulaStorePath = appDir + "/fula";
|
|
73
|
+
File storeDir = new File(fulaStorePath);
|
|
74
|
+
sharedPref = SharedPreferenceHelper.getInstance(reactContext.getApplicationContext());
|
|
75
|
+
boolean success = true;
|
|
76
|
+
if (!storeDir.exists()) {
|
|
77
|
+
success = storeDir.mkdirs();
|
|
78
|
+
}
|
|
79
|
+
if (success) {
|
|
80
|
+
Log.d(NAME, "Fula store folder created");
|
|
81
|
+
} else {
|
|
82
|
+
Log.d(NAME, "Unable to create fula store folder!");
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
@Override
|
|
87
|
+
@NonNull
|
|
88
|
+
public String getName() {
|
|
89
|
+
return NAME;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
private byte[] toByte(@NonNull String input) {
|
|
94
|
+
return input.getBytes(StandardCharsets.UTF_8);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
@NonNull
|
|
98
|
+
@Contract("_ -> new")
|
|
99
|
+
private String toString(byte[] input) {
|
|
100
|
+
return new String(input, StandardCharsets.UTF_8);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
@NonNull
|
|
104
|
+
private static int[] stringArrToIntArr(@NonNull String[] s) {
|
|
105
|
+
int[] result = new int[s.length];
|
|
106
|
+
for (int i = 0; i < s.length; i++) {
|
|
107
|
+
result[i] = Integer.parseInt(s[i]);
|
|
108
|
+
}
|
|
109
|
+
return result;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
@NonNull
|
|
113
|
+
@Contract(pure = true)
|
|
114
|
+
private static byte[] convertIntToByte(@NonNull int[] input) {
|
|
115
|
+
byte[] result = new byte[input.length];
|
|
116
|
+
for (int i = 0; i < input.length; i++) {
|
|
117
|
+
byte b = (byte) input[i];
|
|
118
|
+
result[i] = b;
|
|
119
|
+
}
|
|
120
|
+
return result;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
@NonNull
|
|
124
|
+
private static byte[] convertStringToByte(@NonNull String data) {
|
|
125
|
+
String[] keyInt_S = data.split(",");
|
|
126
|
+
int[] keyInt = stringArrToIntArr(keyInt_S);
|
|
127
|
+
|
|
128
|
+
return convertIntToByte(keyInt);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
@ReactMethod
|
|
132
|
+
public void init(String identityString, String storePath, String bloxAddr, String exchange, Promise promise) {
|
|
133
|
+
Log.d("ReactNative", "init started");
|
|
134
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
135
|
+
try {
|
|
136
|
+
Log.d("ReactNative", "init storePath= " + storePath);
|
|
137
|
+
byte[] identity = toByte(identityString);
|
|
138
|
+
Log.d("ReactNative", "init identity= " + identityString);
|
|
139
|
+
String[] obj = initInternal(identity, storePath, bloxAddr, exchange);
|
|
140
|
+
Log.d("ReactNative", "init object created: [ " + obj[0] + ", " + obj[1] + ", " + obj[2] + " ]");
|
|
141
|
+
promise.resolve(obj);
|
|
142
|
+
} catch (Exception e) {
|
|
143
|
+
Log.d("ReactNative", "init failed with Error: " + e.getMessage());
|
|
144
|
+
promise.reject(e);
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
@NonNull
|
|
150
|
+
private byte[] createPeerIdentity(byte[] privateKey) throws Exception {
|
|
151
|
+
try {
|
|
152
|
+
// 1: First: create public key from provided private key
|
|
153
|
+
// 2: Should read the local keychain store (if it is key-value, key is public key above,
|
|
154
|
+
// 3: if found, decrypt using the private key
|
|
155
|
+
// 4: If not found or decryption not successful, generate an identity
|
|
156
|
+
// 5: then encrypt and store in keychain
|
|
157
|
+
|
|
158
|
+
String encryptedKey = sharedPref.getValue(PRIVATE_KEY_STORE_ID);
|
|
159
|
+
SecretKey secretKey = Cryptography.generateKey(privateKey);
|
|
160
|
+
if (encryptedKey == null) {
|
|
161
|
+
byte[] autoGeneratedIdentity = Fulamobile.generateEd25519Key();
|
|
162
|
+
encryptedKey = Cryptography.encryptMsg(StaticHelper.bytesToBase64(autoGeneratedIdentity), secretKey);
|
|
163
|
+
sharedPref.add(PRIVATE_KEY_STORE_ID, encryptedKey);
|
|
164
|
+
}
|
|
165
|
+
return StaticHelper.base64ToBytes(Cryptography.decryptMsg(encryptedKey, secretKey));
|
|
166
|
+
|
|
167
|
+
} catch (Exception e) {
|
|
168
|
+
Log.d("ReactNative", "createPeerIdentity failed with Error: " + e.getMessage());
|
|
169
|
+
throw (e);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
@NonNull
|
|
174
|
+
private String[] initInternal(byte[] identity, String storePath, String bloxAddr, String exchange) throws Exception {
|
|
175
|
+
try {
|
|
176
|
+
Config config_ext = new Config();
|
|
177
|
+
if (storePath == null || storePath.trim().isEmpty()) {
|
|
178
|
+
config_ext.setStorePath(fulaStorePath);
|
|
179
|
+
} else {
|
|
180
|
+
config_ext.setStorePath(storePath);
|
|
181
|
+
}
|
|
182
|
+
Log.d("ReactNative", "storePath is set: " + config_ext.getStorePath());
|
|
183
|
+
|
|
184
|
+
byte[] peerIdentity = createPeerIdentity(identity);
|
|
185
|
+
config_ext.setIdentity(peerIdentity);
|
|
186
|
+
Log.d("ReactNative", "peerIdentity is set: " + toString(config_ext.getIdentity()));
|
|
187
|
+
config_ext.setBloxAddr(bloxAddr);
|
|
188
|
+
Log.d("ReactNative", "bloxAddr is set: " + config_ext.getBloxAddr());
|
|
189
|
+
config_ext.setExchange(exchange);
|
|
190
|
+
this.fula = Fulamobile.newClient(config_ext);
|
|
191
|
+
this.client = new Client(this.fula);
|
|
192
|
+
Log.d("ReactNative", "fula initialized: " + this.fula.id());
|
|
193
|
+
if (this.rootConfig == null) {
|
|
194
|
+
Log.d("ReactNative", "creating rootConfig");
|
|
195
|
+
this.privateForest = LibKt.createPrivateForest(this.client);
|
|
196
|
+
Log.d("ReactNative", "privateForest is created: " + this.privateForest);
|
|
197
|
+
this.rootConfig = LibKt.createRootDir(this.client, this.privateForest);
|
|
198
|
+
Log.d("ReactNative", "rootConfig is created: " + this.rootConfig.getCid());
|
|
199
|
+
} else {
|
|
200
|
+
Log.d("ReactNative", "rootConfig existed: " + this.rootConfig.getCid());
|
|
201
|
+
}
|
|
202
|
+
String peerId = this.fula.id();
|
|
203
|
+
String[] obj = new String[3];
|
|
204
|
+
obj[0] = peerId;
|
|
205
|
+
obj[1] = this.rootConfig.getCid();
|
|
206
|
+
obj[2] = this.rootConfig.getPrivate_ref();
|
|
207
|
+
Log.d("ReactNative", "initInternal is completed successfully");
|
|
208
|
+
return obj;
|
|
209
|
+
} catch (Exception e) {
|
|
210
|
+
Log.d("ReactNative", "init internal failed with Error: " + e.getMessage());
|
|
211
|
+
throw (e);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
@ReactMethod
|
|
216
|
+
public void mkdir(String path, Promise promise) {
|
|
217
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
218
|
+
Log.d("ReactNative", "mkdir: path = " + path);
|
|
219
|
+
try {
|
|
220
|
+
land.fx.wnfslib.Config config = LibKt.mkdir(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), path);
|
|
221
|
+
this.rootConfig = config;
|
|
222
|
+
promise.resolve(config.getCid());
|
|
223
|
+
} catch (Exception e) {
|
|
224
|
+
Log.d("get", e.getMessage());
|
|
225
|
+
promise.reject(e);
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
@ReactMethod
|
|
231
|
+
public void writeFile(String fulaTargetFilename, String localFilename, Promise promise) {
|
|
232
|
+
/*
|
|
233
|
+
// reads content of the file form localFilename (should include full absolute path to local file with read permission
|
|
234
|
+
// writes content to the specified location by fulaTargetFilename in Fula filesystem
|
|
235
|
+
// fulaTargetFilename: a string including full path and filename of target file on Fula (e.g. root/pictures/cat.jpg)
|
|
236
|
+
// localFilename: a string containing full path and filename of local file on hte device (e.g /usr/bin/cat.jpg)
|
|
237
|
+
// Returns: new cid of the root after this file is placed in the tree
|
|
238
|
+
*/
|
|
239
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
240
|
+
Log.d("ReactNative", "writeFile to : path = " + fulaTargetFilename + ", from: " + localFilename);
|
|
241
|
+
try {
|
|
242
|
+
land.fx.wnfslib.Config config = LibKt.writeFileFromPath(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), fulaTargetFilename, localFilename);
|
|
243
|
+
this.rootConfig = config;
|
|
244
|
+
promise.resolve(config.getCid());
|
|
245
|
+
} catch (Exception e) {
|
|
246
|
+
Log.d("get", e.getMessage());
|
|
247
|
+
promise.reject(e);
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
@ReactMethod
|
|
253
|
+
public void writeFileContent(String path, String contentString, Promise promise) {
|
|
254
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
255
|
+
Log.d("ReactNative", "writeFile: contentString = " + contentString);
|
|
256
|
+
Log.d("ReactNative", "writeFile: path = " + path);
|
|
257
|
+
try {
|
|
258
|
+
byte[] content = convertStringToByte(contentString);
|
|
259
|
+
land.fx.wnfslib.Config config = LibKt.writeFile(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), path, content);
|
|
260
|
+
this.rootConfig = config;
|
|
261
|
+
promise.resolve(config.getCid());
|
|
262
|
+
} catch (Exception e) {
|
|
263
|
+
Log.d("get", e.getMessage());
|
|
264
|
+
promise.reject(e);
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
@ReactMethod
|
|
270
|
+
public void ls(String path, Promise promise) {
|
|
271
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
272
|
+
Log.d("ReactNative", "ls: path = " + path);
|
|
273
|
+
try {
|
|
274
|
+
String res = LibKt.ls(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), path);
|
|
275
|
+
promise.resolve(res);
|
|
276
|
+
} catch (Exception e) {
|
|
277
|
+
Log.d("get", e.getMessage());
|
|
278
|
+
promise.reject(e);
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
@ReactMethod
|
|
284
|
+
public void rm(String path, Promise promise) {
|
|
285
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
286
|
+
Log.d("ReactNative", "rm: path = " + path);
|
|
287
|
+
try {
|
|
288
|
+
land.fx.wnfslib.Config config = LibKt.rm(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), path);
|
|
289
|
+
this.rootConfig = config;
|
|
290
|
+
promise.resolve(config.getCid());
|
|
291
|
+
} catch (Exception e) {
|
|
292
|
+
Log.d("get", e.getMessage());
|
|
293
|
+
promise.reject(e);
|
|
294
|
+
}
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
@ReactMethod
|
|
299
|
+
public void readFile(String fulaTargetFilename, String localFilename, Promise promise) {
|
|
300
|
+
/*
|
|
301
|
+
// reads content of the file form localFilename (should include full absolute path to local file with read permission
|
|
302
|
+
// writes content to the specified location by fulaTargetFilename in Fula filesystem
|
|
303
|
+
// fulaTargetFilename: a string including full path and filename of target file on Fula (e.g. root/pictures/cat.jpg)
|
|
304
|
+
// localFilename: a string containing full path and filename of local file on hte device (e.g /usr/bin/cat.jpg)
|
|
305
|
+
// Returns: new cid of the root after this file is placed in the tree
|
|
306
|
+
*/
|
|
307
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
308
|
+
Log.d("ReactNative", "readFile: fulaTargetFilename = " + fulaTargetFilename);
|
|
309
|
+
try {
|
|
310
|
+
String path = LibKt.readFileToPath(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), fulaTargetFilename, localFilename);
|
|
311
|
+
promise.resolve(path);
|
|
312
|
+
} catch (Exception e) {
|
|
313
|
+
Log.d("get", e.getMessage());
|
|
314
|
+
promise.reject(e);
|
|
315
|
+
}
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
@ReactMethod
|
|
320
|
+
public void readFileContent(String path, Promise promise) {
|
|
321
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
322
|
+
Log.d("ReactNative", "readFileContent: path = " + path);
|
|
323
|
+
try {
|
|
324
|
+
byte[] res = LibKt.readFile(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), path);
|
|
325
|
+
String resString = toString(res);
|
|
326
|
+
promise.resolve(resString);
|
|
327
|
+
} catch (Exception e) {
|
|
328
|
+
Log.d("get", e.getMessage());
|
|
329
|
+
promise.reject(e);
|
|
330
|
+
}
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
@ReactMethod
|
|
335
|
+
public void get(String keyString, Promise promise) {
|
|
336
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
337
|
+
Log.d("ReactNative", "get: keyString = " + keyString);
|
|
338
|
+
try {
|
|
339
|
+
byte[] key = convertStringToByte(keyString);
|
|
340
|
+
byte[] value = getInternal(key);
|
|
341
|
+
String valueString = toString(value);
|
|
342
|
+
promise.resolve(valueString);
|
|
343
|
+
} catch (Exception e) {
|
|
344
|
+
Log.d("get", e.getMessage());
|
|
345
|
+
promise.reject(e);
|
|
346
|
+
}
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
@NonNull
|
|
351
|
+
private byte[] getInternal(byte[] key) throws Exception {
|
|
352
|
+
try {
|
|
353
|
+
Log.d("ReactNative", "getInternal: key.toString() = " + toString(key));
|
|
354
|
+
Log.d("ReactNative", "getInternal: key.toString().bytes = " + Arrays.toString(key));
|
|
355
|
+
byte[] value = this.fula.get(key);
|
|
356
|
+
Log.d("ReactNative", "getInternal: value.toString() = " + toString(value));
|
|
357
|
+
return value;
|
|
358
|
+
} catch (Exception e) {
|
|
359
|
+
Log.d("ReactNative", "getInternal: error = " + e.getMessage());
|
|
360
|
+
Log.d("getInternal", e.getMessage());
|
|
361
|
+
throw (e);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
@ReactMethod
|
|
366
|
+
public void has(String keyString, Promise promise) {
|
|
367
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
368
|
+
Log.d("ReactNative", "has: keyString = " + keyString);
|
|
369
|
+
try {
|
|
370
|
+
byte[] key = convertStringToByte(keyString);
|
|
371
|
+
boolean result = hasInternal(key);
|
|
372
|
+
promise.resolve(result);
|
|
373
|
+
} catch (Exception e) {
|
|
374
|
+
Log.d("get", e.getMessage());
|
|
375
|
+
promise.reject(e);
|
|
376
|
+
}
|
|
377
|
+
});
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
@NonNull
|
|
381
|
+
private boolean hasInternal(byte[] key) throws Exception {
|
|
382
|
+
try {
|
|
383
|
+
boolean res = this.fula.has(key);
|
|
384
|
+
return res;
|
|
385
|
+
} catch (Exception e) {
|
|
386
|
+
Log.d("hasInternal", e.getMessage());
|
|
387
|
+
throw (e);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
@NonNull
|
|
392
|
+
private void pullInternal(byte[] key) throws Exception {
|
|
393
|
+
try {
|
|
394
|
+
this.fula.pull(key);
|
|
395
|
+
} catch (Exception e) {
|
|
396
|
+
Log.d("pullInternal", e.getMessage());
|
|
397
|
+
throw (e);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
@ReactMethod
|
|
402
|
+
public void push(Promise promise) {
|
|
403
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
404
|
+
Log.d("ReactNative", "push started");
|
|
405
|
+
try {
|
|
406
|
+
pushInternal(convertStringToByte(this.rootConfig.getCid()));
|
|
407
|
+
promise.resolve(this.rootConfig.getCid());
|
|
408
|
+
} catch (Exception e) {
|
|
409
|
+
Log.d("get", e.getMessage());
|
|
410
|
+
promise.reject(e);
|
|
411
|
+
}
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
@NonNull
|
|
416
|
+
private void pushInternal(byte[] key) throws Exception {
|
|
417
|
+
try {
|
|
418
|
+
if (this.fula.has(key)) {
|
|
419
|
+
this.fula.push(key);
|
|
420
|
+
} else {
|
|
421
|
+
Log.d("pushInternal", "error: key wasn't found");
|
|
422
|
+
throw new Exception("key wasn't found in local storage");
|
|
423
|
+
}
|
|
424
|
+
} catch (Exception e) {
|
|
425
|
+
Log.d("pushInternal", e.getMessage());
|
|
426
|
+
throw (e);
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
@ReactMethod
|
|
431
|
+
public void put(String valueString, String codecString, Promise promise) {
|
|
432
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
433
|
+
Log.d("ReactNative", "put: codecString = " + codecString);
|
|
434
|
+
Log.d("ReactNative", "put: valueString = " + valueString);
|
|
435
|
+
try {
|
|
436
|
+
//byte[] codec = convertStringToByte(CodecString);
|
|
437
|
+
long codec = Long.parseLong(codecString);
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
Log.d("ReactNative", "put: codec = " + codec);
|
|
441
|
+
byte[] value = toByte(valueString);
|
|
442
|
+
|
|
443
|
+
Log.d("ReactNative", "put: value.toString() = " + toString(value));
|
|
444
|
+
byte[] key = putInternal(value, codec);
|
|
445
|
+
Log.d("ReactNative", "put: key.toString() = " + toString(key));
|
|
446
|
+
promise.resolve(toString(key));
|
|
447
|
+
} catch (Exception e) {
|
|
448
|
+
Log.d("ReactNative", "put: error = " + e.getMessage());
|
|
449
|
+
Log.d("put", e.getMessage());
|
|
450
|
+
promise.reject(e);
|
|
451
|
+
}
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
@NonNull
|
|
456
|
+
private byte[] putInternal(byte[] value, long codec) throws Exception {
|
|
457
|
+
try {
|
|
458
|
+
byte[] key = this.fula.put(value, codec);
|
|
459
|
+
return key;
|
|
460
|
+
} catch (Exception e) {
|
|
461
|
+
Log.d("putInternal", e.getMessage());
|
|
462
|
+
throw (e);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
@ReactMethod
|
|
467
|
+
public void shutdown(Promise promise) {
|
|
468
|
+
ThreadUtils.runOnExecutor(() -> {
|
|
469
|
+
try {
|
|
470
|
+
fula.shutdown();
|
|
471
|
+
promise.resolve(true);
|
|
472
|
+
} catch (Exception e) {
|
|
473
|
+
promise.reject(e);
|
|
474
|
+
Log.d("shutdown", e.getMessage());
|
|
475
|
+
}
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
}
|