Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- `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=<BOLT11>` (served over NFC by Numo point-of-sale terminals, and planned by Square, which serves `lightning:<BOLT11>` 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.
Expand Down
25 changes: 16 additions & 9 deletions src/modules/scanner/implementation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
Expand Down Expand Up @@ -63,9 +64,12 @@ impl Scanner {

pub async fn decode(invoice_str: String) -> Result<Self, DecodingError> {
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://") {
Expand Down Expand Up @@ -103,18 +107,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(),
})
Expand Down
125 changes: 125 additions & 0 deletions src/modules/scanner/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,131 @@ 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_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)
Expand Down
Loading