Merge rust-bitcoin/rust-bitcoin#2508: Add `NetworkValidationError`

7e2a81d03b Remove unused address::Error type (Tobin C. Harding)
a92dc9c35c 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 7e2a81d03b
  apoelstra:
    ACK 7e2a81d03b

Tree-SHA512: 0a220dbec1457f35e3d95f32399f258e65c0477e8b4d14dbe2dfad0a44966ab0339d4c2d9828da318bf131db45cecb2447c0e32d5669a5f4c4739b90c83d3c0d
This commit is contained in:
Andrew Poelstra 2024-02-27 14:24:46 +00:00
commit 36aa627d83
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
2 changed files with 14 additions and 36 deletions

View File

@ -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)]

View File

@ -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.
@ -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 })
}
}