From a74007d920e8867c35462f12e91ecf776cb4b78f Mon Sep 17 00:00:00 2001 From: Tangem Date: Wed, 10 Aug 2022 20:21:14 +0300 Subject: [PATCH] Updated on 2026-08-14 --- .../domain/tasks/product/ScanProductTask.kt | 105 +++++++----------- 1 file changed, 43 insertions(+), 62 deletions(-) diff --git a/app/src/main/java/com/tangem/tap/domain/tasks/product/ScanProductTask.kt b/app/src/main/java/com/tangem/tap/domain/tasks/product/ScanProductTask.kt index 7f1c152bba..e54c3744cb 100644 --- a/app/src/main/java/com/tangem/tap/domain/tasks/product/ScanProductTask.kt +++ b/app/src/main/java/com/tangem/tap/domain/tasks/product/ScanProductTask.kt @@ -101,11 +101,11 @@ private class ScanNoteProcessor : ProductCommandProcessor { callback( CompletionResult.Success( ScanResponse( - card, - ProductType.Note, - session.environment.walletData - ) - ) + card = card, + productType = ProductType.Note, + walletData = session.environment.walletData, + ), + ), ) } } @@ -116,19 +116,17 @@ private class ScanWalletProcessor( ) : ProductCommandProcessor { var primaryCard: PrimaryCard? = null - override fun proceed( card: Card, session: CardSession, - callback: (result: CompletionResult) -> Unit + callback: (result: CompletionResult) -> Unit, ) { createMissingWalletsIfNeeded(card, session, callback) } - private fun createMissingWalletsIfNeeded( card: Card, session: CardSession, - callback: (result: CompletionResult) -> Unit + callback: (result: CompletionResult) -> Unit, ) { if (card.wallets.isEmpty() || card.firmwareVersion < FirmwareVersion.MultiWalletAvailable) { startLinkingForBackupIfNeeded(card, session, callback) @@ -145,18 +143,12 @@ private class ScanWalletProcessor( when (result) { is CompletionResult.Success -> { PreflightReadTask( - PreflightReadMode.FullCardRead, - card.cardId + readMode = PreflightReadMode.FullCardRead, + cardId = card.cardId ).run(session) { readResult -> when (readResult) { - is CompletionResult.Success -> { - startLinkingForBackupIfNeeded(card, session, callback) - } - is CompletionResult.Failure -> callback( - CompletionResult.Failure( - readResult.error - ) - ) + is CompletionResult.Success -> startLinkingForBackupIfNeeded(card, session, callback) + is CompletionResult.Failure -> callback(CompletionResult.Failure(readResult.error)) } } } @@ -167,14 +159,11 @@ private class ScanWalletProcessor( private fun startLinkingForBackupIfNeeded( card: Card, session: CardSession, - callback: (result: CompletionResult) -> Unit + callback: (result: CompletionResult) -> Unit, ) { - val activationIsFinished = - preferencesStorage.usedCardsPrefStorage.isActivationFinished(card.cardId) + val activationIsFinished = preferencesStorage.usedCardsPrefStorage.isActivationFinished(card.cardId) - if (card.backupStatus == Card.BackupStatus.NoBackup && - !activationIsFinished && card.wallets.isNotEmpty() - ) { + if (card.backupStatus == Card.BackupStatus.NoBackup && !activationIsFinished && card.wallets.isNotEmpty()) { StartPrimaryCardLinkingTask().run(session) { linkingResult -> when (linkingResult) { is CompletionResult.Success -> { @@ -190,11 +179,10 @@ private class ScanWalletProcessor( deriveKeysIfNeeded(card, session, callback) } } - private fun deriveKeysIfNeeded( card: Card, session: CardSession, - callback: (result: CompletionResult) -> Unit + callback: (result: CompletionResult) -> Unit, ) { scope.launch { val derivations = collectDerivations(card) @@ -233,12 +221,13 @@ private class ScanWalletProcessor( private suspend fun getBlockchainsToDerive(card: Card): List { val currenciesRepository = currenciesRepository ?: return emptyList() - val cardCurrencies = currenciesRepository.loadSavedCurrencies(card.cardId, card.settings.isHDWalletAllowed).toMutableList() + val cardCurrencies = currenciesRepository + .loadSavedCurrencies(card.cardId, card.settings.isHDWalletAllowed).toMutableList() val blockchainsToDerive = cardCurrencies.ifEmpty { mutableListOf( BlockchainNetwork(Blockchain.Bitcoin, card), - BlockchainNetwork(Blockchain.Ethereum, card) + BlockchainNetwork(Blockchain.Ethereum, card), ) } @@ -246,7 +235,7 @@ private class ScanWalletProcessor( blockchainsToDerive.addAll( listOf( BlockchainNetwork(Blockchain.Ethereum, card), - BlockchainNetwork(Blockchain.EthereumTestnet, card) + BlockchainNetwork(Blockchain.EthereumTestnet, card), ) ) } @@ -303,52 +292,44 @@ private class ScanTwinProcessor : ProductCommandProcessor { is CompletionResult.Success -> { val publicKey = card.getSingleWallet()?.publicKey if (publicKey == null) { - callback( - CompletionResult.Success( - ScanResponse( - card, - ProductType.Twins, - null - ) - ) - ) - return@run - } - val verified = - TwinsHelper.verifyTwinPublicKey(readDataResult.data.issuerData, publicKey) - if (verified) { - val twinPublicKey = readDataResult.data.issuerData.sliceArray(0 until 65) - val walletData = session.environment.walletData val response = ScanResponse( - card, - ProductType.Twins, - walletData, - twinPublicKey.toHexString() + card = card, + productType = ProductType.Twins, + walletData = null, ) callback(CompletionResult.Success(response)) + return@run + } + + val verified = TwinsHelper.verifyTwinPublicKey(readDataResult.data.issuerData, publicKey) + val response = if (verified) { + val twinPublicKey = readDataResult.data.issuerData.sliceArray(0 until 65) + val walletData = session.environment.walletData + ScanResponse( + card = card, + productType = ProductType.Twins, + walletData = walletData, + secondTwinPublicKey = twinPublicKey.toHexString(), + ) } else { - callback( - CompletionResult.Success( - ScanResponse( - card, - ProductType.Twins, - null - ) - ) + ScanResponse( + card = card, + productType = ProductType.Twins, + walletData = null, ) } + callback(CompletionResult.Success(response)) } - is CompletionResult.Failure -> + is CompletionResult.Failure -> { callback(CompletionResult.Success(ScanResponse(card, ProductType.Twins, null))) + } } } } - } fun Card.getCurvesForNonCreatedWallets(): List { val curvesPresent = wallets.map { it.curve }.toSet() - val curvesForNonCreatedWallets = supportedCurves - .subtract(curvesPresent + EllipticCurve.Secp256r1) + val curvesForNonCreatedWallets = supportedCurves.subtract(curvesPresent + EllipticCurve.Secp256r1) return curvesForNonCreatedWallets.toList() } \ No newline at end of file