@@ -143,7 +143,8 @@ func IsOptimistic(
143143 return true , nil
144144}
145145
146- // DecodeHexWithLength takes a string and a length and validates the hex while returning an error
146+ // DecodeHexWithLength takes a string and a length in bytes,
147+ // and validates whether the string is a hex and has the correct length.
147148func DecodeHexWithLength (s string , length int ) ([]byte , error ) {
148149 bytes , err := hexutil .Decode (s )
149150 if err != nil {
@@ -155,23 +156,25 @@ func DecodeHexWithLength(s string, length int) ([]byte, error) {
155156 return bytes , nil
156157}
157158
158- // DecodeHexWithMaxLength takes a string and a max byte length and validates the hex while returning an error
159- func DecodeHexWithMaxLength (s string , length int ) ([]byte , error ) {
159+ // DecodeHexWithMaxLength takes a string and a length in bytes,
160+ // and validates whether the string is a hex and has the correct length.
161+ func DecodeHexWithMaxLength (s string , maxLength int ) ([]byte , error ) {
160162 bytes , err := hexutil .Decode (s )
161163 if err != nil {
162164 return nil , errors .Wrap (err , fmt .Sprintf ("%s is not a valid hex" , s ))
163165 }
164- err = VerifyMaxLength (s , len ( bytes ), length )
166+ err = VerifyMaxLength (bytes , maxLength )
165167 if err != nil {
166- return nil , err
168+ return nil , errors . Wrap ( err , fmt . Sprintf ( "%s has too many bytes" , s ))
167169 }
168170 return bytes , nil
169171}
170172
171- // VerifyMaxLength takes in two lengths and returns an error
172- func VerifyMaxLength (name string , length int , max int ) error {
173- if length > max {
174- return fmt .Errorf ("%s length of %d bytes exceeds max of %d" , name , length , max )
173+ // VerifyMaxLength takes a slice and a maximum length and validates the length.
174+ func VerifyMaxLength [T any ](v []T , max int ) error {
175+ l := len (v )
176+ if l > max {
177+ return fmt .Errorf ("length of %d exceeds max of %d" , l , max )
175178 }
176179 return nil
177180}
0 commit comments