Skip to content

Commit

Permalink
sketch of god method
Browse files Browse the repository at this point in the history
  • Loading branch information
CyonAlexRDX committed Sep 20, 2024
1 parent 46d08fa commit 17cabf3
Show file tree
Hide file tree
Showing 11 changed files with 193 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::prelude::*;

async fn _account_recovery_scan(

Check warning on line 3 in src/recovery_securify_cache/account_recover_scan/account_recovery_scanning.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/account_recover_scan/account_recovery_scanning.rs#L3

Added line #L3 was not covered by tests
input: DeriveAndAnalyzeInputAccountRecoveryScan,
input: DeriveAndAnalyzeAccountRecoveryScanInput,
) -> Result<DerivationAndAnalysisAccountRecoveryScan> {
let input = DeriveAndAnalyzeInput::from(input);
let analysis = derive_and_analyze(input).await?;
Expand All @@ -11,10 +11,12 @@ async fn _account_recovery_scan(
pub async fn account_recovery_scan(

Check warning on line 11 in src/recovery_securify_cache/account_recover_scan/account_recovery_scanning.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/account_recover_scan/account_recovery_scanning.rs#L11

Added line #L11 was not covered by tests
factor_sources: IndexSet<HDFactorSource>,
gateway: Arc<dyn Gateway>,
derivation_interactors: Arc<dyn KeysDerivationInteractors>,
) -> Result<AccountRecoveryScanOutcome> {
let analysis = _account_recovery_scan(DeriveAndAnalyzeInputAccountRecoveryScan::new(
let analysis = _account_recovery_scan(DeriveAndAnalyzeAccountRecoveryScanInput::new(
factor_sources,
gateway,
derivation_interactors,
))
.await?;
Ok(AccountRecoveryScanOutcome::from(analysis))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#![allow(unused)]
#![allow(unused_variables)]

use crate::prelude::*;

pub struct DeriveAndAnalyzeAccountRecoveryScanInput {
factor_sources: IndexSet<HDFactorSource>,
gateway: Arc<dyn Gateway>,
derivation_interactors: Arc<dyn KeysDerivationInteractors>,
}
impl DeriveAndAnalyzeAccountRecoveryScanInput {
pub fn new(

Check warning on line 12 in src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs#L12

Added line #L12 was not covered by tests
factor_sources: IndexSet<HDFactorSource>,
gateway: Arc<dyn Gateway>,
derivation_interactors: Arc<dyn KeysDerivationInteractors>,
) -> Self {
Self {
factor_sources,
gateway,
derivation_interactors,
}
}
}
impl From<DeriveAndAnalyzeAccountRecoveryScanInput> for DeriveAndAnalyzeInput {
fn from(value: DeriveAndAnalyzeAccountRecoveryScanInput) -> Self {
let next_derivation_entity_index_assigner = NextDerivationEntityIndexAssigner::ars();

Check warning on line 26 in src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs#L25-L26

Added lines #L25 - L26 were not covered by tests

let analyze_factor_instances = IntermediaryDerivationAnalyzer::ars(value.gateway);

Check warning on line 28 in src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs#L28

Added line #L28 was not covered by tests

Self::new(
value.factor_sources.clone(),
value

Check warning on line 32 in src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs#L31-L32

Added lines #L31 - L32 were not covered by tests
.factor_sources
.into_iter()
.map(|f| f.factor_source_id())

Check warning on line 35 in src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs#L35

Added line #L35 was not covered by tests
.collect(),
next_derivation_entity_index_assigner,
analyze_factor_instances,

Check warning on line 38 in src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs#L37-L38

Added lines #L37 - L38 were not covered by tests
)
}
}

impl NextDerivationEntityIndexAssigner {
pub fn ars() -> Self {

Check warning on line 44 in src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs#L44

Added line #L44 was not covered by tests
todo!()
}
}

impl IntermediaryDerivationAnalyzer {
pub fn ars(gateway: Arc<dyn Gateway>) -> Self {

Check warning on line 50 in src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/account_recover_scan/derive_and_analyze_account_recovery_scan_input.rs#L50

Added line #L50 was not covered by tests
todo!()
}
}

This file was deleted.

4 changes: 2 additions & 2 deletions src/recovery_securify_cache/account_recover_scan/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
mod account_recovery_scan_outcome;
mod account_recovery_scanning;
mod derivation_and_analysis_account_recovery_scan;
mod derive_and_analyze_input_account_recovery_scan;
mod derive_and_analyze_account_recovery_scan_input;

pub use account_recovery_scan_outcome::*;
pub use account_recovery_scanning::*;
pub use derivation_and_analysis_account_recovery_scan::*;
pub use derive_and_analyze_input_account_recovery_scan::*;
pub use derive_and_analyze_account_recovery_scan_input::*;
111 changes: 98 additions & 13 deletions src/recovery_securify_cache/derivation_and_analysis/input.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,108 @@
#![allow(unused)]

use std::ops::Index;

use crate::prelude::*;

pub struct ProfileNextIndexAnalyzer {}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct IntermediaryDerivationAnalysis {
all_factor_instances: Vec<HierarchicalDeterministicFactorInstance>,
pub probably_free_instances: ProbablyFreeFactorInstances,
pub recovered_unsecurified_entities: RecoveredUnsecurifiedEntities,
pub recovered_securified_entities: RecoveredSecurifiedEntities,
pub unrecovered_securified_entities: UnrecoveredSecurifiedEntities,
}
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
pub fn new(

Check warning on line 23 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#L23

Added line #L23 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());

Check warning on line 33 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#L30-L33

Added lines #L30 - L33 were not covered by tests

assert!(

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
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());

Check warning on line 45 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#L45

Added line #L45 was not covered by tests

assert_eq!(

Check warning on line 47 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#L47

Added line #L47 was not covered by tests
merge.into_iter().collect::<HashSet<_>>(),
all_factor_instances
.clone()
.into_iter()
.collect::<HashSet<_>>()
);

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

Check warning on line 56 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#L56

Added line #L56 was not covered by tests
probably_free_instances,
recovered_unsecurified_entities,
recovered_securified_entities,
unrecovered_securified_entities,
}
}
}

pub struct DerivationRequest {
pub factor_source_id: FactorSourceIDFromHash,
pub network_id: NetworkID,
pub entity_kind: CAP26EntityKind,
pub key_space: KeySpace,
pub key_kind: CAP26KeyKind,
}

/// Lookup which Entity Range Start Value to use for the Range of Derivation Indices
/// to uses per request.
///
/// E.g. when doing recovery scan of unsecurified accounts we would return:
/// `HDPathComponent::unsecurified_hardening_base_index(0)` as start value of a range of lets say
/// 30 indices, for `DeviceFactorSource`s, given a request:
/// `DerivationRequest { entity_kind: Account, key_space: Unsecurified, key_kind: TransactionSigning, ... }`
///
/// But for VECID - Virtual Entity Creating (Factor)Instance Derivation request:
/// `DerivationRequest { entity_kind: Account, key_space: Unsecurified, key_kind: TransactionSigning, ... }`
/// we would use "next free" based on cache or profile analysis.
pub struct NextDerivationEntityIndexAssigner {
start_index: Arc<dyn Fn(DerivationRequest) -> HDPathComponent>,
}

/// Check if there is any known entity associated with a given factor instance,
/// if so, some base info, if not, it is counted as "probably free".
pub struct IntermediaryDerivationAnalyzer {
analuze: Arc<
dyn Fn(IndexSet<HierarchicalDeterministicFactorInstance>) -> IntermediaryDerivationAnalysis,
>,
}

pub struct DeriveAndAnalyzeInput {
factor_sources: IndexSet<HDFactorSource>,
ids_of_new_factor_sources: IndexSet<FactorSourceIDFromHash>,
cache: Option<PreDerivedKeysCache>,
gateway: Arc<dyn Gateway>,

/// "Gateway required"
is_onchain_analysis_required: bool,
/// Which index to start at for the range of indices to derive
next_derivation_entity_index_assigner: NextDerivationEntityIndexAssigner,

profile_next_index_analyzer: Option<ProfileNextIndexAnalyzer>,
/// Check if there is any known entity associated with a given factor instance,
/// if so, some base info, if not, it is counted as "probably free".
analyze_factor_instances: IntermediaryDerivationAnalyzer,
}

impl DeriveAndAnalyzeInput {
Expand Down Expand Up @@ -47,10 +136,8 @@ impl DeriveAndAnalyzeInput {
pub fn new(

Check warning on line 136 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#L136

Added line #L136 was not covered by tests
factor_sources: IndexSet<HDFactorSource>,
ids_of_new_factor_sources: IndexSet<FactorSourceIDFromHash>,
cache: Option<PreDerivedKeysCache>,
gateway: Arc<dyn Gateway>,
is_onchain_analysis_required: bool,
profile_next_index_analyzer: Option<ProfileNextIndexAnalyzer>,
next_derivation_entity_index_assigner: NextDerivationEntityIndexAssigner,
analyze_factor_instances: IntermediaryDerivationAnalyzer,
) -> Self {
assert!(

Check warning on line 142 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#L142

Added line #L142 was not covered by tests
ids_of_new_factor_sources
Expand All @@ -62,10 +149,8 @@ impl DeriveAndAnalyzeInput {
Self {
factor_sources,
ids_of_new_factor_sources,
cache,
gateway,
is_onchain_analysis_required,
profile_next_index_analyzer,
next_derivation_entity_index_assigner,
analyze_factor_instances,
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ impl ProbablyFreeFactorInstances {
factor_instances: instances.into_iter().collect(),
}
}
pub fn merge(&self, other: &Self) -> Self {

Check warning on line 17 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#L17

Added line #L17 was not covered by tests
Self::new(
self.instances()
.union(&other.instances())

Check warning on line 20 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#L19-L20

Added lines #L19 - L20 were not covered by tests
.cloned()
.collect(),
)
}
pub fn instances(&self) -> IndexSet<HierarchicalDeterministicFactorInstance> {
self.factor_instances.iter().cloned().collect()

Check warning on line 26 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#L25-L26

Added lines #L25 - L26 were not covered by tests
}
}

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

pub fn instances(&self) -> IndexSet<HierarchicalDeterministicFactorInstance> {
self.securified_entities()

Check warning on line 29 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#L28-L29

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

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#L31

Added line #L31 was not covered by tests
.collect()
}
}

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

pub fn instances(&self) -> IndexSet<HierarchicalDeterministicFactorInstance> {
self.unsecurified_entities()

Check warning on line 29 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#L28-L29

Added lines #L28 - L29 were not covered by tests
.into_iter()
.map(|x| x.veci())

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#L31

Added line #L31 was not covered by tests
.collect()
}
}

impl HasSampleValues for RecoveredUnsecurifiedEntities {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ 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> {
self.entities()

Check warning on line 25 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#L24-L25

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

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#L27

Added line #L27 was not covered by tests
.collect()
}
}

impl HasSampleValues for UnrecoveredSecurifiedEntities {
Expand Down
1 change: 1 addition & 0 deletions src/recovery_securify_cache/derive_and_analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ use crate::prelude::*;
/// analyze if FactorInstance are free.)
pub async fn derive_and_analyze(input: DeriveAndAnalyzeInput) -> Result<DerivationAndAnalysis> {

Check warning on line 68 in src/recovery_securify_cache/derive_and_analyze.rs

View check run for this annotation

Codecov / codecov/patch

src/recovery_securify_cache/derive_and_analyze.rs#L68

Added line #L68 was not covered by tests
error!("Using SAMPLE data in 'derive_and_analyze'!!!");
let mut derived_instances = IndexSet::<HierarchicalDeterministicFactorInstance>::new();

// To be fed into cache, NOT done by this function.
let probably_free_instances = ProbablyFreeFactorInstances::sample();
Expand Down
3 changes: 3 additions & 0 deletions src/types/sargon_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,9 @@ pub struct SecurifiedEntityControl {
pub access_controller: AccessController,
}
impl SecurifiedEntityControl {
pub fn all_factor_instances(&self) -> IndexSet<HierarchicalDeterministicFactorInstance> {
self.matrix.all_factors()
}
pub fn new(matrix: MatrixOfFactorInstances, access_controller: AccessController) -> Self {
Self {
matrix,
Expand Down

0 comments on commit 17cabf3

Please sign in to comment.