use nostro2_traits::hex::FromHex as _;
pub trait NostrValidate {
fn is_valid_pubkey(&self) -> bool;
fn is_valid_event_id(&self) -> bool;
fn is_valid_signature(&self) -> bool;
}
impl NostrValidate for str {
fn is_valid_pubkey(&self) -> bool {
self.len() == 64 && self.decode_hex().is_ok()
}
fn is_valid_event_id(&self) -> bool {
self.len() == 64 && self.decode_hex().is_ok()
}
fn is_valid_signature(&self) -> bool {
self.len() == 128 && self.decode_hex().is_ok()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_valid_pubkey() {
assert!(
"4f6ddf3e79731d1b7039e28feb394e41e9117c93e383d31e8b88719095c6b17d".is_valid_pubkey()
);
assert!(!"invalid".is_valid_pubkey());
assert!(!"4f6d".is_valid_pubkey()); }
#[test]
fn test_valid_event_id() {
assert!(
"a123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef".is_valid_event_id()
);
assert!(!"not_hex".is_valid_event_id());
}
#[test]
fn test_valid_signature() {
let valid_sig = "a".repeat(128);
assert!(valid_sig.is_valid_signature());
assert!(!"short".is_valid_signature());
}
}