@@ -128,6 +128,8 @@ pub trait WriteBase32 {
128128 fn write_u5 ( & mut self , data : u5 ) -> Result < ( ) , Self :: Err > ;
129129}
130130
131+ const CHECKSUM_LENGTH : usize = 6 ;
132+
131133/// Allocationless Bech32 writer that accumulates the checksum data internally and writes them out
132134/// in the end.
133135pub struct Bech32Writer < ' a > {
@@ -187,13 +189,13 @@ impl<'a> Bech32Writer<'a> {
187189
188190 fn write_checksum ( & mut self ) -> fmt:: Result {
189191 // Pad with 6 zeros
190- for _ in 0 ..6 {
192+ for _ in 0 ..CHECKSUM_LENGTH {
191193 self . polymod_step ( u5 ( 0 ) )
192194 }
193195
194196 let plm: u32 = self . chk ^ self . variant . constant ( ) ;
195197
196- for p in 0 ..6 {
198+ for p in 0 ..CHECKSUM_LENGTH {
197199 self . formatter
198200 . write_char ( u5 ( ( ( plm >> ( 5 * ( 5 - p) ) ) & 0x1f ) as u8 ) . to_char ( ) ) ?;
199201 }
@@ -539,7 +541,7 @@ pub fn encode_without_checksum<T: AsRef<[u5]>>(hrp: &str, data: T) -> Result<Str
539541/// Returns the HRP in lowercase, the data with the checksum removed, and the encoding.
540542pub fn decode ( s : & str ) -> Result < ( String , Vec < u5 > , Variant ) , Error > {
541543 let ( hrp_lower, mut data) = decode_without_checksum ( s) ?;
542- if data. len ( ) < 6 {
544+ if data. len ( ) < CHECKSUM_LENGTH {
543545 return Err ( Error :: InvalidLength ) ;
544546 }
545547
@@ -548,7 +550,7 @@ pub fn decode(s: &str) -> Result<(String, Vec<u5>, Variant), Error> {
548550 Some ( variant) => {
549551 // Remove checksum from data payload
550552 let dbl: usize = data. len ( ) ;
551- data. truncate ( dbl - 6 ) ;
553+ data. truncate ( dbl - CHECKSUM_LENGTH ) ;
552554
553555 Ok ( ( hrp_lower, data, variant) )
554556 }
0 commit comments