@@ -509,32 +509,68 @@ private byte[] createPeerIdentity(byte[] identity) throws GeneralSecurityExcepti
509509
510510 if (encryptedLibp2pId == null || !encryptedLibp2pId .startsWith ("FULA_" +
511511 "ENC_V4:" )) {
512- Log .d ("ReactNative" , "encryptedLibp2pId is not correct. creating new one " + encryptedLibp2pId );
513-
514- try {
515- libp2pId = Fulamobile .generateEd25519KeyFromString (toString (identity ));
516- } catch (Exception e ) {
517- Log .d ("ReactNative" , "Failed to generate libp2pId: " + e .getMessage ());
518- throw new GeneralSecurityException ("Failed to generate libp2pId" , e );
519- }
520- encryptedLibp2pId = "FULA_ENC_V4:" + Cryptography .encryptMsg (StaticHelper .bytesToBase64 (libp2pId ), encryptionSecretKey , null );
521- sharedPref .add (PRIVATE_KEY_STORE_PEERID , encryptedLibp2pId );
512+ Log .d ("ReactNative" , "encryptedLibp2pId is not correct or empty. creating new one " + encryptedLibp2pId );
513+ encryptedLibp2pId = createEncryptedLibp2pId (identity , encryptionSecretKey );
522514 } else {
523515 Log .d ("ReactNative" , "encryptedLibp2pId is correct. decrypting " + encryptedLibp2pId );
524516 }
525517
526518 try {
527- String decryptedLibp2pId = Cryptography .decryptMsg (encryptedLibp2pId .replace ("FULA_ENC_V4:" , "" ), encryptionSecretKey );
528-
519+ String decryptedLibp2pId = decryptLibp2pIdentity (encryptedLibp2pId , encryptionSecretKey );
529520 return StaticHelper .base64ToBytes (decryptedLibp2pId );
530521 } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException e ) {
531522 Log .d ("ReactNative" , "createPeerIdentity decryptMsg failed with Error: " + e .getMessage ());
532- throw (e );
523+ Log .d ("ReactNative" , "creating new encrpyted identity" );
524+ try {
525+ encryptedLibp2pId = createEncryptedLibp2pId (identity , encryptionSecretKey );
526+ String decryptedLibp2pId = decryptLibp2pIdentity (encryptedLibp2pId , encryptionSecretKey );
527+ return StaticHelper .base64ToBytes (decryptedLibp2pId );
528+ } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException e2 ) {
529+ Log .d ("ReactNative" , "createPeerIdentity decryptMsg failed with Error: " + e2 .getMessage ());
530+ Log .d ("ReactNative" , "creating new encrpyted identity" );
531+ throw (e2 );
532+ }
533533 }
534534
535535 } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException e ) {
536536 Log .d ("ReactNative" , "createPeerIdentity failed with Error: " + e .getMessage ());
537537 throw (e );
538+ } catch (Exception e ) {
539+ throw new RuntimeException (e );
540+ }
541+ }
542+
543+ private String decryptLibp2pIdentity (String encryptedLibp2pId , SecretKey encryptionSecretKey ) throws Exception {
544+ try {
545+ String decryptedLibp2pId = Cryptography .decryptMsg (encryptedLibp2pId .replace ("FULA_ENC_V4:" , "" ), encryptionSecretKey );
546+
547+ return decryptedLibp2pId ;
548+ } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException e ) {
549+ Log .d ("ReactNative" , "createPeerIdentity decryptMsg failed with Error: " + e .getMessage ());
550+ Log .d ("ReactNative" , "creating new encrpyted identity" );
551+ throw new GeneralSecurityException ("decryptLibp2pIdentity Failed to decrypt libp2pId" , e );
552+ }
553+ }
554+
555+ private String createEncryptedLibp2pId (byte [] identity , SecretKey encryptionSecretKey ) throws Exception {
556+ byte [] libp2pId ;
557+ String encryptedLibp2pId ;
558+ try {
559+ Log .d ("ReactNative" , "createEncryptedLibp2pId started" );
560+
561+ try {
562+ libp2pId = Fulamobile .generateEd25519KeyFromString (toString (identity ));
563+ } catch (Exception e ) {
564+ Log .d ("ReactNative" , " createEncryptedLibp2pId Failed to generate libp2pId: " + e .getMessage ());
565+ throw new GeneralSecurityException ("createEncryptedLibp2pId Failed to generate libp2pId" , e );
566+ }
567+ encryptedLibp2pId = "FULA_ENC_V4:" + Cryptography .encryptMsg (StaticHelper .bytesToBase64 (libp2pId ), encryptionSecretKey , null );
568+ sharedPref .add (PRIVATE_KEY_STORE_PEERID , encryptedLibp2pId );
569+ return encryptedLibp2pId ;
570+ }
571+ catch (Exception e ) {
572+ Log .d ("ReactNative" , "createEncryptedLibp2pId failed with Error: " + e .getMessage ());
573+ throw new GeneralSecurityException ("createEncryptedLibp2pId Failed to generate libp2pId at hte first level" , e );
538574 }
539575 }
540576
0 commit comments