Skip to content

Commit

Permalink
Implemented OutputNote::hash (#782)
Browse files Browse the repository at this point in the history
* feat: implement `OutputNote::hash`

* fix: review comments
  • Loading branch information
polydez authored and bobbinth committed Jul 4, 2024
1 parent f9b13a9 commit 0ac6785
Show file tree
Hide file tree
Showing 3 changed files with 24 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()])
compute_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 compute_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::{compute_note_hash, NoteHeader};

mod inputs;
pub use inputs::NoteInputs;
Expand Down
10 changes: 8 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::{compute_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,13 @@ impl OutputNote {
_ => self.clone(),
}
}

/// Returns a commitment to the note and its metadata.
///
/// > hash(NOTE_ID || NOTE_METADATA)
pub fn hash(&self) -> Digest {
compute_note_hash(self.id(), self.metadata())
}
}

// CONVERSIONS
Expand Down

0 comments on commit 0ac6785

Please sign in to comment.