Skip to content

Commit

Permalink
Add Payjoin example
Browse files Browse the repository at this point in the history
..to run the example `cargo run --example ldk-node-with-payjoin-support`
  • Loading branch information
jbesraa committed Jun 24, 2024
1 parent 2cc6132 commit af1c223
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

71 changes: 71 additions & 0 deletions examples/ldk-node-with-payjoin-support.rs
Original file line number Diff line number Diff line change
@@ -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();
}
32 changes: 32 additions & 0 deletions tests/integration_tests_payjoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
3 changes: 2 additions & 1 deletion tests/integration_tests_payjoin_with_channel_opening.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit af1c223

Please sign in to comment.