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: ACK7e2a81d03b
apoelstra: ACK7e2a81d03b
Tree-SHA512: 0a220dbec1457f35e3d95f32399f258e65c0477e8b4d14dbe2dfad0a44966ab0339d4c2d9828da318bf131db45cecb2447c0e32d5669a5f4c4739b90c83d3c0d
This commit is contained in:
commit
36aa627d83
|
@ -9,47 +9,25 @@ use crate::blockdata::script::{witness_program, witness_version};
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::Network;
|
use crate::Network;
|
||||||
|
|
||||||
/// Address error.
|
/// Address's network differs from required one.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
#[non_exhaustive]
|
pub struct NetworkValidationError {
|
||||||
pub enum Error {
|
/// Network that was required.
|
||||||
/// Address's network differs from required one.
|
pub(crate) required: Network,
|
||||||
NetworkValidation {
|
/// The address itself.
|
||||||
/// Network that was required.
|
pub(crate) address: Address<NetworkUnchecked>,
|
||||||
required: Network,
|
|
||||||
/// The address itself
|
|
||||||
address: Address<NetworkUnchecked>,
|
|
||||||
},
|
|
||||||
/// Unknown hrp for current bitcoin networks (in bech32 address).
|
|
||||||
UnknownHrp(UnknownHrpError),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for Error {
|
impl fmt::Display for NetworkValidationError {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
use Error::*;
|
write!(f, "address ")?;
|
||||||
|
fmt::Display::fmt(&self.address.0, f)?;
|
||||||
match *self {
|
write!(f, " is not valid on {}", self.required)
|
||||||
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),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
impl std::error::Error for Error {
|
impl std::error::Error for NetworkValidationError {}
|
||||||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
|
||||||
use Error::*;
|
|
||||||
|
|
||||||
match *self {
|
|
||||||
UnknownHrp(ref e) => Some(e),
|
|
||||||
NetworkValidation { .. } => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Error while generating address from script.
|
/// Error while generating address from script.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
|
|
@ -54,7 +54,7 @@ use self::error::P2shError;
|
||||||
#[rustfmt::skip] // Keep public re-exports separate.
|
#[rustfmt::skip] // Keep public re-exports separate.
|
||||||
#[doc(inline)]
|
#[doc(inline)]
|
||||||
pub use self::{
|
pub use self::{
|
||||||
error::{Error, ParseError, UnknownAddressTypeError, UnknownHrpError, FromScriptError},
|
error::{NetworkValidationError, ParseError, UnknownAddressTypeError, UnknownHrpError, FromScriptError, },
|
||||||
};
|
};
|
||||||
|
|
||||||
/// The different types of addresses.
|
/// The different types of addresses.
|
||||||
|
@ -666,11 +666,11 @@ impl Address<NetworkUnchecked> {
|
||||||
/// For details about this mechanism, see section [*Parsing addresses*](Address#parsing-addresses)
|
/// For details about this mechanism, see section [*Parsing addresses*](Address#parsing-addresses)
|
||||||
/// on [`Address`].
|
/// on [`Address`].
|
||||||
#[inline]
|
#[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) {
|
if self.is_valid_for_network(required) {
|
||||||
Ok(self.assume_checked())
|
Ok(self.assume_checked())
|
||||||
} else {
|
} else {
|
||||||
Err(Error::NetworkValidation { required, address: self })
|
Err(NetworkValidationError { required, address: self })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue