Wrap the bech32 decoding error
We do not want `bech32` to appear in the public API of the `address` module in case `bech32` does not stabalize before the soon-to-be-created `address` crates does. We already had a go at removing it but forgot one error variant - wrap the variant in a new type with a private inner bech32 error field.
This commit is contained in:
parent
b11bd9a6b5
commit
9a7b1c232b
|
@ -166,7 +166,7 @@ impl std::error::Error for NetworkValidationError {}
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
pub enum Bech32Error {
|
pub enum Bech32Error {
|
||||||
/// Parse segwit Bech32 error.
|
/// Parse segwit Bech32 error.
|
||||||
ParseBech32(bech32::segwit::DecodeError),
|
ParseBech32(ParseBech32Error),
|
||||||
/// A witness version conversion/parsing error.
|
/// A witness version conversion/parsing error.
|
||||||
WitnessVersion(witness_version::TryFromError),
|
WitnessVersion(witness_version::TryFromError),
|
||||||
/// A witness program error.
|
/// A witness program error.
|
||||||
|
@ -204,10 +204,6 @@ impl std::error::Error for Bech32Error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<bech32::segwit::DecodeError> for Bech32Error {
|
|
||||||
fn from(e: bech32::segwit::DecodeError) -> Self { Self::ParseBech32(e) }
|
|
||||||
}
|
|
||||||
|
|
||||||
impl From<witness_version::TryFromError> for Bech32Error {
|
impl From<witness_version::TryFromError> for Bech32Error {
|
||||||
fn from(e: witness_version::TryFromError) -> Self { Self::WitnessVersion(e) }
|
fn from(e: witness_version::TryFromError) -> Self { Self::WitnessVersion(e) }
|
||||||
}
|
}
|
||||||
|
@ -220,6 +216,24 @@ impl From<UnknownHrpError> for Bech32Error {
|
||||||
fn from(e: UnknownHrpError) -> Self { Self::UnknownHrp(e) }
|
fn from(e: UnknownHrpError) -> Self { Self::UnknownHrp(e) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Bech32 parsing related error.
|
||||||
|
// This wrapper exists because we do not want to expose the `bech32` crate in our public API.
|
||||||
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
|
pub struct ParseBech32Error(pub(crate) bech32::segwit::DecodeError);
|
||||||
|
|
||||||
|
internals::impl_from_infallible!(ParseBech32Error);
|
||||||
|
|
||||||
|
impl fmt::Display for ParseBech32Error {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
write_err!(f, "bech32 parsing error"; self.0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
impl std::error::Error for ParseBech32Error {
|
||||||
|
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { Some(&self.0) }
|
||||||
|
}
|
||||||
|
|
||||||
/// Base58 related error.
|
/// Base58 related error.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
#[non_exhaustive]
|
#[non_exhaustive]
|
||||||
|
|
|
@ -61,7 +61,7 @@ use crate::taproot::TapNodeHash;
|
||||||
pub use self::error::{
|
pub use self::error::{
|
||||||
Base58Error, Bech32Error, FromScriptError, InvalidBase58PayloadLengthError,
|
Base58Error, Bech32Error, FromScriptError, InvalidBase58PayloadLengthError,
|
||||||
InvalidLegacyPrefixError, LegacyAddressTooLongError, NetworkValidationError,
|
InvalidLegacyPrefixError, LegacyAddressTooLongError, NetworkValidationError,
|
||||||
ParseError, UnknownAddressTypeError, UnknownHrpError
|
ParseError, UnknownAddressTypeError, UnknownHrpError, ParseBech32Error,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// The different types of addresses.
|
/// The different types of addresses.
|
||||||
|
@ -803,7 +803,8 @@ impl Address<NetworkUnchecked> {
|
||||||
|
|
||||||
/// Parse a bech32 Address string
|
/// Parse a bech32 Address string
|
||||||
pub fn from_bech32_str(s: &str) -> Result<Address<NetworkUnchecked>, Bech32Error> {
|
pub fn from_bech32_str(s: &str) -> Result<Address<NetworkUnchecked>, Bech32Error> {
|
||||||
let (hrp, witness_version, data) = bech32::segwit::decode(s)?;
|
let (hrp, witness_version, data) = bech32::segwit::decode(s)
|
||||||
|
.map_err(|e| Bech32Error::ParseBech32(ParseBech32Error(e)))?;
|
||||||
let version = WitnessVersion::try_from(witness_version.to_u8())?;
|
let version = WitnessVersion::try_from(witness_version.to_u8())?;
|
||||||
let program = WitnessProgram::new(version, &data)
|
let program = WitnessProgram::new(version, &data)
|
||||||
.expect("bech32 guarantees valid program length for witness");
|
.expect("bech32 guarantees valid program length for witness");
|
||||||
|
|
Loading…
Reference in New Issue