Skip to content

Commit b91207c

Browse files
committed
Add ascii accessor methods
Add an `ascii` accessor method to the `UncheckedHrpstring` and `CheckedHrpstring` types. Include examples in the rustdoc both as documentation and a simple unit test.
1 parent 045b50a commit b91207c

File tree

1 file changed

+59
-4
lines changed

1 file changed

+59
-4
lines changed

src/primitives/decode.rs

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,25 @@ impl<'s> UncheckedHrpstring<'s> {
145145
#[inline]
146146
pub fn hrp(&self) -> Hrp { self.hrp }
147147

148+
/// Returns the data part as ASCII bytes i.e., everything after the separator '1'.
149+
///
150+
/// The byte values are guaranteed to be valid bech32 characters. Includes the checksum
151+
/// if one was present in the parsed string.
152+
///
153+
/// # Examples
154+
///
155+
/// ```
156+
/// use bech32::primitives::decode::UncheckedHrpstring;
157+
///
158+
/// let addr = "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq";
159+
/// let ascii = "qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq";
160+
///
161+
/// let unchecked = UncheckedHrpstring::new(&addr).unwrap();
162+
/// assert!(unchecked.data_part_ascii().iter().eq(ascii.as_bytes().iter()))
163+
/// ```
164+
#[inline]
165+
pub fn data_part_ascii(&self) -> &[u8] { self.data_part_ascii }
166+
148167
/// Validates that data has a valid checksum for the `Ck` algorithm and returns a [`CheckedHrpstring`].
149168
#[inline]
150169
pub fn validate_and_remove_checksum<Ck: Checksum>(
@@ -275,6 +294,25 @@ impl<'s> CheckedHrpstring<'s> {
275294
#[inline]
276295
pub fn hrp(&self) -> Hrp { self.hrp }
277296

297+
/// Returns a partial slice of the data part, as ASCII bytes, everything after the separator '1'
298+
/// before the checksum.
299+
///
300+
/// The byte values are guaranteed to be valid bech32 characters.
301+
///
302+
/// # Examples
303+
///
304+
/// ```
305+
/// use bech32::{Bech32, primitives::decode::CheckedHrpstring};
306+
///
307+
/// let addr = "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq";
308+
/// let ascii = "qar0srrr7xfkvy5l643lydnw9re59gtzz";
309+
///
310+
/// let checked = CheckedHrpstring::new::<Bech32>(&addr).unwrap();
311+
/// assert!(checked.data_part_ascii_no_checksum().iter().eq(ascii.as_bytes().iter()))
312+
/// ```
313+
#[inline]
314+
pub fn data_part_ascii_no_checksum(&self) -> &[u8] { self.ascii }
315+
278316
/// Returns an iterator that yields the data part of the parsed bech32 encoded string.
279317
///
280318
/// Converts the ASCII bytes representing field elements to the respective field elements, then
@@ -398,8 +436,7 @@ impl<'s> SegwitHrpstring<'s> {
398436

399437
let unchecked = UncheckedHrpstring::new(s)?;
400438

401-
// TODO: Use accessor function.
402-
let data_part = unchecked.data_part_ascii;
439+
let data_part = unchecked.data_part_ascii();
403440

404441
if data_part.is_empty() {
405442
return Err(SegwitHrpstringError::NoData);
@@ -434,8 +471,7 @@ impl<'s> SegwitHrpstring<'s> {
434471
#[inline]
435472
pub fn new_bech32(s: &'s str) -> Result<Self, SegwitHrpstringError> {
436473
let unchecked = UncheckedHrpstring::new(s)?;
437-
// TODO: Use accessor function.
438-
let data_part = unchecked.data_part_ascii;
474+
let data_part = unchecked.data_part_ascii();
439475

440476
// Unwrap ok since check_characters (in `Self::new`) checked the bech32-ness of this char.
441477
let witness_version = Fe32::from_char(data_part[0].into()).unwrap();
@@ -463,6 +499,25 @@ impl<'s> SegwitHrpstring<'s> {
463499
#[inline]
464500
pub fn witness_version(&self) -> Fe32 { self.witness_version }
465501

502+
/// Returns a partial slice of the data part, as ASCII bytes, everything after the witness
503+
/// version and before the checksum.
504+
///
505+
/// The byte values are guaranteed to be valid bech32 characters.
506+
///
507+
/// # Examples
508+
///
509+
/// ```
510+
/// use bech32::{Bech32, primitives::decode::SegwitHrpstring};
511+
///
512+
/// let addr = "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzwf5mdq";
513+
/// let ascii = "ar0srrr7xfkvy5l643lydnw9re59gtzz";
514+
///
515+
/// let segwit = SegwitHrpstring::new(&addr).unwrap();
516+
/// assert!(segwit.data_part_ascii_no_witver_no_checksum().iter().eq(ascii.as_bytes().iter()))
517+
/// ```
518+
#[inline]
519+
pub fn data_part_ascii_no_witver_no_checksum(&self) -> &[u8] { self.ascii }
520+
466521
/// Returns an iterator that yields the data part, excluding the witness version, of the parsed
467522
/// bech32 encoded string.
468523
///

0 commit comments

Comments
 (0)