diff --git a/app/src/main/java/com/tangem/tap/domain/extensions/Blockchain.kt b/app/src/main/java/com/tangem/tap/domain/extensions/Blockchain.kt index 7d544aa89f..6baaf8f668 100644 --- a/app/src/main/java/com/tangem/tap/domain/extensions/Blockchain.kt +++ b/app/src/main/java/com/tangem/tap/domain/extensions/Blockchain.kt @@ -29,14 +29,15 @@ fun Blockchain.minimalAmount(): BigDecimal { return 1.toBigDecimal().movePointLeft(decimals()) } -fun Blockchain.getCurve(): EllipticCurve? { +fun Blockchain.getSupportedCurves(): List? { return when (this) { Blockchain.Unknown -> null Blockchain.Bitcoin, Blockchain.BitcoinTestnet, Blockchain.BitcoinCash, Blockchain.Litecoin, Blockchain.Ducatus, Blockchain.Ethereum, Blockchain.EthereumTestnet, Blockchain.RSK, - Blockchain.XRP, Blockchain.Binance, Blockchain.BinanceTestnet -> EllipticCurve.Secp256k1 - Blockchain.Tezos, Blockchain.Cardano, Blockchain.CardanoShelley, Blockchain.Stellar -> - EllipticCurve.Ed25519 + Blockchain.XRP, Blockchain.Binance, Blockchain.BinanceTestnet -> listOf(EllipticCurve.Secp256k1) + Blockchain.Tezos -> listOf(EllipticCurve.Secp256k1, EllipticCurve.Ed25519) + Blockchain.Cardano, Blockchain.CardanoShelley, Blockchain.Stellar -> + listOf(EllipticCurve.Ed25519) } } diff --git a/app/src/main/java/com/tangem/tap/domain/extensions/WalletManagerFactory.kt b/app/src/main/java/com/tangem/tap/domain/extensions/WalletManagerFactory.kt index fc847ae987..f09492cccd 100644 --- a/app/src/main/java/com/tangem/tap/domain/extensions/WalletManagerFactory.kt +++ b/app/src/main/java/com/tangem/tap/domain/extensions/WalletManagerFactory.kt @@ -5,29 +5,30 @@ import com.tangem.blockchain.common.WalletManager import com.tangem.blockchain.common.WalletManagerFactory import com.tangem.commands.common.card.Card import com.tangem.commands.common.card.EllipticCurve +import com.tangem.commands.wallet.CardWallet -fun WalletManagerFactory.makeWalletManagerForApp(card: Card, blockchain: Blockchain): WalletManager? { - val curve = blockchain.getCurve() ?: return null - val publicKey = card.getWallets().firstOrNull { it.curve == curve }?.publicKey ?: return null - return makeWalletManager(card.cardId, publicKey, blockchain, curve) +fun WalletManagerFactory.makeWalletManagerForApp( + card: Card, + blockchain: Blockchain, +): WalletManager? { + val supportedCurves = blockchain.getSupportedCurves() ?: return null + val wallets = card.getWallets().filter { wallet -> supportedCurves.contains(wallet.curve) } + val wallet = selectWallet(wallets) + val publicKey = wallet?.publicKey ?: return null + val curveToUse = wallet.curve ?: return null + return makeWalletManager(card.cardId, publicKey, blockchain, curveToUse) +} + +private fun selectWallet(wallets: List): CardWallet? { + return when (wallets.size) { + 0 -> null + 1 -> wallets[0] + else -> wallets.firstOrNull { it.curve == EllipticCurve.Secp256k1 } ?: wallets[0] + } } fun WalletManagerFactory.makeWalletManagersForApp( - card: Card, blockchains: List + card: Card, blockchains: List, ): List { - return makeWalletManagersForCurve(card, blockchains, EllipticCurve.Secp256k1) + - makeWalletManagersForCurve(card, blockchains, EllipticCurve.Ed25519) + return blockchains.mapNotNull { blockchain -> makeWalletManagerForApp(card, blockchain) } } - -fun WalletManagerFactory.makeWalletManagersForCurve( - card: Card, blockchains: List, curve: EllipticCurve -): List { - val blockchainsForCurve = blockchains.filter { it.getCurve() == curve } - val walletPublicKey = card.getWallets().firstOrNull { it.curve == curve }?.publicKey - - return if (walletPublicKey != null) { - makeWalletManagers(card.cardId, walletPublicKey, blockchainsForCurve, curve) - } else { - emptyList() - } -} \ No newline at end of file