1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
use crate::format::Format;
use crate::no_std::*;
use crate::public_key::{PublicKey, PublicKeyError};
use core::{
    fmt::{Debug, Display},
    hash::Hash,
    str::FromStr,
};

/// The interface for a generic address.
pub trait Address:
    'static + Clone + Debug + Display + FromStr + Hash + PartialEq + Eq + Send + Sized + Sync
{
    type SecretKey;
    type Format: Format;
    type PublicKey: PublicKey;

    /// Returns the address corresponding to the given private key.
    fn from_secret_key(
        secret_key: &Self::SecretKey,
        format: &Self::Format,
    ) -> Result<Self, AddressError>;

    /// Returns the address corresponding to the given public key.
    fn from_public_key(
        public_key: &Self::PublicKey,
        format: &Self::Format,
    ) -> Result<Self, AddressError>;

    fn is_valid(address: &str) -> bool {
        Self::from_str(address).is_ok()
    }
}

#[derive(Debug, Error)]
pub enum AddressError {
    #[error("{0:}: {1:}")]
    Crate(&'static str, String),

    #[error("invalid format conversion from {0:} to {1:}")]
    IncompatibleFormats(String, String),

    #[error("invalid address: {0:}")]
    InvalidAddress(String),

    #[error("invalid byte length: {0:}")]
    InvalidByteLength(usize),

    #[error("invalid character length: {0:}")]
    InvalidCharacterLength(usize),

    #[error("invalid address checksum: {{ expected: {0:}, found: {1:} }}")]
    InvalidChecksum(String, String),

    #[error("invalid network: {{ expected: {0}, found: {1} }}")]
    InvalidNetwork(String, String),

    #[error("invalid address prefix: {0:}")]
    InvalidPrefix(String),

    #[error("invalid address prefix length: {0:?}")]
    InvalidPrefixLength(usize),

    #[error("{0}")]
    Message(String),

    #[error("missing public spend key and/or public view key")]
    MissingPublicKey,

    #[error("{0}")]
    PublicKeyError(PublicKeyError),
}

impl From<crate::no_std::io::Error> for AddressError {
    fn from(error: crate::no_std::io::Error) -> Self {
        AddressError::Crate("crate::no_std::io", format!("{:?}", error))
    }
}

impl From<crate::no_std::FromUtf8Error> for AddressError {
    fn from(error: crate::no_std::FromUtf8Error) -> Self {
        AddressError::Crate("crate::no_std", format!("{:?}", error))
    }
}

impl From<&'static str> for AddressError {
    fn from(msg: &'static str) -> Self {
        AddressError::Message(msg.into())
    }
}

impl From<PublicKeyError> for AddressError {
    fn from(error: PublicKeyError) -> Self {
        AddressError::PublicKeyError(error)
    }
}

impl From<base58::FromBase58Error> for AddressError {
    fn from(error: base58::FromBase58Error) -> Self {
        AddressError::Crate("base58", format!("{:?}", error))
    }
}

impl From<bech32::Error> for AddressError {
    fn from(error: bech32::Error) -> Self {
        AddressError::Crate("bech32", format!("{:?}", error))
    }
}

impl From<core::str::Utf8Error> for AddressError {
    fn from(error: core::str::Utf8Error) -> Self {
        AddressError::Crate("core::str", format!("{:?}", error))
    }
}

impl From<hex::FromHexError> for AddressError {
    fn from(error: hex::FromHexError) -> Self {
        AddressError::Crate("hex", format!("{:?}", error))
    }
}

impl From<rand_core::Error> for AddressError {
    fn from(error: rand_core::Error) -> Self {
        AddressError::Crate("rand", format!("{:?}", error))
    }
}