Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add NoteFile object #721

Merged
merged 11 commits into from
Jun 3, 2024
50 changes: 50 additions & 0 deletions objects/src/notes/file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use vm_core::utils::{ByteReader, ByteWriter, Deserializable, Serializable};
use vm_processor::DeserializationError;

use super::{Note, NoteDetails, NoteInclusionProof};

// NOTE FILE
// ================================================================================================

/// A serialized representation of a note.
pub enum NoteFile {
bobbinth marked this conversation as resolved.
Show resolved Hide resolved
/// The note has not yet been recorded on chain.
Details(NoteDetails),
/// The note has been recorded on chain.
Recorded(Note, NoteInclusionProof),
}
bobbinth marked this conversation as resolved.
Show resolved Hide resolved

// SERIALIZATION
// ================================================================================================

impl Serializable for NoteFile {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
match self {
NoteFile::Details(details) => {
target.write_u8(0);
details.write_into(target);
},
NoteFile::Recorded(note, proof) => {
target.write_u8(1);
note.write_into(target);
proof.write_into(target);
},
}
bobbinth marked this conversation as resolved.
Show resolved Hide resolved
}
}

impl Deserializable for NoteFile {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
match source.read_u8()? {
0 => Ok(NoteFile::Details(NoteDetails::read_from(source)?)),
1 => {
let note = Note::read_from(source)?;
let proof = NoteInclusionProof::read_from(source)?;
Ok(NoteFile::Recorded(note, proof))
},
v => {
Err(DeserializationError::InvalidValue(format!("Unknown variant {v} for NoteFile")))
},
}
}
}
3 changes: 3 additions & 0 deletions objects/src/notes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ pub use recipient::NoteRecipient;
mod script;
pub use script::NoteScript;

mod file;
pub use file::NoteFile;

// CONSTANTS
// ================================================================================================

Expand Down
Loading