@@ -67,7 +67,7 @@ public void initialize() {
6767 SharedPreferenceHelper sharedPref ;
6868 SecretKey secretKeyGlobal ;
6969 String identityEncryptedGlobal ;
70- static String PRIVATE_KEY_STORE_ID = "PRIVATE_KEY" ;
70+ static String PRIVATE_KEY_STORE_PEERID = "PRIVATE_KEY" ;
7171
7272 public static class Client implements land .fx .wnfslib .Datastore {
7373
@@ -133,6 +133,15 @@ private byte[] toByte(@NonNull String input) {
133133 return input .getBytes (StandardCharsets .UTF_8 );
134134 }
135135
136+ private byte [] decToByte (@ NonNull String input ) {
137+ String [] parts = input .split ("," );
138+ byte [] output = new byte [parts .length ];
139+ for (int i = 0 ; i < parts .length ; i ++) {
140+ output [i ] = Byte .parseByte (parts [i ]);
141+ }
142+ return output ;
143+ }
144+
136145 @ NonNull
137146 @ Contract ("_ -> new" )
138147 public String toString (byte [] input ) {
@@ -422,30 +431,48 @@ private boolean retryFailedActionsInternal(int timeout) throws Exception {
422431 }
423432
424433 @ NonNull
425- private byte [] createPeerIdentity (byte [] privateKey ) throws GeneralSecurityException , IOException {
434+ private byte [] createPeerIdentity (byte [] identity ) throws GeneralSecurityException , IOException {
426435 try {
427436 // 1: First: create public key from provided private key
428437 // 2: Should read the local keychain store (if it is key-value, key is public key above,
429438 // 3: if found, decrypt using the private key
430439 // 4: If not found or decryption not successful, generate an identity
431440 // 5: then encrypt and store in keychain
432- String encryptedKey = sharedPref .getValue (PRIVATE_KEY_STORE_ID );
433- SecretKey secretKey = Cryptography .generateKey (privateKey );
441+ byte [] libp2pId ;
442+ String encryptedLibp2pId = sharedPref .getValue (PRIVATE_KEY_STORE_PEERID );
443+ byte [] encryptionPair ;
444+ SecretKey encryptionSecretKey ;
445+ try {
446+ encryptionSecretKey = Cryptography .generateKey (identity );
447+ Log .d ("ReactNative" , "encryptionSecretKey generated from privateKey" );
448+ } catch (Exception e ) {
449+ Log .d ("ReactNative" , "Failed to generate key for encryption: " + e .getMessage ());
450+ throw new GeneralSecurityException ("Failed to generate key encryption" , e );
451+ }
452+
453+ if (encryptedLibp2pId == null || !encryptedLibp2pId .startsWith ("FULA_ENC_V3:" )) {
454+ Log .d ("ReactNative" , "encryptedLibp2pId is not correct. creating new one " + encryptedLibp2pId );
434455
435- if (encryptedKey == null || !encryptedKey .startsWith ("FULA_ENC_V2:" )) {
436- byte [] autoGeneratedIdentity ;
437456 try {
438- autoGeneratedIdentity = Fulamobile .generateEd25519Key ( );
457+ libp2pId = Fulamobile .generateEd25519KeyFromString ( toString ( identity ) );
439458 } catch (Exception e ) {
440- Log .d ("ReactNative" , "Failed to generate key : " + e .getMessage ());
441- throw new GeneralSecurityException ("Failed to generate key " , e );
459+ Log .d ("ReactNative" , "Failed to generate libp2pId : " + e .getMessage ());
460+ throw new GeneralSecurityException ("Failed to generate libp2pId " , e );
442461 }
443- encryptedKey = "FULA_ENC_V2:" + Cryptography .encryptMsg (StaticHelper .bytesToBase64 (autoGeneratedIdentity ), secretKey );
444- sharedPref .add (PRIVATE_KEY_STORE_ID , encryptedKey );
462+ encryptedLibp2pId = "FULA_ENC_V3:" + Cryptography .encryptMsg (StaticHelper .bytesToBase64 (libp2pId ), encryptionSecretKey );
463+ sharedPref .add (PRIVATE_KEY_STORE_PEERID , encryptedLibp2pId );
464+ } else {
465+ Log .d ("ReactNative" , "encryptedLibp2pId is correct. decrypting " + encryptedLibp2pId );
445466 }
446467
447- String decryptedKey = Cryptography .decryptMsg (encryptedKey .replace ("FULA_ENC_V2:" , "" ), secretKey );
448- return StaticHelper .base64ToBytes (decryptedKey );
468+ try {
469+ String decryptedLibp2pId = Cryptography .decryptMsg (encryptedLibp2pId .replace ("FULA_ENC_V3:" , "" ), encryptionSecretKey );
470+
471+ return StaticHelper .base64ToBytes (decryptedLibp2pId );
472+ } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException e ) {
473+ Log .d ("ReactNative" , "createPeerIdentity decryptMsg failed with Error: " + e .getMessage ());
474+ throw (e );
475+ }
449476
450477 } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException e ) {
451478 Log .d ("ReactNative" , "createPeerIdentity failed with Error: " + e .getMessage ());
@@ -478,8 +505,8 @@ private boolean encrypt_and_store_config() throws Exception {
478505 String cid_encrypted = Cryptography .encryptMsg (this .rootConfig .getCid (), this .secretKeyGlobal );
479506 String private_ref_encrypted = Cryptography .encryptMsg (this .rootConfig .getPrivate_ref (), this .secretKeyGlobal );
480507
481- sharedPref .add ("FULA_ENC_V2 :cid_encrypted_" + this .identityEncryptedGlobal , cid_encrypted );
482- sharedPref .add ("FULA_ENC_V2 :private_ref_encrypted_" + this .identityEncryptedGlobal , private_ref_encrypted );
508+ sharedPref .add ("FULA_ENC_V3 :cid_encrypted_" + this .identityEncryptedGlobal , cid_encrypted );
509+ sharedPref .add ("FULA_ENC_V3 :private_ref_encrypted_" + this .identityEncryptedGlobal , private_ref_encrypted );
483510 return true ;
484511 } else {
485512 return false ;
@@ -496,14 +523,15 @@ private boolean logoutInternal(byte[] identity, String storePath) throws Excepti
496523 this .fula .flush ();
497524 }
498525 SecretKey secretKey = Cryptography .generateKey (identity );
526+
499527 String identity_encrypted = Cryptography .encryptMsg (Arrays .toString (identity ), secretKey );
500- sharedPref .remove ("FULA_ENC_V2 :cid_encrypted_" + identity_encrypted );
501- sharedPref .remove ("FULA_ENC_V2 :private_ref_encrypted_" +identity_encrypted );
528+ sharedPref .remove ("FULA_ENC_V3 :cid_encrypted_" + identity_encrypted );
529+ sharedPref .remove ("FULA_ENC_V3 :private_ref_encrypted_" +identity_encrypted );
502530
503531 //TODO: Should also remove peerid @Mahdi
504532
505- sharedPref .remove ("FULA_ENC_V2 :cid_encrypted_" + identity_encrypted );
506- sharedPref .remove ("FULA_ENC_V2 :private_ref_encrypted_" + identity_encrypted );
533+ sharedPref .remove ("FULA_ENC_V3 :cid_encrypted_" + identity_encrypted );
534+ sharedPref .remove ("FULA_ENC_V3 :private_ref_encrypted_" + identity_encrypted );
507535
508536 this .rootConfig = null ;
509537 this .secretKeyGlobal = null ;
@@ -591,8 +619,8 @@ private String[] initInternal(byte[] identity, String storePath, String bloxAddr
591619 Log .d ("ReactNative" , "this.rootCid is empty." );
592620 //Load from keystore
593621
594- String cid_encrypted_fetched = sharedPref .getValue ("FULA_ENC_V2 :cid_encrypted_" + identity_encrypted );
595- String private_ref_encrypted_fetched = sharedPref .getValue ("FULA_ENC_V2 :private_ref_encrypted_" +identity_encrypted );
622+ String cid_encrypted_fetched = sharedPref .getValue ("FULA_ENC_V3 :cid_encrypted_" + identity_encrypted );
623+ String private_ref_encrypted_fetched = sharedPref .getValue ("FULA_ENC_V3 :private_ref_encrypted_" +identity_encrypted );
596624 Log .d ("ReactNative" , "Here1" );
597625 String cid = "" ;
598626 String private_ref = "" ;
@@ -1301,6 +1329,10 @@ public void removeStoredReplication(String seedString, String uploader, long poo
13011329 });
13021330 }
13031331
1332+ ////////////////////////////////////////////////////////////////
1333+ ///////////////// Blox Hardware Methods ////////////////////////
1334+ ////////////////////////////////////////////////////////////////
1335+
13041336 @ ReactMethod
13051337 public void bloxFreeSpace (Promise promise ) {
13061338 ThreadUtils .runOnExecutor (() -> {
@@ -1317,4 +1349,36 @@ public void bloxFreeSpace(Promise promise) {
13171349 });
13181350 }
13191351
1352+ @ ReactMethod
1353+ public void wifiRemoveall (Promise promise ) {
1354+ ThreadUtils .runOnExecutor (() -> {
1355+ Log .d ("ReactNative" , "wifiRemoveall" );
1356+ try {
1357+ byte [] result = this .fula .wifiRemoveall ();
1358+ String resultString = toString (result );
1359+ Log .d ("ReactNative" , "result string=" +resultString );
1360+ promise .resolve (resultString );
1361+ } catch (Exception e ) {
1362+ Log .d ("ReactNative" , e .getMessage ());
1363+ promise .reject (e );
1364+ }
1365+ });
1366+ }
1367+
1368+ @ ReactMethod
1369+ public void reboot (Promise promise ) {
1370+ ThreadUtils .runOnExecutor (() -> {
1371+ Log .d ("ReactNative" , "reboot" );
1372+ try {
1373+ byte [] result = this .fula .reboot ();
1374+ String resultString = toString (result );
1375+ Log .d ("ReactNative" , "result string=" +resultString );
1376+ promise .resolve (resultString );
1377+ } catch (Exception e ) {
1378+ Log .d ("ReactNative" , e .getMessage ());
1379+ promise .reject (e );
1380+ }
1381+ });
1382+ }
1383+
13201384}
0 commit comments