Updated on 2026-08-14

This commit is contained in:
Tangem 2024-03-26 14:52:34 +00:00
commit 0db70a91cd
6 changed files with 35 additions and 39 deletions

View file

@ -13,6 +13,7 @@ import com.tangem.tap.features.details.redux.walletconnect.WalletForSession
import com.trustwallet.walletconnect.models.WCPeerMeta
import com.trustwallet.walletconnect.models.session.WCSession
import timber.log.Timber
import java.io.FileNotFoundException
import java.nio.charset.Charset
class WalletConnectRepository(val context: Application) {
@ -35,6 +36,8 @@ class WalletConnectRepository(val context: Application) {
val json = context.readFileText(FILE_NAME_PREFIX_SESSIONS)
.hexToUtf8()
walletConnectAdapter.fromJson(json)!!.map { it.toSession() }
} catch (e: FileNotFoundException) {
emptyList()
} catch (exception: Exception) {
Timber.w(exception)
emptyList()

View file

@ -14,12 +14,9 @@ import com.walletconnect.android.CoreClient
import com.walletconnect.android.relay.ConnectionType
import com.walletconnect.web3.wallet.client.Wallet
import com.walletconnect.web3.wallet.client.Web3Wallet
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.launch
import timber.log.Timber
import javax.inject.Inject
@ -192,8 +189,8 @@ class WalletConnectRepositoryImpl @Inject constructor(
if (sessionDelete is Wallet.Model.SessionDelete.Success) {
scope.launch {
_events.emit(WalletConnectEvents.SessionDeleted(sessionDelete.topic))
updateSessionsInternal().join()
}
updateSessions()
}
Timber.d("onSessionDelete: $sessionDelete")
}
@ -209,21 +206,21 @@ class WalletConnectRepositoryImpl @Inject constructor(
accounts = userNamespaces?.flatMap { it.value } ?: emptyList(),
),
)
updateSessionsInternal().join()
}
updateSessions()
}
}
override fun onSessionUpdateResponse(sessionUpdateResponse: Wallet.Model.SessionUpdateResponse) {
// Triggered when wallet receives the session update response from Dapp
Timber.d("onSessionUpdateResponse: $sessionUpdateResponse")
updateSessions()
updateSessionsInternal()
}
override fun onConnectionStateChange(state: Wallet.Model.ConnectionState) {
// Triggered whenever the connection state is changed
Timber.d("onConnectionStateChange: $state")
if (state.isAvailable) updateSessions()
if (state.isAvailable) updateSessionsInternal()
}
override fun onError(error: Wallet.Model.Error) {
@ -416,7 +413,7 @@ class WalletConnectRepositoryImpl @Inject constructor(
dAppUrl = session?.url ?: "",
),
)
updateSessions()
updateSessionsInternal()
Timber.d("Disconnected successfully: $it")
},
onError = {
@ -440,20 +437,22 @@ class WalletConnectRepositoryImpl @Inject constructor(
}
override fun updateSessions() {
scope.launch {
val availableSessions = Web3Wallet.getListOfActiveSessions()
.map {
WalletConnectSession(
topic = it.topic,
icon = it.metaData?.icons?.firstOrNull(),
name = it.metaData?.name,
url = it.metaData?.url,
)
}
Timber.d("Available sessions: $availableSessions")
currentSessions = availableSessions
_activeSessions.emit(availableSessions)
}
updateSessionsInternal()
}
private fun updateSessionsInternal(): Job = scope.launch {
val availableSessions = Web3Wallet.getListOfActiveSessions()
.map {
WalletConnectSession(
topic = it.topic,
icon = it.metaData?.icons?.firstOrNull(),
name = it.metaData?.name,
url = it.metaData?.url,
)
}
Timber.d("Available sessions: $availableSessions")
currentSessions = availableSessions
_activeSessions.emit(availableSessions)
}
private fun findMissingNetworks(

View file

@ -37,11 +37,13 @@ class WalletConnectInteractor(
suspend fun startListening(userWalletId: String, cardId: String?) {
this.userWalletId = userWalletId
this.cardId = cardId
walletConnectRepository.updateSessions()
coroutineScope {
launch { subscribeToEvents() }
launch { subscribeToSessions() }
}
walletConnectRepository.updateSessions()
}
fun setUserChains(accounts: List<Account>) {
@ -88,11 +90,12 @@ class WalletConnectInteractor(
is WalletConnectEvents.SessionApprovalSuccess -> {
sessionsRepository.saveSession(
userWallet = userWalletId,
session = Session.fromAccounts(
session = Session(
accounts = wcEvent.accounts,
topic = wcEvent.topic,
),
)
walletConnectRepository.updateSessions()
handler.onSessionEstablished()
}
is WalletConnectEvents.SessionDeleted -> {

View file

@ -3,13 +3,4 @@ package com.tangem.tap.domain.walletconnect2.domain.models
data class Session(
val topic: String,
val accounts: List<Account>,
) {
companion object {
fun fromAccounts(accounts: List<Account>, topic: String): Session {
return Session(
topic = topic,
accounts = accounts,
)
}
}
}
)

View file

@ -59,8 +59,7 @@ sealed class WalletConnectAction : Action {
) : WalletConnectAction()
data class FailureEstablishingSession(val session: WCSession?, val error: TapError? = null) : WalletConnectAction()
data class SetSessionsRestored(val sessions: List<WalletConnectSession>) :
WalletConnectAction()
data class SetSessionsRestored(val sessions: List<WalletConnectSession>) : WalletConnectAction()
data class DisconnectSession(val topic: String, val session: WCSession?) : WalletConnectAction()

View file

@ -20,8 +20,9 @@ object WalletConnectReducer {
is WalletConnectAction.SetNewSessionData -> {
state.copy(newSessionData = action.newSession)
}
is WalletConnectAction.SetSessionsRestored ->
WalletConnectState(sessions = action.sessions)
is WalletConnectAction.SetSessionsRestored -> state.copy(
sessions = action.sessions,
)
is WalletConnectAction.RemoveSession -> {
val sessions =
state.sessions.filterNot { it.session.toUri() == action.session.toUri() }