-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathaccount.rs
78 lines (69 loc) · 2.66 KB
/
account.rs
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
use miden_client::store::AccountRecord;
use miden_objects::account::Account as NativeAccount;
use wasm_bindgen::prelude::*;
use crate::{
models::{
account::Account, account_header::AccountHeader, account_id::AccountId,
auth_secret_key::AuthSecretKey,
},
WebClient,
};
#[wasm_bindgen]
impl WebClient {
pub async fn get_accounts(&mut self) -> Result<Vec<AccountHeader>, JsValue> {
if let Some(client) = self.get_mut_inner() {
let result = client
.get_account_headers()
.await
.map_err(|err| JsValue::from_str(&format!("Failed to get accounts: {err}")))?;
Ok(result.into_iter().map(|(header, _)| header.into()).collect())
} else {
Err(JsValue::from_str("Client not initialized"))
}
}
pub async fn get_account(
&mut self,
account_id: &AccountId,
) -> Result<Option<Account>, JsValue> {
if let Some(client) = self.get_mut_inner() {
let result = client
.get_account(account_id.into())
.await
.map_err(|err| JsValue::from_str(&format!("Failed to get account: {err}")))?;
let account: Option<NativeAccount> = result.map(AccountRecord::into);
Ok(account.map(miden_client::account::Account::into))
} else {
Err(JsValue::from_str("Client not initialized"))
}
}
pub async fn get_account_auth(
&mut self,
account_id: &AccountId,
) -> Result<Option<AuthSecretKey>, JsValue> {
if let Some(client) = self.get_mut_inner() {
let native_auth_secret_key = client
.get_account_auth(account_id.into())
.await
.map_err(|err| JsValue::from_str(&format!("Failed to get account auth: {err}")))?;
Ok(native_auth_secret_key.map(miden_client::auth::AuthSecretKey::into))
} else {
Err(JsValue::from_str("Client not initialized"))
}
}
pub async fn fetch_and_cache_account_auth_by_pub_key(
&mut self,
account_id: &AccountId,
) -> Result<Option<AuthSecretKey>, JsValue> {
if let Some(store) = &self.store {
let native_auth_secret_key = store
.fetch_and_cache_account_auth_by_pub_key(&account_id.to_string())
.await
.map_err(|err| {
JsValue::from_str(&format!("Failed to fetch and cache account auth: {err}"))
})?;
Ok(native_auth_secret_key.map(miden_client::auth::AuthSecretKey::into))
} else {
Err(JsValue::from_str("Client not initialized"))
}
}
}