File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -164,6 +164,39 @@ impl<'s> UncheckedHrpstring<'s> {
164
164
#[ inline]
165
165
pub fn data_part_ascii ( & self ) -> & [ u8 ] { self . data_part_ascii }
166
166
167
+ /// Returns the segwit witness version if there is one.
168
+ ///
169
+ /// Attempts to convert the first character of the data part to a witness version. If this
170
+ /// succeeds, and it is a valid version (0..16 inclusive) we return it, otherwise `None`.
171
+ ///
172
+ /// This function makes no guarantees on the validity of the checksum.
173
+ ///
174
+ /// # Examples
175
+ ///
176
+ /// ```
177
+ /// use bech32::{primitives::decode::UncheckedHrpstring, Fe32};
178
+ ///
179
+ /// // Note the invalid checksum!
180
+ /// let addr = "bc1qar0srrr7xfkvy5l643lydnw9re59gtzzffffff";
181
+ ///
182
+ /// let unchecked = UncheckedHrpstring::new(&addr).unwrap();
183
+ /// assert_eq!(unchecked.witness_version(), Some(Fe32::Q));
184
+ /// ```
185
+ #[ inline]
186
+ pub fn witness_version ( & self ) -> Option < Fe32 > {
187
+ let data_part = self . data_part_ascii ( ) ;
188
+ if data_part. is_empty ( ) {
189
+ return None ;
190
+ }
191
+
192
+ // unwrap ok because we know we gave valid bech32 characters.
193
+ let witness_version = Fe32 :: from_char ( data_part[ 0 ] . into ( ) ) . unwrap ( ) ;
194
+ if witness_version. to_u8 ( ) > 16 {
195
+ return None ;
196
+ }
197
+ Some ( witness_version)
198
+ }
199
+
167
200
/// Validates that data has a valid checksum for the `Ck` algorithm and returns a [`CheckedHrpstring`].
168
201
#[ inline]
169
202
pub fn validate_and_remove_checksum < Ck : Checksum > (
You can’t perform that action at this time.
0 commit comments