From b707b25f83d449567a3ef3b27b9db0fea27a09c4 Mon Sep 17 00:00:00 2001 From: SeniorZhai Date: Mon, 9 Dec 2024 23:16:05 +0800 Subject: [PATCH 1/3] Check entropy --- app/src/main/java/one/mixin/android/tip/Tip.kt | 5 ++++- .../android/ui/tip/CheckRegisterBottomSheetDialogFragment.kt | 5 +++++ app/src/main/java/one/mixin/android/ui/tip/TipFragment.kt | 5 +++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/one/mixin/android/tip/Tip.kt b/app/src/main/java/one/mixin/android/tip/Tip.kt index 899d91a9c5..2d7bcd4dc5 100644 --- a/app/src/main/java/one/mixin/android/tip/Tip.kt +++ b/app/src/main/java/one/mixin/android/tip/Tip.kt @@ -271,9 +271,12 @@ class Tip return salt } - fun getSpendPriv(context: Context, seed: ByteArray): ByteArray { + fun getSpendPriv(context: Context, seed: ByteArray): ByteArray? { var entropy = getMnemonicFromEncryptedPreferences(context) if (entropy == null) { // Register safe must generate mnemonic, Only once + if (Session.getAccount() != null && !Session.hasPhone() && !Session.saltExported()) { + return null + } entropy = generateEntropyAndStore(context) } return getSpendPriv(seed, entropy) diff --git a/app/src/main/java/one/mixin/android/ui/tip/CheckRegisterBottomSheetDialogFragment.kt b/app/src/main/java/one/mixin/android/ui/tip/CheckRegisterBottomSheetDialogFragment.kt index d4bf548b85..4fbbac8fad 100644 --- a/app/src/main/java/one/mixin/android/ui/tip/CheckRegisterBottomSheetDialogFragment.kt +++ b/app/src/main/java/one/mixin/android/ui/tip/CheckRegisterBottomSheetDialogFragment.kt @@ -8,6 +8,7 @@ import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext +import one.mixin.android.MixinApplication import one.mixin.android.R import one.mixin.android.RxBus import one.mixin.android.api.MixinResponse @@ -171,6 +172,10 @@ class CheckRegisterBottomSheetDialogFragment : BiometricBottomSheetDialogFragmen val seed = tip.getOrRecoverTipPriv(requireContext(), pin).getOrThrow() val spendSeed = tip.getSpendPriv(requireContext(), seed) + if (spendSeed == null) { + MixinApplication.get().closeAndClear() + return + } val saltBase64 = tip.getEncryptSalt(requireContext(), pin, seed) val spendKeyPair = newKeyPairFromSeed(spendSeed) val edKey = tip.getMnemonicEdKey(requireContext(), pin, seed) diff --git a/app/src/main/java/one/mixin/android/ui/tip/TipFragment.kt b/app/src/main/java/one/mixin/android/ui/tip/TipFragment.kt index d733243edf..25abdd7438 100644 --- a/app/src/main/java/one/mixin/android/ui/tip/TipFragment.kt +++ b/app/src/main/java/one/mixin/android/ui/tip/TipFragment.kt @@ -16,6 +16,7 @@ import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.launch import one.mixin.android.Constants import one.mixin.android.Constants.INTERVAL_10_MINS +import one.mixin.android.MixinApplication import one.mixin.android.R import one.mixin.android.api.handleMixinResponse import one.mixin.android.api.request.RegisterRequest @@ -538,6 +539,10 @@ class TipFragment : BaseFragment(R.layout.fragment_tip) { return@runCatching false } val spendSeed = tip.getSpendPriv(requireContext(), seed) + if (spendSeed == null) { + MixinApplication.get().closeAndClear() + return@runCatching false + } val saltBase64 = tip.getEncryptSalt(this.requireContext(), pin, seed) val spendKeyPair = newKeyPairFromSeed(spendSeed) val selfAccountId = requireNotNull(Session.getAccountId()) { "self userId can not be null at this step" } From c6ccfe52201d44870656747e43b06f430fbb5323 Mon Sep 17 00:00:00 2001 From: SeniorZhai Date: Mon, 9 Dec 2024 23:27:25 +0800 Subject: [PATCH 2/3] Throw error, commit store --- app/src/main/java/one/mixin/android/crypto/CryptoUtil.kt | 2 +- app/src/main/java/one/mixin/android/tip/Tip.kt | 4 ++-- .../android/ui/tip/CheckRegisterBottomSheetDialogFragment.kt | 4 ---- app/src/main/java/one/mixin/android/ui/tip/TipFragment.kt | 4 ---- 4 files changed, 3 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/one/mixin/android/crypto/CryptoUtil.kt b/app/src/main/java/one/mixin/android/crypto/CryptoUtil.kt index a4d5e665cf..898b13bbca 100644 --- a/app/src/main/java/one/mixin/android/crypto/CryptoUtil.kt +++ b/app/src/main/java/one/mixin/android/crypto/CryptoUtil.kt @@ -311,7 +311,7 @@ fun storeValueInEncryptedPreferences(context: Context, alias: String, entropy: B ) val encodedKey = entropy.toHex() - encryptedPrefs.edit().putString(alias, encodedKey).apply() + encryptedPrefs.edit().putString(alias, encodedKey).commit() } fun removeValueFromEncryptedPreferences(context: Context, alias: String) { diff --git a/app/src/main/java/one/mixin/android/tip/Tip.kt b/app/src/main/java/one/mixin/android/tip/Tip.kt index 2d7bcd4dc5..564e26a712 100644 --- a/app/src/main/java/one/mixin/android/tip/Tip.kt +++ b/app/src/main/java/one/mixin/android/tip/Tip.kt @@ -271,11 +271,11 @@ class Tip return salt } - fun getSpendPriv(context: Context, seed: ByteArray): ByteArray? { + fun getSpendPriv(context: Context, seed: ByteArray): ByteArray { var entropy = getMnemonicFromEncryptedPreferences(context) if (entropy == null) { // Register safe must generate mnemonic, Only once if (Session.getAccount() != null && !Session.hasPhone() && !Session.saltExported()) { - return null + throw IllegalStateException("Entropy lost") } entropy = generateEntropyAndStore(context) } diff --git a/app/src/main/java/one/mixin/android/ui/tip/CheckRegisterBottomSheetDialogFragment.kt b/app/src/main/java/one/mixin/android/ui/tip/CheckRegisterBottomSheetDialogFragment.kt index 4fbbac8fad..61914c7b5a 100644 --- a/app/src/main/java/one/mixin/android/ui/tip/CheckRegisterBottomSheetDialogFragment.kt +++ b/app/src/main/java/one/mixin/android/ui/tip/CheckRegisterBottomSheetDialogFragment.kt @@ -172,10 +172,6 @@ class CheckRegisterBottomSheetDialogFragment : BiometricBottomSheetDialogFragmen val seed = tip.getOrRecoverTipPriv(requireContext(), pin).getOrThrow() val spendSeed = tip.getSpendPriv(requireContext(), seed) - if (spendSeed == null) { - MixinApplication.get().closeAndClear() - return - } val saltBase64 = tip.getEncryptSalt(requireContext(), pin, seed) val spendKeyPair = newKeyPairFromSeed(spendSeed) val edKey = tip.getMnemonicEdKey(requireContext(), pin, seed) diff --git a/app/src/main/java/one/mixin/android/ui/tip/TipFragment.kt b/app/src/main/java/one/mixin/android/ui/tip/TipFragment.kt index 25abdd7438..d82b1923b4 100644 --- a/app/src/main/java/one/mixin/android/ui/tip/TipFragment.kt +++ b/app/src/main/java/one/mixin/android/ui/tip/TipFragment.kt @@ -539,10 +539,6 @@ class TipFragment : BaseFragment(R.layout.fragment_tip) { return@runCatching false } val spendSeed = tip.getSpendPriv(requireContext(), seed) - if (spendSeed == null) { - MixinApplication.get().closeAndClear() - return@runCatching false - } val saltBase64 = tip.getEncryptSalt(this.requireContext(), pin, seed) val spendKeyPair = newKeyPairFromSeed(spendSeed) val selfAccountId = requireNotNull(Session.getAccountId()) { "self userId can not be null at this step" } From 4da8dccb62280c3721ab79fe6ab0b221b158e396 Mon Sep 17 00:00:00 2001 From: SeniorZhai Date: Mon, 9 Dec 2024 23:34:57 +0800 Subject: [PATCH 3/3] Code clean --- .../android/ui/tip/CheckRegisterBottomSheetDialogFragment.kt | 1 - app/src/main/java/one/mixin/android/ui/tip/TipFragment.kt | 1 - 2 files changed, 2 deletions(-) diff --git a/app/src/main/java/one/mixin/android/ui/tip/CheckRegisterBottomSheetDialogFragment.kt b/app/src/main/java/one/mixin/android/ui/tip/CheckRegisterBottomSheetDialogFragment.kt index 61914c7b5a..d4bf548b85 100644 --- a/app/src/main/java/one/mixin/android/ui/tip/CheckRegisterBottomSheetDialogFragment.kt +++ b/app/src/main/java/one/mixin/android/ui/tip/CheckRegisterBottomSheetDialogFragment.kt @@ -8,7 +8,6 @@ import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext -import one.mixin.android.MixinApplication import one.mixin.android.R import one.mixin.android.RxBus import one.mixin.android.api.MixinResponse diff --git a/app/src/main/java/one/mixin/android/ui/tip/TipFragment.kt b/app/src/main/java/one/mixin/android/ui/tip/TipFragment.kt index d82b1923b4..d733243edf 100644 --- a/app/src/main/java/one/mixin/android/ui/tip/TipFragment.kt +++ b/app/src/main/java/one/mixin/android/ui/tip/TipFragment.kt @@ -16,7 +16,6 @@ import dagger.hilt.android.AndroidEntryPoint import kotlinx.coroutines.launch import one.mixin.android.Constants import one.mixin.android.Constants.INTERVAL_10_MINS -import one.mixin.android.MixinApplication import one.mixin.android.R import one.mixin.android.api.handleMixinResponse import one.mixin.android.api.request.RegisterRequest