Skip to content

Commit

Permalink
feat: Android seed gc (#2339)
Browse files Browse the repository at this point in the history
* feat: gc() when seed reference should be no longser in use to decrease probability to find it in memory

* added gc calls for many other method that are using seed phrase
  • Loading branch information
Dmitry-Borodin authored Feb 14, 2024
1 parent 863daf5 commit d6980ac
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -161,6 +164,7 @@ class SeedRepository(
} catch (e: ErrorDisplayed) {
submitErrorState("error in add seed $e")
}
System.gc()
}

/**
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ class DerivationCreateViewModel : ViewModel() {
path = path.value,
networkKey = selectedNetwork.key
)
System.gc()
when (result) {
is OperationResult.Ok -> {
Toast.makeText(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 -> {
Expand Down Expand Up @@ -96,6 +97,8 @@ class KeyDetailsScreenViewModel : ViewModel() {
seedPhrase = seedResult.result,
keyPassword = password,
)
System.gc()

when (secretKeyDetailsQR) {
is UniffiResult.Error -> {
if (passwordModel.attempt > 3) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ class ScanViewModel : ViewModel() {
is RepoResult.Success -> {
val previewDynDerivations =
uniffiInteractor.previewDynamicDerivations(phrases.result, payload)
System.gc()

when (previewDynDerivations) {
is UniffiResult.Error -> {
Expand Down Expand Up @@ -266,6 +267,7 @@ class ScanViewModel : ViewModel() {
uniffiInteractor.signDynamicDerivationsTransactions(
phrases.result, payload
)
System.gc()

when (dynDerivations) {
is UniffiResult.Error -> {
Expand Down Expand Up @@ -383,10 +385,12 @@ class ScanViewModel : ViewModel() {
}

is RepoResult.Success -> {
scanFlowInteractor.continueSigningTransaction(
val result = scanFlowInteractor.continueSigningTransaction(
comment,
phrases.result,
)
System.gc()
result
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class SignSpecsViewModel : ViewModel() {
}
}
}
System.gc()
}
}
}
Expand Down

0 comments on commit d6980ac

Please sign in to comment.