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
use miden_client::store::{InputNoteRecord, NoteFilter, OutputNoteRecord};
use miden_objects::{notes::NoteId, Digest};
use serde::{Deserialize, Serialize};
use serde_wasm_bindgen::from_value;
use wasm_bindgen::prelude::*;

use crate::WebClient;

#[derive(Serialize, Deserialize)]
pub enum WebClientNoteFilter {
    All,
    Consumed,
    Committed,
    Expected,
    Processing,
}

#[wasm_bindgen]
impl WebClient {
    pub async fn get_input_notes(&mut self, filter: JsValue) -> Result<JsValue, JsValue> {
        if let Some(client) = self.get_mut_inner() {
            let filter: WebClientNoteFilter = from_value(filter).unwrap();
            let native_filter = match filter {
                WebClientNoteFilter::All => NoteFilter::All,
                WebClientNoteFilter::Consumed => NoteFilter::Consumed,
                WebClientNoteFilter::Committed => NoteFilter::Committed,
                WebClientNoteFilter::Expected => NoteFilter::Expected,
                WebClientNoteFilter::Processing => NoteFilter::Processing,
            };

            let notes: Vec<InputNoteRecord> = client.get_input_notes(native_filter).await.unwrap();
            let note_ids = notes.iter().map(|note| note.id().to_string()).collect::<Vec<String>>();

            serde_wasm_bindgen::to_value(&note_ids).map_err(|e| JsValue::from_str(&e.to_string()))
        } else {
            Err(JsValue::from_str("Client not initialized"))
        }
    }

    pub async fn get_input_note(&mut self, note_id: String) -> Result<JsValue, JsValue> {
        if let Some(client) = self.get_mut_inner() {
            let note_id: NoteId = Digest::try_from(note_id)
                .map_err(|err| format!("Failed to parse input note id: {}", err))?
                .into();
            let note: InputNoteRecord = client.get_input_note(note_id).await.unwrap();

            serde_wasm_bindgen::to_value(&note.id().to_string())
                .map_err(|e| JsValue::from_str(&e.to_string()))
        } else {
            Err(JsValue::from_str("Client not initialized"))
        }
    }

    pub async fn get_output_notes(&mut self, filter: JsValue) -> Result<JsValue, JsValue> {
        if let Some(client) = self.get_mut_inner() {
            let filter: WebClientNoteFilter = from_value(filter).unwrap();
            let native_filter = match filter {
                WebClientNoteFilter::All => NoteFilter::All,
                WebClientNoteFilter::Consumed => NoteFilter::Consumed,
                WebClientNoteFilter::Committed => NoteFilter::Committed,
                WebClientNoteFilter::Expected => NoteFilter::Expected,
                WebClientNoteFilter::Processing => NoteFilter::Processing,
            };

            let notes: Vec<OutputNoteRecord> =
                client.get_output_notes(native_filter).await.unwrap();
            let note_ids = notes.iter().map(|note| note.id().to_string()).collect::<Vec<String>>();

            serde_wasm_bindgen::to_value(&note_ids).map_err(|e| JsValue::from_str(&e.to_string()))
        } else {
            Err(JsValue::from_str("Client not initialized"))
        }
    }

    pub async fn get_output_note(&mut self, note_id: String) -> Result<JsValue, JsValue> {
        if let Some(client) = self.get_mut_inner() {
            let note_id: NoteId = Digest::try_from(note_id)
                .map_err(|err| format!("Failed to parse output note id: {}", err))?
                .into();
            let note: OutputNoteRecord = client.get_output_note(note_id).await.unwrap();

            serde_wasm_bindgen::to_value(&note.id().to_string())
                .map_err(|e| JsValue::from_str(&e.to_string()))
        } else {
            Err(JsValue::from_str("Client not initialized"))
        }
    }
}