diff --git a/cartera/build.gradle b/cartera/build.gradle index 7521f19..50627a7 100644 --- a/cartera/build.gradle +++ b/cartera/build.gradle @@ -6,7 +6,7 @@ plugins { android { namespace 'exchange.dydx.cartera' - compileSdk 34 + compileSdk 35 defaultConfig { minSdk 24 diff --git a/cartera/src/main/java/exchange/dydx/cartera/walletprovider/providers/WalletConnectModalProvider.kt b/cartera/src/main/java/exchange/dydx/cartera/walletprovider/providers/WalletConnectModalProvider.kt index ae0b6e8..c0c950c 100644 --- a/cartera/src/main/java/exchange/dydx/cartera/walletprovider/providers/WalletConnectModalProvider.kt +++ b/cartera/src/main/java/exchange/dydx/cartera/walletprovider/providers/WalletConnectModalProvider.kt @@ -156,7 +156,11 @@ class WalletConnectModalProvider( fun requestParams(): Modal.Params.Request? { val sessionTopic = currentSession?.topic val account = _walletStatus.connectedWallet?.address - val chainId = currentSession?.namespaces?.get(ethNamespace)?.chains?.firstOrNull() + val chainId = if (request.chainId != null) { + "$ethNamespace:${request.chainId}" + } else { + currentSession?.namespaces?.get(ethNamespace)?.chains?.firstOrNull() + } return if (sessionTopic != null && account != null && chainId != null) { Modal.Params.Request( sessionTopic = sessionTopic, @@ -183,7 +187,11 @@ class WalletConnectModalProvider( fun requestParams(): Modal.Params.Request? { val sessionTopic = currentSession?.topic val account = _walletStatus.connectedWallet?.address - val chainId = currentSession?.namespaces?.get(ethNamespace)?.chains?.firstOrNull() + val chainId = if (request.chainId != null) { + "$ethNamespace:${request.chainId}" + } else { + currentSession?.namespaces?.get(ethNamespace)?.chains?.firstOrNull() + } val message = typedDataProvider?.typedDataAsString?.replace("\"", "\\\"") return if (sessionTopic != null && account != null && chainId != null && message != null) { @@ -211,7 +219,11 @@ class WalletConnectModalProvider( fun requestParams(): Modal.Params.Request? { val sessionTopic = currentSession?.topic val account = _walletStatus.connectedWallet?.address - val chainId = currentSession?.namespaces?.get(ethNamespace)?.chains?.firstOrNull() + val chainId = if (request.walletRequest.chainId != null) { + "$ethNamespace:${request.walletRequest.chainId}" + } else { + currentSession?.namespaces?.get(ethNamespace)?.chains?.firstOrNull() + } val message = request.ethereum?.toJsonRequest() return if (sessionTopic != null && account != null && chainId != null && message != null) { @@ -328,10 +340,12 @@ class WalletConnectModalProvider( CoroutineScope(Dispatchers.Main).launch { val approvedSsssion = approvedSession.namespaces[ethNamespace] - val approvedChain = approvedSsssion?.chains?.firstOrNull() - if (requestingWallet?.ethChain != null && - approvedChain != requestingWallet?.ethChain - ) { + + val requestChainId = requestingWallet?.chainId + val walletChainIds = approvedSsssion?.chains?.map { + it.split(":")[1] + } ?: emptyList() + if (requestChainId != null && !walletChainIds.contains(requestChainId)) { for (connectCompletion in connectCompletions) { connectCompletion.invoke( null, diff --git a/cartera/src/main/java/exchange/dydx/cartera/walletprovider/providers/WalletConnectV2Provider.kt b/cartera/src/main/java/exchange/dydx/cartera/walletprovider/providers/WalletConnectV2Provider.kt index f55ef90..298df1b 100644 --- a/cartera/src/main/java/exchange/dydx/cartera/walletprovider/providers/WalletConnectV2Provider.kt +++ b/cartera/src/main/java/exchange/dydx/cartera/walletprovider/providers/WalletConnectV2Provider.kt @@ -107,10 +107,9 @@ class WalletConnectV2Provider( Log.d(tag(this@WalletConnectV2Provider), "onSessionApproved") CoroutineScope(Dispatchers.Main).launch { - if (requestingWallet?.chainId != null && - approvedSession.chainId() != null && - requestingWallet?.chainId != approvedSession.chainId().toString() - ) { + val requestChainId = requestingWallet?.chainId + val walletChainIds = approvedSession.chainIds() ?: emptyList() + if (requestChainId != null && !walletChainIds.contains(requestChainId)) { for (connectCompletion in connectCompletions) { connectCompletion.invoke( null, @@ -354,7 +353,7 @@ class WalletConnectV2Provider( val sessionTopic = currentSession?.topic val account = currentSession?.account() val namespace = currentSession?.namespace() - val chainId = currentSession?.chainId() + val chainId = request.chainId ?: currentSession?.chainId() return if (sessionTopic != null && account != null && namespace != null && chainId != null) { Sign.Params.Request( sessionTopic = sessionTopic, @@ -382,7 +381,7 @@ class WalletConnectV2Provider( val sessionTopic = currentSession?.topic val account = currentSession?.account() val namespace = currentSession?.namespace() - val chainId = currentSession?.chainId() + val chainId = request.chainId ?: currentSession?.chainId() val message = typedDataProvider?.typedDataAsString?.replace("\"", "\\\"") return if (sessionTopic != null && account != null && namespace != null && chainId != null && message != null) { @@ -411,7 +410,7 @@ class WalletConnectV2Provider( val sessionTopic = currentSession?.topic val account = currentSession?.account() val namespace = currentSession?.namespace() - val chainId = currentSession?.chainId() + val chainId = request.walletRequest.chainId ?: currentSession?.chainId() val message = request.ethereum?.toJsonRequest() return if (sessionTopic != null && account != null && namespace != null && chainId != null && message != null) { Sign.Params.Request( @@ -461,14 +460,12 @@ class WalletConnectV2Provider( val requiredNamespaces: Map = mapOf(namespace to proposal) /*Required namespaces to setup a session*/ val optionalNamespaces: Map = emptyMap() /*Optional namespaces to setup a session*/ - val pairing: Core.Model.Pairing? val pairings = CoreClient.Pairing.getPairings() - pairing = if (pairings.isNotEmpty()) { - pairings.first() - } else { - CoreClient.Pairing.create() { error -> - Log.e(tag(this@WalletConnectV2Provider), error.throwable.stackTraceToString()) - } + for (pairing in pairings) { + CoreClient.Pairing.disconnect(pairing.topic) + } + val pairing = CoreClient.Pairing.create() { error -> + Log.e(tag(this@WalletConnectV2Provider), error.throwable.stackTraceToString()) } val expiry = (System.currentTimeMillis() / 1000) + TimeUnit.SECONDS.convert(7, TimeUnit.DAYS) @@ -623,6 +620,17 @@ private fun Sign.Model.ApprovedSession.chainId(): Int? { } } +private fun Sign.Model.ApprovedSession.chainIds(): List? { + return accounts.mapNotNull { + val split = it.split(":") + if (split.count() > 1) { + split[1] + } else { + null + } + } +} + private fun Sign.Model.ApprovedSession.namespace(): String? { val split = accounts.first().split(":") return if (split.count() > 0) { diff --git a/gradle.properties b/gradle.properties index 934020e..7456470 100644 --- a/gradle.properties +++ b/gradle.properties @@ -26,6 +26,6 @@ android.nonTransitiveRClass=true LIBRARY_GROUP=dydxprotocol LIBRARY_ARTIFACT_ID=cartera-android -LIBRARY_VERSION_NAME=0.1.16 +LIBRARY_VERSION_NAME=0.1.17 android.enableR8.fullMode = false \ No newline at end of file