Updated on 2026-08-14
This commit is contained in:
parent
e61c83cd4e
commit
7168e62dfe
9 changed files with 218 additions and 0 deletions
|
|
@ -0,0 +1,64 @@
|
|||
package com.tangem.blockchain.common
|
||||
|
||||
import com.tangem.blockchain.bitcoin.BitcoinAddressFactory
|
||||
import com.tangem.blockchain.bitcoin.BitcoinAddressValidator
|
||||
import java.math.BigDecimal
|
||||
|
||||
enum class Blockchain(
|
||||
val id: String,
|
||||
val currency: String,
|
||||
val decimals: Byte,
|
||||
val fullName: String,
|
||||
val pendingTransactionTimeout: Int) {
|
||||
Unknown("", "", 0, "", 0),
|
||||
Bitcoin("", "", 8, "", 0),
|
||||
BitcoinTestnet("", "", 8, "", 0),
|
||||
Ethereum("", "", 18, "", 0),
|
||||
Rootstock("", "", 18, "", 0),
|
||||
Cardano("", "", 6, "", 0),
|
||||
Ripple("", "", 6, "", 0),
|
||||
Binance("", "", 8, "", 0),
|
||||
Stellar("", "", 7, "", 0);
|
||||
|
||||
fun roundingMode(): Int = when (this) {
|
||||
Bitcoin, Ethereum, Rootstock, Binance -> BigDecimal.ROUND_DOWN
|
||||
Cardano -> BigDecimal.ROUND_UP
|
||||
else -> BigDecimal.ROUND_HALF_UP
|
||||
}
|
||||
|
||||
fun makeAddress(cardPublicKey: ByteArray): String {
|
||||
return when (this) {
|
||||
Unknown -> throw Exception("unsupported blockchain")
|
||||
Bitcoin -> BitcoinAddressFactory.makeAddress(cardPublicKey)
|
||||
BitcoinTestnet -> BitcoinAddressFactory.makeAddress(cardPublicKey, testNet = true)
|
||||
// Ethereum -> EthereumAddressFactory.makeAddress(cardPublicKey)
|
||||
// Rootstock -> RootstockAddressFactory.makeAddress(cardPublicKey)
|
||||
// Cardano -> CardanoAddressFactory.makeAddress(cardPublicKey)
|
||||
// Ripple -> RippleAddressFactory.makeAddress(cardPublicKey)
|
||||
// Binance -> BinanceAddressFactory.makeAddress(cardPublicKey)
|
||||
// Stellar -> StellarAddressFactory.makeAddress(cardPublicKey)
|
||||
}
|
||||
}
|
||||
|
||||
fun validateAddress(address: String): Boolean {
|
||||
return when (this) {
|
||||
Unknown -> throw Exception("unsupported blockchain")
|
||||
Bitcoin -> BitcoinAddressValidator.validate(address)
|
||||
BitcoinTestnet -> BitcoinAddressValidator.validate(address, testNet = true)
|
||||
// Ethereum -> EthereumAddressValidator.validate(address)
|
||||
// Rootstock -> RootstockAddressValidator.validate(address)
|
||||
// Cardano -> CardanoAddressValidator.validate(address)
|
||||
// Ripple -> RippleAddressValidator.validate(address)
|
||||
// Binance -> BinanceAddressValidator.validate(address)
|
||||
// Stellar -> StellarAddressValidator.validate(address)
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val values = values()
|
||||
fun fromId(id: String): Blockchain = values.find { it.id == id } ?: Unknown
|
||||
fun fromName(name: String): Blockchain = values.find { it.name == name } ?: Unknown
|
||||
fun fromCurrency(currency: String): Blockchain = values.find { it.currency == currency }
|
||||
?: Unknown
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
package com.tangem.blockchain.common
|
||||
|
||||
import java.math.BigDecimal
|
||||
import java.util.*
|
||||
|
||||
interface Wallet {
|
||||
val config: WalletConfig
|
||||
val address: String
|
||||
val exploreUrl: String?
|
||||
val shareUrl: String?
|
||||
}
|
||||
|
||||
class WalletConfig(
|
||||
val allowFeeSelection: Boolean,
|
||||
val allowFeeInclusion: Boolean,
|
||||
var allowExtract: Boolean = false,
|
||||
var allowLoad: Boolean = false
|
||||
)
|
||||
|
||||
data class Amount(
|
||||
val type: AmountType = AmountType.Coin,
|
||||
val currencySymbol: String,
|
||||
val value: BigDecimal
|
||||
)
|
||||
|
||||
data class Transaction(
|
||||
val amount: Amount,
|
||||
val fee: Amount?,
|
||||
val sourceAddress: String,
|
||||
val destinationAddress: String
|
||||
)
|
||||
|
||||
enum class AmountType { Coin, Token, Reserve }
|
||||
|
||||
enum class ValidationError { WrongAmount, WrongFee, WrongTotal }
|
||||
|
||||
interface TransactionValidator{
|
||||
fun validateTransaction(amount: Amount, fee: Amount?): EnumSet<ValidationError>
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
package com.tangem.blockchain.common
|
||||
|
||||
interface WalletManager {
|
||||
var wallet: Wallet
|
||||
val blockchain: Blockchain
|
||||
|
||||
fun update()
|
||||
}
|
||||
|
||||
interface TransactionBuilder {
|
||||
fun getEstimateSize(transaction: Transaction): Int
|
||||
}
|
||||
|
||||
interface TransactionSender {
|
||||
fun send(transaction: Transaction, signer: TransactionSigner)
|
||||
}
|
||||
|
||||
interface TransactionSigner
|
||||
|
||||
interface FeeProvider {
|
||||
fun getFee(amount: Amount, source: String, destination: String): List<Amount>
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
package com.tangem.blockchain.wallets
|
||||
|
||||
import com.tangem.blockchain.common.*
|
||||
import java.util.*
|
||||
|
||||
class CurrencyWallet(
|
||||
override val config: WalletConfig,
|
||||
override val address: String,
|
||||
override val exploreUrl: String?,
|
||||
override val shareUrl: String?,
|
||||
val pendingTransactions: List<Transaction> = listOf(),
|
||||
val balances: List<Amount> = listOf(),
|
||||
val isTestnet: Boolean = false
|
||||
) : Wallet, TransactionValidator {
|
||||
|
||||
|
||||
override fun validateTransaction(amount: Amount, fee: Amount?): EnumSet<ValidationError> {
|
||||
TODO("not implemented")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue