miden_client/rpc/domain/
merkle.rs1use alloc::vec::Vec;
2
3use miden_protocol::Word;
4use miden_protocol::crypto::merkle::mmr::{Forest, MmrDelta};
5use miden_protocol::crypto::merkle::{MerklePath, SparseMerklePath};
6
7use crate::rpc::errors::RpcConversionError;
8use crate::rpc::generated as proto;
9
10impl From<MerklePath> for proto::primitives::MerklePath {
14 fn from(value: MerklePath) -> Self {
15 (&value).into()
16 }
17}
18
19impl From<&MerklePath> for proto::primitives::MerklePath {
20 fn from(value: &MerklePath) -> Self {
21 let siblings = value.nodes().iter().map(proto::primitives::Digest::from).collect();
22 proto::primitives::MerklePath { siblings }
23 }
24}
25
26impl TryFrom<&proto::primitives::MerklePath> for MerklePath {
27 type Error = RpcConversionError;
28
29 fn try_from(merkle_path: &proto::primitives::MerklePath) -> Result<Self, Self::Error> {
30 merkle_path.siblings.iter().map(Word::try_from).collect()
31 }
32}
33
34impl TryFrom<proto::primitives::MerklePath> for MerklePath {
35 type Error = RpcConversionError;
36
37 fn try_from(merkle_path: proto::primitives::MerklePath) -> Result<Self, Self::Error> {
38 MerklePath::try_from(&merkle_path)
39 }
40}
41
42impl From<SparseMerklePath> for proto::primitives::SparseMerklePath {
47 fn from(value: SparseMerklePath) -> Self {
48 let (empty_nodes_mask, siblings) = value.into_parts();
49
50 proto::primitives::SparseMerklePath {
51 empty_nodes_mask,
52
53 siblings: siblings.into_iter().map(proto::primitives::Digest::from).collect(),
54 }
55 }
56}
57
58impl TryFrom<proto::primitives::SparseMerklePath> for SparseMerklePath {
59 type Error = RpcConversionError;
60
61 fn try_from(merkle_path: proto::primitives::SparseMerklePath) -> Result<Self, Self::Error> {
62 Ok(SparseMerklePath::from_parts(
63 merkle_path.empty_nodes_mask,
64 merkle_path
65 .siblings
66 .into_iter()
67 .map(Word::try_from)
68 .collect::<Result<Vec<_>, _>>()?,
69 )?)
70 }
71}
72
73impl TryFrom<MmrDelta> for proto::primitives::MmrDelta {
77 type Error = RpcConversionError;
78
79 fn try_from(value: MmrDelta) -> Result<Self, Self::Error> {
80 let data = value.data.into_iter().map(proto::primitives::Digest::from).collect();
81 Ok(proto::primitives::MmrDelta {
82 forest: u64::try_from(value.forest.num_leaves())?,
83 data,
84 })
85 }
86}
87
88impl TryFrom<proto::primitives::MmrDelta> for MmrDelta {
89 type Error = RpcConversionError;
90
91 fn try_from(value: proto::primitives::MmrDelta) -> Result<Self, Self::Error> {
92 let data: Result<Vec<_>, RpcConversionError> =
93 value.data.into_iter().map(Word::try_from).collect();
94
95 Ok(MmrDelta {
96 forest: Forest::new(usize::try_from(value.forest).expect("u64 should fit in usize")),
97 data: data?,
98 })
99 }
100}