zcash-android-wallet-sdk/src/main/java/cash/z/wallet/sdk/data/WalletTransactionEncoder.kt

30 lines
1.3 KiB
Kotlin
Raw Normal View History

package cash.z.wallet.sdk.data
2019-07-14 15:13:12 -07:00
import cash.z.wallet.sdk.entity.EncodedTransaction
import cash.z.wallet.sdk.exception.TransactionNotEncodedException
import cash.z.wallet.sdk.exception.TransactionNotFoundException
import cash.z.wallet.sdk.secure.Wallet
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.withContext
class WalletTransactionEncoder(
private val wallet: Wallet,
private val repository: TransactionRepository,
private val spendingKeyProvider: Wallet.SpendingKeyProvider
2019-07-14 15:13:12 -07:00
) : TransactionEncoder {
/**
* Creates a transaction, throwing an exception whenever things are missing. When the provided wallet implementation
* doesn't throw an exception, we wrap the issue into a descriptive exception ourselves (rather than using
* double-bangs for things).
*/
override suspend fun create(zatoshi: Long, toAddress: String, memo: String): EncodedTransaction = withContext(IO) {
val transactionId = wallet.createSpend(spendingKeyProvider.key, zatoshi, toAddress, memo)
val transaction = repository.findTransactionById(transactionId)
?: throw TransactionNotFoundException(transactionId)
EncodedTransaction(transaction.transactionId, transaction.raw
2019-07-14 15:13:12 -07:00
?: throw TransactionNotEncodedException(transactionId)
)
}
}