Skip to content

Commit

Permalink
Add new UnknownAddressTypeError for parsing address type
Browse files Browse the repository at this point in the history
There is no need to use the general `address::Error` when parsing an
address type, there is only one error path.
  • Loading branch information
tcharding committed Sep 20, 2023
1 parent e2014cb commit 0f536e8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
21 changes: 13 additions & 8 deletions bitcoin/src/address/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use internals::write_err;

use crate::address::{Address, NetworkUnchecked};
use crate::blockdata::script::{witness_program, witness_version};
use crate::error::impl_std_error;
use crate::prelude::String;
use crate::{base58, Network};

Expand Down Expand Up @@ -34,8 +35,6 @@ pub enum Error {
ExcessiveScriptSize,
/// Script is not a p2pkh, p2sh or witness program.
UnrecognizedScript,
/// Address type is either invalid or not supported in rust-bitcoin.
UnknownAddressType(String),
/// Address's network differs from required one.
NetworkValidation {
/// Network that was required.
Expand Down Expand Up @@ -66,11 +65,6 @@ impl fmt::Display for Error {
write!(f, "an uncompressed pubkey was used where it is not allowed"),
ExcessiveScriptSize => write!(f, "script size exceed 520 bytes"),
UnrecognizedScript => write!(f, "script is not a p2pkh, p2sh or witness program"),
UnknownAddressType(ref s) => write!(
f,
"unknown address type: '{}' is either invalid or not supported in rust-bitcoin",
s
),
NetworkValidation { required, found, ref address } => {
write!(f, "address ")?;
address.fmt_internal(f)?; // Using fmt_internal in order to remove the "Address<NetworkUnchecked>(..)" wrapper
Expand Down Expand Up @@ -99,7 +93,6 @@ impl std::error::Error for Error {
| UncompressedPubkey
| ExcessiveScriptSize
| UnrecognizedScript
| UnknownAddressType(_)
| NetworkValidation { .. } => None,
}
}
Expand All @@ -120,3 +113,15 @@ impl From<witness_version::TryFromError> for Error {
impl From<witness_program::Error> for Error {
fn from(e: witness_program::Error) -> Error { Error::WitnessProgram(e) }
}

/// Address type is either invalid or not supported in rust-bitcoin.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UnknownAddressTypeError(pub String);

impl fmt::Display for UnknownAddressTypeError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write_err!(f, "failed to parse {} as address type", self.0; self)
}
}

impl_std_error!(UnknownAddressTypeError);
8 changes: 4 additions & 4 deletions bitcoin/src/address/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ use crate::taproot::TapNodeHash;

/// Error code for the address module.
pub mod error;
pub use self::error::Error;
pub use self::error::{Error, UnknownAddressTypeError};

/// The different types of addresses.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
Expand Down Expand Up @@ -81,15 +81,15 @@ impl fmt::Display for AddressType {
}

impl FromStr for AddressType {
type Err = Error;
type Err = UnknownAddressTypeError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"p2pkh" => Ok(AddressType::P2pkh),
"p2sh" => Ok(AddressType::P2sh),
"p2wpkh" => Ok(AddressType::P2wpkh),
"p2wsh" => Ok(AddressType::P2wsh),
"p2tr" => Ok(AddressType::P2tr),
_ => Err(Error::UnknownAddressType(s.to_owned())),
_ => Err(UnknownAddressTypeError(s.to_owned())),
}
}
}
Expand Down Expand Up @@ -1491,7 +1491,7 @@ mod tests {
#[test]
fn invalid_address_parses_error() {
let got = AddressType::from_str("invalid");
let want = Err(Error::UnknownAddressType("invalid".to_string()));
let want = Err(UnknownAddressTypeError("invalid".to_string()));
assert_eq!(got, want);
}

Expand Down

0 comments on commit 0f536e8

Please sign in to comment.