Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use nym_offline_monitor::ConnectivityMonitor;
use nym_vpn_api_client::{
error::UNREGISTER_NON_EXISTENT_DEVICE_CODE_ID,
response::NymErrorResponse,
types::{DeviceStatus, VpnAccount},
types::{DeviceStatus, Platform, VpnAccount},
};
use nym_vpn_lib_types::{AccountCommandError, VpnApiError};
use nym_vpn_lib_types::{AccountCommandError, RegisterAccountResponse, VpnApiError};
use nym_vpn_store::account::StorableAccount;
use tracing::info;

Expand Down Expand Up @@ -97,6 +97,24 @@ pub(crate) async fn handle_create_account<C: ConnectivityMonitor>(
Ok(())
}

pub(crate) async fn handle_register_account<C: ConnectivityMonitor>(
shared_state: &mut SharedAccountState<C>,
account: StorableAccount,
platform: Platform,
) -> Result<RegisterAccountResponse, AccountCommandError> {
let vpn_account = VpnAccount::try_from(account.clone())
.map_err(|e| AccountCommandError::InvalidMnemonic(e.to_string()))?;
let account_token = shared_state
.vpn_api_client
.post_account(&vpn_account, platform)
.await?
.account_token;

tracing::debug!("Account registered with API");

Ok(RegisterAccountResponse { account_token })
}

pub(crate) async fn handle_forget_account<C: ConnectivityMonitor>(
shared_state: &mut SharedAccountState<C>,
) -> Result<(), AccountCommandError> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ impl<C: ConnectivityMonitor> AccountControllerStateHandler<C> for ErrorState {
match command {
AccountCommand::CreateAccount(return_sender) => return_sender.send(Err(AccountCommandError::ExistingAccount)),
AccountCommand::StoreAccount(return_sender, _) => return_sender.send(Err(AccountCommandError::ExistingAccount)),
AccountCommand::RegisterAccount(return_sender, _, _) => return_sender.send(Err(AccountCommandError::ExistingAccount)), // do we try to register here?
AccountCommand::RegisterAccount(return_sender, account, platform) => {
let res = handler::handle_register_account(shared_state, account, platform).await;
return_sender.send(res);
}
AccountCommand::ForgetAccount(return_sender) => {
let res = handler::handle_forget_account(shared_state).await;
let error = res.is_err();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ impl<C: ConnectivityMonitor> AccountControllerStateHandler<C> for LoggedOutState

AccountCommand::RegisterAccount(return_sender, _, _) => return_no_account(return_sender),
AccountCommand::RefreshAccountState(return_sender) => return_no_account(return_sender),

AccountCommand::VpnApiFirewallDown(return_sender) => {
shared_state.firewall_active = false;
return_sender.send(Ok(()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ impl<C: ConnectivityMonitor> AccountControllerStateHandler<C> for OfflineState {
return_sender.send(handler::handle_store_account(shared_state, account).await)
},
AccountCommand::RegisterAccount(return_sender, _, _) => return_no_connectivity(return_sender),

// Before that command gets sent to the AC, the tunnel must be in Disconnected state, so we shouldn't ever end up here
// If we nevertheless do, we can't forget the account, because maybe the tunnel is in Offline {reconnect : true} state, and we shouldn't remove the account if it's the case
AccountCommand::ForgetAccount(return_sender) => return_no_connectivity(return_sender),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ impl<C: ConnectivityMonitor> AccountControllerStateHandler<C> for ReadyState {
Some(command) = command_rx.recv() => {
match command {
AccountCommand::CreateAccount(return_sender) | AccountCommand::StoreAccount(return_sender, _) => return_existing_account(return_sender),
AccountCommand::RegisterAccount(return_sender, _, _) => return_existing_account(return_sender),
AccountCommand::RegisterAccount(return_sender, account, platform) => {
let res = handler::handle_register_account(shared_state, account, platform).await;
return_sender.send(res);
}
AccountCommand::ForgetAccount(return_sender) => {
let res = handler::handle_forget_account(shared_state).await;
let error = res.is_err();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,10 @@ impl<C: ConnectivityMonitor> AccountControllerStateHandler<C> for SyncingState {
match command {
AccountCommand::CreateAccount(return_sender) => return_sender.send(Err(AccountCommandError::ExistingAccount)),
AccountCommand::StoreAccount(return_sender, _) => return_sender.send(Err(AccountCommandError::ExistingAccount)),
AccountCommand::RegisterAccount(return_sender, _, _) => return_sender.send(Err(AccountCommandError::ExistingAccount)),
AccountCommand::RegisterAccount(return_sender, account, platform) => {
let res = handler::handle_register_account(shared_state, account, platform).await;
return_sender.send(res);
}
AccountCommand::ForgetAccount(return_sender) => {
let res = handler::handle_forget_account(shared_state).await;
let error = res.is_err();
Expand All @@ -246,7 +249,7 @@ impl<C: ConnectivityMonitor> AccountControllerStateHandler<C> for SyncingState {
return NextAccountControllerState::NewState(LoggedOutState::enter());
}
},
AccountCommand::AccountBalance(return_sender) => return_sender.send(Err(AccountCommandError::AccountNotDecentralised)),
AccountCommand::AccountBalance(return_sender) => return_sender.send(Err(AccountCommandError::AccountNotDecentralised)),
AccountCommand::ObtainTicketbooks(return_sender, _) => return_sender.send(Err(AccountCommandError::AccountNotDecentralised)),
AccountCommand::RefreshAccountState(return_sender) => {
return_sender.send(Ok(()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,10 @@ impl<C: ConnectivityMonitor> AccountControllerStateHandler<C> for RequestingZkNy
match command {
AccountCommand::CreateAccount(return_sender) => return_sender.send(Err(AccountCommandError::ExistingAccount)),
AccountCommand::StoreAccount(return_sender, _) => return_sender.send(Err(AccountCommandError::ExistingAccount)),
AccountCommand::RegisterAccount(return_sender, _, _) => return_sender.send(Err(AccountCommandError::ExistingAccount)),
AccountCommand::RegisterAccount(return_sender, account, platform) => {
let res = handler::handle_register_account(shared_state, account, platform).await;
return_sender.send(res);
}
AccountCommand::ForgetAccount(return_sender) => {
self.zk_nym_fetching_handle.abort();
let res = handler::handle_forget_account(shared_state).await;
Expand Down
Loading