miden_objects/note/
note_type.rs

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