eq_common/
lib.rs

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