Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow uppercase bech32 HRP #3505

Merged
merged 2 commits into from
Jan 8, 2025
Merged
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
17 changes: 16 additions & 1 deletion lightning/src/offers/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ mod sealed {

let parsed = CheckedHrpstring::new::<NoChecksum>(encoded.as_ref())?;
let hrp = parsed.hrp();
if hrp.as_str() != Self::BECH32_HRP {
// Compare the lowercase'd iter to allow for all-uppercase HRPs
if hrp.lowercase_char_iter().ne(Self::BECH32_HRP.chars()) {
return Err(Bolt12ParseError::InvalidBech32Hrp);
}

Expand Down Expand Up @@ -233,6 +234,7 @@ impl From<secp256k1::Error> for Bolt12ParseError {
mod bolt12_tests {
use super::Bolt12ParseError;
use crate::offers::offer::Offer;
use bech32::primitives::decode::{CheckedHrpstringError, UncheckedHrpstringError, CharError};

#[test]
fn encodes_offer_as_bech32_without_checksum() {
Expand All @@ -249,6 +251,9 @@ mod bolt12_tests {
// A complete string is valid
"lno1pqps7sjqpgtyzm3qv4uxzmtsd3jjqer9wd3hy6tsw35k7msjzfpy7nz5yqcnygrfdej82um5wf5k2uckyypwa3eyt44h6txtxquqh7lz5djge4afgfjn7k4rgrkuag0jsd5xvxg",

// Uppercase is valid
"LNO1PQPS7SJQPGTYZM3QV4UXZMTSD3JJQER9WD3HY6TSW35K7MSJZFPY7NZ5YQCNYGRFDEJ82UM5WF5K2UCKYYPWA3EYT44H6TXTXQUQH7LZ5DJGE4AFGFJN7K4RGRKUAG0JSD5XVXG",

// + can join anywhere
"l+no1pqps7sjqpgtyzm3qv4uxzmtsd3jjqer9wd3hy6tsw35k7msjzfpy7nz5yqcnygrfdej82um5wf5k2uckyypwa3eyt44h6txtxquqh7lz5djge4afgfjn7k4rgrkuag0jsd5xvxg",

Expand Down Expand Up @@ -282,6 +287,16 @@ mod bolt12_tests {
}
}
}

#[test]
fn fails_parsing_bech32_encoded_offers_with_mixed_casing() {
// We assert that mixed-case encoding fails to parse.
let mixed_case_offer = "LnO1PqPs7sJqPgTyZm3qV4UxZmTsD3JjQeR9Wd3hY6TsW35k7mSjZfPy7nZ5YqCnYgRfDeJ82uM5Wf5k2uCkYyPwA3EyT44h6tXtXqUqH7Lz5dJgE4AfGfJn7k4rGrKuAg0jSd5xVxG";
match mixed_case_offer.parse::<Offer>() {
Ok(_) => panic!("Valid offer: {}", mixed_case_offer),
Err(e) => assert_eq!(e, Bolt12ParseError::Bech32(CheckedHrpstringError::Parse(UncheckedHrpstringError::Char(CharError::MixedCase)))),
}
}
}

#[cfg(test)]
Expand Down
Loading