1pub mod builder;
3pub mod coinbase;
4pub mod components;
5pub mod fees;
6pub mod sighash;
7mod sighash_v4;
8mod sighash_v5;
9mod sighash_v6;
10
11pub mod txid;
12pub mod util;
13
14#[cfg(any(test, feature = "test-dependencies"))]
15pub mod tests;
16
17use crate::encoding::{ReadBytesExt, WriteBytesExt};
18use alloc::boxed::Box;
19use blake2b_simd::Hash as Blake2bHash;
20use core::convert::TryFrom;
21use core::fmt::Debug;
22use core::ops::Deref;
23use corez::io::{self, Read, Write};
24
25use ::transparent::bundle::{self as transparent, OutPoint};
26use zcash_encoding::{CompactSize, Vector};
27use zcash_protocol::{
28 consensus::{BlockHeight, BranchId},
29 value::{BalanceError, ZatBalance, Zatoshis},
30};
31use zcash_protocol::consensus::BranchId::*;
33
34use self::{
35 components::{
36 orchard as orchard_serialization, sapling as sapling_serialization,
37 sprout::{self, JsDescription},
38 transparent::{read_txin, read_txout, write_txin, write_txout},
39 },
40 txid::{BlockTxCommitmentDigester, TxIdDigester, to_txid},
41 util::sha256d::{HashReader, HashWriter},
42};
43
44#[cfg(feature = "circuits")]
45use ::sapling::builder as sapling_builder;
46
47use zcash_protocol::constants::{
48 V3_TX_VERSION, V3_VERSION_GROUP_ID, V4_TX_VERSION, V4_VERSION_GROUP_ID, V5_TX_VERSION,
49 V5_VERSION_GROUP_ID,
50};
51
52use zcash_protocol::constants::{V6_TX_VERSION, V6_VERSION_GROUP_ID};
53#[cfg(zcash_unstable = "nutachyon")]
54use zcash_protocol::constants::{V7_TX_VERSION, V7_VERSION_GROUP_ID};
55
56pub use zcash_protocol::TxId;
57
58#[derive(Clone, Copy, Debug, PartialEq, Eq)]
66pub enum TxVersion {
67 Sprout(u32),
72 V3,
76 V4,
79 V5,
83 V6,
85 #[cfg(zcash_unstable = "nutachyon")]
89 V7,
90}
91
92impl TxVersion {
93 pub fn read<R: Read>(mut reader: R) -> io::Result<Self> {
94 let header = reader.read_u32_le()?;
95 let overwintered = (header >> 31) == 1;
96 let version = header & 0x7FFFFFFF;
97
98 if overwintered {
99 match (version, reader.read_u32_le()?) {
100 (V3_TX_VERSION, V3_VERSION_GROUP_ID) => Ok(TxVersion::V3),
101 (V4_TX_VERSION, V4_VERSION_GROUP_ID) => Ok(TxVersion::V4),
102 (V5_TX_VERSION, V5_VERSION_GROUP_ID) => Ok(TxVersion::V5),
103 (V6_TX_VERSION, V6_VERSION_GROUP_ID) => Ok(TxVersion::V6),
104 #[cfg(zcash_unstable = "nutachyon")]
105 (V7_TX_VERSION, V7_VERSION_GROUP_ID) => Ok(TxVersion::V7),
106 _ => Err(io::Error::new(
107 io::ErrorKind::InvalidData,
108 "Unknown transaction format",
109 )),
110 }
111 } else if version >= 1 {
112 Ok(TxVersion::Sprout(version))
113 } else {
114 Err(io::Error::new(
115 io::ErrorKind::InvalidData,
116 "Unknown transaction format",
117 ))
118 }
119 }
120
121 pub fn header(&self) -> u32 {
122 let overwintered = match self {
124 TxVersion::Sprout(_) => 0,
125 _ => 1 << 31,
126 };
127
128 overwintered
129 | match self {
130 TxVersion::Sprout(v) => *v,
131 TxVersion::V3 => V3_TX_VERSION,
132 TxVersion::V4 => V4_TX_VERSION,
133 TxVersion::V5 => V5_TX_VERSION,
134 TxVersion::V6 => V6_TX_VERSION,
135 #[cfg(zcash_unstable = "nutachyon")]
136 TxVersion::V7 => V7_TX_VERSION,
137 }
138 }
139
140 pub fn version_group_id(&self) -> u32 {
141 match self {
142 TxVersion::Sprout(_) => 0,
143 TxVersion::V3 => V3_VERSION_GROUP_ID,
144 TxVersion::V4 => V4_VERSION_GROUP_ID,
145 TxVersion::V5 => V5_VERSION_GROUP_ID,
146 TxVersion::V6 => V6_VERSION_GROUP_ID,
147 #[cfg(zcash_unstable = "nutachyon")]
148 TxVersion::V7 => V7_VERSION_GROUP_ID,
149 }
150 }
151
152 pub fn write<W: Write>(&self, mut writer: W) -> io::Result<()> {
153 writer.write_u32_le(self.header())?;
154 match self {
155 TxVersion::Sprout(_) => Ok(()),
156 _ => writer.write_u32_le(self.version_group_id()),
157 }
158 }
159
160 pub fn has_sprout(&self) -> bool {
162 match self {
163 TxVersion::Sprout(v) => *v >= 2u32,
164 TxVersion::V3 | TxVersion::V4 => true,
165 TxVersion::V5 => false,
166 TxVersion::V6 => false,
167 #[cfg(zcash_unstable = "nutachyon")]
168 TxVersion::V7 => false,
169 }
170 }
171
172 pub fn has_overwinter(&self) -> bool {
173 !matches!(self, TxVersion::Sprout(_))
174 }
175
176 pub fn has_sapling(&self) -> bool {
178 match self {
179 TxVersion::Sprout(_) | TxVersion::V3 => false,
180 TxVersion::V4 => true,
181 TxVersion::V5 => true,
182 TxVersion::V6 => true,
183 #[cfg(zcash_unstable = "nutachyon")]
184 TxVersion::V7 => true,
185 }
186 }
187
188 pub fn has_orchard(&self) -> bool {
190 match self {
191 TxVersion::Sprout(_) | TxVersion::V3 | TxVersion::V4 => false,
192 TxVersion::V5 => true,
193 TxVersion::V6 => true,
194 #[cfg(zcash_unstable = "nutachyon")]
195 TxVersion::V7 => true,
196 }
197 }
198
199 pub fn has_ironwood(&self) -> bool {
201 match self {
202 TxVersion::Sprout(_) | TxVersion::V3 | TxVersion::V4 | TxVersion::V5 => false,
203 TxVersion::V6 => true,
204 #[cfg(zcash_unstable = "nutachyon")]
205 TxVersion::V7 => true,
206 }
207 }
208
209 #[cfg(all(feature = "zip-233", zcash_unstable = "zip233"))]
210 pub fn has_zip233(&self, _consensus_branch_id: BranchId) -> bool {
215 false
216 }
217
218 pub fn suggested_for_branch(consensus_branch_id: BranchId) -> Self {
220 match consensus_branch_id {
221 BranchId::Sprout => TxVersion::Sprout(2),
222 BranchId::Overwinter => TxVersion::V3,
223 BranchId::Sapling | BranchId::Blossom | BranchId::Heartwood | BranchId::Canopy => {
224 TxVersion::V4
225 }
226 BranchId::Nu5 => TxVersion::V5,
227 BranchId::Nu6 => TxVersion::V5,
228 BranchId::Nu6_1 => TxVersion::V5,
229 BranchId::Nu6_2 => TxVersion::V5,
230 BranchId::Nu6_3 => TxVersion::V6,
231 BranchId::Nu7 => TxVersion::V6,
232 #[cfg(zcash_unstable = "nutachyon")]
233 BranchId::NuTachyon => TxVersion::V7,
234 }
235 }
236
237 pub fn valid_in_branch(&self, consensus_branch_id: BranchId) -> bool {
240 match self {
243 TxVersion::Sprout(_) => consensus_branch_id == Sprout,
244 TxVersion::V3 => consensus_branch_id == Overwinter,
245 TxVersion::V4 => match consensus_branch_id {
246 Sprout | Overwinter => false,
247 Sapling | Blossom | Heartwood | Canopy | Nu5 | Nu6 | Nu6_1 | Nu6_2 => true,
248 Nu6_3 => true,
249 Nu7 => false, #[cfg(zcash_unstable = "nutachyon")]
251 NuTachyon => false,
252 },
253 TxVersion::V5 => match consensus_branch_id {
254 Sprout | Overwinter | Sapling | Blossom | Heartwood | Canopy => false,
255 Nu5 | Nu6 | Nu6_1 | Nu6_2 => true,
256 Nu6_3 => true,
257 Nu7 => true,
258 #[cfg(zcash_unstable = "nutachyon")]
259 NuTachyon => true,
260 },
261 TxVersion::V6 => match consensus_branch_id {
262 Sprout | Overwinter | Sapling | Blossom | Heartwood | Canopy | Nu5 | Nu6
263 | Nu6_1 | Nu6_2 => false,
264 Nu6_3 => true, Nu7 => true, #[cfg(zcash_unstable = "nutachyon")]
267 NuTachyon => true,
268 },
269 #[cfg(zcash_unstable = "nutachyon")]
270 TxVersion::V7 => consensus_branch_id == NuTachyon,
271 }
272 }
273}
274
275pub trait Authorization {
277 type TransparentAuth: transparent::Authorization;
278 type SaplingAuth: sapling::bundle::Authorization;
279 type OrchardAuth: orchard::bundle::Authorization;
280}
281
282#[derive(Clone, Debug)]
284pub struct Authorized;
285
286impl Authorization for Authorized {
287 type TransparentAuth = transparent::Authorized;
288 type SaplingAuth = sapling::bundle::Authorized;
289 type OrchardAuth = orchard::bundle::Authorized;
290}
291
292pub struct Unauthorized;
297
298#[cfg(feature = "circuits")]
299impl Authorization for Unauthorized {
300 type TransparentAuth = ::transparent::builder::Unauthorized<Zatoshis>;
301 type SaplingAuth =
302 sapling_builder::InProgress<sapling_builder::Proven, sapling_builder::Unsigned>;
303 type OrchardAuth =
304 orchard::builder::InProgress<orchard::builder::Unproven, orchard::builder::Unauthorized>;
305}
306
307#[cfg(feature = "circuits")]
309struct Coinbase;
310
311#[cfg(feature = "circuits")]
312impl Authorization for Coinbase {
313 type TransparentAuth = coinbase::Coinbase;
314 type SaplingAuth =
315 sapling_builder::InProgress<sapling_builder::Proven, sapling_builder::Unsigned>;
316 type OrchardAuth =
317 orchard::builder::InProgress<orchard::builder::Unproven, orchard::builder::Unauthorized>;
318}
319
320#[derive(Debug)]
322pub struct Transaction {
323 txid: TxId,
324 data: TransactionData<Authorized>,
325}
326
327impl Deref for Transaction {
328 type Target = TransactionData<Authorized>;
329
330 fn deref(&self) -> &TransactionData<Authorized> {
331 &self.data
332 }
333}
334
335impl PartialEq for Transaction {
336 fn eq(&self, other: &Transaction) -> bool {
337 self.txid == other.txid
338 }
339}
340
341#[derive(Debug)]
343pub struct TransactionData<A: Authorization> {
344 version: TxVersion,
345 consensus_branch_id: BranchId,
346 lock_time: u32,
347 expiry_height: BlockHeight,
348 #[cfg(all(feature = "zip-233", zcash_unstable = "zip233"))]
349 zip233_amount: Zatoshis,
350 transparent_bundle: Option<transparent::Bundle<A::TransparentAuth, Zatoshis>>,
351 sprout_bundle: Option<Box<sprout::Bundle>>,
352 sapling_bundle: Option<Box<sapling::Bundle<A::SaplingAuth, ZatBalance>>>,
353 orchard_bundle: Option<Box<orchard::bundle::Bundle<A::OrchardAuth, ZatBalance>>>,
354 ironwood_bundle: Option<Box<orchard::bundle::Bundle<A::OrchardAuth, ZatBalance>>>,
355}
356
357impl Clone for TransactionData<Authorized> {
358 fn clone(&self) -> Self {
359 TransactionData {
360 version: self.version,
361 consensus_branch_id: self.consensus_branch_id,
362 lock_time: self.lock_time,
363 expiry_height: self.expiry_height,
364 #[cfg(all(feature = "zip-233", zcash_unstable = "zip233"))]
365 zip233_amount: self.zip233_amount,
366 transparent_bundle: self.transparent_bundle.clone(),
367 sprout_bundle: self.sprout_bundle.clone(),
368 sapling_bundle: self.sapling_bundle.clone(),
369 orchard_bundle: self.orchard_bundle.clone(),
370 ironwood_bundle: self.ironwood_bundle.clone(),
371 }
372 }
373}
374
375impl Clone for Transaction {
376 fn clone(&self) -> Self {
377 Transaction {
381 txid: self.txid,
382 data: self.data.clone(),
383 }
384 }
385}
386
387impl<A: Authorization> TransactionData<A> {
388 #[allow(clippy::too_many_arguments)]
390 pub fn from_parts(
391 version: TxVersion,
392 consensus_branch_id: BranchId,
393 lock_time: u32,
394 expiry_height: BlockHeight,
395 transparent_bundle: Option<transparent::Bundle<A::TransparentAuth, Zatoshis>>,
396 sprout_bundle: Option<sprout::Bundle>,
397 sapling_bundle: Option<sapling::Bundle<A::SaplingAuth, ZatBalance>>,
398 orchard_bundle: Option<orchard::Bundle<A::OrchardAuth, ZatBalance>>,
399 ) -> Self {
400 TransactionData {
401 version,
402 consensus_branch_id,
403 lock_time,
404 expiry_height,
405 #[cfg(all(feature = "zip-233", zcash_unstable = "zip233"))]
406 zip233_amount: Zatoshis::ZERO,
407 transparent_bundle,
408 sprout_bundle: sprout_bundle.map(Box::new),
409 sapling_bundle: sapling_bundle.map(Box::new),
410 orchard_bundle: orchard_bundle.map(Box::new),
411 ironwood_bundle: None,
412 }
413 }
414
415 #[cfg(all(feature = "zip-233", zcash_unstable = "zip233"))]
420 #[allow(clippy::too_many_arguments)]
421 pub fn from_parts_with_zip233(
422 version: TxVersion,
423 consensus_branch_id: BranchId,
424 lock_time: u32,
425 expiry_height: BlockHeight,
426 zip233_amount: Zatoshis,
427 transparent_bundle: Option<transparent::Bundle<A::TransparentAuth, Zatoshis>>,
428 sprout_bundle: Option<sprout::Bundle>,
429 sapling_bundle: Option<sapling::Bundle<A::SaplingAuth, ZatBalance>>,
430 orchard_bundle: Option<orchard::Bundle<A::OrchardAuth, ZatBalance>>,
431 ) -> Self {
432 TransactionData {
433 version,
434 consensus_branch_id,
435 lock_time,
436 expiry_height,
437 zip233_amount,
438 transparent_bundle,
439 sprout_bundle: sprout_bundle.map(Box::new),
440 sapling_bundle: sapling_bundle.map(Box::new),
441 orchard_bundle: orchard_bundle.map(Box::new),
442 ironwood_bundle: None,
443 }
444 }
445
446 #[allow(clippy::too_many_arguments)]
459 pub fn from_parts_v6(
460 consensus_branch_id: BranchId,
461 lock_time: u32,
462 expiry_height: BlockHeight,
463 transparent_bundle: Option<transparent::Bundle<A::TransparentAuth, Zatoshis>>,
464 sapling_bundle: Option<sapling::Bundle<A::SaplingAuth, ZatBalance>>,
465 orchard_bundle: Option<orchard::Bundle<A::OrchardAuth, ZatBalance>>,
466 ironwood_bundle: Option<orchard::Bundle<A::OrchardAuth, ZatBalance>>,
467 ) -> Self {
468 Self::from_parts_v6_or_v7(
469 TxVersion::V6,
470 consensus_branch_id,
471 lock_time,
472 expiry_height,
473 transparent_bundle,
474 sapling_bundle,
475 orchard_bundle,
476 ironwood_bundle,
477 )
478 }
479
480 #[cfg(all(feature = "zip-233", zcash_unstable = "zip233"))]
485 #[allow(clippy::too_many_arguments)]
486 pub fn from_parts_v6_with_zip233(
487 consensus_branch_id: BranchId,
488 lock_time: u32,
489 expiry_height: BlockHeight,
490 zip233_amount: Zatoshis,
491 transparent_bundle: Option<transparent::Bundle<A::TransparentAuth, Zatoshis>>,
492 sapling_bundle: Option<sapling::Bundle<A::SaplingAuth, ZatBalance>>,
493 orchard_bundle: Option<orchard::Bundle<A::OrchardAuth, ZatBalance>>,
494 ironwood_bundle: Option<orchard::Bundle<A::OrchardAuth, ZatBalance>>,
495 ) -> Self {
496 Self::from_parts_v6_or_v7_with_zip233(
497 TxVersion::V6,
498 consensus_branch_id,
499 lock_time,
500 expiry_height,
501 zip233_amount,
502 transparent_bundle,
503 sapling_bundle,
504 orchard_bundle,
505 ironwood_bundle,
506 )
507 }
508
509 #[cfg(zcash_unstable = "nutachyon")]
511 #[allow(clippy::too_many_arguments)]
512 pub fn from_parts_v7(
513 consensus_branch_id: BranchId,
514 lock_time: u32,
515 expiry_height: BlockHeight,
516 transparent_bundle: Option<transparent::Bundle<A::TransparentAuth, Zatoshis>>,
517 sapling_bundle: Option<sapling::Bundle<A::SaplingAuth, ZatBalance>>,
518 orchard_bundle: Option<orchard::Bundle<A::OrchardAuth, ZatBalance>>,
519 ironwood_bundle: Option<orchard::Bundle<A::OrchardAuth, ZatBalance>>,
520 ) -> Self {
521 Self::from_parts_v6_or_v7(
522 TxVersion::V7,
523 consensus_branch_id,
524 lock_time,
525 expiry_height,
526 transparent_bundle,
527 sapling_bundle,
528 orchard_bundle,
529 ironwood_bundle,
530 )
531 }
532
533 #[cfg(all(
538 zcash_unstable = "nutachyon",
539 feature = "zip-233",
540 zcash_unstable = "zip233"
541 ))]
542 #[allow(clippy::too_many_arguments)]
543 pub fn from_parts_v7_with_zip233(
544 consensus_branch_id: BranchId,
545 lock_time: u32,
546 expiry_height: BlockHeight,
547 zip233_amount: Zatoshis,
548 transparent_bundle: Option<transparent::Bundle<A::TransparentAuth, Zatoshis>>,
549 sapling_bundle: Option<sapling::Bundle<A::SaplingAuth, ZatBalance>>,
550 orchard_bundle: Option<orchard::Bundle<A::OrchardAuth, ZatBalance>>,
551 ironwood_bundle: Option<orchard::Bundle<A::OrchardAuth, ZatBalance>>,
552 ) -> Self {
553 Self::from_parts_v6_or_v7_with_zip233(
554 TxVersion::V7,
555 consensus_branch_id,
556 lock_time,
557 expiry_height,
558 zip233_amount,
559 transparent_bundle,
560 sapling_bundle,
561 orchard_bundle,
562 ironwood_bundle,
563 )
564 }
565
566 #[allow(clippy::too_many_arguments)]
567 fn from_parts_v6_or_v7(
568 version: TxVersion,
569 consensus_branch_id: BranchId,
570 lock_time: u32,
571 expiry_height: BlockHeight,
572 transparent_bundle: Option<transparent::Bundle<A::TransparentAuth, Zatoshis>>,
573 sapling_bundle: Option<sapling::Bundle<A::SaplingAuth, ZatBalance>>,
574 orchard_bundle: Option<orchard::Bundle<A::OrchardAuth, ZatBalance>>,
575 ironwood_bundle: Option<orchard::Bundle<A::OrchardAuth, ZatBalance>>,
576 ) -> Self {
577 debug_assert!(match version {
578 TxVersion::V6 => true,
579 #[cfg(zcash_unstable = "nutachyon")]
580 TxVersion::V7 => true,
581 _ => false,
582 });
583 TransactionData {
584 version,
585 consensus_branch_id,
586 lock_time,
587 expiry_height,
588 #[cfg(all(feature = "zip-233", zcash_unstable = "zip233"))]
589 zip233_amount: Zatoshis::ZERO,
590 transparent_bundle,
591 sprout_bundle: None,
592 sapling_bundle: sapling_bundle.map(Box::new),
593 orchard_bundle: orchard_bundle.map(Box::new),
594 ironwood_bundle: ironwood_bundle.map(Box::new),
595 }
596 }
597
598 #[cfg(all(feature = "zip-233", zcash_unstable = "zip233"))]
599 #[allow(clippy::too_many_arguments)]
600 fn from_parts_v6_or_v7_with_zip233(
601 version: TxVersion,
602 consensus_branch_id: BranchId,
603 lock_time: u32,
604 expiry_height: BlockHeight,
605 zip233_amount: Zatoshis,
606 transparent_bundle: Option<transparent::Bundle<A::TransparentAuth, Zatoshis>>,
607 sapling_bundle: Option<sapling::Bundle<A::SaplingAuth, ZatBalance>>,
608 orchard_bundle: Option<orchard::Bundle<A::OrchardAuth, ZatBalance>>,
609 ironwood_bundle: Option<orchard::Bundle<A::OrchardAuth, ZatBalance>>,
610 ) -> Self {
611 debug_assert!(match version {
612 TxVersion::V6 => true,
613 #[cfg(zcash_unstable = "nutachyon")]
614 TxVersion::V7 => true,
615 _ => false,
616 });
617 TransactionData {
618 version,
619 consensus_branch_id,
620 lock_time,
621 expiry_height,
622 zip233_amount,
623 transparent_bundle,
624 sprout_bundle: None,
625 sapling_bundle: sapling_bundle.map(Box::new),
626 orchard_bundle: orchard_bundle.map(Box::new),
627 ironwood_bundle: ironwood_bundle.map(Box::new),
628 }
629 }
630
631 pub fn version(&self) -> TxVersion {
633 self.version
634 }
635
636 pub fn consensus_branch_id(&self) -> BranchId {
638 self.consensus_branch_id
639 }
640
641 pub fn lock_time(&self) -> u32 {
642 self.lock_time
643 }
644
645 pub fn expiry_height(&self) -> BlockHeight {
646 self.expiry_height
647 }
648
649 pub fn transparent_bundle(&self) -> Option<&transparent::Bundle<A::TransparentAuth, Zatoshis>> {
650 self.transparent_bundle.as_ref()
651 }
652
653 pub fn sprout_bundle(&self) -> Option<&sprout::Bundle> {
654 self.sprout_bundle.as_deref()
655 }
656
657 pub fn sapling_bundle(&self) -> Option<&sapling::Bundle<A::SaplingAuth, ZatBalance>> {
658 self.sapling_bundle.as_deref()
659 }
660
661 pub fn orchard_bundle(&self) -> Option<&orchard::Bundle<A::OrchardAuth, ZatBalance>> {
662 self.orchard_bundle.as_deref()
663 }
664
665 pub fn ironwood_bundle(&self) -> Option<&orchard::Bundle<A::OrchardAuth, ZatBalance>> {
666 self.ironwood_bundle.as_deref()
667 }
668
669 #[cfg(all(feature = "zip-233", zcash_unstable = "zip233"))]
674 pub fn zip233_amount(&self) -> Zatoshis {
675 self.zip233_amount
676 }
677
678 pub fn fee_paid<E, F>(&self, get_prevout: F) -> Result<Option<Zatoshis>, E>
682 where
683 E: From<BalanceError>,
684 F: FnMut(&OutPoint) -> Result<Option<Zatoshis>, E>,
685 {
686 let transparent_balance = self.transparent_bundle.as_ref().map_or_else(
687 || Ok(Some(ZatBalance::zero())),
688 |b| components::transparent::value_balance(b, get_prevout),
689 )?;
690
691 transparent_balance
692 .map(|transparent_balance| {
693 let value_balances = [
694 transparent_balance,
695 self.sprout_bundle.as_ref().map_or_else(
696 || Ok(ZatBalance::zero()),
697 |b| b.value_balance().ok_or(BalanceError::Overflow),
698 )?,
699 self.sapling_bundle
700 .as_ref()
701 .map_or_else(ZatBalance::zero, |b| *b.value_balance()),
702 self.orchard_bundle
703 .as_ref()
704 .map_or_else(ZatBalance::zero, |b| *b.value_balance()),
705 self.ironwood_bundle
706 .as_ref()
707 .map_or_else(ZatBalance::zero, |b| *b.value_balance()),
708 #[cfg(all(feature = "zip-233", zcash_unstable = "zip233"))]
709 -ZatBalance::from(self.zip233_amount),
710 ];
711
712 let overall_balance = value_balances
713 .iter()
714 .sum::<Option<_>>()
715 .ok_or(BalanceError::Overflow)?;
716
717 Zatoshis::try_from(overall_balance).map_err(|_| BalanceError::Underflow)
718 })
719 .transpose()
720 .map_err(E::from)
721 }
722
723 pub fn digest<D: TransactionDigest<A>>(&self, digester: D) -> D::Digest {
729 let header_digest = digester.digest_header(
730 self.version,
731 self.consensus_branch_id,
732 self.lock_time,
733 self.expiry_height,
734 );
735
736 digester.combine(
737 header_digest,
738 digester.digest_transparent(self.transparent_bundle.as_ref()),
739 digester.digest_sapling(self.version, self.sapling_bundle.as_deref()),
740 digester.digest_orchard(self.version, self.orchard_bundle.as_deref()),
741 digester.digest_ironwood(self.ironwood_bundle.as_deref()),
742 )
743 }
744
745 pub(crate) fn fix_consensus_branch_id(mut self, consensus_branch_id: BranchId) -> Self {
750 match self.version() {
751 TxVersion::Sprout(_) | TxVersion::V3 | TxVersion::V4 => {
752 self.consensus_branch_id = consensus_branch_id;
753 }
754 _ => (),
757 }
758 self
759 }
760
761 pub fn map_bundles<B: Authorization>(
769 self,
770 f_transparent: impl FnOnce(
771 Option<transparent::Bundle<A::TransparentAuth, Zatoshis>>,
772 ) -> Option<transparent::Bundle<B::TransparentAuth, Zatoshis>>,
773 f_sapling: impl FnOnce(
774 Option<sapling::Bundle<A::SaplingAuth, ZatBalance>>,
775 ) -> Option<sapling::Bundle<B::SaplingAuth, ZatBalance>>,
776 mut f_orchard: impl FnMut(
777 Option<orchard::bundle::Bundle<A::OrchardAuth, ZatBalance>>,
778 )
779 -> Option<orchard::bundle::Bundle<B::OrchardAuth, ZatBalance>>,
780 ) -> TransactionData<B> {
781 TransactionData {
782 version: self.version,
783 consensus_branch_id: self.consensus_branch_id,
784 lock_time: self.lock_time,
785 expiry_height: self.expiry_height,
786 #[cfg(all(feature = "zip-233", zcash_unstable = "zip233"))]
787 zip233_amount: self.zip233_amount,
788 transparent_bundle: f_transparent(self.transparent_bundle),
789 sprout_bundle: self.sprout_bundle,
790 sapling_bundle: f_sapling(self.sapling_bundle.map(|b| *b)).map(Box::new),
791 orchard_bundle: f_orchard(self.orchard_bundle.map(|b| *b)).map(Box::new),
792 ironwood_bundle: f_orchard(self.ironwood_bundle.map(|b| *b)).map(Box::new),
793 }
794 }
795
796 pub fn try_map_bundles<B: Authorization, E>(
804 self,
805 f_transparent: impl FnOnce(
806 Option<transparent::Bundle<A::TransparentAuth, Zatoshis>>,
807 ) -> Result<
808 Option<transparent::Bundle<B::TransparentAuth, Zatoshis>>,
809 E,
810 >,
811 f_sapling: impl FnOnce(
812 Option<sapling::Bundle<A::SaplingAuth, ZatBalance>>,
813 )
814 -> Result<Option<sapling::Bundle<B::SaplingAuth, ZatBalance>>, E>,
815 mut f_orchard: impl FnMut(
816 Option<orchard::bundle::Bundle<A::OrchardAuth, ZatBalance>>,
817 ) -> Result<
818 Option<orchard::bundle::Bundle<B::OrchardAuth, ZatBalance>>,
819 E,
820 >,
821 ) -> Result<TransactionData<B>, E> {
822 Ok(TransactionData {
823 version: self.version,
824 consensus_branch_id: self.consensus_branch_id,
825 lock_time: self.lock_time,
826 expiry_height: self.expiry_height,
827 #[cfg(all(feature = "zip-233", zcash_unstable = "zip233"))]
828 zip233_amount: self.zip233_amount,
829 transparent_bundle: f_transparent(self.transparent_bundle)?,
830 sprout_bundle: self.sprout_bundle,
831 sapling_bundle: f_sapling(self.sapling_bundle.map(|b| *b))?.map(Box::new),
832 orchard_bundle: f_orchard(self.orchard_bundle.map(|b| *b))?.map(Box::new),
833 ironwood_bundle: f_orchard(self.ironwood_bundle.map(|b| *b))?.map(Box::new),
834 })
835 }
836
837 pub fn map_authorization<B: Authorization>(
838 self,
839 f_transparent: impl transparent::MapAuth<A::TransparentAuth, B::TransparentAuth>,
840 mut f_sapling: impl sapling_serialization::MapAuth<A::SaplingAuth, B::SaplingAuth>,
841 mut f_orchard: impl orchard_serialization::MapAuth<A::OrchardAuth, B::OrchardAuth>,
842 ) -> TransactionData<B> {
843 TransactionData {
844 version: self.version,
845 consensus_branch_id: self.consensus_branch_id,
846 lock_time: self.lock_time,
847 expiry_height: self.expiry_height,
848 #[cfg(all(feature = "zip-233", zcash_unstable = "zip233"))]
849 zip233_amount: self.zip233_amount,
850 transparent_bundle: self
851 .transparent_bundle
852 .map(|b| b.map_authorization(f_transparent)),
853 sprout_bundle: self.sprout_bundle,
854 sapling_bundle: self.sapling_bundle.map(|b| {
855 Box::new(b.map_authorization(
856 &mut f_sapling,
857 |f, p| f.map_spend_proof(p),
858 |f, p| f.map_output_proof(p),
859 |f, s| f.map_auth_sig(s),
860 |f, a| f.map_authorization(a),
861 ))
862 }),
863 orchard_bundle: self.orchard_bundle.map(|b| {
864 Box::new(b.map_authorization(
865 &mut f_orchard,
866 |f, _, s| f.map_spend_auth(s),
867 |f, a| f.map_authorization(a),
868 ))
869 }),
870 ironwood_bundle: self.ironwood_bundle.map(|b| {
871 Box::new(b.map_authorization(
872 &mut f_orchard,
873 |f, _, s| f.map_spend_auth(s),
874 |f, a| f.map_authorization(a),
875 ))
876 }),
877 }
878 }
879}
880
881impl<A: Authorization> TransactionData<A> {
882 pub fn sapling_value_balance(&self) -> ZatBalance {
883 self.sapling_bundle
884 .as_ref()
885 .map_or(ZatBalance::zero(), |b| *b.value_balance())
886 }
887}
888
889impl TransactionData<Authorized> {
890 pub fn freeze(self) -> io::Result<Transaction> {
898 #[cfg(all(feature = "zip-233", zcash_unstable = "zip233"))]
899 if self.zip233_amount != Zatoshis::ZERO
900 && !self.version.has_zip233(self.consensus_branch_id)
901 {
902 return Err(io::Error::new(
903 io::ErrorKind::InvalidInput,
904 "nonzero ZIP 233 amount is not supported by the transaction version and consensus branch",
905 ));
906 }
907
908 Transaction::from_data(self)
909 }
910}
911
912struct V6HeaderFragment {
913 consensus_branch_id: BranchId,
914 lock_time: u32,
915 expiry_height: BlockHeight,
916 #[cfg(all(feature = "zip-233", zcash_unstable = "zip233"))]
917 zip233_amount: Zatoshis,
918}
919
920impl Transaction {
921 fn from_data(data: TransactionData<Authorized>) -> io::Result<Self> {
922 match data.version {
923 TxVersion::Sprout(_) | TxVersion::V3 | TxVersion::V4 => Self::from_data_v4(data),
924 TxVersion::V5 => Ok(Self::from_data_v5(data)),
925 TxVersion::V6 => Ok(Self::from_data_v6(data)),
926 #[cfg(zcash_unstable = "nutachyon")]
927 TxVersion::V7 => Ok(Self::from_data_v6(data)),
928 }
929 }
930
931 fn from_data_v4(data: TransactionData<Authorized>) -> io::Result<Self> {
932 let mut tx = Transaction {
933 txid: TxId::from_bytes([0; 32]),
934 data,
935 };
936 let mut writer = HashWriter::default();
937 tx.write(&mut writer)?;
938 tx.txid = TxId::from_bytes(writer.into_hash().into());
939 Ok(tx)
940 }
941
942 fn from_data_v5(data: TransactionData<Authorized>) -> Self {
943 assert_eq!(data.version, TxVersion::V5);
944
945 let txid = to_txid(
946 data.version,
947 data.consensus_branch_id,
948 &data.digest(TxIdDigester),
949 )
950 .expect("is v5 tx");
951
952 Transaction { txid, data }
953 }
954
955 fn from_data_v6(data: TransactionData<Authorized>) -> Self {
956 #[cfg(not(zcash_unstable = "nutachyon"))]
957 assert_eq!(data.version, TxVersion::V6);
958 #[cfg(zcash_unstable = "nutachyon")]
959 assert!(matches!(data.version, TxVersion::V6 | TxVersion::V7));
960
961 let txid = to_txid(
962 data.version,
963 data.consensus_branch_id,
964 &data.digest(TxIdDigester),
965 )
966 .expect("is v6 tx");
967
968 Transaction { txid, data }
969 }
970
971 pub fn into_data(self) -> TransactionData<Authorized> {
972 self.data
973 }
974
975 pub fn txid(&self) -> TxId {
976 self.txid
977 }
978
979 pub fn read<R: Read>(reader: R, consensus_branch_id: BranchId) -> io::Result<Self> {
980 let mut reader = HashReader::new(reader);
981
982 let version = TxVersion::read(&mut reader)?;
983 match version {
984 TxVersion::Sprout(_) | TxVersion::V3 | TxVersion::V4 => {
985 Self::read_v4(reader, version, consensus_branch_id)
986 }
987 TxVersion::V5 => Self::read_v5(reader.into_base_reader(), version),
988 TxVersion::V6 => Self::read_v6(reader.into_base_reader(), version),
989 #[cfg(zcash_unstable = "nutachyon")]
990 TxVersion::V7 => Self::read_v6(reader.into_base_reader(), version),
991 }
992 }
993
994 #[allow(clippy::redundant_closure)]
995 fn read_v4<R: Read>(
996 mut reader: HashReader<R>,
997 version: TxVersion,
998 consensus_branch_id: BranchId,
999 ) -> io::Result<Self> {
1000 let transparent_bundle = Self::read_transparent(&mut reader)?;
1001
1002 let lock_time = reader.read_u32_le()?;
1003 let expiry_height: BlockHeight = if version.has_overwinter() {
1004 reader.read_u32_le()?.into()
1005 } else {
1006 0u32.into()
1007 };
1008
1009 let (value_balance, shielded_spends, shielded_outputs) =
1010 sapling_serialization::read_v4_components(&mut reader, version.has_sapling())?;
1011
1012 let sprout_bundle = if version.has_sprout() {
1013 let joinsplits = Vector::read(&mut reader, |r| {
1014 JsDescription::read(r, version.has_sapling())
1015 })?;
1016
1017 if !joinsplits.is_empty() {
1018 let mut bundle = sprout::Bundle {
1019 joinsplits,
1020 joinsplit_pubkey: [0; 32],
1021 joinsplit_sig: [0; 64],
1022 };
1023 reader.read_exact(&mut bundle.joinsplit_pubkey)?;
1024 reader.read_exact(&mut bundle.joinsplit_sig)?;
1025 Some(bundle)
1026 } else {
1027 None
1028 }
1029 } else {
1030 None
1031 };
1032
1033 let binding_sig = if version.has_sapling()
1034 && !(shielded_spends.is_empty() && shielded_outputs.is_empty())
1035 {
1036 let mut sig = [0; 64];
1037 reader.read_exact(&mut sig)?;
1038 Some(redjubjub::Signature::from(sig))
1039 } else {
1040 None
1041 };
1042
1043 let mut txid = [0; 32];
1044 let hash_bytes = reader.into_hash();
1045 txid.copy_from_slice(&hash_bytes);
1046
1047 Ok(Transaction {
1048 txid: TxId::from_bytes(txid),
1049 data: TransactionData {
1050 version,
1051 consensus_branch_id,
1052 lock_time,
1053 expiry_height,
1054 #[cfg(all(feature = "zip-233", zcash_unstable = "zip233"))]
1055 zip233_amount: Zatoshis::ZERO,
1056 transparent_bundle,
1057 sprout_bundle: sprout_bundle.map(Box::new),
1058 sapling_bundle: binding_sig
1059 .and_then(|binding_sig| {
1060 sapling::Bundle::from_parts(
1061 shielded_spends,
1062 shielded_outputs,
1063 value_balance,
1064 sapling::bundle::Authorized { binding_sig },
1065 )
1066 })
1067 .map(Box::new),
1068 orchard_bundle: None,
1069 ironwood_bundle: None,
1070 },
1071 })
1072 }
1073
1074 fn read_transparent<R: Read>(
1075 mut reader: R,
1076 ) -> io::Result<Option<transparent::Bundle<transparent::Authorized, Zatoshis>>> {
1077 let vin = Vector::read(&mut reader, |r| read_txin(r))?;
1078 let vout = Vector::read(&mut reader, |r| read_txout(r))?;
1079 Ok(if vin.is_empty() && vout.is_empty() {
1080 None
1081 } else {
1082 Some(transparent::Bundle {
1083 vin,
1084 vout,
1085 authorization: transparent::Authorized,
1086 })
1087 })
1088 }
1089
1090 fn read_amount<R: Read>(mut reader: R) -> io::Result<ZatBalance> {
1091 let mut tmp = [0; 8];
1092 reader.read_exact(&mut tmp)?;
1093 ZatBalance::from_i64_le_bytes(tmp)
1094 .map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "valueBalance out of range"))
1095 }
1096
1097 fn read_v5<R: Read>(mut reader: R, version: TxVersion) -> io::Result<Self> {
1098 let (consensus_branch_id, lock_time, expiry_height) =
1099 Self::read_header_fragment(&mut reader)?;
1100
1101 #[cfg(all(feature = "zip-233", zcash_unstable = "zip233"))]
1102 let zip233_amount = Zatoshis::ZERO;
1103
1104 let transparent_bundle = Self::read_transparent(&mut reader)?;
1105 let sapling_bundle = sapling_serialization::read_v5_bundle(&mut reader)?;
1106 let orchard_bundle =
1107 orchard_serialization::read_v5_bundle(&mut reader, consensus_branch_id)?;
1108
1109 let data = TransactionData {
1110 version,
1111 consensus_branch_id,
1112 lock_time,
1113 expiry_height,
1114 #[cfg(all(feature = "zip-233", zcash_unstable = "zip233"))]
1115 zip233_amount,
1116 transparent_bundle,
1117 sprout_bundle: None,
1118 sapling_bundle: sapling_bundle.map(Box::new),
1119 orchard_bundle: orchard_bundle.map(Box::new),
1120 ironwood_bundle: None,
1121 };
1122
1123 Ok(Self::from_data_v5(data))
1124 }
1125
1126 fn read_v6<R: Read>(mut reader: R, version: TxVersion) -> io::Result<Self> {
1127 let header_fragment = Self::read_v6_header_fragment(&mut reader)?;
1128
1129 let transparent_bundle = Self::read_transparent(&mut reader)?;
1130 let sapling_bundle = sapling_serialization::read_v5_bundle(&mut reader)?;
1131 let orchard_bundle = orchard_serialization::read_v6_bundle(
1132 &mut reader,
1133 header_fragment.consensus_branch_id,
1134 orchard::ValuePool::Orchard,
1135 )?;
1136 let ironwood_bundle = orchard_serialization::read_v6_bundle(
1137 &mut reader,
1138 header_fragment.consensus_branch_id,
1139 orchard::ValuePool::Ironwood,
1140 )?;
1141
1142 let data = TransactionData {
1143 version,
1144 consensus_branch_id: header_fragment.consensus_branch_id,
1145 lock_time: header_fragment.lock_time,
1146 expiry_height: header_fragment.expiry_height,
1147 #[cfg(all(feature = "zip-233", zcash_unstable = "zip233"))]
1148 zip233_amount: header_fragment.zip233_amount,
1149 transparent_bundle,
1150 sprout_bundle: None,
1151 sapling_bundle: sapling_bundle.map(Box::new),
1152 orchard_bundle: orchard_bundle.map(Box::new),
1153 ironwood_bundle: ironwood_bundle.map(Box::new),
1154 };
1155
1156 Ok(Self::from_data_v6(data))
1157 }
1158
1159 fn read_header_fragment<R: Read>(mut reader: R) -> io::Result<(BranchId, u32, BlockHeight)> {
1161 let consensus_branch_id = reader.read_u32_le().and_then(|value| {
1162 BranchId::try_from(value).map_err(|_e| {
1163 io::Error::new(
1164 io::ErrorKind::InvalidData,
1165 #[cfg(not(feature = "std"))]
1166 "invalid consensus branch id",
1167 #[cfg(feature = "std")]
1168 format!(
1169 "invalid consensus branch id 0x{}",
1170 hex::encode(value.to_be_bytes())
1171 ),
1172 )
1173 })
1174 })?;
1175 let lock_time = reader.read_u32_le()?;
1176 let expiry_height: BlockHeight = reader.read_u32_le()?.into();
1177 Ok((consensus_branch_id, lock_time, expiry_height))
1178 }
1179
1180 fn read_v6_header_fragment<R: Read>(mut reader: R) -> io::Result<V6HeaderFragment> {
1181 let (consensus_branch_id, lock_time, expiry_height) =
1182 Self::read_header_fragment(&mut reader)?;
1183
1184 Ok(V6HeaderFragment {
1185 consensus_branch_id,
1186 lock_time,
1187 expiry_height,
1188 #[cfg(all(feature = "zip-233", zcash_unstable = "zip233"))]
1189 zip233_amount: Zatoshis::ZERO,
1190 })
1191 }
1192
1193 #[cfg(feature = "temporary-zcashd")]
1194 pub fn temporary_zcashd_read_v5_sapling<R: Read>(
1195 reader: R,
1196 ) -> io::Result<Option<sapling::Bundle<sapling::bundle::Authorized, ZatBalance>>> {
1197 sapling_serialization::read_v5_bundle(reader)
1198 }
1199
1200 pub fn write<W: Write>(&self, writer: W) -> io::Result<()> {
1201 match self.version {
1202 TxVersion::Sprout(_) | TxVersion::V3 | TxVersion::V4 => self.write_v4(writer),
1203 TxVersion::V5 => self.write_v5(writer),
1204 TxVersion::V6 => self.write_v6(writer),
1205 #[cfg(zcash_unstable = "nutachyon")]
1206 TxVersion::V7 => self.write_v6(writer),
1207 }
1208 }
1209
1210 pub fn write_v4<W: Write>(&self, mut writer: W) -> io::Result<()> {
1211 if self.orchard_bundle.is_some() {
1212 return Err(io::Error::new(
1213 io::ErrorKind::InvalidInput,
1214 "Orchard components cannot be present when serializing to the V4 transaction format.",
1215 ));
1216 }
1217
1218 self.version.write(&mut writer)?;
1219
1220 self.write_transparent(&mut writer)?;
1221 writer.write_u32_le(self.lock_time)?;
1222 if self.version.has_overwinter() {
1223 writer.write_u32_le(u32::from(self.expiry_height))?;
1224 }
1225
1226 sapling_serialization::write_v4_components(
1227 &mut writer,
1228 self.sapling_bundle.as_deref(),
1229 self.version.has_sapling(),
1230 )?;
1231
1232 if self.version.has_sprout() {
1233 if let Some(bundle) = self.sprout_bundle.as_ref() {
1234 Vector::write(&mut writer, &bundle.joinsplits, |w, e| e.write(w))?;
1235 writer.write_all(&bundle.joinsplit_pubkey)?;
1236 writer.write_all(&bundle.joinsplit_sig)?;
1237 } else {
1238 CompactSize::write(&mut writer, 0)?;
1239 }
1240 }
1241
1242 if self.version.has_sapling()
1243 && let Some(bundle) = self.sapling_bundle.as_ref()
1244 {
1245 writer.write_all(&<[u8; 64]>::from(bundle.authorization().binding_sig))?;
1246 }
1247
1248 Ok(())
1249 }
1250
1251 pub fn write_transparent<W: Write>(&self, mut writer: W) -> io::Result<()> {
1252 if let Some(bundle) = &self.transparent_bundle {
1253 Vector::write(&mut writer, &bundle.vin, |w, e| write_txin(w, e))?;
1254 Vector::write(&mut writer, &bundle.vout, |w, e| write_txout(w, e))?;
1255 } else {
1256 CompactSize::write(&mut writer, 0)?;
1257 CompactSize::write(&mut writer, 0)?;
1258 }
1259
1260 Ok(())
1261 }
1262
1263 pub fn write_v5<W: Write>(&self, mut writer: W) -> io::Result<()> {
1264 if self.sprout_bundle.is_some() {
1265 return Err(io::Error::new(
1266 io::ErrorKind::InvalidInput,
1267 "Sprout components cannot be present when serializing to the V5 transaction format.",
1268 ));
1269 }
1270 self.write_v5_header(&mut writer)?;
1271 self.write_transparent(&mut writer)?;
1272 self.write_v5_sapling(&mut writer)?;
1273 orchard_serialization::write_v5_bundle(self.orchard_bundle.as_deref(), &mut writer)?;
1274
1275 Ok(())
1276 }
1277
1278 pub fn write_v6<W: Write>(&self, mut writer: W) -> io::Result<()> {
1279 if self.sprout_bundle.is_some() {
1280 return Err(io::Error::new(
1281 io::ErrorKind::InvalidInput,
1282 "Sprout components cannot be present when serializing to V6 or later transaction formats.",
1283 ));
1284 }
1285 self.write_v6_header(&mut writer)?;
1286
1287 self.write_transparent(&mut writer)?;
1288 self.write_v5_sapling(&mut writer)?;
1289 orchard_serialization::write_v6_bundle(self.orchard_bundle.as_deref(), &mut writer)?;
1290 orchard_serialization::write_v6_bundle(self.ironwood_bundle.as_deref(), &mut writer)?;
1291
1292 Ok(())
1293 }
1294
1295 pub fn write_v5_header<W: Write>(&self, mut writer: W) -> io::Result<()> {
1296 self.version.write(&mut writer)?;
1297 writer.write_u32_le(u32::from(self.consensus_branch_id))?;
1298 writer.write_u32_le(self.lock_time)?;
1299 writer.write_u32_le(u32::from(self.expiry_height))?;
1300 Ok(())
1301 }
1302
1303 pub fn write_v6_header<W: Write>(&self, mut writer: W) -> io::Result<()> {
1304 self.version.write(&mut writer)?;
1305 writer.write_u32_le(u32::from(self.consensus_branch_id))?;
1306 writer.write_u32_le(self.lock_time)?;
1307 writer.write_u32_le(u32::from(self.expiry_height))?;
1308
1309 Ok(())
1310 }
1311
1312 #[cfg(feature = "temporary-zcashd")]
1313 pub fn temporary_zcashd_write_v5_sapling<W: Write>(
1314 sapling_bundle: Option<&sapling::Bundle<sapling::bundle::Authorized, ZatBalance>>,
1315 writer: W,
1316 ) -> io::Result<()> {
1317 sapling_serialization::write_v5_bundle(writer, sapling_bundle)
1318 }
1319
1320 pub fn write_v5_sapling<W: Write>(&self, writer: W) -> io::Result<()> {
1321 sapling_serialization::write_v5_bundle(writer, self.sapling_bundle.as_deref())
1322 }
1323
1324 pub fn auth_commitment(&self) -> Blake2bHash {
1326 self.data.digest(BlockTxCommitmentDigester)
1327 }
1328}
1329
1330#[derive(Clone, Debug)]
1338pub struct TransparentDigests<A> {
1339 pub prevouts_digest: A,
1340 pub sequence_digest: A,
1341 pub outputs_digest: A,
1342}
1343
1344#[derive(Clone, Debug)]
1347pub enum TxDigests<A> {
1348 Zip143(Zip143Digests<A>),
1354 Zip244(Zip244Digests<A>),
1360}
1361
1362#[derive(Clone, Debug)]
1367pub struct Zip143Digests<A> {
1368 pub(crate) transparent: TransparentDigests<A>,
1371 pub(crate) joinsplits: Option<A>,
1373 pub(crate) sapling_spends: Option<A>,
1375 pub(crate) sapling_outputs: Option<A>,
1377}
1378
1379#[derive(Clone, Debug)]
1384pub struct Zip244Digests<A> {
1385 pub(crate) header_digest: A,
1386 pub(crate) transparent_digests: Option<TransparentDigests<A>>,
1387 pub(crate) sapling_digest: Option<A>,
1388 pub(crate) orchard_digest: Option<A>,
1389 pub(crate) ironwood_digest: Option<A>,
1390}
1391
1392impl<A> Zip244Digests<A> {
1393 pub fn header_digest(&self) -> &A {
1395 &self.header_digest
1396 }
1397
1398 pub fn transparent_digests(&self) -> Option<&TransparentDigests<A>> {
1401 self.transparent_digests.as_ref()
1402 }
1403
1404 pub fn sapling_digest(&self) -> Option<&A> {
1407 self.sapling_digest.as_ref()
1408 }
1409
1410 pub fn orchard_digest(&self) -> Option<&A> {
1413 self.orchard_digest.as_ref()
1414 }
1415
1416 pub fn ironwood_digest(&self) -> Option<&A> {
1422 self.ironwood_digest.as_ref()
1423 }
1424}
1425
1426pub trait TransactionDigest<A: Authorization> {
1427 type HeaderDigest;
1428 type TransparentDigest;
1429 type SaplingDigest;
1430 type OrchardDigest;
1431 type IronwoodDigest;
1433
1434 type Digest;
1435
1436 fn digest_header(
1439 &self,
1440 version: TxVersion,
1441 consensus_branch_id: BranchId,
1442 lock_time: u32,
1443 expiry_height: BlockHeight,
1444 ) -> Self::HeaderDigest;
1445
1446 fn digest_transparent(
1447 &self,
1448 transparent_bundle: Option<&transparent::Bundle<A::TransparentAuth, Zatoshis>>,
1449 ) -> Self::TransparentDigest;
1450
1451 fn digest_sapling(
1452 &self,
1453 version: TxVersion,
1454 sapling_bundle: Option<&sapling::Bundle<A::SaplingAuth, ZatBalance>>,
1455 ) -> Self::SaplingDigest;
1456
1457 fn digest_orchard(
1458 &self,
1459 version: TxVersion,
1460 orchard_bundle: Option<&orchard::Bundle<A::OrchardAuth, ZatBalance>>,
1461 ) -> Self::OrchardDigest;
1462
1463 fn digest_ironwood(
1472 &self,
1473 ironwood_bundle: Option<&orchard::Bundle<A::OrchardAuth, ZatBalance>>,
1474 ) -> Self::IronwoodDigest;
1475
1476 fn combine(
1477 &self,
1478 header_digest: Self::HeaderDigest,
1479 transparent_digest: Self::TransparentDigest,
1480 sapling_digest: Self::SaplingDigest,
1481 orchard_digest: Self::OrchardDigest,
1482 ironwood_digest: Self::IronwoodDigest,
1483 ) -> Self::Digest;
1484}
1485
1486pub enum DigestError {
1487 NotSigned,
1488}
1489
1490#[cfg(any(test, feature = "test-dependencies"))]
1491pub mod testing {
1492 use alloc::boxed::Box;
1493
1494 use proptest::prelude::*;
1495
1496 use ::transparent::bundle::testing::{self as transparent};
1497 use zcash_protocol::{consensus::BranchId, value::testing::arb_zatoshis};
1498
1499 use super::{
1500 Authorized, Transaction, TransactionData, TxId, TxVersion,
1501 components::{
1502 orchard::testing::{self as orchard},
1503 sapling::testing::{self as sapling},
1504 },
1505 };
1506
1507 #[cfg(all(feature = "zip-233", zcash_unstable = "zip233"))]
1508 use zcash_protocol::value::{MAX_MONEY, Zatoshis};
1509
1510 pub fn arb_txid() -> impl Strategy<Value = TxId> {
1511 prop::array::uniform32(any::<u8>()).prop_map(TxId::from_bytes)
1512 }
1513
1514 pub fn arb_tx_version(branch_id: BranchId) -> impl Strategy<Value = TxVersion> {
1515 match branch_id {
1516 BranchId::Sprout => (1..=2u32).prop_map(TxVersion::Sprout).boxed(),
1517 BranchId::Overwinter => Just(TxVersion::V3).boxed(),
1518 BranchId::Sapling | BranchId::Blossom | BranchId::Heartwood | BranchId::Canopy => {
1519 Just(TxVersion::V4).boxed()
1520 }
1521 BranchId::Nu5 => Just(TxVersion::V5).boxed(),
1522 BranchId::Nu6 => Just(TxVersion::V5).boxed(),
1523 BranchId::Nu6_1 => Just(TxVersion::V5).boxed(),
1524 BranchId::Nu6_2 => Just(TxVersion::V5).boxed(),
1525 BranchId::Nu6_3 => Just(TxVersion::V6).boxed(),
1526 BranchId::Nu7 => Just(TxVersion::V6).boxed(),
1527 #[cfg(zcash_unstable = "nutachyon")]
1528 BranchId::NuTachyon => Just(TxVersion::V7).boxed(),
1529 }
1530 }
1531
1532 #[cfg(not(all(feature = "zip-233", zcash_unstable = "zip233")))]
1533 prop_compose! {
1534 pub fn arb_txdata(consensus_branch_id: BranchId)(
1535 version in arb_tx_version(consensus_branch_id)
1536 )(
1537 lock_time in any::<u32>(),
1538 expiry_height in any::<u32>(),
1539 transparent_bundle in transparent::arb_bundle(arb_zatoshis()),
1540 sapling_bundle in sapling::arb_bundle_for_version(version),
1541 orchard_bundle in orchard::arb_bundle_for_version(version),
1542 ironwood_bundle in orchard::arb_ironwood_bundle_for_version(version),
1543 version in Just(version),
1544 ) -> TransactionData<Authorized> {
1545 TransactionData {
1546 version,
1547 consensus_branch_id,
1548 lock_time,
1549 expiry_height: expiry_height.into(),
1550 transparent_bundle,
1551 sprout_bundle: None,
1552 sapling_bundle: sapling_bundle.map(Box::new),
1553 orchard_bundle: orchard_bundle.map(Box::new),
1554 ironwood_bundle: ironwood_bundle.map(Box::new),
1555 }
1556 }
1557 }
1558
1559 #[cfg(all(feature = "zip-233", zcash_unstable = "zip233"))]
1560 prop_compose! {
1561 pub fn arb_txdata(consensus_branch_id: BranchId)(
1562 version in arb_tx_version(consensus_branch_id)
1563 )(
1564 lock_time in any::<u32>(),
1565 expiry_height in any::<u32>(),
1566 zip233_amount in 0..=MAX_MONEY,
1567 transparent_bundle in transparent::arb_bundle(arb_zatoshis()),
1568 sapling_bundle in sapling::arb_bundle_for_version(version),
1569 orchard_bundle in orchard::arb_bundle_for_version(version),
1570 ironwood_bundle in orchard::arb_ironwood_bundle_for_version(version),
1571 version in Just(version),
1572 ) -> TransactionData<Authorized> {
1573 TransactionData {
1574 version,
1575 consensus_branch_id,
1576 lock_time,
1577 expiry_height: expiry_height.into(),
1578 zip233_amount: if version.has_zip233(consensus_branch_id) {
1579 Zatoshis::from_u64(zip233_amount).unwrap()
1580 } else {
1581 Zatoshis::ZERO
1582 },
1583 transparent_bundle,
1584 sprout_bundle: None,
1585 sapling_bundle: sapling_bundle.map(Box::new),
1586 orchard_bundle: orchard_bundle.map(Box::new),
1587 ironwood_bundle: ironwood_bundle.map(Box::new),
1588 }
1589 }
1590 }
1591
1592 prop_compose! {
1593 pub fn arb_tx(branch_id: BranchId)(tx_data in arb_txdata(branch_id)) -> Transaction {
1594 Transaction::from_data(tx_data).unwrap()
1595 }
1596 }
1597}