Skip to main content

mithril_common/messages/message_parts/
mk_set_proof.rs

1use serde::{Deserialize, Serialize};
2
3use crate::entities::{
4    BlockHash, BlockNumber, CardanoBlock, CardanoTransaction, IntoMKTreeNode, MkSetProof,
5    SlotNumber,
6};
7use crate::{
8    StdError, StdResult,
9    crypto_helper::ProtocolMkProof,
10    entities::{HexEncodedKey, TransactionHash},
11};
12
13#[cfg(target_family = "wasm")]
14use wasm_bindgen::prelude::*;
15
16/// A cryptographic proof that a set of items is included in a Merkle tree
17#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
18pub struct MkSetProofMessagePart<T> {
19    /// Items certified by the proof
20    pub items: Vec<T>,
21
22    /// Proof of the items
23    pub proof: HexEncodedKey,
24}
25
26impl<T: Clone> MkSetProofMessagePart<T> {
27    /// Verify this proof by converting to `MkSetProof<U>` and validating
28    pub fn verify<U>(&self) -> StdResult<MkSetProof<U>>
29    where
30        U: IntoMKTreeNode + Clone + From<T>,
31    {
32        let mk_set_proof: MkSetProof<U> = self.clone().try_into()?;
33        mk_set_proof.verify()?;
34        Ok(mk_set_proof)
35    }
36}
37
38impl<T, U: Into<T> + IntoMKTreeNode + Clone> TryFrom<MkSetProof<U>> for MkSetProofMessagePart<T> {
39    type Error = StdError;
40
41    fn try_from(value: MkSetProof<U>) -> Result<Self, Self::Error> {
42        Ok(Self {
43            items: value.items.into_iter().map(Into::into).collect(),
44            proof: value.proof.to_bytes_hex()?,
45        })
46    }
47}
48
49impl<T: IntoMKTreeNode + Clone, U: Into<T>> TryFrom<MkSetProofMessagePart<U>> for MkSetProof<T> {
50    type Error = StdError;
51
52    fn try_from(value: MkSetProofMessagePart<U>) -> Result<Self, Self::Error> {
53        Ok(Self {
54            items: value.items.into_iter().map(Into::into).collect(),
55            proof: ProtocolMkProof::from_bytes_hex(&value.proof)?,
56        })
57    }
58}
59
60/// Cardano block message representation
61#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
62#[cfg_attr(target_family = "wasm", wasm_bindgen(js_name = "CardanoBlock"))]
63pub struct CardanoBlockMessagePart {
64    /// Block hash
65    #[cfg_attr(target_family = "wasm", wasm_bindgen(getter_with_clone))]
66    pub block_hash: BlockHash,
67    /// Block number
68    #[cfg_attr(target_family = "wasm", wasm_bindgen(skip))]
69    pub block_number: BlockNumber,
70    /// Slot number of the block
71    #[cfg_attr(target_family = "wasm", wasm_bindgen(skip))]
72    pub slot_number: SlotNumber,
73}
74
75impl CardanoBlockMessagePart {
76    /// CardanoBlockMessagePart factory
77    pub fn new<U: Into<BlockHash>>(
78        block_hash: U,
79        block_number: BlockNumber,
80        slot_number: SlotNumber,
81    ) -> Self {
82        Self {
83            block_hash: block_hash.into(),
84            block_number,
85            slot_number,
86        }
87    }
88}
89
90#[cfg(target_family = "wasm")]
91#[wasm_bindgen(js_class = "CardanoBlock")]
92impl CardanoBlockMessagePart {
93    /// Block number
94    #[wasm_bindgen(getter)]
95    pub fn block_number(&self) -> u64 {
96        *self.block_number
97    }
98
99    /// Block number
100    #[wasm_bindgen(getter)]
101    pub fn slot_number(&self) -> u64 {
102        *self.slot_number
103    }
104}
105
106impl From<CardanoBlock> for CardanoBlockMessagePart {
107    fn from(block: CardanoBlock) -> Self {
108        Self {
109            block_hash: block.block_hash,
110            block_number: block.block_number,
111            slot_number: block.slot_number,
112        }
113    }
114}
115
116impl From<CardanoBlockMessagePart> for CardanoBlock {
117    fn from(block: CardanoBlockMessagePart) -> Self {
118        Self {
119            block_hash: block.block_hash,
120            block_number: block.block_number,
121            slot_number: block.slot_number,
122        }
123    }
124}
125
126/// Cardano transaction message representation
127#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
128#[cfg_attr(target_family = "wasm", wasm_bindgen(js_name = "CardanoTransaction"))]
129pub struct CardanoTransactionMessagePart {
130    /// Unique hash of the transaction
131    #[cfg_attr(target_family = "wasm", wasm_bindgen(getter_with_clone))]
132    pub transaction_hash: TransactionHash,
133
134    /// Block number of the transaction
135    #[cfg_attr(target_family = "wasm", wasm_bindgen(skip))]
136    pub block_number: BlockNumber,
137
138    /// Slot number of the transaction
139    #[cfg_attr(target_family = "wasm", wasm_bindgen(skip))]
140    pub slot_number: SlotNumber,
141
142    /// Block hash of the transaction
143    #[cfg_attr(target_family = "wasm", wasm_bindgen(getter_with_clone))]
144    pub block_hash: BlockHash,
145}
146
147impl CardanoTransactionMessagePart {
148    /// CardanoTransactionMessagePart factory
149    pub fn new<T: Into<TransactionHash>, U: Into<BlockHash>>(
150        hash: T,
151        block_number: BlockNumber,
152        slot_number: SlotNumber,
153        block_hash: U,
154    ) -> Self {
155        Self {
156            transaction_hash: hash.into(),
157            block_number,
158            slot_number,
159            block_hash: block_hash.into(),
160        }
161    }
162}
163
164#[cfg(target_family = "wasm")]
165#[wasm_bindgen(js_class = "CardanoTransaction")]
166impl CardanoTransactionMessagePart {
167    /// Block number
168    #[wasm_bindgen(getter)]
169    pub fn block_number(&self) -> u64 {
170        *self.block_number
171    }
172
173    /// Block number
174    #[wasm_bindgen(getter)]
175    pub fn slot_number(&self) -> u64 {
176        *self.slot_number
177    }
178}
179
180impl From<CardanoTransaction> for CardanoTransactionMessagePart {
181    fn from(transaction: CardanoTransaction) -> Self {
182        Self {
183            transaction_hash: transaction.transaction_hash,
184            block_number: transaction.block_number,
185            slot_number: transaction.slot_number,
186            block_hash: transaction.block_hash,
187        }
188    }
189}
190
191impl From<CardanoTransactionMessagePart> for CardanoTransaction {
192    fn from(transaction: CardanoTransactionMessagePart) -> Self {
193        Self {
194            transaction_hash: transaction.transaction_hash,
195            block_number: transaction.block_number,
196            slot_number: transaction.slot_number,
197            block_hash: transaction.block_hash,
198        }
199    }
200}