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 fe1bb63d2a..7071f8c24d 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 @@ -71,9 +71,9 @@ class DefaultCustomTokenInteractor( onSuccess: suspend (ScanResponse) -> Unit, ) { val config = CardConfig.createConfig(scanResponse.card) - val derivationDataList = currencyList.mapNotNull { - val curve = config.primaryCurve(it.blockchain) - curve?.let { getDerivations(curve, scanResponse, currencyList) } + val derivationDataList = currencyList.mapNotNull { currency -> + val curve = config.primaryCurve(currency.blockchain) + curve?.let { getDerivations(curve, scanResponse, currency) } } val derivations = derivationDataList.associate(TokensMiddleware.DerivationData::derivations) @@ -110,24 +110,25 @@ class DefaultCustomTokenInteractor( private fun getDerivations( curve: EllipticCurve, scanResponse: ScanResponse, - currencyList: List, + currency: Currency, ): TokensMiddleware.DerivationData? { val wallet = scanResponse.card.wallets.firstOrNull { it.curve == curve } ?: return null - val manageTokensCandidates = currencyList.map { it.blockchain }.distinct().filter { - it.getSupportedCurves().contains(curve) - }.mapNotNull { - it.derivationPath(scanResponse.derivationStyleProvider.getDerivationStyle()) - } + val supportedCurves = currency.blockchain.getSupportedCurves() + val path = currency.derivationPath + ?.let { + currency.blockchain.derivationPath(scanResponse.derivationStyleProvider.getDerivationStyle()) + } + .takeIf { supportedCurves.contains(curve) } - val customTokensCandidates = currencyList.filter { - it.blockchain.getSupportedCurves().contains(curve) - }.mapNotNull { it.derivationPath }.map { DerivationPath(it) } + val customPath = currency.derivationPath?.let { + DerivationPath(it) + }.takeIf { supportedCurves.contains(curve) } - val bothCandidates = (manageTokensCandidates + customTokensCandidates).distinct().toMutableList() + val bothCandidates = listOfNotNull(path, customPath).distinct().toMutableList() if (bothCandidates.isEmpty()) return null - currencyList.find { it is Currency.Blockchain && it.blockchain == Blockchain.Cardano }?.let { currency -> + if (currency is Currency.Blockchain && currency.blockchain == Blockchain.Cardano) { currency.derivationPath?.let { bothCandidates.add(CardanoUtils.extendedDerivationPath(DerivationPath(it))) } diff --git a/app/src/main/java/com/tangem/tap/features/tokens/impl/domain/DefaultTokensListInteractor.kt b/app/src/main/java/com/tangem/tap/features/tokens/impl/domain/DefaultTokensListInteractor.kt index 9610727483..a70d6379c1 100644 --- a/app/src/main/java/com/tangem/tap/features/tokens/impl/domain/DefaultTokensListInteractor.kt +++ b/app/src/main/java/com/tangem/tap/features/tokens/impl/domain/DefaultTokensListInteractor.kt @@ -136,9 +136,9 @@ internal class DefaultTokensListInteractor( private suspend fun deriveMissingBlockchains(scanResponse: ScanResponse, currencies: List) { val config = CardConfig.createConfig(scanResponse.card) - val derivations = currencies.mapNotNull { - val curve = config.primaryCurve(it.blockchain) - curve?.let { getDerivations(curve, scanResponse, currencies) } + val derivations = currencies.mapNotNull { currency -> + val curve = config.primaryCurve(currency.blockchain) + curve?.let { getDerivations(curve, scanResponse, currency) } }.associate(transform = TokensMiddleware.DerivationData::derivations) if (derivations.isEmpty()) { @@ -176,35 +176,36 @@ internal class DefaultTokensListInteractor( private fun getDerivations( curve: EllipticCurve, scanResponse: ScanResponse, - currencyList: List, + currency: Currency, ): TokensMiddleware.DerivationData? { val wallet = scanResponse.card.wallets.firstOrNull { it.curve == curve } ?: return null - val manageTokensCandidates = currencyList - .map(Currency::blockchain) - .distinct() - .filter { it.getSupportedCurves().contains(curve) } - .mapNotNull { it.derivationPath(scanResponse.derivationStyleProvider.getDerivationStyle()) } + val supportedCurves = currency.blockchain.getSupportedCurves() + val path = currency.derivationPath + ?.let { + currency.blockchain.derivationPath(scanResponse.derivationStyleProvider.getDerivationStyle()) + } + .takeIf { supportedCurves.contains(curve) } - val customTokensCandidates = currencyList - .filter { it.blockchain.getSupportedCurves().contains(curve) } - .mapNotNull(Currency::derivationPath) - .map(::DerivationPath) + val customPath = currency.derivationPath?.let { + DerivationPath(it) + }.takeIf { supportedCurves.contains(curve) } - val bothCandidates = (manageTokensCandidates + customTokensCandidates).distinct().toMutableList() + val bothCandidates = listOfNotNull(path, customPath).distinct().toMutableList() if (bothCandidates.isEmpty()) return null - currencyList.find { it is Currency.Blockchain && it.blockchain == Blockchain.Cardano }?.let { currency -> + if (currency is Currency.Blockchain && currency.blockchain == Blockchain.Cardano) { currency.derivationPath?.let { bothCandidates.add(CardanoUtils.extendedDerivationPath(DerivationPath(it))) } } val mapKeyOfWalletPublicKey = wallet.publicKey.toMapKey() - val alreadyDerivedKeys = scanResponse.derivedKeys[mapKeyOfWalletPublicKey] ?: ExtendedPublicKeysMap(emptyMap()) + val alreadyDerivedKeys: ExtendedPublicKeysMap = + scanResponse.derivedKeys[mapKeyOfWalletPublicKey] ?: ExtendedPublicKeysMap(emptyMap()) val alreadyDerivedPaths = alreadyDerivedKeys.keys.toList() - val toDerive = bothCandidates.filterNot(alreadyDerivedPaths::contains) + val toDerive = bothCandidates.filterNot { alreadyDerivedPaths.contains(it) } if (toDerive.isEmpty()) return null return TokensMiddleware.DerivationData(derivations = mapKeyOfWalletPublicKey to toDerive) diff --git a/app/src/main/java/com/tangem/tap/features/tokens/legacy/redux/TokensMiddleware.kt b/app/src/main/java/com/tangem/tap/features/tokens/legacy/redux/TokensMiddleware.kt index 1a5eb1fd45..70ac55b12f 100644 --- a/app/src/main/java/com/tangem/tap/features/tokens/legacy/redux/TokensMiddleware.kt +++ b/app/src/main/java/com/tangem/tap/features/tokens/legacy/redux/TokensMiddleware.kt @@ -121,9 +121,9 @@ object TokensMiddleware { onSuccess: (ScanResponse) -> Unit, ) { val config = CardConfig.createConfig(scanResponse.card) - val derivationDataList = currencyList.mapNotNull { - val curve = config.primaryCurve(it.blockchain) - curve?.let { getDerivations(curve, scanResponse, currencyList) } + val derivationDataList = currencyList.mapNotNull { currency -> + val curve = config.primaryCurve(currency.blockchain) + curve?.let { getDerivations(curve, scanResponse, currency) } } val derivations = derivationDataList.associate { it.derivations } if (derivations.isEmpty()) { @@ -163,27 +163,24 @@ object TokensMiddleware { } } - private fun getDerivations( - curve: EllipticCurve, - scanResponse: ScanResponse, - currencyList: List, - ): DerivationData? { + private fun getDerivations(curve: EllipticCurve, scanResponse: ScanResponse, currency: Currency): DerivationData? { val wallet = scanResponse.card.wallets.firstOrNull { it.curve == curve } ?: return null - val manageTokensCandidates = currencyList.map { it.blockchain }.distinct().filter { - it.getSupportedCurves().contains(curve) - }.mapNotNull { - it.derivationPath(scanResponse.derivationStyleProvider.getDerivationStyle()) - } + val supportedCurves = currency.blockchain.getSupportedCurves() + val path = currency.derivationPath + ?.let { + currency.blockchain.derivationPath(scanResponse.derivationStyleProvider.getDerivationStyle()) + } + .takeIf { supportedCurves.contains(curve) } - val customTokensCandidates = currencyList.filter { - it.blockchain.getSupportedCurves().contains(curve) - }.mapNotNull { it.derivationPath }.map { DerivationPath(it) } + val customPath = currency.derivationPath?.let { + DerivationPath(it) + }.takeIf { supportedCurves.contains(curve) } - val bothCandidates = (manageTokensCandidates + customTokensCandidates).distinct().toMutableList() + val bothCandidates = listOfNotNull(path, customPath).distinct().toMutableList() if (bothCandidates.isEmpty()) return null - currencyList.find { it is Currency.Blockchain && it.blockchain == Blockchain.Cardano }?.let { currency -> + if (currency is Currency.Blockchain && currency.blockchain == Blockchain.Cardano) { currency.derivationPath?.let { bothCandidates.add(CardanoUtils.extendedDerivationPath(DerivationPath(it))) } diff --git a/app/src/main/java/com/tangem/tap/proxy/DerivationManagerImpl.kt b/app/src/main/java/com/tangem/tap/proxy/DerivationManagerImpl.kt index 174dd3762d..34a6d471b2 100644 --- a/app/src/main/java/com/tangem/tap/proxy/DerivationManagerImpl.kt +++ b/app/src/main/java/com/tangem/tap/proxy/DerivationManagerImpl.kt @@ -26,6 +26,7 @@ import com.tangem.tap.common.extensions.dispatchDebugErrorNotification import com.tangem.tap.common.extensions.dispatchOnMain import com.tangem.tap.common.redux.global.GlobalAction import com.tangem.tap.domain.TapError +import com.tangem.tap.features.tokens.legacy.redux.TokensMiddleware import com.tangem.tap.scope import kotlinx.coroutines.delay import kotlinx.coroutines.launch @@ -97,9 +98,9 @@ class DerivationManagerImpl( onFailure: (Exception) -> Unit, ) { val config = CardConfig.createConfig(scanResponse.card) - val derivationDataList = currencyList.mapNotNull { - val curve = config.primaryCurve(it.blockchain) - curve?.let { getDerivations(curve, scanResponse, currencyList) } + val derivationDataList = currencyList.mapNotNull { currency -> + val curve = config.primaryCurve(currency.blockchain) + curve?.let { getDerivations(curve, scanResponse, currency) } } val derivations = derivationDataList.associate { it.derivations } if (derivations.isEmpty()) { @@ -159,23 +160,32 @@ class DerivationManagerImpl( private fun getDerivations( curve: EllipticCurve, scanResponse: ScanResponse, - currencyList: List, - ): DerivationData? { + currency: com.tangem.tap.features.wallet.models.Currency, + ): TokensMiddleware.DerivationData? { val wallet = scanResponse.card.wallets.firstOrNull { it.curve == curve } ?: return null - val manageTokensCandidates = currencyList.map { it.blockchain }.distinct().filter { - it.getSupportedCurves().contains(curve) - }.mapNotNull { - it.derivationPath(scanResponse.derivationStyleProvider.getDerivationStyle()) - } + val supportedCurves = currency.blockchain.getSupportedCurves() + val path = currency.derivationPath + ?.let { + currency.blockchain.derivationPath(scanResponse.derivationStyleProvider.getDerivationStyle()) + } + .takeIf { supportedCurves.contains(curve) } - val customTokensCandidates = currencyList.filter { - it.blockchain.getSupportedCurves().contains(curve) - }.mapNotNull { it.derivationPath }.map { DerivationPath(it) } + val customPath = currency.derivationPath?.let { + DerivationPath(it) + }.takeIf { supportedCurves.contains(curve) } - val bothCandidates = (manageTokensCandidates + customTokensCandidates).distinct().toMutableList() + val bothCandidates = listOfNotNull(path, customPath).distinct().toMutableList() if (bothCandidates.isEmpty()) return null + if (currency is WalletModelCurrency.Blockchain && + currency.blockchain == Blockchain.Cardano + ) { + currency.derivationPath?.let { + bothCandidates.add(CardanoUtils.extendedDerivationPath(DerivationPath(it))) + } + } + val mapKeyOfWalletPublicKey = wallet.publicKey.toMapKey() val alreadyDerivedKeys: ExtendedPublicKeysMap = scanResponse.derivedKeys[mapKeyOfWalletPublicKey] ?: ExtendedPublicKeysMap(emptyMap()) @@ -184,16 +194,7 @@ class DerivationManagerImpl( val toDerive = bothCandidates.filterNot { alreadyDerivedPaths.contains(it) } if (toDerive.isEmpty()) return null - currencyList.find { it is WalletModelCurrency.Blockchain && it.blockchain == Blockchain.Cardano } - ?.let { currency -> - currency.derivationPath?.let { - bothCandidates.add(CardanoUtils.extendedDerivationPath(DerivationPath(it))) - } - } - - return DerivationData( - derivations = mapKeyOfWalletPublicKey to toDerive, - ) + return TokensMiddleware.DerivationData(derivations = mapKeyOfWalletPublicKey to toDerive) } /**