@@ -78,6 +78,7 @@ use crate::primitives::checksum::{self, Checksum};
78
78
use crate :: primitives:: gf32:: Fe32 ;
79
79
use crate :: primitives:: hrp:: { self , Hrp } ;
80
80
use crate :: primitives:: iter:: { Fe32IterExt , FesToBytes } ;
81
+ use crate :: primitives:: segwit:: { self , WitnessLengthError } ;
81
82
use crate :: { write_err, Bech32 , Bech32m } ;
82
83
83
84
/// Separator between the hrp and payload (as defined by BIP-173).
@@ -264,7 +265,7 @@ impl<'s> CheckedHrpstring<'s> {
264
265
self . data = & self . data [ 1 ..] ; // Remove the witness version byte from data.
265
266
266
267
self . validate_padding ( ) ?;
267
- self . validate_witness_length ( witness_version) ?;
268
+ self . validate_witness_program_length ( witness_version) ?;
268
269
269
270
Ok ( SegwitHrpstring { hrp : self . hrp ( ) , witness_version, data : self . data } )
270
271
}
@@ -309,21 +310,11 @@ impl<'s> CheckedHrpstring<'s> {
309
310
/// Validates the segwit witness length rules.
310
311
///
311
312
/// Must be called after the witness version byte is removed from the data.
312
- #[ allow( clippy:: manual_range_contains) ] // For witness length range check.
313
- fn validate_witness_length ( & self , witness_version : Fe32 ) -> Result < ( ) , WitnessLengthError > {
314
- use WitnessLengthError :: * ;
315
-
316
- let witness_len = self . byte_iter ( ) . len ( ) ;
317
- if witness_len < 2 {
318
- return Err ( TooShort ) ;
319
- }
320
- if witness_len > 40 {
321
- return Err ( TooLong ) ;
322
- }
323
- if witness_version == Fe32 :: Q && witness_len != 20 && witness_len != 32 {
324
- return Err ( InvalidSegwitV0 ) ;
325
- }
326
- Ok ( ( ) )
313
+ fn validate_witness_program_length (
314
+ & self ,
315
+ witness_version : Fe32 ,
316
+ ) -> Result < ( ) , WitnessLengthError > {
317
+ segwit:: validate_witness_program_length ( self . byte_iter ( ) . len ( ) , witness_version)
327
318
}
328
319
}
329
320
@@ -746,41 +737,6 @@ impl std::error::Error for ChecksumError {
746
737
}
747
738
}
748
739
749
- /// Witness program invalid because of incorrect length.
750
- #[ derive( Debug , Clone , PartialEq , Eq ) ]
751
- #[ non_exhaustive]
752
- pub enum WitnessLengthError {
753
- /// The witness data is too short.
754
- TooShort ,
755
- /// The witness data is too long.
756
- TooLong ,
757
- /// The segwit v0 witness is not 20 or 32 bytes long.
758
- InvalidSegwitV0 ,
759
- }
760
-
761
- impl fmt:: Display for WitnessLengthError {
762
- fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
763
- use WitnessLengthError :: * ;
764
-
765
- match * self {
766
- TooShort => write ! ( f, "witness program is less than 2 bytes long" ) ,
767
- TooLong => write ! ( f, "witness program is more than 40 bytes long" ) ,
768
- InvalidSegwitV0 => write ! ( f, "the segwit v0 witness is not 20 or 32 bytes long" ) ,
769
- }
770
- }
771
- }
772
-
773
- #[ cfg( feature = "std" ) ]
774
- impl std:: error:: Error for WitnessLengthError {
775
- fn source ( & self ) -> Option < & ( dyn std:: error:: Error + ' static ) > {
776
- use WitnessLengthError :: * ;
777
-
778
- match * self {
779
- TooShort | TooLong | InvalidSegwitV0 => None ,
780
- }
781
- }
782
- }
783
-
784
740
/// Error validating the padding bits on the witness data.
785
741
#[ derive( Debug , Clone , PartialEq , Eq ) ]
786
742
pub enum PaddingError {
0 commit comments