@functionland/react-native-fula 0.4.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +133 -35
  3. package/android/.gradle/7.5.1/checksums/checksums.lock +0 -0
  4. package/android/.gradle/7.5.1/checksums/md5-checksums.bin +0 -0
  5. package/android/.gradle/7.5.1/checksums/sha1-checksums.bin +0 -0
  6. package/android/.gradle/7.5.1/dependencies-accessors/dependencies-accessors.lock +0 -0
  7. package/android/.gradle/7.5.1/dependencies-accessors/gc.properties +0 -0
  8. package/android/.gradle/7.5.1/executionHistory/executionHistory.lock +0 -0
  9. package/android/.gradle/7.5.1/fileChanges/last-build.bin +0 -0
  10. package/android/.gradle/7.5.1/fileHashes/fileHashes.bin +0 -0
  11. package/android/.gradle/7.5.1/fileHashes/fileHashes.lock +0 -0
  12. package/android/.gradle/7.5.1/fileHashes/resourceHashesCache.bin +0 -0
  13. package/android/.gradle/7.5.1/gc.properties +0 -0
  14. package/android/.gradle/buildOutputCleanup/buildOutputCleanup.lock +0 -0
  15. package/android/.gradle/buildOutputCleanup/cache.properties +2 -2
  16. package/android/.gradle/buildOutputCleanup/outputFiles.bin +0 -0
  17. package/android/.gradle/vcs-1/gc.properties +0 -0
  18. package/android/.idea/compiler.xml +5 -5
  19. package/android/.idea/gradle.xml +17 -18
  20. package/android/.idea/jarRepositories.xml +44 -49
  21. package/android/.idea/misc.xml +9 -9
  22. package/android/.idea/vcs.xml +5 -5
  23. package/android/build.gradle +7 -5
  24. package/android/gradle/wrapper/gradle-wrapper.properties +1 -0
  25. package/android/gradlew +375 -240
  26. package/android/local.properties +8 -8
  27. package/android/src/main/AndroidManifest.xml +4 -4
  28. package/android/src/main/java/land/fx/fula/ConfigRef.java +7 -7
  29. package/android/src/main/java/land/fx/fula/Cryptography.java +47 -47
  30. package/android/src/main/java/land/fx/fula/FulaModule.java +291 -33
  31. package/android/src/main/java/land/fx/fula/FulaPackage.java +32 -32
  32. package/android/src/main/java/land/fx/fula/SharedPreferenceHelper.java +65 -65
  33. package/android/src/main/java/land/fx/fula/StaticHelper.java +13 -13
  34. package/android/src/main/java/land/fx/fula/ThreadUtils.java +42 -42
  35. package/ios/FulaModule.h +10 -10
  36. package/ios/FulaModule.m +149 -149
  37. package/lib/commonjs/interfaces/fulaNativeModule.js.map +1 -1
  38. package/lib/commonjs/protocols/fula.js +90 -10
  39. package/lib/commonjs/protocols/fula.js.map +1 -1
  40. package/lib/module/interfaces/fulaNativeModule.js.map +1 -1
  41. package/lib/module/protocols/fula.js +81 -6
  42. package/lib/module/protocols/fula.js.map +1 -1
  43. package/lib/typescript/index.d.ts +1 -1
  44. package/lib/typescript/interfaces/fulaNativeModule.d.ts +28 -17
  45. package/lib/typescript/protocols/fula.d.ts +99 -70
  46. package/package.json +5 -8
  47. package/react-native-fula.podspec +19 -19
  48. package/src/interfaces/fulaNativeModule.ts +19 -6
  49. package/src/protocols/fula.ts +107 -11
  50. package/android/.gradle/7.5.1/executionHistory/executionHistory.bin +0 -0
  51. package/android/.gradle/file-system.probe +0 -0
@@ -1,7 +1,7 @@
1
- package land.fx.fula;
2
-
3
- public final class ConfigRef {
4
- public byte[] identity;
5
- public java.lang.String storePath;
6
- }
7
-
1
+ package land.fx.fula;
2
+
3
+ public final class ConfigRef {
4
+ public byte[] identity;
5
+ public java.lang.String storePath;
6
+ }
7
+
@@ -1,47 +1,47 @@
1
- package land.fx.fula;
2
-
3
- import android.util.Base64;
4
-
5
- import java.io.UnsupportedEncodingException;
6
- import java.security.InvalidAlgorithmParameterException;
7
- import java.security.InvalidKeyException;
8
- import java.security.NoSuchAlgorithmException;
9
- import java.security.spec.InvalidKeySpecException;
10
- import java.security.spec.InvalidParameterSpecException;
11
-
12
- import javax.crypto.BadPaddingException;
13
- import javax.crypto.Cipher;
14
- import javax.crypto.IllegalBlockSizeException;
15
- import javax.crypto.NoSuchPaddingException;
16
- import javax.crypto.SecretKey;
17
- import javax.crypto.SecretKeyFactory;
18
- import javax.crypto.spec.PBEKeySpec;
19
- import javax.crypto.spec.SecretKeySpec;
20
-
21
- public class Cryptography {
22
- public static String encryptMsg(String message, SecretKey secret)
23
- throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidParameterSpecException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
24
- Cipher cipher = null;
25
- cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
26
- cipher.init(Cipher.ENCRYPT_MODE, secret);
27
- byte[] cipherText = cipher.doFinal(message.getBytes("UTF-8"));
28
- return Base64.encodeToString(cipherText, Base64.NO_WRAP);
29
- }
30
-
31
- public static String decryptMsg(String cipherText, SecretKey secret)
32
- throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidParameterSpecException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException {
33
- Cipher cipher = null;
34
- cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
35
- cipher.init(Cipher.DECRYPT_MODE, secret);
36
- byte[] decode = Base64.decode(cipherText, Base64.NO_WRAP);
37
- String decryptString = new String(cipher.doFinal(decode), "UTF-8");
38
- return decryptString;
39
- }
40
-
41
- public static SecretKey generateKey(byte[] key)
42
- throws NoSuchAlgorithmException, InvalidKeySpecException {
43
- PBEKeySpec pbeKeySpec = new PBEKeySpec(StaticHelper.bytesToBase64(key).toCharArray(), key, 1000, 128);
44
- SecretKey pbeKey = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256").generateSecret(pbeKeySpec);
45
- return new SecretKeySpec(pbeKey.getEncoded(), "AES");
46
- }
47
- }
1
+ package land.fx.fula;
2
+
3
+ import android.util.Base64;
4
+
5
+ import java.io.UnsupportedEncodingException;
6
+ import java.security.InvalidAlgorithmParameterException;
7
+ import java.security.InvalidKeyException;
8
+ import java.security.NoSuchAlgorithmException;
9
+ import java.security.spec.InvalidKeySpecException;
10
+ import java.security.spec.InvalidParameterSpecException;
11
+
12
+ import javax.crypto.BadPaddingException;
13
+ import javax.crypto.Cipher;
14
+ import javax.crypto.IllegalBlockSizeException;
15
+ import javax.crypto.NoSuchPaddingException;
16
+ import javax.crypto.SecretKey;
17
+ import javax.crypto.SecretKeyFactory;
18
+ import javax.crypto.spec.PBEKeySpec;
19
+ import javax.crypto.spec.SecretKeySpec;
20
+
21
+ public class Cryptography {
22
+ public static String encryptMsg(String message, SecretKey secret)
23
+ throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidParameterSpecException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
24
+ Cipher cipher = null;
25
+ cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
26
+ cipher.init(Cipher.ENCRYPT_MODE, secret);
27
+ byte[] cipherText = cipher.doFinal(message.getBytes("UTF-8"));
28
+ return Base64.encodeToString(cipherText, Base64.NO_WRAP);
29
+ }
30
+
31
+ public static String decryptMsg(String cipherText, SecretKey secret)
32
+ throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidParameterSpecException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, UnsupportedEncodingException {
33
+ Cipher cipher = null;
34
+ cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
35
+ cipher.init(Cipher.DECRYPT_MODE, secret);
36
+ byte[] decode = Base64.decode(cipherText, Base64.NO_WRAP);
37
+ String decryptString = new String(cipher.doFinal(decode), "UTF-8");
38
+ return decryptString;
39
+ }
40
+
41
+ public static SecretKey generateKey(byte[] key)
42
+ throws NoSuchAlgorithmException, InvalidKeySpecException {
43
+ PBEKeySpec pbeKeySpec = new PBEKeySpec(StaticHelper.bytesToBase64(key).toCharArray(), key, 1000, 128);
44
+ SecretKey pbeKey = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256").generateSecret(pbeKeySpec);
45
+ return new SecretKeySpec(pbeKey.getEncoded(), "AES");
46
+ }
47
+ }
@@ -8,8 +8,11 @@ import com.facebook.react.bridge.Promise;
8
8
  import com.facebook.react.bridge.ReactApplicationContext;
9
9
  import com.facebook.react.bridge.ReactContextBaseJavaModule;
10
10
  import com.facebook.react.bridge.ReactMethod;
11
+ import com.facebook.react.bridge.WritableMap;
12
+ import com.facebook.react.bridge.WritableNativeMap;
11
13
  import com.facebook.react.module.annotations.ReactModule;
12
14
 
15
+ import org.apache.commons.io.FileUtils;
13
16
  import org.jetbrains.annotations.Contract;
14
17
 
15
18
  import java.io.File;
@@ -18,13 +21,25 @@ import java.util.Arrays;
18
21
 
19
22
  import javax.crypto.SecretKey;
20
23
 
24
+ import org.json.JSONObject;
25
+ import org.json.JSONArray;
26
+
21
27
  import fulamobile.Config;
22
28
  import fulamobile.Fulamobile;
23
29
 
24
- import land.fx.wnfslib.LibKt;
30
+ import land.fx.wnfslib.Fs;
25
31
 
26
32
  @ReactModule(name = FulaModule.NAME)
27
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
+
28
43
  public static final String NAME = "FulaModule";
29
44
  fulamobile.Client fula;
30
45
  Client client;
@@ -33,36 +48,43 @@ public class FulaModule extends ReactContextBaseJavaModule {
33
48
  String privateForest;
34
49
  land.fx.wnfslib.Config rootConfig;
35
50
  SharedPreferenceHelper sharedPref;
51
+ SecretKey secretKeyGlobal;
52
+ String identityEncryptedGlobal;
36
53
  static String PRIVATE_KEY_STORE_ID = "PRIVATE_KEY";
37
54
 
38
- public class Client implements land.fx.wnfslib.Client {
55
+ public static class Client implements land.fx.wnfslib.Datastore {
39
56
 
40
- private fulamobile.Client internalClient;
57
+ private final fulamobile.Client internalClient;
41
58
 
42
59
  Client(fulamobile.Client clientInput) {
43
- internalClient = clientInput;
60
+ this.internalClient = clientInput;
44
61
  }
45
62
 
46
63
  @NonNull
47
64
  @Override
48
65
  public byte[] get(@NonNull byte[] cid) {
49
66
  try {
50
- internalClient.get(cid);
67
+ Log.d("ReactNative", Arrays.toString(cid));
68
+ return this.internalClient.get(cid);
51
69
  } catch (Exception e) {
52
70
  e.printStackTrace();
53
71
  }
54
- return null;
72
+ Log.d("ReactNative","Error get");
73
+ return cid;
55
74
  }
56
75
 
57
76
  @NonNull
58
77
  @Override
59
78
  public byte[] put(@NonNull byte[] data, long codec) {
60
79
  try {
61
- return client.put(data, codec);
80
+ //Log.d("ReactNative", "data="+ Arrays.toString(data) +" ;codec="+codec);
81
+ return this.internalClient.put(data, codec);
62
82
  } catch (Exception e) {
83
+ Log.d("ReactNative", "put Error="+e.getMessage());
63
84
  e.printStackTrace();
64
85
  }
65
- return null;
86
+ Log.d("ReactNative","Error put");
87
+ return data;
66
88
  }
67
89
  }
68
90
 
@@ -85,7 +107,7 @@ public class FulaModule extends ReactContextBaseJavaModule {
85
107
 
86
108
  @Override
87
109
  @NonNull
88
- public String getName() {
110
+ public java.lang.String getName() {
89
111
  return NAME;
90
112
  }
91
113
 
@@ -129,19 +151,39 @@ public class FulaModule extends ReactContextBaseJavaModule {
129
151
  }
130
152
 
131
153
  @ReactMethod
132
- public void init(String identityString, String storePath, String bloxAddr, Promise promise) {
154
+ public void init(String identityString, String storePath, String bloxAddr, String exchange, String rootConfig, Promise promise) {
133
155
  Log.d("ReactNative", "init started");
134
156
  ThreadUtils.runOnExecutor(() -> {
135
157
  try {
158
+ WritableMap resultData = new WritableNativeMap();
136
159
  Log.d("ReactNative", "init storePath= " + storePath);
137
160
  byte[] identity = toByte(identityString);
138
161
  Log.d("ReactNative", "init identity= " + identityString);
139
- String[] obj = initInternal(identity, storePath, bloxAddr);
162
+ String[] obj = initInternal(identity, storePath, bloxAddr, exchange, rootConfig);
140
163
  Log.d("ReactNative", "init object created: [ " + obj[0] + ", " + obj[1] + ", " + obj[2] + " ]");
141
- promise.resolve(obj);
164
+ resultData.putString("peerId", obj[0]);
165
+ resultData.putString("rootCid", obj[1]);
166
+ resultData.putString("private_ref", obj[2]);
167
+ promise.resolve(resultData);
142
168
  } catch (Exception e) {
143
169
  Log.d("ReactNative", "init failed with Error: " + e.getMessage());
144
- promise.reject(e);
170
+ promise.reject("Error", e.getMessage());
171
+ }
172
+ });
173
+ }
174
+
175
+ @ReactMethod
176
+ public void logout(String identityString, String storePath, Promise promise) {
177
+ Log.d("ReactNative", "logout started");
178
+ ThreadUtils.runOnExecutor(() -> {
179
+ try {
180
+ byte[] identity = toByte(identityString);
181
+ boolean obj = logoutInternal(identity, storePath);
182
+ Log.d("ReactNative", "logout completed");
183
+ promise.resolve(obj);
184
+ } catch (Exception e) {
185
+ Log.d("ReactNative", "logout failed with Error: " + e.getMessage());
186
+ promise.reject("Error", e.getMessage());
145
187
  }
146
188
  });
147
189
  }
@@ -170,12 +212,86 @@ public class FulaModule extends ReactContextBaseJavaModule {
170
212
  }
171
213
  }
172
214
 
215
+ private void loadForestInternal(String privateRef, String cid) throws Exception {
216
+ try {
217
+ this.privateForest = Fs.createPrivateForest(this.client);
218
+ } catch (Exception e) {
219
+ Log.d("ReactNative", "loadForestInternal failed with Error: " + e.getMessage());
220
+ throw (e);
221
+ }
222
+ }
223
+
224
+ private void createNewRootConfig(FulaModule.Client iClient, byte[] identity) throws Exception {
225
+ this.privateForest = Fs.createPrivateForest(iClient);
226
+ Log.d("ReactNative", "privateForest is created: " + this.privateForest);
227
+ this.rootConfig = Fs.createRootDir(iClient, this.privateForest, identity);
228
+ Log.d("ReactNative", "new rootConfig is created: cid=" + this.rootConfig.getCid()+" & private_ref="+this.rootConfig.getPrivate_ref());
229
+
230
+ encrypt_and_store_config();
231
+ }
232
+
233
+ private String getPrivateRef(FulaModule.Client iClient, byte[] wnfsKey, String rootCid) throws Exception {
234
+ Log.d("ReactNative", "getPrivateRef called: rootCid=" + rootCid);
235
+ String privateRef = Fs.getPrivateRef(iClient, wnfsKey, rootCid);
236
+ Log.d("ReactNative", "getPrivateRef completed: privateRef=" + privateRef);
237
+ return privateRef;
238
+ }
239
+
240
+ private boolean encrypt_and_store_config() throws Exception {
241
+ try {
242
+ if(!this.identityEncryptedGlobal.isEmpty() && !this.identityEncryptedGlobal.isEmpty()) {
243
+ String cid_encrypted = Cryptography.encryptMsg(this.rootConfig.getCid(), this.secretKeyGlobal);
244
+ String private_ref_encrypted = Cryptography.encryptMsg(this.rootConfig.getPrivate_ref(), this.secretKeyGlobal);
245
+
246
+ sharedPref.add("cid_encrypted_" + this.identityEncryptedGlobal, cid_encrypted);
247
+ sharedPref.add("private_ref_encrypted_" + this.identityEncryptedGlobal, private_ref_encrypted);
248
+ return true;
249
+ } else {
250
+ return false;
251
+ }
252
+ } catch (Exception e) {
253
+ Log.d("ReactNative", "encrypt_and_store_config failed with Error: " + e.getMessage());
254
+ throw (e);
255
+ }
256
+ }
257
+
258
+ @NonNull
259
+ private boolean logoutInternal(byte[] identity, String storePath) throws Exception {
260
+ try {
261
+ SecretKey secretKey = Cryptography.generateKey(identity);
262
+ String identity_encrypted = Cryptography.encryptMsg(Arrays.toString(identity), secretKey);
263
+ sharedPref.remove("cid_encrypted_"+ identity_encrypted);
264
+ sharedPref.remove("private_ref_encrypted_"+identity_encrypted);
265
+
266
+ //TODO: Should also remove peerid @Mahdi
267
+
268
+ sharedPref.remove("cid_encrypted_"+ identity_encrypted);
269
+ sharedPref.remove("private_ref_encrypted_"+ identity_encrypted);
270
+
271
+ this.rootConfig = null;
272
+ this.secretKeyGlobal = null;
273
+ this.identityEncryptedGlobal = null;
274
+
275
+ if (storePath == null || storePath.trim().isEmpty()) {
276
+ storePath = this.fulaStorePath;
277
+ }
278
+
279
+ File file = new File(storePath);
280
+ FileUtils.deleteDirectory(file);
281
+ return true;
282
+
283
+ } catch (Exception e) {
284
+ Log.d("ReactNative", "logout internal failed with Error: " + e.getMessage());
285
+ throw (e);
286
+ }
287
+ }
288
+
173
289
  @NonNull
174
- private String[] initInternal(byte[] identity, String storePath, String bloxAddr) throws Exception {
290
+ private String[] initInternal(byte[] identity, String storePath, String bloxAddr, String exchange, String rootCid) throws Exception {
175
291
  try {
176
292
  Config config_ext = new Config();
177
293
  if (storePath == null || storePath.trim().isEmpty()) {
178
- config_ext.setStorePath(fulaStorePath);
294
+ config_ext.setStorePath(this.fulaStorePath);
179
295
  } else {
180
296
  config_ext.setStorePath(storePath);
181
297
  }
@@ -186,17 +302,87 @@ public class FulaModule extends ReactContextBaseJavaModule {
186
302
  Log.d("ReactNative", "peerIdentity is set: " + toString(config_ext.getIdentity()));
187
303
  config_ext.setBloxAddr(bloxAddr);
188
304
  Log.d("ReactNative", "bloxAddr is set: " + config_ext.getBloxAddr());
305
+ config_ext.setExchange(exchange);
189
306
  this.fula = Fulamobile.newClient(config_ext);
190
307
  this.client = new Client(this.fula);
191
308
  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());
309
+
310
+ SecretKey secretKey = Cryptography.generateKey(identity);
311
+ String identity_encrypted =Cryptography.encryptMsg(Arrays.toString(identity), secretKey);
312
+ this.identityEncryptedGlobal = identity_encrypted;
313
+ this.secretKeyGlobal = secretKey;
314
+
315
+ if (this.rootConfig == null || this.rootConfig.getCid().isEmpty() || this.rootConfig.getPrivate_ref().isEmpty()) {
316
+ Log.d("ReactNative", "this.rootCid is empty.");
317
+ //Load from keystore
318
+
319
+ String cid_encrypted_fetched = sharedPref.getValue("cid_encrypted_"+ identity_encrypted);
320
+ String private_ref_encrypted_fetched = sharedPref.getValue("private_ref_encrypted_"+identity_encrypted);
321
+ Log.d("ReactNative", "Here1");
322
+ String cid = "";
323
+ String private_ref = "";
324
+ if(cid_encrypted_fetched != null && !cid_encrypted_fetched.isEmpty()) {
325
+ Log.d("ReactNative", "decrypting cid="+cid_encrypted_fetched+" with secret="+secretKey.toString());
326
+ cid = Cryptography.decryptMsg(cid_encrypted_fetched, secretKey);
327
+ }
328
+ if(private_ref_encrypted_fetched != null && !private_ref_encrypted_fetched.isEmpty()) {
329
+ Log.d("ReactNative", "decrypting private_ref="+private_ref_encrypted_fetched+" with secret="+secretKey.toString());
330
+ private_ref = Cryptography.decryptMsg(private_ref_encrypted_fetched, secretKey);
331
+ }
332
+ Log.d("ReactNative", "Here2");
333
+ //Log.d("ReactNative", "Attempted to fetch cid from keystore; cid="+cid+" & private_ref="+private_ref);
334
+ if((cid == null || cid.isEmpty()) || (private_ref == null || private_ref.isEmpty()) ){
335
+ Log.d("ReactNative", "cid or PrivateRef was not found");
336
+ if(rootCid !=null && !rootCid.isEmpty()){
337
+ Log.d("ReactNative", "Re-setting cid from input: "+rootCid);
338
+ cid = rootCid;
339
+ }
340
+ if((private_ref == null || private_ref.isEmpty()) && (cid != null && !cid.isEmpty())){
341
+ Log.d("ReactNative", "Re-fetching privateRef from wnfs: cid="+cid);
342
+ private_ref = getPrivateRef(this.client, identity, cid);
343
+ Log.d("ReactNative", "Re-fetching privateRef from wnfs: "+private_ref);
344
+ }
345
+ if(cid == null || cid.isEmpty() || private_ref == null || private_ref.isEmpty()) {
346
+ Log.d("ReactNative", "Tried to recover cid and privateRef but was not successful. Creating new ones");
347
+ createNewRootConfig(this.client, identity);
348
+ } else {
349
+ Log.d("ReactNative", "Tried to recover cid and privateRef and was successful. cid:"+cid+" & private_ref="+private_ref);
350
+ this.rootConfig = new land.fx.wnfslib.Config(cid, private_ref);
351
+ encrypt_and_store_config();
352
+ }
353
+ } else if(cid != null && !cid.isEmpty() && private_ref != null && !private_ref.isEmpty()) {
354
+ Log.d("ReactNative", "Found cid and private ref in keychain store");
355
+ if(cid != null && !cid.isEmpty() && private_ref!=null && !private_ref.isEmpty()) {
356
+ Log.d("ReactNative", "Recovered cid and private ref from keychain store. cid="+cid+" & private_ref="+private_ref);
357
+ this.rootConfig = new land.fx.wnfslib.Config(cid, private_ref);
358
+ } else{
359
+ Log.d("ReactNative", "Found but Could not recover cid and private_ref from keychain store");
360
+ createNewRootConfig(this.client, identity);
361
+ }
362
+ } else{
363
+ Log.d("ReactNative", "This cid and private_ref generation should never happen!!!");
364
+ //Create new root and store cid and private_ref
365
+ createNewRootConfig(this.client, identity);
366
+ }
367
+
368
+
369
+ Log.d("ReactNative", "creating rootConfig completed");
370
+
371
+ /*
372
+ byte[] testbyte = convertStringToByte("-104,40,24,-93,24,100,24,114,24,111,24,111,24,116,24,-126,24,-126,0,0,24,-128,24,103,24,118,24,101,24,114,24,115,24,105,24,111,24,110,24,101,24,48,24,46,24,49,24,46,24,48,24,105,24,115,24,116,24,114,24,117,24,99,24,116,24,117,24,114,24,101,24,100,24,104,24,97,24,109,24,116");
373
+ long testcodec = 85;
374
+ byte[] testputcid = this.client.put(testbyte, testcodec);
375
+ Log.d("ReactNative", "client.put test done"+ Arrays.toString(testputcid));
376
+ byte[] testfetchedcid = convertStringToByte("1,113,18,32,-6,-63,-128,79,-102,-89,57,77,-8,67,-98,8,-81,40,-87,123,122,29,-52,-124,-60,-53,100,105,125,123,-5,-99,41,106,-124,-64");
377
+ byte[] testfetchedbytes = this.client.get(testfetchedcid);
378
+ Log.d("ReactNative", "client.get test done"+ Arrays.toString(testfetchedbytes));
379
+ */
380
+
381
+
382
+ Log.d("ReactNative", "rootConfig is created: cid=" + this.rootConfig.getCid()+ "& private_ref="+this
383
+ .rootConfig.getPrivate_ref());
198
384
  } else {
199
- Log.d("ReactNative", "rootConfig existed: " + this.rootConfig.getCid());
385
+ Log.d("ReactNative", "rootConfig existed: cid=" + this.rootConfig.getCid()+ " & private_ref="+this.rootConfig.getPrivate_ref());
200
386
  }
201
387
  String peerId = this.fula.id();
202
388
  String[] obj = new String[3];
@@ -216,7 +402,7 @@ public class FulaModule extends ReactContextBaseJavaModule {
216
402
  ThreadUtils.runOnExecutor(() -> {
217
403
  Log.d("ReactNative", "mkdir: path = " + path);
218
404
  try {
219
- land.fx.wnfslib.Config config = LibKt.mkdir(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), path);
405
+ land.fx.wnfslib.Config config = Fs.mkdir(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), path);
220
406
  this.rootConfig = config;
221
407
  promise.resolve(config.getCid());
222
408
  } catch (Exception e) {
@@ -238,8 +424,9 @@ public class FulaModule extends ReactContextBaseJavaModule {
238
424
  ThreadUtils.runOnExecutor(() -> {
239
425
  Log.d("ReactNative", "writeFile to : path = " + fulaTargetFilename + ", from: " + localFilename);
240
426
  try {
241
- land.fx.wnfslib.Config config = LibKt.writeFileFromPath(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), fulaTargetFilename, localFilename);
427
+ land.fx.wnfslib.Config config = Fs.writeFileFromPath(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), fulaTargetFilename, localFilename);
242
428
  this.rootConfig = config;
429
+ encrypt_and_store_config();
243
430
  promise.resolve(config.getCid());
244
431
  } catch (Exception e) {
245
432
  Log.d("get", e.getMessage());
@@ -255,8 +442,9 @@ public class FulaModule extends ReactContextBaseJavaModule {
255
442
  Log.d("ReactNative", "writeFile: path = " + path);
256
443
  try {
257
444
  byte[] content = convertStringToByte(contentString);
258
- land.fx.wnfslib.Config config = LibKt.writeFile(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), path, content);
445
+ land.fx.wnfslib.Config config = Fs.writeFile(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), path, content);
259
446
  this.rootConfig = config;
447
+ encrypt_and_store_config();
260
448
  promise.resolve(config.getCid());
261
449
  } catch (Exception e) {
262
450
  Log.d("get", e.getMessage());
@@ -270,8 +458,12 @@ public class FulaModule extends ReactContextBaseJavaModule {
270
458
  ThreadUtils.runOnExecutor(() -> {
271
459
  Log.d("ReactNative", "ls: path = " + path);
272
460
  try {
273
- String res = LibKt.ls(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), path);
274
- promise.resolve(res);
461
+ byte[] res = Fs.ls(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), path);
462
+
463
+ //JSONArray jsonArray = new JSONArray(res);
464
+ String s = new String(res, StandardCharsets.UTF_8);
465
+ Log.d("ReactNative", "ls: res = " + s);
466
+ promise.resolve(s);
275
467
  } catch (Exception e) {
276
468
  Log.d("get", e.getMessage());
277
469
  promise.reject(e);
@@ -280,11 +472,80 @@ public class FulaModule extends ReactContextBaseJavaModule {
280
472
  }
281
473
 
282
474
  @ReactMethod
283
- public void readFile(String path, Promise promise) {
475
+ public void rm(String path, Promise promise) {
284
476
  ThreadUtils.runOnExecutor(() -> {
285
- Log.d("ReactNative", "ls: path = " + path);
477
+ Log.d("ReactNative", "rm: path = " + path);
286
478
  try {
287
- byte[] res = LibKt.readFile(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), path);
479
+ land.fx.wnfslib.Config config = Fs.rm(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), path);
480
+ this.rootConfig = config;
481
+ encrypt_and_store_config();
482
+ promise.resolve(config.getCid());
483
+ } catch (Exception e) {
484
+ Log.d("get", e.getMessage());
485
+ promise.reject(e);
486
+ }
487
+ });
488
+ }
489
+
490
+ @ReactMethod
491
+ public void cp(String sourcePath, String targetPath, Promise promise) {
492
+ ThreadUtils.runOnExecutor(() -> {
493
+ Log.d("ReactNative", "rm: sourcePath = " + sourcePath);
494
+ try {
495
+ land.fx.wnfslib.Config config = Fs.cp(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), sourcePath, targetPath);
496
+ this.rootConfig = config;
497
+ encrypt_and_store_config();
498
+ promise.resolve(config.getCid());
499
+ } catch (Exception e) {
500
+ Log.d("get", e.getMessage());
501
+ promise.reject(e);
502
+ }
503
+ });
504
+ }
505
+
506
+ @ReactMethod
507
+ public void mv(String sourcePath, String targetPath, Promise promise) {
508
+ ThreadUtils.runOnExecutor(() -> {
509
+ Log.d("ReactNative", "rm: sourcePath = " + sourcePath);
510
+ try {
511
+ land.fx.wnfslib.Config config = Fs.mv(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), sourcePath, targetPath);
512
+ this.rootConfig = config;
513
+ encrypt_and_store_config();
514
+ promise.resolve(config.getCid());
515
+ } catch (Exception e) {
516
+ Log.d("get", e.getMessage());
517
+ promise.reject(e);
518
+ }
519
+ });
520
+ }
521
+
522
+ @ReactMethod
523
+ public void readFile(String fulaTargetFilename, String localFilename, Promise promise) {
524
+ /*
525
+ // reads content of the file form localFilename (should include full absolute path to local file with read permission
526
+ // writes content to the specified location by fulaTargetFilename in Fula filesystem
527
+ // fulaTargetFilename: a string including full path and filename of target file on Fula (e.g. root/pictures/cat.jpg)
528
+ // localFilename: a string containing full path and filename of local file on hte device (e.g /usr/bin/cat.jpg)
529
+ // Returns: new cid of the root after this file is placed in the tree
530
+ */
531
+ ThreadUtils.runOnExecutor(() -> {
532
+ Log.d("ReactNative", "readFile: fulaTargetFilename = " + fulaTargetFilename);
533
+ try {
534
+ String path = Fs.readFilestreamToPath(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), fulaTargetFilename, localFilename);
535
+ promise.resolve(path);
536
+ } catch (Exception e) {
537
+ Log.d("get", e.getMessage());
538
+ promise.reject(e);
539
+ }
540
+ });
541
+ }
542
+
543
+ @ReactMethod
544
+ public void readFileContent(String path, Promise promise) {
545
+ ThreadUtils.runOnExecutor(() -> {
546
+ Log.d("ReactNative", "readFileContent: path = " + path);
547
+ try {
548
+ byte[] res = Fs.readFile(this.client, this.rootConfig.getCid(), this.rootConfig.getPrivate_ref(), path);
288
549
  String resString = toString(res);
289
550
  promise.resolve(resString);
290
551
  } catch (Exception e) {
@@ -340,7 +601,6 @@ public class FulaModule extends ReactContextBaseJavaModule {
340
601
  });
341
602
  }
342
603
 
343
- @NonNull
344
604
  private boolean hasInternal(byte[] key) throws Exception {
345
605
  try {
346
606
  boolean res = this.fula.has(key);
@@ -351,7 +611,6 @@ public class FulaModule extends ReactContextBaseJavaModule {
351
611
  }
352
612
  }
353
613
 
354
- @NonNull
355
614
  private void pullInternal(byte[] key) throws Exception {
356
615
  try {
357
616
  this.fula.pull(key);
@@ -375,7 +634,6 @@ public class FulaModule extends ReactContextBaseJavaModule {
375
634
  });
376
635
  }
377
636
 
378
- @NonNull
379
637
  private void pushInternal(byte[] key) throws Exception {
380
638
  try {
381
639
  if (this.fula.has(key)) {
@@ -1,32 +1,32 @@
1
- package land.fx.fula;
2
-
3
- import androidx.annotation.NonNull;
4
-
5
- import com.facebook.react.ReactPackage;
6
- import com.facebook.react.bridge.NativeModule;
7
- import com.facebook.react.bridge.ReactApplicationContext;
8
- import com.facebook.react.uimanager.ViewManager;
9
-
10
- import java.util.ArrayList;
11
- import java.util.Collections;
12
- import java.util.List;
13
-
14
- public class FulaPackage implements ReactPackage {
15
- @NonNull
16
- @Override
17
- public List<NativeModule> createNativeModules(@NonNull ReactApplicationContext reactContext) {
18
- List<NativeModule> modules = new ArrayList<>();
19
- try {
20
- modules.add(new FulaModule(reactContext));
21
- } catch (Exception e) {
22
- e.printStackTrace();
23
- }
24
- return modules;
25
- }
26
-
27
- @NonNull
28
- @Override
29
- public List<ViewManager> createViewManagers(@NonNull ReactApplicationContext reactContext) {
30
- return Collections.emptyList();
31
- }
32
- }
1
+ package land.fx.fula;
2
+
3
+ import androidx.annotation.NonNull;
4
+
5
+ import com.facebook.react.ReactPackage;
6
+ import com.facebook.react.bridge.NativeModule;
7
+ import com.facebook.react.bridge.ReactApplicationContext;
8
+ import com.facebook.react.uimanager.ViewManager;
9
+
10
+ import java.util.ArrayList;
11
+ import java.util.Collections;
12
+ import java.util.List;
13
+
14
+ public class FulaPackage implements ReactPackage {
15
+ @NonNull
16
+ @Override
17
+ public List<NativeModule> createNativeModules(@NonNull ReactApplicationContext reactContext) {
18
+ List<NativeModule> modules = new ArrayList<>();
19
+ try {
20
+ modules.add(new FulaModule(reactContext));
21
+ } catch (Exception e) {
22
+ e.printStackTrace();
23
+ }
24
+ return modules;
25
+ }
26
+
27
+ @NonNull
28
+ @Override
29
+ public List<ViewManager> createViewManagers(@NonNull ReactApplicationContext reactContext) {
30
+ return Collections.emptyList();
31
+ }
32
+ }