From bdcd8f2ce8c7d9a8f2e2b378f126f24279bf583d Mon Sep 17 00:00:00 2001 From: Tangem Date: Wed, 2 Apr 2025 14:24:40 +0700 Subject: [PATCH] Updated on 2026-08-14 --- .../app/TangemWcBlockchainHelper.kt | 2 +- .../DefaultLegacyWalletConnectRepository.kt | 1 + .../DefaultWalletConnectSessionsRepository.kt | 4 +- .../di/WalletConnectInteractorModule.kt | 1 + .../domain/LegacyWalletConnectRepository.kt | 1 + .../domain/WalletConnectInteractor.kt | 3 + .../domain/WcBlockchainHelper.kt | 2 +- .../domain/WcSessionRequestConverter.kt | 1 + .../domain/models/WalletConnectEvents.kt | 1 + core/datasource/build.gradle.kts | 1 + .../datasource/di/WalletConnectModule.kt | 45 ++++++++++ .../DefaultWalletConnectStore.kt | 23 +++++ .../local/walletconnect/WalletConnectStore.kt | 17 ++++ .../di/WalletConnectDataModule.kt | 18 +++- .../pair/DefaultWcPairUseCase.kt | 2 +- .../sessions/DefaultWcSessionsManager.kt | 88 +++++++++++++++---- .../data/walletconnect/utils/ToOurModel.kt | 14 +++ domain/wallet-connect/models/build.gradle.kts | 5 ++ .../walletconnect/model/WcSessionDTO.kt | 10 +++ .../walletconnect/model/legacy}/Account.kt | 2 +- .../walletconnect/model/legacy}/Session.kt | 2 +- .../WalletConnectSessionsRepository.kt | 4 +- .../model/sdkcopy/WcSdkSession.kt | 1 + .../repository/WcSessionsManager.kt | 4 +- features/walletconnect/impl/build.gradle.kts | 2 + 25 files changed, 224 insertions(+), 30 deletions(-) create mode 100644 core/datasource/src/main/java/com/tangem/datasource/di/WalletConnectModule.kt create mode 100644 core/datasource/src/main/java/com/tangem/datasource/local/walletconnect/DefaultWalletConnectStore.kt create mode 100644 core/datasource/src/main/java/com/tangem/datasource/local/walletconnect/WalletConnectStore.kt create mode 100644 domain/wallet-connect/models/src/main/java/com/tangem/domain/walletconnect/model/WcSessionDTO.kt rename {app/src/main/java/com/tangem/tap/domain/walletconnect2/domain/models => domain/wallet-connect/models/src/main/java/com/tangem/domain/walletconnect/model/legacy}/Account.kt (84%) rename {app/src/main/java/com/tangem/tap/domain/walletconnect2/domain/models => domain/wallet-connect/models/src/main/java/com/tangem/domain/walletconnect/model/legacy}/Session.kt (80%) rename {app/src/main/java/com/tangem/tap/domain/walletconnect2/domain => domain/wallet-connect/models/src/main/java/com/tangem/domain/walletconnect/model/legacy}/WalletConnectSessionsRepository.kt (67%) diff --git a/app/src/main/java/com/tangem/tap/domain/walletconnect2/app/TangemWcBlockchainHelper.kt b/app/src/main/java/com/tangem/tap/domain/walletconnect2/app/TangemWcBlockchainHelper.kt index 95046dfdb8..b0e2c44579 100644 --- a/app/src/main/java/com/tangem/tap/domain/walletconnect2/app/TangemWcBlockchainHelper.kt +++ b/app/src/main/java/com/tangem/tap/domain/walletconnect2/app/TangemWcBlockchainHelper.kt @@ -4,7 +4,7 @@ import com.tangem.blockchain.common.Blockchain import com.tangem.blockchainsdk.utils.fromNetworkId import com.tangem.blockchainsdk.utils.toNetworkId import com.tangem.tap.domain.walletconnect2.domain.WcBlockchainHelper -import com.tangem.tap.domain.walletconnect2.domain.models.Account +import com.tangem.domain.walletconnect.model.legacy.Account internal class TangemWcBlockchainHelper : WcBlockchainHelper { diff --git a/app/src/main/java/com/tangem/tap/domain/walletconnect2/data/DefaultLegacyWalletConnectRepository.kt b/app/src/main/java/com/tangem/tap/domain/walletconnect2/data/DefaultLegacyWalletConnectRepository.kt index 3d1c5dc93f..9b88770588 100644 --- a/app/src/main/java/com/tangem/tap/domain/walletconnect2/data/DefaultLegacyWalletConnectRepository.kt +++ b/app/src/main/java/com/tangem/tap/domain/walletconnect2/data/DefaultLegacyWalletConnectRepository.kt @@ -9,6 +9,7 @@ import com.reown.walletkit.client.Wallet import com.reown.walletkit.client.WalletKit import com.tangem.core.analytics.api.AnalyticsEventHandler import com.tangem.data.walletconnect.pair.unsupportedDApps +import com.tangem.domain.walletconnect.model.legacy.Account import com.tangem.tap.common.analytics.events.WalletConnect import com.tangem.tap.domain.walletconnect2.app.TangemWcBlockchainHelper import com.tangem.tap.domain.walletconnect2.domain.LegacyWalletConnectRepository diff --git a/app/src/main/java/com/tangem/tap/domain/walletconnect2/data/DefaultWalletConnectSessionsRepository.kt b/app/src/main/java/com/tangem/tap/domain/walletconnect2/data/DefaultWalletConnectSessionsRepository.kt index 77b531934a..108492f2de 100644 --- a/app/src/main/java/com/tangem/tap/domain/walletconnect2/data/DefaultWalletConnectSessionsRepository.kt +++ b/app/src/main/java/com/tangem/tap/domain/walletconnect2/data/DefaultWalletConnectSessionsRepository.kt @@ -4,8 +4,8 @@ import com.squareup.moshi.JsonAdapter import com.squareup.moshi.Moshi import com.squareup.moshi.Types import com.tangem.datasource.files.FileReader -import com.tangem.tap.domain.walletconnect2.domain.WalletConnectSessionsRepository -import com.tangem.tap.domain.walletconnect2.domain.models.Session +import com.tangem.domain.walletconnect.model.legacy.WalletConnectSessionsRepository +import com.tangem.domain.walletconnect.model.legacy.Session import timber.log.Timber internal class DefaultWalletConnectSessionsRepository( diff --git a/app/src/main/java/com/tangem/tap/domain/walletconnect2/di/WalletConnectInteractorModule.kt b/app/src/main/java/com/tangem/tap/domain/walletconnect2/di/WalletConnectInteractorModule.kt index 3df4ed99be..3e47ce122d 100644 --- a/app/src/main/java/com/tangem/tap/domain/walletconnect2/di/WalletConnectInteractorModule.kt +++ b/app/src/main/java/com/tangem/tap/domain/walletconnect2/di/WalletConnectInteractorModule.kt @@ -7,6 +7,7 @@ import com.tangem.core.configtoggle.feature.FeatureTogglesManager import com.tangem.datasource.di.SdkMoshi import com.tangem.datasource.files.FileReader import com.tangem.domain.tokens.repository.CurrenciesRepository +import com.tangem.domain.walletconnect.model.legacy.WalletConnectSessionsRepository import com.tangem.domain.walletmanager.WalletManagersFacade import com.tangem.domain.wallets.legacy.UserWalletsListManager import com.tangem.tap.domain.walletconnect.WalletConnectSdkHelper diff --git a/app/src/main/java/com/tangem/tap/domain/walletconnect2/domain/LegacyWalletConnectRepository.kt b/app/src/main/java/com/tangem/tap/domain/walletconnect2/domain/LegacyWalletConnectRepository.kt index 289a2664b5..59d5954e00 100644 --- a/app/src/main/java/com/tangem/tap/domain/walletconnect2/domain/LegacyWalletConnectRepository.kt +++ b/app/src/main/java/com/tangem/tap/domain/walletconnect2/domain/LegacyWalletConnectRepository.kt @@ -1,5 +1,6 @@ package com.tangem.tap.domain.walletconnect2.domain +import com.tangem.domain.walletconnect.model.legacy.Account import com.tangem.tap.domain.walletconnect2.domain.models.* import com.tangem.tap.features.details.redux.walletconnect.WalletConnectAction.OpenSession.SourceType import kotlinx.coroutines.flow.Flow diff --git a/app/src/main/java/com/tangem/tap/domain/walletconnect2/domain/WalletConnectInteractor.kt b/app/src/main/java/com/tangem/tap/domain/walletconnect2/domain/WalletConnectInteractor.kt index 3d11b576d0..d8d5a989ae 100644 --- a/app/src/main/java/com/tangem/tap/domain/walletconnect2/domain/WalletConnectInteractor.kt +++ b/app/src/main/java/com/tangem/tap/domain/walletconnect2/domain/WalletConnectInteractor.kt @@ -6,6 +6,9 @@ import com.tangem.blockchainsdk.utils.toNetworkId import com.tangem.domain.tokens.model.CryptoCurrency import com.tangem.domain.tokens.model.Network import com.tangem.domain.tokens.repository.CurrenciesRepository +import com.tangem.domain.walletconnect.model.legacy.Account +import com.tangem.domain.walletconnect.model.legacy.Session +import com.tangem.domain.walletconnect.model.legacy.WalletConnectSessionsRepository import com.tangem.domain.walletmanager.WalletManagersFacade import com.tangem.domain.wallets.legacy.UserWalletsListManager import com.tangem.domain.wallets.models.UserWallet diff --git a/app/src/main/java/com/tangem/tap/domain/walletconnect2/domain/WcBlockchainHelper.kt b/app/src/main/java/com/tangem/tap/domain/walletconnect2/domain/WcBlockchainHelper.kt index 91771ac6e1..74241e4cf6 100644 --- a/app/src/main/java/com/tangem/tap/domain/walletconnect2/domain/WcBlockchainHelper.kt +++ b/app/src/main/java/com/tangem/tap/domain/walletconnect2/domain/WcBlockchainHelper.kt @@ -1,7 +1,7 @@ package com.tangem.tap.domain.walletconnect2.domain import com.tangem.blockchain.common.Blockchain -import com.tangem.tap.domain.walletconnect2.domain.models.Account +import com.tangem.domain.walletconnect.model.legacy.Account interface WcBlockchainHelper { fun chainIdToNetworkIdOrNull(chainId: String): String? diff --git a/app/src/main/java/com/tangem/tap/domain/walletconnect2/domain/WcSessionRequestConverter.kt b/app/src/main/java/com/tangem/tap/domain/walletconnect2/domain/WcSessionRequestConverter.kt index 909120438b..16eaf90e60 100644 --- a/app/src/main/java/com/tangem/tap/domain/walletconnect2/domain/WcSessionRequestConverter.kt +++ b/app/src/main/java/com/tangem/tap/domain/walletconnect2/domain/WcSessionRequestConverter.kt @@ -1,5 +1,6 @@ package com.tangem.tap.domain.walletconnect2.domain +import com.tangem.domain.walletconnect.model.legacy.WalletConnectSessionsRepository import com.tangem.tap.domain.walletconnect.WalletConnectSdkHelper import com.tangem.tap.domain.walletconnect2.domain.models.BnbData import com.tangem.tap.domain.walletconnect2.domain.models.EthTransactionData diff --git a/app/src/main/java/com/tangem/tap/domain/walletconnect2/domain/models/WalletConnectEvents.kt b/app/src/main/java/com/tangem/tap/domain/walletconnect2/domain/models/WalletConnectEvents.kt index 85e6e06f5b..1298ede44d 100644 --- a/app/src/main/java/com/tangem/tap/domain/walletconnect2/domain/models/WalletConnectEvents.kt +++ b/app/src/main/java/com/tangem/tap/domain/walletconnect2/domain/models/WalletConnectEvents.kt @@ -1,5 +1,6 @@ package com.tangem.tap.domain.walletconnect2.domain.models +import com.tangem.domain.walletconnect.model.legacy.Account import com.tangem.tap.domain.walletconnect2.domain.WcRequest import java.net.URI diff --git a/core/datasource/build.gradle.kts b/core/datasource/build.gradle.kts index 3e2fdba3e3..0efd84702a 100644 --- a/core/datasource/build.gradle.kts +++ b/core/datasource/build.gradle.kts @@ -34,6 +34,7 @@ dependencies { implementation(projects.domain.onramp.models) implementation(projects.domain.models) implementation(projects.domain.nft.models) + implementation(projects.domain.walletConnect.models) /** Tangem libraries */ implementation(tangemDeps.blockchain) diff --git a/core/datasource/src/main/java/com/tangem/datasource/di/WalletConnectModule.kt b/core/datasource/src/main/java/com/tangem/datasource/di/WalletConnectModule.kt new file mode 100644 index 0000000000..4c5f149013 --- /dev/null +++ b/core/datasource/src/main/java/com/tangem/datasource/di/WalletConnectModule.kt @@ -0,0 +1,45 @@ +package com.tangem.datasource.di + +import android.content.Context +import androidx.datastore.core.DataStoreFactory +import androidx.datastore.dataStoreFile +import com.squareup.moshi.Moshi +import com.tangem.datasource.local.walletconnect.DefaultWalletConnectStore +import com.tangem.datasource.local.walletconnect.WalletConnectStore +import com.tangem.datasource.utils.MoshiDataStoreSerializer +import com.tangem.datasource.utils.setTypes +import com.tangem.domain.walletconnect.model.WcSessionDTO +import com.tangem.utils.coroutines.CoroutineDispatcherProvider +import dagger.Module +import dagger.Provides +import dagger.hilt.InstallIn +import dagger.hilt.android.qualifiers.ApplicationContext +import dagger.hilt.components.SingletonComponent +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.SupervisorJob +import javax.inject.Singleton + +@Module +@InstallIn(SingletonComponent::class) +object WalletConnectModule { + + @Provides + @Singleton + fun provideWalletConnectStore( + @NetworkMoshi moshi: Moshi, + @ApplicationContext context: Context, + dispatchers: CoroutineDispatcherProvider, + ): WalletConnectStore { + return DefaultWalletConnectStore( + persistenceStore = DataStoreFactory.create( + serializer = MoshiDataStoreSerializer( + moshi = moshi, + types = setTypes(), + defaultValue = emptySet(), + ), + produceFile = { context.dataStoreFile(fileName = "wallet_connect_sessions") }, + scope = CoroutineScope(context = dispatchers.io + SupervisorJob()), + ), + ) + } +} \ No newline at end of file diff --git a/core/datasource/src/main/java/com/tangem/datasource/local/walletconnect/DefaultWalletConnectStore.kt b/core/datasource/src/main/java/com/tangem/datasource/local/walletconnect/DefaultWalletConnectStore.kt new file mode 100644 index 0000000000..57e97f18d7 --- /dev/null +++ b/core/datasource/src/main/java/com/tangem/datasource/local/walletconnect/DefaultWalletConnectStore.kt @@ -0,0 +1,23 @@ +package com.tangem.datasource.local.walletconnect + +import androidx.datastore.core.DataStore +import com.tangem.domain.walletconnect.model.WcSessionDTO +import kotlinx.coroutines.flow.Flow + +internal typealias WcSessionCollection = Set + +class DefaultWalletConnectStore( + private val persistenceStore: DataStore, +) : WalletConnectStore { + + override val sessions: Flow + get() = persistenceStore.data + + override suspend fun saveSessions(sessions: WcSessionCollection) { + persistenceStore.updateData { data -> data.plus(sessions) } + } + + override suspend fun removeSessions(sessions: WcSessionCollection) { + persistenceStore.updateData { data -> data.minus(sessions) } + } +} \ No newline at end of file diff --git a/core/datasource/src/main/java/com/tangem/datasource/local/walletconnect/WalletConnectStore.kt b/core/datasource/src/main/java/com/tangem/datasource/local/walletconnect/WalletConnectStore.kt new file mode 100644 index 0000000000..4f3a8397b8 --- /dev/null +++ b/core/datasource/src/main/java/com/tangem/datasource/local/walletconnect/WalletConnectStore.kt @@ -0,0 +1,17 @@ +package com.tangem.datasource.local.walletconnect + +import com.tangem.domain.walletconnect.model.WcSessionDTO +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.first + +interface WalletConnectStore { + + val sessions: Flow + suspend fun findSessionByTopic(topic: String) = sessions.first().find { it.topic == topic } + + suspend fun saveSessions(sessions: WcSessionCollection) + suspend fun saveSession(session: WcSessionDTO) = saveSessions(setOf(session)) + + suspend fun removeSessions(sessions: WcSessionCollection) + suspend fun removeSession(session: WcSessionDTO) = removeSessions(setOf(session)) +} \ No newline at end of file diff --git a/data/wallet-connect/src/main/kotlin/com/tangem/data/walletconnect/di/WalletConnectDataModule.kt b/data/wallet-connect/src/main/kotlin/com/tangem/data/walletconnect/di/WalletConnectDataModule.kt index 57dd79d196..edab0d12af 100644 --- a/data/wallet-connect/src/main/kotlin/com/tangem/data/walletconnect/di/WalletConnectDataModule.kt +++ b/data/wallet-connect/src/main/kotlin/com/tangem/data/walletconnect/di/WalletConnectDataModule.kt @@ -18,8 +18,10 @@ import com.tangem.data.walletconnect.sessions.DefaultWcSessionsManager import com.tangem.data.walletconnect.utils.WcNamespaceConverter import com.tangem.datasource.di.SdkMoshi import com.tangem.datasource.local.userwallet.UserWalletsStore +import com.tangem.datasource.local.walletconnect.WalletConnectStore import com.tangem.domain.tokens.repository.CurrenciesRepository import com.tangem.domain.walletconnect.model.WcMethod +import com.tangem.domain.walletconnect.model.legacy.WalletConnectSessionsRepository import com.tangem.domain.walletconnect.repository.WalletConnectRepository import com.tangem.domain.walletconnect.repository.WcSessionsManager import com.tangem.domain.walletconnect.request.WcRequestService @@ -82,7 +84,21 @@ internal object WalletConnectDataModule { @Provides @Singleton - fun defaultWcSessionsManager(): DefaultWcSessionsManager = DefaultWcSessionsManager() + fun defaultWcSessionsManager( + store: WalletConnectStore, + dispatchers: CoroutineDispatcherProvider, + legacyStore: WalletConnectSessionsRepository, + getWallets: GetWalletsUseCase, + ): DefaultWcSessionsManager { + val scope = CoroutineScope(SupervisorJob() + dispatchers.io) + return DefaultWcSessionsManager( + store = store, + dispatchers = dispatchers, + legacyStore = legacyStore, + getWallets = getWallets, + scope = scope, + ) + } @Provides @Singleton diff --git a/data/wallet-connect/src/main/kotlin/com/tangem/data/walletconnect/pair/DefaultWcPairUseCase.kt b/data/wallet-connect/src/main/kotlin/com/tangem/data/walletconnect/pair/DefaultWcPairUseCase.kt index f574bcd29e..590b9acbf6 100644 --- a/data/wallet-connect/src/main/kotlin/com/tangem/data/walletconnect/pair/DefaultWcPairUseCase.kt +++ b/data/wallet-connect/src/main/kotlin/com/tangem/data/walletconnect/pair/DefaultWcPairUseCase.kt @@ -96,7 +96,7 @@ internal class DefaultWcPairUseCase( is Wallet.Model.SettledSessionResponse.Result -> { val newSession = settledSession.session.toDomain(sessionForApprove.walletId) - sessionsManager.saveSessions(sessionForApprove.walletId, newSession) + sessionsManager.saveSession(sessionForApprove.walletId, newSession) newSession.right() } } diff --git a/data/wallet-connect/src/main/kotlin/com/tangem/data/walletconnect/sessions/DefaultWcSessionsManager.kt b/data/wallet-connect/src/main/kotlin/com/tangem/data/walletconnect/sessions/DefaultWcSessionsManager.kt index cfd0a023d0..32df16baba 100644 --- a/data/wallet-connect/src/main/kotlin/com/tangem/data/walletconnect/sessions/DefaultWcSessionsManager.kt +++ b/data/wallet-connect/src/main/kotlin/com/tangem/data/walletconnect/sessions/DefaultWcSessionsManager.kt @@ -1,38 +1,92 @@ package com.tangem.data.walletconnect.sessions import com.reown.walletkit.client.Wallet +import com.reown.walletkit.client.WalletKit import com.tangem.data.walletconnect.utils.WcSdkObserver +import com.tangem.data.walletconnect.utils.toOurModel +import com.tangem.datasource.local.walletconnect.WalletConnectStore import com.tangem.domain.walletconnect.model.WcSession +import com.tangem.domain.walletconnect.model.WcSessionDTO +import com.tangem.domain.walletconnect.model.legacy.WalletConnectSessionsRepository import com.tangem.domain.walletconnect.repository.WcSessionsManager import com.tangem.domain.wallets.models.UserWalletId -import kotlinx.coroutines.channels.BufferOverflow -import kotlinx.coroutines.flow.MutableSharedFlow -import kotlinx.coroutines.flow.distinctUntilChanged +import com.tangem.domain.wallets.usecase.GetWalletsUseCase +import com.tangem.utils.coroutines.CoroutineDispatcherProvider +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.flow.* +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import timber.log.Timber -internal class DefaultWcSessionsManager : WcSessionsManager, WcSdkObserver { +internal class DefaultWcSessionsManager constructor( + private val store: WalletConnectStore, + private val legacyStore: WalletConnectSessionsRepository, + private val getWallets: GetWalletsUseCase, + private val dispatchers: CoroutineDispatcherProvider, + private val scope: CoroutineScope, +) : WcSessionsManager, WcSdkObserver { - private val _sessions = MutableSharedFlow>>( - replay = 1, - onBufferOverflow = BufferOverflow.DROP_OLDEST, - ) - override val sessions get() = _sessions.distinctUntilChanged() + override val sessions: Flow>> + get() = store.sessions + .onEach(::migrateLegacyStore) + .map(::associateWithSdk) + .distinctUntilChanged() + .flowOn(dispatchers.io) - override suspend fun saveSessions(userWalletId: UserWalletId, session: WcSession) { - TODO("Not yet implemented") + override suspend fun saveSession(userWalletId: UserWalletId, session: WcSession) { + store.saveSession(WcSessionDTO(session.sdkModel.topic, session.userWalletId)) } - override suspend fun removeSessions(userWalletId: UserWalletId, session: WcSession) { - TODO("Not yet implemented") + override suspend fun removeSession(userWalletId: UserWalletId, session: WcSession) { + store.removeSession(WcSessionDTO(session.sdkModel.topic, session.userWalletId)) } - override suspend fun findSessionByTopic(topic: String): WcSession? { - TODO("Not yet implemented") + override suspend fun findSessionByTopic(topic: String): WcSession? = withContext(dispatchers.io) { + val storedSessions = store.findSessionByTopic(topic) ?: return@withContext null + val sdkSession = WalletKit.getActiveSessionByTopic(topic) ?: return@withContext null + WcSession(userWalletId = storedSessions.walletId, sdkModel = sdkSession.toOurModel()) } override fun onSessionDelete(sessionDelete: Wallet.Model.SessionDelete) { - // Triggered when the session is deleted by the peer + if (sessionDelete !is Wallet.Model.SessionDelete.Success) return Timber.i("onSessionDelete: $sessionDelete") - // todo (wc) find session, delete and update sessions Flow + scope.launch { + val storedSessions = store.findSessionByTopic(sessionDelete.topic) ?: return@launch + store.removeSession(storedSessions) + } + } + + private suspend fun migrateLegacyStore(inNewStoreSessions: Set) { + val walletIds = getWallets.invokeSync().mapTo(mutableSetOf()) { it.walletId } + val inLegacyStoreSessions = walletIds + .map { walletId -> + flow { emit(legacyStore.loadSessions(walletId.stringValue).map { WcSessionDTO(it.topic, walletId) }) } + } + .merge() + .first() + + val mustSaveInNewStore = inLegacyStoreSessions.subtract(inNewStoreSessions) + if (mustSaveInNewStore.isNotEmpty()) store.saveSessions(mustSaveInNewStore) + } + + private suspend fun associateWithSdk(storeSessions: Set): Map> { + val sdkSessions = WalletKit.getListOfActiveSessions() + val wcSessions = sdkSessions.mapNotNull { sdkSession -> + val storedSessions = storeSessions.find { it.topic == sdkSession.topic } + ?: return@mapNotNull null + WcSession(userWalletId = storedSessions.walletId, sdkModel = sdkSession.toOurModel()) + } + + val unknownStoredSessions = storeSessions + .filterNot { dto -> wcSessions.any { it.sdkModel.topic == dto.topic } } + + if (unknownStoredSessions.isNotEmpty()) { + unknownStoredSessions.forEach { unknown -> + legacyStore.removeSession(unknown.walletId.stringValue, unknown.topic) + } + store.removeSessions(unknownStoredSessions.toSet()) + } + + return wcSessions.groupBy { it.userWalletId } } } \ No newline at end of file diff --git a/data/wallet-connect/src/main/kotlin/com/tangem/data/walletconnect/utils/ToOurModel.kt b/data/wallet-connect/src/main/kotlin/com/tangem/data/walletconnect/utils/ToOurModel.kt index 5da50ba3c4..de7c1ff78d 100644 --- a/data/wallet-connect/src/main/kotlin/com/tangem/data/walletconnect/utils/ToOurModel.kt +++ b/data/wallet-connect/src/main/kotlin/com/tangem/data/walletconnect/utils/ToOurModel.kt @@ -1,12 +1,15 @@ package com.tangem.data.walletconnect.utils +import com.reown.android.Core import com.reown.walletkit.client.Wallet +import com.tangem.domain.walletconnect.model.sdkcopy.WcAppMetaData import com.tangem.domain.walletconnect.model.sdkcopy.WcSdkSession import com.tangem.domain.walletconnect.model.sdkcopy.WcSdkSessionRequest import com.tangem.domain.walletconnect.model.sdkcopy.WcSdkSessionRequest.JSONRPCRequest internal fun Wallet.Model.Session.toOurModel(): WcSdkSession = WcSdkSession( topic = topic, + appMetaData = metaData.toOurModel(), ) internal fun Wallet.Model.SessionRequest.toOurModel(): WcSdkSessionRequest = WcSdkSessionRequest( @@ -19,4 +22,15 @@ internal fun Wallet.Model.SessionRequest.JSONRPCRequest.toOurModel(): JSONRPCReq id = this.id, method = this.method, params = this.params, +) + +internal fun Core.Model.AppMetaData?.toOurModel(): WcAppMetaData = WcAppMetaData( + name = this?.name.orEmpty(), + description = this?.description.orEmpty(), + url = this?.url.orEmpty(), + icons = this?.icons.orEmpty(), + redirect = this?.redirect, + appLink = this?.appLink, + linkMode = this?.linkMode ?: false, + verifyUrl = this?.verifyUrl, ) \ No newline at end of file diff --git a/domain/wallet-connect/models/build.gradle.kts b/domain/wallet-connect/models/build.gradle.kts index 47fbf86d0b..c471057d7c 100644 --- a/domain/wallet-connect/models/build.gradle.kts +++ b/domain/wallet-connect/models/build.gradle.kts @@ -1,5 +1,6 @@ plugins { alias(deps.plugins.kotlin.jvm) + alias(deps.plugins.ksp) id("configuration") } dependencies { @@ -7,4 +8,8 @@ dependencies { /* Domain */ implementation(projects.domain.wallets.models) implementation(projects.domain.tokens.models) + + /* Other */ + implementation(deps.moshi) + ksp(deps.moshi.kotlin.codegen) } \ No newline at end of file diff --git a/domain/wallet-connect/models/src/main/java/com/tangem/domain/walletconnect/model/WcSessionDTO.kt b/domain/wallet-connect/models/src/main/java/com/tangem/domain/walletconnect/model/WcSessionDTO.kt new file mode 100644 index 0000000000..63a946b9c6 --- /dev/null +++ b/domain/wallet-connect/models/src/main/java/com/tangem/domain/walletconnect/model/WcSessionDTO.kt @@ -0,0 +1,10 @@ +package com.tangem.domain.walletconnect.model + +import com.squareup.moshi.JsonClass +import com.tangem.domain.wallets.models.UserWalletId + +@JsonClass(generateAdapter = true) +data class WcSessionDTO( + val topic: String, + val walletId: UserWalletId, +) \ No newline at end of file diff --git a/app/src/main/java/com/tangem/tap/domain/walletconnect2/domain/models/Account.kt b/domain/wallet-connect/models/src/main/java/com/tangem/domain/walletconnect/model/legacy/Account.kt similarity index 84% rename from app/src/main/java/com/tangem/tap/domain/walletconnect2/domain/models/Account.kt rename to domain/wallet-connect/models/src/main/java/com/tangem/domain/walletconnect/model/legacy/Account.kt index 48114a57c4..8c370b9fbd 100644 --- a/app/src/main/java/com/tangem/tap/domain/walletconnect2/domain/models/Account.kt +++ b/domain/wallet-connect/models/src/main/java/com/tangem/domain/walletconnect/model/legacy/Account.kt @@ -1,4 +1,4 @@ -package com.tangem.tap.domain.walletconnect2.domain.models +package com.tangem.domain.walletconnect.model.legacy import com.squareup.moshi.Json import com.squareup.moshi.JsonClass diff --git a/app/src/main/java/com/tangem/tap/domain/walletconnect2/domain/models/Session.kt b/domain/wallet-connect/models/src/main/java/com/tangem/domain/walletconnect/model/legacy/Session.kt similarity index 80% rename from app/src/main/java/com/tangem/tap/domain/walletconnect2/domain/models/Session.kt rename to domain/wallet-connect/models/src/main/java/com/tangem/domain/walletconnect/model/legacy/Session.kt index 6950d29486..5ca9f80ed4 100644 --- a/app/src/main/java/com/tangem/tap/domain/walletconnect2/domain/models/Session.kt +++ b/domain/wallet-connect/models/src/main/java/com/tangem/domain/walletconnect/model/legacy/Session.kt @@ -1,4 +1,4 @@ -package com.tangem.tap.domain.walletconnect2.domain.models +package com.tangem.domain.walletconnect.model.legacy import com.squareup.moshi.Json import com.squareup.moshi.JsonClass diff --git a/app/src/main/java/com/tangem/tap/domain/walletconnect2/domain/WalletConnectSessionsRepository.kt b/domain/wallet-connect/models/src/main/java/com/tangem/domain/walletconnect/model/legacy/WalletConnectSessionsRepository.kt similarity index 67% rename from app/src/main/java/com/tangem/tap/domain/walletconnect2/domain/WalletConnectSessionsRepository.kt rename to domain/wallet-connect/models/src/main/java/com/tangem/domain/walletconnect/model/legacy/WalletConnectSessionsRepository.kt index 395b928f86..dce7f7f080 100644 --- a/app/src/main/java/com/tangem/tap/domain/walletconnect2/domain/WalletConnectSessionsRepository.kt +++ b/domain/wallet-connect/models/src/main/java/com/tangem/domain/walletconnect/model/legacy/WalletConnectSessionsRepository.kt @@ -1,6 +1,4 @@ -package com.tangem.tap.domain.walletconnect2.domain - -import com.tangem.tap.domain.walletconnect2.domain.models.Session +package com.tangem.domain.walletconnect.model.legacy interface WalletConnectSessionsRepository { suspend fun loadSessions(userWallet: String): List diff --git a/domain/wallet-connect/models/src/main/java/com/tangem/domain/walletconnect/model/sdkcopy/WcSdkSession.kt b/domain/wallet-connect/models/src/main/java/com/tangem/domain/walletconnect/model/sdkcopy/WcSdkSession.kt index bd88f8ea98..9b2d6c45b7 100644 --- a/domain/wallet-connect/models/src/main/java/com/tangem/domain/walletconnect/model/sdkcopy/WcSdkSession.kt +++ b/domain/wallet-connect/models/src/main/java/com/tangem/domain/walletconnect/model/sdkcopy/WcSdkSession.kt @@ -5,4 +5,5 @@ package com.tangem.domain.walletconnect.model.sdkcopy */ data class WcSdkSession( val topic: String, + val appMetaData: WcAppMetaData, ) \ No newline at end of file diff --git a/domain/wallet-connect/src/main/kotlin/com/tangem/domain/walletconnect/repository/WcSessionsManager.kt b/domain/wallet-connect/src/main/kotlin/com/tangem/domain/walletconnect/repository/WcSessionsManager.kt index 4d2d837da0..7deafa02b2 100644 --- a/domain/wallet-connect/src/main/kotlin/com/tangem/domain/walletconnect/repository/WcSessionsManager.kt +++ b/domain/wallet-connect/src/main/kotlin/com/tangem/domain/walletconnect/repository/WcSessionsManager.kt @@ -6,7 +6,7 @@ import kotlinx.coroutines.flow.Flow interface WcSessionsManager { val sessions: Flow>> - suspend fun saveSessions(userWalletId: UserWalletId, session: WcSession) - suspend fun removeSessions(userWalletId: UserWalletId, session: WcSession) + suspend fun saveSession(userWalletId: UserWalletId, session: WcSession) + suspend fun removeSession(userWalletId: UserWalletId, session: WcSession) suspend fun findSessionByTopic(topic: String): WcSession? } \ No newline at end of file diff --git a/features/walletconnect/impl/build.gradle.kts b/features/walletconnect/impl/build.gradle.kts index 0fb71c954e..e85bbfc291 100644 --- a/features/walletconnect/impl/build.gradle.kts +++ b/features/walletconnect/impl/build.gradle.kts @@ -13,6 +13,8 @@ android { dependencies { implementation(projects.features.walletconnect.api) + implementation(projects.domain.walletConnect) + implementation(projects.domain.walletConnect.models) /** Core */ implementation(projects.core.configToggles)