-
Notifications
You must be signed in to change notification settings - Fork 47
/
mod.rs
246 lines (196 loc) · 6.94 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
use core::ops::Deref;
use miden_crypto::{
utils::{ByteReader, ByteWriter, Deserializable, Serializable},
Word,
};
use vm_processor::DeserializationError;
use crate::{accounts::AccountId, assets::Asset, Digest, Felt, Hasher, NoteError, WORD_SIZE, ZERO};
mod assets;
pub use assets::NoteAssets;
mod details;
pub use details::NoteDetails;
mod header;
pub use header::{compute_note_hash, NoteHeader};
mod inputs;
pub use inputs::NoteInputs;
mod metadata;
pub use metadata::NoteMetadata;
mod execution_hint;
pub use execution_hint::NoteExecutionHint;
mod note_id;
pub use note_id::NoteId;
mod note_tag;
pub use note_tag::{NoteExecutionMode, NoteTag};
mod note_type;
pub use note_type::NoteType;
mod nullifier;
pub use nullifier::Nullifier;
mod location;
pub use location::{NoteInclusionProof, NoteLocation};
mod partial;
pub use partial::PartialNote;
mod recipient;
pub use recipient::NoteRecipient;
mod script;
pub use script::NoteScript;
mod file;
pub use file::NoteFile;
// NOTE
// ================================================================================================
/// A note with all the data required for it to be consumed by executing it against the transaction
/// kernel.
///
/// Notes consist of note metadata and details. Note metadata is always public, but details may be
/// either public, encrypted, or private, depending on the note type. Note details consist of note
/// assets, script, inputs, and a serial number, the three latter grouped into a recipient object.
///
/// Note details can be reduced to two unique identifiers: [NoteId] and [Nullifier]. The former is
/// publicly associated with a note, while the latter is known only to entities which have access
/// to full note details.
///
/// Fungible and non-fungible asset transfers are done by moving assets to the note's assets. The
/// note's script determines the conditions required for the note consumption, i.e. the target
/// account of a P2ID or conditions of a SWAP, and the effects of the note. The serial number has
/// a double duty of preventing double spend, and providing unlikability to the consumer of a note.
/// The note's inputs allow for customization of its script.
///
/// To create a note, the kernel does not require all the information above, a user can create a
/// note only with the commitment to the script, inputs, the serial number (i.e., the recipient),
/// and the kernel only verifies the source account has the assets necessary for the note creation.
/// See [NoteRecipient] for more details.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Note {
header: NoteHeader,
details: NoteDetails,
nullifier: Nullifier,
}
impl Note {
// CONSTRUCTOR
// --------------------------------------------------------------------------------------------
/// Returns a new [Note] created with the specified parameters.
pub fn new(assets: NoteAssets, metadata: NoteMetadata, recipient: NoteRecipient) -> Self {
let details = NoteDetails::new(assets, recipient);
let header = NoteHeader::new(details.id(), metadata);
let nullifier = details.nullifier();
Self { header, details, nullifier }
}
// PUBLIC ACCESSORS
// --------------------------------------------------------------------------------------------
/// Returns the note's header.
pub fn header(&self) -> &NoteHeader {
&self.header
}
/// Returns the note's unique identifier.
///
/// This value is both an unique identifier and a commitment to the note.
pub fn id(&self) -> NoteId {
self.header.id()
}
/// Returns the note's metadata.
pub fn metadata(&self) -> &NoteMetadata {
self.header.metadata()
}
/// Returns the note's assets.
pub fn assets(&self) -> &NoteAssets {
self.details.assets()
}
/// Returns the note's recipient serial_num, the secret required to consume the note.
pub fn serial_num(&self) -> Word {
self.details.serial_num()
}
/// Returns the note's recipient script which locks the assets of this note.
pub fn script(&self) -> &NoteScript {
self.details.script()
}
/// Returns the note's recipient inputs which customizes the script's behavior.
pub fn inputs(&self) -> &NoteInputs {
self.details.inputs()
}
/// Returns the note's recipient.
pub fn recipient(&self) -> &NoteRecipient {
self.details.recipient()
}
/// Returns the note's nullifier.
///
/// This is public data, used to prevent double spend.
pub fn nullifier(&self) -> Nullifier {
self.nullifier
}
/// Returns a commitment to the note and its metadata.
///
/// > hash(NOTE_ID || NOTE_METADATA)
///
/// This value is used primarily for authenticating notes consumed when the are consumed
/// in a transaction.
pub fn hash(&self) -> Digest {
self.header.hash()
}
}
// DEREFERENCING
// ================================================================================================
impl Deref for Note {
type Target = NoteDetails;
fn deref(&self) -> &Self::Target {
&self.details
}
}
// CONVERSIONS FROM NOTE
// ================================================================================================
impl From<&Note> for NoteHeader {
fn from(note: &Note) -> Self {
note.header
}
}
impl From<Note> for NoteHeader {
fn from(note: Note) -> Self {
note.header
}
}
impl From<&Note> for NoteDetails {
fn from(note: &Note) -> Self {
note.details.clone()
}
}
impl From<Note> for NoteDetails {
fn from(note: Note) -> Self {
note.details
}
}
impl From<Note> for PartialNote {
fn from(note: Note) -> Self {
let (assets, recipient, ..) = note.details.into_parts();
PartialNote::new(*note.header.metadata(), recipient.digest(), assets)
}
}
impl From<&Note> for PartialNote {
fn from(note: &Note) -> Self {
PartialNote::new(
*note.header.metadata(),
note.details.recipient().digest(),
note.details.assets().clone(),
)
}
}
// SERIALIZATION
// ================================================================================================
impl Serializable for Note {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
let Self {
header,
details,
// nullifier is not serialized as it can be computed from the rest of the data
nullifier: _,
} = self;
// only metadata is serialized as note ID can be computed from note details
header.metadata().write_into(target);
details.write_into(target);
}
}
impl Deserializable for Note {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let metadata = NoteMetadata::read_from(source)?;
let details = NoteDetails::read_from(source)?;
let (assets, recipient) = details.into_parts();
Ok(Self::new(assets, metadata, recipient))
}
}