miden_node_utils/
formatting.rs

1use std::fmt::Display;
2
3use itertools::Itertools;
4use miden_objects::{
5    crypto::{
6        hash::{blake::Blake3Digest, Digest},
7        utils::bytes_to_hex_string,
8    },
9    transaction::{InputNoteCommitment, InputNotes, OutputNotes},
10};
11
12pub fn format_account_id(id: u64) -> String {
13    format!("0x{id:x}")
14}
15
16pub fn format_opt<T: Display>(opt: Option<&T>) -> String {
17    opt.map_or("None".to_owned(), ToString::to_string)
18}
19
20pub fn format_input_notes(notes: &InputNotes<InputNoteCommitment>) -> String {
21    format_array(notes.iter().map(|c| match c.header() {
22        Some(header) => format!("({}, {})", c.nullifier().to_hex(), header.id().to_hex()),
23        None => format!("({})", c.nullifier().to_hex()),
24    }))
25}
26
27pub fn format_output_notes(notes: &OutputNotes) -> String {
28    format_array(notes.iter().map(|output_note| {
29        let metadata = output_note.metadata();
30        format!(
31            "{{ note_id: {}, note_metadata: {{sender: {}, tag: {} }}}}",
32            output_note.id().to_hex(),
33            metadata.sender(),
34            metadata.tag(),
35        )
36    }))
37}
38
39pub fn format_map<'a, K: Display + 'a, V: Display + 'a>(
40    map: impl IntoIterator<Item = (&'a K, &'a V)>,
41) -> String {
42    let map_str = map.into_iter().map(|(key, val)| format!("{key}: {val}")).join(", ");
43    if map_str.is_empty() {
44        "None".to_owned()
45    } else {
46        format!("{{ {map_str} }}")
47    }
48}
49
50pub fn format_array(list: impl IntoIterator<Item = impl Display>) -> String {
51    let comma_separated = list.into_iter().join(", ");
52    if comma_separated.is_empty() {
53        "None".to_owned()
54    } else {
55        format!("[{comma_separated}]")
56    }
57}
58
59pub fn format_blake3_digest(digest: Blake3Digest<32>) -> String {
60    bytes_to_hex_string(digest.as_bytes())
61}