Skip to content

Commit

Permalink
Merge #165: Improve error display for invalid witness version
Browse files Browse the repository at this point in the history
e988f34 Improve error display for invalid witness version (Tobin C. Harding)
d692972 Add rustfmt::skip to Display impl (Tobin C. Harding)

Pull request description:

  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

ACKs for top commit:
  apoelstra:
    ACK e988f34

Tree-SHA512: 93d666f396c536d6348cf793b74c27363eeacec02c0a7a4ec53786778f7c64c393bad90fe10b718f872d61deb355de4b18f4fd8a66e35a1f53e09ced2cbe7c0b
  • Loading branch information
apoelstra committed Jan 9, 2024
2 parents b7642a6 + e988f34 commit 84b2c50
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
12 changes: 5 additions & 7 deletions src/primitives/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,20 +589,18 @@ pub enum SegwitHrpstringError {
Checksum(ChecksumError),
}

#[rustfmt::skip]
impl fmt::Display for SegwitHrpstringError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use SegwitHrpstringError::*;

match *self {
Unchecked(ref e) => write_err!(f, "parsing unchecked hrpstring failed"; e),
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),
TooLong(len) =>
write!(f, "encoded length {} exceeds spec limit {} chars", len, segwit::MAX_STRING_LENGTH),
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 84b2c50

Please sign in to comment.