Skip to content

Commit 763e710

Browse files
committed
Add Payjoin example
..to run the example `cargo run --example ldk-node-with-payjoin-support`
1 parent baf8b50 commit 763e710

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

Cargo.toml

+5
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,8 @@ panic = "abort"
102102

103103
[profile.dev]
104104
panic = "abort"
105+
106+
[[example]]
107+
name = "ldk-node-with-payjoin-support"
108+
path = "examples/ldk-node-with-payjoin-support.rs"
109+
+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
use ldk_node::bitcoin::Network;
2+
use ldk_node::{Builder, LogLevel};
3+
4+
fn main() {
5+
let mut builder = Builder::new();
6+
builder.set_log_level(LogLevel::Gossip);
7+
builder.set_network(Network::Testnet);
8+
builder.set_esplora_server("https://blockstream.info/testnet/api".to_string());
9+
builder.set_gossip_source_rgs(
10+
"https://rapidsync.lightningdevkit.org/testnet/snapshot".to_string(),
11+
);
12+
13+
// Payjoin directory is needed only if you are setting up Payjoin receiver,
14+
// not required for Payjoin sender.
15+
let payjoin_directory = "https://payjo.in".to_string(); // Run by `payjoin.org`
16+
// Payjoin relay is required for both Payjoin receiver and sender.
17+
let payjoin_relay = "https://pj.bobspacebkk.com".to_string(); // Run by `bobspace`
18+
19+
// Enable sending payjoin transactions
20+
// builder.set_payjoin_sender_config(payjoin_relay.clone());
21+
// ohttp keys refer to the Payjoin directory keys that are needed for the Payjoin receiver
22+
// enrollement. If those keys are not provided the node will attempt to fetch them for you.
23+
let ohttp_keys = None;
24+
// Enable receiving payjoin transactions
25+
builder.set_payjoin_receiver_config(payjoin_directory, payjoin_relay, ohttp_keys);
26+
27+
let node = builder.build().unwrap();
28+
29+
node.start().unwrap();
30+
31+
// Receiving payjoin transaction
32+
let payjoin_payment = node.payjoin_payment();
33+
let amount_to_receive = bitcoin::Amount::from_sat(1000);
34+
let payjoin_uri = tokio::runtime::Runtime::new().unwrap().handle().block_on(async {
35+
let payjoin_uri = payjoin_payment.receive(amount_to_receive).await.unwrap();
36+
payjoin_uri
37+
});
38+
let payjoin_uri = payjoin_uri.to_string();
39+
40+
println!("Payjoin URI: {}", payjoin_uri);
41+
42+
//** Open a channel from incoming payjoin transactions ***//
43+
// let payjoin_payment = node.payjoin_payment();
44+
// let channel_amount_sats = bitcoin::Amount::from_sat(10000);
45+
// use bitcoin::secp256k1::PublicKey;
46+
// use lightning::ln::msgs::SocketAddress;
47+
// let counterparty_node_id: PublicKey = unimplemented!();
48+
// let counterparty_address: SocketAddress = unimplemented!();
49+
// let payjoin_uri = match payjoin_payment.receive_with_channel_opening(channel_amount_sats, None, true,
50+
// counterparty_node_id, counterparty_address,
51+
// ).await {
52+
// Ok(a) => a,
53+
// Err(e) => {
54+
// panic!("{}", e);
55+
// },
56+
// };
57+
// let payjoin_uri = payjoin_uri.to_string();
58+
// println!("Payjoin URI: {}", payjoin_uri);
59+
60+
//** Sending payjoin transaction **//
61+
// let payjoin_uri = payjoin::Uri::try_from(payjoin_uri).unwrap();
62+
// match payjoin_payment.send(payjoin_uri, None, None).await {
63+
// Ok(Some(txid)) => {
64+
// dbg!("Sent transaction and got a response. Transaction completed")
65+
// },
66+
// Ok(None) => {
67+
// dbg!("Sent transaction and got no response. We will keep polling the response for the next 24hours")
68+
// },
69+
// Err(e) => {
70+
// dbg!(e);
71+
// }
72+
// }
73+
node.stop().unwrap();
74+
}

0 commit comments

Comments
 (0)