Skip to main content

zecwallet_parser/zwl/keys/
sapling.rs

1use std::fmt;
2use std::io::{self, ErrorKind, Read};
3
4use byteorder::{LittleEndian, ReadBytesExt};
5use sapling_crypto::PaymentAddress;
6use sapling_crypto::zip32::{ExtendedFullViewingKey, ExtendedSpendingKey};
7use zcash_encoding::{Optional, Vector};
8
9#[derive(PartialEq, Debug, Clone)]
10pub enum WalletZKeyType {
11    HdKey = 0,
12    ImportedSpendingKey = 1,
13    ImportedViewKey = 2,
14}
15
16#[derive(Clone, Debug, PartialEq)]
17pub struct WalletZKey {
18    pub keytype: WalletZKeyType,
19    pub(crate) locked: bool,
20    pub extsk: Option<ExtendedSpendingKey>,
21    pub extfvk: ExtendedFullViewingKey,
22    pub zaddress: PaymentAddress,
23
24    // If this is a HD key, what is the key number
25    pub hdkey_num: Option<u32>,
26
27    // If locked, the encrypted private key is stored here
28    pub enc_key: Option<Vec<u8>>,
29    pub nonce: Option<Vec<u8>>,
30}
31
32impl WalletZKey {
33    pub fn serialized_version() -> u8 {
34        1
35    }
36
37    pub fn read<R: Read>(mut reader: R) -> io::Result<Self> {
38        let version = reader.read_u8()?;
39        assert!(version <= Self::serialized_version());
40
41        // read type of the key
42        let keytype: WalletZKeyType = match reader.read_u32::<LittleEndian>()? {
43            0 => Ok(WalletZKeyType::HdKey),
44            1 => Ok(WalletZKeyType::ImportedSpendingKey),
45            2 => Ok(WalletZKeyType::ImportedViewKey),
46            n => Err(io::Error::new(
47                ErrorKind::InvalidInput,
48                format!("Unknown zkey type {}", n),
49            )),
50        }?;
51
52        // read if address is locked
53        let locked = reader.read_u8()? > 0;
54
55        // read address extsk
56        let extsk = Optional::read(&mut reader, ExtendedSpendingKey::read)?;
57
58        // read address extfvk
59        let extfvk = ExtendedFullViewingKey::read(&mut reader)?;
60
61        // derive zaddress from extfvk
62        let (_, zaddress) = extfvk.default_address();
63
64        // If HD derived, read the key index
65        let hdkey_num = Optional::read(&mut reader, |r| r.read_u32::<LittleEndian>())?;
66
67        // read "possible" encrypted key
68        let enc_key = Optional::read(&mut reader, |r| Vector::read(r, |r| r.read_u8()))?;
69
70        // read ""possible" nounce used for encryption
71        let nonce = Optional::read(&mut reader, |r| Vector::read(r, |r| r.read_u8()))?;
72
73        Ok(Self {
74            keytype,
75            locked,
76            extsk,
77            extfvk,
78            zaddress,
79            hdkey_num,
80            enc_key,
81            nonce,
82        })
83    }
84}
85
86impl fmt::Display for WalletZKey {
87    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
88        match self.keytype {
89            WalletZKeyType::HdKey => {
90                writeln!(f, "Type: HD key").unwrap();
91            }
92            WalletZKeyType::ImportedSpendingKey => {
93                writeln!(f, "Type: Imported spending key").unwrap();
94            }
95            WalletZKeyType::ImportedViewKey => {
96                writeln!(f, "Type: Imported view key").unwrap();
97            }
98        }
99
100        match self.locked {
101            true => {
102                writeln!(f, "Status: Encrypted").unwrap();
103            }
104            false => {
105                writeln!(f, "Status: Decrypted").unwrap();
106            }
107        }
108
109        if let Some(extsk) = &self.extsk {
110            writeln!(f, "{:?}", extsk).unwrap();
111        }
112
113        writeln!(f, "{:?}", self.extfvk).unwrap();
114
115        writeln!(f, "{:?}", self.zaddress).unwrap();
116
117        Ok(())
118    }
119}