From 2d79953060e235aecc09e47d4deab9175778aff5 Mon Sep 17 00:00:00 2001 From: Tangem Date: Mon, 15 Sep 2025 12:56:45 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../DefaultUserWalletsListRepository.kt | 44 ++++++++++--------- .../core/wallets/UserWalletsListRepository.kt | 9 ++-- .../CreateWalletSelectionModel.kt | 11 ++++- .../start/AddExistingWalletStartModel.kt | 9 +++- .../intents/WalletWarningsClickIntents.kt | 2 +- .../welcome/impl/model/WelcomeModel.kt | 2 +- 6 files changed, 48 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/com/tangem/tap/domain/userWalletList/repository/DefaultUserWalletsListRepository.kt b/app/src/main/java/com/tangem/tap/domain/userWalletList/repository/DefaultUserWalletsListRepository.kt index fd30adec16..c4a0b716f7 100644 --- a/app/src/main/java/com/tangem/tap/domain/userWalletList/repository/DefaultUserWalletsListRepository.kt +++ b/app/src/main/java/com/tangem/tap/domain/userWalletList/repository/DefaultUserWalletsListRepository.kt @@ -4,6 +4,7 @@ import arrow.core.Either import arrow.core.left import arrow.core.raise.either import arrow.core.right +import com.tangem.common.CompletionResult import com.tangem.common.doOnFailure import com.tangem.common.doOnSuccess import com.tangem.common.flatMap @@ -210,6 +211,7 @@ internal class DefaultUserWalletsListRepository( } } + @Suppress("CyclomaticComplexMethod") override suspend fun unlock( userWalletId: UserWalletId, unlockMethod: UserWalletsListRepository.UnlockMethod, @@ -257,31 +259,33 @@ internal class DefaultUserWalletsListRepository( raise(UnlockWalletError.UnableToUnlock) } } - UserWalletsListRepository.UnlockMethod.Scan -> { + is UserWalletsListRepository.UnlockMethod.Scan -> { if (userWallet !is UserWallet.Cold) { raise(UnlockWalletError.UnableToUnlock) } - tangemSdkManagerProvider().scanProduct() - .doOnSuccess { scanResponse -> - val expectedId = UserWalletIdBuilder.scanResponse(scanResponse).build() - - if (expectedId != userWallet.walletId) { - raise(UnlockWalletError.ScannedCardWalletNotMatched) - } - - val encryptionKey = UserWalletEncryptionKey( - walletId = userWallet.walletId, - encryptionKey = scanResponse.encryptionKey ?: raise(UnlockWalletError.UnableToUnlock), - ) - - sensitiveInformationRepository.getAll(listOf(encryptionKey)) - .doOnSuccess { sensitiveInfo -> updateWallets { it?.updateWith(sensitiveInfo) } } - .doOnFailure { error -> raise(UnlockWalletError.UnableToUnlock) } - } - .doOnFailure { - raise(UnlockWalletError.UserCancelled) + val scanResponse = unlockMethod.scanResponse ?: run { + val res = tangemSdkManagerProvider().scanProduct() + when (res) { + is CompletionResult.Failure -> raise(UnlockWalletError.UserCancelled) + is CompletionResult.Success -> res.data } + } + + val expectedId = UserWalletIdBuilder.scanResponse(scanResponse).build() + + if (expectedId != userWallet.walletId) { + raise(UnlockWalletError.ScannedCardWalletNotMatched) + } + + val encryptionKey = UserWalletEncryptionKey( + walletId = userWallet.walletId, + encryptionKey = scanResponse.encryptionKey ?: raise(UnlockWalletError.UnableToUnlock), + ) + + sensitiveInformationRepository.getAll(listOf(encryptionKey)) + .doOnSuccess { sensitiveInfo -> updateWallets { it?.updateWith(sensitiveInfo) } } + .doOnFailure { error -> raise(UnlockWalletError.UnableToUnlock) } } } } diff --git a/domain/core/src/main/kotlin/com/tangem/domain/core/wallets/UserWalletsListRepository.kt b/domain/core/src/main/kotlin/com/tangem/domain/core/wallets/UserWalletsListRepository.kt index fbcfeb9a0c..c3704524c2 100644 --- a/domain/core/src/main/kotlin/com/tangem/domain/core/wallets/UserWalletsListRepository.kt +++ b/domain/core/src/main/kotlin/com/tangem/domain/core/wallets/UserWalletsListRepository.kt @@ -7,6 +7,7 @@ import com.tangem.domain.core.wallets.error.SaveWalletError import com.tangem.domain.core.wallets.error.SelectWalletError import com.tangem.domain.core.wallets.error.SetLockError import com.tangem.domain.core.wallets.error.UnlockWalletError +import com.tangem.domain.models.scan.ScanResponse import com.tangem.domain.models.wallet.UserWallet import com.tangem.domain.models.wallet.UserWalletId import kotlinx.coroutines.flow.StateFlow @@ -133,10 +134,10 @@ interface UserWalletsListRepository { data object NoLock : LockMethod() } - enum class UnlockMethod { - Biometric, - AccessCode, - Scan, + sealed class UnlockMethod { + data object Biometric : UnlockMethod() + data object AccessCode : UnlockMethod() + data class Scan(val scanResponse: ScanResponse? = null) : UnlockMethod() } } diff --git a/features/create-wallet-selection/impl/src/main/kotlin/com/tangem/features/createwalletselection/CreateWalletSelectionModel.kt b/features/create-wallet-selection/impl/src/main/kotlin/com/tangem/features/createwalletselection/CreateWalletSelectionModel.kt index 26ce10e204..d5c27dd004 100644 --- a/features/create-wallet-selection/impl/src/main/kotlin/com/tangem/features/createwalletselection/CreateWalletSelectionModel.kt +++ b/features/create-wallet-selection/impl/src/main/kotlin/com/tangem/features/createwalletselection/CreateWalletSelectionModel.kt @@ -140,13 +140,20 @@ internal class CreateWalletSelectionModel @Inject constructor( return } - saveWalletUseCase(userWallet).fold( + saveWalletUseCase(userWallet = userWallet).fold( ifLeft = { delay(HIDE_PROGRESS_DELAY) setLoading(false) when (it) { is SaveWalletError.DataError -> Timber.e(it.toString(), "Unable to save user wallet") - is SaveWalletError.WalletAlreadySaved -> appRouter.replaceAll(AppRoute.Wallet) + is SaveWalletError.WalletAlreadySaved -> { + userWalletsListRepository.unlock( + userWalletId = userWallet.walletId, + unlockMethod = UserWalletsListRepository.UnlockMethod.Scan(scanResponse), + ).onRight { + appRouter.replaceAll(AppRoute.Wallet) + } + } } }, ifRight = { diff --git a/features/hot-wallet/impl/src/main/kotlin/com/tangem/features/hotwallet/addexistingwallet/start/AddExistingWalletStartModel.kt b/features/hot-wallet/impl/src/main/kotlin/com/tangem/features/hotwallet/addexistingwallet/start/AddExistingWalletStartModel.kt index e39dd4adbb..e8d5c52060 100644 --- a/features/hot-wallet/impl/src/main/kotlin/com/tangem/features/hotwallet/addexistingwallet/start/AddExistingWalletStartModel.kt +++ b/features/hot-wallet/impl/src/main/kotlin/com/tangem/features/hotwallet/addexistingwallet/start/AddExistingWalletStartModel.kt @@ -146,7 +146,14 @@ internal class AddExistingWalletStartModel @Inject constructor( setLoading(false) when (it) { is SaveWalletError.DataError -> Timber.e(it.toString(), "Unable to save user wallet") - is SaveWalletError.WalletAlreadySaved -> appRouter.replaceAll(AppRoute.Wallet) + is SaveWalletError.WalletAlreadySaved -> { + userWalletsListRepository.unlock( + userWalletId = userWallet.walletId, + unlockMethod = UserWalletsListRepository.UnlockMethod.Scan(scanResponse), + ).onRight { + appRouter.replaceAll(AppRoute.Wallet) + } + } } }, ifRight = { diff --git a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/child/wallet/model/intents/WalletWarningsClickIntents.kt b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/child/wallet/model/intents/WalletWarningsClickIntents.kt index 203128c7ce..55390d5b9e 100644 --- a/features/wallet/impl/src/main/java/com/tangem/feature/wallet/child/wallet/model/intents/WalletWarningsClickIntents.kt +++ b/features/wallet/impl/src/main/java/com/tangem/feature/wallet/child/wallet/model/intents/WalletWarningsClickIntents.kt @@ -178,7 +178,7 @@ internal class WalletWarningsClickIntentsImplementor @Inject constructor( .onLeft { val selectedUserWallet = getSelectedUserWallet() ?: return@onLeft val method = when (selectedUserWallet) { - is UserWallet.Cold -> UserWalletsListRepository.UnlockMethod.Scan + is UserWallet.Cold -> UserWalletsListRepository.UnlockMethod.Scan() is UserWallet.Hot -> UserWalletsListRepository.UnlockMethod.AccessCode } userWalletsListRepository.unlock(stateHolder.getSelectedWalletId(), method) diff --git a/features/welcome/impl/src/main/kotlin/com/tangem/features/welcome/impl/model/WelcomeModel.kt b/features/welcome/impl/src/main/kotlin/com/tangem/features/welcome/impl/model/WelcomeModel.kt index d1128d02f2..4b7bdf5496 100644 --- a/features/welcome/impl/src/main/kotlin/com/tangem/features/welcome/impl/model/WelcomeModel.kt +++ b/features/welcome/impl/src/main/kotlin/com/tangem/features/welcome/impl/model/WelcomeModel.kt @@ -208,7 +208,7 @@ internal class WelcomeModel @Inject constructor( } val unlockMethod = when (userWallet) { - is UserWallet.Cold -> UserWalletsListRepository.UnlockMethod.Scan + is UserWallet.Cold -> UserWalletsListRepository.UnlockMethod.Scan() is UserWallet.Hot -> { uiState.value = WelcomeUM.Empty UserWalletsListRepository.UnlockMethod.AccessCode