Skip to content

Commit

Permalink
Move transparent protocol code into zcash_transparent
Browse files Browse the repository at this point in the history
Move-only on the relevant files; things are currently broken.
  • Loading branch information
str4d committed Dec 15, 2024
1 parent ff5ec14 commit 1a3eeab
Show file tree
Hide file tree
Showing 30 changed files with 195 additions and 86 deletions.
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ pasta_curves = "0.5"
bip32 = { version = "0.5", default-features = false }
ripemd = "0.1"
secp256k1 = "0.27"
transparent = { package = "zcash_transparent", version = "0.0", path = "zcash_transparent", default-features = false }

# Boilerplate
getset = "0.1"
Expand Down
29 changes: 23 additions & 6 deletions pczt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ serde_with = "3"

# Payment protocols
# - Transparent
secp256k1 = { workspace = true, optional = true }
secp256k1 = { workspace = true, optional = true }
transparent = { workspace = true, optional = true }

# - Sapling
bls12_381 = { workspace = true, optional = true }
Expand All @@ -46,7 +47,10 @@ getset = "0.1"
incrementalmerkletree.workspace = true
secp256k1 = { workspace = true, features = ["rand"] }
shardtree.workspace = true
zcash_primitives = { workspace = true, features = ["test-dependencies", "transparent-inputs"] }
zcash_primitives = { workspace = true, features = [
"test-dependencies",
"transparent-inputs",
] }
zcash_proofs = { workspace = true, features = ["bundled-prover"] }
zip32.workspace = true

Expand All @@ -67,13 +71,26 @@ sapling = [
"dep:zcash_note_encryption",
"dep:zcash_protocol",
]
transparent = ["dep:secp256k1", "dep:zcash_primitives", "dep:zcash_protocol"]
transparent = ["dep:secp256k1", "dep:transparent", "dep:zcash_protocol"]
zcp-builder = ["dep:zcash_primitives", "dep:zcash_protocol"]
io-finalizer = ["orchard", "sapling"]
io-finalizer = ["dep:zcash_primitives", "orchard", "sapling", "transparent"]
prover = ["dep:rand_core", "sapling?/temporary-zcashd"]
signer = ["dep:blake2b_simd", "dep:rand_core", "orchard", "sapling", "transparent"]
signer = [
"dep:blake2b_simd",
"dep:rand_core",
"dep:zcash_primitives",
"orchard",
"sapling",
"transparent",
]
spend-finalizer = ["transparent"]
tx-extractor = ["dep:rand_core", "orchard", "sapling", "transparent"]
tx-extractor = [
"dep:rand_core",
"dep:zcash_primitives",
"orchard",
"sapling",
"transparent",
]

[[test]]
name = "end_to_end"
Expand Down
3 changes: 1 addition & 2 deletions pczt/src/roles/io_finalizer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use rand_core::OsRng;
use zcash_primitives::transaction::{
components::transparent, sighash::SignableInput, sighash_v5::v5_signature_hash,
txid::TxIdDigester,
sighash::SignableInput, sighash_v5::v5_signature_hash, txid::TxIdDigester,
};

use crate::{
Expand Down
3 changes: 1 addition & 2 deletions pczt/src/roles/signer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use blake2b_simd::Hash as Blake2bHash;
use rand_core::OsRng;
use zcash_primitives::transaction::{
components::transparent,
sighash::{SignableInput, SIGHASH_ANYONECANPAY, SIGHASH_NONE, SIGHASH_SINGLE},
sighash_v5::v5_signature_hash,
txid::TxIdDigester,
Expand Down Expand Up @@ -261,7 +260,7 @@ pub(crate) fn pczt_to_tx_data(
pub(crate) struct EffectsOnly;

impl Authorization for EffectsOnly {
type TransparentAuth = transparent::EffectsOnly;
type TransparentAuth = transparent::bundle::EffectsOnly;
type SaplingAuth = sapling::bundle::EffectsOnly;
type OrchardAuth = orchard::bundle::EffectsOnly;
#[cfg(zcash_unstable = "zfuture")]
Expand Down
2 changes: 0 additions & 2 deletions pczt/src/roles/spend_finalizer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use zcash_primitives::transaction::components::transparent;

use crate::Pczt;

pub struct SpendFinalizer {
Expand Down
4 changes: 2 additions & 2 deletions pczt/src/roles/tx_extractor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ impl<'a> TransactionExtractor<'a> {
struct Unbound;

impl Authorization for Unbound {
type TransparentAuth = zcash_primitives::transaction::components::transparent::pczt::Unbound;
type TransparentAuth = ::transparent::pczt::Unbound;
type SaplingAuth = ::sapling::pczt::Unbound;
type OrchardAuth = ::orchard::pczt::Unbound;
#[cfg(zcash_unstable = "zfuture")]
Expand Down Expand Up @@ -250,7 +250,7 @@ impl LockTimeInput for crate::transparent::Input {
}

#[cfg(feature = "transparent")]
impl LockTimeInput for zcash_primitives::transaction::components::transparent::pczt::Input {
impl LockTimeInput for ::transparent::pczt::Input {
fn required_time_lock_time(&self) -> Option<u32> {
*self.required_time_lock_time()
}
Expand Down
4 changes: 2 additions & 2 deletions pczt/src/roles/tx_extractor/transparent.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use zcash_primitives::transaction::components::transparent::{
use transparent::{
bundle::{Authorization, Authorized, Bundle, MapAuth},
pczt::{ParseError, TxExtractorError, Unbound},
Authorization, Authorized, Bundle, MapAuth,
};

pub(super) fn extract_bundle(
Expand Down
4 changes: 1 addition & 3 deletions pczt/src/roles/updater/transparent.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use zcash_primitives::transaction::components::transparent::pczt::{
ParseError, Updater, UpdaterError,
};
use transparent::pczt::{ParseError, Updater, UpdaterError};

use crate::Pczt;

Expand Down
2 changes: 0 additions & 2 deletions pczt/src/roles/verifier/transparent.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use zcash_primitives::transaction::components::transparent;

use crate::Pczt;

impl super::Verifier {
Expand Down
3 changes: 0 additions & 3 deletions pczt/src/transparent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ use getset::Getters;
use serde::{Deserialize, Serialize};
use serde_with::serde_as;

#[cfg(feature = "transparent")]
use zcash_primitives::transaction::components::transparent;

/// PCZT fields that are specific to producing the transaction's transparent bundle (if
/// any).
#[derive(Clone, Debug, Serialize, Deserialize, Getters)]
Expand Down
10 changes: 9 additions & 1 deletion zcash_primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ incrementalmerkletree = { workspace = true, features = ["legacy-api"] }
# - Test dependencies
proptest = { workspace = true, optional = true }

# - Transparent protocol
transparent.workspace = true

# - Transparent inputs
# - `Error` type exposed
bip32.workspace = true
Expand Down Expand Up @@ -111,7 +114,11 @@ default = ["multicore"]
multicore = ["orchard/multicore", "sapling/multicore"]

## Enables spending transparent notes with the transaction builder.
transparent-inputs = ["bip32/secp256k1-ffi", "dep:secp256k1"]
transparent-inputs = [
"transparent/transparent-inputs",
"bip32/secp256k1-ffi",
"dep:secp256k1",
]

### A temporary feature flag that exposes granular APIs needed by `zcashd`. These APIs
### should not be relied upon and will be removed in a future release.
Expand All @@ -122,6 +129,7 @@ test-dependencies = [
"dep:proptest",
"orchard/test-dependencies",
"sapling/test-dependencies",
"transparent/test-dependencies",
"zcash_protocol/test-dependencies",
]

Expand Down
7 changes: 6 additions & 1 deletion zcash_primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@
pub mod block;
pub use zcash_protocol::consensus;
pub use zcash_protocol::constants;
pub mod legacy;
pub use zcash_protocol::memo;
pub mod merkle_tree;
use sapling;
pub mod transaction;
pub use zip32;
#[cfg(zcash_unstable = "zfuture")]
pub mod extensions;

pub mod legacy {
pub use transparent::address::*;
#[cfg(feature = "transparent-inputs")]
pub use transparent::keys;
}
4 changes: 2 additions & 2 deletions zcash_primitives/src/transaction/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
transaction::{
components::{
amount::{Amount, BalanceError},
transparent::{self, builder::TransparentBuilder, TxOut},
transparent::{builder::TransparentBuilder, TxOut},
},
fees::{
transparent::{InputView, OutputView},
Expand Down Expand Up @@ -504,7 +504,7 @@ impl<'a, P: consensus::Parameters, U: sapling::builder::ProverProgress> Builder<
pub fn add_transparent_input(
&mut self,
pubkey: secp256k1::PublicKey,
utxo: transparent::OutPoint,
utxo: transparent::bundle::OutPoint,
coin: TxOut,
) -> Result<(), transparent::builder::Error> {
self.transparent_builder.add_input(pubkey, utxo, coin)
Expand Down
7 changes: 6 additions & 1 deletion zcash_primitives/src/transaction/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ pub mod amount {
pub mod orchard;
pub mod sapling;
pub mod sprout;
pub mod transparent;
#[cfg(zcash_unstable = "zfuture")]
pub mod tze;

pub mod transparent {
pub use transparent::builder;
pub use transparent::bundle::*;
pub use transparent::pczt;
}

pub use self::{
amount::Amount,
sprout::JsDescription,
Expand Down
60 changes: 3 additions & 57 deletions zcash_primitives/src/transaction/sighash.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use blake2b_simd::Hash as Blake2bHash;

use super::{
components::{amount::NonNegativeAmount, transparent},
sighash_v4::v4_signature_hash,
sighash_v5::v5_signature_hash,
Authorization, TransactionData, TxDigests, TxVersion,
components::amount::NonNegativeAmount, sighash_v4::v4_signature_hash,
sighash_v5::v5_signature_hash, Authorization, TransactionData, TxDigests, TxVersion,
};
use crate::{
legacy::Script,
Expand All @@ -14,44 +12,7 @@ use crate::{
#[cfg(zcash_unstable = "zfuture")]
use {super::components::Amount, crate::extensions::transparent::Precondition};

pub const SIGHASH_ALL: u8 = 0x01;
pub const SIGHASH_NONE: u8 = 0x02;
pub const SIGHASH_SINGLE: u8 = 0x03;
pub const SIGHASH_MASK: u8 = 0x1f;
pub const SIGHASH_ANYONECANPAY: u8 = 0x80;

/// A [ZIP 244] sighash type.
///
/// [ZIP 244]: https://zips.z.cash/zip-0244#s-2a-hash-type
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SighashType(u8);

impl SighashType {
pub const ALL: Self = Self(SIGHASH_ALL);
pub const NONE: Self = Self(SIGHASH_NONE);
pub const SINGLE: Self = Self(SIGHASH_SINGLE);
pub const ALL_ANYONECANPAY: Self = Self(SIGHASH_ALL | SIGHASH_ANYONECANPAY);
pub const NONE_ANYONECANPAY: Self = Self(SIGHASH_NONE | SIGHASH_ANYONECANPAY);
pub const SINGLE_ANYONECANPAY: Self = Self(SIGHASH_SINGLE | SIGHASH_ANYONECANPAY);

/// Parses the given `hash_type` using the [ZIP 244] rules.
///
/// [ZIP 244]: https://zips.z.cash/zip-0244#s-2a-hash-type
pub fn parse(hash_type: u8) -> Option<Self> {
match hash_type & !SIGHASH_ANYONECANPAY {
SIGHASH_ALL | SIGHASH_NONE | SIGHASH_SINGLE => Some(Self(hash_type)),
_ => None,
}
}

/// Encodes this `SighashType` using the [ZIP 244] rules.
///
/// [ZIP 244]: https://zips.z.cash/zip-0244#s-2a-hash-type
pub fn encode(&self) -> u8 {
// Correct by construction.
self.0
}
}
pub use transparent::sighash::*;

pub enum SignableInput<'a> {
Shielded,
Expand Down Expand Up @@ -89,21 +50,6 @@ impl AsRef<[u8; 32]> for SignatureHash {
}
}

/// Additional context that is needed to compute signature hashes
/// for transactions that include transparent inputs or outputs.
pub trait TransparentAuthorizingContext: transparent::Authorization {
/// Returns the list of all transparent input amounts, provided
/// so that wallets can commit to the transparent input breakdown
/// without requiring the full data of the previous transactions
/// providing these inputs.
fn input_amounts(&self) -> Vec<NonNegativeAmount>;
/// Returns the list of all transparent input scriptPubKeys, provided
/// so that wallets can commit to the transparent input breakdown
/// without requiring the full data of the previous transactions
/// providing these inputs.
fn input_scriptpubkeys(&self) -> Vec<Script>;
}

/// Computes the signature hash for an input to a transaction, given
/// the full data of the transaction, the input being signed, and the
/// set of precomputed hashes produced in the construction of the
Expand Down
Loading

0 comments on commit 1a3eeab

Please sign in to comment.