Skip to content

Commit

Permalink
Use client requested chain ID instead of using the one from active se…
Browse files Browse the repository at this point in the history
…ssion
  • Loading branch information
ruixhuang committed Dec 16, 2024
1 parent 358387d commit 50c27bd
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 23 deletions.
2 changes: 1 addition & 1 deletion cartera/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {

android {
namespace 'exchange.dydx.cartera'
compileSdk 34
compileSdk 35

defaultConfig {
minSdk 24
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -461,14 +460,12 @@ class WalletConnectV2Provider(
val requiredNamespaces: Map<String, Sign.Model.Namespace.Proposal> = mapOf(namespace to proposal) /*Required namespaces to setup a session*/
val optionalNamespaces: Map<String, Sign.Model.Namespace.Proposal> = 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)
Expand Down Expand Up @@ -623,6 +620,17 @@ private fun Sign.Model.ApprovedSession.chainId(): Int? {
}
}

private fun Sign.Model.ApprovedSession.chainIds(): List<String>? {
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) {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 50c27bd

Please sign in to comment.