From 5cd0d0b878f23a06c498f9523a4c7c1442ad17c6 Mon Sep 17 00:00:00 2001 From: Tangem Date: Fri, 30 Aug 2024 12:17:42 +0400 Subject: [PATCH] Updated on 2026-08-14 --- .../tap/di/domain/ManageTokensDomainModule.kt | 18 +++++++++-- .../DefaultCustomTokensRepository.kt | 30 +++++++++++++++++++ .../GetSupportedNetworksUseCase.kt | 27 +++++++++++++++++ .../ValidateDerivationPathUseCase.kt | 24 +++++++++++++++ .../DerivationPathValidationException.kt | 8 +++++ .../exceptoin/SupportedBlockchainException.kt | 8 +++++ .../repository/CustomTokensRepository.kt | 4 +++ 7 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 domain/manage-tokens/src/main/kotlin/com/tangem/domain/managetokens/GetSupportedNetworksUseCase.kt create mode 100644 domain/manage-tokens/src/main/kotlin/com/tangem/domain/managetokens/ValidateDerivationPathUseCase.kt create mode 100644 domain/manage-tokens/src/main/kotlin/com/tangem/domain/managetokens/model/exceptoin/DerivationPathValidationException.kt create mode 100644 domain/manage-tokens/src/main/kotlin/com/tangem/domain/managetokens/model/exceptoin/SupportedBlockchainException.kt diff --git a/app/src/main/java/com/tangem/tap/di/domain/ManageTokensDomainModule.kt b/app/src/main/java/com/tangem/tap/di/domain/ManageTokensDomainModule.kt index 576da07163..02bb0f9562 100644 --- a/app/src/main/java/com/tangem/tap/di/domain/ManageTokensDomainModule.kt +++ b/app/src/main/java/com/tangem/tap/di/domain/ManageTokensDomainModule.kt @@ -1,8 +1,6 @@ package com.tangem.tap.di.domain import com.tangem.domain.card.repository.DerivationsRepository -import com.tangem.domain.managetokens.SaveManagedTokensUseCase -import com.tangem.domain.managetokens.GetManagedTokensUseCase import com.tangem.domain.managetokens.* import com.tangem.domain.managetokens.repository.CustomTokensRepository import com.tangem.domain.managetokens.repository.ManageTokensRepository @@ -67,4 +65,20 @@ internal object ManageTokensDomainModule { derivationsRepository = derivationsRepository, ) } + + @Provides + @Singleton + fun provideGetSupportedNetworksUseCase( + customTokensRepository: CustomTokensRepository, + ): GetSupportedNetworksUseCase { + return GetSupportedNetworksUseCase(customTokensRepository) + } + + @Provides + @Singleton + fun provideValidateDerivationPathUseCase( + customTokensRepository: CustomTokensRepository, + ): ValidateDerivationPathUseCase { + return ValidateDerivationPathUseCase(customTokensRepository) + } } \ No newline at end of file diff --git a/data/manage-tokens/src/main/kotlin/com/tangem/data/managetokens/DefaultCustomTokensRepository.kt b/data/manage-tokens/src/main/kotlin/com/tangem/data/managetokens/DefaultCustomTokensRepository.kt index 0ec95b565f..752cef4833 100644 --- a/data/manage-tokens/src/main/kotlin/com/tangem/data/managetokens/DefaultCustomTokensRepository.kt +++ b/data/manage-tokens/src/main/kotlin/com/tangem/data/managetokens/DefaultCustomTokensRepository.kt @@ -2,6 +2,7 @@ package com.tangem.data.managetokens import com.tangem.blockchain.common.Blockchain import com.tangem.blockchainsdk.utils.toNetworkId +import com.tangem.crypto.hdWallet.DerivationPath import com.tangem.data.common.api.safeApiCall import com.tangem.data.common.currency.CryptoCurrencyFactory import com.tangem.data.common.currency.UserTokensResponseFactory @@ -16,8 +17,10 @@ import com.tangem.datasource.local.preferences.PreferencesKeys import com.tangem.datasource.local.preferences.utils.getObjectSyncOrNull import com.tangem.datasource.local.preferences.utils.storeObject import com.tangem.datasource.local.userwallet.UserWalletsStore +import com.tangem.domain.common.extensions.canHandleBlockchain import com.tangem.domain.common.extensions.supportedBlockchains import com.tangem.domain.common.util.cardTypesResolver +import com.tangem.domain.common.util.derivationStyleProvider import com.tangem.domain.managetokens.model.AddCustomTokenForm import com.tangem.domain.managetokens.model.ManagedCryptoCurrency import com.tangem.domain.managetokens.repository.CustomTokensRepository @@ -178,6 +181,33 @@ internal class DefaultCustomTokensRepository( } } + override suspend fun getSupportedNetworks(userWalletId: UserWalletId): List = withContext(dispatchers.io) { + val userWallet = userWalletsStore.getSyncOrNull(userWalletId) + ?: error("User wallet not found") + val scanResponse = userWallet.scanResponse + + Blockchain.entries + .mapNotNull { blockchain -> + if (scanResponse.card.canHandleBlockchain(blockchain, scanResponse.cardTypesResolver)) { + getNetwork( + blockchain = blockchain, + extraDerivationPath = null, + derivationStyleProvider = scanResponse.derivationStyleProvider, + ) + } else { + null + } + } + } + + override fun createDerivationPath(rawPath: String): Network.DerivationPath { + val sdkPath = DerivationPath(rawPath) + + return Network.DerivationPath.Custom( + value = sdkPath.rawPath, + ) + } + private suspend fun storeAndPushTokens(userWalletId: UserWalletId, response: UserTokensResponse) { val compatibleUserTokensResponse = userTokensBackwardCompatibility.applyCompatibilityAndGetUpdated(response) appPreferencesStore.storeObject( diff --git a/domain/manage-tokens/src/main/kotlin/com/tangem/domain/managetokens/GetSupportedNetworksUseCase.kt b/domain/manage-tokens/src/main/kotlin/com/tangem/domain/managetokens/GetSupportedNetworksUseCase.kt new file mode 100644 index 0000000000..1074a21b02 --- /dev/null +++ b/domain/manage-tokens/src/main/kotlin/com/tangem/domain/managetokens/GetSupportedNetworksUseCase.kt @@ -0,0 +1,27 @@ +package com.tangem.domain.managetokens + +import arrow.core.Either +import arrow.core.raise.catch +import arrow.core.raise.either +import arrow.core.raise.ensureNotNull +import com.tangem.domain.managetokens.model.exceptoin.SupportedBlockchainException +import com.tangem.domain.managetokens.repository.CustomTokensRepository +import com.tangem.domain.tokens.model.Network +import com.tangem.domain.wallets.models.UserWalletId + +class GetSupportedNetworksUseCase( + private val repository: CustomTokensRepository, +) { + + suspend operator fun invoke(userWalletId: UserWalletId): Either> { + return either { + val networks = catch({ repository.getSupportedNetworks(userWalletId) }) { + raise(SupportedBlockchainException.DataError(it)) + } + + ensureNotNull(networks.takeIf { it.isNotEmpty() }) { + SupportedBlockchainException.EmptyList + } + } + } +} \ No newline at end of file diff --git a/domain/manage-tokens/src/main/kotlin/com/tangem/domain/managetokens/ValidateDerivationPathUseCase.kt b/domain/manage-tokens/src/main/kotlin/com/tangem/domain/managetokens/ValidateDerivationPathUseCase.kt new file mode 100644 index 0000000000..deaa42edf9 --- /dev/null +++ b/domain/manage-tokens/src/main/kotlin/com/tangem/domain/managetokens/ValidateDerivationPathUseCase.kt @@ -0,0 +1,24 @@ +package com.tangem.domain.managetokens + +import arrow.core.Either +import arrow.core.raise.catch +import arrow.core.raise.either +import arrow.core.raise.ensure +import com.tangem.domain.managetokens.model.exceptoin.DerivationPathValidationException +import com.tangem.domain.managetokens.repository.CustomTokensRepository +import com.tangem.domain.tokens.model.Network + +class ValidateDerivationPathUseCase( + private val repository: CustomTokensRepository, +) { + + operator fun invoke(rawValue: String): Either { + return either { + ensure(rawValue.isNotEmpty()) { DerivationPathValidationException.Empty } + + catch({ repository.createDerivationPath(rawValue) }) { + raise(DerivationPathValidationException.Invalid) + } + } + } +} \ No newline at end of file diff --git a/domain/manage-tokens/src/main/kotlin/com/tangem/domain/managetokens/model/exceptoin/DerivationPathValidationException.kt b/domain/manage-tokens/src/main/kotlin/com/tangem/domain/managetokens/model/exceptoin/DerivationPathValidationException.kt new file mode 100644 index 0000000000..fdda23776c --- /dev/null +++ b/domain/manage-tokens/src/main/kotlin/com/tangem/domain/managetokens/model/exceptoin/DerivationPathValidationException.kt @@ -0,0 +1,8 @@ +package com.tangem.domain.managetokens.model.exceptoin + +sealed class DerivationPathValidationException { + + data object Empty : DerivationPathValidationException() + + data object Invalid : DerivationPathValidationException() +} \ No newline at end of file diff --git a/domain/manage-tokens/src/main/kotlin/com/tangem/domain/managetokens/model/exceptoin/SupportedBlockchainException.kt b/domain/manage-tokens/src/main/kotlin/com/tangem/domain/managetokens/model/exceptoin/SupportedBlockchainException.kt new file mode 100644 index 0000000000..946e3cd9b4 --- /dev/null +++ b/domain/manage-tokens/src/main/kotlin/com/tangem/domain/managetokens/model/exceptoin/SupportedBlockchainException.kt @@ -0,0 +1,8 @@ +package com.tangem.domain.managetokens.model.exceptoin + +sealed class SupportedBlockchainException { + + data object EmptyList : SupportedBlockchainException() + + data class DataError(val cause: Throwable) : SupportedBlockchainException() +} \ No newline at end of file diff --git a/domain/manage-tokens/src/main/kotlin/com/tangem/domain/managetokens/repository/CustomTokensRepository.kt b/domain/manage-tokens/src/main/kotlin/com/tangem/domain/managetokens/repository/CustomTokensRepository.kt index 5f84209200..c0f898207e 100644 --- a/domain/manage-tokens/src/main/kotlin/com/tangem/domain/managetokens/repository/CustomTokensRepository.kt +++ b/domain/manage-tokens/src/main/kotlin/com/tangem/domain/managetokens/repository/CustomTokensRepository.kt @@ -33,4 +33,8 @@ interface CustomTokensRepository { ): CryptoCurrency.Token suspend fun removeCurrency(userWalletId: UserWalletId, currency: ManagedCryptoCurrency.Custom) + + suspend fun getSupportedNetworks(userWalletId: UserWalletId): List + + fun createDerivationPath(rawPath: String): Network.DerivationPath } \ No newline at end of file