diff --git a/app/src/main/java/com/tangem/tap/common/DialogManager.kt b/app/src/main/java/com/tangem/tap/common/DialogManager.kt index 2895613815..342e29a83e 100644 --- a/app/src/main/java/com/tangem/tap/common/DialogManager.kt +++ b/app/src/main/java/com/tangem/tap/common/DialogManager.kt @@ -65,6 +65,15 @@ class DialogManager : StoreSubscriber { messageRes = R.string.wallet_connect_scanner_error_no_ethereum_wallet, context = context ) + is WalletConnectDialog.AddNetwork -> + SimpleAlertDialog.create( + titleRes = R.string.wallet_connect, + message = context.getString( + R.string.wallet_connect_network_not_found_format, + state.dialog.network + ), + context = context + ) is WalletConnectDialog.OpeningSessionRejected -> { SimpleAlertDialog.create( titleRes = R.string.wallet_connect, diff --git a/app/src/main/java/com/tangem/tap/features/details/redux/walletconnect/WalletConnectMiddleware.kt b/app/src/main/java/com/tangem/tap/features/details/redux/walletconnect/WalletConnectMiddleware.kt index 2bdc6edf5f..25c5d373b4 100644 --- a/app/src/main/java/com/tangem/tap/features/details/redux/walletconnect/WalletConnectMiddleware.kt +++ b/app/src/main/java/com/tangem/tap/features/details/redux/walletconnect/WalletConnectMiddleware.kt @@ -1,12 +1,11 @@ package com.tangem.tap.features.details.redux.walletconnect import com.tangem.blockchain.common.Blockchain -import com.tangem.blockchain.common.DerivationParams import com.tangem.blockchain.common.WalletManager import com.tangem.common.extensions.guard import com.tangem.domain.common.ScanResponse -import com.tangem.domain.common.TapWorkarounds.derivationStyle import com.tangem.domain.common.TapWorkarounds.isTestCard +import com.tangem.domain.common.extensions.withMainContext import com.tangem.tap.common.extensions.dispatchOnMain import com.tangem.tap.common.extensions.getFromClipboard import com.tangem.tap.common.redux.AppState @@ -21,8 +20,6 @@ import com.tangem.tap.domain.walletconnect.BnbHelper import com.tangem.tap.domain.walletconnect.WalletConnectManager import com.tangem.tap.domain.walletconnect.WalletConnectNetworkUtils import com.tangem.tap.features.demo.DemoHelper -import com.tangem.tap.features.wallet.models.Currency -import com.tangem.tap.features.wallet.redux.WalletAction import com.tangem.tap.scope import com.tangem.tap.store import com.tangem.wallet.R @@ -192,40 +189,51 @@ class WalletConnectMiddleware { return } - val walletManager = getWalletManager(scanResponse, blockchain).guard { - store.dispatchOnMain(WalletConnectAction.FailureEstablishingSession(null)) - return + scope.launch { + val walletManager = getWalletManager(scanResponse, blockchain).guard { + store.dispatchOnMain(WalletConnectAction.FailureEstablishingSession(session.session)) + store.dispatchOnMain( + GlobalAction.ShowDialog( + WalletConnectDialog.AddNetwork(blockchain.fullName) + ) + ) + return@launch + } + + val wallet = walletManager.wallet + val derivedKey = + if (wallet.publicKey.blockchainKey.contentEquals(wallet.publicKey.seedKey)) { + null + } else { + walletManager.wallet.publicKey.blockchainKey + } + val walletForSession = WalletForSession( + cardId = scanResponse.card.cardId, + walletPublicKey = wallet.publicKey.seedKey, + derivedPublicKey = derivedKey, + derivationPath = wallet.publicKey.derivationPath, + blockchain = wallet.blockchain + ) + + withMainContext { + val updatedSession = session.copy(wallet = walletForSession) + walletConnectManager.updateSession(updatedSession) + + store.dispatch(WalletConnectAction.AddScanResponse(scanResponse)) + + store.dispatch( + GlobalAction.ShowDialog( + WalletConnectDialog.ApproveWcSession( + updatedSession + ) + ) + ) + } } - val wallet = walletManager.wallet - val derivedKey = - if (wallet.publicKey.blockchainKey.contentEquals(wallet.publicKey.seedKey)) { - null - } else { - walletManager.wallet.publicKey.blockchainKey - } - val walletForSession = WalletForSession( - cardId = scanResponse.card.cardId, - walletPublicKey = wallet.publicKey.seedKey, - derivedPublicKey = derivedKey, - derivationPath = wallet.publicKey.derivationPath, - blockchain = wallet.blockchain - ) - val updatedSession = session.copy(wallet = walletForSession) - walletConnectManager.updateSession(updatedSession) - - store.dispatchOnMain(WalletConnectAction.AddScanResponse(scanResponse)) - - store.dispatchOnMain( - GlobalAction.ShowDialog( - WalletConnectDialog.ApproveWcSession( - updatedSession - ) - ) - ) } - private fun getWalletManager( + private suspend fun getWalletManager( scanResponse: ScanResponse, blockchain: Blockchain ): WalletManager? { val card = scanResponse.card @@ -236,42 +244,25 @@ class WalletConnectMiddleware { blockchain } + val blockchainNetwork = BlockchainNetwork( + blockchain = blockchainToMake, + card = card + ) + return if (store.state.globalState.scanResponse?.card?.cardId == card.cardId) { - val derivationPath = blockchainToMake.derivationPath(card.derivationStyle)?.rawPath - store.state.walletState.getWalletManager( - Currency.Blockchain(blockchainToMake, derivationPath) - ) - ?: factory.makeWalletManagerForApp( - scanResponse, - blockchainToMake, - card.derivationStyle?.let { DerivationParams.Default(it) } - ) - ?.also { walletManager -> - store.dispatch( - WalletAction.MultiWallet.AddBlockchain( - BlockchainNetwork.fromWalletManager(walletManager), walletManager - ) - ) - } + store.state.walletState.getWalletManager(blockchainNetwork) } else { - val walletManager = factory.makeWalletManagerForApp( - scanResponse, - blockchainToMake, - card.derivationStyle?.let { DerivationParams.Default(it) } - ) - scope.launch { - if (currenciesRepository.loadSavedCurrencies(card.cardId, card.settings.isHDWalletAllowed) - .find { it.blockchain == blockchainToMake } != null - ) { - walletManager?.let { - currenciesRepository.saveUpdatedCurrency( - card.cardId, - BlockchainNetwork.fromWalletManager(walletManager) - ) - } - } + if (currenciesRepository + .loadSavedCurrencies(card.cardId, card.settings.isHDWalletAllowed) + .contains(blockchainNetwork) + ) { + factory.makeWalletManagerForApp( + scanResponse = scanResponse, + blockchainNetwork = blockchainNetwork + ) + } else { + null } - return walletManager } } } \ No newline at end of file diff --git a/app/src/main/java/com/tangem/tap/features/details/redux/walletconnect/WalletConnectState.kt b/app/src/main/java/com/tangem/tap/features/details/redux/walletconnect/WalletConnectState.kt index 0039e247ee..4f5861c67b 100644 --- a/app/src/main/java/com/tangem/tap/features/details/redux/walletconnect/WalletConnectState.kt +++ b/app/src/main/java/com/tangem/tap/features/details/redux/walletconnect/WalletConnectState.kt @@ -82,6 +82,7 @@ data class WalletForSession( sealed class WalletConnectDialog : StateDialog { data class ClipboardOrScanQr(val clipboardUri: String) : WalletConnectDialog() object UnsupportedCard : WalletConnectDialog() + data class AddNetwork(val network: String) : WalletConnectDialog() object OpeningSessionRejected : WalletConnectDialog() object SessionTimeout : WalletConnectDialog() data class ApproveWcSession(val session: WalletConnectSession) : WalletConnectDialog() diff --git a/app/src/main/res/values-de/strings_final.xml b/app/src/main/res/values-de/strings_final.xml index d2c6336c24..e523e56dac 100644 --- a/app/src/main/res/values-de/strings_final.xml +++ b/app/src/main/res/values-de/strings_final.xml @@ -54,4 +54,5 @@ Unable to hide %s The %s token is the main currency on the %s network and cannot be hidden as long as you have other tokens on this network in the list. + %s network not found. Please, add it first and try again. \ No newline at end of file diff --git a/app/src/main/res/values-fr/strings_final.xml b/app/src/main/res/values-fr/strings_final.xml index d2c6336c24..e523e56dac 100644 --- a/app/src/main/res/values-fr/strings_final.xml +++ b/app/src/main/res/values-fr/strings_final.xml @@ -54,4 +54,5 @@ Unable to hide %s The %s token is the main currency on the %s network and cannot be hidden as long as you have other tokens on this network in the list. + %s network not found. Please, add it first and try again. \ No newline at end of file diff --git a/app/src/main/res/values-it/strings_final.xml b/app/src/main/res/values-it/strings_final.xml index d2c6336c24..a64a7a676d 100644 --- a/app/src/main/res/values-it/strings_final.xml +++ b/app/src/main/res/values-it/strings_final.xml @@ -54,4 +54,6 @@ Unable to hide %s The %s token is the main currency on the %s network and cannot be hidden as long as you have other tokens on this network in the list. + %s network not found. Please, add it first and try again. + \ No newline at end of file diff --git a/app/src/main/res/values-ru/strings_final.xml b/app/src/main/res/values-ru/strings_final.xml index d3299007a1..4d79e63197 100644 --- a/app/src/main/res/values-ru/strings_final.xml +++ b/app/src/main/res/values-ru/strings_final.xml @@ -59,4 +59,5 @@ Невозможно скрыть %s Токен %s является основной валютой в сети %s и не может быть скрыт, до тех пор пока у вас в списке есть другие токены этой сети. + Сеть %s не найдена. Пожалуйста, добавьте её и попробуйте заново. diff --git a/app/src/main/res/values/strings_final.xml b/app/src/main/res/values/strings_final.xml index 99845e1fb7..3ff79d9b5d 100644 --- a/app/src/main/res/values/strings_final.xml +++ b/app/src/main/res/values/strings_final.xml @@ -59,4 +59,5 @@ Unable to hide %s The %s token is the main currency on the %s network and cannot be hidden as long as you have other tokens on this network in the list. + %s network not found. Please, add it first and try again.