From 8ab2f512e93bdc77229efc138f6979670e824ab4 Mon Sep 17 00:00:00 2001 From: Alexander Cyon Date: Wed, 18 Sep 2024 10:22:53 +0200 Subject: [PATCH] playground: sketching ideas based on the important realization that AddFactorSource, Recovery, Derive&Securify-Without-or-with-Empty-Cache are all essentially the same operation. Deriving many keys, scanning Gateway (and Profile if not recovery) and matching against those 'filters' known keys and putting beleived-to-be-'free' keys into the cache. --- src/factor_instance_provider/mod.rs | 1 + src/lib.rs | 3 + src/playground.rs | 88 +++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+) create mode 100644 src/playground.rs diff --git a/src/factor_instance_provider/mod.rs b/src/factor_instance_provider/mod.rs index 703104b0..243fadca 100644 --- a/src/factor_instance_provider/mod.rs +++ b/src/factor_instance_provider/mod.rs @@ -11,5 +11,6 @@ pub use cache::*; pub use derivation_request::*; pub use factor_instance_provider::*; pub use gateway::*; +use indexmap::IndexSet; pub use profile_extensions::*; pub use securify::*; diff --git a/src/lib.rs b/src/lib.rs index 15f358e5..88fc1a2d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,6 +7,7 @@ mod derivation; mod factor_instance_provider; +mod playground; mod recovery; mod samples; mod signing; @@ -34,6 +35,8 @@ pub mod prelude { pub(crate) use indexmap::{IndexMap, IndexSet}; pub(crate) use itertools::Itertools; pub(crate) use std::cell::RefCell; + pub(crate) use std::future::Future; + pub(crate) use std::pin::Pin; pub(crate) use std::time::SystemTime; pub(crate) use uuid::Uuid; diff --git a/src/playground.rs b/src/playground.rs new file mode 100644 index 00000000..3626e398 --- /dev/null +++ b/src/playground.rs @@ -0,0 +1,88 @@ +#![allow(unused)] + +use crate::prelude::*; + +/// "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. +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct ProbablyFreeFactorInstances(IndexSet); + +enum ScanHookDecision { + /// "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. + ProbablyIsFree(HierarchicalDeterministicFactorInstance), + UnsecurifiedEntityRecovered(AccountOrPersona), + SecurifiedEntityReferencesFactor { + entity_address: AddressOfAccountOrPersona, + factor_instance: HierarchicalDeterministicFactorInstance, + }, +} + +type OnFactorInstance = Box< + dyn FnOnce( + HierarchicalDeterministicFactorInstance, + ) -> Pin>>, +>; +struct ScanHook { + on_factor_instance: OnFactorInstance, +} + +async fn scan( + factor_sources: IndexSet, + profile_scan_hook: impl Into>, + gateway_scan_hook: impl Into>, +) -> Result<(IndexSet, ProbablyFreeFactorInstances)> { + todo!() +} +impl dyn GatewayReadonly { + fn scan_hook(&self) -> ScanHook { + todo!() + } +} +impl Profile { + fn scan_hook(&self) -> ScanHook { + todo!() + } + + async fn add_factor_source( + &mut self, + factor_source: HDFactorSource, + derivation_interactors: Arc, + gateway_scan_hook: ScanHook, + ) -> Result<()> { + let (found_entities, probably_free) = scan( + IndexSet::just(factor_source), + self.scan_hook(), + gateway_scan_hook, + ) + .await?; + todo!() + } + + async fn add_factor_source_with_gateway( + &mut self, + factor_source: HDFactorSource, + derivation_interactors: Arc, + gateway: Arc, + ) -> Result<()> { + self.add_factor_source(factor_source, derivation_interactors, gateway.scan_hook()) + .await + } +} + +async fn recovery( + factor_sources: IndexSet, + gateway_scan_hook: ScanHook, +) -> Result<(Profile, ProbablyFreeFactorInstances)> { + let (found_entities, probably_free) = scan(factor_sources, None, gateway_scan_hook).await?; + todo!() +} + +async fn recovery_with_gateway( + factor_sources: IndexSet, + gateway: Arc, +) -> Result<(Profile, ProbablyFreeFactorInstances)> { + recovery(factor_sources, gateway.scan_hook()).await +}