Skip to content

Commit

Permalink
Remove references to deleted accounts + emit profile changes (#269)
Browse files Browse the repository at this point in the history
* remove references to deleted accounts

* wip

* revert cargo lock

* wip

* wip

* tests

* wip
  • Loading branch information
GhenadieVP authored Nov 19, 2024
1 parent da9ecc2 commit b2b2f2a
Show file tree
Hide file tree
Showing 12 changed files with 638 additions and 73 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/sargon-uniffi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "sargon-uniffi"
# Don't forget to update version in crates/sargon/Cargo.toml
version = "1.1.53"
version = "1.1.54"
edition = "2021"
build = "build.rs"

Expand Down
2 changes: 1 addition & 1 deletion crates/sargon/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "sargon"
# Don't forget to update version in crates/sargon-uniffi/Cargo.toml
version = "1.1.53"
version = "1.1.54"
edition = "2021"
build = "build.rs"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ impl<V: Debug + PartialEq + Eq + Clone + Identifiable> IdentifiedVecOf<V> {
true
}

/// Updates in place each item by `mutate`.
#[cfg(not(tarpaulin_include))] // false negative
#[inline]
pub fn update_all_with<F>(&mut self, mut mutate: F)
where
F: FnMut(&mut V),
{
for item in self.0.values_mut() {
mutate(item)
}
}

/// Tries to mutate the value identified by `id`, if no such value exists
/// an error is returned, the mutation is failable, if your return an `Err`
/// in `mutate`, this method propagates that error.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ pub struct AuthorizedDapp {
pub preferences: AuthorizedDappPreferences,
}

impl AuthorizedDapp {
/// Removes the referenced account for this dApp
pub(crate) fn remove_referenced_account(
&mut self,
account_address: &AccountAddress,
) {
self.references_to_authorized_personas
.update_all_with(|persona| {
persona.remove_shared_account(account_address);
});
}
}

impl IsNetworkAware for AuthorizedDapp {
fn network_id(&self) -> NetworkID {
self.network_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@ pub struct AuthorizedPersonaSimple {
pub shared_persona_data: SharedPersonaData,
}

impl AuthorizedPersonaSimple {
/// Removes the referenced account from the shared accounts
pub(crate) fn remove_shared_account(
&mut self,
account_address: &AccountAddress,
) -> bool {
if let Some(shared_accounts) = self.shared_accounts.as_mut() {
shared_accounts.remove_entry(&account_address);
true
} else {
false
}
}
}

impl AuthorizedPersonaSimple {
pub fn description(&self) -> String {
format!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ macro_rules! declare_shared_with_dapp {
let ids_str = self.ids.iter().map(|v| v.to_string()).join(", ");
format!("{} - shared ids: [{}]", self.request, ids_str)
}

pub fn remove_entry(&mut self, id: &<$id as Identifiable>::ID) -> Option<$id> {
self.ids.remove_id(id)
}
}

#[cfg(test)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ decl_identified_vec_of!(
AuthorizedDapp
);

impl AuthorizedDapps {
/// Remove referenced account from all the dApps
pub(crate) fn remove_referenced_account(
&mut self,
account_address: &AccountAddress,
) {
self.update_all_with(|dapp| {
dapp.remove_referenced_account(account_address);
})
}
}

impl HasSampleValues for AuthorizedDapps {
/// A sample used to facilitate unit tests.
fn sample() -> Self {
Expand Down
26 changes: 26 additions & 0 deletions crates/sargon/src/profile/v100/networks/network/profile_network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,32 @@ impl ProfileNetwork {
None
}
}

/// Hides the account associated with the `account_address`
pub(crate) fn hide_account(
&mut self,
account_address: &AccountAddress,
) -> Option<Account> {
let account = self.update_account(account_address, |account| {
account.mark_as_hidden();
});
self.authorized_dapps
.remove_referenced_account(account_address);
account
}

/// Tombstones the account associated with the `account_address`
pub(crate) fn tombstone_account(
&mut self,
account_address: &AccountAddress,
) -> Option<Account> {
let account = self.update_account(account_address, |account| {
account.mark_as_tombstoned();
});
self.authorized_dapps
.remove_referenced_account(account_address);
account
}
}

impl HasSampleValues for ProfileNetwork {
Expand Down
30 changes: 30 additions & 0 deletions crates/sargon/src/profile/v100/networks/profile_networks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,36 @@ impl ProfileNetworks {
});
self.get_account(address)
}

/// Hides the account associated with the `account_address`
pub(crate) fn hide_account(
&mut self,
account_address: &AccountAddress,
) -> bool {
self.update_with(account_address.network_id(), |n| {
n.hide_account(account_address);
})
}

/// Tombstones the account associated with the `account_address`
pub(crate) fn tombstone_account(
&mut self,
account_address: &AccountAddress,
) -> bool {
self.update_with(account_address.network_id(), |n| {
n.tombstone_account(account_address);
})
}

/// Tombstones the accounts
pub(crate) fn tombstone_accounts(
&mut self,
account_addresses: &Vec<AccountAddress>,
) {
for account_address in account_addresses {
self.tombstone_account(&account_address);
}
}
}

impl ProfileNetworks {
Expand Down
Loading

0 comments on commit b2b2f2a

Please sign in to comment.