Skip to main content

zecwallet_parser/zwl/
block.rs

1use std::{
2    fmt,
3    io::{self, Read},
4};
5
6use byteorder::{LittleEndian, ReadBytesExt};
7use sapling_crypto::CommitmentTree;
8use zcash_encoding::Vector;
9use zcash_primitives::merkle_tree::read_commitment_tree;
10
11#[derive(Clone, Debug)]
12pub struct CompactBlockData {
13    pub ecb: Vec<u8>,
14    pub height: u64,
15}
16
17impl CompactBlockData {
18    pub fn serialized_version() -> u64 {
19        20
20    }
21
22    pub fn read<R: Read>(mut reader: R) -> io::Result<Self> {
23        // read height of CompactBlock
24        let height = reader.read_i32::<LittleEndian>()? as u64;
25
26        // read CompactBlock hash
27        let mut hash_bytes = [0; 32];
28        reader.read_exact(&mut hash_bytes)?;
29        hash_bytes.reverse();
30        let _hash = hex::encode(hash_bytes);
31
32        // We don't need this, but because of a quirk, the version is stored later, so we can't actually
33        // detect the version here. So we write an empty tree and read it back here
34        let tree: CommitmentTree = read_commitment_tree(&mut reader)?;
35        let _tree = if tree.size() == 0 { None } else { Some(tree) };
36
37        // read version
38        let _version = reader.read_u64::<LittleEndian>()?;
39
40        // read "ecb" (encoded compact block?)
41        let ecb = Vector::read(&mut reader, |r| r.read_u8()).unwrap_or_default();
42
43        Ok(Self { ecb, height })
44    }
45}
46
47impl fmt::Display for CompactBlockData {
48    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49        writeln!(f, "Block height: {}", self.height).unwrap();
50
51        writeln!(f, "ECB size in bytes: {}", self.ecb.len()).unwrap();
52        Ok(())
53    }
54}