Skip to content

Commit e0494d7

Browse files
committed
Use a constant for checksum length
1 parent 82367c8 commit e0494d7

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/lib.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,13 @@ impl<'a> Bech32Writer<'a> {
194194
}
195195

196196
// Pad with 6 zeros
197-
for _ in 0..6 {
197+
for _ in 0..CHECKSUM_LENGTH {
198198
self.polymod_step(u5(0))
199199
}
200200

201201
let plm: u32 = self.chk ^ self.variant.constant();
202202

203-
for p in 0..6 {
203+
for p in 0..CHECKSUM_LENGTH {
204204
self.formatter
205205
.write_char(u5(((plm >> (5 * (5 - p))) & 0x1f) as u8).to_char())?;
206206
}
@@ -468,6 +468,8 @@ pub enum Checksum {
468468
Exclude,
469469
}
470470

471+
const CHECKSUM_LENGTH: usize = 6;
472+
471473
/// Encode a bech32 payload to string.
472474
///
473475
/// # Errors
@@ -497,7 +499,7 @@ pub fn encode_without_checksum<T: AsRef<[u5]>>(hrp: &str, data: T, variant: Vari
497499
/// Returns the HRP in lowercase, the data with the checksum removed, and the encoding.
498500
pub fn decode(s: &str) -> Result<(String, Vec<u5>, Variant), Error> {
499501
let (hrp_lower, mut data) = decode_without_checksum(s)?;
500-
if data.len() < 6 {
502+
if data.len() < CHECKSUM_LENGTH {
501503
return Err(Error::InvalidLength);
502504
}
503505

@@ -506,7 +508,7 @@ pub fn decode(s: &str) -> Result<(String, Vec<u5>, Variant), Error> {
506508
Some(variant) => {
507509
// Remove checksum from data payload
508510
let dbl: usize = data.len();
509-
data.truncate(dbl - 6);
511+
data.truncate(dbl - CHECKSUM_LENGTH);
510512

511513
Ok((hrp_lower, data, variant))
512514
}

0 commit comments

Comments
 (0)