use bitcoin::sighash::{Prevouts, SighashCache};
use bitcoin::{TapSighashType, Transaction, TxOut};
use crate::BtcError;
#[derive(Debug, Clone)]
pub struct BtcSendPlan {
pub unsigned_tx: Transaction,
pub prevout: TxOut,
}
#[derive(Debug, Clone)]
pub struct UnsignedBtcTx {
pub unsigned_tx: Transaction,
pub sighash: [u8; 32],
}
pub fn build_unsigned_tx(plan: BtcSendPlan) -> Result<UnsignedBtcTx, BtcError> {
let mut cache = SighashCache::new(&plan.unsigned_tx);
let prevouts = [plan.prevout.clone()];
let h = cache
.taproot_key_spend_signature_hash(0, &Prevouts::All(&prevouts), TapSighashType::Default)
.map_err(|e| BtcError::Bitcoin(format!("taproot sighash: {e}")))?;
Ok(UnsignedBtcTx {
unsigned_tx: plan.unsigned_tx,
sighash: *h.as_ref(),
})
}