miden_objects/note/
note_type.rs

1use crate::{
2    utils::serde::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable},
3    Felt, NoteError,
4};
5
6// CONSTANTS
7// ================================================================================================
8
9// Keep these masks in sync with `miden-lib/asm/miden/kernels/tx/tx.masm`
10const PUBLIC: u8 = 0b01;
11const PRIVATE: u8 = 0b10;
12const ENCRYPTED: u8 = 0b11;
13
14// NOTE TYPE
15// ================================================================================================
16
17#[derive(Clone, Copy, Debug, PartialEq, Eq)]
18#[repr(u8)]
19pub enum NoteType {
20    /// Notes with this type have only their hash published to the network.
21    Private = PRIVATE,
22
23    /// Notes with this type are shared with the network encrypted.
24    Encrypted = ENCRYPTED,
25
26    /// Notes with this type are fully shared with the network.
27    Public = PUBLIC,
28}
29
30// CONVERSIONS FROM NOTE TYPE
31// ================================================================================================
32
33impl From<NoteType> for Felt {
34    fn from(id: NoteType) -> Self {
35        Felt::new(id as u64)
36    }
37}
38
39// CONVERSIONS INTO NOTE TYPE
40// ================================================================================================
41
42impl TryFrom<u8> for NoteType {
43    type Error = NoteError;
44
45    fn try_from(value: u8) -> Result<Self, Self::Error> {
46        match value {
47            PRIVATE => Ok(NoteType::Private),
48            ENCRYPTED => Ok(NoteType::Encrypted),
49            PUBLIC => Ok(NoteType::Public),
50            _ => Err(NoteError::InvalidNoteType(value.into())),
51        }
52    }
53}
54
55impl TryFrom<u16> for NoteType {
56    type Error = NoteError;
57
58    fn try_from(value: u16) -> Result<Self, Self::Error> {
59        Self::try_from(value as u64)
60    }
61}
62
63impl TryFrom<u32> for NoteType {
64    type Error = NoteError;
65
66    fn try_from(value: u32) -> Result<Self, Self::Error> {
67        Self::try_from(value as u64)
68    }
69}
70
71impl TryFrom<u64> for NoteType {
72    type Error = NoteError;
73
74    fn try_from(value: u64) -> Result<Self, Self::Error> {
75        let value: u8 = value.try_into().map_err(|_| NoteError::InvalidNoteType(value))?;
76        value.try_into()
77    }
78}
79
80impl TryFrom<Felt> for NoteType {
81    type Error = NoteError;
82
83    fn try_from(value: Felt) -> Result<Self, Self::Error> {
84        value.as_int().try_into()
85    }
86}
87
88// SERIALIZATION
89// ================================================================================================
90
91impl Serializable for NoteType {
92    fn write_into<W: ByteWriter>(&self, target: &mut W) {
93        (*self as u8).write_into(target)
94    }
95}
96
97impl Deserializable for NoteType {
98    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
99        let discriminat = u8::read_from(source)?;
100
101        let note_type = match discriminat {
102            PRIVATE => NoteType::Private,
103            ENCRYPTED => NoteType::Encrypted,
104            PUBLIC => NoteType::Public,
105            v => {
106                return Err(DeserializationError::InvalidValue(format!(
107                    "value {} is not a valid NoteType",
108                    v
109                )))
110            },
111        };
112
113        Ok(note_type)
114    }
115}