From 4fa8ad0f9ba9b4eea9b8c014d64e7c7816dccfe5 Mon Sep 17 00:00:00 2001 From: Tangem Date: Sun, 17 Apr 2022 16:03:51 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../tap/features/wallet/redux/WalletState.kt | 24 ++++++++++++++ .../java/com/tangem/domain/DomainWrapped.kt | 33 +++++++++++++++---- .../com/tangem/domain/common/ScanResponse.kt | 31 +++++++++++++++++ .../com/tangem/domain/redux/ReStoreHub.kt | 4 +++ 4 files changed, 86 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletState.kt b/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletState.kt index abecd0f54a..6c592e7152 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletState.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletState.kt @@ -6,6 +6,7 @@ import com.tangem.blockchain.common.address.AddressType import com.tangem.blockchain.extensions.isAboveZero import com.tangem.common.extensions.isZero import com.tangem.domain.common.TapWorkarounds.derivationStyle +import com.tangem.domain.features.addCustomToken.CustomCurrency import com.tangem.tap.common.entities.Button import com.tangem.tap.common.extensions.toQrCode import com.tangem.tap.common.redux.StateDialog @@ -17,6 +18,7 @@ import com.tangem.tap.domain.extensions.sellIsAllowed import com.tangem.tap.domain.extensions.toSendableAmounts import com.tangem.tap.domain.tokens.BlockchainNetwork import com.tangem.tap.features.onboarding.products.twins.redux.TwinCardsState +import com.tangem.tap.features.tokens.redux.TokenWithBlockchain import com.tangem.tap.features.wallet.models.PendingTransaction import com.tangem.tap.features.wallet.models.toPendingTransactions import com.tangem.tap.features.wallet.models.toPendingTransactionsForToken @@ -439,6 +441,28 @@ sealed interface Currency { ) } } + + fun fromCustomCurrency(customCurrency: CustomCurrency): Currency { + return when (customCurrency) { + is CustomCurrency.CustomBlockchain -> Blockchain( + blockchain = customCurrency.network, + derivationPath = customCurrency.derivationPath?.rawPath + ) + is CustomCurrency.CustomToken -> Token( + token = customCurrency.token, + blockchain = customCurrency.network, + derivationPath = customCurrency.derivationPath?.rawPath, + ) + } + } + + fun fromTokenWithBlockchain(tokenWithBlockchain: TokenWithBlockchain): Token { + return Currency.Token( + token = tokenWithBlockchain.token, + blockchain = tokenWithBlockchain.blockchain, + derivationPath = null + ) + } } } diff --git a/domain/src/main/java/com/tangem/domain/DomainWrapped.kt b/domain/src/main/java/com/tangem/domain/DomainWrapped.kt index 41ab1c8569..3872eeaab1 100644 --- a/domain/src/main/java/com/tangem/domain/DomainWrapped.kt +++ b/domain/src/main/java/com/tangem/domain/DomainWrapped.kt @@ -1,7 +1,6 @@ package com.tangem.domain -import com.tangem.blockchain.common.Blockchain -import com.tangem.blockchain.common.Token +import com.tangem.blockchain.common.DerivationStyle /** [REDACTED_AUTHOR] @@ -11,8 +10,30 @@ import com.tangem.blockchain.common.Token // to appropriate parts of module sealed interface DomainWrapped { - data class TokenWithBlockchain( - val token: Token, - val blockchain: Blockchain - ) + // Mirror reflection ot the com.tangem.tap.features.wallet.redux.Currency + sealed interface Currency { + val blockchain: com.tangem.blockchain.common.Blockchain + val currencySymbol: String + val derivationPath: String? + + data class Token( + val token: com.tangem.blockchain.common.Token, + override val blockchain: com.tangem.blockchain.common.Blockchain, + override val derivationPath: String? + ) : Currency { + override val currencySymbol = token.symbol + } + + data class Blockchain( + override val blockchain: com.tangem.blockchain.common.Blockchain, + override val derivationPath: String? + ) : Currency { + override val currencySymbol: String = blockchain.currency + } + + fun isCustomCurrency(derivationStyle: DerivationStyle?): Boolean { + if (derivationPath == null || derivationStyle == null) return false + return derivationPath != blockchain.derivationPath(derivationStyle)?.rawPath + } + } } \ No newline at end of file diff --git a/domain/src/main/java/com/tangem/domain/common/ScanResponse.kt b/domain/src/main/java/com/tangem/domain/common/ScanResponse.kt index b41eb4f116..35ec0dbe8b 100644 --- a/domain/src/main/java/com/tangem/domain/common/ScanResponse.kt +++ b/domain/src/main/java/com/tangem/domain/common/ScanResponse.kt @@ -3,9 +3,13 @@ package com.tangem.domain.common import com.tangem.blockchain.common.Blockchain import com.tangem.blockchain.common.Token import com.tangem.common.card.Card +import com.tangem.common.card.EllipticCurve import com.tangem.common.card.WalletData import com.tangem.common.extensions.ByteArrayKey +import com.tangem.common.extensions.toMapKey +import com.tangem.common.hdWallet.DerivationPath import com.tangem.domain.common.TapWorkarounds.getTangemNoteBlockchain +import com.tangem.domain.common.TapWorkarounds.isTestCard import com.tangem.operations.CommandResponse import com.tangem.operations.backup.PrimaryCard import com.tangem.operations.derivation.ExtendedPublicKeysMap @@ -48,6 +52,33 @@ data class ScanResponse( fun twinsIsTwinned(): Boolean = card.isTangemTwins() && walletData != null && secondTwinPublicKey != null + + fun hasDerivation(blockchain: Blockchain, rawDerivationPath: String): Boolean { + return hasDerivation(blockchain, DerivationPath(rawDerivationPath)) + } + + fun hasDerivation(blockchain: Blockchain, derivationPath: DerivationPath): Boolean { + val isTestnet = card.isTestCard || blockchain.isTestnet() + return when { + Blockchain.secp256k1Blockchains(isTestnet).contains(blockchain) -> { + hasDerivation(EllipticCurve.Secp256k1, derivationPath) + } + Blockchain.ed25519OnlyBlockchains(isTestnet).contains(blockchain) -> { + hasDerivation(EllipticCurve.Ed25519, derivationPath) + } + else -> false + } + } + + fun hasDerivation(curve: EllipticCurve, derivationPath: DerivationPath): Boolean { + val foundWallet = card.wallets.firstOrNull { it.curve == curve } + ?: return false + + val extendedPublicKeysMap = derivedKeys[foundWallet.publicKey.toMapKey()] ?: return false + + val extendedPublicKey = extendedPublicKeysMap[derivationPath] + return extendedPublicKey != null + } } enum class ProductType { diff --git a/domain/src/main/java/com/tangem/domain/redux/ReStoreHub.kt b/domain/src/main/java/com/tangem/domain/redux/ReStoreHub.kt index 9de3cedcf3..1c8bd9314c 100644 --- a/domain/src/main/java/com/tangem/domain/redux/ReStoreHub.kt +++ b/domain/src/main/java/com/tangem/domain/redux/ReStoreHub.kt @@ -97,6 +97,10 @@ internal abstract class BaseStoreHub( } } + protected fun cancelAll() { + actionsAndJobs.forEach { (_, job) -> job.cancel() } + } + protected abstract suspend fun handleAction(action: Action, storeState: DomainState, cancel: ValueCallback) protected abstract fun reduceAction(action: Action, state: State): State