Files
bft-crdt-experiment/side-node/src/bitcoin/driver/create_htlc.rs

53 lines
1.7 KiB
Rust
Raw Normal View History

2024-07-29 10:03:02 +01:00
use crate::bitcoin::{self, driver};
use crate::utils;
2024-07-29 10:03:02 +01:00
use bdk::wallet::AddressIndex::New;
2024-07-26 15:27:32 +01:00
use bdk::SignOptions;
pub(crate) async fn run() -> anyhow::Result<()> {
tracing::info!("starting htlc flow");
let (dave, sammy) = driver::setup().await?;
2024-07-29 10:03:02 +01:00
let _ = dave.sync();
let _ = sammy.sync();
2024-07-26 15:27:32 +01:00
2024-07-29 11:20:29 +01:00
// Create an HTLC descriptor with a redeem identity, a hashlock from the preimage,
// a refund timelock, and a refund identity
let value = 500;
2024-07-29 10:03:02 +01:00
let recipient = sammy.wallet.get_address(New)?.script_pubkey();
let hash_preimage = "blah".to_string();
let hashlock = utils::sha256(hash_preimage);
let htlc = bitcoin::htlc::Htlc::new(dave.public_key, hashlock, 100, sammy.public_key);
2024-07-29 10:03:02 +01:00
let htlc_descriptor = htlc.to_miniscript_descriptor();
2024-07-29 11:20:29 +01:00
// format a new commitment transaction like in Lightning
let mut commitment_builder = dave.wallet.build_tx();
2024-07-29 11:02:41 +01:00
commitment_builder.add_recipient(recipient, value);
2024-07-26 15:27:32 +01:00
commitment_builder.enable_rbf();
let (psbt, _) = commitment_builder
.finish()
.expect("unable to build commitment");
// sign the commitment transaction
let mut dave_psbt = dave.sign(&mut psbt.clone(), false)?;
let sammy_psbt = sammy.sign(&mut psbt.clone(), false)?;
dave_psbt
.combine(sammy_psbt)
.expect("problem combining bitcoin PSBTs"); // these guys love mutability
let finalized = dave
.wallet
.finalize_psbt(&mut dave_psbt, SignOptions::default())
.expect("couldn't finalize");
assert!(finalized);
let tx = dave_psbt.extract_tx();
let _ = dave.broadcast(&tx)?;
let _ = sammy.sync();
let sammy_balance = sammy.wallet.get_balance()?;
tracing::info!("sammy balance: {}", sammy_balance);
2024-07-29 11:02:57 +01:00
2024-07-26 15:27:32 +01:00
Ok(())
}