Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AND-8236 handle finalized card #407

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,17 @@ class BackupService(
}

private fun updateState(): State {
val preparingStateCondition =
repo.data.accessCode == null || repo.data.primaryCard == null || repo.data.backupCards.isEmpty()
val finalizingStateCondition =
repo.data.attestSignature == null ||
repo.data.backupData.size < repo.data.backupCards.size ||
repo.data.primaryCardFinalized == false
val finalizingStateConditionWithCount = repo.data.finalizedBackupCardsCount < repo.data.backupCards.size
Comment on lines +244 to +250
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Можно is добавить

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

не стал добавлять is, т.к. в конце есть Condition, например isFinalizingState == finalizingStateCondition на мой взгляд

currentState = when {
repo.data.accessCode == null || repo.data.primaryCard == null || repo.data.backupCards.isEmpty() ->
State.Preparing

repo.data.attestSignature == null || repo.data.backupData.size < repo.data.backupCards.size ->
State.FinalizingPrimaryCard

repo.data.finalizedBackupCardsCount < repo.data.backupCards.size ->
State.FinalizingBackupCard(repo.data.finalizedBackupCardsCount + 1)
preparingStateCondition -> State.Preparing
finalizingStateCondition -> State.FinalizingPrimaryCard
finalizingStateConditionWithCount -> State.FinalizingBackupCard(repo.data.finalizedBackupCardsCount + 1)

else -> {
onBackupCompleted()
Expand Down Expand Up @@ -335,10 +337,21 @@ class BackupService(
readBackupStartIndex = repo.data.backupData.size,
attestSignature = repo.data.attestSignature,
onLink = {
repo.data = repo.data.copy(attestSignature = it)
repo.data = repo.data.copy(
attestSignature = it,
primaryCardFinalized = false,
)
},
onRead = { cardId, data ->
repo.data = repo.data.copy(backupData = repo.data.backupData.plus(Pair(cardId, data)))
repo.data = repo.data.copy(
backupData = repo.data.backupData.plus(Pair(cardId, data)),
primaryCardFinalized = false,
)
},
onFinalize = {
repo.data = repo.data.copy(
primaryCardFinalized = true,
)
},
)
val formattedCardId = CardIdFormatter(style = sdk.config.cardIdDisplayFormat)
Expand Down Expand Up @@ -537,6 +550,7 @@ data class BackupServiceData(
val certificates: Map<String, ByteArray> = emptyMap(),
val backupData: Map<String, List<EncryptedBackupData>> = emptyMap(),
val finalizedBackupCardsCount: Int = 0,
val primaryCardFinalized: Boolean? = null,
) {
val shouldSave: Boolean
get() = attestSignature != null || backupData.isNotEmpty()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.tangem.operations.backup

import com.tangem.Log
import com.tangem.common.CompletionResult
import com.tangem.common.UserCode
import com.tangem.common.UserCodeType
Expand All @@ -19,6 +20,7 @@ class FinalizePrimaryCardTask(
private val attestSignature: ByteArray?, // We already have attestSignature
private val onLink: (ByteArray) -> Unit,
private val onRead: (String, List<EncryptedBackupData>) -> Unit,
private val onFinalize: () -> Unit,
private val readBackupStartIndex: Int,
) : CardSessionRunnable<Card> {

Expand Down Expand Up @@ -94,13 +96,28 @@ class FinalizePrimaryCardTask(
return
}
if (card.firmwareVersion < FirmwareVersion.KeysImportAvailable) {
onFinalize()
callback(CompletionResult.Success(card))
return
}
FinalizeReadBackupDataCommand(accessCode).run(session) { result ->
when (result) {
is CompletionResult.Failure -> callback(CompletionResult.Failure(result.error))
is CompletionResult.Success -> callback(CompletionResult.Success(card))
is CompletionResult.Failure -> {
// Backup data already finalized,
// but we didn't catch the original response due to NFC errors or tag lost.
// Just cover invalid state error
if (result.error is TangemSdkError.InvalidState) {
Log.debug { "Got ${result.error}. Ignoring.." }
onFinalize()
callback(CompletionResult.Success(card))
} else {
callback(CompletionResult.Failure(result.error))
}
}
is CompletionResult.Success -> {
onFinalize()
callback(CompletionResult.Success(card))
}
}
}
}
Expand Down
Loading