Skip to content

Commit e988f34

Browse files
committed
Improve error display for invalid witness version
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
1 parent d692972 commit e988f34

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

src/primitives/decode.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,8 @@ impl fmt::Display for SegwitHrpstringError {
599599
NoData => write!(f, "no data found after removing the checksum"),
600600
TooLong(len) =>
601601
write!(f, "encoded length {} exceeds spec limit {} chars", len, segwit::MAX_STRING_LENGTH),
602-
InvalidWitnessVersion(fe) => write!(f, "invalid segwit witness version: {}", fe),
602+
InvalidWitnessVersion(fe) =>
603+
write!(f, "invalid segwit witness version: {} (bech32 character: '{}')", fe.to_u8(), fe),
603604
Padding(ref e) => write_err!(f, "invalid padding on the witness data"; e),
604605
WitnessLength(ref e) => write_err!(f, "invalid witness length"; e),
605606
Checksum(ref e) => write_err!(f, "invalid checksum"; e),

src/primitives/segwit.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ pub fn validate_witness_program_length(
6565
#[non_exhaustive]
6666
pub struct InvalidWitnessVersionError(pub Fe32);
6767

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

0 commit comments

Comments
 (0)