miden_objects/note/
note_type.rs

1use core::fmt::Display;
2use core::str::FromStr;
3
4use crate::utils::serde::{
5    ByteReader,
6    ByteWriter,
7    Deserializable,
8    DeserializationError,
9    Serializable,
10};
11use crate::{Felt, NoteError};
12
13// CONSTANTS
14// ================================================================================================
15
16// Keep these masks in sync with `miden-lib/asm/miden/kernels/tx/tx.masm`
17const PUBLIC: u8 = 0b01;
18const PRIVATE: u8 = 0b10;
19const ENCRYPTED: u8 = 0b11;
20
21// NOTE TYPE
22// ================================================================================================
23
24#[derive(Clone, Copy, Debug, PartialEq, Eq)]
25#[repr(u8)]
26pub enum NoteType {
27    /// Notes with this type have only their hash published to the network.
28    Private = PRIVATE,
29
30    /// Notes with this type are shared with the network encrypted.
31    Encrypted = ENCRYPTED,
32
33    /// Notes with this type are fully shared with the network.
34    Public = PUBLIC,
35}
36
37// CONVERSIONS FROM NOTE TYPE
38// ================================================================================================
39
40impl From<NoteType> for Felt {
41    fn from(id: NoteType) -> Self {
42        Felt::new(id as u64)
43    }
44}
45
46// CONVERSIONS INTO NOTE TYPE
47// ================================================================================================
48
49impl TryFrom<u8> for NoteType {
50    type Error = NoteError;
51
52    fn try_from(value: u8) -> Result<Self, Self::Error> {
53        match value {
54            PRIVATE => Ok(NoteType::Private),
55            ENCRYPTED => Ok(NoteType::Encrypted),
56            PUBLIC => Ok(NoteType::Public),
57            _ => Err(NoteError::UnknownNoteType(format!("0b{value:b}").into())),
58        }
59    }
60}
61
62impl TryFrom<u16> for NoteType {
63    type Error = NoteError;
64
65    fn try_from(value: u16) -> Result<Self, Self::Error> {
66        Self::try_from(value as u64)
67    }
68}
69
70impl TryFrom<u32> for NoteType {
71    type Error = NoteError;
72
73    fn try_from(value: u32) -> Result<Self, Self::Error> {
74        Self::try_from(value as u64)
75    }
76}
77
78impl TryFrom<u64> for NoteType {
79    type Error = NoteError;
80
81    fn try_from(value: u64) -> Result<Self, Self::Error> {
82        let value: u8 = value
83            .try_into()
84            .map_err(|_| NoteError::UnknownNoteType(format!("0b{value:b}").into()))?;
85        value.try_into()
86    }
87}
88
89impl TryFrom<Felt> for NoteType {
90    type Error = NoteError;
91
92    fn try_from(value: Felt) -> Result<Self, Self::Error> {
93        value.as_int().try_into()
94    }
95}
96
97impl FromStr for NoteType {
98    type Err = NoteError;
99
100    fn from_str(s: &str) -> Result<Self, Self::Err> {
101        match s {
102            "private" => Ok(NoteType::Private),
103            "encrypted" => Ok(NoteType::Encrypted),
104            "public" => Ok(NoteType::Public),
105            _ => Err(NoteError::UnknownNoteType(s.into())),
106        }
107    }
108}
109
110// SERIALIZATION
111// ================================================================================================
112
113impl Serializable for NoteType {
114    fn write_into<W: ByteWriter>(&self, target: &mut W) {
115        (*self as u8).write_into(target)
116    }
117}
118
119impl Deserializable for NoteType {
120    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
121        let discriminant = u8::read_from(source)?;
122
123        let note_type = match discriminant {
124            PRIVATE => NoteType::Private,
125            ENCRYPTED => NoteType::Encrypted,
126            PUBLIC => NoteType::Public,
127            discriminant => {
128                return Err(DeserializationError::InvalidValue(format!(
129                    "discriminant {discriminant} is not a valid NoteType"
130                )));
131            },
132        };
133
134        Ok(note_type)
135    }
136}
137
138// DISPLAY
139// ================================================================================================
140
141impl Display for NoteType {
142    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
143        match self {
144            NoteType::Private => write!(f, "private"),
145            NoteType::Encrypted => write!(f, "encrypted"),
146            NoteType::Public => write!(f, "public"),
147        }
148    }
149}
150
151#[test]
152fn test_from_str_note_type() {
153    use assert_matches::assert_matches;
154
155    use crate::alloc::string::ToString;
156
157    for string in ["private", "public", "encrypted"] {
158        let parsed_note_type = NoteType::from_str(string).unwrap();
159        assert_eq!(parsed_note_type.to_string(), string);
160    }
161
162    let public_type_invalid_err = NoteType::from_str("puBlIc").unwrap_err();
163    assert_matches!(public_type_invalid_err, NoteError::UnknownNoteType(_));
164
165    let encrypted_type_invalid = NoteType::from_str("eNcrYptEd").unwrap_err();
166    assert_matches!(encrypted_type_invalid, NoteError::UnknownNoteType(_));
167
168    let invalid_type = NoteType::from_str("invalid").unwrap_err();
169    assert_matches!(invalid_type, NoteError::UnknownNoteType(_));
170}