Updated on 2026-08-14

This commit is contained in:
Tangem 2026-02-06 10:58:30 +03:00
parent 6ed3317da4
commit e4b1bf4e61
4 changed files with 35 additions and 14 deletions

View file

@ -69,7 +69,7 @@ internal class WcEthAddNetworkUseCase @AssistedInject constructor(
?: return illegalState()
// find and add all derivation
val networkToAddCAIP10 = networksConverter
.allAddressForChain(networkToAddCAIP2.raw, wallet)
.allAddressForChain(networkToAddCAIP2.raw, wallet, session.account)
.map { address -> CAIP10(networkToAddCAIP2, address).raw }
val newNamespaces = namespaces.copy(
chains = namespaces.chains.plus(networkToAddCAIP2.raw),
@ -134,7 +134,11 @@ internal class WcEthAddSwitchCommonDelegate @AssistedInject constructor(
if (generalNetwork == null) {
return HandleMethodError.TangemUnsupportedNetwork(caip2.raw).left()
}
val addedNetwork = networksConverter.mainOrAnyWalletNetworkForRequest(caip2.raw, wallet)
val addedNetwork = networksConverter.mainOrAnyWalletNetworkForRequest(
rawChainId = caip2.raw,
wallet = wallet,
account = context.session.account,
)
if (addedNetwork == null) {
return HandleMethodError.NotAddedNetwork(generalNetwork.name).left()
}

View file

@ -39,12 +39,17 @@ internal class WcEthNetwork(
val name = toWcMethodName(request) ?: return error("Unknown method name")
val session = sessionsManager.findSessionByTopic(request.topic)
?: return HandleMethodError.UnknownSession.left()
val account = session.account
val wallet = session.wallet
val chainId = request.chainId.orEmpty()
val method: WcEthMethod = name.toMethod(request)
.getOrElse { return error(it.message.orEmpty()) }
?: return error("Failed to parse $name")
suspend fun anyExistNetwork() = networksConverter.mainOrAnyWalletNetworkForRequest(chainId, wallet)
suspend fun anyExistNetwork() = networksConverter.mainOrAnyWalletNetworkForRequest(
rawChainId = chainId,
wallet = wallet,
account = account,
)
val accountAddress = when (method) {
is WcEthMethod.MessageSign -> method.account
@ -69,12 +74,13 @@ internal class WcEthNetwork(
-> anyExistNetwork()
} ?: return error("Failed to find walletNetwork for accountAddress $accountAddress")
val networkDerivationsCount = networksConverter.filterWalletNetworkForRequest(chainId, wallet, account).size
val context = WcMethodUseCaseContext(
session = session,
rawSdkRequest = request,
network = walletNetwork,
accountAddress = accountAddress,
networkDerivationsCount = networksConverter.filterWalletNetworkForRequest(chainId, wallet).size,
networkDerivationsCount = networkDerivationsCount,
)
return when (method) {
is WcEthMethod.MessageSign -> factories.messageSign.create(context, method)

View file

@ -47,8 +47,9 @@ internal class WcSolanaNetwork(
val session = sessionsManager.findSessionByTopic(request.topic)
?: return HandleMethodError.UnknownSession.left()
val wallet = session.wallet
val account = session.account
val chainId = request.chainId.orEmpty()
suspend fun anyExistNetwork() = networksConverter.mainOrAnyWalletNetworkForRequest(chainId, wallet)
suspend fun anyExistNetwork() = networksConverter.mainOrAnyWalletNetworkForRequest(chainId, wallet, account)
suspend fun anyAddress() = anyExistNetwork()
?.let { network -> networksConverter.getAddressForWC(wallet.walletId, network).orEmpty() }
.orEmpty()
@ -63,12 +64,13 @@ internal class WcSolanaNetwork(
?: anyExistNetwork()
?: return error("Failed to find walletNetwork for accountAddress $accountAddress")
val networkDerivationsCount = networksConverter.filterWalletNetworkForRequest(chainId, wallet, account).size
val context = WcMethodUseCaseContext(
session = session,
rawSdkRequest = request,
network = walletNetwork,
accountAddress = accountAddress,
networkDerivationsCount = networksConverter.filterWalletNetworkForRequest(chainId, wallet).size,
networkDerivationsCount = networkDerivationsCount,
)
return when (method) {
is WcSolanaMethod.SignMessage -> factories.messageSign.create(context, method)

View file

@ -44,7 +44,11 @@ internal class WcNetworksConverter @Inject constructor(
requestAddress: String,
): Network? {
val wallet = session.wallet
val allCoinNetwork = filterWalletNetworkForRequest(request.chainId.orEmpty(), session.wallet)
val allCoinNetwork = filterWalletNetworkForRequest(
rawChainId = request.chainId.orEmpty(),
wallet = session.wallet,
account = session.account,
)
val requestNetwork = allCoinNetwork.find { network ->
val address = getAddressForWC(wallet.walletId, network)
@ -56,13 +60,13 @@ internal class WcNetworksConverter @Inject constructor(
/**
* return network with not custom derivationPath or first custom or any
*/
suspend fun mainOrAnyWalletNetworkForRequest(rawChainId: String, wallet: UserWallet): Network? {
val networks = filterWalletNetworkForRequest(rawChainId, wallet)
suspend fun mainOrAnyWalletNetworkForRequest(rawChainId: String, wallet: UserWallet, account: Account?): Network? {
val networks = filterWalletNetworkForRequest(rawChainId, wallet, account)
return networks.firstOrNull { !isCustomCoin(it) } ?: networks.firstOrNull()
}
suspend fun allAddressForChain(rawChainId: String, wallet: UserWallet): List<String> {
return filterWalletNetworkForRequest(rawChainId, wallet)
suspend fun allAddressForChain(rawChainId: String, wallet: UserWallet, account: Account?): List<String> {
return filterWalletNetworkForRequest(rawChainId, wallet, account)
.mapNotNull { getAddressForWC(wallet.walletId, it)?.lowercase() }
}
@ -80,13 +84,18 @@ internal class WcNetworksConverter @Inject constructor(
/**
* return all exist derivation networks
*/
suspend fun filterWalletNetworkForRequest(rawChainId: String, wallet: UserWallet): List<Network> {
val walletNetworks = getWalletNetworks(wallet.walletId)
suspend fun filterWalletNetworkForRequest(
rawChainId: String,
wallet: UserWallet,
account: Account?,
): List<Network> {
val portfolioNetworks = account?.let { getAccountNetworks(it.accountId) }
?: getWalletNetworks(wallet.walletId)
val blockchain = namespaceConverters
.firstNotNullOfOrNull { it.toBlockchain(rawChainId) } ?: return listOf()
val allCoinNetwork = walletNetworks.filter { it.rawId == blockchain.id }
val allCoinNetwork = portfolioNetworks.filter { it.rawId == blockchain.id }
return allCoinNetwork
}