Skip to content

Commit 472fff7

Browse files
committed
Fail Unified QR URI parsing if any known param fails
Previously, we decided to continue parsing any fields if we failed to parse a known (i.e., `lightning` or `lno`) parameter failed to parse. This however just hides the error and is a bit anti-idiomatic even though allowing to use *some* URI fields even in the face of incompatible formats for others. Here we therefore opt to fail parsing the URI if any field fails.
1 parent 0554c6f commit 472fff7

File tree

2 files changed

+15
-21
lines changed

2 files changed

+15
-21
lines changed

src/payment/unified_qr.rs

+9-12
Original file line numberDiff line numberDiff line change
@@ -256,22 +256,19 @@ impl<'a> bip21::de::DeserializationState<'a> for DeserializationState {
256256
"lightning" => {
257257
let bolt11_value =
258258
String::try_from(value).map_err(|_| Error::UriParameterParsingFailed)?;
259-
if let Ok(invoice) = bolt11_value.parse::<Bolt11Invoice>() {
260-
self.bolt11_invoice = Some(invoice);
261-
Ok(bip21::de::ParamKind::Known)
262-
} else {
263-
Ok(bip21::de::ParamKind::Unknown)
264-
}
259+
let invoice = bolt11_value
260+
.parse::<Bolt11Invoice>()
261+
.map_err(|_| Error::UriParameterParsingFailed)?;
262+
self.bolt11_invoice = Some(invoice);
263+
Ok(bip21::de::ParamKind::Known)
265264
},
266265
"lno" => {
267266
let bolt12_value =
268267
String::try_from(value).map_err(|_| Error::UriParameterParsingFailed)?;
269-
if let Ok(offer) = bolt12_value.parse::<Offer>() {
270-
self.bolt12_offer = Some(offer);
271-
Ok(bip21::de::ParamKind::Known)
272-
} else {
273-
Ok(bip21::de::ParamKind::Unknown)
274-
}
268+
let offer =
269+
bolt12_value.parse::<Offer>().map_err(|_| Error::UriParameterParsingFailed)?;
270+
self.bolt12_offer = Some(offer);
271+
Ok(bip21::de::ParamKind::Known)
275272
},
276273
_ => Ok(bip21::de::ParamKind::Unknown),
277274
}

tests/integration_tests_rust.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -868,11 +868,10 @@ fn unified_qr_send_receive() {
868868

869869
expect_payment_successful_event!(node_a, Some(offer_payment_id), None);
870870

871-
// Removed one character from the offer to fall back on to invoice.
872-
// Still needs work
873-
let uri_str_with_invalid_offer = &uri_str[..uri_str.len() - 1];
871+
// Cut off the BOLT12 part to fallback to BOLT11.
872+
let uri_str_without_offer = uri_str.split("&lno=").next().unwrap();
874873
let invoice_payment_id: PaymentId =
875-
match node_a.unified_qr_payment().send(uri_str_with_invalid_offer) {
874+
match node_a.unified_qr_payment().send(uri_str_without_offer) {
876875
Ok(QrPaymentResult::Bolt12 { payment_id: _ }) => {
877876
panic!("Expected Bolt11 payment but got Bolt12");
878877
},
@@ -893,11 +892,9 @@ fn unified_qr_send_receive() {
893892
let onchain_uqr_payment =
894893
node_b.unified_qr_payment().receive(expect_onchain_amount_sats, "asdf", 4_000).unwrap();
895894

896-
// Removed a character from the offer, so it would move on to the other parameters.
897-
let txid = match node_a
898-
.unified_qr_payment()
899-
.send(&onchain_uqr_payment.as_str()[..onchain_uqr_payment.len() - 1])
900-
{
895+
// Cut off any lightning part to fallback to on-chain only.
896+
let uri_str_without_lightning = onchain_uqr_payment.split("&lightning=").next().unwrap();
897+
let txid = match node_a.unified_qr_payment().send(&uri_str_without_lightning) {
901898
Ok(QrPaymentResult::Bolt12 { payment_id: _ }) => {
902899
panic!("Expected on-chain payment but got Bolt12")
903900
},

0 commit comments

Comments
 (0)