Skip to content

Commit

Permalink
Add encoding/decoding tests for UnifiedQrPayment
Browse files Browse the repository at this point in the history
Introduces an integration test for UnifiedQrPayment.
The test uses dumby/mock values but verifies that receive
correctly generates a URI containing the bitcoin address
and lightning invoice. Parsing the URI is also tested,
printing the same address/invoice generated in receive.

Note
- "bitcoin" in the URI is all caps ex: BITCOIN:BC1QY...
   - I believe it should be a lowercase "bitcoin"
- The lightning invoice would ideally be in all caps as well
   - ex: lighting=LNBC1....
  • Loading branch information
slanesuke committed Jun 5, 2024
1 parent 5f10997 commit e296eee
Showing 1 changed file with 72 additions and 2 deletions.
74 changes: 72 additions & 2 deletions src/payment/unified_qr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,76 @@ impl DeserializationError for Extras {
type Error = Error;
}

// TODO tests for send and receive
#[cfg(test)]
mod test {}
mod tests {
use super::*;
use crate::builder::NodeBuilder;
use crate::config::Config;
use bitcoin::Network;
use std::sync::{Arc, RwLock};
use tokio::runtime::Runtime;

#[test]
fn test_encoding_and_decoding() {
let mut config = Config::default();
config.network = Network::Testnet;

let builder = NodeBuilder::from_config(config);
let node = builder.build().unwrap();

let liq = &node.liquidity_source;
let peerstore = &node.peer_store;
let payment_store = &node.payment_store;
let runtime = Arc::new(RwLock::new(Some(Runtime::new().unwrap())));
let channel_mgr = &node.channel_manager;
let conn_mgr = &node.connection_manager;
let key_mgr = &node.keys_manager;
let config = &node.config;
let log = &node.logger;

let invoice = Bolt11Payment::new(
runtime.clone(),
channel_mgr.clone(),
conn_mgr.clone(),
key_mgr.clone(),
liq.clone(),
payment_store.clone(),
peerstore.clone(),
config.clone(),
log.clone(),
);

let wallet = &node.wallet;
let onchain_payment = OnchainPayment::new(runtime.clone(), wallet.clone(), log.clone());

let unified_qr_payment =
UnifiedQrPayment::new(Arc::new(onchain_payment), Arc::new(invoice), log.clone());

let amount_sats = 1_000_000;
let message = Some("Test message".to_string());
let expiry_sec = 4000;

let uqr_payment = unified_qr_payment.receive(amount_sats, message.clone(), expiry_sec);
match uqr_payment.clone() {
Ok(ref uri) => {
assert!(uri.contains("BITCOIN:"));
assert!(uri.contains("lightning="));
println!("Generated URI: {}", uri);
},
Err(e) => panic!("Failed to generate URI: {:?}", e),
}

let uri = uqr_payment.unwrap();

let parsed_uri: bip21::Uri<NetworkUnchecked, Extras> =
uri.parse().expect("Failed to parse URI");
let parsed_uri = parsed_uri.require_network(Network::Testnet).expect("Invalid network");

println!("\nParsed Bitcoin address: {}", parsed_uri.address);
if let Some(invoice) = parsed_uri.extras.bolt11_invoice {
println!("\nParsed Lightning invoice: {}", invoice);
} else {
println!("No Lightning invoice found");
}
}
}

0 comments on commit e296eee

Please sign in to comment.