use alloc::{boxed::Box, string::String};
use bdk_wallet::signer::SignerError;
use bitcoin::{
OutPoint, Txid,
consensus::encode::Error as ConsensusError,
io::Error as IoError,
psbt::{Error as PsbtError, ExtractTxError},
};
use core::fmt;
#[derive(Debug)]
pub enum Error {
InvalidFormat(String),
InvalidMessage,
InvalidPublicKey(String),
SighashError,
InvalidSignature(String),
NotSegwitAddress,
UnsupportedSegwitVersion(String),
InvalidSighashType,
InvalidWitness(String),
SignerError(SignerError),
IoError(IoError),
ExtractTxError(Box<ExtractTxError>),
PsbtError(PsbtError),
ConsensusError(ConsensusError),
TransactionNotFound(Txid),
UtxoNotFound(OutPoint),
UnsupportedScriptType(String),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::InvalidFormat(e) => write!(f, "Invalid format: {}", e),
Self::InvalidMessage => write!(f, "Message hash is not secure"),
Self::InvalidPublicKey(e) => write!(f, "Invalid public key {}", e),
Self::SighashError => write!(f, "Unable to compute signature hash"),
Self::InvalidSignature(e) => write!(f, "Invalid Signature - {}", e),
Self::NotSegwitAddress => write!(f, "Not a Segwit address"),
Self::UnsupportedSegwitVersion(e) => write!(f, "Only Segwit {} is supported", e),
Self::InvalidSighashType => write!(f, "Sighash type is invalid"),
Self::InvalidWitness(e) => write!(f, "Invalid Witness - {}", e),
Self::SignerError(err) => write!(f, "Signer error: {}", err),
Self::IoError(err) => write!(f, "Bitcoin IO Error: {}", err),
Self::ExtractTxError(err) => write!(f, "Extract TX Error: {}", err),
Self::PsbtError(err) => write!(f, "Psbt Error: {}", err),
Self::ConsensusError(err) => write!(f, "Consensus Error: {}", err),
Self::TransactionNotFound(err) => write!(f, "Transaction not found: {}", err),
Self::UtxoNotFound(err) => write!(f, "UTXO not found: {}", err),
Self::UnsupportedScriptType(err) => write!(f, "Unsupported script type: {}", err),
}
}
}
impl From<SignerError> for Error {
fn from(err: SignerError) -> Self {
Error::SignerError(err)
}
}
impl From<IoError> for Error {
fn from(err: IoError) -> Self {
Error::IoError(err)
}
}
impl From<ExtractTxError> for Error {
fn from(err: ExtractTxError) -> Self {
Error::ExtractTxError(Box::new(err))
}
}
impl From<PsbtError> for Error {
fn from(err: PsbtError) -> Self {
Error::PsbtError(err)
}
}
impl From<ConsensusError> for Error {
fn from(err: ConsensusError) -> Self {
Error::ConsensusError(err)
}
}
#[cfg(feature = "std")]
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::SignerError(e) => Some(e),
Self::IoError(e) => Some(e),
Self::ExtractTxError(e) => Some(e.as_ref()),
Self::PsbtError(e) => Some(e),
Self::ConsensusError(e) => Some(e),
_ => None,
}
}
}