@@ -117,8 +117,8 @@ pub struct UncheckedHrpstring<'s> {
117
117
hrp : Hrp ,
118
118
/// This is ASCII byte values of the parsed string, guaranteed to be valid bech32 characters.
119
119
///
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 ] ,
122
122
/// The length of the parsed hrpstring.
123
123
hrpstring_length : usize ,
124
124
}
@@ -130,11 +130,11 @@ impl<'s> UncheckedHrpstring<'s> {
130
130
#[ inline]
131
131
pub fn new ( s : & ' s str ) -> Result < Self , UncheckedHrpstringError > {
132
132
let sep_pos = check_characters ( s) ?;
133
- let ( hrp, data ) = s. split_at ( sep_pos) ;
133
+ let ( hrp, rest ) = s. split_at ( sep_pos) ;
134
134
135
135
let ret = UncheckedHrpstring {
136
136
hrp : Hrp :: parse ( hrp) ?,
137
- data : data [ 1 ..] . as_bytes ( ) , // Skip the separator.
137
+ data_part_ascii : rest [ 1 ..] . as_bytes ( ) , // Skip the separator.
138
138
hrpstring_length : s. len ( ) ,
139
139
} ;
140
140
@@ -183,15 +183,15 @@ impl<'s> UncheckedHrpstring<'s> {
183
183
return Ok ( ( ) ) ;
184
184
}
185
185
186
- if self . data . len ( ) < Ck :: CHECKSUM_LENGTH {
186
+ if self . data_part_ascii . len ( ) < Ck :: CHECKSUM_LENGTH {
187
187
return Err ( InvalidLength ) ;
188
188
}
189
189
190
190
let mut checksum_eng = checksum:: Engine :: < Ck > :: new ( ) ;
191
191
checksum_eng. input_hrp ( self . hrp ( ) ) ;
192
192
193
193
// 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) ) {
195
195
checksum_eng. input_fe ( fe) ;
196
196
}
197
197
@@ -213,20 +213,20 @@ impl<'s> UncheckedHrpstring<'s> {
213
213
/// May panic if data is not valid.
214
214
#[ inline]
215
215
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 ;
217
217
218
218
CheckedHrpstring {
219
219
hrp : self . hrp ( ) ,
220
- data : & self . data [ ..data_len ] ,
220
+ ascii : & self . data_part_ascii [ ..end ] ,
221
221
hrpstring_length : self . hrpstring_length ,
222
222
}
223
223
}
224
224
}
225
225
226
226
/// An HRP string that has been parsed and had the checksum validated.
227
227
///
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`].
230
230
///
231
231
/// > We first describe the general checksummed base32 format called Bech32 and then
232
232
/// > define Segregated Witness addresses using it.
@@ -250,9 +250,10 @@ impl<'s> UncheckedHrpstring<'s> {
250
250
pub struct CheckedHrpstring < ' s > {
251
251
/// The human-readable part, guaranteed to be lowercase ASCII characters.
252
252
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 ] ,
256
257
/// The length of the parsed hrpstring.
257
258
hrpstring_length : usize , // Guaranteed to be <= CK::CODE_LENGTH
258
259
}
@@ -280,13 +281,13 @@ impl<'s> CheckedHrpstring<'s> {
280
281
/// converts the stream of field elements to a stream of bytes.
281
282
#[ inline]
282
283
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 ( ) }
284
285
}
285
286
286
287
/// Converts this type to a [`SegwitHrpstring`] after validating the witness and HRP.
287
288
#[ inline]
288
289
pub fn validate_segwit ( mut self ) -> Result < SegwitHrpstring < ' s > , SegwitHrpstringError > {
289
- if self . data . is_empty ( ) {
290
+ if self . ascii . is_empty ( ) {
290
291
return Err ( SegwitHrpstringError :: NoData ) ;
291
292
}
292
293
@@ -295,28 +296,28 @@ impl<'s> CheckedHrpstring<'s> {
295
296
}
296
297
297
298
// 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.
300
301
301
302
self . validate_padding ( ) ?;
302
303
self . validate_witness_program_length ( witness_version) ?;
303
304
304
- Ok ( SegwitHrpstring { hrp : self . hrp ( ) , witness_version, data : self . data } )
305
+ Ok ( SegwitHrpstring { hrp : self . hrp ( ) , witness_version, ascii : self . ascii } )
305
306
}
306
307
307
308
/// Validates the segwit padding rules.
308
309
///
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 .
310
311
///
311
312
/// From BIP-173:
312
313
/// > Re-arrange those bits into groups of 8 bits. Any incomplete group at the
313
314
/// > end MUST be 4 bits or less, MUST be all zeroes, and is discarded.
314
315
fn validate_padding ( & self ) -> Result < ( ) , PaddingError > {
315
- if self . data . is_empty ( ) {
316
+ if self . ascii . is_empty ( ) {
316
317
return Ok ( ( ) ) ; // Empty data implies correct padding.
317
318
}
318
319
319
- let fe_iter = AsciiToFe32Iter { iter : self . data . iter ( ) . copied ( ) } ;
320
+ let fe_iter = AsciiToFe32Iter { iter : self . ascii . iter ( ) . copied ( ) } ;
320
321
let padding_len = fe_iter. len ( ) * 5 % 8 ;
321
322
322
323
if padding_len > 4 {
@@ -343,7 +344,7 @@ impl<'s> CheckedHrpstring<'s> {
343
344
344
345
/// Validates the segwit witness length rules.
345
346
///
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 .
347
348
fn validate_witness_program_length (
348
349
& self ,
349
350
witness_version : Fe32 ,
@@ -372,11 +373,12 @@ impl<'s> CheckedHrpstring<'s> {
372
373
pub struct SegwitHrpstring < ' s > {
373
374
/// The human-readable part, valid for segwit addresses.
374
375
hrp : Hrp ,
375
- /// The first byte of the parsed data.
376
+ /// The first byte of the parsed data part .
376
377
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 ] ,
380
382
}
381
383
382
384
impl < ' s > SegwitHrpstring < ' s > {
@@ -396,12 +398,15 @@ impl<'s> SegwitHrpstring<'s> {
396
398
397
399
let unchecked = UncheckedHrpstring :: new ( s) ?;
398
400
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 ( ) {
400
405
return Err ( SegwitHrpstringError :: NoData ) ;
401
406
}
402
407
403
408
// 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 ( ) ;
405
410
if witness_version. to_u8 ( ) > 16 {
406
411
return Err ( SegwitHrpstringError :: InvalidWitnessVersion ( witness_version) ) ;
407
412
}
@@ -429,9 +434,11 @@ impl<'s> SegwitHrpstring<'s> {
429
434
#[ inline]
430
435
pub fn new_bech32 ( s : & ' s str ) -> Result < Self , SegwitHrpstringError > {
431
436
let unchecked = UncheckedHrpstring :: new ( s) ?;
437
+ // TODO: Use accessor function.
438
+ let data_part = unchecked. data_part_ascii ;
432
439
433
440
// 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 ( ) ;
435
442
if witness_version. to_u8 ( ) > 16 {
436
443
return Err ( SegwitHrpstringError :: InvalidWitnessVersion ( witness_version) ) ;
437
444
}
@@ -465,12 +472,12 @@ impl<'s> SegwitHrpstring<'s> {
465
472
/// Use `self.witness_version()` to get the witness version.
466
473
#[ inline]
467
474
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 ( ) }
469
476
}
470
477
}
471
478
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 ).
474
481
///
475
482
/// # Returns
476
483
///
0 commit comments