From 6cf69ca99c034e4ba686d1ddad2d9bf1c3452f0f Mon Sep 17 00:00:00 2001 From: Rui Date: Thu, 28 Mar 2024 13:27:36 -0700 Subject: [PATCH 1/3] Update WC2 to 1.31.0 --- build.gradle | 4 ++-- cartera/build.gradle | 3 ++- .../providers/WalletConnectV2Provider.kt | 23 ++++++++----------- gradle.properties | 2 +- gradle/wrapper/gradle-wrapper.properties | 2 +- 5 files changed, 16 insertions(+), 18 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..f21b2a7 100644 --- a/cartera/build.gradle +++ b/cartera/build.gradle @@ -42,6 +42,7 @@ android { configurations { all { exclude group: 'org.bouncycastle', module: 'bcprov-jdk15to18' + exclude group: 'org.bouncycastle', module: 'bcprov-jdk15on' } } } @@ -61,7 +62,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..bc546a0 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 @@ -289,7 +289,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()) } @@ -414,18 +413,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") 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 From c11d124d4f60827917908f790fd90fd749af23ab Mon Sep 17 00:00:00 2001 From: Rui Date: Thu, 28 Mar 2024 15:22:11 -0700 Subject: [PATCH 2/3] Add expiry --- app/build.gradle | 4 ++-- cartera/build.gradle | 1 - .../providers/WalletConnectV2Provider.kt | 23 +++++++++++++++---- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index d21f46d..df9e783 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -51,7 +51,7 @@ dependencies { implementation platform('org.jetbrains.kotlin:kotlin-bom:1.8.0') implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.7.0' implementation 'androidx.activity:activity-compose:1.8.2' - implementation platform('androidx.compose:compose-bom:2022.10.00') + implementation platform('androidx.compose:compose-bom:2024.03.00') implementation 'androidx.compose.ui:ui' implementation 'androidx.compose.ui:ui-graphics' implementation 'androidx.compose.ui:ui-tooling-preview' @@ -60,7 +60,7 @@ dependencies { testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' - androidTestImplementation platform('androidx.compose:compose-bom:2022.10.00') + androidTestImplementation platform('androidx.compose:compose-bom:2024.03.00') androidTestImplementation 'androidx.compose.ui:ui-test-junit4' debugImplementation 'androidx.compose.ui:ui-tooling' debugImplementation 'androidx.compose.ui:ui-test-manifest' diff --git a/cartera/build.gradle b/cartera/build.gradle index f21b2a7..90897e8 100644 --- a/cartera/build.gradle +++ b/cartera/build.gradle @@ -42,7 +42,6 @@ android { configurations { all { exclude group: 'org.bouncycastle', module: 'bcprov-jdk15to18' - exclude group: 'org.bouncycastle', module: 'bcprov-jdk15on' } } } 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 bc546a0..4c328ce 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 @@ -315,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 } } @@ -348,6 +353,7 @@ class WalletConnectV2Provider( method = "eth_signTypedData", params = "[\"${account}\", \"${message}\"]", chainId = "$namespace:$chainId", + expiry = requestExpiry, ) } else { null @@ -374,6 +380,7 @@ class WalletConnectV2Provider( method = "eth_sendTransaction", params = "[$message]", chainId = "$namespace:$chainId", + expiry = requestExpiry, ) } else { null @@ -471,13 +478,19 @@ class WalletConnectV2Provider( val params = requestParams() if (params != null) { reallyMakeRequest(request, params) { result, error -> - completion(result, error) + CoroutineScope(Dispatchers.Main).launch { + completion(result, error) + } } } 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)) + } } } } From e800f351beb948b31c4754bbcca12b92940e73c0 Mon Sep 17 00:00:00 2001 From: Rui Date: Thu, 28 Mar 2024 17:43:40 -0700 Subject: [PATCH 3/3] Fix build --- app/build.gradle | 4 ++-- .../providers/WalletConnectV2Provider.kt | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index df9e783..d21f46d 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -51,7 +51,7 @@ dependencies { implementation platform('org.jetbrains.kotlin:kotlin-bom:1.8.0') implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.7.0' implementation 'androidx.activity:activity-compose:1.8.2' - implementation platform('androidx.compose:compose-bom:2024.03.00') + implementation platform('androidx.compose:compose-bom:2022.10.00') implementation 'androidx.compose.ui:ui' implementation 'androidx.compose.ui:ui-graphics' implementation 'androidx.compose.ui:ui-tooling-preview' @@ -60,7 +60,7 @@ dependencies { testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.5' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' - androidTestImplementation platform('androidx.compose:compose-bom:2024.03.00') + androidTestImplementation platform('androidx.compose:compose-bom:2022.10.00') androidTestImplementation 'androidx.compose.ui:ui-test-junit4' debugImplementation 'androidx.compose.ui:ui-tooling' debugImplementation 'androidx.compose.ui:ui-test-manifest' 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 4c328ce..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 @@ -477,9 +477,9 @@ class WalletConnectV2Provider( val params = requestParams() if (params != null) { - reallyMakeRequest(request, params) { result, error -> + reallyMakeRequest(request, params) { result, requestError -> CoroutineScope(Dispatchers.Main).launch { - completion(result, error) + completion(result, requestError) } } } else { @@ -502,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()) @@ -547,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, ) @@ -616,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 }