Skip to content

Commit 416e599

Browse files
committed
Add custom error for unknown address type parsing
Adds a custom error `UnknownAddressType(_)` which is returned in cases where an unknown address type tries to be parsed via `FromStr`. This provides more context for the error. For more info see [1]. [1] - `https://github.com/rust-bitcoin/rust-bitcoin/issues/1064`
1 parent 07692bb commit 416e599

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

src/util/address.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ pub enum Error {
8585
ExcessiveScriptSize,
8686
/// Script is not a p2pkh, p2sh or witness program.
8787
UnrecognizedScript,
88+
/// Address type is either invalid or not supported in rust-bitcoin.
89+
UnknownAddressType(String),
8890
}
8991

9092
impl fmt::Display for Error {
@@ -101,7 +103,8 @@ impl fmt::Display for Error {
101103
Error::InvalidSegwitV0ProgramLength(l) => write!(f, "a v0 witness program must be either of length 20 or 32 bytes: length={}", l),
102104
Error::UncompressedPubkey => write!(f, "an uncompressed pubkey was used where it is not allowed"),
103105
Error::ExcessiveScriptSize => write!(f, "script size exceed 520 bytes"),
104-
Error::UnrecognizedScript => write!(f, "script is not a p2pkh, p2sh or witness program")
106+
Error::UnrecognizedScript => write!(f, "script is not a p2pkh, p2sh or witness program"),
107+
Error::UnknownAddressType(ref s) => write!(f, "unknown address type: '{}' is either invalid or not supported in rust-bitcoin", s),
105108
}
106109
}
107110
}
@@ -124,7 +127,8 @@ impl std::error::Error for Error {
124127
| InvalidSegwitV0ProgramLength(_)
125128
| UncompressedPubkey
126129
| ExcessiveScriptSize
127-
| UnrecognizedScript => None,
130+
| UnrecognizedScript
131+
| UnknownAddressType(_) => None,
128132
}
129133
}
130134
}
@@ -172,15 +176,15 @@ impl fmt::Display for AddressType {
172176
}
173177

174178
impl FromStr for AddressType {
175-
type Err = ();
179+
type Err = Error;
176180
fn from_str(s: &str) -> Result<Self, Self::Err> {
177181
match s {
178182
"p2pkh" => Ok(AddressType::P2pkh),
179183
"p2sh" => Ok(AddressType::P2sh),
180184
"p2wpkh" => Ok(AddressType::P2wpkh),
181185
"p2wsh" => Ok(AddressType::P2wsh),
182186
"p2tr" => Ok(AddressType::P2tr),
183-
_ => Err(()),
187+
_ => Err(Error::UnknownAddressType(s.to_owned())),
184188
}
185189
}
186190
}
@@ -1510,4 +1514,17 @@ mod tests {
15101514
assert_eq!(Address::from_script(&bad_p2wsh, Network::Bitcoin), expected);
15111515
assert_eq!(Address::from_script(&invalid_segwitv0_script, Network::Bitcoin), Err(Error::InvalidSegwitV0ProgramLength(17)));
15121516
}
1517+
1518+
#[test]
1519+
fn valid_address_parses_correctly() {
1520+
let addr = AddressType::from_str("p2tr").expect("false negative while parsing address");
1521+
assert_eq!(addr, AddressType::P2tr);
1522+
}
1523+
1524+
#[test]
1525+
fn invalid_address_parses_error() {
1526+
let got = AddressType::from_str("invalid");
1527+
let want = Err(Error::UnknownAddressType("invalid".to_string()));
1528+
assert_eq!(got, want);
1529+
}
15131530
}

0 commit comments

Comments
 (0)