1pub trait SecretKey: Send + Sync {
2 type PublicKey: PublicKey;
3
4 fn new() -> Self;
6
7 fn to_bytes(&self) -> Vec<u8>;
9
10 fn from_bytes(bytes: impl AsRef<[u8]>) -> Result<Self, KeyPairError>
12 where
13 Self: Sized;
14
15 fn to_hex(&self) -> String;
17
18 fn from_hex(hex: &str) -> Result<Self, KeyPairError>
20 where
21 Self: Sized;
22
23 fn pubkey(&self) -> Self::PublicKey;
25}
26
27pub trait PublicKey: Clone + Send + Sync {
28 type SecretKey: SecretKey;
29
30 fn from_secret_key(secret_key: &Self::SecretKey) -> Self;
32
33 fn to_bytes(&self) -> Vec<u8>;
35
36 fn from_bytes(bytes: impl AsRef<[u8]>) -> Result<Self, KeyPairError>
38 where
39 Self: Sized;
40
41 fn to_hex(&self) -> String;
43
44 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}