From 5edc800273a38a4f77b7235bc2e94d98bb027080 Mon Sep 17 00:00:00 2001 From: Rafael Muhamedzyanov Date: Thu, 26 Oct 2023 12:30:10 +0600 Subject: [PATCH] Handle custom WC pending requests topics --- app/build.gradle | 4 +- .../horizontalsystems/bankwallet/core/App.kt | 3 +- .../version2/SessionRequestFilterManager.kt | 43 +++++++++++++++++++ .../walletconnect/version2/WC2Service.kt | 7 ++- 4 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/io/horizontalsystems/bankwallet/modules/walletconnect/version2/SessionRequestFilterManager.kt diff --git a/app/build.gradle b/app/build.gradle index 3568b00aba8..a3bf511fcf0 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -17,8 +17,8 @@ android { compileSdk compile_sdk_version minSdkVersion min_sdk_version targetSdkVersion compile_sdk_version - versionCode 91 - versionName "0.36.0" + versionCode 92 + versionName "0.36.1" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" kapt { diff --git a/app/src/main/java/io/horizontalsystems/bankwallet/core/App.kt b/app/src/main/java/io/horizontalsystems/bankwallet/core/App.kt index 5eeab7171ad..ff06dd5d249 100644 --- a/app/src/main/java/io/horizontalsystems/bankwallet/core/App.kt +++ b/app/src/main/java/io/horizontalsystems/bankwallet/core/App.kt @@ -101,6 +101,7 @@ import io.horizontalsystems.bankwallet.modules.settings.appearance.LaunchScreenS import io.horizontalsystems.bankwallet.modules.theme.ThemeService import io.horizontalsystems.bankwallet.modules.theme.ThemeType import io.horizontalsystems.bankwallet.modules.walletconnect.storage.WC2SessionStorage +import io.horizontalsystems.bankwallet.modules.walletconnect.version2.SessionRequestFilterManager import io.horizontalsystems.bankwallet.modules.walletconnect.version2.WC2Manager import io.horizontalsystems.bankwallet.modules.walletconnect.version2.WC2Service import io.horizontalsystems.bankwallet.modules.walletconnect.version2.WC2SessionManager @@ -398,7 +399,7 @@ class App : CoreApp(), WorkConfiguration.Provider, ImageLoaderFactory { initializeWalletConnectV2(appConfig) - wc2Service = WC2Service() + wc2Service = WC2Service(SessionRequestFilterManager()) wc2SessionManager = WC2SessionManager(accountManager, WC2SessionStorage(appDatabase), wc2Service, wc2Manager) baseTokenManager = BaseTokenManager(coinManager, localStorage) diff --git a/app/src/main/java/io/horizontalsystems/bankwallet/modules/walletconnect/version2/SessionRequestFilterManager.kt b/app/src/main/java/io/horizontalsystems/bankwallet/modules/walletconnect/version2/SessionRequestFilterManager.kt new file mode 100644 index 00000000000..f599b9be245 --- /dev/null +++ b/app/src/main/java/io/horizontalsystems/bankwallet/modules/walletconnect/version2/SessionRequestFilterManager.kt @@ -0,0 +1,43 @@ +package io.horizontalsystems.bankwallet.modules.walletconnect.version2 + +import android.util.Log +import com.walletconnect.sign.client.Sign +import com.walletconnect.sign.client.SignClient + +class SessionRequestFilterManager() { + private val TAG = "SessionRequestFilterManager" + private val rejectList = setOf( + // custom methods + "personal_ecRecover", + "eth_getCode", + "wallet_switchEthereumChain", + "wallet_addEthereumChain" + ) + + private fun reject(request: Sign.Model.SessionRequest) { + try { + val response = Sign.Params.Response( + sessionTopic = request.topic, + jsonRpcResponse = Sign.Model.JsonRpcResponse.JsonRpcError( + id = request.request.id, + code = 500, + message = "Rejected by user" + ) + ) + + SignClient.respond(response) { + Log.e(TAG, "rejectRequest onError: ", it.throwable) + } + } catch (error: Throwable) { + println(error) + } + } + + fun canHandle(request: Sign.Model.SessionRequest): Boolean { + if (rejectList.contains(request.request.method)) { + reject(request) + return true + } + return false + } +} diff --git a/app/src/main/java/io/horizontalsystems/bankwallet/modules/walletconnect/version2/WC2Service.kt b/app/src/main/java/io/horizontalsystems/bankwallet/modules/walletconnect/version2/WC2Service.kt index 58831ff785b..f6898539611 100644 --- a/app/src/main/java/io/horizontalsystems/bankwallet/modules/walletconnect/version2/WC2Service.kt +++ b/app/src/main/java/io/horizontalsystems/bankwallet/modules/walletconnect/version2/WC2Service.kt @@ -19,7 +19,9 @@ import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch import java.util.concurrent.CopyOnWriteArrayList -class WC2Service : SignClient.WalletDelegate { +class WC2Service( + private val sessionRequestFilterManager: SessionRequestFilterManager +) : SignClient.WalletDelegate { private val TAG = "WC2Service" @@ -215,6 +217,9 @@ class WC2Service : SignClient.WalletDelegate { } override fun onSessionRequest(sessionRequest: Sign.Model.SessionRequest, verifyContext: Sign.Model.VerifyContext) { + if (sessionRequestFilterManager.canHandle(sessionRequest)) { + return + } sessionsRequestReceivedSubject.onNext(sessionRequest) pendingRequestUpdatedSubject.onNext(Unit) }