Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use client requested chain ID instead of using the one from active session #24

Merged
merged 2 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,13 @@ 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?.mapNotNull {
val components = it.split(":")
if (components.size > 1) components[1] else null
} ?: 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