Skip to main content

vouch_core/
error.rs

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