From 0ac67851120e535e54798708668b8cf487f866e5 Mon Sep 17 00:00:00 2001 From: polydez <155382956+polydez@users.noreply.github.com> Date: Tue, 2 Jul 2024 19:44:42 +0500 Subject: [PATCH] Implemented `OutputNote::hash` (#782) * feat: implement `OutputNote::hash` * fix: review comments --- objects/src/notes/header.rs | 17 +++++++++++++++-- objects/src/notes/mod.rs | 2 +- objects/src/transaction/outputs.rs | 10 ++++++++-- 3 files changed, 24 insertions(+), 5 deletions(-) diff --git a/objects/src/notes/header.rs b/objects/src/notes/header.rs index feb6feb01..4cc7247a2 100644 --- a/objects/src/notes/header.rs +++ b/objects/src/notes/header.rs @@ -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 // ================================================================================================ diff --git a/objects/src/notes/mod.rs b/objects/src/notes/mod.rs index 6d25cf219..e7dd6aca6 100644 --- a/objects/src/notes/mod.rs +++ b/objects/src/notes/mod.rs @@ -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; diff --git a/objects/src/transaction/outputs.rs b/objects/src/transaction/outputs.rs index d5d4f5a52..1e0c04ac8 100644 --- a/objects/src/transaction/outputs.rs +++ b/objects/src/transaction/outputs.rs @@ -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 // ================================================================================================ @@ -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