Skip to content

Commit

Permalink
zcash_primitives: Generalise TransactionData protocol-specific bundles
Browse files Browse the repository at this point in the history
  • Loading branch information
str4d committed May 16, 2024
1 parent 70f412c commit c9c2486
Show file tree
Hide file tree
Showing 10 changed files with 314 additions and 136 deletions.
9 changes: 5 additions & 4 deletions zcash_primitives/src/transaction/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::{
components::{
amount::{Amount, BalanceError},
transparent::{self, builder::TransparentBuilder, TxOut},
AllBundles,
},
fees::FeeRule,
sighash::{signature_hash, SignableInput},
Expand Down Expand Up @@ -285,7 +286,7 @@ pub struct Builder<'a, P, U: sapling::builder::ProverProgress> {
sapling_asks: Vec<sapling::keys::SpendAuthorizingKey>,
orchard_saks: Vec<orchard::keys::SpendAuthorizingKey>,
#[cfg(zcash_unstable = "zfuture")]
tze_builder: TzeBuilder<'a, TransactionData<Unauthorized>>,
tze_builder: TzeBuilder<'a, TransactionData<AllBundles<Unauthorized>>>,
#[cfg(not(zcash_unstable = "zfuture"))]
tze_builder: std::marker::PhantomData<&'a ()>,
progress_notifier: U,
Expand Down Expand Up @@ -737,7 +738,7 @@ impl<'a, P: consensus::Parameters, U: sapling::builder::ProverProgress> Builder<
#[cfg(zcash_unstable = "zfuture")]
let (tze_bundle, tze_signers) = self.tze_builder.build();

let unauthed_tx: TransactionData<Unauthorized> = TransactionData {
let unauthed_tx: TransactionData<AllBundles<Unauthorized>> = TransactionData {
version,
consensus_branch_id: BranchId::for_height(&self.params, self.target_height),
lock_time: 0,
Expand All @@ -756,7 +757,7 @@ impl<'a, P: consensus::Parameters, U: sapling::builder::ProverProgress> Builder<
let txid_parts = unauthed_tx.digest(TxIdDigester);

let transparent_bundle = unauthed_tx.transparent_bundle.clone().map(|b| {
b.apply_signatures(
b.apply_signatures::<_, _, AllBundles<Unauthorized>>(
#[cfg(feature = "transparent-inputs")]
&unauthed_tx,
#[cfg(feature = "transparent-inputs")]
Expand Down Expand Up @@ -832,7 +833,7 @@ impl<'a, P: consensus::Parameters, U: sapling::builder::ProverProgress> Builder<
impl<'a, P: consensus::Parameters, U: sapling::builder::ProverProgress> ExtensionTxBuilder<'a>
for Builder<'a, P, U>
{
type BuildCtx = TransactionData<Unauthorized>;
type BuildCtx = TransactionData<AllBundles<Unauthorized>>;
type BuildError = tze::builder::Error;

fn add_tze_input<WBuilder, W: ToPayload>(
Expand Down
29 changes: 29 additions & 0 deletions zcash_primitives/src/transaction/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,38 @@ pub use crate::sapling::bundle::{OutputDescription, SpendDescription};
#[cfg(zcash_unstable = "zfuture")]
pub use self::tze::{TzeIn, TzeOut};

use super::Authorization;

// π_A + π_B + π_C
pub const GROTH_PROOF_SIZE: usize = 48 + 96 + 48;

/// The protocol-specific bundles of data within a transaction.
pub trait Bundles {
type Transparent: TransparentPart;
type Sprout: SproutPart;
type Sapling: SaplingPart;
type Orchard: OrchardPart;

#[cfg(zcash_unstable = "zfuture")]
type Tze: TzePart;
}

/// Marker type for a transaction that may contain payments within any Zcash protocol.
#[derive(Debug)]
pub struct AllBundles<A: Authorization> {
_auth: PhantomData<A>,
}

impl<A: Authorization> Bundles for AllBundles<A> {
type Transparent = Transparent<A::TransparentAuth>;
type Sprout = Sprout;
type Sapling = Sapling<A::SaplingAuth>;
type Orchard = Orchard<A::OrchardAuth>;

#[cfg(zcash_unstable = "zfuture")]
type Tze = Tze<A::TzeAuth>;
}

/// The protocol-agnostic parts of a shielded bundle.
///
/// The trait methods can be implemented without any knowledge of protocol-specific
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::fmt;
use crate::{
legacy::{Script, TransparentAddress},
transaction::{
self as tx,
components::{
amount::{Amount, BalanceError, NonNegativeAmount},
transparent::{self, Authorization, Authorized, Bundle, TxIn, TxOut},
Expand All @@ -16,7 +17,6 @@ use crate::{
#[cfg(feature = "transparent-inputs")]
use {
crate::transaction::{
self as tx,
components::transparent::OutPoint,
sighash::{signature_hash, SignableInput, SIGHASH_ALL},
TransactionData, TxDigests,
Expand Down Expand Up @@ -239,9 +239,14 @@ impl TransparentAuthorizingContext for Unauthorized {
}

impl Bundle<Unauthorized> {
pub fn apply_signatures(
pub fn apply_signatures<
Sp: tx::sighash_v4::SproutSigDigester,
Sa: tx::sighash_v4::SaplingSigDigester,
B: tx::Bundles<Transparent = tx::Transparent<Unauthorized>, Sprout = Sp, Sapling = Sa>
+ tx::sighash_v5::FutureBundles,
>(
self,
#[cfg(feature = "transparent-inputs")] mtx: &TransactionData<tx::Unauthorized>,
#[cfg(feature = "transparent-inputs")] mtx: &TransactionData<B>,
#[cfg(feature = "transparent-inputs")] txid_parts_cache: &TxDigests<Blake2bHash>,
) -> Bundle<Authorized> {
#[cfg(feature = "transparent-inputs")]
Expand Down
4 changes: 2 additions & 2 deletions zcash_primitives/src/transaction/components/tze/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ impl<'a, BuildCtx> TzeBuilder<'a, BuildCtx> {
impl Bundle<Unauthorized> {
pub fn into_authorized(
self,
unauthed_tx: &tx::TransactionData<tx::Unauthorized>,
signers: Vec<TzeSigner<'_, tx::TransactionData<tx::Unauthorized>>>,
unauthed_tx: &tx::TransactionData<tx::AllBundles<tx::Unauthorized>>,
signers: Vec<TzeSigner<'_, tx::TransactionData<tx::AllBundles<tx::Unauthorized>>>>,
) -> Result<Bundle<Authorized>, Error> {
// Create TZE input witnesses
let payloads = signers
Expand Down
Loading

0 comments on commit c9c2486

Please sign in to comment.