@@ -128,6 +128,8 @@ pub trait WriteBase32 {
128
128
fn write_u5 ( & mut self , data : u5 ) -> Result < ( ) , Self :: Err > ;
129
129
}
130
130
131
+ const CHECKSUM_LENGTH : usize = 6 ;
132
+
131
133
/// Allocationless Bech32 writer that accumulates the checksum data internally and writes them out
132
134
/// in the end.
133
135
pub struct Bech32Writer < ' a > {
@@ -187,13 +189,13 @@ impl<'a> Bech32Writer<'a> {
187
189
188
190
fn inner_finalize ( & mut self ) -> fmt:: Result {
189
191
// Pad with 6 zeros
190
- for _ in 0 ..6 {
192
+ for _ in 0 ..CHECKSUM_LENGTH {
191
193
self . polymod_step ( u5 ( 0 ) )
192
194
}
193
195
194
196
let plm: u32 = self . chk ^ self . variant . constant ( ) ;
195
197
196
- for p in 0 ..6 {
198
+ for p in 0 ..CHECKSUM_LENGTH {
197
199
self . formatter
198
200
. write_char ( u5 ( ( ( plm >> ( 5 * ( 5 - p) ) ) & 0x1f ) as u8 ) . to_char ( ) ) ?;
199
201
}
@@ -469,7 +471,7 @@ pub fn encode<T: AsRef<[u5]>>(hrp: &str, data: T, variant: Variant) -> Result<St
469
471
/// Returns the HRP in lowercase..
470
472
pub fn decode ( s : & str ) -> Result < ( String , Vec < u5 > , Variant ) , Error > {
471
473
// Ensure overall length is within bounds
472
- if s. len ( ) < 8 {
474
+ if s. len ( ) < CHECKSUM_LENGTH + 2 {
473
475
return Err ( Error :: InvalidLength ) ;
474
476
}
475
477
@@ -481,7 +483,7 @@ pub fn decode(s: &str) -> Result<(String, Vec<u5>, Variant), Error> {
481
483
( hrp, & data[ 1 ..] )
482
484
}
483
485
} ;
484
- if raw_data. len ( ) < 6 {
486
+ if raw_data. len ( ) < CHECKSUM_LENGTH {
485
487
return Err ( Error :: InvalidLength ) ;
486
488
}
487
489
@@ -533,7 +535,7 @@ pub fn decode(s: &str) -> Result<(String, Vec<u5>, Variant), Error> {
533
535
Some ( variant) => {
534
536
// Remove checksum from data payload
535
537
let dbl: usize = data. len ( ) ;
536
- data. truncate ( dbl - 6 ) ;
538
+ data. truncate ( dbl - CHECKSUM_LENGTH ) ;
537
539
538
540
Ok ( ( hrp_lower, data, variant) )
539
541
}
0 commit comments