Skip to content

Commit

Permalink
feat: implement OutputNote::hash
Browse files Browse the repository at this point in the history
  • Loading branch information
polydez committed Jul 2, 2024
1 parent 6831761 commit 4978633
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
17 changes: 15 additions & 2 deletions objects/src/notes/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,26 @@ impl NoteHeader {
///
/// > hash(NOTE_ID || NOTE_METADATA)
///
/// This value is used primarily for authenticating notes consumed when the are consumed
/// This value is used primarily for authenticating notes consumed when they are consumed
/// in a transaction.
pub fn hash(&self) -> Digest {
Hasher::merge(&[self.id().inner(), Word::from(self.metadata()).into()])
note_hash(self.id(), self.metadata())
}
}

// UTILITIES
// ================================================================================================

/// Returns a commitment to the note and its metadata.
///
/// > hash(NOTE_ID || NOTE_METADATA)
///
/// This value is used primarily for authenticating notes consumed when they are consumed
/// in a transaction.
pub fn note_hash(id: NoteId, metadata: &NoteMetadata) -> Digest {
Hasher::merge(&[id.inner(), Word::from(metadata).into()])
}

// CONVERSIONS FROM NOTE HEADER
// ================================================================================================

Expand Down
2 changes: 1 addition & 1 deletion objects/src/notes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ mod details;
pub use details::NoteDetails;

mod header;
pub use header::NoteHeader;
pub use header::{note_hash, NoteHeader};

mod inputs;
pub use inputs::NoteInputs;
Expand Down
13 changes: 11 additions & 2 deletions objects/src/transaction/outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ use vm_processor::DeserializationError;

use crate::{
accounts::AccountStub,
notes::{Note, NoteAssets, NoteHeader, NoteId, NoteMetadata, PartialNote},
notes::{note_hash, Note, NoteAssets, NoteHeader, NoteId, NoteMetadata, PartialNote},
Digest, Felt, Hasher, TransactionOutputError, Word, MAX_OUTPUT_NOTES_PER_TX,
};

// TRANSACTION OUTPUTS
// ================================================================================================

Expand Down Expand Up @@ -205,6 +204,16 @@ impl OutputNote {
_ => self.clone(),
}
}

/// Returns a commitment to the note and its metadata.
///
/// > hash(NOTE_ID || NOTE_METADATA)
///
/// This value is used primarily for authenticating notes consumed when they are consumed
/// in a transaction.
pub fn hash(&self) -> Digest {
note_hash(self.id(), self.metadata())
}
}

// CONVERSIONS
Expand Down

0 comments on commit 4978633

Please sign in to comment.