aum_core/
keypair.rs

1pub trait SecretKey: Send + Sync {
2    type PublicKey: PublicKey;
3
4    /// Generates a new secret key.
5    fn new() -> Self;
6
7    /// Converts the secret key to a byte vector.
8    fn to_bytes(&self) -> Vec<u8>;
9
10    /// Creates a secret key from a byte slice.
11    fn from_bytes(bytes: impl AsRef<[u8]>) -> Result<Self, KeyPairError>
12    where
13        Self: Sized;
14
15    /// Converts the secret key to a hexadecimal string.
16    fn to_hex(&self) -> String;
17
18    /// Creates a secret key from a hexadecimal string.
19    fn from_hex(hex: &str) -> Result<Self, KeyPairError>
20    where
21        Self: Sized;
22
23    /// Returns the corresponding public key.
24    fn pubkey(&self) -> Self::PublicKey;
25}
26
27pub trait PublicKey: Clone + Send + Sync {
28    type SecretKey: SecretKey;
29
30    /// Retrieves the public key from the secret key.
31    fn from_secret_key(secret_key: &Self::SecretKey) -> Self;
32
33    /// Converts the public key to a byte vector.
34    fn to_bytes(&self) -> Vec<u8>;
35
36    /// Creates a public key from a byte slice.
37    fn from_bytes(bytes: impl AsRef<[u8]>) -> Result<Self, KeyPairError>
38    where
39        Self: Sized;
40
41    /// Converts the public key to a hexadecimal string.
42    fn to_hex(&self) -> String;
43
44    /// Creates a public key from a hexadecimal string.
45    fn from_hex(hex: &str) -> Result<Self, KeyPairError>
46    where
47        Self: Sized;
48}
49
50#[derive(Debug, thiserror::Error)]
51pub enum KeyPairError {
52    #[error("Failed to generate key pair")]
53    FailedToGenerateKeyPair,
54
55    #[error("Invalid byte representation")]
56    InvalidBytes,
57
58    #[error("Invalid hexadecimal representation")]
59    InvalidHex,
60
61    #[error("Invalid public key")]
62    InvalidPublicKey,
63
64    #[error("Invalid secret key")]
65    InvalidSecretKey,
66
67    #[error(transparent)]
68    Custom(#[from] Box<dyn std::error::Error + Send + Sync>),
69}