Skip to main content

vouch_core/
error.rs

1//! Error type for the Vouch core.
2
3use std::fmt;
4
5/// Errors returned by the protocol core. Kept small and stable so the WASM and
6/// UniFFI/C-FFI wrappers can surface them uniformly across languages.
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub enum CoreError {
9    /// A key had the wrong length for its algorithm.
10    InvalidKeyLength { expected: usize, got: usize },
11    /// A multikey string was malformed (bad prefix, bad base58, too short).
12    InvalidMultikey(String),
13    /// The multicodec prefix did not match a supported algorithm.
14    UnknownMulticodec(String),
15    /// A DID string was malformed or used an unsupported method.
16    InvalidDid(String),
17    /// A signature length was wrong.
18    InvalidSignatureLength { expected: usize, got: usize },
19    /// A cryptographic operation failed (keygen RNG, parse, etc.).
20    Crypto(String),
21    /// JSON was malformed or had an unexpected shape.
22    Json(String),
23    /// A FROST threshold-signing operation failed (bad share, wrong
24    /// participant count, mismatched commitments).
25    Threshold(String),
26}
27
28impl fmt::Display for CoreError {
29    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30        match self {
31            CoreError::InvalidKeyLength { expected, got } => {
32                write!(
33                    f,
34                    "invalid key length: expected {expected} bytes, got {got}"
35                )
36            }
37            CoreError::InvalidMultikey(m) => write!(f, "invalid multikey: {m}"),
38            CoreError::UnknownMulticodec(m) => write!(f, "unknown multicodec prefix: {m}"),
39            CoreError::InvalidDid(m) => write!(f, "invalid did: {m}"),
40            CoreError::InvalidSignatureLength { expected, got } => {
41                write!(
42                    f,
43                    "invalid signature length: expected {expected} bytes, got {got}"
44                )
45            }
46            CoreError::Crypto(m) => write!(f, "crypto error: {m}"),
47            CoreError::Json(m) => write!(f, "json error: {m}"),
48            CoreError::Threshold(m) => write!(f, "threshold signing error: {m}"),
49        }
50    }
51}
52
53impl std::error::Error for CoreError {}
54
55/// Convenience alias for results in the core.
56pub type Result<T> = std::result::Result<T, CoreError>;