Skip to content

Commit

Permalink
Implement WalletConnect methods
Browse files Browse the repository at this point in the history
`wallet_addEthereumChain`, `wallet_switchEthereumChain`
  • Loading branch information
omurovch committed May 14, 2024
1 parent 2ee8069 commit c149f26
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 11 deletions.
3 changes: 3 additions & 0 deletions app/src/main/java/io/horizontalsystems/bankwallet/core/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ import io.horizontalsystems.bankwallet.modules.theme.ThemeService
import io.horizontalsystems.bankwallet.modules.theme.ThemeType
import io.horizontalsystems.bankwallet.modules.walletconnect.WCManager
import io.horizontalsystems.bankwallet.modules.walletconnect.WCSessionManager
import io.horizontalsystems.bankwallet.modules.walletconnect.WCWalletRequestHandler
import io.horizontalsystems.bankwallet.modules.walletconnect.storage.WCSessionStorage
import io.horizontalsystems.bankwallet.widgets.MarketWidgetManager
import io.horizontalsystems.bankwallet.widgets.MarketWidgetRepository
Expand Down Expand Up @@ -166,6 +167,7 @@ class App : CoreApp(), WorkConfiguration.Provider, ImageLoaderFactory {
lateinit var coinManager: ICoinManager
lateinit var wcSessionManager: WCSessionManager
lateinit var wcManager: WCManager
lateinit var wcWalletRequestHandler: WCWalletRequestHandler
lateinit var termsManager: ITermsManager
lateinit var marketFavoritesManager: MarketFavoritesManager
lateinit var marketKit: MarketKitWrapper
Expand Down Expand Up @@ -361,6 +363,7 @@ class App : CoreApp(), WorkConfiguration.Provider, ImageLoaderFactory {
rateAppManager = RateAppManager(walletManager, adapterManager, localStorage)

wcManager = WCManager(accountManager)
wcWalletRequestHandler = WCWalletRequestHandler(evmBlockchainManager)

termsManager = TermsManager(localStorage)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ object WCDelegate : Web3Wallet.WalletDelegate, CoreClient.CoreDelegate {
App.wcSessionManager.getCurrentSessionRequests().reversed().firstOrNull()
?: return

if (App.wcWalletRequestHandler.handle(sessionRequestForAccount)) return

sessionRequestEvent = sessionRequestForAccount

scope.launch {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package io.horizontalsystems.bankwallet.modules.walletconnect

import android.util.Log
import com.google.gson.Gson
import com.google.gson.JsonParser
import com.walletconnect.web3.wallet.client.Wallet
import com.walletconnect.web3.wallet.client.Web3Wallet
import io.horizontalsystems.bankwallet.core.managers.EvmBlockchainManager
import io.horizontalsystems.ethereumkit.core.hexStringToIntOrNull

class WCWalletRequestHandler(
private val evmBlockchainManager: EvmBlockchainManager
) {
private val gson by lazy { Gson() }

fun handle(sessionRequest: Wallet.Model.SessionRequest): Boolean {
try {
val request = sessionRequest.request
val params = JsonParser.parseString(sessionRequest.request.params).asJsonArray
val chain = gson.fromJson(params.first(), WalletConnectChain::class.java)

return when (request.method) {
"wallet_addEthereumChain",
"wallet_switchEthereumChain" -> {
val blockchain = chain.chainId.hexStringToIntOrNull()?.let { evmBlockchainManager.getBlockchain(it) }
if (blockchain != null) {
val response = Wallet.Params.SessionRequestResponse(
sessionTopic = sessionRequest.topic,
jsonRpcResponse = Wallet.Model.JsonRpcResponse.JsonRpcResult(
id = request.id,
result = "null"
)
)
Web3Wallet.respondSessionRequest(
params = response,
onSuccess = {},
onError = { error ->
Log.e("WCWalletHandler", "${request.method} response error: $error")
})

} else {
val result = Wallet.Params.SessionRequestResponse(
sessionTopic = sessionRequest.topic,
jsonRpcResponse = Wallet.Model.JsonRpcResponse.JsonRpcError(
id = request.id,
code = 4902,
message = "Unrecognized chain ID"
)
)
Web3Wallet.respondSessionRequest(result,
onSuccess = {},
onError = { error ->
Log.e("WCWalletHandler", "${request.method} response error: $error")
})
}

true
}

else -> false
}
} catch (error: Throwable) {
return false
}
}

data class WalletConnectChain(
val chainId: String,
val chainName: String?,
val rpcUrls: List<String>?,
val iconUrls: List<String>?,
val nativeCurrency: WalletConnectNativeCurrency?,
val blockExplorerUrls: List<String>?,
)

data class WalletConnectNativeCurrency(
val name: String,
val symbol: String,
val decimals: Int,
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ object WCSessionModule {
App.connectivityManager,
App.accountManager.activeAccount,
sessionTopic,
App.evmBlockchainManager
) as T
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import io.horizontalsystems.bankwallet.R
import io.horizontalsystems.bankwallet.core.UnsupportedAccountException
import io.horizontalsystems.bankwallet.core.ViewModelUiState
import io.horizontalsystems.bankwallet.core.managers.ConnectivityManager
import io.horizontalsystems.bankwallet.core.managers.EvmBlockchainManager
import io.horizontalsystems.bankwallet.core.providers.Translator
import io.horizontalsystems.bankwallet.entities.Account
import io.horizontalsystems.bankwallet.entities.AccountType
Expand Down Expand Up @@ -35,6 +36,7 @@ class WCSessionViewModel(
private val connectivityManager: ConnectivityManager,
private val account: Account?,
private val topic: String?,
private val evmBlockchainManager: EvmBlockchainManager
) : ViewModelUiState<WCSessionUiState>() {

val closeLiveEvent = SingleLiveEvent<Unit>()
Expand All @@ -49,15 +51,7 @@ class WCSessionViewModel(
private var status: Status? = null
private var pendingRequests = listOf<WCRequestViewItem>()

private val supportedChains = listOf(
Chain.Ethereum,
Chain.BinanceSmartChain,
Chain.Polygon,
Chain.Optimism,
Chain.ArbitrumOne,
Chain.Avalanche,
Chain.Gnosis,
)
private val supportedChains = EvmBlockchainManager.blockchainTypes.map { evmBlockchainManager.getChain(it) }

private val supportedMethods = listOf(
"eth_sendTransaction",
Expand All @@ -70,8 +64,8 @@ class WCSessionViewModel(
"eth_sign",
"eth_signTransaction",
"eth_signTypedData",
// "wallet_addEthereumChain",
// "wallet_switchEthereumChain"
"wallet_addEthereumChain",
"wallet_switchEthereumChain"
)

private val supportedEvents = listOf("chainChanged", "accountsChanged" /*"connect", "disconnect", "message"*/)
Expand Down

0 comments on commit c149f26

Please sign in to comment.