Skip to main content

celestia_types/block/
data.rs

1use celestia_proto::tendermint_celestia_mods::types::Data as RawData;
2#[cfg(all(target_arch = "wasm32", feature = "wasm-bindgen"))]
3use js_sys::Uint8Array;
4use serde::{Deserialize, Serialize};
5use tendermint_proto::Protobuf;
6#[cfg(all(target_arch = "wasm32", feature = "wasm-bindgen"))]
7use wasm_bindgen::prelude::*;
8
9use crate::Error;
10
11/// Data contained in a [`Block`].
12///
13/// [`Block`]: crate::block::Block
14#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
15#[serde(try_from = "RawData", into = "RawData")]
16#[cfg_attr(
17    all(target_arch = "wasm32", feature = "wasm-bindgen"),
18    wasm_bindgen(getter_with_clone)
19)]
20pub struct Data {
21    /// Transactions.
22    #[cfg_attr(
23        all(target_arch = "wasm32", feature = "wasm-bindgen"),
24        wasm_bindgen(skip)
25    )]
26    pub txs: Vec<Vec<u8>>,
27
28    /// Square width of original data square.
29    pub square_size: u64,
30
31    /// Hash is the root of a binary Merkle tree where the leaves of the tree are
32    /// the row and column roots of an extended data square. Hash is often referred
33    /// to as the "data root".
34    pub hash: Vec<u8>,
35}
36
37#[cfg(all(target_arch = "wasm32", feature = "wasm-bindgen"))]
38#[wasm_bindgen]
39impl Data {
40    /// Transactions
41    #[wasm_bindgen(getter)]
42    pub fn transactions(&self) -> Vec<Uint8Array> {
43        self.txs
44            .iter()
45            .map(|tx| Uint8Array::from(&tx[..]))
46            .collect()
47    }
48}
49
50impl Protobuf<RawData> for Data {}
51
52impl TryFrom<RawData> for Data {
53    type Error = Error;
54
55    fn try_from(value: RawData) -> Result<Self, Self::Error> {
56        Ok(Data {
57            txs: value.txs,
58            square_size: value.square_size,
59            hash: value.hash,
60        })
61    }
62}
63
64impl From<Data> for RawData {
65    fn from(value: Data) -> RawData {
66        RawData {
67            txs: value.txs,
68            square_size: value.square_size,
69            hash: value.hash,
70        }
71    }
72}
73
74#[cfg(feature = "uniffi")]
75mod uniffi_types {
76    use super::Data as BlockData;
77
78    // we need to rename `Data`, otherwise it clashes with `Foundation::Data` for Swift
79    #[uniffi::remote(Record)]
80    pub struct BlockData {
81        /// Transactions.
82        pub txs: Vec<Vec<u8>>,
83
84        /// Square width of original data square.
85        pub square_size: u64,
86
87        /// Hash is the root of a binary Merkle tree where the leaves of the tree are
88        /// the row and column roots of an extended data square. Hash is often referred
89        /// to as the "data root".
90        pub hash: Vec<u8>,
91    }
92}