mithril_common/messages/message_parts/
mk_set_proof.rs1use 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#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
18pub struct MkSetProofMessagePart<T> {
19 pub items: Vec<T>,
21
22 pub proof: HexEncodedKey,
24}
25
26impl<T: Clone> MkSetProofMessagePart<T> {
27 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#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
62#[cfg_attr(target_family = "wasm", wasm_bindgen(js_name = "CardanoBlock"))]
63pub struct CardanoBlockMessagePart {
64 #[cfg_attr(target_family = "wasm", wasm_bindgen(getter_with_clone))]
66 pub block_hash: BlockHash,
67 #[cfg_attr(target_family = "wasm", wasm_bindgen(skip))]
69 pub block_number: BlockNumber,
70 #[cfg_attr(target_family = "wasm", wasm_bindgen(skip))]
72 pub slot_number: SlotNumber,
73}
74
75impl CardanoBlockMessagePart {
76 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 #[wasm_bindgen(getter)]
95 pub fn block_number(&self) -> u64 {
96 *self.block_number
97 }
98
99 #[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#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
128#[cfg_attr(target_family = "wasm", wasm_bindgen(js_name = "CardanoTransaction"))]
129pub struct CardanoTransactionMessagePart {
130 #[cfg_attr(target_family = "wasm", wasm_bindgen(getter_with_clone))]
132 pub transaction_hash: TransactionHash,
133
134 #[cfg_attr(target_family = "wasm", wasm_bindgen(skip))]
136 pub block_number: BlockNumber,
137
138 #[cfg_attr(target_family = "wasm", wasm_bindgen(skip))]
140 pub slot_number: SlotNumber,
141
142 #[cfg_attr(target_family = "wasm", wasm_bindgen(getter_with_clone))]
144 pub block_hash: BlockHash,
145}
146
147impl CardanoTransactionMessagePart {
148 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 #[wasm_bindgen(getter)]
169 pub fn block_number(&self) -> u64 {
170 *self.block_number
171 }
172
173 #[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}