Skip to content

Commit

Permalink
Improve error display for invalid witness version
Browse files Browse the repository at this point in the history
We have two errors that cover invalid witness version, for one we print
the field element which can be confusing for some values and for the
other we omit the invalid version all together - we can do better.

Print the field element as well as the integer value when displaying the
two invalid witness version errors.

Fix: #162
  • Loading branch information
tcharding committed Jan 9, 2024
1 parent d692972 commit e988f34
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/primitives/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,8 @@ impl fmt::Display for SegwitHrpstringError {
NoData => write!(f, "no data found after removing the checksum"),
TooLong(len) =>
write!(f, "encoded length {} exceeds spec limit {} chars", len, segwit::MAX_STRING_LENGTH),
InvalidWitnessVersion(fe) => write!(f, "invalid segwit witness version: {}", fe),
InvalidWitnessVersion(fe) =>
write!(f, "invalid segwit witness version: {} (bech32 character: '{}')", fe.to_u8(), fe),
Padding(ref e) => write_err!(f, "invalid padding on the witness data"; e),
WitnessLength(ref e) => write_err!(f, "invalid witness length"; e),
Checksum(ref e) => write_err!(f, "invalid checksum"; e),
Expand Down
3 changes: 2 additions & 1 deletion src/primitives/segwit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,10 @@ pub fn validate_witness_program_length(
#[non_exhaustive]
pub struct InvalidWitnessVersionError(pub Fe32);

#[rustfmt::skip]
impl fmt::Display for InvalidWitnessVersionError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "field element does not represent a valid witness version")
write!(f, "invalid segwit witness version: {} (bech32 character: '{}')", self.0.to_u8(), self.0)
}
}

Expand Down

0 comments on commit e988f34

Please sign in to comment.