Skip to main content

lb_rs/service/
sub_account.rs

1use crate::Lb;
2use crate::io::server::Server;
3use crate::model::account::{Account, MAX_USERNAME_LENGTH, SUB_ACCOUNT_SEPARATOR, Username};
4use crate::model::api::{
5    DeleteSubAccountRequest, ListSubAccountsRequest, NewSubAccountRequest, SubAccountInfo,
6};
7use crate::model::errors::{LbErrKind, LbResult};
8use crate::model::meta::Meta;
9
10impl<S: Server> Lb<S> {
11    #[instrument(level = "debug", skip(self), err(Debug))]
12    pub async fn create_sub_account(&self, label: &str) -> LbResult<Username> {
13        let account = self.get_account()?;
14        let username =
15            format!("{}{SUB_ACCOUNT_SEPARATOR}{}", account.username, label.to_lowercase());
16        if username.len() > MAX_USERNAME_LENGTH {
17            return Err(LbErrKind::UsernameInvalid.into());
18        }
19
20        let counter = self
21            .client
22            .list_sub_accounts(account, ListSubAccountsRequest {})
23            .await?
24            .next_counter;
25        let sub_account = account.sub_account(username.clone(), counter)?;
26        let root_folder =
27            Meta::create_root(&sub_account, &self.entropy)?.sign_with(&sub_account)?;
28
29        self.client
30            .new_sub_account(
31                account,
32                NewSubAccountRequest {
33                    username: username.clone(),
34                    public_key: sub_account.public_key(),
35                    root_folder,
36                    counter,
37                },
38            )
39            .await?;
40
41        Ok(username)
42    }
43
44    #[instrument(level = "debug", skip(self), err(Debug))]
45    pub async fn list_sub_accounts(&self) -> LbResult<Vec<SubAccountInfo>> {
46        let account = self.get_account()?;
47        Ok(self
48            .client
49            .list_sub_accounts(account, ListSubAccountsRequest {})
50            .await?
51            .sub_accounts)
52    }
53
54    #[instrument(level = "debug", skip(self), err(Debug))]
55    pub async fn delete_sub_account(&self, username: &str) -> LbResult<()> {
56        let account = self.get_account()?;
57        self.client
58            .delete_sub_account(
59                account,
60                DeleteSubAccountRequest { username: username.to_lowercase() },
61            )
62            .await?;
63        Ok(())
64    }
65
66    #[instrument(level = "debug", skip(self), err(Debug))]
67    pub async fn export_sub_account_private_key(&self, username: &str) -> LbResult<String> {
68        let sub_account = self.sub_account(username).await?;
69        Ok(base64::encode(sub_account.private_key.serialize()))
70    }
71
72    #[instrument(level = "debug", skip(self), err(Debug))]
73    pub async fn export_sub_account_phrase(&self, username: &str) -> LbResult<String> {
74        let sub_account = self.sub_account(username).await?;
75        Ok(sub_account.get_phrase()?.join(" "))
76    }
77
78    async fn sub_account(&self, username: &str) -> LbResult<Account> {
79        let account = self.get_account()?;
80        let username = username.to_lowercase();
81        let info = self
82            .list_sub_accounts()
83            .await?
84            .into_iter()
85            .find(|sub_account| sub_account.username == username)
86            .ok_or(LbErrKind::UsernameNotFound)?;
87
88        let sub_account = account.sub_account(info.username, info.counter)?;
89        if sub_account.public_key() != info.public_key {
90            return Err(LbErrKind::UsernamePublicKeyMismatch.into());
91        }
92
93        Ok(sub_account)
94    }
95}