Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: unregister device raw #1968

Merged
merged 2 commits into from
Jan 22, 2025
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
42 changes: 40 additions & 2 deletions nym-vpn-core/crates/nym-vpn-lib/src/platform/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,11 @@ pub(super) async fn get_device_id() -> Result<String, VpnError> {
pub(crate) mod raw {
use std::path::Path;

use nym_sdk::mixnet::StoragePaths;

use super::*;
use crate::platform::environment;
use nym_sdk::mixnet::StoragePaths;
use nym_vpn_api_client::types::{Device, DeviceStatus};
use nym_vpn_api_client::VpnApiClient;

async fn setup_account_storage(
path: &str,
Expand Down Expand Up @@ -326,6 +328,40 @@ pub(crate) mod raw {
})
}

async fn create_vpn_api_client() -> Result<VpnApiClient, VpnError> {
let network_env = environment::current_environment_details().await?;
let user_agent = crate::util::construct_user_agent();
VpnApiClient::new(network_env.vpn_api_url(), user_agent).map_err(|e| {
VpnError::InternalError {
details: e.to_string(),
}
})
}

async fn unregister_device_from_api_raw(path: &str) -> Result<(), VpnError> {
let account_storage = setup_account_storage(path).await?;
let device_keys = account_storage
.load_keys()
.await
.map_err(|_| VpnError::NoDeviceIdentity)?;
let device = Device::from(device_keys.device_keypair().clone());
let mnemonic = account_storage
.load_mnemonic()
.await
.map_err(|_| VpnError::NoAccountStored)?;
let account = VpnApiAccount::from(mnemonic);

let vpn_api_client = create_vpn_api_client().await?;

vpn_api_client
.update_device(&account, &device, DeviceStatus::DeleteMe)
.await
.map_err(|err| VpnError::UnregisterDeviceApiClientFailure {
details: err.to_string(),
})?;
Ok(())
}

pub(crate) async fn forget_account_raw(path: &str) -> Result<(), VpnError> {
tracing::info!("REMOVING ALL ACCOUNT AND DEVICE DATA IN: {path}");

Expand All @@ -334,6 +370,8 @@ pub(crate) mod raw {
details: err.to_string(),
})?;

unregister_device_from_api_raw(path).await?;

// First remove the files we own directly
remove_account_mnemonic_raw(path).await?;
remove_device_identity_raw(path).await?;
Expand Down
3 changes: 3 additions & 0 deletions nym-vpn-core/crates/nym-vpn-lib/src/platform/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ pub enum VpnError {

#[error("invalid statistics recipient")]
StatisticsRecipient,

#[error("failed to remove device from nym vpn api: {details}")]
UnregisterDeviceApiClientFailure { details: String },
}

#[derive(uniffi::Record, Clone, Debug, PartialEq, Eq)]
Expand Down
Loading