Skip to main content

wagyu_model/
address.rs

1use crate::format::Format;
2use crate::private_key::{PrivateKey, PrivateKeyError};
3use crate::public_key::{PublicKey, PublicKeyError};
4
5use std::{
6    fmt::{Debug, Display},
7    hash::Hash,
8    str::FromStr,
9};
10
11/// The interface for a generic address.
12pub trait Address: Clone + Debug + Display + FromStr + Send + Sync + 'static + Eq + Ord + Sized + Hash {
13    type Format: Format;
14    type PrivateKey: PrivateKey;
15    type PublicKey: PublicKey;
16
17    /// Returns the address corresponding to the given private key.
18    fn from_private_key(private_key: &Self::PrivateKey, format: &Self::Format) -> Result<Self, AddressError>;
19
20    /// Returns the address corresponding to the given public key.
21    fn from_public_key(public_key: &Self::PublicKey, format: &Self::Format) -> Result<Self, AddressError>;
22}
23
24#[derive(Debug, Fail)]
25pub enum AddressError {
26    #[fail(display = "{}: {}", _0, _1)]
27    Crate(&'static str, String),
28
29    #[fail(display = "invalid format conversion from {:?} to {:?}", _0, _1)]
30    IncompatibleFormats(String, String),
31
32    #[fail(display = "invalid address: {}", _0)]
33    InvalidAddress(String),
34
35    #[fail(display = "invalid byte length: {}", _0)]
36    InvalidByteLength(usize),
37
38    #[fail(display = "invalid character length: {}", _0)]
39    InvalidCharacterLength(usize),
40
41    #[fail(display = "invalid address checksum: {{ expected: {:?}, found: {:?} }}", _0, _1)]
42    InvalidChecksum(String, String),
43
44    #[fail(display = "invalid network: {{ expected: {:?}, found: {:?} }}", _0, _1)]
45    InvalidNetwork(String, String),
46
47    #[fail(display = "invalid address prefix: {:?}", _0)]
48    InvalidPrefix(Vec<u8>),
49
50    #[fail(display = "invalid address prefix length: {:?}", _0)]
51    InvalidPrefixLength(usize),
52
53    #[fail(display = "{}", _0)]
54    Message(String),
55
56    #[fail(display = "missing public spend key and/or public view key")]
57    MissingPublicKey,
58
59    #[fail(display = "{}", _0)]
60    PrivateKeyError(PrivateKeyError),
61
62    #[fail(display = "{}", _0)]
63    PublicKeyError(PublicKeyError),
64}
65
66impl From<&'static str> for AddressError {
67    fn from(msg: &'static str) -> Self {
68        AddressError::Message(msg.into())
69    }
70}
71
72impl From<PrivateKeyError> for AddressError {
73    fn from(error: PrivateKeyError) -> Self {
74        AddressError::PrivateKeyError(error)
75    }
76}
77
78impl From<PublicKeyError> for AddressError {
79    fn from(error: PublicKeyError) -> Self {
80        AddressError::PublicKeyError(error)
81    }
82}
83
84impl From<base58::FromBase58Error> for AddressError {
85    fn from(error: base58::FromBase58Error) -> Self {
86        AddressError::Crate("base58", format!("{:?}", error))
87    }
88}
89
90impl From<base58_monero::base58::Error> for AddressError {
91    fn from(error: base58_monero::base58::Error) -> Self {
92        AddressError::Crate("base58_monero", format!("{:?}", error))
93    }
94}
95
96impl From<bech32::Error> for AddressError {
97    fn from(error: bech32::Error) -> Self {
98        AddressError::Crate("bech32", format!("{:?}", error))
99    }
100}
101
102impl From<hex::FromHexError> for AddressError {
103    fn from(error: hex::FromHexError) -> Self {
104        AddressError::Crate("hex", format!("{:?}", error))
105    }
106}
107
108impl From<rand_core::Error> for AddressError {
109    fn from(error: rand_core::Error) -> Self {
110        AddressError::Crate("rand", format!("{:?}", error))
111    }
112}
113
114impl From<std::io::Error> for AddressError {
115    fn from(error: std::io::Error) -> Self {
116        AddressError::Crate("std::io", format!("{:?}", error))
117    }
118}
119
120impl From<std::str::Utf8Error> for AddressError {
121    fn from(error: std::str::Utf8Error) -> Self {
122        AddressError::Crate("std::str", format!("{:?}", error))
123    }
124}
125
126impl From<std::string::FromUtf8Error> for AddressError {
127    fn from(error: std::string::FromUtf8Error) -> Self {
128        AddressError::Crate("std::string", format!("{:?}", error))
129    }
130}