Skip to content

Commit 045b50a

Browse files
committed
decode: Rename data field
We have a `data` field in each of the structs but it contains different stuff. Re-name it to `data_part_ascii` when it is the data part and `ascii` when it is a subset of the data part.
1 parent b7642a6 commit 045b50a

File tree

1 file changed

+39
-32
lines changed

1 file changed

+39
-32
lines changed

src/primitives/decode.rs

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ pub struct UncheckedHrpstring<'s> {
117117
hrp: Hrp,
118118
/// This is ASCII byte values of the parsed string, guaranteed to be valid bech32 characters.
119119
///
120-
/// Contains the checksum if one was present in the parsed string.
121-
data: &'s [u8],
120+
/// The characters after the separator i.e., the "data part" defined by BIP-173.
121+
data_part_ascii: &'s [u8],
122122
/// The length of the parsed hrpstring.
123123
hrpstring_length: usize,
124124
}
@@ -130,11 +130,11 @@ impl<'s> UncheckedHrpstring<'s> {
130130
#[inline]
131131
pub fn new(s: &'s str) -> Result<Self, UncheckedHrpstringError> {
132132
let sep_pos = check_characters(s)?;
133-
let (hrp, data) = s.split_at(sep_pos);
133+
let (hrp, rest) = s.split_at(sep_pos);
134134

135135
let ret = UncheckedHrpstring {
136136
hrp: Hrp::parse(hrp)?,
137-
data: data[1..].as_bytes(), // Skip the separator.
137+
data_part_ascii: rest[1..].as_bytes(), // Skip the separator.
138138
hrpstring_length: s.len(),
139139
};
140140

@@ -183,15 +183,15 @@ impl<'s> UncheckedHrpstring<'s> {
183183
return Ok(());
184184
}
185185

186-
if self.data.len() < Ck::CHECKSUM_LENGTH {
186+
if self.data_part_ascii.len() < Ck::CHECKSUM_LENGTH {
187187
return Err(InvalidLength);
188188
}
189189

190190
let mut checksum_eng = checksum::Engine::<Ck>::new();
191191
checksum_eng.input_hrp(self.hrp());
192192

193193
// Unwrap ok since we checked all characters in our constructor.
194-
for fe in self.data.iter().map(|&b| Fe32::from_char_unchecked(b)) {
194+
for fe in self.data_part_ascii.iter().map(|&b| Fe32::from_char_unchecked(b)) {
195195
checksum_eng.input_fe(fe);
196196
}
197197

@@ -213,20 +213,20 @@ impl<'s> UncheckedHrpstring<'s> {
213213
/// May panic if data is not valid.
214214
#[inline]
215215
pub fn remove_checksum<Ck: Checksum>(self) -> CheckedHrpstring<'s> {
216-
let data_len = self.data.len() - Ck::CHECKSUM_LENGTH;
216+
let end = self.data_part_ascii.len() - Ck::CHECKSUM_LENGTH;
217217

218218
CheckedHrpstring {
219219
hrp: self.hrp(),
220-
data: &self.data[..data_len],
220+
ascii: &self.data_part_ascii[..end],
221221
hrpstring_length: self.hrpstring_length,
222222
}
223223
}
224224
}
225225

226226
/// An HRP string that has been parsed and had the checksum validated.
227227
///
228-
/// This type does not treat the first byte of the data in any special way i.e., as the witness
229-
/// version byte. If you are parsing Bitcoin segwit addresses you likely want to use [`SegwitHrpstring`].
228+
/// This type does not treat the first byte of the data part in any special way i.e., as the witness
229+
/// version byte. If you are parsing Bitcoin segwit addresses consider using [`SegwitHrpstring`].
230230
///
231231
/// > We first describe the general checksummed base32 format called Bech32 and then
232232
/// > define Segregated Witness addresses using it.
@@ -250,9 +250,10 @@ impl<'s> UncheckedHrpstring<'s> {
250250
pub struct CheckedHrpstring<'s> {
251251
/// The human-readable part, guaranteed to be lowercase ASCII characters.
252252
hrp: Hrp,
253-
/// This is ASCII byte values of the parsed string, guaranteed to be valid bech32 characters,
254-
/// with the checksum removed.
255-
data: &'s [u8],
253+
/// This is ASCII byte values of the parsed string, guaranteed to be valid bech32 characters.
254+
///
255+
/// The characters after the '1' separator and the before the checksum.
256+
ascii: &'s [u8],
256257
/// The length of the parsed hrpstring.
257258
hrpstring_length: usize, // Guaranteed to be <= CK::CODE_LENGTH
258259
}
@@ -280,13 +281,13 @@ impl<'s> CheckedHrpstring<'s> {
280281
/// converts the stream of field elements to a stream of bytes.
281282
#[inline]
282283
pub fn byte_iter(&self) -> ByteIter {
283-
ByteIter { iter: AsciiToFe32Iter { iter: self.data.iter().copied() }.fes_to_bytes() }
284+
ByteIter { iter: AsciiToFe32Iter { iter: self.ascii.iter().copied() }.fes_to_bytes() }
284285
}
285286

286287
/// Converts this type to a [`SegwitHrpstring`] after validating the witness and HRP.
287288
#[inline]
288289
pub fn validate_segwit(mut self) -> Result<SegwitHrpstring<'s>, SegwitHrpstringError> {
289-
if self.data.is_empty() {
290+
if self.ascii.is_empty() {
290291
return Err(SegwitHrpstringError::NoData);
291292
}
292293

@@ -295,28 +296,28 @@ impl<'s> CheckedHrpstring<'s> {
295296
}
296297

297298
// Unwrap ok since check_characters checked the bech32-ness of this char.
298-
let witness_version = Fe32::from_char(self.data[0].into()).unwrap();
299-
self.data = &self.data[1..]; // Remove the witness version byte from data.
299+
let witness_version = Fe32::from_char(self.ascii[0].into()).unwrap();
300+
self.ascii = &self.ascii[1..]; // Remove the witness version byte.
300301

301302
self.validate_padding()?;
302303
self.validate_witness_program_length(witness_version)?;
303304

304-
Ok(SegwitHrpstring { hrp: self.hrp(), witness_version, data: self.data })
305+
Ok(SegwitHrpstring { hrp: self.hrp(), witness_version, ascii: self.ascii })
305306
}
306307

307308
/// Validates the segwit padding rules.
308309
///
309-
/// Must be called after the witness version byte is removed from the data.
310+
/// Must be called after the witness version byte is removed from the data part.
310311
///
311312
/// From BIP-173:
312313
/// > Re-arrange those bits into groups of 8 bits. Any incomplete group at the
313314
/// > end MUST be 4 bits or less, MUST be all zeroes, and is discarded.
314315
fn validate_padding(&self) -> Result<(), PaddingError> {
315-
if self.data.is_empty() {
316+
if self.ascii.is_empty() {
316317
return Ok(()); // Empty data implies correct padding.
317318
}
318319

319-
let fe_iter = AsciiToFe32Iter { iter: self.data.iter().copied() };
320+
let fe_iter = AsciiToFe32Iter { iter: self.ascii.iter().copied() };
320321
let padding_len = fe_iter.len() * 5 % 8;
321322

322323
if padding_len > 4 {
@@ -343,7 +344,7 @@ impl<'s> CheckedHrpstring<'s> {
343344

344345
/// Validates the segwit witness length rules.
345346
///
346-
/// Must be called after the witness version byte is removed from the data.
347+
/// Must be called after the witness version byte is removed from the data part.
347348
fn validate_witness_program_length(
348349
&self,
349350
witness_version: Fe32,
@@ -372,11 +373,12 @@ impl<'s> CheckedHrpstring<'s> {
372373
pub struct SegwitHrpstring<'s> {
373374
/// The human-readable part, valid for segwit addresses.
374375
hrp: Hrp,
375-
/// The first byte of the parsed data.
376+
/// The first byte of the parsed data part.
376377
witness_version: Fe32,
377-
/// This is ASCII byte values of the parsed string, guaranteed to be valid bech32 characters,
378-
/// with the witness version and checksum removed.
379-
data: &'s [u8],
378+
/// This is ASCII byte values of the parsed string, guaranteed to be valid bech32 characters.
379+
///
380+
/// The characters after the witness version and before the checksum.
381+
ascii: &'s [u8],
380382
}
381383

382384
impl<'s> SegwitHrpstring<'s> {
@@ -396,12 +398,15 @@ impl<'s> SegwitHrpstring<'s> {
396398

397399
let unchecked = UncheckedHrpstring::new(s)?;
398400

399-
if unchecked.data.is_empty() {
401+
// TODO: Use accessor function.
402+
let data_part = unchecked.data_part_ascii;
403+
404+
if data_part.is_empty() {
400405
return Err(SegwitHrpstringError::NoData);
401406
}
402407

403408
// Unwrap ok since check_characters (in `Self::new`) checked the bech32-ness of this char.
404-
let witness_version = Fe32::from_char(unchecked.data[0].into()).unwrap();
409+
let witness_version = Fe32::from_char(data_part[0].into()).unwrap();
405410
if witness_version.to_u8() > 16 {
406411
return Err(SegwitHrpstringError::InvalidWitnessVersion(witness_version));
407412
}
@@ -429,9 +434,11 @@ impl<'s> SegwitHrpstring<'s> {
429434
#[inline]
430435
pub fn new_bech32(s: &'s str) -> Result<Self, SegwitHrpstringError> {
431436
let unchecked = UncheckedHrpstring::new(s)?;
437+
// TODO: Use accessor function.
438+
let data_part = unchecked.data_part_ascii;
432439

433440
// Unwrap ok since check_characters (in `Self::new`) checked the bech32-ness of this char.
434-
let witness_version = Fe32::from_char(unchecked.data[0].into()).unwrap();
441+
let witness_version = Fe32::from_char(data_part[0].into()).unwrap();
435442
if witness_version.to_u8() > 16 {
436443
return Err(SegwitHrpstringError::InvalidWitnessVersion(witness_version));
437444
}
@@ -465,12 +472,12 @@ impl<'s> SegwitHrpstring<'s> {
465472
/// Use `self.witness_version()` to get the witness version.
466473
#[inline]
467474
pub fn byte_iter(&self) -> ByteIter {
468-
ByteIter { iter: AsciiToFe32Iter { iter: self.data.iter().copied() }.fes_to_bytes() }
475+
ByteIter { iter: AsciiToFe32Iter { iter: self.ascii.iter().copied() }.fes_to_bytes() }
469476
}
470477
}
471478

472-
/// Checks whether a given HRP string has data characters in the bech32 alphabet (incl. checksum
473-
/// characters), and that the whole string has consistent casing (hrp, data, and checksum).
479+
/// Checks whether a given HRP string has data part characters in the bech32 alphabet (incl.
480+
/// checksum characters), and that the whole string has consistent casing (hrp and data part).
474481
///
475482
/// # Returns
476483
///

0 commit comments

Comments
 (0)