diff --git a/app/src/main/java/com/tangem/tap/domain/TapWalletManager.kt b/app/src/main/java/com/tangem/tap/domain/TapWalletManager.kt index 4e7d1f84b4..360c6f1b07 100644 --- a/app/src/main/java/com/tangem/tap/domain/TapWalletManager.kt +++ b/app/src/main/java/com/tangem/tap/domain/TapWalletManager.kt @@ -6,7 +6,6 @@ import com.tangem.commands.common.card.CardStatus import com.tangem.commands.common.network.Result import com.tangem.tap.common.analytics.AnalyticsEvent import com.tangem.tap.common.analytics.FirebaseAnalyticsHandler -import com.tangem.tap.common.redux.global.CryptoCurrencyName import com.tangem.tap.common.redux.global.FiatCurrencyName import com.tangem.tap.common.redux.global.GlobalAction import com.tangem.tap.currenciesRepository @@ -150,11 +149,7 @@ class TapWalletManager { val config = store.state.globalState.configManager?.config ?: return@withContext val blockchain = data.card.getBlockchain() - val primaryWalletManager = if (blockchain != null) { - walletManagerFactory.makeWalletManagerForApp(data.card, blockchain) - } else { - null - } + val primaryWalletManager = walletManagerFactory.makePrimaryWalletManager(data) if (blockchain != null && primaryWalletManager != null) { val primaryToken = data.card.getToken() diff --git a/app/src/main/java/com/tangem/tap/domain/extensions/WalletManagerFactory.kt b/app/src/main/java/com/tangem/tap/domain/extensions/WalletManagerFactory.kt index f09492cccd..8b985f728f 100644 --- a/app/src/main/java/com/tangem/tap/domain/extensions/WalletManagerFactory.kt +++ b/app/src/main/java/com/tangem/tap/domain/extensions/WalletManagerFactory.kt @@ -6,6 +6,9 @@ import com.tangem.blockchain.common.WalletManagerFactory import com.tangem.commands.common.card.Card import com.tangem.commands.common.card.EllipticCurve import com.tangem.commands.wallet.CardWallet +import com.tangem.common.extensions.hexToBytes +import com.tangem.tap.domain.tasks.ScanNoteResponse +import com.tangem.tap.domain.twins.isTwinCard fun WalletManagerFactory.makeWalletManagerForApp( card: Card, @@ -32,3 +35,24 @@ fun WalletManagerFactory.makeWalletManagersForApp( ): List { return blockchains.mapNotNull { blockchain -> makeWalletManagerForApp(card, blockchain) } } + +fun WalletManagerFactory.makePrimaryWalletManager( + data: ScanNoteResponse, +): WalletManager? { + val card = data.card + val blockchain = card.getBlockchain() + val supportedCurves = blockchain?.getSupportedCurves() ?: return null + val wallets = card.getWallets().filter { wallet -> supportedCurves.contains(wallet.curve) } + val wallet = selectWallet(wallets) + val publicKey = wallet?.publicKey ?: return null + val curveToUse = wallet.curve ?: return null + return if (card.isTwinCard() && data.secondTwinPublicKey != null) { + makeMultisigWalletManager( + cardId = card.cardId, + walletPublicKey = publicKey, pairPublicKey = data.secondTwinPublicKey.hexToBytes(), + blockchain = blockchain, curve = curveToUse + ) + } else { + makeWalletManager(card.cardId, publicKey, blockchain, curveToUse) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/tangem/tap/domain/tasks/CreateWalletAndRescanTask.kt b/app/src/main/java/com/tangem/tap/domain/tasks/CreateWalletAndRescanTask.kt index 735cc1de64..19b87612a0 100644 --- a/app/src/main/java/com/tangem/tap/domain/tasks/CreateWalletAndRescanTask.kt +++ b/app/src/main/java/com/tangem/tap/domain/tasks/CreateWalletAndRescanTask.kt @@ -35,6 +35,4 @@ class CreateWalletAndRescanTask : CardSessionRunnable, PreflightReadCapabl } } } - - } \ No newline at end of file diff --git a/app/src/main/java/com/tangem/tap/domain/tasks/ScanNoteTask.kt b/app/src/main/java/com/tangem/tap/domain/tasks/ScanNoteTask.kt index f252714774..c7a8a6cb8e 100644 --- a/app/src/main/java/com/tangem/tap/domain/tasks/ScanNoteTask.kt +++ b/app/src/main/java/com/tangem/tap/domain/tasks/ScanNoteTask.kt @@ -44,7 +44,7 @@ class ScanNoteTask(val card: Card? = null) : CardSessionRunnable { override val requiresPin2 = false - override fun run(session: CardSession, callback: (result: CompletionResult) -> Unit) { + override fun run( + session: CardSession, + callback: (result: CompletionResult) -> Unit, + ) { if (session.environment.card?.getSingleWallet()?.publicKey != null) { PurgeWalletCommand(TangemSdkConstants.getDefaultWalletIndex()).run(session) { response -> when (response) { is CompletionResult.Success -> { - session.environment.card = session.environment.card?.copy(status = CardStatus.Empty) + session.environment.card = session.environment.card?.changeStatusToEmpty() CreateWalletTask().run(session) { callback(it) } } is CompletionResult.Failure -> callback(CompletionResult.Failure(response.error)) diff --git a/app/src/main/java/com/tangem/tap/domain/twins/CreateSecondTwinWalletTask.kt b/app/src/main/java/com/tangem/tap/domain/twins/CreateSecondTwinWalletTask.kt index 9946e37d07..c8e936bcce 100644 --- a/app/src/main/java/com/tangem/tap/domain/twins/CreateSecondTwinWalletTask.kt +++ b/app/src/main/java/com/tangem/tap/domain/twins/CreateSecondTwinWalletTask.kt @@ -3,7 +3,6 @@ package com.tangem.tap.domain.twins import com.tangem.CardSession import com.tangem.CardSessionRunnable import com.tangem.Message -import com.tangem.commands.common.card.CardStatus import com.tangem.commands.wallet.CreateWalletResponse import com.tangem.commands.wallet.PurgeWalletCommand import com.tangem.common.CompletionResult @@ -26,8 +25,7 @@ class CreateSecondTwinWalletTask( PurgeWalletCommand(TangemSdkConstants.getDefaultWalletIndex()).run(session) { response -> when (response) { is CompletionResult.Success -> { - session.environment.card = - session.environment.card?.copy(status = CardStatus.Empty) + session.environment.card = session.environment.card?.changeStatusToEmpty() finishTask(session, callback) } is CompletionResult.Failure -> callback(CompletionResult.Failure(response.error)) @@ -43,8 +41,8 @@ class CreateSecondTwinWalletTask( CreateWalletTask().run(session) { result -> when (result) { is CompletionResult.Success -> { - session.environment.card = - session.environment.card?.copy(status = CardStatus.Loaded) + session.environment.card = session.environment.card?.changeStatusToLoaded() + WriteProtectedIssuerDataTask( firstPublicKey.hexToBytes(), TwinCardsManager.issuerKeys ).run(session) { writeResult -> diff --git a/app/src/main/java/com/tangem/tap/domain/twins/FinalizeTwinTask.kt b/app/src/main/java/com/tangem/tap/domain/twins/FinalizeTwinTask.kt index e8454b6756..8eb0315241 100644 --- a/app/src/main/java/com/tangem/tap/domain/twins/FinalizeTwinTask.kt +++ b/app/src/main/java/com/tangem/tap/domain/twins/FinalizeTwinTask.kt @@ -3,27 +3,32 @@ package com.tangem.tap.domain.twins import com.tangem.CardSession import com.tangem.CardSessionRunnable import com.tangem.KeyPair -import com.tangem.commands.read.ReadCommand import com.tangem.common.CompletionResult import com.tangem.tap.domain.tasks.ScanNoteResponse import com.tangem.tap.domain.tasks.ScanNoteTask +import com.tangem.tasks.PreflightReadSettings +import com.tangem.tasks.PreflightReadTask class FinalizeTwinTask( - private val twinPublicKey: ByteArray, private val issuerKeys: KeyPair - ) : CardSessionRunnable { + private val twinPublicKey: ByteArray, private val issuerKeys: KeyPair, +) : CardSessionRunnable { override val requiresPin2 = true - override fun run(session: CardSession, callback: (result: CompletionResult) -> Unit) { + override fun run( + session: CardSession, + callback: (result: CompletionResult) -> Unit, + ) { WriteProtectedIssuerDataTask(twinPublicKey, issuerKeys).run(session) { result -> when (result) { - is CompletionResult.Success -> { - ReadCommand().run(session) { readResult -> + is CompletionResult.Success -> + PreflightReadTask(PreflightReadSettings.FullCardRead).run(session) { readResult -> when (readResult) { - is CompletionResult.Success -> ScanNoteTask(readResult.data).run(session, callback) - is CompletionResult.Failure -> callback(CompletionResult.Failure(readResult.error)) + is CompletionResult.Success -> + ScanNoteTask(readResult.data).run(session, callback) + is CompletionResult.Failure -> + callback(CompletionResult.Failure(readResult.error)) } } - } is CompletionResult.Failure -> callback(CompletionResult.Failure(result.error)) } } diff --git a/app/src/main/java/com/tangem/tap/domain/twins/TwinsHelper.kt b/app/src/main/java/com/tangem/tap/domain/twins/TwinsHelper.kt index a1ca7dcc53..9140cf5321 100644 --- a/app/src/main/java/com/tangem/tap/domain/twins/TwinsHelper.kt +++ b/app/src/main/java/com/tangem/tap/domain/twins/TwinsHelper.kt @@ -1,7 +1,9 @@ package com.tangem.tap.domain.twins import com.tangem.commands.common.card.Card +import com.tangem.commands.common.card.CardStatus import com.tangem.commands.common.card.masks.Product +import com.tangem.commands.wallet.WalletStatus import com.tangem.tap.common.extensions.isEven class TwinsHelper { @@ -53,16 +55,16 @@ class TwinsHelper { private fun String.calculateLuhn(): Int { val checksum = this.reversed() - .mapIndexed { index, c -> - val digit = if (c in '0'..'9') c - '0' else c - 'A' - if (!index.isEven()) { - digit - } else { - val newDigit = digit * 2 - if (newDigit >= 10) newDigit - 9 else newDigit - } - }.sum() - .rem(10) + .mapIndexed { index, c -> + val digit = if (c in '0'..'9') c - '0' else c - 'A' + if (!index.isEven()) { + digit + } else { + val newDigit = digit * 2 + if (newDigit >= 10) newDigit - 9 else newDigit + } + }.sum() + .rem(10) return (10 - checksum) % 10 } @@ -81,4 +83,16 @@ fun Card.isTwinCard(): Boolean { fun Card.getTwinCardIdForUser(): String { return TwinsHelper.getTwinCardIdForUser(this.cardId) +} + +fun Card.changeStatusToLoaded(): Card { + val wallets = getWallets().map { it.copy(status = WalletStatus.Loaded) } + return copy(status = CardStatus.Loaded) + .also { it.setWallets(wallets) } +} + +fun Card.changeStatusToEmpty(): Card { + val wallets = getWallets().map { it.copy(status = WalletStatus.Empty) } + return copy(status = CardStatus.Empty) + .also { it.setWallets(wallets) } } \ No newline at end of file