From 763e710edd6f0ff5a2fae13f56d5dc7d13d61743 Mon Sep 17 00:00:00 2001 From: jbesraa Date: Tue, 11 Jun 2024 14:43:13 +0300 Subject: [PATCH] Add Payjoin example ..to run the example `cargo run --example ldk-node-with-payjoin-support` --- Cargo.toml | 5 ++ examples/ldk-node-with-payjoin-support.rs | 74 +++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 examples/ldk-node-with-payjoin-support.rs diff --git a/Cargo.toml b/Cargo.toml index 4ab1ddfa6..a8df39903 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -102,3 +102,8 @@ panic = "abort" [profile.dev] panic = "abort" + +[[example]] +name = "ldk-node-with-payjoin-support" +path = "examples/ldk-node-with-payjoin-support.rs" + diff --git a/examples/ldk-node-with-payjoin-support.rs b/examples/ldk-node-with-payjoin-support.rs new file mode 100644 index 000000000..7f06af77d --- /dev/null +++ b/examples/ldk-node-with-payjoin-support.rs @@ -0,0 +1,74 @@ +use ldk_node::bitcoin::Network; +use ldk_node::{Builder, LogLevel}; + +fn main() { + let mut builder = Builder::new(); + builder.set_log_level(LogLevel::Gossip); + builder.set_network(Network::Testnet); + builder.set_esplora_server("https://blockstream.info/testnet/api".to_string()); + builder.set_gossip_source_rgs( + "https://rapidsync.lightningdevkit.org/testnet/snapshot".to_string(), + ); + + // Payjoin directory is needed only if you are setting up Payjoin receiver, + // not required for Payjoin sender. + let payjoin_directory = "https://payjo.in".to_string(); // Run by `payjoin.org` + // Payjoin relay is required for both Payjoin receiver and sender. + let payjoin_relay = "https://pj.bobspacebkk.com".to_string(); // Run by `bobspace` + + // Enable sending payjoin transactions + // builder.set_payjoin_sender_config(payjoin_relay.clone()); + // ohttp keys refer to the Payjoin directory keys that are needed for the Payjoin receiver + // enrollement. If those keys are not provided the node will attempt to fetch them for you. + let ohttp_keys = None; + // Enable receiving payjoin transactions + builder.set_payjoin_receiver_config(payjoin_directory, payjoin_relay, ohttp_keys); + + let node = builder.build().unwrap(); + + node.start().unwrap(); + + // Receiving payjoin transaction + let payjoin_payment = node.payjoin_payment(); + let amount_to_receive = bitcoin::Amount::from_sat(1000); + let payjoin_uri = tokio::runtime::Runtime::new().unwrap().handle().block_on(async { + let payjoin_uri = payjoin_payment.receive(amount_to_receive).await.unwrap(); + payjoin_uri + }); + let payjoin_uri = payjoin_uri.to_string(); + + println!("Payjoin URI: {}", payjoin_uri); + + //** Open a channel from incoming payjoin transactions ***// + // let payjoin_payment = node.payjoin_payment(); + // let channel_amount_sats = bitcoin::Amount::from_sat(10000); + // use bitcoin::secp256k1::PublicKey; + // use lightning::ln::msgs::SocketAddress; + // let counterparty_node_id: PublicKey = unimplemented!(); + // let counterparty_address: SocketAddress = unimplemented!(); + // let payjoin_uri = match payjoin_payment.receive_with_channel_opening(channel_amount_sats, None, true, + // counterparty_node_id, counterparty_address, + // ).await { + // Ok(a) => a, + // Err(e) => { + // panic!("{}", e); + // }, + // }; + // let payjoin_uri = payjoin_uri.to_string(); + // println!("Payjoin URI: {}", payjoin_uri); + + //** Sending payjoin transaction **// + // let payjoin_uri = payjoin::Uri::try_from(payjoin_uri).unwrap(); + // match payjoin_payment.send(payjoin_uri, None, None).await { + // Ok(Some(txid)) => { + // dbg!("Sent transaction and got a response. Transaction completed") + // }, + // Ok(None) => { + // dbg!("Sent transaction and got no response. We will keep polling the response for the next 24hours") + // }, + // Err(e) => { + // dbg!(e); + // } + // } + node.stop().unwrap(); +}