use core::fmt::Debug;
use ledger_mob_apdu::state::TxState;
use mc_crypto_ring_signature_signer::Error as SignerError;
use tokio::time::error::Elapsed;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("no device found")]
NoDevice,
#[error("Transport error {0}")]
Transport(#[from] ledger_lib::Error),
#[error("could not create HidApi instance")]
HidInit,
#[cfg(feature = "transport_hid")]
#[error("Transport error {0}")]
Hid(#[from] hidapi::HidError),
#[error("IO error {0}")]
Io(#[from] std::io::Error),
#[error("Invalid transaction state (actual: {0}, expected: {1})")]
InvalidState(TxState, TxState),
#[error("Unexpected APDU response")]
UnexpectedResponse,
#[error("Mismatch in rolling transaction digest")]
DigestMismatch,
#[error("Ring signing failed: {0}")]
Ring(SignerError),
#[error("Timeout waiting for user interaction")]
UserTimeout,
#[error("Timeout waiting for device response")]
RequestTimeout,
#[error("Engine operation failed: {0}")]
Engine(u16),
#[error("Operation rejected by user")]
UserDenied,
#[error("Invalid key object")]
InvalidKey,
#[error("Invalid length")]
InvalidLength,
#[error("UTF8 encoding error")]
Utf8,
#[error("APDU error")]
Apdu(#[from] ledger_proto::ApduError),
#[error("Ring CT error: {0}")]
RingCt(mc_transaction_core::ring_ct::Error),
#[error("Ring signer error: {0}")]
RingSigner(mc_crypto_ring_signature::Error),
#[error("Unknown error")]
Unknown,
}
impl From<Error> for SignerError {
fn from(value: Error) -> Self {
match value {
Error::Ring(r) => r,
_ => SignerError::Unknown,
}
}
}
impl From<Elapsed> for Error {
fn from(_: Elapsed) -> Self {
Error::RequestTimeout
}
}
impl From<mc_crypto_ring_signature::Error> for Error {
fn from(value: mc_crypto_ring_signature::Error) -> Self {
Error::RingSigner(value)
}
}
impl From<mc_transaction_core::ring_ct::Error> for Error {
fn from(value: mc_transaction_core::ring_ct::Error) -> Self {
Error::RingCt(value)
}
}