1use crate::keypair::{PublicKey, SecretKey};
2use std::{
3 fmt::{Debug, Display},
4 hash::Hash,
5 str::FromStr,
6};
7
8pub 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 fn from_secret_key(
18 secret_key: &Self::SecretKey,
19 format: &Self::Format,
20 ) -> Result<Self, AddressError>;
21
22 fn from_public_key(
24 public_key: &Self::PublicKey,
25 format: &Self::Format,
26 ) -> Result<Self, AddressError>;
27
28 fn is_valid(address: &str) -> bool {
30 Self::from_str(address).is_ok()
31 }
32}
33
34pub trait Format:
36 'static + Clone + Debug + Display + Send + Sync + Eq + Ord + Sized + Hash
37{
38}
39
40#[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}