[#1413] Update ZecSend with Proposal

* [#1413] Update ZecSend with Proposal

- Closes #1413
- Changelog update
This commit is contained in:
Honza Rychnovský 2024-03-14 14:40:40 +01:00 committed by GitHub
parent b1fbb1b73e
commit fe6b11c4ec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 43 additions and 15 deletions

View File

@ -9,6 +9,7 @@ and this library adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed ### Changed
- The SDK uses ZIP-317 fee system internally - The SDK uses ZIP-317 fee system internally
- `ZcashSdk.MINERS_FEE` has been deprecated, and will be removed in 2.1.0 - `ZcashSdk.MINERS_FEE` has been deprecated, and will be removed in 2.1.0
- `ZecSend` data class now provides `Proposal?` object initiated using `Synchronizer.proposeTransfer`
## [2.0.7] - 2024-03-08 ## [2.0.7] - 2024-03-08

View File

@ -231,7 +231,7 @@ class WalletViewModel(application: Application) : AndroidViewModel(application)
runBlocking { runBlocking {
val spendingKey = spendingKey.filterNotNull().first() val spendingKey = spendingKey.filterNotNull().first()
kotlin.runCatching { kotlin.runCatching {
synchronizer.proposeSend(spendingKey, zecSend) synchronizer.proposeSend(spendingKey.account, zecSend)
}.onFailure { }.onFailure {
Twig.error(it) { "Failed to get transaction proposal" } Twig.error(it) { "Failed to get transaction proposal" }
}.getOrNull() }.getOrNull()

View File

@ -1,6 +1,7 @@
package cash.z.ecc.android.sdk.fixture package cash.z.ecc.android.sdk.fixture
import cash.z.ecc.android.sdk.model.Memo import cash.z.ecc.android.sdk.model.Memo
import cash.z.ecc.android.sdk.model.Proposal
import cash.z.ecc.android.sdk.model.WalletAddress import cash.z.ecc.android.sdk.model.WalletAddress
import cash.z.ecc.android.sdk.model.Zatoshi import cash.z.ecc.android.sdk.model.Zatoshi
import cash.z.ecc.android.sdk.model.ZecSend import cash.z.ecc.android.sdk.model.ZecSend
@ -12,9 +13,13 @@ object ZecSendFixture {
val AMOUNT = Zatoshi(123) val AMOUNT = Zatoshi(123)
val MEMO = MemoFixture.new() val MEMO = MemoFixture.new()
// Null until we figure out how to proper test this
val PROPOSAL = null
suspend fun new( suspend fun new(
address: String = ADDRESS, address: String = ADDRESS,
amount: Zatoshi = AMOUNT, amount: Zatoshi = AMOUNT,
message: Memo = MEMO message: Memo = MEMO,
) = ZecSend(WalletAddress.Unified.new(address), amount, message) proposal: Proposal? = PROPOSAL
) = ZecSend(WalletAddress.Unified.new(address), amount, message, proposal)
} }

View File

@ -2,7 +2,12 @@ package cash.z.ecc.android.sdk.model
import cash.z.ecc.android.sdk.Synchronizer import cash.z.ecc.android.sdk.Synchronizer
data class ZecSend(val destination: WalletAddress, val amount: Zatoshi, val memo: Memo) { data class ZecSend(
val destination: WalletAddress,
val amount: Zatoshi,
val memo: Memo,
val proposal: Proposal?
) {
companion object companion object
} }
@ -19,11 +24,14 @@ suspend fun Synchronizer.send(
spendingKey spendingKey
) )
/**
* This is just a syntactic sugar function for [Synchronizer.proposeTransfer]
*/
suspend fun Synchronizer.proposeSend( suspend fun Synchronizer.proposeSend(
spendingKey: UnifiedSpendingKey, account: Account,
send: ZecSend send: ZecSend
) = proposeTransfer( ) = proposeTransfer(
spendingKey.account, account,
send.destination.address, send.destination.address,
send.amount, send.amount,
send.memo.value send.memo.value

View File

@ -28,7 +28,7 @@ object ZecSendExt {
} }
return if (validationErrors.isEmpty()) { return if (validationErrors.isEmpty()) {
ZecSendValidation.Valid(ZecSend(destination, amount!!, memo)) ZecSendValidation.Valid(ZecSend(destination, amount!!, memo, null))
} else { } else {
ZecSendValidation.Invalid(validationErrors) ZecSendValidation.Invalid(validationErrors)
} }

View File

@ -89,7 +89,7 @@ internal class TransactionEncoderImpl(
}.onFailure { }.onFailure {
Twig.error(it) { "Caught exception while creating proposal." } Twig.error(it) { "Caught exception while creating proposal." }
}.onSuccess { result -> }.onSuccess { result ->
Twig.debug { "result of proposeTransfer: $result" } Twig.debug { "result of proposeTransfer: ${result.toPrettyString()}" }
}.getOrThrow() }.getOrThrow()
} }

View File

@ -15,15 +15,22 @@ class Proposal(
* @throws IllegalArgumentException if the proposal is invalid. * @throws IllegalArgumentException if the proposal is invalid.
*/ */
@Throws(IllegalArgumentException::class) @Throws(IllegalArgumentException::class)
fun fromUnsafe(proposal: ProposalUnsafe): Proposal { fun fromUnsafe(proposal: ProposalUnsafe) =
val typed = Proposal(proposal) Proposal(proposal).also {
it.check()
}
// Check for type errors eagerly, to ensure that the caller won't /**
// encounter these errors later. * @throws IllegalArgumentException if the given [ByteArray] data could not be parsed and mapped to the new
typed.totalFeeRequired() * type-safe Proposal class.
*/
@Throws(IllegalArgumentException::class)
fun fromByteArray(array: ByteArray) = fromUnsafe(ProposalUnsafe.parse(array))
}
return typed // Check for type errors eagerly, to ensure that the caller won't encounter these errors later.
} private fun check() {
totalFeeRequired()
} }
/** /**
@ -33,6 +40,13 @@ class Proposal(
return inner return inner
} }
/**
* Serializes this proposal type-safe data to [ByteArray] for storing purposes.
*/
fun toByteArray(): ByteArray {
return inner.toByteArray()
}
/** /**
* Returns the number of transactions that this proposal will create. * Returns the number of transactions that this proposal will create.
* *