From f05290c3c291c05ec3114763e9d25cdb7ebcd61a Mon Sep 17 00:00:00 2001 From: Ekrem BAL Date: Mon, 27 Nov 2023 17:06:15 +0300 Subject: [PATCH 1/2] #5 - change wire taget to byte array --- src/wire.rs | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/wire.rs b/src/wire.rs index 228a3da..e438b96 100644 --- a/src/wire.rs +++ b/src/wire.rs @@ -4,13 +4,12 @@ use bitcoin::hashes::sha256; use bitcoin::hashes::Hash; use bitcoin::opcodes::all::*; use bitcoin::ScriptBuf; -use bitcoin::Target; use rand::Rng; #[derive(Clone, Debug)] pub struct Wire { - pub preimages: Option<[Target; 2]>, - pub hashes: [Target; 2], + pub preimages: Option<[[u8; 32]; 2]>, + pub hashes: [[u8; 32]; 2], pub selector: Option, } @@ -18,13 +17,11 @@ impl Wire { pub fn new() -> Self { let mut rng = rand::thread_rng(); - let preimage1 = Target::from_le_bytes(rng.gen()); - let preimage2 = Target::from_le_bytes(rng.gen()); + let preimage1: [u8; 32] = rng.gen(); + let preimage2: [u8; 32] = rng.gen(); - let hash1 = - Target::from_le_bytes(sha256::Hash::hash(&preimage1.to_le_bytes()).to_byte_array()); - let hash2 = - Target::from_le_bytes(sha256::Hash::hash(&preimage2.to_le_bytes()).to_byte_array()); + let hash1 = sha256::Hash::hash(&preimage1).to_byte_array(); + let hash2 = sha256::Hash::hash(&preimage2).to_byte_array(); return Wire { preimages: Some([preimage1, preimage2]), @@ -38,10 +35,10 @@ impl WireTrait for Wire { fn generate_anti_contradiction_script(&self) -> ScriptBuf { Builder::new() .push_opcode(OP_SHA256) - .push_slice(&self.hashes[0].to_le_bytes()) + .push_slice(&self.hashes[0]) .push_opcode(OP_EQUALVERIFY) .push_opcode(OP_SHA256) - .push_slice(&self.hashes[1].to_le_bytes()) + .push_slice(&self.hashes[1]) .push_opcode(OP_EQUAL) .into_script() } @@ -65,4 +62,3 @@ mod tests { // TODO:Test if script returns 1 given input witness with [preimages[0], preimages[1] } } - From 4ccecc3849565345be3cab500655de26e63f513c Mon Sep 17 00:00:00 2001 From: Ekrem BAL Date: Mon, 27 Nov 2023 18:15:10 +0300 Subject: [PATCH 2/2] #2 - Add tests for wire --- Cargo.toml | 3 +++ src/wire.rs | 42 ++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 72ecd19..a65eb28 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,6 @@ edition = "2021" [dependencies] bitcoin = "0.31.0" rand = "0.8.5" + +[dev-dependencies] +bitcoin-scriptexec = { git = "https://github.com/ekrembal/rust-bitcoin-scriptexec" } \ No newline at end of file diff --git a/src/wire.rs b/src/wire.rs index e438b96..7d92bf8 100644 --- a/src/wire.rs +++ b/src/wire.rs @@ -47,6 +47,9 @@ impl WireTrait for Wire { #[cfg(test)] mod tests { use super::*; + use bitcoin::TapLeafHash; + use bitcoin::Transaction; + use bitcoin_scriptexec::*; #[test] fn test_wire() { @@ -58,7 +61,42 @@ mod tests { #[test] fn test_generate_anti_contradiction_script() { let wire = Wire::new(); - let _script = wire.generate_anti_contradiction_script(); - // TODO:Test if script returns 1 given input witness with [preimages[0], preimages[1] + let script = wire.generate_anti_contradiction_script(); + + let preimages_vec = if let Some(preimages) = wire.preimages { + vec![preimages[1].to_vec(), preimages[0].to_vec()] + } else { + panic!("wire preimages are None") + }; + + let mut exec = Exec::new( + ExecCtx::Tapscript, + Options::default(), + TxTemplate { + tx: Transaction { + version: bitcoin::transaction::Version::TWO, + lock_time: bitcoin::locktime::absolute::LockTime::ZERO, + input: vec![], + output: vec![], + }, + prevouts: vec![], + input_idx: 0, + taproot_annex_scriptleaf: Some((TapLeafHash::all_zeros(), None)), + }, + script, + preimages_vec, + ) + .expect("error creating exec"); + + loop { + if exec.exec_next().is_err() { + break; + } + } + + let res = exec.result().unwrap().clone(); + + println!("{:?}", res); + assert_eq!(res.error, None); } }