1use super::{
2 AccountId,
3 ByteReader,
4 ByteWriter,
5 Deserializable,
6 DeserializationError,
7 Felt,
8 NoteTag,
9 NoteType,
10 Serializable,
11 Word,
12};
13use crate::Hasher;
14use crate::note::{NoteAttachmentHeader, NoteAttachments};
15
16#[derive(Debug, Clone, Copy, Eq, PartialEq)]
24pub struct PartialNoteMetadata {
25 sender: AccountId,
27
28 note_type: NoteType,
30
31 tag: NoteTag,
33}
34
35impl PartialNoteMetadata {
36 pub fn new(sender: AccountId, note_type: NoteType) -> Self {
44 Self {
45 sender,
46 note_type,
47 tag: NoteTag::default(),
48 }
49 }
50
51 pub fn sender(&self) -> AccountId {
56 self.sender
57 }
58
59 pub fn note_type(&self) -> NoteType {
61 self.note_type
62 }
63
64 pub fn tag(&self) -> NoteTag {
66 self.tag
67 }
68
69 pub fn is_private(&self) -> bool {
71 self.note_type == NoteType::Private
72 }
73
74 pub fn is_public(&self) -> bool {
76 self.note_type == NoteType::Public
77 }
78
79 pub fn set_tag(&mut self, tag: NoteTag) {
84 self.tag = tag;
85 }
86
87 pub fn with_tag(mut self, tag: NoteTag) -> Self {
91 self.tag = tag;
92 self
93 }
94}
95
96impl Serializable for PartialNoteMetadata {
100 fn write_into<W: ByteWriter>(&self, target: &mut W) {
101 self.note_type().write_into(target);
102 self.sender().write_into(target);
103 self.tag().write_into(target);
104 }
105
106 fn get_size_hint(&self) -> usize {
107 self.note_type().get_size_hint()
108 + self.sender().get_size_hint()
109 + self.tag().get_size_hint()
110 }
111}
112
113impl Deserializable for PartialNoteMetadata {
114 fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
115 let note_type = NoteType::read_from(source)?;
116 let sender = AccountId::read_from(source)?;
117 let tag = NoteTag::read_from(source)?;
118
119 Ok(PartialNoteMetadata::new(sender, note_type).with_tag(tag))
120 }
121}
122
123#[derive(Debug, Clone, Copy, Eq, PartialEq)]
152pub struct NoteMetadata {
153 partial_metadata: PartialNoteMetadata,
154 attachment_headers: [NoteAttachmentHeader; NoteAttachments::MAX_COUNT],
155 attachments_commitment: Word,
156}
157
158impl NoteMetadata {
159 const NOTE_TYPE_SHIFT: u64 = 4;
164
165 const VERSION_1: u8 = 1;
170
171 pub fn new(partial_metadata: PartialNoteMetadata, attachments: &NoteAttachments) -> Self {
178 Self::from_parts(partial_metadata, attachments.to_headers(), attachments.to_commitment())
179 }
180
181 pub fn from_parts(
185 partial_metadata: PartialNoteMetadata,
186 attachment_headers: [NoteAttachmentHeader; NoteAttachments::MAX_COUNT],
187 attachments_commitment: Word,
188 ) -> Self {
189 Self {
190 partial_metadata,
191 attachment_headers,
192 attachments_commitment,
193 }
194 }
195
196 pub fn partial_metadata(&self) -> &PartialNoteMetadata {
201 &self.partial_metadata
202 }
203
204 pub fn sender(&self) -> AccountId {
206 self.partial_metadata.sender()
207 }
208
209 pub fn note_type(&self) -> NoteType {
211 self.partial_metadata.note_type()
212 }
213
214 pub fn tag(&self) -> NoteTag {
216 self.partial_metadata.tag()
217 }
218
219 pub fn attachment_headers(&self) -> &[NoteAttachmentHeader; NoteAttachments::MAX_COUNT] {
221 &self.attachment_headers
222 }
223
224 pub fn attachments_commitment(&self) -> Word {
226 self.attachments_commitment
227 }
228
229 pub fn is_private(&self) -> bool {
231 self.partial_metadata.is_private()
232 }
233
234 pub fn is_public(&self) -> bool {
236 self.partial_metadata.is_public()
237 }
238
239 pub fn to_metadata_word(&self) -> Word {
243 let mut word = Word::empty();
244 word[0] = merge_sender_suffix_and_note_type(
245 self.partial_metadata.sender.suffix(),
246 self.partial_metadata.note_type,
247 );
248 word[1] = self.partial_metadata.sender.prefix().as_felt();
249 word[2] = self.partial_metadata.tag.into();
250 word[3] = merge_schemes(self.attachment_headers);
251 word
252 }
253
254 pub fn to_commitment(&self) -> Word {
260 Hasher::merge(&[self.to_metadata_word(), self.attachments_commitment])
261 }
262
263 pub fn into_partial_metadata(self) -> PartialNoteMetadata {
265 self.partial_metadata
266 }
267}
268
269impl Serializable for NoteMetadata {
270 fn write_into<W: ByteWriter>(&self, target: &mut W) {
271 self.partial_metadata.write_into(target);
272
273 let present_headers_iter =
274 self.attachment_headers.iter().filter(|header| !header.is_absent());
275
276 let num_headers_present = u8::try_from(present_headers_iter.clone().count())
277 .expect("num attachments is validated to be at most 4");
278 num_headers_present.write_into(target);
279 target.write_many(present_headers_iter);
280
281 self.attachments_commitment.write_into(target);
282 }
283
284 fn get_size_hint(&self) -> usize {
285 self.partial_metadata.get_size_hint()
286 + core::mem::size_of::<u8>()
287 + self
288 .attachment_headers
289 .iter()
290 .filter(|header| !header.is_absent())
291 .map(NoteAttachmentHeader::get_size_hint)
292 .sum::<usize>()
293 + self.attachments_commitment.get_size_hint()
294 }
295}
296
297impl Deserializable for NoteMetadata {
298 fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
299 let partial_metadata = PartialNoteMetadata::read_from(source)?;
300
301 let num_headers_present = u8::read_from(source)? as usize;
302 if num_headers_present > NoteAttachments::MAX_COUNT {
303 return Err(DeserializationError::InvalidValue(format!(
304 "number of attachment headers ({num_headers_present}) exceeds maximum ({})",
305 NoteAttachments::MAX_COUNT
306 )));
307 }
308
309 let mut attachment_headers = [NoteAttachmentHeader::absent(); NoteAttachments::MAX_COUNT];
310 for header in attachment_headers.iter_mut().take(num_headers_present) {
311 *header = NoteAttachmentHeader::read_from(source)?;
312 }
313
314 let attachment_commitment = Word::read_from(source)?;
315
316 Ok(Self::from_parts(partial_metadata, attachment_headers, attachment_commitment))
317 }
318}
319
320fn merge_sender_suffix_and_note_type(sender_id_suffix: Felt, note_type: NoteType) -> Felt {
336 let mut merged = sender_id_suffix.as_canonical_u64();
337
338 let note_type_byte = note_type as u8;
339 debug_assert!(note_type_byte < 2, "note type must not contain values >= 2");
340 merged |= (note_type_byte as u64) << NoteMetadata::NOTE_TYPE_SHIFT;
342 merged |= NoteMetadata::VERSION_1 as u64;
343
344 Felt::try_from(merged).expect("encoded value should be a valid felt")
347}
348
349fn merge_schemes(headers: [NoteAttachmentHeader; NoteAttachments::MAX_COUNT]) -> Felt {
360 let mut merged: u64 = headers[0].as_u16() as u64;
361 merged |= (headers[1].as_u16() as u64) << 16;
362 merged |= (headers[2].as_u16() as u64) << 32;
363 merged |= (headers[3].as_u16() as u64) << 48;
364
365 Felt::try_from(merged).expect("encoded value should be a valid felt (schemes <= 65534)")
366}
367
368#[cfg(test)]
372mod tests {
373
374 use super::*;
375 use crate::note::{NoteAttachment, NoteAttachmentScheme};
376 use crate::testing::account_id::ACCOUNT_ID_MAX_ONES;
377
378 #[test]
379 fn note_metadata_word_encodes_attachment_header() -> anyhow::Result<()> {
380 let sender = AccountId::try_from(ACCOUNT_ID_MAX_ONES).unwrap();
381 let partial_metadata =
382 PartialNoteMetadata::new(sender, NoteType::Public).with_tag(NoteTag::new(0xff));
383 let attachment0 = NoteAttachment::with_word(
384 NoteAttachmentScheme::new(1)?,
385 Word::from([10, 20, 30, 40u32]),
386 );
387 let attachment1 = NoteAttachment::with_words(
388 NoteAttachmentScheme::new(0xfffe)?,
389 vec![Word::from([10, 20, 30, 40u32]), Word::from([10, 20, 30, 40u32])],
390 )?;
391 let attachments = NoteAttachments::new(vec![attachment0, attachment1])?;
392 let metadata = NoteMetadata::new(partial_metadata, &attachments);
393
394 let encoded = metadata.to_metadata_word();
395
396 let tag = encoded[2].as_canonical_u64();
397 assert_eq!(tag, 0x0000_0000_0000_00ff);
398
399 let schemes = encoded[3].as_canonical_u64();
400 assert_eq!(schemes, 0x0000_0000_fffe_0001);
402
403 Ok(())
404 }
405
406 #[rstest::rstest]
407 #[case::attachment_none([])]
408 #[case::attachment_two_words([
409 NoteAttachment::with_word(NoteAttachmentScheme::none(), Word::from([3, 4, 5, 6u32])),
410 NoteAttachment::with_word(NoteAttachmentScheme::none(), Word::from([3, 4, 5, 6u32])),
411 ])]
412 #[case::attachment_word_and_two_arrays([
413 NoteAttachment::with_word(NoteAttachmentScheme::none(), Word::from([3, 4, 5, 6u32])),
414 NoteAttachment::with_words(
415 NoteAttachmentScheme::MAX,
416 vec![Word::from([5, 5, 5, 5u32]); 2],
417 )?,
418 NoteAttachment::with_words(
419 NoteAttachmentScheme::MAX,
420 vec![Word::from([10, 10, 10, 10u32]); NoteAttachment::MAX_NUM_WORDS as usize],
421 )?,
422 ])]
423 #[test]
424 fn note_metadata_serde(
425 #[case] attachments: impl IntoIterator<Item = NoteAttachment>,
426 ) -> anyhow::Result<()> {
427 let sender = AccountId::try_from(ACCOUNT_ID_MAX_ONES).unwrap();
430 let note_type = NoteType::Public;
431 let tag = NoteTag::new(u32::MAX);
432 let partial_metadata = PartialNoteMetadata::new(sender, note_type).with_tag(tag);
433 let attachments = NoteAttachments::new(attachments.into_iter().collect())?;
434 let metadata = NoteMetadata::new(partial_metadata, &attachments);
435
436 let deserialized = PartialNoteMetadata::read_from_bytes(&partial_metadata.to_bytes())?;
438 assert_eq!(deserialized, partial_metadata);
439
440 let roundtripped = NoteMetadata::read_from_bytes(&metadata.to_bytes())?;
442 assert_eq!(roundtripped, metadata);
443
444 Ok(())
445 }
446
447 #[test]
448 fn note_metadata_header_encodes_v1_as_one() {
449 let sender = AccountId::try_from(ACCOUNT_ID_MAX_ONES).unwrap();
450 let metadata = PartialNoteMetadata::new(sender, NoteType::Private);
451 let metadata = NoteMetadata::new(metadata, &NoteAttachments::default());
452
453 let metadata = metadata.to_metadata_word();
454 let version = metadata[0].as_canonical_u64() & 0b1111;
455
456 assert_eq!(version, NoteMetadata::VERSION_1 as u64);
457 assert_eq!(version, 1);
458 }
459}