zebra_chain/primitives/
viewing_key.rs

1//! Type definitions for viewing keys and their hashes.
2
3use crate::parameters::Network;
4
5mod orchard;
6mod sapling;
7
8use orchard::OrchardViewingKey;
9use sapling::SaplingViewingKey;
10
11#[cfg(test)]
12mod tests;
13
14/// A Zcash Sapling or Orchard viewing key
15#[derive(Debug, Clone)]
16pub enum ViewingKey {
17    /// A viewing key for Sapling
18    Sapling(SaplingViewingKey),
19
20    /// A viewing key for Orchard
21    Orchard(OrchardViewingKey),
22}
23
24impl ViewingKey {
25    /// Accepts an encoded Sapling viewing key to decode
26    ///
27    /// Returns a [`ViewingKey`] if successful, or None otherwise
28    fn parse_sapling(sapling_key: &str, network: &Network) -> Option<Self> {
29        SaplingViewingKey::parse(sapling_key, network).map(Self::Sapling)
30    }
31
32    /// Accepts an encoded Orchard viewing key to decode
33    ///
34    /// Returns a [`ViewingKey`] if successful, or None otherwise
35    fn parse_orchard(sapling_key: &str, network: &Network) -> Option<Self> {
36        OrchardViewingKey::parse(sapling_key, network).map(Self::Orchard)
37    }
38
39    /// Parses an encoded viewing key and returns it as a [`ViewingKey`] type.
40    pub fn parse(key: &str, network: &Network) -> Option<Self> {
41        Self::parse_sapling(key, network).or_else(|| Self::parse_orchard(key, network))
42    }
43}