Skip to content

Commit 2fae3aa

Browse files
committed
Add UncheckedHrpstring::remove_witness_version function
Add a function to the `UncheckedHrpstring` that attempts to treat the first byte of the data part as a witness version and remove it. Note, later calls to `data_part_ascii` will not include it.
1 parent 0ee999d commit 2fae3aa

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/primitives/decode.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,33 @@ impl<'s> UncheckedHrpstring<'s> {
164164
#[inline]
165165
pub fn data_part_ascii(&self) -> &[u8] { self.data_part_ascii }
166166

167+
/// Attempts to remove the first byte of the data part, treating it as a witness version.
168+
///
169+
/// If [`Self::witness_version`] succeeds this function removes the first character (witness
170+
/// version byte) from the internal ASCII data part buffer. Future calls to
171+
/// [`Self::data_part_ascii`] will no longer include it.
172+
///
173+
/// # Examples
174+
///
175+
/// ```
176+
/// use bech32::{primitives::decode::UncheckedHrpstring, Fe32};
177+
///
178+
/// let addr = "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq";
179+
/// let ascii = "ar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq";
180+
///
181+
/// let mut unchecked = UncheckedHrpstring::new(&addr).unwrap();
182+
/// let witness_version = unchecked.remove_witness_version().unwrap();
183+
/// assert_eq!(witness_version, Fe32::Q);
184+
/// assert!(unchecked.data_part_ascii().iter().eq(ascii.as_bytes().iter()))
185+
/// ```
186+
#[inline]
187+
pub fn remove_witness_version(&mut self) -> Option<Fe32> {
188+
self.witness_version().map(|witver| {
189+
self.data_part_ascii = &self.data_part_ascii[1..]; // Remove the witness version byte.
190+
witver
191+
})
192+
}
193+
167194
/// Returns the segwit witness version if there is one.
168195
///
169196
/// Attempts to convert the first character of the data part to a witness version. If this

0 commit comments

Comments
 (0)