2019-12-23 11:16:00 -08:00
|
|
|
package cash.z.ecc.android.ui.send
|
|
|
|
|
|
|
|
import androidx.lifecycle.ViewModel
|
|
|
|
import cash.z.ecc.android.lockbox.LockBox
|
|
|
|
import cash.z.ecc.android.ui.setup.WalletSetupViewModel
|
2020-01-06 22:45:24 -08:00
|
|
|
import cash.z.wallet.sdk.Initializer
|
2019-12-23 11:16:00 -08:00
|
|
|
import cash.z.wallet.sdk.Synchronizer
|
|
|
|
import cash.z.wallet.sdk.entity.PendingTransaction
|
2020-01-08 00:45:45 -08:00
|
|
|
import cash.z.wallet.sdk.ext.ZcashSdk
|
2019-12-23 11:16:00 -08:00
|
|
|
import cash.z.wallet.sdk.ext.twig
|
|
|
|
import kotlinx.coroutines.flow.Flow
|
2020-01-08 00:45:45 -08:00
|
|
|
import kotlinx.coroutines.flow.flow
|
2019-12-23 11:16:00 -08:00
|
|
|
import kotlinx.coroutines.flow.onEach
|
|
|
|
import javax.inject.Inject
|
|
|
|
|
2020-01-05 21:01:06 -08:00
|
|
|
class SendViewModel @Inject constructor() : ViewModel() {
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
lateinit var lockBox: LockBox
|
|
|
|
|
2020-01-06 22:45:24 -08:00
|
|
|
@Inject
|
|
|
|
lateinit var synchronizer: Synchronizer
|
|
|
|
|
|
|
|
@Inject
|
|
|
|
lateinit var initializer: Initializer
|
|
|
|
|
|
|
|
fun send(): Flow<PendingTransaction> {
|
|
|
|
val keys = initializer.deriveSpendingKeys(
|
2019-12-23 11:16:00 -08:00
|
|
|
lockBox.getBytes(WalletSetupViewModel.LockBoxKey.SEED)!!
|
|
|
|
)
|
|
|
|
return synchronizer.sendToAddress(
|
|
|
|
keys[0],
|
|
|
|
zatoshiAmount,
|
|
|
|
toAddress,
|
|
|
|
memo
|
|
|
|
).onEach {
|
|
|
|
twig(it.toString())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-08 00:45:45 -08:00
|
|
|
fun validate() = flow<String?> {
|
|
|
|
|
|
|
|
when {
|
|
|
|
synchronizer.validateAddress(toAddress).isNotValid -> {
|
|
|
|
emit("Please enter a valid address")
|
|
|
|
}
|
|
|
|
zatoshiAmount < ZcashSdk.MINERS_FEE_ZATOSHI -> {
|
|
|
|
emit("Please enter a larger amount")
|
|
|
|
}
|
2020-01-09 08:02:47 -08:00
|
|
|
synchronizer.getAddress() == toAddress -> {
|
|
|
|
emit("That appears to be your address!")
|
|
|
|
}
|
2020-01-08 00:45:45 -08:00
|
|
|
else -> emit(null)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-23 11:16:00 -08:00
|
|
|
var toAddress: String = ""
|
|
|
|
var memo: String = ""
|
|
|
|
var zatoshiAmount: Long = -1L
|
|
|
|
}
|