aum_core/
address.rs

1use crate::keypair::{PublicKey, SecretKey};
2use std::{
3    fmt::{Debug, Display},
4    hash::Hash,
5    str::FromStr,
6};
7
8/// A trait representing an address that can be derived from a secret or public key.
9pub trait Address:
10    'static + Clone + Debug + Display + FromStr + Hash + PartialEq + Eq + Send + Sized + Sync
11{
12    type SecretKey: SecretKey;
13    type PublicKey: PublicKey;
14    type Format: Format;
15
16    /// Creates an address from a secret key and a specific format.
17    fn from_secret_key(
18        secret_key: &Self::SecretKey,
19        format: &Self::Format,
20    ) -> Result<Self, AddressError>;
21
22    /// Creates an address from a public key and a specific format.
23    fn from_public_key(
24        public_key: &Self::PublicKey,
25        format: &Self::Format,
26    ) -> Result<Self, AddressError>;
27
28    /// Validates if a given string is a valid address.
29    fn is_valid(address: &str) -> bool {
30        Self::from_str(address).is_ok()
31    }
32}
33
34/// A trait representing the format of an address.
35pub trait Format:
36    'static + Clone + Debug + Display + Send + Sync + Eq + Ord + Sized + Hash
37{
38}
39
40/// An enumeration of possible errors that can occur when working with addresses.
41#[derive(Debug, thiserror::Error)]
42pub enum AddressError {
43    #[error("Invalid address format")]
44    InvalidFormat,
45
46    #[error("Failed to parse address")]
47    ParseError,
48
49    #[error("Unsupported address format")]
50    UnsupportedFormat,
51
52    #[error("Invalid public key")]
53    InvalidPublicKey,
54
55    #[error("Invalid secret key")]
56    InvalidSecretKey,
57}