Updated on 2026-08-14

This commit is contained in:
Tangem 2026-06-05 12:32:13 +04:00
parent fa55c16871
commit 85b00c4c65
18 changed files with 269 additions and 16 deletions

View file

@ -1,5 +1,6 @@
package com.tangem.tap.common.libs.blockchainsdk
import androidx.annotation.VisibleForTesting
import com.tangem.Message
import com.tangem.TangemSdk
import com.tangem.blockchain.common.TransactionSigner
@ -7,22 +8,70 @@ import com.tangem.core.analytics.models.Basic.TransactionSent.WalletForm
import com.tangem.core.analytics.store.LastSignedWalletFormStore
import com.tangem.data.card.TransactionSignerFactory
import com.tangem.domain.card.models.TwinKey
import com.tangem.domain.common.wallets.UserWalletsListRepository
import com.tangem.domain.common.wallets.update
import com.tangem.domain.models.wallet.UserWallet
import com.tangem.domain.models.wallet.UserWalletId
import com.tangem.tap.domain.TangemSigner
import com.tangem.tap.domain.TangemSignerResponse
import com.tangem.utils.coroutines.AppCoroutineScope
import kotlinx.coroutines.launch
internal class DefaultTransactionSignerFactory(
private val lastSignedWalletFormStore: LastSignedWalletFormStore,
private val userWalletsListRepository: UserWalletsListRepository,
private val coroutineScope: AppCoroutineScope,
) : TransactionSignerFactory {
override fun createTransactionSigner(cardId: String?, sdk: TangemSdk, twinKey: TwinKey?): TransactionSigner {
override fun createTransactionSigner(
cardId: String?,
sdk: TangemSdk,
twinKey: TwinKey?,
userWalletId: UserWalletId,
): TransactionSigner {
return TangemSigner(
cardId = cardId,
tangemSdk = sdk,
initialMessage = Message(),
twinKey = twinKey,
) { signResponse ->
lastSignedWalletFormStore.update(
if (signResponse.isRing) WalletForm.Ring else WalletForm.Card,
)
onSignerResponse(userWalletId, signResponse)
}
}
@VisibleForTesting
internal fun onSignerResponse(userWalletId: UserWalletId, signResponse: TangemSignerResponse) {
lastSignedWalletFormStore.update(
if (signResponse.isRing) WalletForm.Ring else WalletForm.Card,
)
coroutineScope.launch {
userWalletsListRepository.update(userWalletId) { userWallet ->
userWallet.updateSignedHashes(signResponse)
}
}
}
private fun UserWallet.updateSignedHashes(signResponse: TangemSignerResponse): UserWallet {
if (this !is UserWallet.Cold) return this
return copy(
scanResponse = scanResponse.copy(
card = scanResponse.card.copy(
wallets = scanResponse.card.wallets.map { wallet ->
if (wallet.publicKey.contentEquals(signResponse.signedWalletPublicKey)) {
wallet.copy(
// Keep previously known counters if the signer response does not provide them,
// otherwise we would regress the UI counters to null.
totalSignedHashes = signResponse.totalSignedHashes ?: wallet.totalSignedHashes,
remainingSignatures = signResponse.remainingSignatures ?: wallet.remainingSignatures,
)
} else {
wallet
}
},
),
),
)
}
}

View file

@ -51,6 +51,7 @@ internal object WalletConnectDomainModule {
cardSdkConfigRepository.getCommonSigner(
cardId = card.cardId.takeIf { isCardNotBackedUp },
twinKey = TwinKey.getOrNull(scanResponse = wallet.scanResponse),
userWalletId = wallet.walletId,
)
}
}

View file

@ -2,7 +2,9 @@ package com.tangem.tap.di.libs.blockchainsdk
import com.tangem.core.analytics.store.LastSignedWalletFormStore
import com.tangem.data.card.TransactionSignerFactory
import com.tangem.domain.common.wallets.UserWalletsListRepository
import com.tangem.tap.common.libs.blockchainsdk.DefaultTransactionSignerFactory
import com.tangem.utils.coroutines.AppCoroutineScope
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
@ -20,7 +22,13 @@ internal class TransactionSignerFactoryModule {
@Singleton
fun provideTransactionSignerFactory(
lastSignedWalletFormStore: LastSignedWalletFormStore,
userWalletsListRepository: UserWalletsListRepository,
appCoroutineScope: AppCoroutineScope,
): TransactionSignerFactory {
return DefaultTransactionSignerFactory(lastSignedWalletFormStore)
return DefaultTransactionSignerFactory(
lastSignedWalletFormStore = lastSignedWalletFormStore,
userWalletsListRepository = userWalletsListRepository,
coroutineScope = appCoroutineScope,
)
}
}

View file

@ -40,6 +40,7 @@ class TangemSigner(
totalSignedHashes = result.data.totalSignedHashes,
remainingSignatures = result.data.remainingSignatures,
isRing = result.data.batchId?.let(::isRing) == true,
signedWalletPublicKey = publicKey.seedKey,
),
)
if (continuation.isActive) {
@ -86,6 +87,7 @@ class TangemSigner(
totalSignedHashes = result.data.totalSignedHashes,
remainingSignatures = result.data.remainingSignatures,
isRing = result.data.batchId?.let(::isRing) == true,
signedWalletPublicKey = publicKey.seedKey,
),
)
if (continuation.isActive) {
@ -102,8 +104,10 @@ class TangemSigner(
}
}
@Suppress("ArrayInDataClass")
data class TangemSignerResponse(
val totalSignedHashes: Int?,
val remainingSignatures: Int?,
val isRing: Boolean,
val signedWalletPublicKey: ByteArray,
)

View file

@ -0,0 +1,163 @@
package com.tangem.tap.common.libs.blockchainsdk
import arrow.core.right
import com.google.common.truth.Truth.assertThat
import com.tangem.common.test.TestAppCoroutineScope
import com.tangem.common.test.domain.wallet.MockUserWalletFactory
import com.tangem.core.analytics.models.Basic.TransactionSent.WalletForm
import com.tangem.core.analytics.store.LastSignedWalletFormStore
import com.tangem.domain.common.wallets.UserWalletsListRepository
import com.tangem.domain.models.wallet.UserWallet
import com.tangem.tap.domain.TangemSignerResponse
import io.mockk.clearMocks
import io.mockk.coEvery
import io.mockk.every
import io.mockk.mockk
import io.mockk.slot
import io.mockk.verify
import kotlinx.coroutines.flow.MutableStateFlow
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
internal class DefaultTransactionSignerFactoryTest {
private val lastSignedWalletFormStore = mockk<LastSignedWalletFormStore>(relaxed = true)
private val userWalletsListRepository = mockk<UserWalletsListRepository>()
private val factory = DefaultTransactionSignerFactory(
lastSignedWalletFormStore = lastSignedWalletFormStore,
userWalletsListRepository = userWalletsListRepository,
coroutineScope = TestAppCoroutineScope(),
)
private val baseWallet = MockUserWalletFactory.create()
/** Wallet that will be the target of the signing operation. */
private val walletA = baseWallet.scanResponse.card.wallets.first().copy(
publicKey = PUBLIC_KEY_A,
totalSignedHashes = 0,
remainingSignatures = 100,
)
/** Another wallet that must stay untouched after signing with [walletA]'s key. */
private val walletB = baseWallet.scanResponse.card.wallets.first().copy(
publicKey = PUBLIC_KEY_B,
totalSignedHashes = 7,
remainingSignatures = 50,
)
private val userWallet = baseWallet.copy(
scanResponse = baseWallet.scanResponse.copy(
card = baseWallet.scanResponse.card.copy(wallets = listOf(walletA, walletB)),
),
)
private val savedWalletSlot = slot<UserWallet>()
@BeforeEach
fun setup() {
clearMocks(lastSignedWalletFormStore, userWalletsListRepository)
every { userWalletsListRepository.userWallets } returns MutableStateFlow(listOf(userWallet))
coEvery { userWalletsListRepository.saveWithoutLock(capture(savedWalletSlot), any()) } answers {
savedWalletSlot.captured.right()
}
}
@Test
fun `updates signed hashes only for the wallet matching the signed public key`() {
factory.onSignerResponse(
userWalletId = userWallet.walletId,
signResponse = signerResponse(
signedWalletPublicKey = PUBLIC_KEY_A,
totalSignedHashes = 5,
remainingSignatures = 95,
),
)
val savedWallets = (savedWalletSlot.captured as UserWallet.Cold).scanResponse.card.wallets
val savedA = savedWallets.first { it.publicKey.contentEquals(PUBLIC_KEY_A) }
val savedB = savedWallets.first { it.publicKey.contentEquals(PUBLIC_KEY_B) }
assertThat(savedA.totalSignedHashes).isEqualTo(5)
assertThat(savedA.remainingSignatures).isEqualTo(95)
// The non-signed wallet must keep its original values.
assertThat(savedB.totalSignedHashes).isEqualTo(7)
assertThat(savedB.remainingSignatures).isEqualTo(50)
}
@Test
fun `keeps previously known counters when the signer response has null values`() {
factory.onSignerResponse(
userWalletId = userWallet.walletId,
signResponse = signerResponse(
signedWalletPublicKey = PUBLIC_KEY_A,
totalSignedHashes = null,
remainingSignatures = null,
),
)
val savedA = (savedWalletSlot.captured as UserWallet.Cold).scanResponse.card.wallets
.first { it.publicKey.contentEquals(PUBLIC_KEY_A) }
// Null response values must not overwrite the known counters.
assertThat(savedA.totalSignedHashes).isEqualTo(0)
assertThat(savedA.remainingSignatures).isEqualTo(100)
}
@Test
fun `leaves all wallets untouched when no public key matches`() {
factory.onSignerResponse(
userWalletId = userWallet.walletId,
signResponse = signerResponse(
signedWalletPublicKey = UNKNOWN_PUBLIC_KEY,
totalSignedHashes = 5,
remainingSignatures = 95,
),
)
val savedWallets = (savedWalletSlot.captured as UserWallet.Cold).scanResponse.card.wallets
assertThat(savedWallets.first { it.publicKey.contentEquals(PUBLIC_KEY_A) }.totalSignedHashes).isEqualTo(0)
assertThat(savedWallets.first { it.publicKey.contentEquals(PUBLIC_KEY_B) }.totalSignedHashes).isEqualTo(7)
}
@Test
fun `updates last signed wallet form with Card for a non-ring response`() {
factory.onSignerResponse(
userWalletId = userWallet.walletId,
signResponse = signerResponse(signedWalletPublicKey = PUBLIC_KEY_A, isRing = false),
)
verify(exactly = 1) { lastSignedWalletFormStore.update(WalletForm.Card) }
}
@Test
fun `updates last signed wallet form with Ring for a ring response`() {
factory.onSignerResponse(
userWalletId = userWallet.walletId,
signResponse = signerResponse(signedWalletPublicKey = PUBLIC_KEY_A, isRing = true),
)
verify(exactly = 1) { lastSignedWalletFormStore.update(WalletForm.Ring) }
}
private fun signerResponse(
signedWalletPublicKey: ByteArray,
totalSignedHashes: Int? = 1,
remainingSignatures: Int? = 1,
isRing: Boolean = false,
) = TangemSignerResponse(
totalSignedHashes = totalSignedHashes,
remainingSignatures = remainingSignatures,
isRing = isRing,
signedWalletPublicKey = signedWalletPublicKey,
)
private companion object {
val PUBLIC_KEY_A = byteArrayOf(1, 2, 3)
val PUBLIC_KEY_B = byteArrayOf(4, 5, 6)
val UNKNOWN_PUBLIC_KEY = byteArrayOf(9, 9, 9)
}
}