@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.
@@ -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
- this.fula = Fulamobile.newClient(config_ext);
190
- this.client = new Client(this.fula);
191
- Log.d("ReactNative", "fula initialized: " + this.fula.id());
192
- if (this.rootConfig == null) {
193
- Log.d("ReactNative", "creating rootConfig");
194
- this.privateForest = LibKt.createPrivateForest(this.client);
195
- Log.d("ReactNative", "privateForest is created: " + this.privateForest);
196
- this.rootConfig = LibKt.createRootDir(this.client, this.privateForest);
197
- Log.d("ReactNative", "rootConfig is created: " + this.rootConfig.getCid());
198
- } else {
199
- Log.d("ReactNative", "rootConfig existed: " + this.rootConfig.getCid());
200
- }
201
- String peerId = this.fula.id();
202
- String[] obj = new String[3];
203
- obj[0] = peerId;
204
- obj[1] = this.rootConfig.getCid();
205
- obj[2] = this.rootConfig.getPrivate_ref();
206
- Log.d("ReactNative", "initInternal is completed successfully");
207
- return obj;
208
- } catch (Exception e) {
209
- Log.d("ReactNative", "init internal failed with Error: " + e.getMessage());
210
- throw (e);
211
- }
212
- }
213
-
214
- @ReactMethod
215
- public void mkdir(String path, Promise promise) {
216
- ThreadUtils.runOnExecutor(() -> {
217
- Log.d("ReactNative", "mkdir: path = " + path);
218
- try {
219
- land.fx.wnfslib.Config config = LibKt.mkdir(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), path);
220
- this.rootConfig = config;
221
- promise.resolve(config.getCid());
222
- } catch (Exception e) {
223
- Log.d("get", e.getMessage());
224
- promise.reject(e);
225
- }
226
- });
227
- }
228
-
229
- @ReactMethod
230
- public void writeFile(String fulaTargetFilename, String localFilename, Promise promise) {
231
- /*
232
- // reads content of the file form localFilename (should include full absolute path to local file with read permission
233
- // writes content to the specified location by fulaTargetFilename in Fula filesystem
234
- // fulaTargetFilename: a string including full path and filename of target file on Fula (e.g. root/pictures/cat.jpg)
235
- // localFilename: a string containing full path and filename of local file on hte device (e.g /usr/bin/cat.jpg)
236
- // Returns: new cid of the root after this file is placed in the tree
237
- */
238
- ThreadUtils.runOnExecutor(() -> {
239
- Log.d("ReactNative", "writeFile to : path = " + fulaTargetFilename + ", from: " + localFilename);
240
- try {
241
- land.fx.wnfslib.Config config = LibKt.writeFileFromPath(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), fulaTargetFilename, localFilename);
242
- this.rootConfig = config;
243
- promise.resolve(config.getCid());
244
- } catch (Exception e) {
245
- Log.d("get", e.getMessage());
246
- promise.reject(e);
247
- }
248
- });
249
- }
250
-
251
- @ReactMethod
252
- public void writeFileContent(String path, String contentString, Promise promise) {
253
- ThreadUtils.runOnExecutor(() -> {
254
- Log.d("ReactNative", "writeFile: contentString = " + contentString);
255
- Log.d("ReactNative", "writeFile: path = " + path);
256
- try {
257
- byte[] content = convertStringToByte(contentString);
258
- land.fx.wnfslib.Config config = LibKt.writeFile(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), path, content);
259
- this.rootConfig = config;
260
- promise.resolve(config.getCid());
261
- } catch (Exception e) {
262
- Log.d("get", e.getMessage());
263
- promise.reject(e);
264
- }
265
- });
266
- }
267
-
268
- @ReactMethod
269
- public void ls(String path, Promise promise) {
270
- ThreadUtils.runOnExecutor(() -> {
271
- Log.d("ReactNative", "ls: path = " + path);
272
- try {
273
- String res = LibKt.ls(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), path);
274
- promise.resolve(res);
275
- } catch (Exception e) {
276
- Log.d("get", e.getMessage());
277
- promise.reject(e);
278
- }
279
- });
280
- }
281
-
282
- @ReactMethod
283
- public void readFile(String path, Promise promise) {
284
- ThreadUtils.runOnExecutor(() -> {
285
- Log.d("ReactNative", "ls: path = " + path);
286
- try {
287
- byte[] res = LibKt.readFile(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), path);
288
- String resString = toString(res);
289
- promise.resolve(resString);
290
- } catch (Exception e) {
291
- Log.d("get", e.getMessage());
292
- promise.reject(e);
293
- }
294
- });
295
- }
296
-
297
- @ReactMethod
298
- public void get(String keyString, Promise promise) {
299
- ThreadUtils.runOnExecutor(() -> {
300
- Log.d("ReactNative", "get: keyString = " + keyString);
301
- try {
302
- byte[] key = convertStringToByte(keyString);
303
- byte[] value = getInternal(key);
304
- String valueString = toString(value);
305
- promise.resolve(valueString);
306
- } catch (Exception e) {
307
- Log.d("get", e.getMessage());
308
- promise.reject(e);
309
- }
310
- });
311
- }
312
-
313
- @NonNull
314
- private byte[] getInternal(byte[] key) throws Exception {
315
- try {
316
- Log.d("ReactNative", "getInternal: key.toString() = " + toString(key));
317
- Log.d("ReactNative", "getInternal: key.toString().bytes = " + Arrays.toString(key));
318
- byte[] value = this.fula.get(key);
319
- Log.d("ReactNative", "getInternal: value.toString() = " + toString(value));
320
- return value;
321
- } catch (Exception e) {
322
- Log.d("ReactNative", "getInternal: error = " + e.getMessage());
323
- Log.d("getInternal", e.getMessage());
324
- throw (e);
325
- }
326
- }
327
-
328
- @ReactMethod
329
- public void has(String keyString, Promise promise) {
330
- ThreadUtils.runOnExecutor(() -> {
331
- Log.d("ReactNative", "has: keyString = " + keyString);
332
- try {
333
- byte[] key = convertStringToByte(keyString);
334
- boolean result = hasInternal(key);
335
- promise.resolve(result);
336
- } catch (Exception e) {
337
- Log.d("get", e.getMessage());
338
- promise.reject(e);
339
- }
340
- });
341
- }
342
-
343
- @NonNull
344
- private boolean hasInternal(byte[] key) throws Exception {
345
- try {
346
- boolean res = this.fula.has(key);
347
- return res;
348
- } catch (Exception e) {
349
- Log.d("hasInternal", e.getMessage());
350
- throw (e);
351
- }
352
- }
353
-
354
- @NonNull
355
- private void pullInternal(byte[] key) throws Exception {
356
- try {
357
- this.fula.pull(key);
358
- } catch (Exception e) {
359
- Log.d("pullInternal", e.getMessage());
360
- throw (e);
361
- }
362
- }
363
-
364
- @ReactMethod
365
- public void push(Promise promise) {
366
- ThreadUtils.runOnExecutor(() -> {
367
- Log.d("ReactNative", "push started");
368
- try {
369
- pushInternal(convertStringToByte(this.rootConfig.getCid()));
370
- promise.resolve(this.rootConfig.getCid());
371
- } catch (Exception e) {
372
- Log.d("get", e.getMessage());
373
- promise.reject(e);
374
- }
375
- });
376
- }
377
-
378
- @NonNull
379
- private void pushInternal(byte[] key) throws Exception {
380
- try {
381
- if (this.fula.has(key)) {
382
- this.fula.push(key);
383
- } else {
384
- Log.d("pushInternal", "error: key wasn't found");
385
- throw new Exception("key wasn't found in local storage");
386
- }
387
- } catch (Exception e) {
388
- Log.d("pushInternal", e.getMessage());
389
- throw (e);
390
- }
391
- }
392
-
393
- @ReactMethod
394
- public void put(String valueString, String codecString, Promise promise) {
395
- ThreadUtils.runOnExecutor(() -> {
396
- Log.d("ReactNative", "put: codecString = " + codecString);
397
- Log.d("ReactNative", "put: valueString = " + valueString);
398
- try {
399
- //byte[] codec = convertStringToByte(CodecString);
400
- long codec = Long.parseLong(codecString);
401
-
402
-
403
- Log.d("ReactNative", "put: codec = " + codec);
404
- byte[] value = toByte(valueString);
405
-
406
- Log.d("ReactNative", "put: value.toString() = " + toString(value));
407
- byte[] key = putInternal(value, codec);
408
- Log.d("ReactNative", "put: key.toString() = " + toString(key));
409
- promise.resolve(toString(key));
410
- } catch (Exception e) {
411
- Log.d("ReactNative", "put: error = " + e.getMessage());
412
- Log.d("put", e.getMessage());
413
- promise.reject(e);
414
- }
415
- });
416
- }
417
-
418
- @NonNull
419
- private byte[] putInternal(byte[] value, long codec) throws Exception {
420
- try {
421
- byte[] key = this.fula.put(value, codec);
422
- return key;
423
- } catch (Exception e) {
424
- Log.d("putInternal", e.getMessage());
425
- throw (e);
426
- }
427
- }
428
-
429
- @ReactMethod
430
- public void shutdown(Promise promise) {
431
- ThreadUtils.runOnExecutor(() -> {
432
- try {
433
- fula.shutdown();
434
- promise.resolve(true);
435
- } catch (Exception e) {
436
- promise.reject(e);
437
- Log.d("shutdown", e.getMessage());
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
+ }