eq_common/
lib.rs

1use celestia_types::{RowProof, nmt::{Namespace, NamespaceProof}};
2use serde::{Deserialize, Serialize};
3
4#[cfg(feature = "host")]
5mod error;
6#[cfg(feature = "host")]
7pub use error::InclusionServiceError;
8
9#[cfg(feature = "grpc")]
10/// gRPC generated bindings
11pub mod eqs {
12    include!("generated/eqs.rs");
13}
14
15/*
16    The types of proofs we expect to support:
17    1. KeccakInclusionToDataRootProof
18    2. KeccakInclusionToBlockHashProof
19    3. PayyPoseidonToDataRootProof
20    4. PayyPoseidonToBlockHashProof
21*/
22
23#[derive(Serialize, Deserialize, Clone, Debug)]
24pub struct KeccakInclusionToDataRootProofInput {
25    pub data: Vec<u8>,
26    pub namespace_id: Namespace,
27    pub share_proofs: Vec<NamespaceProof>,
28    pub row_proof: RowProof,
29    pub data_root: [u8; 32],
30    pub keccak_hash: [u8; 32],
31}
32
33/// Expecting bytes:
34/// (keccak_hash: [u8; 32], pub data_root: [u8; 32])
35pub struct KeccakInclusionToDataRootProofOutput {
36    pub keccak_hash: [u8; 32],
37    pub data_root: [u8; 32],
38}
39impl KeccakInclusionToDataRootProofOutput {
40    // Simple encoding, rather than use any Ethereum libraries
41    pub fn to_vec(&self) -> Vec<u8> {
42        let mut encoded = Vec::new();
43        encoded.extend_from_slice(&self.keccak_hash);
44        encoded.extend_from_slice(&self.data_root);
45        encoded
46    }
47
48    #[cfg(feature = "host")]
49    pub fn from_bytes(data: &[u8]) -> Result<Self, InclusionServiceError> {
50        if data.len() != 64 {
51            return Err(InclusionServiceError::OutputDeserializationError);
52        }
53        let decoded = KeccakInclusionToDataRootProofOutput {
54            keccak_hash: data[0..32]
55                .try_into()
56                .map_err(|_| InclusionServiceError::OutputDeserializationError)?,
57            data_root: data[32..64]
58                .try_into()
59                .map_err(|_| InclusionServiceError::OutputDeserializationError)?,
60        };
61        Ok(decoded)
62    }
63}
64
65#[cfg(test)]
66mod test {
67    use super::*;
68
69    #[test]
70    #[cfg(feature = "host")]
71    fn test_abi_encoding() {
72        let output = KeccakInclusionToDataRootProofOutput {
73            keccak_hash: [0; 32],
74            data_root: [0; 32],
75        };
76        let encoded = output.to_vec();
77        let decoded = KeccakInclusionToDataRootProofOutput::from_bytes(&encoded).unwrap();
78        assert_eq!(output.keccak_hash, decoded.keccak_hash);
79        assert_eq!(output.data_root, decoded.data_root);
80    }
81}