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
use miden_client::accounts::AccountTemplate;
use miden_objects::{accounts::AccountStorageType, assets::TokenSymbol};
use wasm_bindgen::prelude::*;

use crate::WebClient;

#[wasm_bindgen]
impl WebClient {
    pub async fn new_wallet(
        &mut self,
        storage_type: String,
        mutable: bool,
    ) -> Result<JsValue, JsValue> {
        if let Some(client) = self.get_mut_inner() {
            let client_template = AccountTemplate::BasicWallet {
                mutable_code: mutable,
                storage_type: match storage_type.as_str() {
                    "OffChain" => AccountStorageType::OffChain,
                    "OnChain" => AccountStorageType::OnChain,
                    _ => return Err(JsValue::from_str("Invalid storage mode")),
                },
            };

            match client.new_account(client_template).await {
                Ok((account, _)) => serde_wasm_bindgen::to_value(&account.id().to_string())
                    .map_err(|e| JsValue::from_str(&e.to_string())),
                Err(err) => {
                    let error_message = format!("Failed to create new account: {:?}", err);
                    Err(JsValue::from_str(&error_message))
                },
            }
        } else {
            Err(JsValue::from_str("Client not initialized"))
        }
    }

    pub async fn new_faucet(
        &mut self,
        storage_type: String,
        non_fungible: bool,
        token_symbol: String,
        decimals: String,
        max_supply: String,
    ) -> Result<JsValue, JsValue> {
        if non_fungible {
            return Err(JsValue::from_str("Non-fungible faucets are not supported yet"));
        }

        if let Some(client) = self.get_mut_inner() {
            let client_template = AccountTemplate::FungibleFaucet {
                token_symbol: TokenSymbol::new(&token_symbol)
                    .map_err(|e| JsValue::from_str(&e.to_string()))?,
                decimals: decimals.parse::<u8>().map_err(|e| JsValue::from_str(&e.to_string()))?,
                max_supply: max_supply
                    .parse::<u64>()
                    .map_err(|e| JsValue::from_str(&e.to_string()))?,
                storage_type: match storage_type.as_str() {
                    "OffChain" => AccountStorageType::OffChain,
                    "OnChain" => AccountStorageType::OnChain,
                    _ => return Err(JsValue::from_str("Invalid storage mode")),
                },
            };

            match client.new_account(client_template).await {
                Ok((account, _)) => serde_wasm_bindgen::to_value(&account.id().to_string())
                    .map_err(|e| JsValue::from_str(&e.to_string())),
                Err(err) => {
                    let error_message = format!("Failed to create new account: {:?}", err);
                    Err(JsValue::from_str(&error_message))
                },
            }
        } else {
            Err(JsValue::from_str("Client not initialized"))
        }
    }
}