Skip to main content

Module error

Module error 

Source
Expand description

Error types for cryptographic operations

This module defines all error types that can occur during cryptographic operations in the identity crate.

§Error Handling Examples

§Handling Signature Verification Errors

use ave_identity::keys::{KeyPair, KeyPairAlgorithm};
use ave_identity::error::CryptoError;

let keypair = KeyPair::generate(KeyPairAlgorithm::Ed25519).unwrap();
let message = b"Hello, World!";
let signature = keypair.sign(message).unwrap();
let public_key = keypair.public_key();

// Verify signature
match public_key.verify(message, &signature) {
    Ok(()) => println!("Signature is valid"),
    Err(CryptoError::SignatureVerificationFailed) => {
        eprintln!("Invalid signature or tampered data");
    }
    Err(CryptoError::InvalidSignatureFormat(msg)) => {
        eprintln!("Malformed signature: {}", msg);
    }
    Err(e) => eprintln!("Unexpected error: {}", e),
}

§Handling Key Parsing Errors

use ave_identity::keys::PublicKey;
use ave_identity::error::CryptoError;

let key_str = "EInvalidData";

match key_str.parse::<PublicKey>() {
    Ok(key) => println!("Successfully parsed public key"),
    Err(CryptoError::Base64DecodeError(msg)) => {
        eprintln!("Invalid encoding: {}", msg);
    }
    Err(CryptoError::UnknownAlgorithm(id)) => {
        eprintln!("Unknown algorithm identifier: {}", id);
    }
    Err(CryptoError::InvalidPublicKey(msg)) => {
        eprintln!("Invalid public key: {}", msg);
    }
    Err(e) => eprintln!("Unexpected error: {}", e),
}

§Handling Hash Parsing Errors

use ave_identity::hash::DigestIdentifier;
use ave_identity::error::CryptoError;

let hash_str = "BInvalidHashData";

match hash_str.parse::<DigestIdentifier>() {
    Ok(hash) => println!("Successfully parsed hash"),
    Err(CryptoError::Base64DecodeError(msg)) => {
        eprintln!("Invalid encoding: {}", msg);
    }
    Err(CryptoError::UnknownAlgorithm(id)) => {
        eprintln!("Unknown hash algorithm: {}", id);
    }
    Err(CryptoError::InvalidHashFormat(msg)) => {
        eprintln!("Invalid hash format: {}", msg);
    }
    Err(e) => eprintln!("Unexpected error: {}", e),
}

§Handling Serialization Errors

use ave_identity::{hash_borsh, BLAKE3_HASHER};
use ave_identity::error::CryptoError;
use borsh::BorshSerialize;

#[derive(BorshSerialize)]
struct MyData {
    value: u64,
}

let data = MyData { value: 42 };

match hash_borsh(&BLAKE3_HASHER, &data) {
    Ok(hash) => println!("Hash: {}", hash),
    Err(CryptoError::SerializationError(msg)) => {
        eprintln!("Failed to serialize data: {}", msg);
    }
    Err(e) => eprintln!("Unexpected error: {}", e),
}

Enums§

CryptoError
Errors that can occur during cryptographic operations