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
use js_export_macro::js_export;
use miden_client::note::NoteId as NativeNoteId;
use super::word::Word;
use crate::js_error_with_context;
use crate::platform::JsErr;
/// Returns a unique identifier of a note, which is simultaneously a commitment to the note.
///
/// Note ID is computed as:
///
/// > `hash(details_commitment, metadata_commitment)`
///
/// On the 0.15 protocol surface the upstream `NoteId::new` signature
/// changed from `(recipient_digest, asset_commitment)` to
/// `(NoteDetailsCommitment, &NoteMetadata)`. The JS API exposes
/// `NoteId.fromRaw(word)` for constructing an ID from a pre-computed
/// 32-byte commitment word (the previous two-Word constructor has no
/// 0.15 equivalent).
#[derive(Clone, Copy)]
#[js_export]
pub struct NoteId(NativeNoteId);
#[js_export]
impl NoteId {
/// Builds a note ID from its raw commitment word.
///
/// `word` must already encode the final note-ID commitment — the
/// metadata-mixing that the previous 2-Word constructor did is no
/// longer part of the protocol surface.
#[js_export(js_name = "fromRaw")]
pub fn from_raw(word: &Word) -> NoteId {
let native_word: miden_client::Word = word.into();
NoteId(NativeNoteId::from_raw(native_word))
}
/// Parses a note ID from its hex encoding.
#[js_export(js_name = "fromHex")]
pub fn from_hex(hex: String) -> Result<NoteId, JsErr> {
let native_note_id = NativeNoteId::try_from_hex(&hex)
.map_err(|err| js_error_with_context(err, "error instantiating NoteId from hex"))?;
Ok(NoteId(native_note_id))
}
/// Returns the canonical hex representation of the note ID.
#[js_export(js_name = "toString")]
#[allow(clippy::inherent_to_string)]
pub fn to_string(&self) -> String {
self.0.to_string()
}
}
// CONVERSIONS
// ================================================================================================
impl From<NativeNoteId> for NoteId {
fn from(native_note_id: NativeNoteId) -> Self {
NoteId(native_note_id)
}
}
impl From<&NativeNoteId> for NoteId {
fn from(native_note_id: &NativeNoteId) -> Self {
NoteId(*native_note_id)
}
}
impl From<NoteId> for NativeNoteId {
fn from(note_id: NoteId) -> Self {
note_id.0
}
}
impl From<&NoteId> for NativeNoteId {
fn from(note_id: &NoteId) -> Self {
note_id.0
}
}
impl_napi_from_value!(NoteId);