From 64d3a0842e641282cd017e1773987e0f55c218ed Mon Sep 17 00:00:00 2001 From: kernelkind Date: Sun, 25 May 2025 18:26:44 -0400 Subject: [PATCH] add `NoteId::from_bech` method Signed-off-by: kernelkind --- crates/enostr/src/note.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/crates/enostr/src/note.rs b/crates/enostr/src/note.rs index cfd75509..ed178488 100644 --- a/crates/enostr/src/note.rs +++ b/crates/enostr/src/note.rs @@ -36,6 +36,16 @@ impl NoteId { pub fn to_bech(&self) -> Option { bech32::encode::(HRP_NOTE, &self.0).ok() } + + pub fn from_bech(bech: &str) -> Option { + let (hrp, data) = bech32::decode(bech).ok()?; + + if hrp != HRP_NOTE { + return None; + } + + Some(NoteId::new(data.try_into().ok()?)) + } } /// Event is the struct used to represent a Nostr event