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
use js_export_macro::js_export;
use miden_client::asset::Asset as NativeAsset;
use miden_client::note::NoteAssets as NativeNoteAssets;
use super::fungible_asset::FungibleAsset;
/// An asset container for a note.
///
/// A note must contain at least 1 asset and can contain up to `MAX_ASSETS_PER_NOTE` = 16 assets
/// (`miden_protocol::constants`). No duplicates are allowed, but the order of assets is
/// unspecified.
///
/// Note for JS callers: the constructors below `unwrap` the protocol's `TooManyAssets` error, so
/// exceeding the cap from JavaScript traps the WASM instance rather than surfacing a catchable
/// error. Check the length before constructing.
///
/// All the assets in a note can be reduced to a single commitment which is computed by sequentially
/// hashing the assets. Note that the same list of assets can result in two different commitments if
/// the asset ordering is different.
#[derive(Clone)]
#[js_export]
pub struct NoteAssets(NativeNoteAssets);
#[js_export]
impl NoteAssets {
/// Creates a new asset list for a note.
#[js_export(constructor)]
pub fn new(assets_array: Option<Vec<FungibleAsset>>) -> NoteAssets {
let assets = assets_array.unwrap_or_default();
let native_assets: Vec<NativeAsset> = assets.into_iter().map(Into::into).collect();
NoteAssets(NativeNoteAssets::new(native_assets).unwrap())
}
/// Adds a fungible asset to the collection.
pub fn push(&mut self, asset: &FungibleAsset) {
let mut assets: Vec<miden_client::asset::Asset> = self.0.iter().copied().collect();
assets.push(asset.into());
self.0 = NativeNoteAssets::new(assets).unwrap();
}
/// Returns all fungible assets contained in the note.
#[js_export(js_name = "fungibleAssets")]
pub fn fungible_assets(&self) -> Vec<FungibleAsset> {
self.0
.iter()
.filter_map(|asset| {
if asset.is_fungible() {
Some(asset.unwrap_fungible().into())
} else {
None
}
})
.collect()
}
}
// CONVERSIONS
// ================================================================================================
impl From<NativeNoteAssets> for NoteAssets {
fn from(native_note_assets: NativeNoteAssets) -> Self {
NoteAssets(native_note_assets)
}
}
impl From<&NativeNoteAssets> for NoteAssets {
fn from(native_note_assets: &NativeNoteAssets) -> Self {
NoteAssets(native_note_assets.clone())
}
}
impl From<NoteAssets> for NativeNoteAssets {
fn from(note_assets: NoteAssets) -> Self {
note_assets.0
}
}
impl From<&NoteAssets> for NativeNoteAssets {
fn from(note_assets: &NoteAssets) -> Self {
note_assets.0.clone()
}
}