HTLC broadcast working

This commit is contained in:
Dave Hrycyszyn
2024-07-29 10:03:02 +01:00
parent 918544a76b
commit e4eedbd206
4 changed files with 52 additions and 16 deletions

View File

@@ -1,7 +1,7 @@
use std::fs;
use bdk::{
bitcoin::{psbt::PartiallySignedTransaction, Network, Transaction},
bitcoin::{psbt::PartiallySignedTransaction, secp256k1::PublicKey, Network, Transaction},
blockchain::EsploraBlockchain,
database::MemoryDatabase,
keys::{bip39::Mnemonic, DerivableKey, ExtendedKey},
@@ -17,6 +17,7 @@ pub struct BitcoinClient {
pub(crate) blockchain: bdk::blockchain::EsploraBlockchain,
name: String,
pub(crate) wallet: Wallet<MemoryDatabase>,
pub(crate) external_public_key: PublicKey,
}
impl BitcoinClient {
@@ -91,22 +92,27 @@ impl BitcoinClient {
.into_xprv(Network::Signet)
.expect("couldn't turn xkey into xprv");
let external_descriptor = Bip84(xprv, KeychainKind::External);
let secp = bdk::bitcoin::secp256k1::Secp256k1::new();
let external_descriptor1 = Bip84(xprv.clone(), KeychainKind::External);
let external_descriptor2 = Bip84(xprv, KeychainKind::External);
let internal_descriptor = Some(Bip84(xprv, KeychainKind::Internal));
let wallet = Wallet::new(
external_descriptor,
external_descriptor1,
internal_descriptor,
network,
MemoryDatabase::default(),
)?;
let blockchain = EsploraBlockchain::new("https://mutinynet.com/api", 20);
let external_public_key = external_descriptor2.0.private_key.public_key(&secp);
let esplora = BitcoinClient {
name: name.to_string(),
wallet,
blockchain,
external_public_key,
};
Ok(esplora)

View File

@@ -1,15 +1,33 @@
use crate::bitcoin::driver;
use bdk::wallet::AddressIndex;
use crate::bitcoin::{self, driver};
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?;
tracing::info!("syncing dave wallet");
let _ = dave.sync();
tracing::info!("syncing samy wallet");
let _ = sammy.sync();
// format a new commitment transaction like in Lightning
let mut commitment_builder = dave.wallet.build_tx();
let amount = 500;
let recipient = sammy.wallet.get_address(AddressIndex::New)?.script_pubkey();
let recipient = sammy.wallet.get_address(New)?.script_pubkey();
// Feed it 500 sats, a redeem identity, a hashlock, a refund timelock, and a refund identity
//
let htlc = bitcoin::htlc::Htlc::new(
amount,
dave.external_public_key,
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff".to_string(),
100,
sammy.external_public_key,
);
let htlc_descriptor = htlc.to_miniscript_descriptor();
tracing::info!("descriptor: {}", htlc_descriptor);
commitment_builder.add_recipient(recipient, amount);
commitment_builder.enable_rbf();
let (psbt, _) = commitment_builder

View File

@@ -1,14 +1,26 @@
use std::str::FromStr;
use bdk::miniscript::policy;
use crate::bitcoin;
pub(crate) async fn run() -> anyhow::Result<()> {
let (dave, sammy) = bitcoin::driver::setup().await?;
let (dave, _sammy) = bitcoin::driver::setup().await?;
tracing::info!("starting transfer policy flow");
let policy = format!(
"pk({:?})",
dave.wallet.get_address(bdk::wallet::AddressIndex::New)
);
let policy_str = format!("sh({:?})", dave.external_public_key);
let policy = policy::Concrete::<bdk::bitcoin::PublicKey>::from_str(&policy_str)
.expect("policy compilation failed")
.to_owned();
// let descriptor = Wsh::new(policy)
// .compile()
// .expect("descriptor compilation failed");
// let policy = policy::Concrete::<bdk::bitcoin::PublicKey>::from_str(&policy_str)
// .expect("policy compilation failed");
tracing::info!("policy: {}", policy);
Ok(())

View File

@@ -1,7 +1,7 @@
use std::str::FromStr;
use bdk::{
bitcoin::Address,
bitcoin::secp256k1::PublicKey,
miniscript::{descriptor::Wsh, policy::Concrete},
};
@@ -12,20 +12,20 @@ use bdk::{
/// Alternately, if the refund timelock expires, the value can be refunded to the refund_identity.
pub(crate) struct Htlc {
value: u64,
redeem_identity: Address,
redeem_identity: PublicKey,
hashlock: String,
refund_timelock: u64,
refund_indentiy: Address,
refund_indentiy: PublicKey,
}
impl Htlc {
/// Create a new HTLC.
pub(crate) fn new(
value: u64,
redeem_identity: Address,
redeem_identity: PublicKey,
hashlock: String,
refund_timelock: u64,
refund_indentiy: Address,
refund_indentiy: PublicKey,
) -> Self {
Self {
value,