wallet_adapter/wallet_ser_der/standard_features/
connect.rs1use web_sys::{js_sys, wasm_bindgen::JsValue};
2
3use crate::{
4 Reflection, SemverVersion, StandardFunction, WalletAccount, WalletError, WalletResult,
5};
6
7#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
10pub struct Connect(StandardFunction);
11
12impl Connect {
13 pub(crate) fn new(reflection: &Reflection, version: SemverVersion) -> WalletResult<Self> {
15 Ok(Self(StandardFunction::new(
16 reflection, version, "connect", "standard",
17 )?))
18 }
19
20 pub(crate) async fn call_connect(&self) -> WalletResult<WalletAccount> {
22 let outcome = self.0.callback.call0(&JsValue::from_bool(false))?;
23
24 let outcome = js_sys::Promise::resolve(&outcome);
25
26 wasm_bindgen_futures::JsFuture::from(outcome)
27 .await
28 .map(|success| {
29 let get_accounts = Reflection::new(success)?.reflect_js_array("accounts")?;
30
31 let wallet_account = get_accounts
32 .into_iter()
33 .map(|raw_account| WalletAccount::parse(Reflection::new(raw_account)?))
34 .collect::<WalletResult<Vec<WalletAccount>>>()
35 .map(|mut accounts| {
36 if accounts.is_empty() {
37 Err(WalletError::ConnectHasNoAccounts)
38 } else {
39 Ok(accounts.remove(0))
40 }
41 })??;
42
43 Ok(wallet_account)
44 })
45 .map_err(|error| {
46 let value: WalletError = error.into();
47
48 WalletError::WalletConnectError(value.to_string())
49 })?
50 }
51}