@@ -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 write_checksum ( & 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
}
@@ -539,7 +541,7 @@ pub fn encode_without_checksum<T: AsRef<[u5]>>(hrp: &str, data: T) -> Result<Str
539
541
/// Returns the HRP in lowercase, the data with the checksum removed, and the encoding.
540
542
pub fn decode ( s : & str ) -> Result < ( String , Vec < u5 > , Variant ) , Error > {
541
543
let ( hrp_lower, mut data) = decode_without_checksum ( s) ?;
542
- if data. len ( ) < 6 {
544
+ if data. len ( ) < CHECKSUM_LENGTH {
543
545
return Err ( Error :: InvalidLength ) ;
544
546
}
545
547
@@ -548,7 +550,7 @@ pub fn decode(s: &str) -> Result<(String, Vec<u5>, Variant), Error> {
548
550
Some ( variant) => {
549
551
// Remove checksum from data payload
550
552
let dbl: usize = data. len ( ) ;
551
- data. truncate ( dbl - 6 ) ;
553
+ data. truncate ( dbl - CHECKSUM_LENGTH ) ;
552
554
553
555
Ok ( ( hrp_lower, data, variant) )
554
556
}
0 commit comments