From 353dacc4a8d840ce8c3ddb1c19eebda4d821a9c8 Mon Sep 17 00:00:00 2001 From: Ghenadie <118184705+GhenadieVP@users.noreply.github.com> Date: Thu, 12 Dec 2024 15:53:20 +0200 Subject: [PATCH] Keep HostId in memory (#303) * store host id in memory * fmt * wip * wi[ * wip * wip --- Cargo.lock | 4 +- crates/sargon-uniffi/Cargo.toml | 2 +- .../src/system/sargon_os/sargon_os.rs | 4 +- crates/sargon/Cargo.toml | 2 +- .../sargon/src/system/sargon_os/sargon_os.rs | 50 +++++++++++-------- .../src/system/sargon_os/sargon_os_profile.rs | 5 +- 6 files changed, 36 insertions(+), 31 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e4a1d85c9..962e444a5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2772,7 +2772,7 @@ dependencies = [ [[package]] name = "sargon" -version = "1.1.82" +version = "1.1.83" dependencies = [ "actix-rt", "aes-gcm", @@ -2827,7 +2827,7 @@ dependencies = [ [[package]] name = "sargon-uniffi" -version = "1.1.82" +version = "1.1.83" dependencies = [ "actix-rt", "assert-json-diff", diff --git a/crates/sargon-uniffi/Cargo.toml b/crates/sargon-uniffi/Cargo.toml index e1a3c3d74..2d6780862 100644 --- a/crates/sargon-uniffi/Cargo.toml +++ b/crates/sargon-uniffi/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "sargon-uniffi" # Don't forget to update version in crates/sargon/Cargo.toml -version = "1.1.82" +version = "1.1.83" edition = "2021" build = "build.rs" diff --git a/crates/sargon-uniffi/src/system/sargon_os/sargon_os.rs b/crates/sargon-uniffi/src/system/sargon_os/sargon_os.rs index d8e7fe391..4a15696e0 100644 --- a/crates/sargon-uniffi/src/system/sargon_os/sargon_os.rs +++ b/crates/sargon-uniffi/src/system/sargon_os/sargon_os.rs @@ -75,8 +75,8 @@ impl SargonOS { self.wrapped.delete_wallet().await.into_result() } - pub async fn resolve_host_id(&self) -> Result { - self.wrapped.resolve_host_id().await.into_result() + pub fn host_id(&self) -> HostId { + self.wrapped.host_id().into() } pub async fn resolve_host_info(&self) -> HostInfo { diff --git a/crates/sargon/Cargo.toml b/crates/sargon/Cargo.toml index 4b67ab8d8..f22b0e8a8 100644 --- a/crates/sargon/Cargo.toml +++ b/crates/sargon/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "sargon" # Don't forget to update version in crates/sargon-uniffi/Cargo.toml -version = "1.1.82" +version = "1.1.83" edition = "2021" build = "build.rs" diff --git a/crates/sargon/src/system/sargon_os/sargon_os.rs b/crates/sargon/src/system/sargon_os/sargon_os.rs index 8a05114d3..a046a6090 100644 --- a/crates/sargon/src/system/sargon_os/sargon_os.rs +++ b/crates/sargon/src/system/sargon_os/sargon_os.rs @@ -14,6 +14,7 @@ pub struct SargonOS { pub(crate) profile_state_holder: ProfileStateHolder, pub(crate) clients: Clients, pub(crate) interactors: Interactors, + pub(crate) host_id: HostId, } /// So that we do not have to go through `self.clients`, @@ -68,12 +69,14 @@ impl SargonOS { profile_state = ProfileState::None; } + let host_id = Self::get_host_id(&clients).await; let os = Arc::new(Self { clients, profile_state_holder: ProfileStateHolder::new( profile_state.clone(), ), interactors, + host_id, }); os.clients .profile_state_change @@ -227,12 +230,11 @@ impl SargonOS { .save_private_hd_factor_source(&hd_factor_source) .await?; - let host_id = self.host_id().await?; let host_info = self.host_info().await; let profile = Profile::from_device_factor_source( hd_factor_source.factor_source, - host_id, + self.host_id, host_info, Some(accounts), ); @@ -281,8 +283,8 @@ impl SargonOS { as Arc } - pub async fn resolve_host_id(&self) -> Result { - self.host_id().await + pub fn host_id(&self) -> HostId { + self.host_id } pub async fn resolve_host_info(&self) -> HostInfo { @@ -297,7 +299,6 @@ impl SargonOS { ) -> Result<(Profile, PrivateHierarchicalDeterministicFactorSource)> { debug!("Creating new Profile and BDFS"); - let host_id = self.host_id().await?; let host_info = self.host_info().await; let is_main = true; @@ -325,7 +326,7 @@ impl SargonOS { debug!("Creating new Profile..."); let profile = Profile::with( - Header::new(DeviceInfo::new_from_info(&host_id, &host_info)), + Header::new(DeviceInfo::new_from_info(&self.host_id, &host_info)), FactorSources::with_bdfs(private_bdfs.factor_source.clone()), AppPreferences::default(), ProfileNetworks::default(), @@ -334,26 +335,34 @@ impl SargonOS { Ok((profile, private_bdfs)) } - pub(crate) async fn host_id(&self) -> Result { - Self::get_host_id(&self.clients).await - } - - pub(crate) async fn get_host_id(clients: &Clients) -> Result { + pub(crate) async fn get_host_id(clients: &Clients) -> HostId { debug!("Get Host ID"); let secure_storage = &clients.secure_storage; + let stored_host_id = secure_storage.load_host_id().await; - match secure_storage.load_host_id().await? { - Some(loaded_host_id) => { + match stored_host_id { + Ok(Some(loaded_host_id)) => { debug!("Found saved host id: {:?}", &loaded_host_id); - Ok(loaded_host_id) + loaded_host_id } - None => { + Ok(None) => { debug!("Found no saved host id, creating new."); let new_host_id = HostId::generate_new(); debug!("Created new host id: {:?}", &new_host_id); - secure_storage.save_host_id(&new_host_id).await?; - debug!("Saved new host id"); - Ok(new_host_id) + + let save_result = + secure_storage.save_host_id(&new_host_id).await; + if let Err(error) = save_result { + debug!("Failed to save new host id {:?}", error); + } else { + debug!("Saved new host id"); + } + new_host_id + } + Err(error) => { + debug!("Failed to load the host id {:?}", error); + + HostId::generate_new() } } } @@ -756,10 +765,7 @@ mod tests { async fn test_resolve_host_id() { let os = SUT::fast_boot().await; - assert_eq!( - os.resolve_host_id().await.unwrap(), - os.host_id().await.unwrap() - ) + assert_eq!(SargonOS::get_host_id(&os.clients).await, os.host_id()) } #[actix_rt::test] diff --git a/crates/sargon/src/system/sargon_os/sargon_os_profile.rs b/crates/sargon/src/system/sargon_os/sargon_os_profile.rs index eed6e6d08..73c60c328 100644 --- a/crates/sargon/src/system/sargon_os/sargon_os_profile.rs +++ b/crates/sargon/src/system/sargon_os/sargon_os_profile.rs @@ -21,10 +21,9 @@ impl SargonOS { /// header pub async fn claim_profile(&self, profile: &mut Profile) -> Result<()> { debug!("Claiming profile, id: {}", &profile.id()); - let host_id = self.host_id().await?; let host_info = self.host_info().await; let claiming_device_info = - DeviceInfo::new_from_info(&host_id, &host_info); + DeviceInfo::new_from_info(&self.host_id, &host_info); Self::claim_provided_profile(profile, claiming_device_info); info!( @@ -345,7 +344,7 @@ mod tests { .unwrap(); // ASSERT - let host_id = os.host_id().await.unwrap(); + let host_id = os.host_id; let host_info = os.host_info().await; assert_eq!( os.profile().unwrap().header.last_used_on_device,