use alloy_primitives::{hex, ChainId};
use k256::ecdsa;
use std::fmt;
use thiserror::Error;
pub type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Debug, Error)]
pub enum Error {
#[error("operation `{0}` is not supported by the signer")]
UnsupportedOperation(UnsupportedSignerOperation),
#[error(
"transaction-provided chain ID ({tx}) does not match the signer's chain ID ({signer})"
)]
TransactionChainIdMismatch {
signer: ChainId,
tx: ChainId,
},
#[error(transparent)]
#[cfg(feature = "eip712")]
DynAbiError(#[from] alloy_dyn_abi::Error),
#[error(transparent)]
Ecdsa(#[from] ecdsa::Error),
#[error(transparent)]
HexError(#[from] hex::FromHexError),
#[error(transparent)]
SignatureError(#[from] alloy_primitives::SignatureError),
#[error(transparent)]
Other(#[from] Box<dyn std::error::Error + Send + Sync + 'static>),
}
impl Error {
#[cold]
pub fn other(error: impl Into<Box<dyn std::error::Error + Send + Sync + 'static>>) -> Self {
Self::Other(error.into())
}
#[inline]
pub const fn is_unsupported(&self) -> bool {
matches!(self, Self::UnsupportedOperation(_))
}
#[inline]
pub const fn unsupported(&self) -> Option<UnsupportedSignerOperation> {
match self {
Self::UnsupportedOperation(op) => Some(*op),
_ => None,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum UnsupportedSignerOperation {
SignHash,
SignMessage,
SignTransaction,
SignTypedData,
}
impl fmt::Display for UnsupportedSignerOperation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.as_str().fmt(f)
}
}
impl UnsupportedSignerOperation {
#[inline]
pub const fn as_str(&self) -> &'static str {
match self {
Self::SignHash => "sign_hash",
Self::SignMessage => "sign_message",
Self::SignTransaction => "sign_transaction",
Self::SignTypedData => "sign_typed_data",
}
}
}