Skip to content

Commit 8721105

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

4 files changed

+110
-1
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+
+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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();
16+
// Payjoin relay is required for both Payjoin receiver and sender.
17+
let payjoin_relay = "https://pj.bobspacebkk.com".to_string();
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_config(payjoin_directory, payjoin_relay);
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 = payjoin_payment.receive(amount_to_receive).unwrap();
35+
let payjoin_uri = payjoin_uri.to_string();
36+
37+
println!("Payjoin URI: {}", payjoin_uri);
38+
39+
//** Open a channel from incoming payjoin transactions ***//
40+
// let payjoin_payment = node.payjoin_payment();
41+
// let channel_amount_sats = bitcoin::Amount::from_sat(10000);
42+
// use bitcoin::secp256k1::PublicKey;
43+
// use lightning::ln::msgs::SocketAddress;
44+
// let counterparty_node_id: PublicKey = unimplemented!();
45+
// let counterparty_address: SocketAddress = unimplemented!();
46+
// let payjoin_uri = match payjoin_payment.receive_with_channel_opening(channel_amount_sats, None, true,
47+
// counterparty_node_id, counterparty_address,
48+
// ).await {
49+
// Ok(a) => a,
50+
// Err(e) => {
51+
// panic!("{}", e);
52+
// },
53+
// };
54+
// let payjoin_uri = payjoin_uri.to_string();
55+
// println!("Payjoin URI: {}", payjoin_uri);
56+
57+
//** Sending payjoin transaction **//
58+
// let payjoin_uri = payjoin::Uri::try_from(payjoin_uri).unwrap();
59+
// match payjoin_payment.send(payjoin_uri, None, None).await {
60+
// Ok(Some(txid)) => {
61+
// dbg!("Sent transaction and got a response. Transaction completed")
62+
// },
63+
// Ok(None) => {
64+
// dbg!("Sent transaction and got no response. We will keep polling the response for the next 24hours")
65+
// },
66+
// Err(e) => {
67+
// dbg!(e);
68+
// }
69+
// }
70+
node.stop().unwrap();
71+
}

tests/integration_tests_payjoin.rs

+32
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,35 @@ fn send_receive_regular_payjoin_transaction() {
4040
let node_b_balance = node_b_pj_sender.list_balances();
4141
assert!(node_b_balance.total_onchain_balance_sats < premine_amount_sat - 80000);
4242
}
43+
44+
#[test]
45+
fn send_payjoin_with_amount() {
46+
let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();
47+
let (node_a_pj_receiver, node_b_pj_sender) = setup_two_payjoin_nodes(&electrsd, false);
48+
let addr_b = node_b_pj_sender.onchain_payment().new_address().unwrap();
49+
let addr_a = node_a_pj_receiver.onchain_payment().new_address().unwrap();
50+
let premine_amount_sat = 100_000_00;
51+
premine_and_distribute_funds(
52+
&bitcoind.client,
53+
&electrsd.client,
54+
vec![addr_b, addr_a],
55+
Amount::from_sat(premine_amount_sat),
56+
);
57+
node_a_pj_receiver.sync_wallets().unwrap();
58+
node_b_pj_sender.sync_wallets().unwrap();
59+
assert_eq!(node_b_pj_sender.list_balances().spendable_onchain_balance_sats, premine_amount_sat);
60+
assert_eq!(node_a_pj_receiver.list_balances().spendable_onchain_balance_sats, 100_000_00);
61+
assert_eq!(node_a_pj_receiver.next_event(), None);
62+
let payjoin_payment = node_a_pj_receiver.payjoin_payment();
63+
let payjoin_uri = payjoin_payment.receive(Amount::from_sat(100_000_000)).unwrap();
64+
let payjoin_uri = payjoin_uri.to_string();
65+
dbg!(&payjoin_uri);
66+
let sender_payjoin_payment = node_b_pj_sender.payjoin_payment();
67+
assert!(sender_payjoin_payment.send_with_amount(payjoin_uri, 80_000).is_ok());
68+
let txid = expect_payjoin_tx_sent_successfully_event!(node_b_pj_sender);
69+
wait_for_tx(&electrsd.client, txid);
70+
generate_blocks_and_wait(&bitcoind.client, &electrsd.client, 6);
71+
node_b_pj_sender.sync_wallets().unwrap();
72+
let node_b_balance = node_b_pj_sender.list_balances();
73+
assert!(node_b_balance.total_onchain_balance_sats < premine_amount_sat - 80000);
74+
}

tests/integration_tests_payjoin_with_channel_opening.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ fn send_receive_payjoin_transaction_with_channel_opening() {
3939
false,
4040
node_b_pj_sender.node_id(),
4141
node_b_listening_address,
42-
).unwrap();
42+
)
43+
.unwrap();
4344
let payjoin_uri = payjoin_uri.to_string();
4445
let sender_payjoin_payment = node_b_pj_sender.payjoin_payment();
4546
assert!(sender_payjoin_payment.send(payjoin_uri).is_ok());

0 commit comments

Comments
 (0)