Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mmtftr committed Feb 7, 2025
1 parent bb87b61 commit 9e71637
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 33 deletions.
18 changes: 9 additions & 9 deletions core/src/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ mod tests {
use crate::builder::address::create_taproot_address;

use super::*;
use crate::builder::script::{CheckSig, ScriptKind, SpendPath, SpendableScript};
use crate::builder::script::{CheckSig, SpendPath, SpendableScript};
use crate::builder::transaction::input::SpendableTxIn;
use crate::builder::transaction::output::UnspentTxOut;
use crate::builder::transaction::{TxHandler, TxHandlerBuilder};
Expand All @@ -434,11 +434,11 @@ mod tests {
initialize_database,
};
use bitcoin::secp256k1::{schnorr, Message, SecretKey};
use bitcoin::sighash::SighashCache;
use bitcoin::sighash::{Prevouts, TapSighashType};

use bitcoin::sighash::TapSighashType;
use bitcoin::transaction::Transaction;
use bitcoin::Sequence;
use bitcoin::{Amount, Network, OutPoint, ScriptBuf};

use bitcoin::{Amount, Network, OutPoint};
use bitvm::{
execute_script,
signatures::winternitz::{
Expand All @@ -447,7 +447,7 @@ mod tests {
treepp::script,
};
use rand::thread_rng;
use secp256k1::{rand, SECP256K1};
use secp256k1::rand;
use std::env;
use std::str::FromStr;
use std::sync::Arc;
Expand Down Expand Up @@ -583,7 +583,7 @@ mod tests {
fn test_actor_script_spend_sig_valid() {
let sk = SecretKey::new(&mut thread_rng());
let actor = Actor::new(sk, None, Network::Regtest);
let (prevutxo, mut txhandler) = create_script_spend_tx_handler(&actor);
let (_, mut txhandler) = create_script_spend_tx_handler(&actor);

// Actor performs a partial sign for script spend.
// Using an empty signature slice since our dummy CheckSig uses actor signature.
Expand All @@ -604,7 +604,7 @@ mod tests {

// Compute the sighash expected for a pubkey spend (similar to key spend).
let sighash = txhandler
.calculate_script_spend_sighash_indexed(0, 0, TapSighashType::All)
.calculate_script_spend_sighash_indexed(0, 0, TapSighashType::Default)
.expect("Sighash computed");

let message = Message::from_digest(*sighash.as_byte_array());
Expand Down Expand Up @@ -656,7 +656,7 @@ mod tests {

// This transaction is matching with prevouts. Therefore signing will
// be successful.
let mut tx_handler = create_key_spend_tx_handler(&actor).1;
let tx_handler = create_key_spend_tx_handler(&actor).1;
let sighash = tx_handler
.calculate_pubkey_spend_sighash(0, Some(bitcoin::TapSighashType::Default))
.expect("calculating pubkey spend sighash");
Expand Down
2 changes: 1 addition & 1 deletion core/src/builder/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub enum SpendPath {
/// generate a witness, the trait object can be used to generate the script_buf.
///
/// We store [`Arc<dyn SpendableScript>`]s inside a [`super::transaction::TxHandler`] input, and we cast them into a [`ScriptKind`] when signing.
///
///
/// When creating a new Script, make sure you add it to the [`ScriptKind`] enum and add a test for it below.
/// Otherwise, it will not be spendable.
pub trait SpendableScript: Send + Sync + 'static + std::any::Any {
Expand Down
2 changes: 1 addition & 1 deletion core/src/builder/sighash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ pub fn create_nofn_sighash_stream(
bridge_amount_sats: Amount,
network: bitcoin::Network,
) -> impl Stream<Item = Result<(TapSighash, SignatureInfo), BridgeError>> {
use bitcoin::TapSighashType::All as SighashAll;
use bitcoin::TapSighashType::Default as SighashAll;
try_stream! {
// Create move_tx handler. This is unique for each deposit tx.
let move_txhandler = builder::transaction::create_move_to_vault_txhandler(
Expand Down
6 changes: 3 additions & 3 deletions core/src/builder/transaction/txhandler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,12 @@ impl TxHandler<Unsigned> {
move || -> Result<TapSighash, BridgeError> {
match self.txins[idx].get_spend_path() {
SpendPath::KeySpend => {
self.calculate_pubkey_spend_sighash(idx, Some(TapSighashType::All))
self.calculate_pubkey_spend_sighash(idx, Some(TapSighashType::Default))
}
SpendPath::ScriptSpend(script_idx) => self.calculate_script_spend_sighash_indexed(
idx,
script_idx,
TapSighashType::All,
TapSighashType::Default,
),
SpendPath::Unknown => Err(BridgeError::SpendPathNotSpecified),
}
Expand Down Expand Up @@ -167,7 +167,7 @@ impl TxHandler<Unsigned> {
.to_script_buf();

// TODO: remove copy here
self.calculate_script_spend_sighash(txin_index, &script.clone(), sighash_type)
self.calculate_script_spend_sighash(txin_index, &script, sighash_type)
}

pub fn calculate_script_spend_sighash(
Expand Down
25 changes: 6 additions & 19 deletions core/tests/taproot.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use bitcoin::hashes::{Hash, HashEngine};
use bitcoin::secp256k1::Scalar;
use bitcoin::{Address, Amount, TapTweakHash, TxOut};
use bitcoin::{Amount, TxOut};
use bitcoincore_rpc::RpcApi;
use clementine_core::actor::Actor;
use clementine_core::builder::script::{CheckSig, SpendPath, SpendableScript};
Expand Down Expand Up @@ -30,23 +28,11 @@ async fn create_address_and_transaction_then_sign_transaction() {
.unwrap();

let (xonly_pk, _) = config.secret_key.public_key(&SECP).x_only_public_key();
let address = Address::p2tr(&SECP, xonly_pk, None, config.network);
let script = address.script_pubkey();
let tweaked_pk_script: [u8; 32] = script.as_bytes()[2..].try_into().unwrap();

// Calculate tweaked public key.
let mut hasher = TapTweakHash::engine();
hasher.input(&xonly_pk.serialize());
xonly_pk
.add_tweak(
&SECP,
&Scalar::from_be_bytes(TapTweakHash::from_engine(hasher).to_byte_array()).unwrap(),
)
.unwrap();

// Prepare script and address.
let script = Arc::new(CheckSig::new(
bitcoin::XOnlyPublicKey::from_slice(&tweaked_pk_script).unwrap(),
// bitcoin::XOnlyPublicKey::from_slice(&tweaked_pk_script).unwrap(),
xonly_pk,
));
let scripts: Vec<Arc<dyn SpendableScript>> = vec![script.clone()];
let (taproot_address, taproot_spend_info) = builder::address::create_taproot_address(
Expand All @@ -66,7 +52,7 @@ async fn create_address_and_transaction_then_sign_transaction() {

let mut builder = TxHandlerBuilder::new();
builder = builder.add_input(
NormalSignatureKind::NormalSignatureUnknown,
NormalSignatureKind::NotStored,
SpendableTxIn::new(
utxo,
TxOut {
Expand All @@ -76,7 +62,7 @@ async fn create_address_and_transaction_then_sign_transaction() {
scripts.clone(),
Some(taproot_spend_info.clone()),
),
SpendPath::Unknown,
SpendPath::ScriptSpend(0),
DEFAULT_SEQUENCE,
);

Expand All @@ -97,6 +83,7 @@ async fn create_address_and_transaction_then_sign_transaction() {
config.winternitz_secret_key,
config.network,
);

signer
.partial_sign(&mut tx_handler, &[])
.expect("failed to sign transaction");
Expand Down

0 comments on commit 9e71637

Please sign in to comment.