Skip to content

Commit

Permalink
Merge pull request #6 from MutinyWallet/fix-uppercase-prefix
Browse files Browse the repository at this point in the history
Fix uppercase prefixes
  • Loading branch information
benthecarman authored Jun 23, 2023
2 parents cfc20f4 + 28d9f63 commit b89c9de
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions waila/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,27 +191,27 @@ impl FromStr for PaymentParams<'_> {
fn from_str(str: &str) -> Result<Self, Self::Err> {
let lower = str.to_lowercase();
if lower.starts_with("lightning:") {
let str = str.strip_prefix("lightning:").unwrap();
let str = lower.strip_prefix("lightning:").unwrap();
return Invoice::from_str(str)
.map(PaymentParams::Bolt11)
.or_else(|_| LnUrl::from_str(str).map(PaymentParams::LnUrl))
.or_else(|_| LightningAddress::from_str(str).map(PaymentParams::LightningAddress))
.or_else(|_| Offer::from_str(str).map(PaymentParams::Bolt12))
.map_err(|_| ());
} else if lower.starts_with("lnurl:") {
let str = str.strip_prefix("lnurl:").unwrap();
let str = lower.strip_prefix("lnurl:").unwrap();
return LnUrl::from_str(str)
.map(PaymentParams::LnUrl)
.or_else(|_| LightningAddress::from_str(str).map(PaymentParams::LightningAddress))
.map_err(|_| ());
} else if lower.starts_with("lnurlp:") {
let str = str.strip_prefix("lnurlp:").unwrap();
let str = lower.strip_prefix("lnurlp:").unwrap();
return LnUrl::from_str(str)
.map(PaymentParams::LnUrl)
.or_else(|_| LightningAddress::from_str(str).map(PaymentParams::LightningAddress))
.map_err(|_| ());
} else if lower.starts_with("nostr:") {
let str = str.strip_prefix("nostr:").unwrap();
let str = lower.strip_prefix("nostr:").unwrap();
return XOnlyPublicKey::from_str(str)
.map(PaymentParams::Nostr)
.or_else(|_| XOnlyPublicKey::from_bech32(str).map(PaymentParams::Nostr))
Expand Down Expand Up @@ -308,6 +308,25 @@ mod tests {
assert_eq!(parsed.lnurl(), None);
}

#[test]
fn parse_invoice_with_prefix_capital() {
let parsed =
PaymentParams::from_str(&format!("LIGHTNING:{}", SAMPLE_INVOICE.to_uppercase()))
.unwrap();
let expected_pubkey = PublicKey::from_str(SAMPLE_PUBKEY).unwrap();

assert_eq!(parsed.amount(), Some(Amount::from_sat(2_000_000)));
assert_eq!(parsed.amount_msats(), Some(2_000_000_000));
assert_eq!(parsed.node_pubkey(), Some(expected_pubkey));
assert_eq!(parsed.network(), Some(Network::Bitcoin));
assert_eq!(
parsed.address(),
Some(Address::from_str("1RustyRX2oai4EYYDpQGWvEL62BBGqN9T").unwrap())
);
assert_eq!(parsed.memo(), None);
assert_eq!(parsed.lnurl(), None);
}

#[test]
fn parse_bip_21() {
let parsed = PaymentParams::from_str(SAMPLE_BIP21).unwrap();
Expand Down

0 comments on commit b89c9de

Please sign in to comment.