|
| 1 | +package to.bitkit.models |
| 2 | + |
| 3 | +import androidx.compose.runtime.Immutable |
| 4 | +import androidx.compose.runtime.Stable |
| 5 | +import com.synonym.bitkitcore.AccountType |
| 6 | +import com.synonym.bitkitcore.Activity |
| 7 | +import com.synonym.bitkitcore.AddressType |
| 8 | +import kotlinx.collections.immutable.ImmutableList |
| 9 | +import kotlinx.collections.immutable.ImmutableSet |
| 10 | +import kotlinx.collections.immutable.persistentSetOf |
| 11 | +import kotlinx.serialization.Serializable |
| 12 | + |
| 13 | +/** A paired hardware wallet tracked as a watch-only balance. */ |
| 14 | +@Stable |
| 15 | +data class HwWallet( |
| 16 | + val id: String, |
| 17 | + val name: String, |
| 18 | + val model: String?, |
| 19 | + val transportType: TransportType, |
| 20 | + val isConnected: Boolean, |
| 21 | + val balanceSats: ULong, |
| 22 | + val activities: ImmutableList<Activity>, |
| 23 | + val fundingBalanceSats: ULong = balanceSats, |
| 24 | + val deviceIds: ImmutableSet<String> = persistentSetOf(id), |
| 25 | +) |
| 26 | + |
| 27 | +/** Serializable per-device balance snapshot carried by [BalanceState]. */ |
| 28 | +@Immutable |
| 29 | +@Serializable |
| 30 | +data class HwWalletBalance( |
| 31 | + val id: String, |
| 32 | + val sats: ULong, |
| 33 | +) |
| 34 | + |
| 35 | +/** A newly detected inbound transaction to a watched hardware wallet. */ |
| 36 | +@Immutable |
| 37 | +data class HwWalletReceivedTx( |
| 38 | + val txid: String, |
| 39 | + val sats: ULong, |
| 40 | +) |
| 41 | + |
| 42 | +sealed interface HwFundingAccount { |
| 43 | + val vendor: HwWalletVendor |
| 44 | + val xpub: String |
| 45 | + val addressType: HwFundingAddressType |
| 46 | + val accountType: AccountType |
| 47 | + val balanceSats: ULong |
| 48 | + |
| 49 | + data class Trezor( |
| 50 | + override val xpub: String, |
| 51 | + override val addressType: HwFundingAddressType, |
| 52 | + override val balanceSats: ULong, |
| 53 | + ) : HwFundingAccount { |
| 54 | + override val vendor: HwWalletVendor = HwWalletVendor.TREZOR |
| 55 | + override val accountType: AccountType |
| 56 | + get() = addressType.accountType |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +data class HwFundingTransaction( |
| 61 | + val psbt: String, |
| 62 | + val miningFeeSats: ULong, |
| 63 | + val feeRate: Float, |
| 64 | + val totalSpent: ULong, |
| 65 | + val satsPerVByte: ULong, |
| 66 | +) |
| 67 | + |
| 68 | +data class HwFundingBroadcastResult( |
| 69 | + val txId: String, |
| 70 | + val miningFeeSats: ULong, |
| 71 | + val feeRate: ULong, |
| 72 | + val totalSpent: ULong, |
| 73 | +) |
| 74 | + |
| 75 | +enum class HwWalletVendor { |
| 76 | + TREZOR, |
| 77 | +} |
| 78 | + |
| 79 | +enum class HwFundingAddressType( |
| 80 | + val addressType: AddressType, |
| 81 | +) { |
| 82 | + LEGACY(AddressType.P2PKH), |
| 83 | + NESTED_SEGWIT(AddressType.P2SH), |
| 84 | + NATIVE_SEGWIT(AddressType.P2WPKH), |
| 85 | + TAPROOT(AddressType.P2TR); |
| 86 | + |
| 87 | + val settingsKey: String |
| 88 | + get() = addressType.toSettingsString() |
| 89 | + |
| 90 | + val accountType: AccountType |
| 91 | + get() = addressType.toAccountType() |
| 92 | + |
| 93 | + companion object { |
| 94 | + val DEFAULT: HwFundingAddressType = entries.first { it.addressType == DEFAULT_ADDRESS_TYPE } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +fun HwWallet.toBalance() = HwWalletBalance(id = id, sats = balanceSats) |
0 commit comments