From bc0455aca7b13dfd3967f818daa2a57a91b3a7de Mon Sep 17 00:00:00 2001 From: Dmitry Borodin Date: Tue, 13 Feb 2024 12:40:54 +0100 Subject: [PATCH 1/2] feat: gc() when seed reference should be no longser in use to decrease probability to find it in memory --- .../io/parity/signer/domain/storage/SeedRepository.kt | 7 +++++++ .../java/io/parity/signer/domain/storage/SeedStorage.kt | 8 +++++--- .../java/io/parity/signer/screens/scan/ScanViewModel.kt | 6 +++++- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/android/src/main/java/io/parity/signer/domain/storage/SeedRepository.kt b/android/src/main/java/io/parity/signer/domain/storage/SeedRepository.kt index 04e889bf2d..3830216b91 100644 --- a/android/src/main/java/io/parity/signer/domain/storage/SeedRepository.kt +++ b/android/src/main/java/io/parity/signer/domain/storage/SeedRepository.kt @@ -134,17 +134,20 @@ class SeedRepository( return when (val authResult = authentication.authenticate(activity)) { AuthResult.AuthSuccess -> { addSeedDangerous(seedName, seedPhrase, networksKeys) + System.gc() AuthOperationResult.Success } AuthResult.AuthError, AuthResult.AuthFailed, AuthResult.AuthUnavailable -> { + System.gc() Timber.w(TAG, "auth error - $authResult") AuthOperationResult.AuthFailed(authResult) } } } catch (e: java.lang.Exception) { + System.gc() Timber.e(TAG, e.toString()) return AuthOperationResult.Error(e) } @@ -161,6 +164,7 @@ class SeedRepository( } catch (e: ErrorDisplayed) { submitErrorState("error in add seed $e") } + System.gc() } /** @@ -200,6 +204,9 @@ class SeedRepository( .filter { it.isNotEmpty() } .joinToString(separator = "\n") + //Just collect some old seeds + System.gc() + return if (seedPhrases.isNotBlank()) { RepoResult.Success(seedPhrases) } else { diff --git a/android/src/main/java/io/parity/signer/domain/storage/SeedStorage.kt b/android/src/main/java/io/parity/signer/domain/storage/SeedStorage.kt index cf1fb945fb..b197707594 100644 --- a/android/src/main/java/io/parity/signer/domain/storage/SeedStorage.kt +++ b/android/src/main/java/io/parity/signer/domain/storage/SeedStorage.kt @@ -13,7 +13,6 @@ import io.parity.signer.domain.FeatureFlags import io.parity.signer.domain.FeatureOption import io.parity.signer.domain.backend.OperationResult import io.parity.signer.screens.error.ErrorStateDestinationState -import io.parity.signer.uniffi.ErrorDisplayed import io.parity.signer.uniffi.historySeedWasShown import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow @@ -145,8 +144,11 @@ class SeedStorage { /** * @throws UserNotAuthenticatedException */ - fun checkIfSeedNameAlreadyExists(seedPhrase: String) = - sharedPreferences.all.values.contains(seedPhrase) + fun checkIfSeedNameAlreadyExists(seedPhrase: String) : Boolean { + val result = sharedPreferences.all.values.contains(seedPhrase) + Runtime.getRuntime().gc() + return result + } /** * @throws UserNotAuthenticatedException diff --git a/android/src/main/java/io/parity/signer/screens/scan/ScanViewModel.kt b/android/src/main/java/io/parity/signer/screens/scan/ScanViewModel.kt index 029c601332..f1a7b4f273 100644 --- a/android/src/main/java/io/parity/signer/screens/scan/ScanViewModel.kt +++ b/android/src/main/java/io/parity/signer/screens/scan/ScanViewModel.kt @@ -233,6 +233,7 @@ class ScanViewModel : ViewModel() { is RepoResult.Success -> { val previewDynDerivations = uniffiInteractor.previewDynamicDerivations(phrases.result, payload) + System.gc() when (previewDynDerivations) { is UniffiResult.Error -> { @@ -266,6 +267,7 @@ class ScanViewModel : ViewModel() { uniffiInteractor.signDynamicDerivationsTransactions( phrases.result, payload ) + System.gc() when (dynDerivations) { is UniffiResult.Error -> { @@ -383,10 +385,12 @@ class ScanViewModel : ViewModel() { } is RepoResult.Success -> { - scanFlowInteractor.continueSigningTransaction( + val result = scanFlowInteractor.continueSigningTransaction( comment, phrases.result, ) + System.gc() + result } } } From 0d891e9d6f46739009864f25b46e389bfce9e9f3 Mon Sep 17 00:00:00 2001 From: Dmitry Borodin Date: Tue, 13 Feb 2024 13:00:50 +0100 Subject: [PATCH 2/2] added gc calls for many other method that are using seed phrase --- .../screens/createderivation/DerivationCreateViewModel.kt | 1 + .../signer/screens/keydetails/KeyDetailsScreenViewModel.kt | 3 +++ .../signer/screens/scan/addnetwork/AddedNetworkViewModel.kt | 1 + .../scan/importderivations/ImportDerivedKeysRepository.kt | 1 + .../screens/settings/networks/signspecs/SignSpecsViewModel.kt | 1 + 5 files changed, 7 insertions(+) diff --git a/android/src/main/java/io/parity/signer/screens/createderivation/DerivationCreateViewModel.kt b/android/src/main/java/io/parity/signer/screens/createderivation/DerivationCreateViewModel.kt index 3271e922b2..c543acf6ab 100644 --- a/android/src/main/java/io/parity/signer/screens/createderivation/DerivationCreateViewModel.kt +++ b/android/src/main/java/io/parity/signer/screens/createderivation/DerivationCreateViewModel.kt @@ -134,6 +134,7 @@ class DerivationCreateViewModel : ViewModel() { path = path.value, networkKey = selectedNetwork.key ) + System.gc() when (result) { is OperationResult.Ok -> { Toast.makeText( diff --git a/android/src/main/java/io/parity/signer/screens/keydetails/KeyDetailsScreenViewModel.kt b/android/src/main/java/io/parity/signer/screens/keydetails/KeyDetailsScreenViewModel.kt index 828b00448d..ee44452de5 100644 --- a/android/src/main/java/io/parity/signer/screens/keydetails/KeyDetailsScreenViewModel.kt +++ b/android/src/main/java/io/parity/signer/screens/keydetails/KeyDetailsScreenViewModel.kt @@ -49,6 +49,7 @@ class KeyDetailsScreenViewModel : ViewModel() { seedPhrase = seedResult.result, keyPassword = password, ) + System.gc() when (secretKeyDetailsQR) { is UniffiResult.Error -> OperationResult.Err(secretKeyDetailsQR.error) is UniffiResult.Success -> { @@ -96,6 +97,8 @@ class KeyDetailsScreenViewModel : ViewModel() { seedPhrase = seedResult.result, keyPassword = password, ) + System.gc() + when (secretKeyDetailsQR) { is UniffiResult.Error -> { if (passwordModel.attempt > 3) { diff --git a/android/src/main/java/io/parity/signer/screens/scan/addnetwork/AddedNetworkViewModel.kt b/android/src/main/java/io/parity/signer/screens/scan/addnetwork/AddedNetworkViewModel.kt index f65559f329..cbd02c1850 100644 --- a/android/src/main/java/io/parity/signer/screens/scan/addnetwork/AddedNetworkViewModel.kt +++ b/android/src/main/java/io/parity/signer/screens/scan/addnetwork/AddedNetworkViewModel.kt @@ -57,6 +57,7 @@ class AddedNetworkViewModel : ViewModel() { seedName = seedPair.first, seedPhrase = seedPair.second, path = network.pathId, network = network.key, ) + System.gc() } catch (e: ErrorDisplayed) { result = false submitErrorState("can't create network key for added network, ${e.message}") diff --git a/android/src/main/java/io/parity/signer/screens/scan/importderivations/ImportDerivedKeysRepository.kt b/android/src/main/java/io/parity/signer/screens/scan/importderivations/ImportDerivedKeysRepository.kt index 20c0e7fa4c..27c4b2ee4c 100644 --- a/android/src/main/java/io/parity/signer/screens/scan/importderivations/ImportDerivedKeysRepository.kt +++ b/android/src/main/java/io/parity/signer/screens/scan/importderivations/ImportDerivedKeysRepository.kt @@ -35,6 +35,7 @@ class ImportDerivedKeysRepository( seedRepository.getAllSeeds().mapError() ?: return RepoResult.Failure() return try { val filledSeedKeys = populateDerivationsHasPwd(seeds, seedPreviews) + System.gc() RepoResult.Success(filledSeedKeys) } catch (e: java.lang.Exception) { RepoResult.Failure(e) diff --git a/android/src/main/java/io/parity/signer/screens/settings/networks/signspecs/SignSpecsViewModel.kt b/android/src/main/java/io/parity/signer/screens/settings/networks/signspecs/SignSpecsViewModel.kt index ea9d9c8e2f..b8297373db 100644 --- a/android/src/main/java/io/parity/signer/screens/settings/networks/signspecs/SignSpecsViewModel.kt +++ b/android/src/main/java/io/parity/signer/screens/settings/networks/signspecs/SignSpecsViewModel.kt @@ -95,6 +95,7 @@ class SignSpecsViewModel : ViewModel() { } } } + System.gc() } } }