From 9c3467e94187b4f44f8ec3c0dc5f66e4eacfdc00 Mon Sep 17 00:00:00 2001 From: Kris Nuttycombe Date: Thu, 29 Feb 2024 07:19:42 -0700 Subject: [PATCH] zcash_client_backend: Implement `TreeState::orchard_tree` --- zcash_client_backend/CHANGELOG.md | 1 + zcash_client_backend/src/proto.rs | 42 +++++++++++++++++++++++-------- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/zcash_client_backend/CHANGELOG.md b/zcash_client_backend/CHANGELOG.md index 8dc4c1e33..57bd0bbe9 100644 --- a/zcash_client_backend/CHANGELOG.md +++ b/zcash_client_backend/CHANGELOG.md @@ -81,6 +81,7 @@ and this library adheres to Rust's notion of - `PROPOSAL_SER_V1` - `ProposalDecodingError` - `proposal` module, for parsing and serializing transaction proposals. + - `service::TreeState::orchard_tree` (behind the `orchard` feature flag) - `impl Clone for zcash_client_backend::{ zip321::{Payment, TransactionRequest, Zip321Error, parse::Param, parse::IndexedParam}, wallet::WalletTransparentOutput, diff --git a/zcash_client_backend/src/proto.rs b/zcash_client_backend/src/proto.rs index d74381609..421018f65 100644 --- a/zcash_client_backend/src/proto.rs +++ b/zcash_client_backend/src/proto.rs @@ -1,5 +1,7 @@ //! Generated code for handling light client protobuf structs. +use incrementalmerkletree::frontier::CommitmentTree; +use nonempty::NonEmpty; use std::{ array::TryFromSliceError, collections::BTreeMap, @@ -7,10 +9,8 @@ use std::{ io, }; -use incrementalmerkletree::frontier::CommitmentTree; - -use nonempty::NonEmpty; -use sapling::{self, note::ExtractedNoteCommitment, Node, Nullifier, NOTE_COMMITMENT_TREE_DEPTH}; +use sapling::{self, note::ExtractedNoteCommitment, Node}; +use zcash_note_encryption::{EphemeralKeyBytes, COMPACT_NOTE_SIZE}; use zcash_primitives::{ block::{BlockHash, BlockHeader}, consensus::{self, BlockHeight, Parameters}, @@ -19,8 +19,6 @@ use zcash_primitives::{ transaction::{components::amount::NonNegativeAmount, fees::StandardFeeRule, TxId}, }; -use zcash_note_encryption::{EphemeralKeyBytes, COMPACT_NOTE_SIZE}; - use crate::{ data_api::InputSource, fees::{ChangeValue, TransactionBalance}, @@ -32,6 +30,9 @@ use crate::{ #[cfg(feature = "transparent-inputs")] use zcash_primitives::transaction::components::OutPoint; +#[cfg(feature = "orchard")] +use orchard::tree::MerkleHashOrchard; + #[rustfmt::skip] #[allow(unknown_lints)] #[allow(clippy::derive_partial_eq_without_eq)] @@ -169,8 +170,8 @@ impl TryFrom } impl compact_formats::CompactSaplingSpend { - pub fn nf(&self) -> Result { - Nullifier::from_slice(&self.nf).map_err(|_| ()) + pub fn nf(&self) -> Result { + sapling::Nullifier::from_slice(&self.nf).map_err(|_| ()) } } @@ -198,14 +199,35 @@ impl From<&orchard::Action> for compact_formats::CompactOr impl service::TreeState { /// Deserializes and returns the Sapling note commitment tree field of the tree state. - pub fn sapling_tree(&self) -> io::Result> { + pub fn sapling_tree( + &self, + ) -> io::Result> { let sapling_tree_bytes = hex::decode(&self.sapling_tree).map_err(|e| { io::Error::new( io::ErrorKind::InvalidData, format!("Hex decoding of Sapling tree bytes failed: {:?}", e), ) })?; - read_commitment_tree::(&sapling_tree_bytes[..]) + read_commitment_tree::( + &sapling_tree_bytes[..], + ) + } + + /// Deserializes and returns the Sapling note commitment tree field of the tree state. + #[cfg(feature = "orchard")] + pub fn orchard_tree( + &self, + ) -> io::Result> + { + let orchard_tree_bytes = hex::decode(&self.orchard_tree).map_err(|e| { + io::Error::new( + io::ErrorKind::InvalidData, + format!("Hex decoding of Orchard tree bytes failed: {:?}", e), + ) + })?; + read_commitment_tree::( + &orchard_tree_bytes[..], + ) } }