Updated on 2026-08-14

This commit is contained in:
Tangem 2023-03-08 19:16:58 +03:00
parent dc383831d3
commit 6b95243f9a
4 changed files with 36 additions and 14 deletions

View file

@ -59,12 +59,19 @@ class DerivationManagerImpl(
}
}
override fun hasDerivation(networkId: String): Boolean {
override fun getDerivationPathForBlockchain(networkId: String): String? {
val scanResponse = appStateHolder.scanResponse
val blockchain = Blockchain.fromNetworkId(networkId)
if (scanResponse != null && blockchain != null) {
return blockchain.derivationPath(appStateHolder.getActualCard()?.derivationStyle)?.rawPath
}
return null
}
override fun hasDerivation(networkId: String, derivationPath: String): Boolean {
val scanResponse = appStateHolder.scanResponse
val blockchain = Blockchain.fromNetworkId(networkId)
if (scanResponse != null && blockchain != null) {
val derivationPath = blockchain.derivationPath(appStateHolder.getActualCard()?.derivationStyle)?.rawPath
if (derivationPath.isNullOrEmpty()) return false
return scanResponse.hasDerivation(
blockchain,
derivationPath,

View file

@ -93,9 +93,13 @@ class UserWalletManagerImpl(
override suspend fun isTokenAdded(currency: Currency, derivationPath: String?): Boolean {
val blockchain = requireNotNull(Blockchain.fromNetworkId(currency.networkId)) { "blockchain not found" }
val walletManager = getActualWalletManager(blockchain, derivationPath)
return walletManager.cardTokens.any {
it.id == currency.id
return try {
val walletManager = getActualWalletManager(blockchain, derivationPath)
walletManager.cardTokens.any {
it.id == currency.id
}
} catch (e: IllegalArgumentException) {
false
}
}
@ -191,6 +195,7 @@ class UserWalletManagerImpl(
appStateHolder.mainStore?.dispatchOnMain(WalletAction.LoadData.Refresh)
}
@kotlin.jvm.Throws(IllegalArgumentException::class)
private fun getActualWalletManager(blockchain: Blockchain, derivationPath: String?): WalletManager {
val blockchainNetwork = BlockchainNetwork(blockchain, derivationPath, emptyList())
return requireNotNull(appStateHolder.walletState?.getWalletManager(blockchainNetwork)) {

View file

@ -25,8 +25,8 @@ internal class ReferralInteractorImpl(
override suspend fun startReferral(): ReferralData {
if (tokensForReferral.isNotEmpty()) {
val currency = tokensConverter.convert(tokensForReferral.first())
deriveOrAddTokens(currency)
val publicAddress = userWalletManager.getWalletAddress(currency.networkId, null)
val derivationPath = deriveOrAddTokens(currency)
val publicAddress = userWalletManager.getWalletAddress(currency.networkId, derivationPath)
return repository.startReferral(
walletId = userWalletManager.getWalletId(),
networkId = currency.networkId,
@ -38,13 +38,16 @@ internal class ReferralInteractorImpl(
}
}
private suspend fun deriveOrAddTokens(currency: Currency) {
if (!derivationManager.hasDerivation(currency.networkId)) {
private suspend fun deriveOrAddTokens(currency: Currency): String {
val derivationPath = derivationManager.getDerivationPathForBlockchain(currency.networkId)
if (derivationPath.isNullOrEmpty()) error("derivationPath shouldn't be empty")
if (!derivationManager.hasDerivation(currency.networkId, derivationPath)) {
derivationManager.deriveMissingBlockchains(currency)
}
if (!userWalletManager.isTokenAdded(currency, null)) {
userWalletManager.addToken(currency, null)
if (!userWalletManager.isTokenAdded(currency, derivationPath)) {
userWalletManager.addToken(currency, derivationPath)
}
return derivationPath
}
private fun saveRefTokens(tokens: List<TokenData>) {

View file

@ -12,7 +12,14 @@ interface DerivationManager {
suspend fun deriveMissingBlockchains(currency: Currency): Boolean
/**
* Checks that given [networkId] has derivations
* Returns derivationPath or null for blockchain with given networkId
*
* @param networkId
*/
fun hasDerivation(networkId: String): Boolean
fun getDerivationPathForBlockchain(networkId: String): String?
/**
* Checks that given [networkId] has derivations for [derivationPath]
*/
fun hasDerivation(networkId: String, derivationPath: String): Boolean
}