Skip to main content

miden_client/rpc/domain/
nullifier.rs

1use miden_protocol::Word;
2use miden_protocol::block::BlockNumber;
3use miden_protocol::note::Nullifier;
4
5use crate::rpc::domain::MissingFieldHelper;
6use crate::rpc::errors::RpcConversionError;
7use crate::rpc::generated as proto;
8
9// NULLIFIER UPDATE
10// ================================================================================================
11
12/// Represents a note that was consumed in the node at a certain block.
13#[derive(Debug, Clone, Eq, PartialOrd, Ord)]
14pub struct NullifierUpdate {
15    /// The nullifier of the consumed note.
16    pub nullifier: Nullifier,
17    /// The number of the block in which the note consumption was registered.
18    pub block_num: BlockNumber,
19}
20
21impl PartialEq for NullifierUpdate {
22    fn eq(&self, other: &Self) -> bool {
23        self.nullifier == other.nullifier
24    }
25}
26
27// CONVERSIONS
28// ================================================================================================
29
30/// Reads a nullifier off the wire. A free function because both types are foreign, so there can be
31/// no `TryFrom` impl.
32pub(crate) fn nullifier_from_proto(
33    value: proto::primitives::Word,
34) -> Result<Nullifier, RpcConversionError> {
35    let word: Word = value.try_into()?;
36    Ok(Nullifier::from_raw(word))
37}
38
39impl TryFrom<&proto::rpc::sync_nullifiers_response::NullifierUpdate> for NullifierUpdate {
40    type Error = RpcConversionError;
41
42    fn try_from(
43        value: &proto::rpc::sync_nullifiers_response::NullifierUpdate,
44    ) -> Result<Self, Self::Error> {
45        Ok(Self {
46            nullifier: nullifier_from_proto(value.nullifier.clone().ok_or(
47                proto::rpc::sync_nullifiers_response::NullifierUpdate::missing_field(stringify!(
48                    nullifier
49                )),
50            )?)?,
51            block_num: value.block_num.into(),
52        })
53    }
54}