diff --git a/app/src/main/java/com/tangem/tap/features/customtoken/impl/di/CustomTokenInteractorModule.kt b/app/src/main/java/com/tangem/tap/features/customtoken/impl/di/CustomTokenInteractorModule.kt index 193a65643f..44d4a98284 100644 --- a/app/src/main/java/com/tangem/tap/features/customtoken/impl/di/CustomTokenInteractorModule.kt +++ b/app/src/main/java/com/tangem/tap/features/customtoken/impl/di/CustomTokenInteractorModule.kt @@ -1,7 +1,9 @@ package com.tangem.tap.features.customtoken.impl.di import com.tangem.datasource.api.tangemTech.TangemTechApi +import com.tangem.domain.card.DerivePublicKeysUseCase import com.tangem.domain.wallets.usecase.GetSelectedWalletSyncUseCase +import com.tangem.features.tester.api.TesterFeatureToggles import com.tangem.tap.features.customtoken.impl.data.DefaultCustomTokenRepository import com.tangem.tap.features.customtoken.impl.domain.CustomTokenInteractor import com.tangem.tap.features.customtoken.impl.domain.DefaultCustomTokenInteractor @@ -25,8 +27,10 @@ internal object CustomTokenInteractorModule { fun provideCustomTokenInteractor( tangemTechApi: TangemTechApi, appCoroutineDispatcherProvider: AppCoroutineDispatcherProvider, - getSelectedWalletSyncUseCase: GetSelectedWalletSyncUseCase, reduxStateHolder: AppStateHolder, + getSelectedWalletSyncUseCase: GetSelectedWalletSyncUseCase, + derivePublicKeysUseCase: DerivePublicKeysUseCase, + testerFeatureToggles: TesterFeatureToggles, ): CustomTokenInteractor { return DefaultCustomTokenInteractor( featureRepository = DefaultCustomTokenRepository( @@ -35,6 +39,8 @@ internal object CustomTokenInteractorModule { reduxStateHolder = reduxStateHolder, ), getSelectedWalletSyncUseCase = getSelectedWalletSyncUseCase, + derivePublicKeysUseCase = derivePublicKeysUseCase, + testerFeatureToggles = testerFeatureToggles, ) } } \ No newline at end of file diff --git a/app/src/main/java/com/tangem/tap/features/customtoken/impl/domain/DefaultCustomTokenInteractor.kt b/app/src/main/java/com/tangem/tap/features/customtoken/impl/domain/DefaultCustomTokenInteractor.kt index 2eb39173ce..2d6ad2aebf 100644 --- a/app/src/main/java/com/tangem/tap/features/customtoken/impl/domain/DefaultCustomTokenInteractor.kt +++ b/app/src/main/java/com/tangem/tap/features/customtoken/impl/domain/DefaultCustomTokenInteractor.kt @@ -9,6 +9,7 @@ import com.tangem.common.extensions.ByteArrayKey import com.tangem.common.extensions.toMapKey import com.tangem.crypto.hdWallet.DerivationPath import com.tangem.data.tokens.utils.CryptoCurrencyFactory +import com.tangem.domain.card.DerivePublicKeysUseCase import com.tangem.domain.common.configs.CardConfig import com.tangem.domain.common.extensions.toNetworkId import com.tangem.domain.common.util.derivationStyleProvider @@ -16,8 +17,10 @@ import com.tangem.domain.common.util.hasDerivation import com.tangem.domain.features.addCustomToken.CustomCurrency import com.tangem.domain.models.scan.ScanResponse import com.tangem.domain.tokens.AddCryptoCurrenciesUseCase +import com.tangem.domain.tokens.model.CryptoCurrency import com.tangem.domain.wallets.models.UserWallet import com.tangem.domain.wallets.usecase.GetSelectedWalletSyncUseCase +import com.tangem.features.tester.api.TesterFeatureToggles import com.tangem.operations.derivation.ExtendedPublicKeysMap import com.tangem.tap.common.extensions.dispatchDebugErrorNotification import com.tangem.tap.common.extensions.dispatchOnMain @@ -32,6 +35,7 @@ import com.tangem.tap.tangemSdkManager import com.tangem.tap.userWalletsListManager import com.tangem.utils.extensions.DELAY_SDK_DIALOG_CLOSE import kotlinx.coroutines.delay +import timber.log.Timber /** * Default implementation of custom token interactor @@ -43,6 +47,8 @@ import kotlinx.coroutines.delay class DefaultCustomTokenInteractor( private val featureRepository: CustomTokenRepository, private val getSelectedWalletSyncUseCase: GetSelectedWalletSyncUseCase, + private val derivePublicKeysUseCase: DerivePublicKeysUseCase, + private val testerFeatureToggles: TesterFeatureToggles, ) : CustomTokenInteractor { // TODO: Move to DI @@ -62,19 +68,29 @@ class DefaultCustomTokenInteractor( override suspend fun saveToken(customCurrency: CustomCurrency) { val userWallet = getSelectedWalletSyncUseCase().fold(ifLeft = { return }, ifRight = { it }) - val currency = Currency.fromCustomCurrency(customCurrency) - val isNeedToDerive = isNeedToDerive(userWallet, currency) - if (isNeedToDerive) { - deriveMissingBlockchains( - userWallet = userWallet, - currencyList = listOf(currency), - onSuccess = { submitAdd(userWallet = userWallet.copy(scanResponse = it), currency = currency) }, - ) { - throw it - } + + if (testerFeatureToggles.isDerivePublicKeysRefactoringEnabled) { + val currencies = listOfNotNull(element = currency.toCryptoCurrency(userWallet.scanResponse)) + derivePublicKeysUseCase(userWalletId = userWallet.walletId, currencies = currencies) + .onRight { + addCryptoCurrenciesUseCase(userWalletId = userWallet.walletId, currencies = currencies) + } + .onLeft { Timber.e("Failed to derive public keys: $it") } } else { - submitAdd(userWallet, currency) + // TODO: delete [REDACTED_JIRA] + val isNeedToDerive = isNeedToDerive(userWallet, currency) + if (isNeedToDerive) { + deriveMissingBlockchains( + userWallet = userWallet, + currencyList = listOf(currency), + onSuccess = { submitAdd(userWallet = userWallet.copy(scanResponse = it), currency = currency) }, + ) { + throw it + } + } else { + submitAdd(userWallet, currency) + } } } @@ -83,8 +99,6 @@ class DefaultCustomTokenInteractor( return currency.derivationPath?.let { !scanResponse.hasDerivation(currency.blockchain, it) } ?: false } - @Deprecated("Use DerivePublicKeysUseCase instead") - // FIXME: Migration [REDACTED_JIRA] private suspend fun deriveMissingBlockchains( userWallet: UserWallet, currencyList: List, @@ -176,30 +190,10 @@ class DefaultCustomTokenInteractor( } private suspend fun submitAdd(userWallet: UserWallet, currency: Currency) { - val cryptoCurrencyFactory = CryptoCurrencyFactory() - val scanResponse = userWallet.scanResponse val userWalletId = userWallet.walletId - val currencyList = listOfNotNull( - when (currency) { - is Currency.Blockchain -> { - cryptoCurrencyFactory.createCoin( - blockchain = currency.blockchain, - extraDerivationPath = currency.derivationPath, - derivationStyleProvider = scanResponse.derivationStyleProvider, - ) - } - is Currency.Token -> { - cryptoCurrencyFactory.createToken( - sdkToken = currency.token, - blockchain = currency.blockchain, - extraDerivationPath = currency.derivationPath, - derivationStyleProvider = scanResponse.derivationStyleProvider, - ) - } - }, - ) + val currencyList = listOfNotNull(element = currency.toCryptoCurrency(scanResponse)) userWalletsListManager.update(userWalletId) { it.copy(scanResponse = scanResponse) @@ -207,4 +201,26 @@ class DefaultCustomTokenInteractor( addCryptoCurrenciesUseCase(userWalletId, currencyList) } + + private fun Currency.toCryptoCurrency(scanResponse: ScanResponse): CryptoCurrency? { + val cryptoCurrencyFactory = CryptoCurrencyFactory() + + return when (this) { + is Currency.Blockchain -> { + cryptoCurrencyFactory.createCoin( + blockchain = blockchain, + extraDerivationPath = derivationPath, + derivationStyleProvider = scanResponse.derivationStyleProvider, + ) + } + is Currency.Token -> { + cryptoCurrencyFactory.createToken( + sdkToken = token, + blockchain = blockchain, + extraDerivationPath = derivationPath, + derivationStyleProvider = scanResponse.derivationStyleProvider, + ) + } + } + } } \ No newline at end of file