Skip to content

Commit

Permalink
Merge pull request #22 from MutinyWallet/nprofile
Browse files Browse the repository at this point in the history
Parse nprofile
  • Loading branch information
benthecarman authored Feb 20, 2024
2 parents b1c8165 + 6044616 commit 8c7e325
Showing 1 changed file with 59 additions and 2 deletions.
61 changes: 59 additions & 2 deletions waila/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use lightning_invoice::{Bolt11Invoice, Bolt11InvoiceDescription};
use lnurl::lightning_address::LightningAddress;
use lnurl::lnurl::LnUrl;
use moksha_core::token::TokenV3;
use nostr::FromBech32;

#[cfg(feature = "rgb")]
use rgbstd::Chain;
Expand Down Expand Up @@ -469,6 +470,10 @@ impl FromStr for PaymentParams<'_> {
let str = lower.strip_prefix("nostr:").unwrap();
return nostr::PublicKey::from_str(str)
.map(PaymentParams::Nostr)
.or_else(|_| {
nostr::nips::nip19::Nip19Profile::from_bech32(str)
.map(|p| PaymentParams::Nostr(p.public_key))
})
.map_err(|_| ());
} else if lower.starts_with("fedimint:") {
let str = lower.strip_prefix("fedimint:").unwrap();
Expand All @@ -488,11 +493,15 @@ impl FromStr for PaymentParams<'_> {
.or_else(|_| UnifiedUri::from_str(str).map(|u| PaymentParams::Bip21(Box::new(u))))
.or_else(|_| LightningAddress::from_str(str).map(PaymentParams::LightningAddress))
.or_else(|_| LnUrl::from_str(str).map(PaymentParams::LnUrl))
.or_else(|_| PublicKey::from_str(str).map(PaymentParams::NodePubkey))
.or_else(|_| nostr::PublicKey::from_str(str).map(PaymentParams::Nostr))
.or_else(|_| {
nostr::nips::nip19::Nip19Profile::from_bech32(str)
.map(|p| PaymentParams::Nostr(p.public_key))
})
.or_else(|_| Offer::from_str(str).map(PaymentParams::Bolt12))
.or_else(|_| Refund::from_str(str).map(PaymentParams::Bolt12Refund))
.or_else(|_| nostr::PublicKey::from_str(str).map(PaymentParams::Nostr))
.or_else(|_| NIP49URI::from_str(str).map(PaymentParams::NostrWalletAuth))
.or_else(|_| PublicKey::from_str(str).map(PaymentParams::NodePubkey))
.or_else(|_| parse_fedi_invite_code(str).map(PaymentParams::FedimintInvite))
.or_else(|_| TokenV3::try_from(str.to_string()).map(PaymentParams::CashuToken))
.or_else(|_| OOBNotes::from_str(str).map(PaymentParams::FedimintOOBNotes))
Expand Down Expand Up @@ -833,6 +842,54 @@ mod tests {
);
}

#[test]
fn parse_nprofile_key() {
let parsed = PaymentParams::from_str(
"nprofile1qqsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8gpp4mhxue69uhhytnc9e3k7mgpz4mhxue69uhkg6nzv9ejuumpv34kytnrdaksjlyr9p",
)
.unwrap();

assert_eq!(parsed.amount(), None);
assert_eq!(parsed.address(), None);
assert_eq!(parsed.memo(), None);
assert_eq!(parsed.network(), None);
assert_eq!(parsed.invoice(), None);
assert_eq!(parsed.node_pubkey(), None);
assert_eq!(
parsed.nostr_pubkey(),
Some(
nostr::PublicKey::from_str(
"3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d"
)
.unwrap()
)
);
}

#[test]
fn parse_nprofile_with_prefix() {
let parsed = PaymentParams::from_str(
"nostr:nprofile1qqsrhuxx8l9ex335q7he0f09aej04zpazpl0ne2cgukyawd24mayt8gpp4mhxue69uhhytnc9e3k7mgpz4mhxue69uhkg6nzv9ejuumpv34kytnrdaksjlyr9p",
)
.unwrap();

assert_eq!(parsed.amount(), None);
assert_eq!(parsed.address(), None);
assert_eq!(parsed.memo(), None);
assert_eq!(parsed.network(), None);
assert_eq!(parsed.invoice(), None);
assert_eq!(parsed.node_pubkey(), None);
assert_eq!(
parsed.nostr_pubkey(),
Some(
nostr::PublicKey::from_str(
"3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d"
)
.unwrap()
)
);
}

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

0 comments on commit 8c7e325

Please sign in to comment.