Skip to content

Commit

Permalink
Merge pull request #11 from chainwayxyz/ekrem/minor-changes
Browse files Browse the repository at this point in the history
Ekrem/minor changes
  • Loading branch information
ekrembal authored Nov 28, 2023
2 parents f6e870d + f608bad commit 6e858b6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 14 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
63 changes: 49 additions & 14 deletions src/wire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ use bitcoin::hashes::sha256;
use bitcoin::hashes::Hash;
use bitcoin::opcodes::all::*;
use bitcoin::ScriptBuf;
use bitcoin::Target;
use rand::Rng;

#[derive(Clone)]
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<bool>,
pub index: usize,
}
Expand All @@ -33,13 +32,11 @@ impl Wire {
pub fn new(index: usize) -> 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();

Wire {
preimages: Some([preimage1, preimage2]),
Expand All @@ -54,10 +51,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()
}
Expand All @@ -66,6 +63,9 @@ impl WireTrait for Wire {
#[cfg(test)]
mod tests {
use super::*;
use bitcoin::TapLeafHash;
use bitcoin::Transaction;
use bitcoin_scriptexec::*;

#[test]
fn test_wire() {
Expand All @@ -76,8 +76,43 @@ mod tests {

#[test]
fn test_generate_anti_contradiction_script() {
let wire = Wire::new(0);
let _script = wire.generate_anti_contradiction_script();
// TODO:Test if script returns 1 given input witness with [preimages[0], preimages[1]
let wire = Wire::new();
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);
}
}

0 comments on commit 6e858b6

Please sign in to comment.