Skip to content

Commit

Permalink
Allow only specific wallets in the modal screen (#27)
Browse files Browse the repository at this point in the history
* Allow only specific wallets in the modal screen

* Lint

* Bump version
  • Loading branch information
ruixhuang authored Dec 20, 2024
1 parent d52bc5b commit 41918bf
Show file tree
Hide file tree
Showing 6 changed files with 490 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package exchange.dydx.carteraexample

import exchange.dydx.cartera.WalletConnectModalConfig
import exchange.dydx.cartera.WalletConnectV1Config
import exchange.dydx.cartera.WalletConnectV2Config
import exchange.dydx.cartera.WalletProvidersConfig
Expand Down Expand Up @@ -29,9 +30,10 @@ object WalletProvidersConfigUtil {
)

return WalletProvidersConfig(
walletConnectV1Config,
walletConnectV2Config,
walletSegueConfig,
walletConnectV1 = walletConnectV1Config,
walletConnectV2 = walletConnectV2Config,
walletConnectModal = WalletConnectModalConfig.default,
walletSegue = walletSegueConfig,
)
}
}
41 changes: 36 additions & 5 deletions cartera/src/main/java/exchange/dydx/cartera/CarteraConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,24 @@ class CarteraConfig(
if (walletProvidersConfig.walletConnectV2 != null) {
registration[WalletConnectionType.WalletConnectV2] = RegistrationConfig(
provider = WalletConnectV2Provider(
walletProvidersConfig.walletConnectV2,
application,
walletConnectV2Config = walletProvidersConfig.walletConnectV2,
application = application,
),
)

registration[WalletConnectionType.WalletConnectModal] = RegistrationConfig(
provider = WalletConnectModalProvider(
application = application,
config = walletProvidersConfig.walletConnectModal,
),
)
}
if (walletProvidersConfig.walletSegue != null) {
registration[WalletConnectionType.WalletSegue] = RegistrationConfig(
provider = WalletSegueProvider(
walletProvidersConfig.walletSegue,
application,
launcher,
walletSegueConfig = walletProvidersConfig.walletSegue,
application = application,
launcher = launcher,
),
)
}
Expand All @@ -106,6 +107,7 @@ class CarteraConfig(
registration[WalletConnectionType.WalletConnectModal] = RegistrationConfig(
provider = WalletConnectModalProvider(
application = application,
config = walletProvidersConfig.walletConnectModal,
),
)
}
Expand Down Expand Up @@ -164,6 +166,7 @@ class CarteraConfig(
data class WalletProvidersConfig(
val walletConnectV1: WalletConnectV1Config? = null,
val walletConnectV2: WalletConnectV2Config? = null,
val walletConnectModal: WalletConnectModalConfig? = null,
val walletSegue: WalletSegueConfig? = null
)

Expand All @@ -184,6 +187,34 @@ data class WalletConnectV2Config(
val iconUrls: List<String>
)

data class WalletConnectModalConfig(
val walletIds: List<String>?
) {
companion object {
val default: WalletConnectModalConfig = WalletConnectModalConfig(
walletIds = listOf(
"c57ca95b47569778a828d19178114f4db188b89b763c899ba0be274e97267d96", // Metamask
"4622a2b2d6af1c9844944291e5e7351a6aa24cd7b23099efac1b2fd875da31a0", // Trust
"971e689d0a5be527bac79629b4ee9b925e82208e5168b733496a09c0faed0709", // OKX
"c03dfee351b6fcc421b4494ea33b9d4b92a984f87aa76d1663bb28705e95034a", // Uniswap
"1ae92b26df02f0abca6304df07debccd18262fdf5fe82daa81593582dac9a369", // Rainbow
"ecc4036f814562b41a5268adc86270fba1365471402006302e70169465b7ac18", // Zerion
"c286eebc742a537cd1d6818363e9dc53b21759a1e8e5d9b263d0c03ec7703576", // 1inch
"ef333840daf915aafdc4a004525502d6d49d77bd9c65e0642dbaefb3c2893bef", // imToken
"38f5d18bd8522c244bdd70cb4a68e0e718865155811c043f052fb9f1c51de662", // Bitget
"0b415a746fb9ee99cce155c2ceca0c6f6061b1dbca2d722b3ba16381d0562150", // Safepal
"15c8b91ade1a4e58f3ce4e7a0dd7f42b47db0c8df7e0d84f63eb39bcb96c4e0f", // Bybit
"19177a98252e07ddfc9af2083ba8e07ef627cb6103467ffebb3f8f4205fd7927", // Ledger Live
"344d0e58b139eb1b6da0c29ea71d52a8eace8b57897c6098cb9b46012665c193", // Timeless X
"225affb176778569276e484e1b92637ad061b01e13a048b35a9d280c3b58970f", // Safe
"f2436c67184f158d1beda5df53298ee84abfc367581e4505134b5bcf5f46697d", // Crypto.com
"18450873727504ae9315a084fa7624b5297d2fe5880f0982979c17345a138277", // Kraken
"541d5dcd4ede02f3afaf75bf8e3e4c4f1fb09edb5fa6c4377ebf31c2785d9adf", // Ronin
),
)
}
}

data class WalletSegueConfig(
val callbackUrl: String
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ import android.content.ActivityNotFoundException
import android.content.Intent
import android.net.Uri
import androidx.navigation.NavHostController
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.walletconnect.android.CoreClient
import com.walletconnect.wcmodal.client.Modal
import com.walletconnect.wcmodal.client.WalletConnectModal
import com.walletconnect.wcmodal.ui.openWalletConnectModal
import exchange.dydx.cartera.CarteraErrorCode
import exchange.dydx.cartera.R
import exchange.dydx.cartera.WalletConnectModalConfig
import exchange.dydx.cartera.entities.Wallet
import exchange.dydx.cartera.entities.toJsonRequest
import exchange.dydx.cartera.tag
Expand All @@ -34,9 +38,11 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import timber.log.Timber
import java.lang.reflect.Type

class WalletConnectModalProvider(
private val application: Application
private val application: Application,
private val config: WalletConnectModalConfig?,
) : WalletOperationProviderProtocol, WalletConnectModal.ModalDelegate {

private var _walletStatus = WalletStatusImp()
Expand Down Expand Up @@ -65,8 +71,23 @@ class WalletConnectModalProvider(
var nav: NavHostController? = null

init {
val jsonData = application.getResources().openRawResource(R.raw.wc_modal_ids)
.bufferedReader().use { it.readText() }
val gson = Gson()
val idListType: Type = object : TypeToken<List<String>?>() {}.type
val wc_modal_ids: List<String>? = gson.fromJson(jsonData, idListType)
val excludedIds = wc_modal_ids?.toMutableList() ?: mutableListOf()
for (id in config?.walletIds ?: emptyList()) {
if (excludedIds.contains(id)) {
excludedIds.remove(id)
}
}
WalletConnectModal.initialize(
init = Modal.Params.Init(CoreClient),
init = Modal.Params.Init(
core = CoreClient,
recommendedWalletsIds = config?.walletIds ?: emptyList(),
excludedWalletIds = excludedIds,
),
onSuccess = {
// Callback will be called if initialization is successful
Timber.tag(tag(this)).d("WalletConnectModal initialized.")
Expand Down Expand Up @@ -306,9 +327,9 @@ class WalletConnectModalProvider(
completion(
null,
WalletError(
CarteraErrorCode.CONNECTION_FAILED,
"WalletConnectModal.request error",
error.throwable.stackTraceToString(),
code = CarteraErrorCode.CONNECTION_FAILED,
title = "WalletConnectModal.request error",
message = error.throwable.stackTraceToString(),
),
)
},
Expand Down
Loading

0 comments on commit 41918bf

Please sign in to comment.