pczt 0.9.1

Tools for working with partially-created Zcash transactions
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//! The Creator role (single entity).
//!
//!  - Creates the base PCZT with no information about spends or outputs.

use alloc::collections::BTreeMap;

use crate::{
    Pczt,
    common::{
        FLAG_SHIELDED_MODIFIABLE, FLAG_TRANSPARENT_INPUTS_MODIFIABLE,
        FLAG_TRANSPARENT_OUTPUTS_MODIFIABLE,
    },
    orchard::{Bundle as OrchardBundle, ORCHARD_SPENDS_AND_OUTPUTS_ENABLED},
};

#[cfg(feature = "orchard")]
use crate::orchard::bundle_version_for_revision;

use zcash_protocol::consensus::BranchId;
use zcash_protocol::constants::{
    V5_TX_VERSION, V5_VERSION_GROUP_ID, V6_TX_VERSION, V6_VERSION_GROUP_ID,
};

/// Initial flags allowing any modification.
const INITIAL_TX_MODIFIABLE: u8 = FLAG_TRANSPARENT_INPUTS_MODIFIABLE
    | FLAG_TRANSPARENT_OUTPUTS_MODIFIABLE
    | FLAG_SHIELDED_MODIFIABLE;

/// Errors that can occur when creating a PCZT.
#[derive(Debug)]
pub enum Error {
    /// A v5 transaction's shielded bundle anchors must be set by the Creator,
    /// because they are transaction effecting data and cannot subsequently
    /// change.
    AnchorRequiredForV5,
    /// The transaction version implied by the consensus branch ID does not carry an
    /// Ironwood bundle.
    IronwoodNotSupported,
    /// The consensus branch ID does not correspond to any known network upgrade.
    UnknownConsensusBranchId,
    /// The network upgrade for the consensus branch ID predates the v5
    /// transaction format, so it cannot be used to create a PCZT.
    UnsupportedConsensusBranchId,
    /// The requested Orchard-protocol flags cannot be represented under the
    /// Orchard or Ironwood bundle version implied by the consensus branch ID.
    #[cfg(feature = "orchard")]
    UnrepresentableOrchardFlags,
}

/// Returns the network upgrade for `consensus_branch_id`, rejecting unrecognized
/// branch IDs and any upgrade that predates the v5 transaction format.
fn consensus_branch_id_for_pczt(consensus_branch_id: u32) -> Result<BranchId, Error> {
    match BranchId::try_from(consensus_branch_id).map_err(|_| Error::UnknownConsensusBranchId)? {
        BranchId::Sprout
        | BranchId::Overwinter
        | BranchId::Sapling
        | BranchId::Blossom
        | BranchId::Heartwood
        | BranchId::Canopy => Err(Error::UnsupportedConsensusBranchId),
        branch_id => Ok(branch_id),
    }
}

pub struct Creator {
    tx_version: u32,
    version_group_id: u32,
    consensus_branch_id: BranchId,
    fallback_lock_time: Option<u32>,
    expiry_height: u32,
    coin_type: u32,
    orchard_flags: u8,
    ironwood_flags: u8,
    sapling_anchor: Option<[u8; 32]>,
    orchard_anchor: Option<[u8; 32]>,
    ironwood_anchor: Option<[u8; 32]>,
}

impl Creator {
    /// Creates a new PCZT for the given consensus branch ID.
    ///
    /// The transaction version is implied by the consensus branch ID: the v6
    /// transaction format from NU6.3 onward, and the v5 format for earlier upgrades.
    /// For v5 transactions, `sapling_anchor` and `orchard_anchor` must both be
    /// [`Option::Some`]. For v6 transactions, either anchor may be
    /// [`Option::None`] and restored later.
    ///
    /// # Errors
    ///
    /// Returns [`Error::UnknownConsensusBranchId`] if `consensus_branch_id` is not
    /// a recognized branch ID, or [`Error::UnsupportedConsensusBranchId`] if it
    /// predates the v5 transaction format.
    pub fn new(
        consensus_branch_id: u32,
        expiry_height: u32,
        coin_type: u32,
        sapling_anchor: Option<[u8; 32]>,
        orchard_anchor: Option<[u8; 32]>,
    ) -> Result<Self, Error> {
        let branch_id = consensus_branch_id_for_pczt(consensus_branch_id)?;

        let (tx_version, version_group_id) = match branch_id {
            // Pre-NU5 branches are rejected by `consensus_branch_id_for_pczt`; NU5
            // through NU6.2 use the v5 transaction format.
            BranchId::Sprout
            | BranchId::Overwinter
            | BranchId::Sapling
            | BranchId::Blossom
            | BranchId::Heartwood
            | BranchId::Canopy
            | BranchId::Nu5
            | BranchId::Nu6
            | BranchId::Nu6_1
            | BranchId::Nu6_2 => (V5_TX_VERSION, V5_VERSION_GROUP_ID),
            BranchId::Nu6_3 => (V6_TX_VERSION, V6_VERSION_GROUP_ID),
            #[cfg(zcash_unstable = "nu7")]
            BranchId::Nu7 => (V6_TX_VERSION, V6_VERSION_GROUP_ID),
        };

        Ok(Self {
            tx_version,
            version_group_id,
            consensus_branch_id: branch_id,
            fallback_lock_time: None,
            expiry_height,
            coin_type,
            orchard_flags: ORCHARD_SPENDS_AND_OUTPUTS_ENABLED,
            ironwood_flags: crate::orchard::IRONWOOD_SPENDS_OUTPUTS_AND_CROSS_ADDRESS_ENABLED,
            sapling_anchor,
            orchard_anchor,
            ironwood_anchor: None,
        })
    }

    /// Returns the bundle version in effect for the given Orchard-protocol value pool
    /// under this Creator's consensus branch ID, or `None` if the pool is not
    /// supported under that branch.
    #[cfg(feature = "orchard")]
    fn bundle_version(&self, pool: orchard::ValuePool) -> Option<orchard::bundle::BundleVersion> {
        self.consensus_branch_id
            .orchard_protocol_revision()
            .and_then(|revision| bundle_version_for_revision(revision, pool))
    }

    pub fn with_fallback_lock_time(mut self, fallback: u32) -> Self {
        self.fallback_lock_time = Some(fallback);
        self
    }

    /// Sets the Orchard flags for the PCZT.
    ///
    /// The flags are validated against, and encoded under, the Orchard bundle
    /// version implied by the consensus branch ID passed to [`Creator::new`]
    /// (which also fixes the note-plaintext version).
    ///
    /// # Errors
    ///
    /// Returns [`Error::UnrepresentableOrchardFlags`] if `flags` cannot be encoded
    /// under that bundle version (e.g. cross-address-enabled flags under a
    /// post-NU6.3 Orchard version, or cross-address-disabled flags under a pre-NU6.3
    /// Orchard version).
    #[cfg(feature = "orchard")]
    pub fn with_orchard_flags(
        mut self,
        orchard_flags: orchard::bundle::Flags,
    ) -> Result<Self, Error> {
        self.orchard_flags = orchard_flags
            .to_byte(
                self.bundle_version(orchard::ValuePool::Orchard)
                    .expect("`Creator::new` rejects branches that predate NU5"),
            )
            .ok_or(Error::UnrepresentableOrchardFlags)?;
        Ok(self)
    }

    /// Sets the Ironwood anchor for the PCZT.
    ///
    /// # Errors
    ///
    /// Returns [`Error::IronwoodNotSupported`] if the transaction version implied by
    /// the consensus branch ID passed to [`Creator::new`] does not carry an Ironwood
    /// bundle.
    pub fn with_ironwood_anchor(mut self, ironwood_anchor: [u8; 32]) -> Result<Self, Error> {
        if self.tx_version != V6_TX_VERSION {
            return Err(Error::IronwoodNotSupported);
        }
        self.ironwood_anchor = Some(ironwood_anchor);
        Ok(self)
    }

    /// Sets the Ironwood flags for the PCZT.
    ///
    /// # Errors
    ///
    /// Returns [`Error::IronwoodNotSupported`] if the transaction version implied by
    /// the consensus branch ID passed to [`Creator::new`] does not carry an Ironwood
    /// bundle, or [`Error::UnrepresentableOrchardFlags`] if `flags` cannot be encoded
    /// under the Ironwood bundle version.
    #[cfg(feature = "orchard")]
    pub fn with_ironwood_flags(
        mut self,
        ironwood_flags: orchard::bundle::Flags,
    ) -> Result<Self, Error> {
        self.ironwood_flags = ironwood_flags
            .to_byte(
                self.bundle_version(orchard::ValuePool::Ironwood)
                    .ok_or(Error::IronwoodNotSupported)?,
            )
            .ok_or(Error::UnrepresentableOrchardFlags)?;
        Ok(self)
    }

    /// Builds the initial PCZT.
    ///
    /// # Errors
    ///
    /// Returns [`Error::AnchorRequiredForV5`] if this Creator describes a v5
    /// transaction and either the Sapling anchor is missing from a bundle with
    /// spends, or the Orchard anchor is missing from a bundle with actions.
    pub fn build(self) -> Result<Pczt, Error> {
        let sapling = crate::sapling::Bundle {
            spends: vec![],
            outputs: vec![],
            value_sum: 0,
            anchor: self.sapling_anchor,
            bsk: None,
        };
        let orchard = OrchardBundle {
            actions: vec![],
            flags: self.orchard_flags,
            value_sum: (0, false),
            anchor: self.orchard_anchor,
            // The note-plaintext version is determined by the Orchard bundle version.
            #[cfg(feature = "orchard")]
            note_version: self
                .bundle_version(orchard::ValuePool::Orchard)
                .expect("`Creator::new` rejects branches that predate NU5")
                .note_version(),
            #[cfg(not(feature = "orchard"))]
            note_version: crate::orchard::NoteVersion::V2,
            zkproof: None,
            bsk: None,
        };

        if self.tx_version == V5_TX_VERSION
            && ((sapling.anchor.is_none() && !sapling.spends.is_empty())
                || (orchard.anchor.is_none() && !orchard.actions.is_empty()))
        {
            return Err(Error::AnchorRequiredForV5);
        }

        Ok(Pczt {
            global: crate::common::Global {
                tx_version: self.tx_version,
                version_group_id: self.version_group_id,
                consensus_branch_id: self.consensus_branch_id.into(),
                fallback_lock_time: self.fallback_lock_time,
                expiry_height: self.expiry_height,
                coin_type: self.coin_type,
                tx_modifiable: INITIAL_TX_MODIFIABLE,
                proprietary: BTreeMap::new(),
            },
            transparent: crate::transparent::Bundle {
                inputs: vec![],
                outputs: vec![],
            },
            sapling,
            orchard,
            ironwood: OrchardBundle {
                flags: self.ironwood_flags,
                anchor: self.ironwood_anchor,
                ..crate::orchard::EMPTY_IRONWOOD
            },
        })
    }

    /// Builds a PCZT from the output of a [`Builder`].
    ///
    /// Returns `None` if the `TxVersion` is incompatible with PCZTs.
    ///
    /// [`Builder`]: zcash_primitives::transaction::builder::Builder
    #[cfg(feature = "zcp-builder")]
    pub fn build_from_parts<P: zcash_protocol::consensus::Parameters>(
        parts: zcash_primitives::transaction::builder::PcztParts<P>,
    ) -> Option<Pczt> {
        use ::transparent::sighash::{SIGHASH_ANYONECANPAY, SIGHASH_SINGLE};
        use zcash_protocol::{consensus::NetworkConstants, constants::V4_TX_VERSION};

        use crate::common::FLAG_HAS_SIGHASH_SINGLE;

        let tx_version = match parts.version {
            zcash_primitives::transaction::TxVersion::Sprout(_)
            | zcash_primitives::transaction::TxVersion::V3 => None,
            zcash_primitives::transaction::TxVersion::V4 => Some(V4_TX_VERSION),
            zcash_primitives::transaction::TxVersion::V5 => Some(V5_TX_VERSION),
            zcash_primitives::transaction::TxVersion::V6 => Some(V6_TX_VERSION),
        }?;

        // Spends and outputs not modifiable.
        let mut tx_modifiable = 0b0000_0000;
        // Check if any input is using `SIGHASH_SINGLE` (with or without `ANYONECANPAY`).
        if parts.transparent.as_ref().is_some_and(|bundle| {
            bundle.inputs().iter().any(|input| {
                (input.sighash_type().encode() & !SIGHASH_ANYONECANPAY) == SIGHASH_SINGLE
            })
        }) {
            tx_modifiable |= FLAG_HAS_SIGHASH_SINGLE;
        }

        Some(Pczt {
            global: crate::common::Global {
                tx_version,
                version_group_id: parts.version.version_group_id(),
                consensus_branch_id: parts.consensus_branch_id.into(),
                fallback_lock_time: Some(parts.lock_time),
                expiry_height: parts.expiry_height.into(),
                coin_type: parts.params.network_type().coin_type(),
                tx_modifiable,
                proprietary: BTreeMap::new(),
            },
            transparent: parts
                .transparent
                .map(crate::transparent::Bundle::serialize_from)
                .unwrap_or(crate::transparent::EMPTY_BUNDLE),
            sapling: parts
                .sapling
                .map(crate::sapling::Bundle::serialize_from)
                .unwrap_or(crate::sapling::EMPTY_BUNDLE),
            orchard: parts
                .orchard
                .map(OrchardBundle::serialize_from)
                .unwrap_or(crate::orchard::EMPTY_ORCHARD),
            ironwood: parts
                .ironwood
                .map(OrchardBundle::serialize_from)
                .unwrap_or(crate::orchard::EMPTY_IRONWOOD),
        })
    }
}

#[cfg(test)]
mod tests {
    use zcash_protocol::consensus::BranchId;
    use zcash_protocol::constants::{
        V5_TX_VERSION, V5_VERSION_GROUP_ID, V6_TX_VERSION, V6_VERSION_GROUP_ID,
    };

    use super::{Creator, Error};

    #[test]
    fn tx_version_follows_branch() {
        let pczt = Creator::new(
            BranchId::Nu6_2.into(),
            10_000_000,
            133,
            Some([0; 32]),
            Some([0; 32]),
        )
        .unwrap()
        .build()
        .unwrap();
        assert_eq!(pczt.global.tx_version, V5_TX_VERSION);
        assert_eq!(pczt.global.version_group_id, V5_VERSION_GROUP_ID);

        let pczt = Creator::new(
            BranchId::Nu6_3.into(),
            10_000_000,
            133,
            Some([0; 32]),
            Some([0; 32]),
        )
        .unwrap()
        .build()
        .unwrap();
        assert_eq!(pczt.global.tx_version, V6_TX_VERSION);
        assert_eq!(pczt.global.version_group_id, V6_VERSION_GROUP_ID);
    }

    #[test]
    fn optional_anchors_are_supported_for_empty_bundles() {
        let pczt = Creator::new(BranchId::Nu6_2.into(), 10_000_000, 133, None, Some([0; 32]))
            .unwrap()
            .build()
            .unwrap();
        assert!(pczt.sapling.anchor.is_none());
        assert_eq!(pczt.orchard.anchor, Some([0; 32]));

        let pczt = Creator::new(BranchId::Nu6_2.into(), 10_000_000, 133, Some([0; 32]), None)
            .unwrap()
            .build()
            .unwrap();
        assert_eq!(pczt.sapling.anchor, Some([0; 32]));
        assert!(pczt.orchard.anchor.is_none());

        let pczt = Creator::new(BranchId::Nu6_3.into(), 10_000_000, 133, None, None)
            .unwrap()
            .build()
            .unwrap();

        assert!(pczt.sapling.anchor.is_none());
        assert!(pczt.orchard.anchor.is_none());
    }

    #[test]
    fn ironwood_anchor_requires_v6() {
        assert!(matches!(
            Creator::new(
                BranchId::Nu6_2.into(),
                10_000_000,
                133,
                Some([0; 32]),
                Some([0; 32])
            )
            .unwrap()
            .with_ironwood_anchor([1; 32]),
            Err(Error::IronwoodNotSupported)
        ));

        let pczt = Creator::new(
            BranchId::Nu6_3.into(),
            10_000_000,
            133,
            Some([0; 32]),
            Some([0; 32]),
        )
        .unwrap()
        .with_ironwood_anchor([1; 32])
        .unwrap()
        .build()
        .unwrap();
        assert_eq!(pczt.ironwood.anchor, Some([1; 32]));
    }
}