Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ members = [
"group",
"pairing",
"zcash_client_backend",
"zcash_extensions_api",
"zcash_extensions",
"zcash_history",
"zcash_primitives",
"zcash_proofs",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
[package]
name = "zcash_extensions_api"
name = "zcash_extensions"
description = "TBD"
version = "0.0.0"
authors = ["Jack Grigg <jack@z.cash>"]
authors = ["Jack Grigg <jack@z.cash>", "Kris Nuttycombe <kris@z.cash>"]
homepage = "https://github.com/zcash/librustzcash"
repository = "https://github.com/zcash/librustzcash"
license = "MIT OR Apache-2.0"
edition = "2018"

[dependencies]
blake2b_simd = "0.5"
zcash_primitives = { version = "0.2", path = "../zcash_primitives" }
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
//! Consensus logic for Transparent Zcash Extensions.

use std::convert::TryFrom;
use zcash_extensions_api::transparent::{Error, Extension, Precondition, Witness};
use zcash_primitives::transaction::components::TzeOut;
use zcash_primitives::transaction::Transaction;
use zcash_primitives::extensions::transparent::{Error, Extension, Precondition, Witness};

use crate::extensions::transparent::demo;
use crate::transaction::components::TzeOut;
use crate::transaction::Transaction;
use crate::transparent::demo;

/// The set of programs that have assigned type IDs within the Zcash consensus rules.
#[derive(Debug, Clone, Copy)]
Expand Down
3 changes: 3 additions & 0 deletions zcash_extensions/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod transparent;
pub mod consensus;

3 changes: 3 additions & 0 deletions zcash_extensions/src/transparent.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
//! Zcash transparent extensions.

pub mod demo;
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
use blake2b_simd::Params;
use std::convert::TryFrom;
use std::fmt;
use zcash_extensions_api::transparent::{Extension, FromPayload, ToPayload};

use crate::transaction::components::TzeOut;
use zcash_primitives::transaction::components::{OutPoint, TzeOut, amount::Amount};
use zcash_primitives::extensions::transparent::{Extension, FromPayload, ToPayload, ExtensionTxBuilder};

mod open {
pub const MODE: usize = 0;
Expand Down Expand Up @@ -273,6 +273,94 @@ impl<C: Context> Extension<C> for Program {
}
}

fn builder_hashes(preimage_1: &[u8; 32], preimage_2: &[u8; 32]) -> ([u8; 32], [u8; 32]) {
let hash_2 = {
let mut hash = [0; 32];
hash.copy_from_slice(Params::new().hash_length(32).hash(preimage_2).as_bytes());
hash
};

let hash_1 = {
let mut hash = [0; 32];
hash.copy_from_slice(
Params::new()
.hash_length(32)
.to_state()
.update(preimage_1)
.update(&hash_2)
.finalize()
.as_bytes(),
);
hash
};

(hash_1, hash_2)
}

pub struct DemoBuilder<'a, B> {
txn_builder: &'a mut B,
extension_id: usize,
}

impl<'a, B: ExtensionTxBuilder<'a>> DemoBuilder<'a, B> {
pub fn demo_open(
&mut self,
value: Amount,
preimage_1: [u8; 32],
preimage_2: [u8; 32]
) -> Result<(), B::BuildError> {
let (hash_1, _) = builder_hashes(&preimage_1, &preimage_2);

// Call through to the generic builder.
self.txn_builder.add_tze_output(
self.extension_id,
value,
&Precondition::open(hash_1),
)
}

pub fn demo_transfer_to_close(
&mut self,
prevout: (OutPoint, TzeOut),
transfer_amount: Amount,
preimage_1: [u8; 32],
preimage_2: [u8; 32]
) -> Result<(), B::BuildError> {
let (_, hash_2) = builder_hashes(&preimage_1, &preimage_2);

// should we eagerly validate the relationship between prevout.1 and preimage_1?
self.txn_builder.add_tze_input(
self.extension_id,
prevout,
move |_| Ok(Witness::open(preimage_1))
)?;

self.txn_builder.add_tze_output(
self.extension_id,
transfer_amount, // can this be > prevout.1.value?
&Precondition::close(hash_2)
)
}

pub fn demo_close(
&mut self,
prevout: (OutPoint, TzeOut),
preimage: [u8; 32]
) -> Result<(), B::BuildError> {
let hash_2 = {
let mut hash = [0; 32];
hash.copy_from_slice(Params::new().hash_length(32).hash(&preimage).as_bytes());
hash
};

self.txn_builder.add_tze_input(
self.extension_id,
prevout,
move |_| Ok(Witness::close(hash_2))
)
}
}

#[cfg(test)]
mod tests {
use blake2b_simd::Params;
Expand Down Expand Up @@ -382,31 +470,52 @@ mod tests {
hash
};

let mut mtx_a = TransactionData::nu4();
mtx_a.tze_outputs.push(TzeOut {
//
// Opening transaction
//

let out_a = TzeOut {
value: Amount::from_u64(1).unwrap(),
precondition: tze::Precondition::from(0, &Precondition::open(hash_1)),
});
};

println!("{:x?}", precondition.payload);

let mut mtx_a = TransactionData::nu4();
mtx_a.tze_outputs.push(out_a);
let tx_a = mtx_a.freeze().unwrap();

let mut mtx_b = TransactionData::nu4();
mtx_b.tze_inputs.push(TzeIn {
//
// Transfer
//

let in_b = TzeIn {
prevout: OutPoint::new(tx_a.txid().0, 0),
witness: tze::Witness::from(0, &Witness::open(preimage_1)),
});
mtx_b.tze_outputs.push(TzeOut {
};
let out_b = TzeOut {
value: Amount::from_u64(1).unwrap(),
precondition: tze::Precondition::from(0, &Precondition::close(hash_2)),
});
};
let mut mtx_b = TransactionData::nu4();
mtx_b.tze_inputs.push(in_b);
mtx_b.tze_outputs.push(out_b);
let tx_b = mtx_b.freeze().unwrap();

let mut mtx_c = TransactionData::nu4();
mtx_c.tze_inputs.push(TzeIn {
//
// Closing transaction
//

let in_c = TzeIn {
prevout: OutPoint::new(tx_b.txid().0, 0),
witness: tze::Witness::from(0, &Witness::close(preimage_2)),
});
};

let mut mtx_c = TransactionData::nu4();
mtx_c.tze_inputs.push(in_c);
let tx_c = mtx_c.freeze().unwrap();


// Verify tx_b
{
let ctx = Ctx { tx: &tx_b };
Expand Down
107 changes: 0 additions & 107 deletions zcash_extensions_api/src/transparent.rs

This file was deleted.

1 change: 0 additions & 1 deletion zcash_primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ ripemd160 = { version = "0.8", optional = true }
secp256k1 = { version = "=0.15.0", optional = true }
sha2 = "0.8"
subtle = "2.2.1"
zcash_extensions_api = { version = "0.0", path = "../zcash_extensions_api" }

[dev-dependencies]
criterion = "0.3"
Expand Down
2 changes: 0 additions & 2 deletions zcash_primitives/src/consensus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
use std::convert::TryFrom;
use std::fmt;

pub mod extensions;

/// Zcash consensus parameters.
pub trait Parameters {
fn activation_height(nu: NetworkUpgrade) -> Option<u32>;
Expand Down
3 changes: 0 additions & 3 deletions zcash_primitives/src/consensus/extensions.rs

This file was deleted.

2 changes: 0 additions & 2 deletions zcash_primitives/src/extensions.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
//! Zcash extensions.

pub mod transparent;
Loading