zebra_rpc/methods/types/
z_validate_address.rs

1//! Response type for the `z_validateaddress` RPC.
2
3use zebra_chain::primitives::Address;
4
5/// `z_validateaddress` response
6#[derive(Clone, Default, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
7pub struct Response {
8    /// Whether the address is valid.
9    ///
10    /// If not, this is the only property returned.
11    #[serde(rename = "isvalid")]
12    pub is_valid: bool,
13
14    /// The zcash address that has been validated.
15    #[serde(skip_serializing_if = "Option::is_none")]
16    pub address: Option<String>,
17
18    /// The type of the address.
19    #[serde(skip_serializing_if = "Option::is_none")]
20    pub address_type: Option<AddressType>,
21
22    /// Whether the address is yours or not.
23    ///
24    /// Always false for now since Zebra doesn't have a wallet yet.
25    #[serde(rename = "ismine")]
26    #[serde(skip_serializing_if = "Option::is_none")]
27    pub is_mine: Option<bool>,
28}
29
30impl Response {
31    /// Creates an empty response with `isvalid` of false.
32    pub fn invalid() -> Self {
33        Self::default()
34    }
35}
36
37/// Address types supported by the `z_validateaddress` RPC according to
38/// <https://zcash.github.io/rpc/z_validateaddress.html>.
39#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, PartialEq, Eq)]
40#[serde(rename_all = "lowercase")]
41pub enum AddressType {
42    /// The `p2pkh` address type.
43    P2pkh,
44    /// The `p2sh` address type.
45    P2sh,
46    /// The `sapling` address type.
47    Sapling,
48    /// The `unified` address type.
49    Unified,
50}
51
52impl From<&Address> for AddressType {
53    fn from(address: &Address) -> Self {
54        match address {
55            Address::Transparent(_) => {
56                if address.is_script_hash() {
57                    Self::P2sh
58                } else {
59                    Self::P2pkh
60                }
61            }
62            Address::Sapling { .. } => Self::Sapling,
63            Address::Unified { .. } => Self::Unified,
64        }
65    }
66}