zebra_rpc/methods/types/
z_validate_address.rs

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