Skip to content
This repository was archived by the owner on Feb 4, 2025. It is now read-only.

Commit

Permalink
adding VirtualEntityCreatingInstances
Browse files Browse the repository at this point in the history
  • Loading branch information
CyonAlexRDX committed Sep 20, 2024
1 parent 17cabf3 commit ca1dfd9
Show file tree
Hide file tree
Showing 11 changed files with 307 additions and 130 deletions.
35 changes: 7 additions & 28 deletions src/recovery_securify_cache/derivation_and_analysis/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,20 @@ pub struct IntermediaryDerivationAnalysis {
}
impl IntermediaryDerivationAnalysis {
/// # Panics
/// Panics if the union all instances from:
/// `probably_free_instances`, `recovered_unsecurified_entities`, `recovered_securified_entities`, `unrecovered_securified_entities` are not equal to `all_factor_instances`
/// Also panics if any factor_instance in:
/// `recovered_unsecurified_entities`, `recovered_securified_entities`, `unrecovered_securified_entities` is found in probably_free_instances
/// Panics if the collections of factor instances are not disjoint
pub fn new(

Check warning on line 20 in src/recovery_securify_cache/derivation_and_analysis/input.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/derivation_and_analysis/input.rs#L20

Added line #L20 was not covered by tests
all_factor_instances: IndexSet<HierarchicalDeterministicFactorInstance>,
probably_free_instances: ProbablyFreeFactorInstances,
recovered_unsecurified_entities: RecoveredUnsecurifiedEntities,
recovered_securified_entities: RecoveredSecurifiedEntities,
unrecovered_securified_entities: UnrecoveredSecurifiedEntities,
) -> Self {
let mut merge = IndexSet::new();
merge.extend(recovered_unsecurified_entities.instances());
merge.extend(recovered_securified_entities.instances());
merge.extend(unrecovered_securified_entities.instances());

assert!(
merge.clone().into_iter().collect::<HashSet<_>>()
.intersection(
&probably_free_instances.instances().into_iter().collect::<HashSet<_>>()
)
.collect_vec()
.is_empty(),
"Discrepancy! Some factor instances in probably_free_instances are found in other collections of non free instances!");

// Only extend with `probably_free_instances` once we have verified it does not contain any of the instances in the other collections
merge.extend(probably_free_instances.instances());

assert_eq!(
merge.into_iter().collect::<HashSet<_>>(),
all_factor_instances
.clone()
.into_iter()
.collect::<HashSet<_>>()
);
assert_are_factor_instance_collections_disjoint(vec![

Check warning on line 27 in src/recovery_securify_cache/derivation_and_analysis/input.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/derivation_and_analysis/input.rs#L27

Added line #L27 was not covered by tests
&probably_free_instances,
&recovered_unsecurified_entities,
&recovered_securified_entities,
&unrecovered_securified_entities,
]);

Self {
all_factor_instances: all_factor_instances.into_iter().collect(),

Check warning on line 35 in src/recovery_securify_cache/derivation_and_analysis/input.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/derivation_and_analysis/input.rs#L35

Added line #L35 was not covered by tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub struct DerivationAndAnalysis {
/// Securified entities that were not recovered
pub unrecovered_securified_entities: UnrecoveredSecurifiedEntities,

virtual_entity_creating_instances: Vec<HierarchicalDeterministicFactorInstance>,
pub virtual_entity_creating_instances: VirtualEntityCreatingInstances,

/// Used FactorSources which are not new - might be empty
old_factor_sources: Vec<HDFactorSource>,
Expand All @@ -45,12 +45,14 @@ pub struct DerivationAndAnalysis {
impl DerivationAndAnalysis {
/// # Panics
/// Panics if `old_factor_sources` intersects with `new_factor_sources`
///
/// Panics if the collections of factor instances are not disjoint
pub fn new(

Check warning on line 50 in src/recovery_securify_cache/derivation_and_analysis/output/derivation_and_analysis.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/derivation_and_analysis/output/derivation_and_analysis.rs#L50

Added line #L50 was not covered by tests
probably_free_instances: ProbablyFreeFactorInstances,
recovered_unsecurified_entities: RecoveredUnsecurifiedEntities,
recovered_securified_entities: RecoveredSecurifiedEntities,
unrecovered_securified_entities: UnrecoveredSecurifiedEntities,
virtual_entity_creating_instances: IndexSet<HierarchicalDeterministicFactorInstance>,
virtual_entity_creating_instances: VirtualEntityCreatingInstances,
old_factor_sources: IndexSet<HDFactorSource>,
new_factor_sources: IndexSet<HDFactorSource>,
) -> Self {
Expand All @@ -61,28 +63,24 @@ impl DerivationAndAnalysis {
.is_empty(),
"Discrepancy! FactorSource found in old an new, this is a programmer error!"
);
assert_are_factor_instance_collections_disjoint(vec![

Check warning on line 66 in src/recovery_securify_cache/derivation_and_analysis/output/derivation_and_analysis.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/derivation_and_analysis/output/derivation_and_analysis.rs#L66

Added line #L66 was not covered by tests
&probably_free_instances,
&recovered_unsecurified_entities,
&recovered_securified_entities,
&unrecovered_securified_entities,
&virtual_entity_creating_instances,
]);
Self {
probably_free_instances,
recovered_unsecurified_entities,
recovered_securified_entities,
unrecovered_securified_entities,
virtual_entity_creating_instances: virtual_entity_creating_instances
.into_iter()
.collect(),
virtual_entity_creating_instances,
old_factor_sources: old_factor_sources.into_iter().collect(),
new_factor_sources: new_factor_sources.into_iter().collect(),

Check warning on line 80 in src/recovery_securify_cache/derivation_and_analysis/output/derivation_and_analysis.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/derivation_and_analysis/output/derivation_and_analysis.rs#L79-L80

Added lines #L79 - L80 were not covered by tests
}
}

pub fn virtual_entity_creating_instances(
&self,
) -> IndexSet<HierarchicalDeterministicFactorInstance> {
self.virtual_entity_creating_instances
.clone()
.into_iter()
.collect()
}

pub fn new_factor_sources(&self) -> IndexSet<HDFactorSource> {
self.new_factor_sources.clone().into_iter().collect()

Check warning on line 85 in src/recovery_securify_cache/derivation_and_analysis/output/derivation_and_analysis.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/derivation_and_analysis/output/derivation_and_analysis.rs#L84-L85

Added lines #L84 - L85 were not covered by tests
}
Expand All @@ -105,10 +103,7 @@ impl HasSampleValues for DerivationAndAnalysis {
RecoveredUnsecurifiedEntities::sample(),
RecoveredSecurifiedEntities::sample(),
UnrecoveredSecurifiedEntities::sample(),
IndexSet::from_iter([
HierarchicalDeterministicFactorInstance::sample(),
HierarchicalDeterministicFactorInstance::sample_other(),
]),
VirtualEntityCreatingInstances::sample(),
IndexSet::just(HDFactorSource::sample()),
IndexSet::just(HDFactorSource::sample_other()),

Check warning on line 108 in src/recovery_securify_cache/derivation_and_analysis/output/derivation_and_analysis.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/derivation_and_analysis/output/derivation_and_analysis.rs#L102-L108

Added lines #L102 - L108 were not covered by tests
)
Expand All @@ -120,7 +115,7 @@ impl HasSampleValues for DerivationAndAnalysis {
RecoveredUnsecurifiedEntities::sample_other(),
RecoveredSecurifiedEntities::sample_other(),
UnrecoveredSecurifiedEntities::sample_other(),
IndexSet::just(HierarchicalDeterministicFactorInstance::sample_other()),
VirtualEntityCreatingInstances::sample(),
IndexSet::just(HDFactorSource::sample_other()),
IndexSet::just(HDFactorSource::sample()),

Check warning on line 120 in src/recovery_securify_cache/derivation_and_analysis/output/derivation_and_analysis.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/derivation_and_analysis/output/derivation_and_analysis.rs#L114-L120

Added lines #L114 - L120 were not covered by tests
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod securified_entity;
mod unrecovered_securified_entities;
mod unrecovered_securified_entity;
mod unsecurified_entity;
mod virtual_entity_creating_instances;

pub use derivation_and_analysis::*;
pub use probably_free_factor_instances::*;
Expand All @@ -15,3 +16,4 @@ pub use securified_entity::*;
pub use unrecovered_securified_entities::*;
pub use unrecovered_securified_entity::*;
pub use unsecurified_entity::*;
pub use virtual_entity_creating_instances::*;
Original file line number Diff line number Diff line change
@@ -1,5 +1,46 @@
use crate::prelude::*;

pub trait IsFactorInstanceCollectionBase {
fn factor_instances(&self) -> IndexSet<HierarchicalDeterministicFactorInstance>;
}
pub trait IsFactorInstanceCollection:
IsFactorInstanceCollectionBase + HasSampleValues + Sized
{
fn new(instances: IndexSet<HierarchicalDeterministicFactorInstance>) -> Self;

fn merge<T: IsFactorInstanceCollection>(&self, other: T) -> Self {

Check warning on line 11 in src/recovery_securify_cache/derivation_and_analysis/output/probably_free_factor_instances.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/derivation_and_analysis/output/probably_free_factor_instances.rs#L11

Added line #L11 was not covered by tests
Self::new(
self.factor_instances()
.union(&other.factor_instances())
.cloned()
.collect(),

Check warning on line 16 in src/recovery_securify_cache/derivation_and_analysis/output/probably_free_factor_instances.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/derivation_and_analysis/output/probably_free_factor_instances.rs#L13-L16

Added lines #L13 - L16 were not covered by tests
)
}
}

pub fn are_factor_instance_collections_disjoint(

Check warning on line 21 in src/recovery_securify_cache/derivation_and_analysis/output/probably_free_factor_instances.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/derivation_and_analysis/output/probably_free_factor_instances.rs#L21

Added line #L21 was not covered by tests
collections: Vec<&dyn IsFactorInstanceCollectionBase>,
) -> bool {
let mut all_instances = IndexSet::new();
for collection in collections {
let instances = collection.factor_instances();
if !instances.is_disjoint(&all_instances) {
return false;

Check warning on line 28 in src/recovery_securify_cache/derivation_and_analysis/output/probably_free_factor_instances.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/derivation_and_analysis/output/probably_free_factor_instances.rs#L24-L28

Added lines #L24 - L28 were not covered by tests
}
all_instances.extend(instances);

Check warning on line 30 in src/recovery_securify_cache/derivation_and_analysis/output/probably_free_factor_instances.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/derivation_and_analysis/output/probably_free_factor_instances.rs#L30

Added line #L30 was not covered by tests
}
true

Check warning on line 32 in src/recovery_securify_cache/derivation_and_analysis/output/probably_free_factor_instances.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/derivation_and_analysis/output/probably_free_factor_instances.rs#L32

Added line #L32 was not covered by tests
}

pub fn assert_are_factor_instance_collections_disjoint(

Check warning on line 35 in src/recovery_securify_cache/derivation_and_analysis/output/probably_free_factor_instances.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/derivation_and_analysis/output/probably_free_factor_instances.rs#L35

Added line #L35 was not covered by tests
collections: Vec<&dyn IsFactorInstanceCollectionBase>,
) {
assert!(

Check warning on line 38 in src/recovery_securify_cache/derivation_and_analysis/output/probably_free_factor_instances.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/derivation_and_analysis/output/probably_free_factor_instances.rs#L38

Added line #L38 was not covered by tests
are_factor_instance_collections_disjoint(collections),
"Discrepancy! FactorInstance found in multiple collections, this is a programmer error!"
);
}

/// "Probably" since we might not have all the information to be sure, since
/// Gateway might not keep track of past FactorInstances, some of the FactorInstances
/// in KeySpace::Securified might in fact have been used in the past for some entity.
Expand All @@ -8,23 +49,17 @@ pub struct ProbablyFreeFactorInstances {
factor_instances: Vec<HierarchicalDeterministicFactorInstance>,
}

impl ProbablyFreeFactorInstances {
pub fn new(instances: IndexSet<HierarchicalDeterministicFactorInstance>) -> Self {
impl IsFactorInstanceCollectionBase for ProbablyFreeFactorInstances {
fn factor_instances(&self) -> IndexSet<HierarchicalDeterministicFactorInstance> {
self.factor_instances.iter().cloned().collect()

Check warning on line 54 in src/recovery_securify_cache/derivation_and_analysis/output/probably_free_factor_instances.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/derivation_and_analysis/output/probably_free_factor_instances.rs#L53-L54

Added lines #L53 - L54 were not covered by tests
}
}
impl IsFactorInstanceCollection for ProbablyFreeFactorInstances {
fn new(instances: IndexSet<HierarchicalDeterministicFactorInstance>) -> Self {
Self {
factor_instances: instances.into_iter().collect(),
}
}
pub fn merge(&self, other: &Self) -> Self {
Self::new(
self.instances()
.union(&other.instances())
.cloned()
.collect(),
)
}
pub fn instances(&self) -> IndexSet<HierarchicalDeterministicFactorInstance> {
self.factor_instances.iter().cloned().collect()
}
}

impl HasSampleValues for ProbablyFreeFactorInstances {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ impl RecoveredSecurifiedEntities {
.map(AccountOrPersona::from)
.collect()
}
}

pub fn instances(&self) -> IndexSet<HierarchicalDeterministicFactorInstance> {
impl IsFactorInstanceCollectionBase for RecoveredSecurifiedEntities {
fn factor_instances(&self) -> IndexSet<HierarchicalDeterministicFactorInstance> {
self.securified_entities()

Check warning on line 31 in src/recovery_securify_cache/derivation_and_analysis/output/recovered_securified_entities.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/derivation_and_analysis/output/recovered_securified_entities.rs#L30-L31

Added lines #L30 - L31 were not covered by tests
.into_iter()
.flat_map(|x| x.securified_entity_control().all_factor_instances())

Check warning on line 33 in src/recovery_securify_cache/derivation_and_analysis/output/recovered_securified_entities.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/derivation_and_analysis/output/recovered_securified_entities.rs#L33

Added line #L33 was not covered by tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ impl RecoveredUnsecurifiedEntities {
.map(AccountOrPersona::from)
.collect()
}
}

pub fn instances(&self) -> IndexSet<HierarchicalDeterministicFactorInstance> {
impl IsFactorInstanceCollectionBase for RecoveredUnsecurifiedEntities {
fn factor_instances(&self) -> IndexSet<HierarchicalDeterministicFactorInstance> {
self.unsecurified_entities()

Check warning on line 31 in src/recovery_securify_cache/derivation_and_analysis/output/recovered_unsecurified_entities.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/derivation_and_analysis/output/recovered_unsecurified_entities.rs#L30-L31

Added lines #L30 - L31 were not covered by tests
.into_iter()
.map(|x| x.veci())
.map(|x| x.factor_instance())

Check warning on line 33 in src/recovery_securify_cache/derivation_and_analysis/output/recovered_unsecurified_entities.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/derivation_and_analysis/output/recovered_unsecurified_entities.rs#L33

Added line #L33 was not covered by tests
.collect()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ impl UnrecoveredSecurifiedEntities {
pub fn entities(&self) -> IndexSet<UnrecoveredSecurifiedEntity> {
self.entities.clone().into_iter().collect()

Check warning on line 21 in src/recovery_securify_cache/derivation_and_analysis/output/unrecovered_securified_entities.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/derivation_and_analysis/output/unrecovered_securified_entities.rs#L20-L21

Added lines #L20 - L21 were not covered by tests
}
}

pub fn instances(&self) -> IndexSet<HierarchicalDeterministicFactorInstance> {
impl IsFactorInstanceCollectionBase for UnrecoveredSecurifiedEntities {
fn factor_instances(&self) -> IndexSet<HierarchicalDeterministicFactorInstance> {
self.entities()

Check warning on line 27 in src/recovery_securify_cache/derivation_and_analysis/output/unrecovered_securified_entities.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/derivation_and_analysis/output/unrecovered_securified_entities.rs#L26-L27

Added lines #L26 - L27 were not covered by tests
.into_iter()
.flat_map(|x| x.matched_factor_instances())

Check warning on line 29 in src/recovery_securify_cache/derivation_and_analysis/output/unrecovered_securified_entities.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/derivation_and_analysis/output/unrecovered_securified_entities.rs#L29

Added line #L29 was not covered by tests
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
use std::vec;

use crate::prelude::*;

/// The HDFactorInstance, address and possibly third party deposit state of some
/// unsecurified entity.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct UnsecurifiedEntity {
/// The address which is verified to match the `veci`
address: AddressOfAccountOrPersona,

/// Virtual Entity Creating (Factor)Instance
veci: HierarchicalDeterministicFactorInstance,
veci: VirtualEntityCreatingInstance,

/// If we found this UnsecurifiedEntity while scanning OnChain using
/// Gateway, we might have been able to read out the third party deposit
Expand All @@ -17,50 +15,36 @@ pub struct UnsecurifiedEntity {
}

impl UnsecurifiedEntity {
/// # Panics
/// Panics if address does not match `veci`
pub fn new(
address: AddressOfAccountOrPersona,
veci: HierarchicalDeterministicFactorInstance,
pub fn with_veci(
veci: VirtualEntityCreatingInstance,
third_party_deposit: impl Into<Option<ThirdPartyDepositPreference>>,
) -> Self {
assert!(
address.derived_from_factor_instance(&veci),
"Discrepancy, mismatching public keys, this is a programmer error!"
);
Self {
address,
veci,
third_party_deposit: third_party_deposit.into(),
}
}

fn with_veci_on_network(
veci: HierarchicalDeterministicFactorInstance,
entity_kind: CAP26EntityKind,
network_id: NetworkID,
/// # Panics
/// Panics if address does not match `factor_instance`
pub fn new(

Check warning on line 30 in src/recovery_securify_cache/derivation_and_analysis/output/unsecurified_entity.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/derivation_and_analysis/output/unsecurified_entity.rs#L30

Added line #L30 was not covered by tests
address: AddressOfAccountOrPersona,
factor_instance: HierarchicalDeterministicFactorInstance,
third_party_deposit: impl Into<Option<ThirdPartyDepositPreference>>,
) -> Self {
let public_key_hash = veci.public_key_hash();
let address = match entity_kind {
CAP26EntityKind::Account => {
AddressOfAccountOrPersona::from(AccountAddress::new(network_id, public_key_hash))
}
CAP26EntityKind::Identity => {
AddressOfAccountOrPersona::from(IdentityAddress::new(network_id, public_key_hash))
}
};
Self {
address,
veci,
third_party_deposit: None,
}
let veci = VirtualEntityCreatingInstance::new(factor_instance, address);
Self::with_veci(veci, third_party_deposit)

Check warning on line 36 in src/recovery_securify_cache/derivation_and_analysis/output/unsecurified_entity.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/derivation_and_analysis/output/unsecurified_entity.rs#L35-L36

Added lines #L35 - L36 were not covered by tests
}

pub fn address(&self) -> AddressOfAccountOrPersona {
self.address.clone()
self.veci.clone().address()

Check warning on line 40 in src/recovery_securify_cache/derivation_and_analysis/output/unsecurified_entity.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/derivation_and_analysis/output/unsecurified_entity.rs#L39-L40

Added lines #L39 - L40 were not covered by tests
}

pub fn factor_instance(&self) -> HierarchicalDeterministicFactorInstance {
self.veci.factor_instance()

Check warning on line 44 in src/recovery_securify_cache/derivation_and_analysis/output/unsecurified_entity.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/derivation_and_analysis/output/unsecurified_entity.rs#L43-L44

Added lines #L43 - L44 were not covered by tests
}

pub fn veci(&self) -> HierarchicalDeterministicFactorInstance {
pub fn veci(&self) -> VirtualEntityCreatingInstance {
self.veci.clone()

Check warning on line 48 in src/recovery_securify_cache/derivation_and_analysis/output/unsecurified_entity.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/derivation_and_analysis/output/unsecurified_entity.rs#L47-L48

Added lines #L47 - L48 were not covered by tests
}

Expand All @@ -73,7 +57,7 @@ impl From<UnsecurifiedEntity> for AccountOrPersona {
fn from(value: UnsecurifiedEntity) -> Self {
let address = value.address();
let name = "Recovered";
let security_state = EntitySecurityState::Unsecured(value.veci());
let security_state = EntitySecurityState::Unsecured(value.factor_instance());

if let Ok(account_address) = address.clone().into_account() {
Account::new(name, account_address, security_state).into()
Expand All @@ -87,17 +71,15 @@ impl From<UnsecurifiedEntity> for AccountOrPersona {

impl HasSampleValues for UnsecurifiedEntity {
fn sample() -> Self {
Self::with_veci_on_network(
HierarchicalDeterministicFactorInstance::sample(),
CAP26EntityKind::Account,
NetworkID::Mainnet,
Self::with_veci(
VirtualEntityCreatingInstance::sample(),
ThirdPartyDepositPreference::sample(),
)
}
fn sample_other() -> Self {
Self::with_veci_on_network(
HierarchicalDeterministicFactorInstance::sample_other(),
CAP26EntityKind::Identity,
NetworkID::Stokenet,
Self::with_veci(
VirtualEntityCreatingInstance::sample_other(),
ThirdPartyDepositPreference::sample_other(),
)
}
}
Expand Down
Loading

0 comments on commit ca1dfd9

Please sign in to comment.