use alloc::string::String;
use core::fmt;
use core::str::FromStr;
use crate::nips::nip19;
#[non_exhaustive]
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("missing or malformed nostr: scheme")]
MissingScheme,
#[error("nostr: URIs must not contain an nsec entity")]
SecretKey,
#[error(transparent)]
Nip19(#[from] nip19::Error),
}
const SCHEME: &str = "nostr:";
const NSEC: &str = "nsec1";
const fn is_scheme_char(b: u8) -> bool {
b.is_ascii_alphanumeric()
}
const fn is_data_char(b: u8) -> bool {
matches!(
b.to_ascii_lowercase(),
b'0' | b'2'..=b'9' | b'a' | b'c'..=b'h' | b'j'..=b'n' | b'p'..=b'z'
)
}
fn is_nsec(entity: &str) -> bool {
entity.len() >= NSEC.len() && entity[..NSEC.len()].eq_ignore_ascii_case(NSEC)
}
fn uri_entity(uri: &str) -> Option<&str> {
if uri.len() <= SCHEME.len() || !uri.get(..SCHEME.len())?.eq_ignore_ascii_case(SCHEME) {
return None;
}
let entity = &uri[SCHEME.len()..];
let sep = entity.rfind('1')?;
let hrp = entity.get(..sep)?;
let data = entity.get(sep + 1..)?;
if hrp.is_empty() || !hrp.bytes().all(is_scheme_char) {
return None;
}
if data.is_empty() || !data.bytes().all(is_data_char) {
return None;
}
Some(entity)
}
#[derive(Clone, Debug)]
pub struct NostrUri {
value: String,
entity: nip19::Entity,
}
impl NostrUri {
pub fn parse(uri: &str) -> Result<Self, Error> {
let Some(entity) = uri_entity(uri) else {
return Err(Error::MissingScheme);
};
if is_nsec(entity) {
return Err(Error::SecretKey);
}
let decoded = nip19::Entity::from_bech32(entity)?;
Ok(Self {
value: entity.into(),
entity: decoded,
})
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.value
}
#[must_use]
pub const fn entity(&self) -> &nip19::Entity {
&self.entity
}
#[must_use]
pub fn into_entity(self) -> nip19::Entity {
self.entity
}
}
impl fmt::Display for NostrUri {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{SCHEME}{}", self.value)
}
}
impl FromStr for NostrUri {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Error> {
Self::parse(s)
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::panic, reason = "tests fail by panicking")]
use alloc::format;
use alloc::string::ToString;
use core::error::Error as _;
use super::*;
const NPUB: &str = "npub180cvv07tjdrrgpa0j7j7tmnyl2yr6yr7l8j4s3evf6u64th6gkwsyjh6w6";
const NSEC: &str = "nsec1vl029mgpspedva04g90vltkh6fvh240zqtv9k0t9af8935ke9laqsnlfe5";
#[test]
fn parse_round_trip() {
let uri = NostrUri::parse(&format!("nostr:{NPUB}")).unwrap();
assert_eq!(uri.as_str(), NPUB);
assert_eq!(uri.to_string(), format!("nostr:{NPUB}"));
assert!(matches!(uri.entity(), nip19::Entity::Public(_)));
assert!(matches!(uri.into_entity(), nip19::Entity::Public(_)));
assert!(format!("nostr:{NPUB}").parse::<NostrUri>().is_ok());
}
#[test]
fn accepts_case_insensitive_scheme_and_upper_entity() {
assert!(NostrUri::parse(&format!("NOSTR:{NPUB}")).is_ok());
assert!(NostrUri::parse(&format!("nostr:{}", NPUB.to_uppercase())).is_ok());
}
#[test]
fn rejects_nsec() {
let err = NostrUri::parse(&format!("nostr:{NSEC}")).unwrap_err();
assert!(matches!(err, Error::SecretKey));
assert!(matches!(
NostrUri::parse(&format!("nostr:{}", NSEC.to_uppercase())),
Err(Error::SecretKey)
));
}
#[test]
fn rejects_malformed_and_wrong_scheme() {
for input in [
"",
"npub180cvv07",
"http://x",
"nostr:",
"nostr:abc",
"nostr://abc",
"nostr:npub1bad!chars",
] {
assert!(
matches!(NostrUri::parse(input), Err(Error::MissingScheme)),
"{input}"
);
}
}
#[test]
fn rejects_undecodable_entity_with_nip19_source() {
let mut code = String::from(NPUB);
let last = code.pop().unwrap();
code.push(if last == 'q' { 'p' } else { 'q' });
let err = NostrUri::parse(&format!("nostr:{code}")).unwrap_err();
assert!(matches!(err, Error::Nip19(_)));
assert!(err.source().is_some());
}
}