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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use alloc::vec::Vec;
use anyhow::Error;
use prost::Message;
use warg_crypto::{
    hash::{Hash, SupportedDigest},
    VisitBytes,
};
use warg_protobuf::transparency as protobuf;

use crate::map::proof::Proof;

/// A collection of inclusion proof info
pub struct ProofBundle<D, K, V>
where
    D: SupportedDigest,
    K: VisitBytes,
    V: VisitBytes,
{
    proofs: Vec<Proof<D, K, V>>,
}

impl<D, K, V> ProofBundle<D, K, V>
where
    D: SupportedDigest,
    K: VisitBytes,
    V: VisitBytes,
{
    /// Bundles inclusion proofs together
    pub fn bundle(proofs: Vec<Proof<D, K, V>>) -> Self {
        ProofBundle { proofs }
    }

    /// Splits a bundle into its constituent inclusion proofs
    pub fn unbundle(self) -> Vec<Proof<D, K, V>> {
        self.proofs
    }

    /// Turn a bundle into bytes using protobuf
    pub fn encode(self) -> Vec<u8> {
        let proto: protobuf::MapProofBundle = self.into();
        proto.encode_to_vec()
    }

    /// Parse a bundle from bytes using protobuf
    pub fn decode(bytes: &[u8]) -> Result<Self, Error> {
        let proto = protobuf::MapProofBundle::decode(bytes)?;
        let bundle = proto.try_into()?;
        Ok(bundle)
    }
}

impl<D, K, V> From<ProofBundle<D, K, V>> for protobuf::MapProofBundle
where
    D: SupportedDigest,
    K: VisitBytes,
    V: VisitBytes,
{
    fn from(value: ProofBundle<D, K, V>) -> Self {
        let proofs = value.proofs.into_iter().map(|proof| proof.into()).collect();
        protobuf::MapProofBundle { proofs }
    }
}

impl<D, K, V> From<Proof<D, K, V>> for protobuf::MapInclusionProof
where
    D: SupportedDigest,
    K: VisitBytes,
    V: VisitBytes,
{
    fn from(value: Proof<D, K, V>) -> Self {
        let peers: Vec<Option<Hash<D>>> = value.into();
        protobuf::MapInclusionProof {
            hashes: peers.into_iter().map(|h| h.into()).collect(),
        }
    }
}

impl<D, K, V> TryFrom<protobuf::MapProofBundle> for ProofBundle<D, K, V>
where
    D: SupportedDigest,
    K: VisitBytes,
    V: VisitBytes,
{
    type Error = Error;

    fn try_from(value: protobuf::MapProofBundle) -> Result<Self, Self::Error> {
        let mut proofs = Vec::new();
        for entry in value.proofs {
            proofs.push(entry.try_into()?);
        }
        let bundle = ProofBundle { proofs };
        Ok(bundle)
    }
}

impl<D, K, V> TryFrom<protobuf::MapInclusionProof> for Proof<D, K, V>
where
    D: SupportedDigest,
    K: VisitBytes,
    V: VisitBytes,
{
    type Error = Error;

    fn try_from(value: protobuf::MapInclusionProof) -> Result<Self, Self::Error> {
        let peers: Result<Vec<Option<Hash<D>>>, Error> =
            value.hashes.into_iter().map(|h| h.try_into()).collect();
        let proof = Proof::new(peers?);
        Ok(proof)
    }
}