Skip to content

Commit

Permalink
Merge #2508: Add NetworkValidationError
Browse files Browse the repository at this point in the history
7e2a81d Remove unused address::Error type (Tobin C. Harding)
a92dc9c Add NetworkValidationError (Tobin C. Harding)

Pull request description:

  Replaces #2502 because there is going  to be way too much arguing on this to bother a newer contributor with.

  This PR takes into consideration #2507 but does not improve the issue, it also does not make it worse. I propose to do this and then consider #2507 since this is a step forwards IMO.

  Remove the `address::Error` because its not good. Add a `NetworkValidationError` and return it from `require_network` - leaving the door open for resolving Kix's issue in 2507.

ACKs for top commit:
  Kixunil:
    ACK 7e2a81d
  apoelstra:
    ACK 7e2a81d

Tree-SHA512: 0a220dbec1457f35e3d95f32399f258e65c0477e8b4d14dbe2dfad0a44966ab0339d4c2d9828da318bf131db45cecb2447c0e32d5669a5f4c4739b90c83d3c0d
  • Loading branch information
apoelstra committed Feb 27, 2024
2 parents 42e8f53 + 7e2a81d commit 36aa627
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 36 deletions.
44 changes: 11 additions & 33 deletions bitcoin/src/address/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,25 @@ use crate::blockdata::script::{witness_program, witness_version};
use crate::prelude::*;
use crate::Network;

/// Address error.
/// Address's network differs from required one.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
/// Address's network differs from required one.
NetworkValidation {
/// Network that was required.
required: Network,
/// The address itself
address: Address<NetworkUnchecked>,
},
/// Unknown hrp for current bitcoin networks (in bech32 address).
UnknownHrp(UnknownHrpError),
pub struct NetworkValidationError {
/// Network that was required.
pub(crate) required: Network,
/// The address itself.
pub(crate) address: Address<NetworkUnchecked>,
}

impl fmt::Display for Error {
impl fmt::Display for NetworkValidationError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use Error::*;

match *self {
NetworkValidation { required, ref address } => {
write!(f, "address ")?;
fmt::Display::fmt(&address.0, f)?;
write!(f, " is not valid on {}", required)
}
Error::UnknownHrp(ref e) => write_err!(f, "unknown hrp"; e),
}
write!(f, "address ")?;
fmt::Display::fmt(&self.address.0, f)?;
write!(f, " is not valid on {}", self.required)
}
}

#[cfg(feature = "std")]
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
use Error::*;

match *self {
UnknownHrp(ref e) => Some(e),
NetworkValidation { .. } => None,
}
}
}
impl std::error::Error for NetworkValidationError {}

/// Error while generating address from script.
#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down
6 changes: 3 additions & 3 deletions bitcoin/src/address/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ use self::error::P2shError;
#[rustfmt::skip] // Keep public re-exports separate.
#[doc(inline)]
pub use self::{
error::{Error, ParseError, UnknownAddressTypeError, UnknownHrpError, FromScriptError},
error::{NetworkValidationError, ParseError, UnknownAddressTypeError, UnknownHrpError, FromScriptError, },
};

/// The different types of addresses.
Expand Down Expand Up @@ -666,11 +666,11 @@ impl Address<NetworkUnchecked> {
/// For details about this mechanism, see section [*Parsing addresses*](Address#parsing-addresses)
/// on [`Address`].
#[inline]
pub fn require_network(self, required: Network) -> Result<Address, Error> {
pub fn require_network(self, required: Network) -> Result<Address, NetworkValidationError> {
if self.is_valid_for_network(required) {
Ok(self.assume_checked())
} else {
Err(Error::NetworkValidation { required, address: self })
Err(NetworkValidationError { required, address: self })
}
}

Expand Down

0 comments on commit 36aa627

Please sign in to comment.