2023-08-04 00:07:59 +00:00
|
|
|
use core::fmt;
|
|
|
|
|
|
|
|
use internals::write_err;
|
|
|
|
|
|
|
|
use crate::address::{Address, NetworkUnchecked};
|
|
|
|
use crate::blockdata::script::{witness_program, witness_version};
|
2023-08-04 00:29:27 +00:00
|
|
|
use crate::error::impl_std_error;
|
2023-08-04 00:07:59 +00:00
|
|
|
use crate::prelude::String;
|
|
|
|
use crate::{base58, Network};
|
|
|
|
|
|
|
|
/// Address error.
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
#[non_exhaustive]
|
|
|
|
pub enum Error {
|
|
|
|
/// A witness version construction error.
|
|
|
|
WitnessVersion(witness_version::TryFromError),
|
|
|
|
/// A witness program error.
|
|
|
|
WitnessProgram(witness_program::Error),
|
|
|
|
/// An uncompressed pubkey was used where it is not allowed.
|
|
|
|
UncompressedPubkey,
|
|
|
|
/// Address size more than 520 bytes is not allowed.
|
|
|
|
ExcessiveScriptSize,
|
|
|
|
/// Script is not a p2pkh, p2sh or witness program.
|
|
|
|
UnrecognizedScript,
|
|
|
|
/// Address's network differs from required one.
|
|
|
|
NetworkValidation {
|
|
|
|
/// Network that was required.
|
|
|
|
required: Network,
|
|
|
|
/// Network on which the address was found to be valid.
|
|
|
|
found: Network,
|
|
|
|
/// The address itself
|
|
|
|
address: Address<NetworkUnchecked>,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for Error {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2023-08-04 00:12:45 +00:00
|
|
|
use Error::*;
|
|
|
|
|
2023-08-04 00:07:59 +00:00
|
|
|
match *self {
|
2023-08-04 00:12:45 +00:00
|
|
|
WitnessVersion(ref e) => write_err!(f, "witness version construction error"; e),
|
|
|
|
WitnessProgram(ref e) => write_err!(f, "witness program error"; e),
|
|
|
|
UncompressedPubkey =>
|
2023-08-04 00:07:59 +00:00
|
|
|
write!(f, "an uncompressed pubkey was used where it is not allowed"),
|
2023-08-04 00:12:45 +00:00
|
|
|
ExcessiveScriptSize => write!(f, "script size exceed 520 bytes"),
|
|
|
|
UnrecognizedScript => write!(f, "script is not a p2pkh, p2sh or witness program"),
|
|
|
|
NetworkValidation { required, found, ref address } => {
|
2023-08-04 00:07:59 +00:00
|
|
|
write!(f, "address ")?;
|
|
|
|
address.fmt_internal(f)?; // Using fmt_internal in order to remove the "Address<NetworkUnchecked>(..)" wrapper
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
" belongs to network {} which is different from required {}",
|
|
|
|
found, required
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "std")]
|
|
|
|
impl std::error::Error for Error {
|
|
|
|
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
2023-08-04 00:11:26 +00:00
|
|
|
use Error::*;
|
2023-08-04 00:07:59 +00:00
|
|
|
|
|
|
|
match self {
|
|
|
|
WitnessVersion(e) => Some(e),
|
|
|
|
WitnessProgram(e) => Some(e),
|
2023-08-04 00:36:48 +00:00
|
|
|
UncompressedPubkey
|
2023-08-04 00:07:59 +00:00
|
|
|
| ExcessiveScriptSize
|
|
|
|
| UnrecognizedScript
|
|
|
|
| NetworkValidation { .. } => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<witness_version::TryFromError> for Error {
|
|
|
|
fn from(e: witness_version::TryFromError) -> Error { Error::WitnessVersion(e) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<witness_program::Error> for Error {
|
|
|
|
fn from(e: witness_program::Error) -> Error { Error::WitnessProgram(e) }
|
|
|
|
}
|
2023-08-04 00:29:27 +00:00
|
|
|
|
|
|
|
/// 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);
|
2023-08-04 00:36:48 +00:00
|
|
|
|
|
|
|
/// Address parsing error.
|
|
|
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
|
|
|
#[non_exhaustive]
|
|
|
|
pub enum ParseError {
|
|
|
|
/// Base58 error.
|
|
|
|
Base58(base58::Error),
|
|
|
|
/// Bech32 error.
|
|
|
|
Bech32(bech32::Error),
|
|
|
|
/// The bech32 payload was empty.
|
|
|
|
EmptyBech32Payload,
|
|
|
|
/// The wrong checksum algorithm was used. See BIP-0350.
|
|
|
|
InvalidBech32Variant {
|
|
|
|
/// Bech32 variant that is required by the used Witness version.
|
|
|
|
expected: bech32::Variant,
|
|
|
|
/// The actual Bech32 variant encoded in the address representation.
|
|
|
|
found: bech32::Variant,
|
|
|
|
},
|
|
|
|
/// A witness version conversion/parsing error.
|
|
|
|
WitnessVersion(witness_version::TryFromError),
|
|
|
|
/// A witness program error.
|
|
|
|
WitnessProgram(witness_program::Error),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for ParseError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
use ParseError::*;
|
|
|
|
|
|
|
|
match *self {
|
|
|
|
Base58(ref e) => write_err!(f, "base58 error"; e),
|
|
|
|
Bech32(ref e) => write_err!(f, "bech32 error"; e),
|
|
|
|
EmptyBech32Payload => write!(f, "the bech32 payload was empty"),
|
|
|
|
InvalidBech32Variant { expected, found } => write!(
|
|
|
|
f,
|
|
|
|
"invalid bech32 checksum variant found {:?} when {:?} was expected",
|
|
|
|
found, expected
|
|
|
|
),
|
|
|
|
WitnessVersion(ref e) => write_err!(f, "witness version conversion/parsing error"; e),
|
|
|
|
WitnessProgram(ref e) => write_err!(f, "witness program error"; e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "std")]
|
|
|
|
impl std::error::Error for ParseError {
|
|
|
|
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
|
|
|
use ParseError::*;
|
|
|
|
|
|
|
|
match *self {
|
|
|
|
Base58(ref e) => Some(e),
|
|
|
|
Bech32(ref e) => Some(e),
|
|
|
|
WitnessVersion(ref e) => Some(e),
|
|
|
|
WitnessProgram(ref e) => Some(e),
|
|
|
|
EmptyBech32Payload | InvalidBech32Variant { .. } => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<base58::Error> for ParseError {
|
|
|
|
fn from(e: base58::Error) -> Self { Self::Base58(e) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<bech32::Error> for ParseError {
|
|
|
|
fn from(e: bech32::Error) -> Self { Self::Bech32(e) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<witness_version::TryFromError> for ParseError {
|
|
|
|
fn from(e: witness_version::TryFromError) -> Self { Self::WitnessVersion(e) }
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<witness_program::Error> for ParseError {
|
|
|
|
fn from(e: witness_program::Error) -> Self { Self::WitnessProgram(e) }
|
|
|
|
}
|