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 326b59acf3..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 @@ -68,16 +68,15 @@ class ScanProductTask( when (processorResult) { is CompletionResult.Success -> ScanTask().run(session) { scanTaskResult -> when (scanTaskResult) { - is CompletionResult.Success -> callback( - CompletionResult.Success( - processorResult.data + is CompletionResult.Success -> { + // it need because processorResult.data.card doesn't contains attestation result + // and CardWallet.derivedKeys + val processorScanResponseWithNewCard = processorResult.data.copy( + card = scanTaskResult.data ) - ) - is CompletionResult.Failure -> callback( - CompletionResult.Failure( - scanTaskResult.error - ) - ) + callback(CompletionResult.Success(processorScanResponseWithNewCard)) + } + is CompletionResult.Failure -> callback(CompletionResult.Failure(scanTaskResult.error)) } } is CompletionResult.Failure -> callback(CompletionResult.Failure(processorResult.error)) @@ -102,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, + ), + ), ) } } @@ -117,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) @@ -146,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)) } } } @@ -165,18 +156,14 @@ 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 -> { @@ -192,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) @@ -235,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), ) } @@ -248,7 +235,7 @@ private class ScanWalletProcessor( blockchainsToDerive.addAll( listOf( BlockchainNetwork(Blockchain.Ethereum, card), - BlockchainNetwork(Blockchain.EthereumTestnet, card) + BlockchainNetwork(Blockchain.EthereumTestnet, card), ) ) } @@ -305,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 diff --git a/app/src/main/java/com/tangem/tap/features/send/redux/middlewares/SendMiddleware.kt b/app/src/main/java/com/tangem/tap/features/send/redux/middlewares/SendMiddleware.kt index 5eaa250690..df1e3dd5ba 100644 --- a/app/src/main/java/com/tangem/tap/features/send/redux/middlewares/SendMiddleware.kt +++ b/app/src/main/java/com/tangem/tap/features/send/redux/middlewares/SendMiddleware.kt @@ -219,8 +219,10 @@ private fun sendTransaction( return@launch } - withContext(Dispatchers.Main) { + withMainContext { + dispatch(SendAction.ChangeSendButtonState(ButtonState.ENABLED)) tangemSdk.config.linkedTerminal = isLinkedTerminal + when (sendResult) { is SimpleResult.Success -> { store.state.globalState.analyticsHandlers?.triggerEvent( @@ -260,12 +262,12 @@ private fun sendTransaction( card = card, ) - val error = (sendResult.error as? BlockchainSdkError) ?: return@withContext + val error = (sendResult.error as? BlockchainSdkError) ?: return@withMainContext when (error) { is BlockchainSdkError.WrappedTangemError -> { - val tangemSdkError = (error.tangemError as? TangemSdkError) ?: return@withContext - if (tangemSdkError is TangemSdkError.UserCancelled) return@withContext + val tangemSdkError = (error.tangemError as? TangemSdkError) ?: return@withMainContext + if (tangemSdkError is TangemSdkError.UserCancelled) return@withMainContext dispatch(SendAction.Dialog.SendTransactionFails.CardSdkError(tangemSdkError)) } @@ -294,7 +296,6 @@ private fun sendTransaction( } } } - dispatch(SendAction.ChangeSendButtonState(ButtonState.ENABLED)) } } }