Make `TransactionBuilder::AddOrchardOutput` memo optional

This replaces the previous use of the `NO_MEMO` constant, instead
passing the optionality through to Rust for handling.
This commit is contained in:
Jack Grigg 2022-02-16 13:21:47 +00:00
parent 2da0856e6f
commit da755c2ae8
5 changed files with 10 additions and 8 deletions

View File

@ -278,7 +278,7 @@ TEST(TransactionBuilder, TransparentToOrchard)
// 0.0005 t-ZEC in, 0.0004 z-ZEC out, default fee
auto builder = TransactionBuilder(consensusParams, 1, orchardAnchor, &keystore);
builder.AddTransparentInput(COutPoint(uint256S("1234"), 0), scriptPubKey, 50000);
builder.AddOrchardOutput(std::nullopt, recipient, 40000);
builder.AddOrchardOutput(std::nullopt, recipient, 40000, std::nullopt);
auto maybeTx = builder.Build();
EXPECT_TRUE(maybeTx.IsTx());
if (maybeTx.IsError()) {

View File

@ -35,6 +35,8 @@ void orchard_builder_free(OrchardBuilderPtr* ptr);
///
/// `ovk` is a pointer to the outgoing viewing key to make this recipient recoverable by,
/// or `null` to make the recipient unrecoverable by the sender.
///
/// `memo` is a pointer to the 512-byte memo field encoding, or `null` for "no memo".
bool orchard_builder_add_recipient(
OrchardBuilderPtr* ptr,
const unsigned char* ovk,

View File

@ -46,9 +46,9 @@ pub extern "C" fn orchard_builder_add_recipient(
.map(OutgoingViewingKey::from);
let recipient = unsafe { recipient.as_ref() }.expect("Recipient may not be null.");
let value = NoteValue::from_raw(value);
let memo = unsafe { memo.as_ref() }.expect("Memo may not be null.");
let memo = unsafe { memo.as_ref() }.copied();
match builder.add_recipient(ovk, *recipient, value, Some(*memo)) {
match builder.add_recipient(ovk, *recipient, value, memo) {
Ok(()) => true,
Err(e) => {
error!("Failed to add Orchard recipient: {}", e);

View File

@ -45,7 +45,7 @@ void Builder::AddOutput(
const std::optional<uint256>& ovk,
const libzcash::OrchardRawAddress& to,
CAmount value,
std::array<unsigned char, ZC_MEMO_SIZE> memo)
const std::optional<std::array<unsigned char, ZC_MEMO_SIZE>>& memo)
{
if (!inner) {
throw std::logic_error("orchard::Builder has already been used");
@ -56,7 +56,7 @@ void Builder::AddOutput(
ovk.has_value() ? ovk->begin() : nullptr,
to.inner.get(),
value,
memo.data());
memo.has_value() ? memo->data() : nullptr);
}
std::optional<UnauthorizedBundle> Builder::Build() {
@ -264,7 +264,7 @@ void TransactionBuilder::AddOrchardOutput(
const std::optional<uint256>& ovk,
const libzcash::OrchardRawAddress& to,
CAmount value,
std::array<unsigned char, ZC_MEMO_SIZE> memo)
const std::optional<std::array<unsigned char, ZC_MEMO_SIZE>>& memo)
{
if (!orchardBuilder.has_value()) {
throw std::runtime_error("TransactionBuilder cannot add Orchard output without Orchard anchor");

View File

@ -64,7 +64,7 @@ public:
const std::optional<uint256>& ovk,
const libzcash::OrchardRawAddress& to,
CAmount value,
std::array<unsigned char, ZC_MEMO_SIZE> memo = NO_MEMO);
const std::optional<std::array<unsigned char, ZC_MEMO_SIZE>>& memo);
/// Builds a bundle containing the given spent notes and recipients.
///
@ -289,7 +289,7 @@ public:
const std::optional<uint256>& ovk,
const libzcash::OrchardRawAddress& to,
CAmount value,
std::array<unsigned char, ZC_MEMO_SIZE> memo = NO_MEMO);
const std::optional<std::array<unsigned char, ZC_MEMO_SIZE>>& memo);
// Throws if the anchor does not match the anchor used by
// previously-added Sapling spends.