From 87211050b20d7b366266e587d6bb2ef157020ac8 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 | 71 +++++++++++++++++++ tests/integration_tests_payjoin.rs | 32 +++++++++ ...tion_tests_payjoin_with_channel_opening.rs | 3 +- 4 files changed, 110 insertions(+), 1 deletion(-) create mode 100644 examples/ldk-node-with-payjoin-support.rs diff --git a/Cargo.toml b/Cargo.toml index 3b9ac2796..aa43bca67 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..627f172b6 --- /dev/null +++ b/examples/ldk-node-with-payjoin-support.rs @@ -0,0 +1,71 @@ +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(); + // Payjoin relay is required for both Payjoin receiver and sender. + let payjoin_relay = "https://pj.bobspacebkk.com".to_string(); + + // 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_config(payjoin_directory, payjoin_relay); + + 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 = payjoin_payment.receive(amount_to_receive).unwrap(); + 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(); +} diff --git a/tests/integration_tests_payjoin.rs b/tests/integration_tests_payjoin.rs index 9a85897ac..f482c5382 100644 --- a/tests/integration_tests_payjoin.rs +++ b/tests/integration_tests_payjoin.rs @@ -40,3 +40,35 @@ fn send_receive_regular_payjoin_transaction() { let node_b_balance = node_b_pj_sender.list_balances(); assert!(node_b_balance.total_onchain_balance_sats < premine_amount_sat - 80000); } + +#[test] +fn send_payjoin_with_amount() { + let (bitcoind, electrsd) = setup_bitcoind_and_electrsd(); + let (node_a_pj_receiver, node_b_pj_sender) = setup_two_payjoin_nodes(&electrsd, false); + let addr_b = node_b_pj_sender.onchain_payment().new_address().unwrap(); + let addr_a = node_a_pj_receiver.onchain_payment().new_address().unwrap(); + let premine_amount_sat = 100_000_00; + premine_and_distribute_funds( + &bitcoind.client, + &electrsd.client, + vec![addr_b, addr_a], + Amount::from_sat(premine_amount_sat), + ); + node_a_pj_receiver.sync_wallets().unwrap(); + node_b_pj_sender.sync_wallets().unwrap(); + assert_eq!(node_b_pj_sender.list_balances().spendable_onchain_balance_sats, premine_amount_sat); + assert_eq!(node_a_pj_receiver.list_balances().spendable_onchain_balance_sats, 100_000_00); + assert_eq!(node_a_pj_receiver.next_event(), None); + let payjoin_payment = node_a_pj_receiver.payjoin_payment(); + let payjoin_uri = payjoin_payment.receive(Amount::from_sat(100_000_000)).unwrap(); + let payjoin_uri = payjoin_uri.to_string(); + dbg!(&payjoin_uri); + let sender_payjoin_payment = node_b_pj_sender.payjoin_payment(); + assert!(sender_payjoin_payment.send_with_amount(payjoin_uri, 80_000).is_ok()); + let txid = expect_payjoin_tx_sent_successfully_event!(node_b_pj_sender); + wait_for_tx(&electrsd.client, txid); + generate_blocks_and_wait(&bitcoind.client, &electrsd.client, 6); + node_b_pj_sender.sync_wallets().unwrap(); + let node_b_balance = node_b_pj_sender.list_balances(); + assert!(node_b_balance.total_onchain_balance_sats < premine_amount_sat - 80000); +} diff --git a/tests/integration_tests_payjoin_with_channel_opening.rs b/tests/integration_tests_payjoin_with_channel_opening.rs index 1b38edfbd..d57868927 100644 --- a/tests/integration_tests_payjoin_with_channel_opening.rs +++ b/tests/integration_tests_payjoin_with_channel_opening.rs @@ -39,7 +39,8 @@ fn send_receive_payjoin_transaction_with_channel_opening() { false, node_b_pj_sender.node_id(), node_b_listening_address, - ).unwrap(); + ) + .unwrap(); let payjoin_uri = payjoin_uri.to_string(); let sender_payjoin_payment = node_b_pj_sender.payjoin_payment(); assert!(sender_payjoin_payment.send(payjoin_uri).is_ok());