unc/commands/account/import_account/using_web_wallet/
mod.rs

1#[derive(Debug, Clone, interactive_clap::InteractiveClap)]
2#[interactive_clap(input_context = crate::GlobalContext)]
3#[interactive_clap(output_context = LoginFromWebWalletContext)]
4pub struct LoginFromWebWallet {
5    #[interactive_clap(named_arg)]
6    /// Select network
7    network_config: crate::network::Network,
8}
9
10#[derive(Clone)]
11pub struct LoginFromWebWalletContext(crate::network::NetworkContext);
12
13impl LoginFromWebWalletContext {
14    pub fn from_previous_context(
15        previous_context: crate::GlobalContext,
16        _scope: &<LoginFromWebWallet as interactive_clap::ToInteractiveClapContextScope>::InteractiveClapContextScope,
17    ) -> color_eyre::eyre::Result<Self> {
18        let on_after_getting_network_callback: crate::network::OnAfterGettingNetworkCallback =
19            std::sync::Arc::new({
20                let config = previous_context.config.clone();
21
22                move |network_config| {
23                    let key_pair_properties: crate::common::KeyPairProperties =
24                        crate::common::generate_ed25519_keypair()?;
25                    let mut url: url::Url = network_config.wallet_url.join("login/")?;
26                    url.query_pairs_mut()
27                        .append_pair("title", "unc CLI")
28                        .append_pair("public_key", &key_pair_properties.public_key_str);
29                    // Use `success_url` once capture mode is implemented
30                    //.append_pair("success_url", "http://127.0.0.1:8080");
31                    eprintln!(
32                        "If your browser doesn't automatically open, please visit this URL:\n {}\n",
33                        &url.as_str()
34                    );
35                    // url.open();
36                    open::that(url.as_ref()).ok();
37
38                    let key_pair_properties_buf = serde_json::to_string(&key_pair_properties)?;
39                    let error_message = format!("\nIt is currently not possible to verify the account access key.\nYou may not be logged in to {} or you may have entered an incorrect account_id.\nYou have the option to reconfirm your account or save your access key information.\n", &url.as_str());
40                    super::login(
41                        network_config.clone(),
42                        config.credentials_home_dir.clone(),
43                        &key_pair_properties_buf,
44                        &key_pair_properties.public_key_str,
45                        &error_message,
46                    )
47                }
48            });
49
50        Ok(Self(crate::network::NetworkContext {
51            config: previous_context.config,
52            interacting_with_account_ids: Vec::new(),
53            on_after_getting_network_callback,
54        }))
55    }
56}
57
58impl From<LoginFromWebWalletContext> for crate::network::NetworkContext {
59    fn from(item: LoginFromWebWalletContext) -> Self {
60        item.0
61    }
62}