From 53e5b18b30ede4d4b02ed7acfb4e9873a97fa185 Mon Sep 17 00:00:00 2001 From: jvsena42 Date: Wed, 25 Nov 2026 10:54:25 -0300 Subject: [PATCH 1/2] fix: route to lightning only on a leading lightning: scheme Scanner::decode sent any input containing `lightning:` to the Lightning decoder, so a bitcoin: URI with `lightning:` in a label or message failed to decode. Match the scheme at the start only. Fixes #227 Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 1 + src/modules/scanner/implementation.rs | 15 +++-- src/modules/scanner/tests.rs | 82 +++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 608c2172..4965ae5c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +- `decode` sends input to Lightning only when it starts with `lightning:` (any case) or is a bare `lnbc`/`lntb` invoice. A `bitcoin:` URI with `lightning:` inside a parameter value such as `label` or `message` now decodes as that URI instead of failing. - Decode BIP 321 `bitcoin:` URIs without an address. `bitcoin:?lightning=` (served over NFC by Numo point-of-sale terminals, and planned by Square, which serves `lightning:` today) returns `Scanner::Lightning`, and a segwit address in the `bc`, `tb` or `bcrt` parameter returns `Scanner::OnChain` with that address, keeping other instructions such as `lightning` in `params`. An address in the wrong parameter (`bc=tb1…`) returns `InvalidAddress`. `lno`, `sp` or `pay` alone returns `UnsupportedType`, and no payment instruction returns `InvalidAddress` as before. Any `req-` parameter, `req-pop` included, now returns `InvalidFormat`, because Bitkit handles none of them and BIP 321 forbids paying such a URI (breaking for callers that paid `req-pop` URIs). - Update `lightning` to 0.2.8. - `regtest_mine`, `regtest_deposit`, `regtest_pay`, `regtest_get_payment` and `regtest_close_channel` take an optional `auth` (`user:password`), sent as basic auth to Blocktank servers that protect the regtest routes; a 401 returns a clear error. diff --git a/src/modules/scanner/implementation.rs b/src/modules/scanner/implementation.rs index 2272dca7..17ae0e66 100644 --- a/src/modules/scanner/implementation.rs +++ b/src/modules/scanner/implementation.rs @@ -103,18 +103,21 @@ impl Scanner { } } - if invoice_str.to_lowercase().contains("lightning:") - || invoice_str.to_lowercase().starts_with("lntb") - || invoice_str.to_lowercase().starts_with("lnbc") + let invoice_lower = invoice_str.to_lowercase(); + // Match the scheme only at the start: a `bitcoin:` URI can carry `lightning:` inside a + // parameter value such as `label` or `message`, and its `lightning` parameter is read by + // `decode_bitcoin_uri`. + if invoice_lower.starts_with("lightning:") + || invoice_lower.starts_with("lntb") + || invoice_lower.starts_with("lnbc") { - let invoice_lower = invoice_str.to_lowercase(); let invoice = invoice_lower .strip_prefix("lightning:") .unwrap_or(&invoice_lower); Self::decode_lightning(invoice) - } else if invoice_str.to_lowercase().starts_with("bitcoin:") { + } else if invoice_lower.starts_with("bitcoin:") { Self::decode_bitcoin_uri(invoice_str).await - } else if invoice_str.to_lowercase().starts_with("pubkyauth:") { + } else if invoice_lower.starts_with("pubkyauth:") { Ok(Scanner::PubkyAuth { data: invoice_str.to_string(), }) diff --git a/src/modules/scanner/tests.rs b/src/modules/scanner/tests.rs index c0a24bb3..fc0bd320 100644 --- a/src/modules/scanner/tests.rs +++ b/src/modules/scanner/tests.rs @@ -718,6 +718,88 @@ mod tests { } } + const SCHEME_TEST_ADDRESS: &str = "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq"; + + async fn decode_onchain_uri(uri: String) -> crate::modules::scanner::OnChainInvoice { + match Scanner::decode(uri).await.unwrap() { + Scanner::OnChain { invoice } => invoice, + other => panic!("Should be an OnChain invoice, got {other:?}"), + } + } + + #[tokio::test] + async fn test_bitcoin_uri_label_containing_lightning_scheme() { + for label in ["pay%20lightning:x", "pay lightning:x", "LIGHTNING:x"] { + let invoice = + decode_onchain_uri(format!("bitcoin:{SCHEME_TEST_ADDRESS}?label={label}")).await; + assert_eq!(invoice.address, SCHEME_TEST_ADDRESS); + assert_eq!(invoice.label.as_deref(), Some(label)); + } + } + + #[tokio::test] + async fn test_bitcoin_uri_message_containing_lightning_scheme() { + for message in ["see%20lightning:lnbc1", "see lightning:lnbc1"] { + let invoice = decode_onchain_uri(format!( + "bitcoin:{SCHEME_TEST_ADDRESS}?amount=0.0001&message={message}" + )) + .await; + assert_eq!(invoice.address, SCHEME_TEST_ADDRESS); + assert_eq!(invoice.amount_satoshis, 10_000); + assert_eq!(invoice.message.as_deref(), Some(message)); + } + } + + #[tokio::test] + async fn test_uppercase_lightning_prefix_with_lowercase_invoice() { + let bolt11 = create_test_invoice_with_msat(21_000); + match Scanner::decode(format!("LIGHTNING:{bolt11}")) + .await + .unwrap() + { + Scanner::Lightning { invoice } => { + assert_eq!(invoice.bolt11, bolt11); + assert_eq!(invoice.amount_satoshis, 21); + } + other => panic!("Should be a Lightning invoice, got {other:?}"), + } + } + + #[tokio::test] + async fn test_addressless_bitcoin_uri_with_lightning_param() { + let bolt11 = create_test_invoice_with_msat(42_000); + match Scanner::decode(format!("bitcoin:?lightning={bolt11}")) + .await + .unwrap() + { + Scanner::Lightning { invoice } => { + assert_eq!(invoice.bolt11, bolt11); + assert_eq!(invoice.amount_satoshis, 42); + } + other => panic!("Should be a Lightning invoice, got {other:?}"), + } + } + + #[tokio::test] + async fn test_bitcoin_uri_with_address_and_lightning_param() { + let bolt11 = create_test_invoice_with_msat(42_000); + let invoice = decode_onchain_uri(format!( + "bitcoin:{SCHEME_TEST_ADDRESS}?amount=0.00000042&label=pay%20lightning:x&lightning={bolt11}" + )) + .await; + assert_eq!(invoice.address, SCHEME_TEST_ADDRESS); + assert_eq!(invoice.amount_satoshis, 42); + assert_eq!(invoice.label.as_deref(), Some("pay%20lightning:x")); + assert_eq!( + invoice + .params + .as_ref() + .and_then(|params| params.get("lightning")) + .map(String::as_str), + Some(bolt11.as_str()) + ); + } + #[tokio::test] async fn test_uppercase_bitcoin_uri() { // Test uppercase BITCOIN: prefix with uppercase address (common in QR codes) From 3166f0616170c152da8668650b69aab3f416f651 Mon Sep 17 00:00:00 2001 From: jvsena42 Date: Wed, 25 Nov 2026 11:53:53 -0300 Subject: [PATCH 2/2] fix: strip the lightning: scheme in any case The leading `lightning:` strip was case-sensitive, so `LIGHTNING:` with an LNURL, node id or Lightning address reached the BOLT11 decoder and failed. Also test an uppercase bitcoin: URI with `LIGHTNING:` in its label. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 2 +- src/modules/scanner/implementation.rs | 10 +++++-- src/modules/scanner/tests.rs | 43 +++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4965ae5c..d4431d25 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## Unreleased -- `decode` sends input to Lightning only when it starts with `lightning:` (any case) or is a bare `lnbc`/`lntb` invoice. A `bitcoin:` URI with `lightning:` inside a parameter value such as `label` or `message` now decodes as that URI instead of failing. +- `decode` sends input to Lightning only when it starts with `lightning:` or is a bare `lnbc`/`lntb` invoice. A `bitcoin:` URI with `lightning:` inside a parameter value such as `label` or `message` now decodes as that URI instead of failing. The `lightning:` scheme is matched in any case, so `LIGHTNING:` followed by an LNURL, a node id (`pubkey@host:port`) or a Lightning address now decodes instead of returning `InvalidFormat`. - Decode BIP 321 `bitcoin:` URIs without an address. `bitcoin:?lightning=` (served over NFC by Numo point-of-sale terminals, and planned by Square, which serves `lightning:` today) returns `Scanner::Lightning`, and a segwit address in the `bc`, `tb` or `bcrt` parameter returns `Scanner::OnChain` with that address, keeping other instructions such as `lightning` in `params`. An address in the wrong parameter (`bc=tb1…`) returns `InvalidAddress`. `lno`, `sp` or `pay` alone returns `UnsupportedType`, and no payment instruction returns `InvalidAddress` as before. Any `req-` parameter, `req-pop` included, now returns `InvalidFormat`, because Bitkit handles none of them and BIP 321 forbids paying such a URI (breaking for callers that paid `req-pop` URIs). - Update `lightning` to 0.2.8. - `regtest_mine`, `regtest_deposit`, `regtest_pay`, `regtest_get_payment` and `regtest_close_channel` take an optional `auth` (`user:password`), sent as basic auth to Blocktank servers that protect the regtest routes; a 401 returns a clear error. diff --git a/src/modules/scanner/implementation.rs b/src/modules/scanner/implementation.rs index 17ae0e66..96484179 100644 --- a/src/modules/scanner/implementation.rs +++ b/src/modules/scanner/implementation.rs @@ -18,6 +18,7 @@ use url::Url; use crate::modules::onchain::BitcoinAddressValidator; const BITCOIN_SCHEME: &str = "bitcoin:"; +const LIGHTNING_SCHEME: &str = "lightning:"; const REQUIRED_PARAM_PREFIX: &str = "req-"; /// BIP 321 parameters holding a segwit address, named after the address's human-readable part. const SEGWIT_ADDRESS_KEYS: [&str; 3] = ["bc", "tb", "bcrt"]; @@ -63,9 +64,12 @@ impl Scanner { pub async fn decode(invoice_str: String) -> Result { let invoice_str = invoice_str.trim(); - let invoice_str = invoice_str - .strip_prefix("lightning:") - .unwrap_or(invoice_str); + let invoice_str = match invoice_str.get(..LIGHTNING_SCHEME.len()) { + Some(prefix) if prefix.eq_ignore_ascii_case(LIGHTNING_SCHEME) => { + &invoice_str[LIGHTNING_SCHEME.len()..] + } + _ => invoice_str, + }; // Handle Bitkit deep links if invoice_str.starts_with("bitkit://") { diff --git a/src/modules/scanner/tests.rs b/src/modules/scanner/tests.rs index fc0bd320..f2b0a72d 100644 --- a/src/modules/scanner/tests.rs +++ b/src/modules/scanner/tests.rs @@ -800,6 +800,49 @@ mod tests { ); } + #[tokio::test] + async fn test_uppercase_bitcoin_uri_label_containing_lightning_scheme() { + let address = SCHEME_TEST_ADDRESS.to_uppercase(); + let invoice = decode_onchain_uri(format!("BITCOIN:{address}?LABEL=LIGHTNING:X")).await; + assert_eq!(invoice.address, SCHEME_TEST_ADDRESS); + assert_eq!( + invoice + .params + .as_ref() + .and_then(|params| params.get("label")) + .map(String::as_str), + Some("LIGHTNING:X") + ); + } + + #[tokio::test] + async fn test_uppercase_lightning_prefix_with_lnurl_auth() { + // LNURL-auth for https://example.com/login?tag=login&k1=ab..ab&action=login, decoded without a request. + let lnurl = "lnurl1dp68gurn8ghj7etcv9khqmr99e3k7mf0d3hkw6tw8a6xzeead3hkw6twye4nz0tpvfskyctzv93xzcnpvfskyctzv93xzcnpvfskyctzv93xzcnpvfskyctzv93xzcnpvfskyctzv93xzcnpvfskyctzv93xzcnpvfskyfnpvd6xjmmw84kx7emfdclcrgla"; + for input in [ + format!("LIGHTNING:{}", lnurl.to_uppercase()), + format!("Lightning:{lnurl}"), + ] { + match Scanner::decode(input).await.unwrap() { + Scanner::LnurlAuth { data } => { + assert_eq!(data.domain, "example.com"); + assert_eq!(data.k1, "ab".repeat(32)); + } + other => panic!("Should be LNURL-auth, got {other:?}"), + } + } + } + + #[tokio::test] + async fn test_uppercase_lightning_prefix_with_node_id() { + let node = + "03864ef025fde8fb587d989186ce6a4a186895ee44a926bfc370e2c366597a3f8f@3.33.236.230:9735"; + match Scanner::decode(format!("LIGHTNING:{node}")).await.unwrap() { + Scanner::NodeId { url, .. } => assert_eq!(url, node), + other => panic!("Should be a NodeId, got {other:?}"), + } + } + #[tokio::test] async fn test_uppercase_bitcoin_uri() { // Test uppercase BITCOIN: prefix with uppercase address (common in QR codes)