Skip to content

Commit dc0b2e1

Browse files
committed
Storing cid and private_ref in key store
1 parent 3dd1f11 commit dc0b2e1

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

android/src/main/java/land/fx/fula/FulaModule.java

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,16 @@ private byte[] createPeerIdentity(byte[] privateKey) throws Exception {
190190
}
191191
}
192192

193+
@NonNull
194+
private void loadForestInternal(String privateRef, String cid) throws Exception {
195+
try {
196+
this.privateForest = Fs.createPrivateForest(this.client);
197+
} catch (Exception e) {
198+
Log.d("ReactNative", "loadForestInternal failed with Error: " + e.getMessage());
199+
throw (e);
200+
}
201+
}
202+
193203
@NonNull
194204
private String[] initInternal(byte[] identity, String storePath, String bloxAddr, String exchange) throws Exception {
195205
try {
@@ -210,21 +220,41 @@ private String[] initInternal(byte[] identity, String storePath, String bloxAddr
210220
this.fula = Fulamobile.newClient(config_ext);
211221
this.client = new Client(this.fula);
212222
Log.d("ReactNative", "fula initialized: " + this.fula.id());
223+
213224
if (this.rootConfig == null) {
225+
226+
//Load from keystore
227+
String cid = sharedPref.getValue("cid_encrypted");
228+
String private_ref = sharedPref.getValue("private_ref_encrypted");
229+
SecretKey secretKey = Cryptography.generateKey(identity);
230+
if(cid != null && !cid.isEmpty() && cid != "" && private_ref != null && !private_ref.isEmpty() && private_ref != "") {
231+
String cid_decrypted = Cryptography.decryptMsg(cid, secretKey);
232+
String private_ref_decrypted = Cryptography.decryptMsg(private_ref, secretKey);
233+
this.rootConfig = new land.fx.wnfslib.Config(cid_decrypted, private_ref_decrypted);
234+
}else{
235+
this.privateForest = Fs.createPrivateForest(this.client);
236+
Log.d("ReactNative", "privateForest is created: " + this.privateForest);
237+
this.rootConfig = Fs.createRootDir(this.client, this.privateForest);
238+
String cid_encrypted = Cryptography.encryptMsg(this.rootConfig.getCid(), secretKey);
239+
String private_ref_encrypted = Cryptography.encryptMsg(this.rootConfig.getPrivate_ref(), secretKey);
240+
sharedPref.add("cid_encrypted", cid_encrypted);
241+
sharedPref.add("private_ref_encrypted", private_ref_encrypted);
242+
}
243+
244+
214245
Log.d("ReactNative", "creating rootConfig");
215246

216-
/*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");
247+
/*
248+
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");
217249
long testcodec = 85;
218250
byte[] testputcid = this.client.put(testbyte, testcodec);
219-
Log.d("ReactNative", "client.put test done"+ Arrays.toString(testputcid));
251+
Log.d("ReactNative", "client.put test done"+ Arrays.toString(testputcid));
220252
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");
221253
byte[] testfetchedbytes = this.client.get(testfetchedcid);
222254
Log.d("ReactNative", "client.get test done"+ Arrays.toString(testfetchedbytes));
223-
*/
255+
*/
256+
224257

225-
this.privateForest = Fs.createPrivateForest(this.client);
226-
Log.d("ReactNative", "privateForest is created: " + this.privateForest);
227-
this.rootConfig = Fs.createRootDir(this.client, this.privateForest);
228258
Log.d("ReactNative", "rootConfig is created: " + this.rootConfig.getCid());
229259
} else {
230260
Log.d("ReactNative", "rootConfig existed: " + this.rootConfig.getCid());

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@functionland/react-native-fula",
3-
"version": "0.4.2",
3+
"version": "0.4.3",
44
"description": "This package is a bridge to use the Fula libp2p protocols in the react-native which is using wnfs",
55
"main": "lib/commonjs/index",
66
"module": "lib/module/index",

0 commit comments

Comments
 (0)