zecwallet-parser 0.0.0

A parser for ZecWallet Lite wallet files.
Documentation
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use std::{
    fmt::{self, Display},
    io::{self, Read},
};
use zcash_encoding::Optional;

// Struct that tracks the latest and historical price of ZEC in the wallet
#[derive(Clone, Debug)]
pub struct WalletZecPriceInfo {
    // Latest price of ZEC and when it was fetched
    pub zec_price: Option<(u64, f64)>,

    // Wallet's currency. All the prices are in this currency
    pub currency: String,

    // When the last time historical prices were fetched
    pub last_historical_prices_fetched_at: Option<u64>,

    // Historical prices retry count
    pub historical_prices_retry_count: u64,
}

impl Default for WalletZecPriceInfo {
    fn default() -> Self {
        Self::new()
    }
}

impl WalletZecPriceInfo {
    pub fn new() -> Self {
        Self {
            zec_price: None,
            currency: "USD".to_string(), // Only USD is supported right now.
            last_historical_prices_fetched_at: None,
            historical_prices_retry_count: 0,
        }
    }

    pub fn serialized_version() -> u64 {
        20
    }

    pub fn read<R: Read>(mut reader: R) -> io::Result<Self> {
        let version = reader.read_u64::<LittleEndian>()?;
        if version > Self::serialized_version() {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "Can't read ZecPriceInfo because of incorrect version",
            ));
        }

        // The "current" zec price is not persisted, since it is almost certainly outdated
        let zec_price = None;

        // Currency is only USD for now
        let currency = "USD".to_string();

        let last_historical_prices_fetched_at =
            Optional::read(&mut reader, |r| r.read_u64::<LittleEndian>())?;
        let historical_prices_retry_count = reader.read_u64::<LittleEndian>()?;

        Ok(Self {
            zec_price,
            currency,
            last_historical_prices_fetched_at,
            historical_prices_retry_count,
        })
    }

    pub fn write<W: WriteBytesExt>(&self, mut writer: W) -> io::Result<()> {
        writer.write_u64::<LittleEndian>(Self::serialized_version())?;

        // We don't write the currency zec price or the currency yet.
        Optional::write(
            &mut writer,
            self.last_historical_prices_fetched_at,
            |w, t| w.write_u64::<LittleEndian>(t),
        )?;
        writer.write_u64::<LittleEndian>(self.historical_prices_retry_count)?;

        Ok(())
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MemoDownloadOption {
    NoMemos = 0,
    WalletMemos,
    AllMemos,
}

impl fmt::Display for MemoDownloadOption {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            MemoDownloadOption::NoMemos => write!(f, "NoMemos"),
            MemoDownloadOption::WalletMemos => write!(f, "WalletMemos"),
            MemoDownloadOption::AllMemos => write!(f, "AllMemos"),
        }
    }
}

#[derive(Debug, Clone, Copy)]
pub struct WalletOptions {
    pub(crate) download_memos: MemoDownloadOption,
    pub(crate) spam_threshold: i64,
}

impl Default for WalletOptions {
    fn default() -> Self {
        WalletOptions {
            download_memos: MemoDownloadOption::WalletMemos,
            spam_threshold: -1,
        }
    }
}

impl WalletOptions {
    pub fn serialized_version() -> u64 {
        2
    }

    pub fn read<R: ReadBytesExt>(mut reader: R) -> io::Result<Self> {
        let version = reader.read_u64::<LittleEndian>()?;

        let download_memos = match reader.read_u8()? {
            0 => MemoDownloadOption::NoMemos,
            1 => MemoDownloadOption::WalletMemos,
            2 => MemoDownloadOption::AllMemos,
            v => {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    format!("Bad download option {}", v),
                ));
            }
        };

        let spam_threshold = if version <= 1 {
            -1
        } else {
            reader.read_i64::<LittleEndian>()?
        };

        Ok(Self {
            download_memos,
            spam_threshold,
        })
    }

    pub fn write<W: WriteBytesExt>(&self, mut writer: W) -> io::Result<()> {
        // Write the version
        writer.write_u64::<LittleEndian>(Self::serialized_version())?;

        writer.write_u8(self.download_memos as u8)?;

        writer.write_i64::<LittleEndian>(self.spam_threshold)
    }
}

impl fmt::Display for WalletOptions {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "download_memos: {}, spam_threshold: {}",
            self.download_memos, self.spam_threshold
        )
    }
}

#[derive(Debug, Clone, Copy)]
pub enum ChainType {
    Mainnet,
    Testnet,
    Regtest,
    Unknown,
}

impl From<String> for ChainType {
    fn from(s: String) -> Self {
        match s.as_str() {
            "mainnet" | "main" => ChainType::Mainnet,
            "testnet" | "test" => ChainType::Testnet,
            "regtest" => ChainType::Regtest,
            _ => ChainType::Unknown,
        }
    }
}

impl Display for ChainType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            ChainType::Mainnet => write!(f, "Mainnet"),
            ChainType::Testnet => write!(f, "Testnet"),
            ChainType::Regtest => write!(f, "Regtest"),
            ChainType::Unknown => write!(f, "Unkown"),
        }
    }
}