-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AND-4928 move online attestation on backup flow
- Loading branch information
1 parent
a89cfbe
commit 2e7f8f4
Showing
6 changed files
with
152 additions
and
88 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
tangem-sdk-core/src/main/java/com/tangem/operations/backup/BackupCertificateProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.tangem.operations.backup | ||
|
||
import com.tangem.common.CompletionResult | ||
import com.tangem.common.core.CompletionCallback | ||
import com.tangem.common.core.TangemSdkError | ||
import com.tangem.common.extensions.guard | ||
import com.tangem.common.extensions.hexToBytes | ||
import com.tangem.common.services.Result | ||
import com.tangem.common.tlv.TlvBuilder | ||
import com.tangem.common.tlv.TlvTag | ||
import com.tangem.crypto.sign | ||
import com.tangem.operations.attestation.OnlineCardVerifier | ||
|
||
class BackupCertificateProvider { | ||
|
||
private val onlineCardVerifier: OnlineCardVerifier = OnlineCardVerifier() | ||
|
||
suspend fun getCertificate( | ||
cardId: String, | ||
cardPublicKey: ByteArray, | ||
developmentMode: Boolean, | ||
callback: CompletionCallback<ByteArray>, | ||
) { | ||
if (developmentMode) { | ||
val issuerPrivateKey = | ||
"11121314151617184771ED81F2BACF57479E4735EB1405083927372D40DA9E92".hexToBytes() | ||
val issuerSignature = cardPublicKey.sign(issuerPrivateKey) | ||
callback(CompletionResult.Success(generateCertificate(cardPublicKey, issuerSignature))) | ||
return | ||
} | ||
|
||
when ( | ||
val result = | ||
onlineCardVerifier.getCardData(cardId, cardPublicKey) | ||
) { | ||
is Result.Success -> { | ||
val signature = result.data.issuerSignature.guard { | ||
callback(CompletionResult.Failure(TangemSdkError.IssuerSignatureLoadingFailed())) | ||
return | ||
} | ||
callback( | ||
CompletionResult.Success( | ||
generateCertificate(cardPublicKey, signature.hexToBytes()), | ||
), | ||
) | ||
} | ||
|
||
is Result.Failure -> | ||
callback(CompletionResult.Failure(TangemSdkError.IssuerSignatureLoadingFailed())) | ||
} | ||
} | ||
|
||
private fun generateCertificate(cardPublicKey: ByteArray, issuerSignature: ByteArray): ByteArray { | ||
return TlvBuilder().run { | ||
append(TlvTag.CardPublicKey, cardPublicKey) | ||
append(TlvTag.IssuerDataSignature, issuerSignature) | ||
serialize() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.