Updated on 2026-08-14

This commit is contained in:
Tangem 2023-07-26 15:12:13 +08:00
parent 159468fc39
commit e74f7adde9
13 changed files with 133 additions and 92 deletions

View file

@ -26,6 +26,7 @@ dependencies {
implementation(project(":domain:models"))
implementation(project(":domain:core"))
implementation(projects.domain.card)
implementation(projects.domain.demo)
implementation(project(":domain:wallets"))
implementation(project(":domain:wallets:models"))
implementation(projects.domain.settings)

View file

@ -3,23 +3,20 @@ package com.tangem.tap.di.domain
import com.tangem.domain.card.*
import com.tangem.domain.card.repository.CardRepository
import com.tangem.domain.card.repository.CardSdkConfigRepository
import com.tangem.tap.domain.scanCard.DefaultScanCardProcessor
import com.tangem.domain.demo.DemoConfig
import com.tangem.domain.demo.IsDemoCardUseCase
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton
import dagger.hilt.android.components.ViewModelComponent
import dagger.hilt.android.scopes.ViewModelScoped
@Module
@InstallIn(SingletonComponent::class)
@InstallIn(ViewModelComponent::class)
internal object CardDomainModule {
@Provides
@Singleton
fun provideScanCardUseCase(): ScanCardProcessor = DefaultScanCardProcessor()
@Provides
@Singleton
@ViewModelScoped
fun provideGetBiometricsStatusUseCase(
cardSdkConfigRepository: CardSdkConfigRepository,
): GetBiometricsStatusUseCase {
@ -27,7 +24,7 @@ internal object CardDomainModule {
}
@Provides
@Singleton
@ViewModelScoped
fun provideSetAccessCodeRequestPolicyUseCase(
cardSdkConfigRepository: CardSdkConfigRepository,
): SetAccessCodeRequestPolicyUseCase {
@ -35,7 +32,7 @@ internal object CardDomainModule {
}
@Provides
@Singleton
@ViewModelScoped
fun provideGetAccessCodeSavingStatusUseCase(
cardSdkConfigRepository: CardSdkConfigRepository,
): GetAccessCodeSavingStatusUseCase {
@ -43,8 +40,12 @@ internal object CardDomainModule {
}
@Provides
@Singleton
@ViewModelScoped
fun provideGetCardWasScannedUseCase(cardRepository: CardRepository): GetCardWasScannedUseCase {
return GetCardWasScannedUseCase(cardRepository = cardRepository)
}
@Provides
@ViewModelScoped
fun provideIsDemoCardUseCase(): IsDemoCardUseCase = IsDemoCardUseCase(config = DemoConfig())
}

View file

@ -0,0 +1,18 @@
package com.tangem.tap.di.domain
import com.tangem.domain.card.*
import com.tangem.tap.domain.scanCard.DefaultScanCardProcessor
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton
@Module
@InstallIn(SingletonComponent::class)
internal object CardLegacyDomainModule {
@Provides
@Singleton
fun provideScanCardUseCase(): ScanCardProcessor = DefaultScanCardProcessor()
}

View file

@ -1,18 +1,7 @@
package com.tangem.tap.features.demo
import com.tangem.blockchain.common.Amount
import com.tangem.blockchain.common.TransactionData
import com.tangem.blockchain.common.TransactionSender
import com.tangem.blockchain.common.TransactionSigner
import com.tangem.blockchain.common.WalletManager
import com.tangem.blockchain.common.toBlockchainSdkError
import com.tangem.blockchain.common.transaction.Fee
import com.tangem.blockchain.common.transaction.TransactionFee
import com.tangem.blockchain.extensions.Result
import com.tangem.blockchain.extensions.SimpleResult
import com.tangem.common.CompletionResult
import com.tangem.domain.demo.DemoConfig
import com.tangem.domain.models.scan.ScanResponse
import com.tangem.domain.common.demo.DemoConfig
import com.tangem.tap.common.extensions.dispatchNotification
import com.tangem.tap.common.redux.AppState
import com.tangem.tap.features.details.redux.DetailsAction
@ -23,13 +12,6 @@ import com.tangem.tap.store
import com.tangem.wallet.R
import org.rekotlin.Action
/**
[REDACTED_AUTHOR]
*/
interface DemoMiddleware {
fun tryHandle(config: DemoConfig, scanResponse: ScanResponse, action: Action): Boolean
}
object DemoHelper {
val config = DemoConfig()
@ -68,14 +50,6 @@ object DemoHelper {
return false
}
fun injectDemoBalance(walletManager: WalletManager?) {
val manager = walletManager ?: return
val blockchain = walletManager.wallet.blockchain
val amount = config.getBalance(blockchain)
manager.wallet.setAmount(amount)
}
private fun getScanResponse(appState: () -> AppState?): ScanResponse? {
val state = appState() ?: return null
@ -83,48 +57,3 @@ object DemoHelper {
?: state.globalState.scanResponse
}
}
class DemoTransactionSender(private val walletManager: WalletManager) : TransactionSender {
override suspend fun getFee(amount: Amount, destination: String): Result<TransactionFee> {
val blockchain = walletManager.wallet.blockchain
return Result.Success(
TransactionFee.Choosable(
minimum = Fee.Common(Amount(minimumFee, blockchain)),
normal = Fee.Common(Amount(normalFee, blockchain)),
priority = Fee.Common(Amount(priorityFee, blockchain)),
),
)
}
@Suppress("MagicNumber")
override suspend fun send(transactionData: TransactionData, signer: TransactionSigner): SimpleResult {
val dataToSign = randomString(32).toByteArray()
val signerResponse = signer.sign(
hash = dataToSign,
publicKey = walletManager.wallet.publicKey,
)
return when (signerResponse) {
is CompletionResult.Success -> SimpleResult.Failure(Exception(ID).toBlockchainSdkError())
is CompletionResult.Failure -> SimpleResult.fromTangemSdkError(signerResponse.error)
}
}
private fun randomInt(from: Int, to: Int): Int = kotlin.random.Random.nextInt(from, to)
private fun randomString(length: Int): String {
val charPool: List<Char> = ('a'..'z') + ('A'..'Z') + ('0'..'9')
return (1..length)
.map { randomInt(0, charPool.size) }
.map(charPool::get)
.joinToString("")
}
companion object {
val ID = DemoTransactionSender::class.java.simpleName
private val minimumFee = 0.0001.toBigDecimal()
private val normalFee = 0.0002.toBigDecimal()
private val priorityFee = 0.0003.toBigDecimal()
}
}

View file

@ -0,0 +1,12 @@
package com.tangem.tap.features.demo
import com.tangem.domain.demo.DemoConfig
import com.tangem.domain.models.scan.ScanResponse
import org.rekotlin.Action
/**
[REDACTED_AUTHOR]
*/
interface DemoMiddleware {
fun tryHandle(config: DemoConfig, scanResponse: ScanResponse, action: Action): Boolean
}

View file

@ -1,9 +1,9 @@
package com.tangem.tap.features.demo
import com.tangem.common.extensions.guard
import com.tangem.domain.models.scan.ScanResponse
import com.tangem.domain.common.demo.DemoConfig
import com.tangem.domain.common.extensions.withMainContext
import com.tangem.domain.demo.DemoConfig
import com.tangem.domain.models.scan.ScanResponse
import com.tangem.tap.domain.extensions.makePrimaryWalletManager
import com.tangem.tap.features.onboarding.products.note.redux.OnboardingNoteAction
import com.tangem.tap.features.wallet.models.Currency

View file

@ -0,0 +1,51 @@
package com.tangem.tap.features.demo
import com.tangem.blockchain.common.*
import com.tangem.blockchain.common.transaction.Fee
import com.tangem.blockchain.common.transaction.TransactionFee
import com.tangem.blockchain.extensions.Result
import com.tangem.blockchain.extensions.SimpleResult
import com.tangem.common.CompletionResult
import kotlin.random.Random
class DemoTransactionSender(private val walletManager: WalletManager) : TransactionSender {
override suspend fun getFee(amount: Amount, destination: String): Result<TransactionFee> {
val blockchain = walletManager.wallet.blockchain
return Result.Success(
TransactionFee.Choosable(
minimum = Fee.Common(Amount(minimumFee, blockchain)),
normal = Fee.Common(Amount(normalFee, blockchain)),
priority = Fee.Common(Amount(priorityFee, blockchain)),
),
)
}
override suspend fun send(transactionData: TransactionData, signer: TransactionSigner): SimpleResult {
val signerResponse = signer.sign(
hash = getDataToSign(),
publicKey = walletManager.wallet.publicKey,
)
return when (signerResponse) {
is CompletionResult.Success -> SimpleResult.Failure(Exception(ID).toBlockchainSdkError())
is CompletionResult.Failure -> SimpleResult.fromTangemSdkError(signerResponse.error)
}
}
private fun getDataToSign(): ByteArray {
val charPool: List<Char> = ('a'..'z') + ('A'..'Z') + ('0'..'9')
return IntRange(start = 1, endInclusive = 32)
.map { Random.nextInt(from = 0, until = charPool.size) }
.map(charPool::get)
.joinToString(separator = "")
.toByteArray()
}
companion object {
val ID: String = DemoTransactionSender::class.java.simpleName
private val minimumFee = 0.0001.toBigDecimal()
private val normalFee = 0.0002.toBigDecimal()
private val priorityFee = 0.0003.toBigDecimal()
}
}

View file

@ -1,14 +1,22 @@
plugins {
alias(deps.plugins.kotlin.jvm)
alias(deps.plugins.android.library)
alias(deps.plugins.kotlin.android)
id("configuration")
}
android {
namespace = "com.tangem.domain.card"
}
dependencies {
implementation(projects.core.analytics.models)
implementation(projects.domain.demo)
implementation(projects.domain.core)
implementation(projects.domain.legacy)
// TODO: Remove after new card scan result was implemented
implementation(projects.domain.models)
implementation(projects.core.analytics.models)
implementation(deps.tangem.card.core)
}

1
domain/demo/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/build

View file

@ -0,0 +1,13 @@
plugins {
alias(deps.plugins.android.library)
alias(deps.plugins.kotlin.android)
id("configuration")
}
android {
namespace = "com.tangem.domain.demo"
}
dependencies {
implementation(deps.tangem.blockchain)
}

View file

@ -1,8 +1,8 @@
package com.tangem.domain.common.demo
package com.tangem.domain.demo
import com.tangem.blockchain.BuildConfig
import com.tangem.blockchain.common.Amount
import com.tangem.blockchain.common.Blockchain
import com.tangem.domain.features.BuildConfig
import java.math.BigDecimal
@Suppress("LargeClass")
@ -15,7 +15,7 @@ class DemoConfig {
Blockchain.Solana,
)
val demoCardIds: List<String> by lazy {
private val demoCardIds: List<String> by lazy {
val demoIds = getReleaseIds().toMutableList()
if (BuildConfig.DEBUG) demoIds.addAll(debugTestDemoCardIds)

View file

@ -0,0 +1,6 @@
package com.tangem.domain.demo
class IsDemoCardUseCase(private val config: DemoConfig) {
operator fun invoke(cardId: String): Boolean = config.isDemoCardId(cardId)
}

View file

@ -77,8 +77,9 @@ include(":features:learn2earn:impl")
include(":domain:models")
include(":domain:legacy")
include(":domain:core")
include(":domain:card")
include(":domain:core")
include(":domain:demo")
include(":domain:settings")
include(":domain:tokens")
include(":domain:wallets")