mpc_wallet_core/
error.rs

1//! Error types for MPC wallet operations
2
3use thiserror::Error;
4
5/// Result type alias for MPC wallet operations
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Errors that can occur during MPC wallet operations
9#[derive(Debug, Error)]
10pub enum Error {
11    // ============ Configuration Errors ============
12    /// Invalid party configuration
13    #[error("Invalid configuration: {0}")]
14    InvalidConfig(String),
15
16    /// Invalid party ID
17    #[error("Invalid party ID: {0}")]
18    InvalidPartyId(usize),
19
20    /// Invalid party role
21    #[error("Invalid party role: {0}")]
22    InvalidPartyRole(String),
23
24    // ============ Threshold Errors ============
25    /// Threshold requirements not met
26    #[error("Threshold not met: required {required}, got {actual}")]
27    ThresholdNotMet { required: usize, actual: usize },
28
29    /// Invalid signing party combination
30    #[error("Invalid signing party combination: {0}")]
31    InvalidSigningParties(String),
32
33    // ============ Policy Errors ============
34    /// Policy violation - transaction rejected
35    #[error("Policy violation: {0}")]
36    PolicyViolation(String),
37
38    /// Spending limit exceeded
39    #[error("Spending limit exceeded: {limit} {currency} (attempted: {attempted})")]
40    SpendingLimitExceeded {
41        limit: String,
42        attempted: String,
43        currency: String,
44    },
45
46    /// Address not in whitelist
47    #[error("Address not whitelisted: {0}")]
48    AddressNotWhitelisted(String),
49
50    /// Address is blacklisted
51    #[error("Address is blacklisted: {0}")]
52    AddressBlacklisted(String),
53
54    /// Transaction outside allowed time window
55    #[error("Transaction outside allowed time window: {0}")]
56    TimeWindowViolation(String),
57
58    /// Contract interaction not allowed
59    #[error("Contract interaction not allowed: {0}")]
60    ContractNotAllowed(String),
61
62    // ============ Cryptographic Errors ============
63    /// Message verification failed
64    #[error("Message verification failed: {0}")]
65    VerificationFailed(String),
66
67    /// Cryptographic operation failed
68    #[error("Cryptographic error: {0}")]
69    Crypto(String),
70
71    /// Invalid signature
72    #[error("Invalid signature: {0}")]
73    InvalidSignature(String),
74
75    // ============ Storage Errors ============
76    /// Key share not found
77    #[error("Key share not found: {0}")]
78    KeyShareNotFound(String),
79
80    /// Storage operation failed
81    #[error("Storage error: {0}")]
82    Storage(String),
83
84    /// Encryption/decryption failed
85    #[error("Encryption error: {0}")]
86    Encryption(String),
87
88    // ============ Serialization Errors ============
89    /// Serialization error
90    #[error("Serialization error: {0}")]
91    Serialization(String),
92
93    /// Deserialization error
94    #[error("Deserialization error: {0}")]
95    Deserialization(String),
96
97    // ============ Network/Protocol Errors ============
98    /// Network/relay error
99    #[error("Relay error: {0}")]
100    Relay(String),
101
102    /// Timeout waiting for message
103    #[error("Timeout waiting for {0}")]
104    Timeout(String),
105
106    /// Session not found
107    #[error("Session not found: {0}")]
108    SessionNotFound(String),
109
110    /// Session expired
111    #[error("Session expired: {0}")]
112    SessionExpired(String),
113
114    // ============ Key Derivation Errors ============
115    /// Key derivation error
116    #[error("Key derivation error: {0}")]
117    Derivation(String),
118
119    /// Hardened derivation not supported
120    #[error("Hardened derivation not supported in threshold setting")]
121    HardenedDerivationNotSupported,
122
123    // ============ Chain Errors ============
124    /// Unsupported chain
125    #[error("Unsupported chain: {0}")]
126    UnsupportedChain(String),
127
128    /// Chain operation failed
129    #[error("Chain error: {0}")]
130    ChainError(String),
131
132    // ============ Internal Errors ============
133    /// Internal error
134    #[error("Internal error: {0}")]
135    Internal(String),
136
137    /// IO error
138    #[error("IO error: {0}")]
139    Io(#[from] std::io::Error),
140}
141
142impl From<serde_json::Error> for Error {
143    fn from(e: serde_json::Error) -> Self {
144        Error::Serialization(e.to_string())
145    }
146}
147
148impl From<hex::FromHexError> for Error {
149    fn from(e: hex::FromHexError) -> Self {
150        Error::Deserialization(e.to_string())
151    }
152}
153
154#[cfg(test)]
155mod tests {
156    use super::*;
157
158    #[test]
159    fn test_error_display() {
160        let err = Error::SpendingLimitExceeded {
161            limit: "1.0".to_string(),
162            attempted: "2.0".to_string(),
163            currency: "ETH".to_string(),
164        };
165        assert!(err.to_string().contains("1.0"));
166        assert!(err.to_string().contains("2.0"));
167        assert!(err.to_string().contains("ETH"));
168    }
169
170    #[test]
171    fn test_policy_violation() {
172        let err = Error::PolicyViolation("daily limit exceeded".to_string());
173        assert!(err.to_string().contains("daily limit"));
174    }
175}