From c885b0fd60090ee97b029024827973eb12186c8c Mon Sep 17 00:00:00 2001 From: Rui <102453770+ruixhuang@users.noreply.github.com> Date: Fri, 29 Mar 2024 14:07:09 -0700 Subject: [PATCH] Update WC2 to 1.31.0 (#20) * Update WC2 to 1.31.0 * Add expiry * Fix build --- build.gradle | 4 +- cartera/build.gradle | 2 +- .../providers/WalletConnectV2Provider.kt | 58 +++++++++++-------- gradle.properties | 2 +- gradle/wrapper/gradle-wrapper.properties | 2 +- 5 files changed, 39 insertions(+), 29 deletions(-) diff --git a/build.gradle b/build.gradle index d3c375d..fad3c76 100644 --- a/build.gradle +++ b/build.gradle @@ -10,8 +10,8 @@ buildscript { // Top-level build file where you can add configuration options common to all sub-projects/modules. plugins { - id 'com.android.application' version '8.2.2' apply false - id 'com.android.library' version '8.2.2' apply false + id 'com.android.application' version '8.3.1' apply false + id 'com.android.library' version '8.3.1' apply false id 'org.jetbrains.kotlin.android' version '1.8.21' apply false id 'com.google.dagger.hilt.android' version '2.41' apply false id "com.diffplug.spotless" version "6.22.0" // apply false diff --git a/cartera/build.gradle b/cartera/build.gradle index 5897e43..90897e8 100644 --- a/cartera/build.gradle +++ b/cartera/build.gradle @@ -61,7 +61,7 @@ dependencies { // https://github.com/WalletConnect/WalletConnectKotlinV2 // - implementation platform('com.walletconnect:android-bom:1.23.0') + implementation platform('com.walletconnect:android-bom:1.31.0') implementation('com.walletconnect:android-core') implementation 'com.walletconnect:sign' //implementation 'com.walletconnect:push' 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 bbb27fc..4472928 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 @@ -65,6 +65,10 @@ class WalletConnectV2Provider( private var currentPairing: Core.Model.Pairing? = null + // expiry must be between current timestamp + MIN_INTERVAL and current timestamp + MAX_INTERVAL (MIN_INTERVAL: 300, MAX_INTERVAL: 604800) + private val requestExpiry: Long + get() = (System.currentTimeMillis() / 1000) + 400 + private val dappDelegate = object : SignClient.DappDelegate { override fun onSessionApproved(approvedSession: Sign.Model.ApprovedSession) { // Triggered when Dapp receives the session approval from wallet @@ -289,7 +293,6 @@ class WalletConnectV2Provider( override fun disconnect() { currentPairing?.let { - it CoreClient.Pairing.disconnect(Core.Params.Disconnect(it.topic)) { error -> Log.e(tag(this@WalletConnectV2Provider), error.throwable.stackTraceToString()) } @@ -316,14 +319,15 @@ class WalletConnectV2Provider( val namespace = currentSession?.namespace() val chainId = currentSession?.chainId() return if (sessionTopic != null && account != null && namespace != null && chainId != null) { - return Sign.Params.Request( + Sign.Params.Request( sessionTopic = sessionTopic, method = "personal_sign", params = "[\"${message}\", \"${account}\"]", chainId = "$namespace:$chainId", + expiry = requestExpiry, ) } else { - return null + null } } @@ -349,6 +353,7 @@ class WalletConnectV2Provider( method = "eth_signTypedData", params = "[\"${account}\", \"${message}\"]", chainId = "$namespace:$chainId", + expiry = requestExpiry, ) } else { null @@ -375,6 +380,7 @@ class WalletConnectV2Provider( method = "eth_sendTransaction", params = "[$message]", chainId = "$namespace:$chainId", + expiry = requestExpiry, ) } else { null @@ -414,18 +420,16 @@ class WalletConnectV2Provider( val proposal = Sign.Model.Namespace.Proposal(chains, methods, events) 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() -// if (pairings.isNotEmpty()) { -// pairing = pairings.first() -// } else { -// pairing = CoreClient.Pairing.create() { error -> -// Log.e(tag(this@WalletConnectV2Provider), error.throwable.stackTraceToString()) -// }!! -// } - - val pairing = CoreClient.Pairing.create() + + val pairing: Core.Model.Pairing? + val pairings = CoreClient.Pairing.getPairings() + if (pairings.isNotEmpty()) { + pairing = pairings.first() + } else { + pairing = CoreClient.Pairing.create() { error -> + Log.e(tag(this@WalletConnectV2Provider), error.throwable.stackTraceToString()) + } + } val expiry = (System.currentTimeMillis() / 1000) + TimeUnit.SECONDS.convert(7, TimeUnit.DAYS) val properties: Map = mapOf("sessionExpiry" to "$expiry") @@ -473,14 +477,20 @@ class WalletConnectV2Provider( val params = requestParams() if (params != null) { - reallyMakeRequest(request, params) { result, error -> - completion(result, error) + reallyMakeRequest(request, params) { result, requestError -> + CoroutineScope(Dispatchers.Main).launch { + completion(result, requestError) + } } } else { - completion(null, WalletError(CarteraErrorCode.INVALID_SESSION)) + CoroutineScope(Dispatchers.Main).launch { + completion(null, WalletError(CarteraErrorCode.INVALID_SESSION)) + } } } else { - completion(null, WalletError(CarteraErrorCode.INVALID_SESSION)) + CoroutineScope(Dispatchers.Main).launch { + completion(null, WalletError(CarteraErrorCode.INVALID_SESSION)) + } } } } @@ -492,9 +502,9 @@ class WalletConnectV2Provider( ) { SignClient.request( request = requestParams, - onSuccess = { request: Sign.Model.SentRequest -> + onSuccess = { sendRequest: Sign.Model.SentRequest -> Log.d(tag(this@WalletConnectV2Provider), "Wallet request made.") - operationCompletions[request.sessionTopic] = completion + operationCompletions[sendRequest.sessionTopic] = completion }, onError = { error -> Log.e(tag(this@WalletConnectV2Provider), error.throwable.stackTraceToString()) @@ -537,14 +547,14 @@ class WalletConnectV2Provider( Log.d(tag(this@WalletConnectV2Provider), "Wallet is null") return } - if (pairing?.uri == null) { + if (pairing == null) { Log.d(tag(this@WalletConnectV2Provider), "Pairing is null") return } // val deeplinkPairingUri = it.replace("wc:", "wc://") val url = WalletConnectUtils.createUrl( wallet = request.wallet, - deeplink = pairing?.uri, + deeplink = pairing.uri, type = WalletConnectionType.WalletConnectV2, context = request.context, ) @@ -606,7 +616,7 @@ private fun EthereumTransactionRequest.toJsonRequest(): String? { val filtered = request.filterValues { it != null } return try { - JSONObject(filtered as Map<*, *>?).toString() + JSONObject(filtered).toString() } catch (e: JSONException) { null } diff --git a/gradle.properties b/gradle.properties index 775aa01..151ccd0 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.13 +LIBRARY_VERSION_NAME=0.1.14 android.enableR8.fullMode = false \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 75a3abf..377d345 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ #Fri Jun 02 11:57:06 PDT 2023 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists