miden_objects/notes/note_id.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
use alloc::string::String;
use core::fmt::Display;
use super::{Digest, Felt, Hasher, NoteDetails, Word};
use crate::utils::{
serde::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable},
HexParseError,
};
// NOTE ID
// ================================================================================================
/// Returns a unique identifier of a note, which is simultaneously a commitment to the note.
///
/// Note ID is computed as:
///
/// > hash(recipient, asset_hash),
///
/// where `recipient` is defined as:
///
/// > hash(hash(hash(serial_num, ZERO), script_hash), input_hash)
///
/// This achieves the following properties:
/// - Every note can be reduced to a single unique ID.
/// - To compute a note ID, we do not need to know the note's serial_num. Knowing the hash of the
/// serial_num (as well as script hash, input hash, and note assets) is sufficient.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct NoteId(Digest);
impl NoteId {
/// Returns a new [NoteId] instantiated from the provided note components.
pub fn new(recipient: Digest, asset_commitment: Digest) -> Self {
Self(Hasher::merge(&[recipient, asset_commitment]))
}
/// Returns the elements representation of this note ID.
pub fn as_elements(&self) -> &[Felt] {
self.0.as_elements()
}
/// Returns the byte representation of this note ID.
pub fn as_bytes(&self) -> [u8; 32] {
self.0.as_bytes()
}
/// Returns a big-endian, hex-encoded string.
pub fn to_hex(&self) -> String {
self.0.to_hex()
}
/// Returns the digest defining this note ID.
pub fn inner(&self) -> Digest {
self.0
}
}
impl Display for NoteId {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", self.to_hex())
}
}
// CONVERSIONS INTO NOTE ID
// ================================================================================================
impl From<&NoteDetails> for NoteId {
fn from(note: &NoteDetails) -> Self {
Self::new(note.recipient().digest(), note.assets().commitment())
}
}
impl From<Word> for NoteId {
fn from(value: Word) -> Self {
Self(value.into())
}
}
impl From<Digest> for NoteId {
fn from(value: Digest) -> Self {
Self(value)
}
}
impl NoteId {
/// Attempts to convert from a hexadecimal string to [NoteId].
pub fn try_from_hex(hex_value: &str) -> Result<NoteId, HexParseError> {
Digest::try_from(hex_value).map(NoteId::from)
}
}
// CONVERSIONS FROM NOTE ID
// ================================================================================================
impl From<NoteId> for Digest {
fn from(id: NoteId) -> Self {
id.inner()
}
}
impl From<NoteId> for Word {
fn from(id: NoteId) -> Self {
id.0.into()
}
}
impl From<NoteId> for [u8; 32] {
fn from(id: NoteId) -> Self {
id.0.into()
}
}
impl From<&NoteId> for Word {
fn from(id: &NoteId) -> Self {
id.0.into()
}
}
impl From<&NoteId> for [u8; 32] {
fn from(id: &NoteId) -> Self {
id.0.into()
}
}
// SERIALIZATION
// ================================================================================================
impl Serializable for NoteId {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
target.write_bytes(&self.0.to_bytes());
}
}
impl Deserializable for NoteId {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let id = Digest::read_from(source)?;
Ok(Self(id))
}
}
#[cfg(test)]
mod tests {
use alloc::string::ToString;
use super::NoteId;
#[test]
fn note_id_try_from_hex() {
let note_id_hex = "0xc9d31c82c098e060c9b6e3af2710b3fc5009a1a6f82ef9465f8f35d1f5ba4a80";
let note_id = NoteId::try_from_hex(note_id_hex).unwrap();
assert_eq!(note_id.inner().to_string(), note_id_hex)
}
}