1616use crate :: prelude:: * ;
1717
1818use anyhow:: Result ;
19- use bech32:: { self , FromBase32 , ToBase32 } ;
2019use serde:: { Deserialize , Deserializer , Serialize , Serializer , de} ;
2120use std:: borrow:: Borrow ;
2221
@@ -97,17 +96,16 @@ impl<T: Clone + Debug + ToBytes + FromBytes + PartialEq + Eq + Sync + Send, cons
9796 /// Reads in a bech32m string.
9897 #[ inline]
9998 fn from_str ( string : & str ) -> Result < Self , Self :: Err > {
100- let ( hrp, data, variant) = bech32:: decode ( string) ?;
99+ let checked = bech32:: primitives:: decode:: CheckedHrpstring :: new :: < LongBech32m > ( string) ?;
100+ let hrp = checked. hrp ( ) ;
101+ let data: Vec < u8 > = checked. byte_iter ( ) . collect ( ) ;
101102 if hrp. as_bytes ( ) != PREFIX . to_le_bytes ( ) {
102103 bail ! ( "Invalid prefix for a bech32m hash: {hrp}" )
103104 } ;
104105 if data. is_empty ( ) {
105106 bail ! ( "Bech32m hash data is empty" )
106107 }
107- if variant != bech32:: Variant :: Bech32m {
108- bail ! ( "Hash is not a bech32m hash" )
109- }
110- Ok ( Self :: read_le ( & * Vec :: from_base32 ( & data) ?) ?)
108+ Ok ( Self :: read_le ( & * data) ?)
111109 }
112110}
113111
@@ -116,13 +114,12 @@ impl<T: Clone + Debug + ToBytes + FromBytes + PartialEq + Eq + Sync + Send, cons
116114{
117115 #[ inline]
118116 fn fmt ( & self , f : & mut Formatter ) -> fmt:: Result {
119- bech32:: encode_to_fmt (
117+ bech32:: encode_to_fmt :: < LongBech32m , _ > (
120118 f,
121- & Self :: prefix ( ) ,
122- self . 0 . to_bytes_le ( ) . expect ( "Failed to write data as bytes" ) . to_base32 ( ) ,
123- bech32:: Variant :: Bech32m ,
119+ bech32:: Hrp :: parse_unchecked ( & Self :: prefix ( ) ) ,
120+ & self . 0 . to_bytes_le ( ) . expect ( "Failed to write data as bytes" ) ,
124121 )
125- . expect ( "Failed to encode in bech32m" )
122+ . map_err ( |_| fmt :: Error )
126123 }
127124}
128125
0 commit comments