1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
use serde::{Deserialize, Serialize};
use tendermint_proto::v0_37::types::TxProof as RawTxProof;
use tendermint_proto::Protobuf;

use crate::{merkle, prelude::*, Error, Hash};

/// Merkle proof of the presence of a transaction in the Merkle tree.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(try_from = "RawTxProof", into = "RawTxProof")]
pub struct Proof {
    pub root_hash: Hash,
    pub data: Vec<u8>,
    pub proof: merkle::Proof,
}

impl Protobuf<RawTxProof> for Proof {}

impl TryFrom<RawTxProof> for Proof {
    type Error = Error;

    fn try_from(message: RawTxProof) -> Result<Self, Self::Error> {
        Ok(Self {
            root_hash: message.root_hash.try_into()?,
            data: message.data,
            proof: message.proof.ok_or_else(Error::missing_data)?.try_into()?,
        })
    }
}

impl From<Proof> for RawTxProof {
    fn from(value: Proof) -> Self {
        Self {
            root_hash: value.root_hash.into(),
            data: value.data,
            proof: Some(value.proof.into()),
        }
    }
}