From 820e2618b42c460cf2d33b2c464d1eb1f1a65c9c Mon Sep 17 00:00:00 2001 From: Alexander Cyon Date: Mon, 30 Sep 2024 11:56:16 +0200 Subject: [PATCH] Yagni, many Recovery Types --- .../final_derivations_and_analysis.rs | 10 -- .../intermediary_derivations_and_analysis.rs | 12 -- .../known_taken_instances.rs | 119 ----------------- .../helpers/derivation_analysis_types/mod.rs | 14 -- .../derivation_analysis_types/new_types.rs | 3 - .../recovered_securified_entities.rs | 91 ------------- .../recovered_unsecurified_entities.rs | 113 ---------------- .../unrecovered_securified_entities.rs | 82 ------------ .../unrecovered_securified_entity.rs | 122 ------------------ 9 files changed, 566 deletions(-) delete mode 100644 src/recovery_securify_cache/helpers/derivation_analysis_types/final_derivations_and_analysis.rs delete mode 100644 src/recovery_securify_cache/helpers/derivation_analysis_types/intermediary_derivations_and_analysis.rs delete mode 100644 src/recovery_securify_cache/helpers/derivation_analysis_types/known_taken_instances.rs delete mode 100644 src/recovery_securify_cache/helpers/derivation_analysis_types/new_types.rs delete mode 100644 src/recovery_securify_cache/helpers/derivation_analysis_types/recovered_securified_entities.rs delete mode 100644 src/recovery_securify_cache/helpers/derivation_analysis_types/recovered_unsecurified_entities.rs delete mode 100644 src/recovery_securify_cache/helpers/derivation_analysis_types/unrecovered_securified_entities.rs delete mode 100644 src/recovery_securify_cache/helpers/derivation_analysis_types/unrecovered_securified_entity.rs diff --git a/src/recovery_securify_cache/helpers/derivation_analysis_types/final_derivations_and_analysis.rs b/src/recovery_securify_cache/helpers/derivation_analysis_types/final_derivations_and_analysis.rs deleted file mode 100644 index c8f8193d..00000000 --- a/src/recovery_securify_cache/helpers/derivation_analysis_types/final_derivations_and_analysis.rs +++ /dev/null @@ -1,10 +0,0 @@ -use crate::prelude::*; - -/// The final outcome of `FactorInstancesProvider::poly_derive`, used -/// by operations such as Account Recovery Scan and -/// Securifying accounts -#[derive(Clone, Debug)] -pub struct FinalDerivationsAndAnalysis { - pub entities_from_analysis: EntitiesFromAnalysis, - pub cache: Arc, -} diff --git a/src/recovery_securify_cache/helpers/derivation_analysis_types/intermediary_derivations_and_analysis.rs b/src/recovery_securify_cache/helpers/derivation_analysis_types/intermediary_derivations_and_analysis.rs deleted file mode 100644 index c164706b..00000000 --- a/src/recovery_securify_cache/helpers/derivation_analysis_types/intermediary_derivations_and_analysis.rs +++ /dev/null @@ -1,12 +0,0 @@ -use crate::prelude::*; - -#[derive(Clone, Default, Debug, PartialEq, Eq)] -pub struct IntermediaryDerivationAndAnalysis { - pub entities_from_analysis: EntitiesFromAnalysis, -} - -impl IntermediaryDerivationAndAnalysis { - pub fn all_account_addresses(&self) -> IndexSet { - todo!() - } -} diff --git a/src/recovery_securify_cache/helpers/derivation_analysis_types/known_taken_instances.rs b/src/recovery_securify_cache/helpers/derivation_analysis_types/known_taken_instances.rs deleted file mode 100644 index 6f7fbd03..00000000 --- a/src/recovery_securify_cache/helpers/derivation_analysis_types/known_taken_instances.rs +++ /dev/null @@ -1,119 +0,0 @@ -use crate::prelude::*; - -// TODO figure out if we want this or `DerivedFactorInstances`? Or neither or both -/// A collection of collections of FactorInstances, all collections are disjoint, -/// i.e. no FactorInstance is present in more than one collection. -/// -/// All FactorInstances are known to be not free, i.e. they are taken, meaning -/// they are already used by some Securified or Unsecurified entity, which we -/// know by having matched them against Profile or Gateway or both. -#[derive(Clone, Default, Debug, PartialEq, Eq, Hash)] -pub struct EntitiesFromAnalysis { - hiding_ctor: HiddenConstructor, - /// Unsecurified entities that were recovered - pub recovered_unsecurified_entities: RecoveredUnsecurifiedEntities, - - /// Securified entities that were recovered - pub recovered_securified_entities: RecoveredSecurifiedEntities, - - /// Securified entities that were not recovered - pub unrecovered_securified_entities: UnrecoveredSecurifiedEntities, - - pub virtual_entity_creating_instances: VirtualEntityCreatingInstances, -} - -impl IsFactorInstanceCollectionBase for EntitiesFromAnalysis { - fn factor_instances(&self) -> IndexSet { - let mut set = self.recovered_unsecurified_entities.factor_instances(); - set.extend(self.recovered_securified_entities.factor_instances()); - set.extend(self.unrecovered_securified_entities.factor_instances()); - set.extend(self.virtual_entity_creating_instances.factor_instances()); - set - } -} - -impl HasSampleValues for EntitiesFromAnalysis { - fn sample() -> Self { - Self::new( - RecoveredUnsecurifiedEntities::sample(), - RecoveredSecurifiedEntities::sample(), - UnrecoveredSecurifiedEntities::sample(), - VirtualEntityCreatingInstances::sample(), - ) - } - - fn sample_other() -> Self { - Self::new( - RecoveredUnsecurifiedEntities::sample_other(), - RecoveredSecurifiedEntities::sample_other(), - UnrecoveredSecurifiedEntities::sample_other(), - VirtualEntityCreatingInstances::sample(), - ) - } -} - -impl EntitiesFromAnalysis { - /// # Panics - /// Panics if the collections of factor instances are not disjoint - pub fn new( - recovered_unsecurified_entities: RecoveredUnsecurifiedEntities, - recovered_securified_entities: RecoveredSecurifiedEntities, - unrecovered_securified_entities: UnrecoveredSecurifiedEntities, - virtual_entity_creating_instances: VirtualEntityCreatingInstances, - ) -> Self { - assert_are_factor_instance_collections_disjoint(vec![ - &recovered_unsecurified_entities, - &recovered_securified_entities, - &unrecovered_securified_entities, - &virtual_entity_creating_instances, - ]); - Self { - hiding_ctor: HiddenConstructor, - recovered_unsecurified_entities, - recovered_securified_entities, - unrecovered_securified_entities, - virtual_entity_creating_instances, - } - } - - pub fn merge(self, other: Self) -> Self { - Self::new( - self.recovered_unsecurified_entities - .merge(other.recovered_unsecurified_entities), - self.recovered_securified_entities - .merge(other.recovered_securified_entities), - self.unrecovered_securified_entities - .merge(other.unrecovered_securified_entities), - self.virtual_entity_creating_instances - .merge(other.virtual_entity_creating_instances), - ) - } - - pub fn recovered_entities(&self) -> IndexSet { - let mut set = self.recovered_unsecurified_entities.entities(); - set.extend(self.recovered_securified_entities.entities()); - set - } - - pub fn recovered_unsecurified_entities(&self) -> IndexSet { - self.recovered_unsecurified_entities.entities() - } - - pub fn recovered_unsecurified_accounts(&self) -> IndexSet { - self.recovered_unsecurified_entities() - .into_iter() - .filter_map(|e| e.as_account_entity().cloned()) - .collect() - } - - pub fn recovered_securified_entities(&self) -> IndexSet { - self.recovered_securified_entities.entities() - } - - pub fn recovered_securified_accounts(&self) -> IndexSet { - self.recovered_securified_entities() - .into_iter() - .filter_map(|e| e.as_account_entity().cloned()) - .collect() - } -} diff --git a/src/recovery_securify_cache/helpers/derivation_analysis_types/mod.rs b/src/recovery_securify_cache/helpers/derivation_analysis_types/mod.rs index 0b1878d2..5ea09bbc 100644 --- a/src/recovery_securify_cache/helpers/derivation_analysis_types/mod.rs +++ b/src/recovery_securify_cache/helpers/derivation_analysis_types/mod.rs @@ -1,29 +1,15 @@ mod derived_factor_instances; mod factor_instance_in_securified_space; mod factor_instance_in_unsecurified_space; -mod final_derivations_and_analysis; -mod intermediary_derivations_and_analysis; -mod known_taken_instances; mod probably_free_factor_instances; -mod recovered_securified_entities; -mod recovered_unsecurified_entities; mod securified_entity; -mod unrecovered_securified_entities; -mod unrecovered_securified_entity; mod unsecurified_entity; mod virtual_entity_creating_instances; pub use derived_factor_instances::*; pub use factor_instance_in_securified_space::*; pub use factor_instance_in_unsecurified_space::*; -pub use final_derivations_and_analysis::*; -pub use intermediary_derivations_and_analysis::*; -pub use known_taken_instances::*; pub use probably_free_factor_instances::*; -pub use recovered_securified_entities::*; -pub use recovered_unsecurified_entities::*; pub use securified_entity::*; -pub use unrecovered_securified_entities::*; -pub use unrecovered_securified_entity::*; pub use unsecurified_entity::*; pub use virtual_entity_creating_instances::*; diff --git a/src/recovery_securify_cache/helpers/derivation_analysis_types/new_types.rs b/src/recovery_securify_cache/helpers/derivation_analysis_types/new_types.rs deleted file mode 100644 index 30bacd4f..00000000 --- a/src/recovery_securify_cache/helpers/derivation_analysis_types/new_types.rs +++ /dev/null @@ -1,3 +0,0 @@ -use std::{net, ops::Range}; - -use crate::prelude::*; diff --git a/src/recovery_securify_cache/helpers/derivation_analysis_types/recovered_securified_entities.rs b/src/recovery_securify_cache/helpers/derivation_analysis_types/recovered_securified_entities.rs deleted file mode 100644 index a098a5c4..00000000 --- a/src/recovery_securify_cache/helpers/derivation_analysis_types/recovered_securified_entities.rs +++ /dev/null @@ -1,91 +0,0 @@ -use crate::prelude::*; - -/// Securified Entities that were discovered and recovered part of -/// `derive_and_analyze` -#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)] -pub struct RecoveredSecurifiedEntities { - securified_entities: Vec, -} - -impl FromIterator for RecoveredSecurifiedEntities { - fn from_iter>(iter: I) -> Self { - Self::new(iter.into_iter().collect()) - } -} - -impl IntoIterator for RecoveredSecurifiedEntities { - type Item = SecurifiedEntity; - type IntoIter = as IntoIterator>::IntoIter; - - fn into_iter(self) -> Self::IntoIter { - self.securified_entities().into_iter() - } -} - -impl RecoveredSecurifiedEntities { - pub fn new(securified_entities: IndexSet) -> Self { - Self { - securified_entities: securified_entities.into_iter().collect(), - } - } - - pub fn securified_entities(&self) -> IndexSet { - self.securified_entities.clone().into_iter().collect() - } - - pub fn entities(&self) -> IndexSet { - self.securified_entities() - .into_iter() - .map(AccountOrPersona::from) - .collect() - } - - pub fn merge(self, other: Self) -> Self { - Self::new( - self.securified_entities() - .union(&other.securified_entities()) - .cloned() - .collect(), - ) - } -} - -impl IsFactorInstanceCollectionBase for RecoveredSecurifiedEntities { - fn factor_instances(&self) -> IndexSet { - self.securified_entities() - .into_iter() - .flat_map(|x| x.securified_entity_control().all_factor_instances()) - .collect() - } -} - -impl HasSampleValues for RecoveredSecurifiedEntities { - fn sample() -> Self { - Self::new(IndexSet::from_iter([ - SecurifiedEntity::sample(), - SecurifiedEntity::sample_other(), - ])) - } - - fn sample_other() -> Self { - Self::new(IndexSet::just(SecurifiedEntity::sample_other())) - } -} - -#[cfg(test)] -mod tests { - use super::*; - - type Sut = RecoveredSecurifiedEntities; - - #[test] - fn equality() { - assert_eq!(Sut::sample(), Sut::sample()); - assert_eq!(Sut::sample_other(), Sut::sample_other()); - } - - #[test] - fn inequality() { - assert_ne!(Sut::sample(), Sut::sample_other()); - } -} diff --git a/src/recovery_securify_cache/helpers/derivation_analysis_types/recovered_unsecurified_entities.rs b/src/recovery_securify_cache/helpers/derivation_analysis_types/recovered_unsecurified_entities.rs deleted file mode 100644 index d284d9f1..00000000 --- a/src/recovery_securify_cache/helpers/derivation_analysis_types/recovered_unsecurified_entities.rs +++ /dev/null @@ -1,113 +0,0 @@ -use crate::prelude::*; - -/// Unsecurified Entities that were discovered and recovered part of -/// `derive_and_analyze` -#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)] -pub struct RecoveredUnsecurifiedEntities { - unsecurified_entities: Vec, -} - -impl FromIterator for RecoveredUnsecurifiedEntities { - fn from_iter>(iter: I) -> Self { - Self::new(iter.into_iter().collect()) - } -} - -impl IntoIterator for RecoveredUnsecurifiedEntities { - type Item = UnsecurifiedEntity; - type IntoIter = as IntoIterator>::IntoIter; - - fn into_iter(self) -> Self::IntoIter { - self.unsecurified_entities().into_iter() - } -} - -impl RecoveredUnsecurifiedEntities { - pub fn new(unsecurified_entities: IndexSet) -> Self { - Self { - unsecurified_entities: unsecurified_entities.into_iter().collect(), - } - } - - pub fn unsecurified_entities(&self) -> IndexSet { - self.unsecurified_entities.clone().into_iter().collect() - } - - pub fn entities(&self) -> IndexSet { - self.unsecurified_entities() - .into_iter() - .map(AccountOrPersona::from) - .collect() - } - - pub fn merge(self, other: Self) -> Self { - Self::new( - self.unsecurified_entities() - .union(&other.unsecurified_entities()) - .cloned() - .collect(), - ) - } -} - -impl IsFactorInstanceCollectionBase for RecoveredUnsecurifiedEntities { - fn factor_instances(&self) -> IndexSet { - self.unsecurified_entities() - .into_iter() - .map(|x| x.factor_instance()) - .collect() - } -} - -impl HasSampleValues for RecoveredUnsecurifiedEntities { - fn sample() -> Self { - Self::new(IndexSet::from_iter([ - UnsecurifiedEntity::sample(), - UnsecurifiedEntity::sample_other(), - ])) - } - - fn sample_other() -> Self { - Self::new(IndexSet::just(UnsecurifiedEntity::sample_other())) - } -} - -fn merge_recovered_entities( - unsecurified: RecoveredUnsecurifiedEntities, - securified: RecoveredSecurifiedEntities, -) -> IndexSet { - let unsecurified = unsecurified.entities(); - let securified = securified.entities(); - let mut entities = IndexSet::new(); - entities.extend(unsecurified); - entities.extend(securified); - - entities -} - -impl RecoveredUnsecurifiedEntities { - pub fn merge_with_securified( - self, - securified: RecoveredSecurifiedEntities, - ) -> IndexSet { - merge_recovered_entities(self, securified) - } -} - -#[cfg(test)] -mod tests { - use super::*; - - type Sut = RecoveredUnsecurifiedEntities; - - #[test] - fn equality() { - assert_eq!(Sut::sample(), Sut::sample()); - assert_eq!(Sut::sample_other(), Sut::sample_other()); - } - - #[test] - fn inequality() { - assert_ne!(Sut::sample(), Sut::sample_other()); - } -} diff --git a/src/recovery_securify_cache/helpers/derivation_analysis_types/unrecovered_securified_entities.rs b/src/recovery_securify_cache/helpers/derivation_analysis_types/unrecovered_securified_entities.rs deleted file mode 100644 index 65ecb08b..00000000 --- a/src/recovery_securify_cache/helpers/derivation_analysis_types/unrecovered_securified_entities.rs +++ /dev/null @@ -1,82 +0,0 @@ -use crate::prelude::*; - -use crate::prelude::*; - -/// Securified Entities that were discovered and recovered part of -/// `derive_and_analyze` that we did not successfully recover due to -/// not enough matched FactorInstances. -#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)] -pub struct UnrecoveredSecurifiedEntities { - entities: Vec, -} - -impl FromIterator for UnrecoveredSecurifiedEntities { - fn from_iter>(iter: I) -> Self { - Self::new(iter.into_iter().collect()) - } -} - -impl IntoIterator for UnrecoveredSecurifiedEntities { - type Item = UnrecoveredSecurifiedEntity; - type IntoIter = as IntoIterator>::IntoIter; - - fn into_iter(self) -> Self::IntoIter { - self.entities().into_iter() - } -} - -impl UnrecoveredSecurifiedEntities { - pub fn new(entities: IndexSet) -> Self { - Self { - entities: entities.into_iter().collect(), - } - } - - pub fn entities(&self) -> IndexSet { - self.entities.clone().into_iter().collect() - } - - pub fn merge(self, other: Self) -> Self { - Self::new(self.entities().union(&other.entities()).cloned().collect()) - } -} - -impl IsFactorInstanceCollectionBase for UnrecoveredSecurifiedEntities { - fn factor_instances(&self) -> IndexSet { - self.entities() - .into_iter() - .flat_map(|x| x.matched_factor_instances()) - .collect() - } -} - -impl HasSampleValues for UnrecoveredSecurifiedEntities { - fn sample() -> Self { - Self::new(IndexSet::from_iter([ - UnrecoveredSecurifiedEntity::sample(), - UnrecoveredSecurifiedEntity::sample_other(), - ])) - } - - fn sample_other() -> Self { - Self::new(IndexSet::just(UnrecoveredSecurifiedEntity::sample_other())) - } -} - -#[cfg(test)] -mod tests { - use super::*; - - type Sut = UnrecoveredSecurifiedEntity; - - #[test] - fn equality() { - assert_eq!(Sut::sample(), Sut::sample()); - assert_eq!(Sut::sample_other(), Sut::sample_other()); - } - - #[test] - fn inequality() { - assert_ne!(Sut::sample(), Sut::sample_other()); - } -} diff --git a/src/recovery_securify_cache/helpers/derivation_analysis_types/unrecovered_securified_entity.rs b/src/recovery_securify_cache/helpers/derivation_analysis_types/unrecovered_securified_entity.rs deleted file mode 100644 index 992d858f..00000000 --- a/src/recovery_securify_cache/helpers/derivation_analysis_types/unrecovered_securified_entity.rs +++ /dev/null @@ -1,122 +0,0 @@ -use crate::prelude::*; - -/// The hash of the public key of a missing factor instance and the factor list -/// kind it belongs to - according to ScryptoAccessRule checked against gateway. -/// -/// # Future -/// We might want to add information about which role, primary, confirmation -/// or recover. -#[derive(Clone, Debug, PartialEq, Eq, Hash)] -pub struct MissingFactorInstance { - /// The hash of the public key of a missing factor instance. - pub public_key_hash: PublicKeyHash, - - /// Threshold or override factor - pub factor_list: FactorListKind, -} - -impl HasSampleValues for MissingFactorInstance { - fn sample() -> Self { - Self { - public_key_hash: PublicKeyHash::sample(), - factor_list: FactorListKind::sample(), - } - } - fn sample_other() -> Self { - Self { - public_key_hash: PublicKeyHash::sample_other(), - factor_list: FactorListKind::sample_other(), - } - } -} - -/// An unrecovered securified entity means that we did not regain -/// control of it, that is, some FactorInstances are missing to have -/// control of the entity. -#[derive(Clone, Debug, PartialEq, Eq, Hash)] -pub struct UnrecoveredSecurifiedEntity { - /// The address which is verified to match the `veci` - pub address: AddressOfAccountOrPersona, - - /// FactorInstances we have control over. - matched_factor_instances: Vec, - - /// List of FactorInstances we were not able to locally re-derive, but set - /// on this entity according to ScryptoAccessRule checked against Gateway. - missing_factor_instances: Vec, - - /// If we found this UnsecurifiedEntity while scanning OnChain using - /// Gateway, we might have been able to read out the third party deposit - /// settings. - pub third_party_deposit: Option, -} - -impl UnrecoveredSecurifiedEntity { - pub fn new( - address: AddressOfAccountOrPersona, - matched_factor_instances: IndexSet, - missing_factor_instances: IndexSet, - third_party_deposit: impl Into>, - ) -> Self { - Self { - address, - matched_factor_instances: matched_factor_instances.into_iter().collect(), - missing_factor_instances: missing_factor_instances.into_iter().collect(), - third_party_deposit: third_party_deposit.into(), - } - } - - /// FactorInstances we have control over. - pub fn matched_factor_instances(&self) -> IndexSet { - self.matched_factor_instances.clone().into_iter().collect() - } - - /// List of FactorInstances we were not able to locally re-derive, but set - /// on this entity according to ScryptoAccessRule checked against Gateway. - pub fn missing_factor_instances(&self) -> IndexSet { - self.missing_factor_instances.clone().into_iter().collect() - } -} - -impl HasSampleValues for UnrecoveredSecurifiedEntity { - fn sample() -> Self { - Self::new( - AddressOfAccountOrPersona::sample(), - IndexSet::from_iter([ - HierarchicalDeterministicFactorInstance::sample(), - HierarchicalDeterministicFactorInstance::sample_other(), - ]), - IndexSet::from_iter([ - MissingFactorInstance::sample(), - MissingFactorInstance::sample_other(), - ]), - ThirdPartyDepositPreference::sample(), - ) - } - fn sample_other() -> Self { - Self::new( - AddressOfAccountOrPersona::sample_other(), - IndexSet::from_iter([HierarchicalDeterministicFactorInstance::sample_other()]), - IndexSet::from_iter([MissingFactorInstance::sample_other()]), - ThirdPartyDepositPreference::sample_other(), - ) - } -} - -#[cfg(test)] -mod tests { - use super::*; - - type Sut = UnrecoveredSecurifiedEntity; - - #[test] - fn equality() { - assert_eq!(Sut::sample(), Sut::sample()); - assert_eq!(Sut::sample_other(), Sut::sample_other()); - } - - #[test] - fn inequality() { - assert_ne!(Sut::sample(), Sut::sample_other()); - } -}