Skip to content

Commit

Permalink
[#1413] Update ZecSend with Proposal
Browse files Browse the repository at this point in the history
* [#1413] Update ZecSend with Proposal

- Closes #1413
- Changelog update
  • Loading branch information
HonzaR authored Mar 14, 2024
1 parent b1fbb1b commit fe6b11c
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this library adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- The SDK uses ZIP-317 fee system internally
- `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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ class WalletViewModel(application: Application) : AndroidViewModel(application)
runBlocking {
val spendingKey = spendingKey.filterNotNull().first()
kotlin.runCatching {
synchronizer.proposeSend(spendingKey, zecSend)
synchronizer.proposeSend(spendingKey.account, zecSend)
}.onFailure {
Twig.error(it) { "Failed to get transaction proposal" }
}.getOrNull()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cash.z.ecc.android.sdk.fixture

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.Zatoshi
import cash.z.ecc.android.sdk.model.ZecSend
Expand All @@ -12,9 +13,13 @@ object ZecSendFixture {
val AMOUNT = Zatoshi(123)
val MEMO = MemoFixture.new()

// Null until we figure out how to proper test this
val PROPOSAL = null

suspend fun new(
address: String = ADDRESS,
amount: Zatoshi = AMOUNT,
message: Memo = MEMO
) = ZecSend(WalletAddress.Unified.new(address), amount, message)
message: Memo = MEMO,
proposal: Proposal? = PROPOSAL
) = ZecSend(WalletAddress.Unified.new(address), amount, message, proposal)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ package cash.z.ecc.android.sdk.model

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
}

Expand All @@ -19,11 +24,14 @@ suspend fun Synchronizer.send(
spendingKey
)

/**
* This is just a syntactic sugar function for [Synchronizer.proposeTransfer]
*/
suspend fun Synchronizer.proposeSend(
spendingKey: UnifiedSpendingKey,
account: Account,
send: ZecSend
) = proposeTransfer(
spendingKey.account,
account,
send.destination.address,
send.amount,
send.memo.value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ object ZecSendExt {
}

return if (validationErrors.isEmpty()) {
ZecSendValidation.Valid(ZecSend(destination, amount!!, memo))
ZecSendValidation.Valid(ZecSend(destination, amount!!, memo, null))
} else {
ZecSendValidation.Invalid(validationErrors)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ internal class TransactionEncoderImpl(
}.onFailure {
Twig.error(it) { "Caught exception while creating proposal." }
}.onSuccess { result ->
Twig.debug { "result of proposeTransfer: $result" }
Twig.debug { "result of proposeTransfer: ${result.toPrettyString()}" }
}.getOrThrow()
}

Expand Down
28 changes: 21 additions & 7 deletions sdk-lib/src/main/java/cash/z/ecc/android/sdk/model/Proposal.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,22 @@ class Proposal(
* @throws IllegalArgumentException if the proposal is invalid.
*/
@Throws(IllegalArgumentException::class)
fun fromUnsafe(proposal: ProposalUnsafe): Proposal {
val typed = Proposal(proposal)
fun fromUnsafe(proposal: ProposalUnsafe) =
Proposal(proposal).also {
it.check()
}

// Check for type errors eagerly, to ensure that the caller won't
// encounter these errors later.
typed.totalFeeRequired()
/**
* @throws IllegalArgumentException if the given [ByteArray] data could not be parsed and mapped to the new
* 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()
}

/**
Expand All @@ -33,6 +40,13 @@ class Proposal(
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.
*
Expand Down

0 comments on commit fe6b11c

Please sign in to comment.