From e5057f5b7680a0184f429e03ba7f0300ebb125ce Mon Sep 17 00:00:00 2001 From: Alex Tsokurov Date: Tue, 10 Dec 2024 20:54:38 +0100 Subject: [PATCH 1/2] slabel methods --- protocol/src/slabel.rs | 43 +++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/protocol/src/slabel.rs b/protocol/src/slabel.rs index 73b92be..dc3ae8d 100644 --- a/protocol/src/slabel.rs +++ b/protocol/src/slabel.rs @@ -188,6 +188,32 @@ impl<'a> TryFrom<&'a [u8]> for SLabelRef<'a> { } } +impl SLabel { + pub fn as_str_unprefixed(&self) -> Result<&str, core::str::Utf8Error> { + let label_len = self.0[0] as usize; + let label = &self.0[1..=label_len]; + core::str::from_utf8(label) + } + + pub fn to_string_unprefixed(&self) -> Result { + self.as_str_unprefixed().map(|s| s.to_string()) + } + + pub fn from_str_unprefixed(label: &str) -> Result { + if label.is_empty() { + return Err(Error::Name(NameErrorKind::ZeroLength)); + } + if label.len() > MAX_LABEL_LEN { + return Err(Error::Name(NameErrorKind::TooLong)); + } + let mut label_bytes = [0; MAX_LABEL_LEN + 1]; + label_bytes[0] = label.len() as u8; + label_bytes[1..=label.len()].copy_from_slice(label.as_bytes()); + + SLabel::try_from(label_bytes.as_slice()) + } +} + impl TryFrom for SLabel { type Error = Error; @@ -204,26 +230,13 @@ impl TryFrom<&str> for SLabel { return Err(Error::Name(NameErrorKind::NotCanonical)); } let label = &value[1..]; - if label.is_empty() { - return Err(Error::Name(NameErrorKind::ZeroLength)); - } - if label.len() > MAX_LABEL_LEN { - return Err(Error::Name(NameErrorKind::TooLong)); - } - let mut label_bytes = [0; MAX_LABEL_LEN + 1]; - label_bytes[0] = label.len() as u8; - label_bytes[1..=label.len()].copy_from_slice(label.as_bytes()); - - SLabel::try_from(label_bytes.as_slice()) + Self::from_str_unprefixed(label) } } impl Display for SLabel { fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { - let label_len = self.0[0] as usize; - let label = &self.0[1..=label_len]; - - let label_str = core::str::from_utf8(label).map_err(|_| core::fmt::Error)?; + let label_str = self.as_str_unprefixed().map_err(|_| core::fmt::Error)?; write!(f, "@{}", label_str) } } From 7ac8443bd0d7c90aac7d353ea5b3b557114a26d8 Mon Sep 17 00:00:00 2001 From: Alex Tsokurov Date: Fri, 24 Jan 2025 14:40:56 +0100 Subject: [PATCH 2/2] slabel is_reserved --- protocol/src/script.rs | 6 +----- protocol/src/slabel.rs | 12 +++++++++++- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/protocol/src/script.rs b/protocol/src/script.rs index 5dbb1b3..b2f77b8 100644 --- a/protocol/src/script.rs +++ b/protocol/src/script.rs @@ -12,7 +12,6 @@ use bitcoin::{ use serde::{Deserialize, Serialize}; use crate::{ - constants::RESERVED_SPACES, hasher::{KeyHasher, SpaceKey}, prepare::DataSource, slabel::{SLabel, SLabelRef}, @@ -140,10 +139,7 @@ impl SpaceScript { } let name = name.unwrap(); - if RESERVED_SPACES - .iter() - .any(|reserved| *reserved == name.as_ref()) - { + if name.is_reserved() { return Ok(Err(ScriptError::ReservedName)); } diff --git a/protocol/src/slabel.rs b/protocol/src/slabel.rs index dc3ae8d..8f7c0bf 100644 --- a/protocol/src/slabel.rs +++ b/protocol/src/slabel.rs @@ -9,7 +9,7 @@ use core::{ #[cfg(feature = "serde")] use serde::{de::Error as ErrorUtil, Deserialize, Deserializer, Serialize, Serializer}; -use crate::errors::Error; +use crate::{constants::RESERVED_SPACES, errors::Error}; pub const MAX_LABEL_LEN: usize = 62; pub const PUNYCODE_PREFIX: &[u8] = b"xn--"; @@ -251,6 +251,10 @@ impl SLabel { pub fn as_name_ref(&self) -> SLabelRef { SLabelRef(&self.0) } + + pub fn is_reserved(&self) -> bool { + self.as_name_ref().is_reserved() + } } impl SLabelRef<'_> { @@ -259,6 +263,12 @@ impl SLabelRef<'_> { owned.0[..self.0.len()].copy_from_slice(self.0); owned } + + pub fn is_reserved(&self) -> bool { + RESERVED_SPACES + .iter() + .any(|reserved| *reserved == self.as_ref()) + } } #[cfg(test)]