1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright (c) 2022-2023 The MobileCoin Foundation

use core::fmt::Debug;

use ledger_mob_apdu::state::TxState;
use mc_crypto_ring_signature_signer::Error as SignerError;
use tokio::time::error::Elapsed;

/// Ledger MobileCoin API Error Type
#[derive(Debug, thiserror::Error)]
pub enum Error {
    /// No device found
    #[error("no device found")]
    NoDevice,

    /// Ledger transport error
    #[error("Transport error {0}")]
    Transport(#[from] ledger_lib::Error),

    /// HID Init Error
    #[error("could not create HidApi instance")]
    HidInit,

    /// Ledger HID Error
    #[cfg(feature = "transport_hid")]
    #[error("Transport error {0}")]
    Hid(#[from] hidapi::HidError),

    /// Ledger IO Error
    #[error("IO error {0}")]
    Io(#[from] std::io::Error),

    /// Invalid transaction state
    #[error("Invalid transaction state (actual: {0}, expected: {1})")]
    InvalidState(TxState, TxState),

    /// Unexpected APDU response
    #[error("Unexpected APDU response")]
    UnexpectedResponse,

    /// Mismatch in rolling transaction digest
    #[error("Mismatch in rolling transaction digest")]
    DigestMismatch,

    /// Error signing ring
    #[error("Ring signing failed: {0}")]
    Ring(SignerError),

    /// Timeout waiting for user
    #[error("Timeout waiting for user interaction")]
    UserTimeout,

    /// Request timeout
    #[error("Timeout waiting for device response")]
    RequestTimeout,

    /// Transaction engine error
    #[error("Engine operation failed: {0}")]
    Engine(u16),

    /// User denied operation
    #[error("Operation rejected by user")]
    UserDenied,

    /// Invalid key in response
    #[error("Invalid key object")]
    InvalidKey,

    /// Invalid length
    #[error("Invalid length")]
    InvalidLength,

    /// UTF8 encoding error
    #[error("UTF8 encoding error")]
    Utf8,

    /// APDU error
    #[error("APDU error")]
    Apdu(#[from] ledger_proto::ApduError),

    /// Ring CT error
    #[error("Ring CT error: {0}")]
    RingCt(mc_transaction_core::ring_ct::Error),

    /// Ring signer error
    #[error("Ring signer error: {0}")]
    RingSigner(mc_crypto_ring_signature::Error),

    /// Unknown (TEMPORARY)
    /// TODO: remove once ledger_transport_tcp is updated / fixed
    #[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)
    }
}