@@ -145,6 +145,25 @@ impl<'s> UncheckedHrpstring<'s> {
145
145
#[ inline]
146
146
pub fn hrp ( & self ) -> Hrp { self . hrp }
147
147
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
+
148
167
/// Validates that data has a valid checksum for the `Ck` algorithm and returns a [`CheckedHrpstring`].
149
168
#[ inline]
150
169
pub fn validate_and_remove_checksum < Ck : Checksum > (
@@ -275,6 +294,25 @@ impl<'s> CheckedHrpstring<'s> {
275
294
#[ inline]
276
295
pub fn hrp ( & self ) -> Hrp { self . hrp }
277
296
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
+
278
316
/// Returns an iterator that yields the data part of the parsed bech32 encoded string.
279
317
///
280
318
/// Converts the ASCII bytes representing field elements to the respective field elements, then
@@ -398,8 +436,7 @@ impl<'s> SegwitHrpstring<'s> {
398
436
399
437
let unchecked = UncheckedHrpstring :: new ( s) ?;
400
438
401
- // TODO: Use accessor function.
402
- let data_part = unchecked. data_part_ascii ;
439
+ let data_part = unchecked. data_part_ascii ( ) ;
403
440
404
441
if data_part. is_empty ( ) {
405
442
return Err ( SegwitHrpstringError :: NoData ) ;
@@ -434,8 +471,7 @@ impl<'s> SegwitHrpstring<'s> {
434
471
#[ inline]
435
472
pub fn new_bech32 ( s : & ' s str ) -> Result < Self , SegwitHrpstringError > {
436
473
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 ( ) ;
439
475
440
476
// Unwrap ok since check_characters (in `Self::new`) checked the bech32-ness of this char.
441
477
let witness_version = Fe32 :: from_char ( data_part[ 0 ] . into ( ) ) . unwrap ( ) ;
@@ -463,6 +499,25 @@ impl<'s> SegwitHrpstring<'s> {
463
499
#[ inline]
464
500
pub fn witness_version ( & self ) -> Fe32 { self . witness_version }
465
501
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
+
466
521
/// Returns an iterator that yields the data part, excluding the witness version, of the parsed
467
522
/// bech32 encoded string.
468
523
///
0 commit comments