miden_objects/note/
recipient.rs

1use alloc::vec::Vec;
2use core::fmt::Debug;
3
4use miden_crypto::Felt;
5
6use super::{
7    ByteReader, ByteWriter, Deserializable, DeserializationError, Digest, Hasher, NoteInputs,
8    NoteScript, Serializable, Word,
9};
10
11/// Value that describes under which condition a note can be consumed.
12///
13/// The recipient is not an account address, instead it is a value that describes when a note
14/// can be consumed. Because not all notes have predetermined consumer addresses, e.g. swap
15/// notes can be consumed by anyone, the recipient is defined as the code and its inputs, that
16/// when successfully executed results in the note's consumption.
17///
18/// Recipient is computed as:
19///
20/// > hash(hash(hash(serial_num, [0; 4]), script_hash), input_hash)
21#[derive(Clone, Debug, PartialEq, Eq)]
22pub struct NoteRecipient {
23    serial_num: Word,
24    script: NoteScript,
25    inputs: NoteInputs,
26    digest: Digest,
27}
28
29impl NoteRecipient {
30    pub fn new(serial_num: Word, script: NoteScript, inputs: NoteInputs) -> Self {
31        let digest = compute_recipient_digest(serial_num, &script, &inputs);
32        Self { serial_num, script, inputs, digest }
33    }
34
35    // PUBLIC ACCESSORS
36    // --------------------------------------------------------------------------------------------
37
38    /// The recipient's serial_num, the secret required to consume the note.
39    pub fn serial_num(&self) -> Word {
40        self.serial_num
41    }
42
43    /// The recipients's script which locks the assets of this note.
44    pub fn script(&self) -> &NoteScript {
45        &self.script
46    }
47
48    /// The recipient's inputs which customizes the script's behavior.
49    pub fn inputs(&self) -> &NoteInputs {
50        &self.inputs
51    }
52
53    /// The recipient's digest, which commits to its details.
54    ///
55    /// This is the public data required to create a note.
56    pub fn digest(&self) -> Digest {
57        self.digest
58    }
59
60    /// Returns the recipient encoded as [Felt]s.
61    pub fn to_elements(&self) -> Vec<Felt> {
62        let mut result = Vec::with_capacity(12);
63        result.extend(self.inputs.commitment());
64        result.extend(self.script.hash());
65        result.extend(self.serial_num);
66        result
67    }
68}
69
70fn compute_recipient_digest(serial_num: Word, script: &NoteScript, inputs: &NoteInputs) -> Digest {
71    let serial_num_hash = Hasher::merge(&[serial_num.into(), Digest::default()]);
72    let merge_script = Hasher::merge(&[serial_num_hash, script.hash()]);
73    Hasher::merge(&[merge_script, inputs.commitment()])
74}
75
76// SERIALIZATION
77// ================================================================================================
78
79impl Serializable for NoteRecipient {
80    fn write_into<W: ByteWriter>(&self, target: &mut W) {
81        let Self {
82            script,
83            inputs,
84            serial_num,
85
86            // These attributes don't have to be serialized, they can be re-computed from the rest
87            // of the data
88            digest: _,
89        } = self;
90
91        script.write_into(target);
92        inputs.write_into(target);
93        serial_num.write_into(target);
94    }
95}
96
97impl Deserializable for NoteRecipient {
98    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
99        let script = NoteScript::read_from(source)?;
100        let inputs = NoteInputs::read_from(source)?;
101        let serial_num = Word::read_from(source)?;
102
103        Ok(Self::new(serial_num, script, inputs))
104    }
105}