Renamed htlc to create_htlc so the driver is distinguished from the htlc model

This commit is contained in:
Dave Hrycyszyn
2024-07-29 11:08:29 +01:00
parent f78784973c
commit cea9d8ead5
3 changed files with 2 additions and 2 deletions

View File

@@ -0,0 +1,54 @@
use crate::bitcoin::{self, driver};
use crate::utils;
use bdk::wallet::AddressIndex::New;
use bdk::SignOptions;
pub(crate) async fn run() -> anyhow::Result<()> {
tracing::info!("starting htlc flow");
let (dave, sammy) = driver::setup().await?;
let _ = dave.sync();
let _ = sammy.sync();
// format a new commitment transaction like in Lightning
let mut commitment_builder = dave.wallet.build_tx();
let value = 500;
let recipient = sammy.wallet.get_address(New)?.script_pubkey();
let hash_preimage = "blah".to_string();
let hashlock = utils::sha256(hash_preimage);
// Feed it a redeem identity, a hashlock from the preimage, a refund timelock, and a refund identity
//
let htlc = bitcoin::htlc::Htlc::new(dave.public_key, hashlock, 100, sammy.public_key);
let htlc_descriptor = htlc.to_miniscript_descriptor();
commitment_builder.add_recipient(recipient, value);
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);
Ok(())
}