Skip to content

Commit acb56b7

Browse files
committed
f Fix parsing for Bolt11 and Bolt12 payments in URI
Updated in `unifed_qr.rs`: - I updated `deserialize_temp` to handle different network prefixes for Bolt11 invoices (lnbc, lntb, lnbcrt, lnsb) and Bolt12 offers (lno). - Added parsing logic based on the mentioned prefixes. - Ensured that both Bolt11 and Bolt12 payment tests pass correctly. Test: - Verified that `unified_qr_send_receive` handled Bolt11 invoices and Bolt12 offers correctly. - Added Bolt12 offer to URI and had a successful payment sent. - Then added a fallback Bolt11 payment that was successful - Tested node_b to verify it received both lighting payments.
1 parent 35e2e43 commit acb56b7

File tree

2 files changed

+57
-42
lines changed

2 files changed

+57
-42
lines changed

src/payment/unified_qr.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,19 @@ impl<'a> bip21::de::DeserializationState<'a> for DeserializationState {
214214
if key == "lightning" {
215215
let lighting_str = String::try_from(value).map_err(|_| Error::UriParameterFailed)?;
216216

217-
for part in lighting_str.split('&') {
218-
if part.starts_with("LN") || part.starts_with("ln") {
217+
for prefix in lighting_str.split('&') {
218+
let prefix_lowercase = prefix.to_lowercase();
219+
220+
if prefix_lowercase.starts_with("lnbc")
221+
|| prefix_lowercase.starts_with("lntb")
222+
|| prefix_lowercase.starts_with("lnbcrt")
223+
|| prefix_lowercase.starts_with("lnsb")
224+
{
219225
let invoice =
220-
part.parse::<Bolt11Invoice>().map_err(|_| Error::InvalidInvoice)?;
226+
prefix.parse::<Bolt11Invoice>().map_err(|_| Error::InvalidInvoice)?;
221227
self.bolt11_invoice = Some(invoice)
222-
} else if part.starts_with("LNO") || part.starts_with("lno") {
223-
let offer = part.parse::<Offer>().map_err(|_| Error::InvalidOffer)?;
228+
} else if prefix_lowercase.starts_with("lno") {
229+
let offer = prefix.parse::<Offer>().map_err(|_| Error::InvalidOffer)?;
224230
self.bolt12_offer = Some(offer)
225231
}
226232
}

tests/integration_tests_rust.rs

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -584,45 +584,58 @@ fn unified_qr_send_receive() {
584584
// Sleep one more sec to make sure the node announcement propagates.
585585
std::thread::sleep(std::time::Duration::from_secs(1));
586586

587-
let expected_lightning_amount_sats = 100_000;
587+
let expected_amount_msats = 100_000_000;
588+
let offer = node_b.bolt12_payment().receive(expected_amount_msats, "hi");
589+
590+
let offer_str = offer.clone().unwrap().to_string();
591+
let bolt12_offer_param = format!("&lightning={}", offer_str);
592+
593+
let expected_amount_sats = 100_000;
588594
let message = "TestMessage".to_string();
589595
let expiry_sec = 4_000;
590596

591-
let uqr_payment = node_b.unified_qr_payment().receive(
592-
expected_lightning_amount_sats,
593-
Some(message),
594-
expiry_sec,
595-
);
596-
597-
match uqr_payment {
598-
Ok(ref uri) => {
599-
assert!(uri.contains("BITCOIN:"));
600-
assert!(uri.contains("lightning"));
601-
println!("\nGenerated URI: {}", uri);
602-
},
603-
Err(e) => panic!("Failed to generate URI: {:?}", e),
604-
}
597+
let uqr_payment =
598+
node_b.unified_qr_payment().receive(expected_amount_sats, Some(message), expiry_sec);
605599

606600
let uri_str = uqr_payment.clone().unwrap();
601+
let uri_with_offer = format!("{}{}", uri_str, bolt12_offer_param);
607602

608-
match node_a.unified_qr_payment().send(&uri_str) {
603+
let offer_payment_id: PaymentId = match node_a.unified_qr_payment().send(&uri_with_offer) {
609604
Ok(PaymentResult::Bolt12 { payment_id }) => {
610-
println!("Bolt12 Payment sent successfully with PaymentID: {:?}", payment_id);
605+
println!("\nBolt12 payment sent successfully with PaymentID: {:?}", payment_id);
606+
payment_id
611607
},
612-
Ok(PaymentResult::Bolt11 { payment_id }) => {
613-
println!("Bolt11 payment sent successfully with PaymentID: {:?}", payment_id);
608+
Ok(PaymentResult::Bolt11 { payment_id: _ }) => {
609+
panic!("Expected Bolt12 payment but got Bolt11");
614610
},
615-
Ok(PaymentResult::Onchain { txid }) => {
616-
println!("Onchain payment sent successfully with Txid: {:?}", txid);
617-
wait_for_tx(&electrsd.client, txid);
611+
Ok(PaymentResult::Onchain { txid: _ }) => {
612+
panic!("Expected Bolt12 payment but get On-chain transaction");
618613
},
619614
Err(e) => {
620-
println!("Failed to send payment using the URI: {:?}", e);
615+
panic!("Expected Bolt12 payment but got error: {:?}", e);
621616
},
622-
}
617+
};
623618

624-
let payment_id = expect_payment_received_event!(node_b, expected_lightning_amount_sats * 1_000);
625-
expect_payment_successful_event!(node_a, payment_id, None);
619+
expect_payment_successful_event!(node_a, Some(offer_payment_id), None);
620+
621+
let uri_with_invalid_offer = format!("{}{}", uri_str, "&lightning=some_invalid_offer");
622+
let invoice_payment_id: PaymentId =
623+
match node_a.unified_qr_payment().send(&uri_with_invalid_offer) {
624+
Ok(PaymentResult::Bolt12 { payment_id: _ }) => {
625+
panic!("Expected Bolt11 payment but got Bolt12");
626+
},
627+
Ok(PaymentResult::Bolt11 { payment_id }) => {
628+
println!("\nBolt11 payment sent successfully with PaymentID: {:?}", payment_id);
629+
payment_id
630+
},
631+
Ok(PaymentResult::Onchain { txid: _ }) => {
632+
panic!("Expected Bolt11 payment but got on-chain transaction");
633+
},
634+
Err(e) => {
635+
panic!("Expected Bolt11 payment but got error: {:?}", e);
636+
},
637+
};
638+
expect_payment_successful_event!(node_a, Some(invoice_payment_id), None);
626639

627640
let expect_onchain_amount_sats = 800_000;
628641
let onchain_uqr_payment = node_b
@@ -631,21 +644,18 @@ fn unified_qr_send_receive() {
631644
.unwrap();
632645

633646
let txid = match node_a.unified_qr_payment().send(onchain_uqr_payment.as_str()) {
634-
Ok(PaymentResult::Bolt12 { payment_id }) => {
635-
println!("Bolt12 Payment sent successfully with PaymentID: {:?}", payment_id);
636-
panic!("Expected on-chain payment but got Bolt12 payment")
647+
Ok(PaymentResult::Bolt12 { payment_id: _ }) => {
648+
panic!("Expected on-chain payment but got Bolt12")
637649
},
638-
Ok(PaymentResult::Bolt11 { payment_id }) => {
639-
println!("Bolt11 invoice sent successfully with PaymentId: {}", payment_id);
640-
panic!("Expected on-chain payment but got Bolt11 payment.");
650+
Ok(PaymentResult::Bolt11 { payment_id: _ }) => {
651+
panic!("Expected on-chain payment but got Bolt11");
641652
},
642653
Ok(PaymentResult::Onchain { txid }) => {
643-
println!("on-chain transaction successful with Txid: {}", txid);
654+
println!("\nOn-chain transaction successful with Txid: {}", txid);
644655
txid
645656
},
646657
Err(e) => {
647-
println!("Error to send payment with URI: {:?}", e);
648-
panic!("Expected on-chain payment but got error");
658+
panic!("Expected on-chain payment but got error: {:?}", e);
649659
},
650660
};
651661

@@ -656,6 +666,5 @@ fn unified_qr_send_receive() {
656666
node_b.sync_wallets().unwrap();
657667

658668
assert_eq!(node_b.list_balances().total_onchain_balance_sats, 800_000);
659-
println!("Node B on-chain balance: {:?}", node_b.list_balances().total_onchain_balance_sats);
660-
println!("Node B lighting balance: {:?}", node_b.list_balances().total_lightning_balance_sats)
669+
assert_eq!(node_b.list_balances().total_lightning_balance_sats, 200_000);
661670
}

0 commit comments

Comments
 (0)