newton-enclave 0.4.15

newton prover enclave compute
use thiserror::Error;

use crate::protocol::{EnclaveErrorCode, EnclaveWireError};

/// enclave compute error.
#[derive(Debug, Error)]
pub enum EnclaveError {
    /// invalid request shape or value.
    #[error("invalid request: {0}")]
    InvalidRequest(String),
    /// required enclave input was missing.
    #[error("missing input: {0}")]
    MissingInput(String),
    /// envelope decryption failed.
    #[error("decrypt failed: {0}")]
    DecryptFailed(String),
    /// threshold decryption failed.
    #[error("threshold failed: {0}")]
    ThresholdFailed(String),
    /// policy evaluation failed.
    #[error("policy eval failed: {0}")]
    PolicyEvalFailed(String),
    /// KMS or HKDF key derivation failed.
    #[error("key derivation failed: {0}")]
    KeyDerivation(String),
}

impl EnclaveError {
    /// convert to wire-safe error.
    pub fn to_wire(&self) -> EnclaveWireError {
        let code = match self {
            Self::InvalidRequest(_) => EnclaveErrorCode::InvalidRequest,
            Self::MissingInput(_) => EnclaveErrorCode::MissingInput,
            Self::DecryptFailed(_) => EnclaveErrorCode::DecryptFailed,
            Self::ThresholdFailed(_) => EnclaveErrorCode::ThresholdFailed,
            Self::PolicyEvalFailed(_) => EnclaveErrorCode::PolicyEvalFailed,
            Self::KeyDerivation(_) => EnclaveErrorCode::KeyDerivation,
        };
        EnclaveWireError {
            code,
            message: self.to_string(),
            retryable: false,
        }
    }
}