Skip to content

Commit 3a338a1

Browse files
committed
Merge rust-bitcoin/rust-bitcoin#1091: Add custom error for invalid parsing of address types
19ba7ec Add custom error for unknown address type parsing (Arturo Marquez) Pull request description: 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, since it contains the `String` that tried to be parsed. Closes rust-bitcoin/rust-bitcoin#1064 ACKs for top commit: Kixunil: ACK 19ba7ec dunxen: ACK 19ba7ec tcharding: ACK 19ba7ec apoelstra: ACK 19ba7ec Tree-SHA512: 2f94eb2e122c1acafcb8c852f7bfe22cb3725806541ae40f82d3a882011fb911ce8fc430153ebea4066f43c5a51813359a4c9b95d2a9077ce8f6dcaa58eee6fd
2 parents 8e7e59a + 416e599 commit 3a338a1

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
@@ -75,6 +75,8 @@ pub enum Error {
7575
ExcessiveScriptSize,
7676
/// Script is not a p2pkh, p2sh or witness program.
7777
UnrecognizedScript,
78+
/// Address type is either invalid or not supported in rust-bitcoin.
79+
UnknownAddressType(String),
7880
}
7981

8082
impl fmt::Display for Error {
@@ -91,7 +93,8 @@ impl fmt::Display for Error {
9193
Error::InvalidSegwitV0ProgramLength(l) => write!(f, "a v0 witness program must be either of length 20 or 32 bytes: length={}", l),
9294
Error::UncompressedPubkey => write!(f, "an uncompressed pubkey was used where it is not allowed"),
9395
Error::ExcessiveScriptSize => write!(f, "script size exceed 520 bytes"),
94-
Error::UnrecognizedScript => write!(f, "script is not a p2pkh, p2sh or witness program")
96+
Error::UnrecognizedScript => write!(f, "script is not a p2pkh, p2sh or witness program"),
97+
Error::UnknownAddressType(ref s) => write!(f, "unknown address type: '{}' is either invalid or not supported in rust-bitcoin", s),
9598
}
9699
}
97100
}
@@ -114,7 +117,8 @@ impl std::error::Error for Error {
114117
| InvalidSegwitV0ProgramLength(_)
115118
| UncompressedPubkey
116119
| ExcessiveScriptSize
117-
| UnrecognizedScript => None,
120+
| UnrecognizedScript
121+
| UnknownAddressType(_) => None,
118122
}
119123
}
120124
}
@@ -162,15 +166,15 @@ impl fmt::Display for AddressType {
162166
}
163167

164168
impl FromStr for AddressType {
165-
type Err = ();
169+
type Err = Error;
166170
fn from_str(s: &str) -> Result<Self, Self::Err> {
167171
match s {
168172
"p2pkh" => Ok(AddressType::P2pkh),
169173
"p2sh" => Ok(AddressType::P2sh),
170174
"p2wpkh" => Ok(AddressType::P2wpkh),
171175
"p2wsh" => Ok(AddressType::P2wsh),
172176
"p2tr" => Ok(AddressType::P2tr),
173-
_ => Err(()),
177+
_ => Err(Error::UnknownAddressType(s.to_owned())),
174178
}
175179
}
176180
}
@@ -1500,4 +1504,17 @@ mod tests {
15001504
assert_eq!(Address::from_script(&bad_p2wsh, Network::Bitcoin), expected);
15011505
assert_eq!(Address::from_script(&invalid_segwitv0_script, Network::Bitcoin), Err(Error::InvalidSegwitV0ProgramLength(17)));
15021506
}
1507+
1508+
#[test]
1509+
fn valid_address_parses_correctly() {
1510+
let addr = AddressType::from_str("p2tr").expect("false negative while parsing address");
1511+
assert_eq!(addr, AddressType::P2tr);
1512+
}
1513+
1514+
#[test]
1515+
fn invalid_address_parses_error() {
1516+
let got = AddressType::from_str("invalid");
1517+
let want = Err(Error::UnknownAddressType("invalid".to_string()));
1518+
assert_eq!(got, want);
1519+
}
15031520
}

0 commit comments

Comments
 (0)