From 60961433ceaffa192e72e92d71fcf00c09d2fbef Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Tue, 3 Dec 2024 13:26:23 +0000 Subject: [PATCH] pczt: Add the Spend Finalizer role --- pczt/Cargo.toml | 1 + pczt/src/lib.rs | 2 ++ pczt/src/roles.rs | 3 ++ pczt/src/roles/spend_finalizer/mod.rs | 44 +++++++++++++++++++++++++++ 4 files changed, 50 insertions(+) create mode 100644 pczt/src/roles/spend_finalizer/mod.rs diff --git a/pczt/Cargo.toml b/pczt/Cargo.toml index 1d4ff885f..e9dbeb374 100644 --- a/pczt/Cargo.toml +++ b/pczt/Cargo.toml @@ -56,4 +56,5 @@ zcp-builder = ["dep:zcash_primitives", "dep:zcash_protocol"] io-finalizer = ["orchard", "sapling"] prover = ["dep:rand_core", "sapling?/temporary-zcashd"] signer = ["dep:blake2b_simd", "dep:rand_core", "orchard", "sapling", "transparent"] +spend-finalizer = ["transparent"] tx-extractor = ["dep:rand_core", "orchard", "sapling", "transparent"] diff --git a/pczt/src/lib.rs b/pczt/src/lib.rs index dfbc55dfb..90a50020e 100644 --- a/pczt/src/lib.rs +++ b/pczt/src/lib.rs @@ -33,6 +33,8 @@ //! - A source of randomness. //! - Combiner (anyone can execute) //! - Combines several PCZTs that represent the same transaction into a single PCZT. +//! - Spend Finalizer (anyone can execute) +//! - Combines partial transparent signatures into `script_sig`s. //! - Transaction Extractor (anyone can execute) //! - Creates bindingSig and extracts the final transaction. diff --git a/pczt/src/roles.rs b/pczt/src/roles.rs index 27ec4e8b3..5a063dba1 100644 --- a/pczt/src/roles.rs +++ b/pczt/src/roles.rs @@ -11,6 +11,9 @@ pub mod signer; pub mod combiner; +#[cfg(feature = "spend-finalizer")] +pub mod spend_finalizer; + #[cfg(feature = "tx-extractor")] pub mod tx_extractor; diff --git a/pczt/src/roles/spend_finalizer/mod.rs b/pczt/src/roles/spend_finalizer/mod.rs new file mode 100644 index 000000000..7acf1e413 --- /dev/null +++ b/pczt/src/roles/spend_finalizer/mod.rs @@ -0,0 +1,44 @@ +use zcash_primitives::transaction::components::transparent; + +use crate::Pczt; + +pub struct SpendFinalizer { + pczt: Pczt, +} + +impl SpendFinalizer { + /// Instantiates the Spend Finalizer role with the given PCZT. + pub fn new(pczt: Pczt) -> Self { + Self { pczt } + } + + /// Finalizes the spends of the PCZT. + pub fn finalize_spends(self) -> Result { + let Pczt { + global, + transparent, + sapling, + orchard, + } = self.pczt; + + let mut transparent = transparent.into_parsed().map_err(Error::TransparentParse)?; + + transparent + .finalize_spends() + .map_err(Error::TransparentFinalize)?; + + Ok(Pczt { + global, + transparent: crate::transparent::Bundle::serialize_from(transparent), + sapling, + orchard, + }) + } +} + +/// Errors that can occur while finalizing the spends of a PCZT. +#[derive(Debug)] +pub enum Error { + TransparentFinalize(transparent::pczt::SpendFinalizerError), + TransparentParse(transparent::pczt::ParseError), +}