diff --git a/src/derivation/collector/keys_collector.rs b/src/derivation/collector/keys_collector.rs index ae0093b4..bf842e8b 100644 --- a/src/derivation/collector/keys_collector.rs +++ b/src/derivation/collector/keys_collector.rs @@ -23,23 +23,23 @@ impl KeysCollector { all_factor_sources_in_profile: impl Into>, interactors: Arc, preprocessor: KeysCollectorPreprocessor, - ) -> Self { + ) -> Result { let all_factor_sources_in_profile = all_factor_sources_in_profile.into(); - let (state, factors) = preprocessor.preprocess(all_factor_sources_in_profile); + let (state, factors) = preprocessor.preprocess(all_factor_sources_in_profile)?; let dependencies = KeysCollectorDependencies::new(interactors, factors); - Self { + Ok(Self { dependencies, state: RefCell::new(state), - } + }) } pub fn new( all_factor_sources_in_profile: IndexSet, derivation_paths: IndexMap>, interactors: Arc, - ) -> Self { + ) -> Result { let preprocessor = KeysCollectorPreprocessor::new(derivation_paths); Self::with_preprocessor(all_factor_sources_in_profile, interactors, preprocessor) } diff --git a/src/derivation/collector/keys_collector_preprocessor.rs b/src/derivation/collector/keys_collector_preprocessor.rs index c15dd664..1b191358 100644 --- a/src/derivation/collector/keys_collector_preprocessor.rs +++ b/src/derivation/collector/keys_collector_preprocessor.rs @@ -13,25 +13,27 @@ impl KeysCollectorPreprocessor { pub(crate) fn preprocess( &self, all_factor_sources_in_profile: IndexSet, - ) -> (KeysCollectorState, IndexSet) { + ) -> Result<(KeysCollectorState, IndexSet)> { let all_factor_sources_in_profile = all_factor_sources_in_profile .into_iter() .map(|f| (f.factor_source_id(), f)) .collect::>(); - let factor_sources_of_kind = sort_group_factors( - self.derivation_paths - .clone() - .keys() - .map(|id| { - all_factor_sources_in_profile - .get(id) - .expect("Should have all factor sources") - .clone() - }) - .collect::>(), - ); + let unsorted = self + .derivation_paths + .clone() + .keys() + .map(|id| { + all_factor_sources_in_profile + .get(id) + .cloned() + .ok_or(CommonError::UnknownFactorSource) + }) + .collect::>>()?; + + let factor_sources_of_kind = sort_group_factors(unsorted); let state = KeysCollectorState::new(self.derivation_paths.clone()); - (state, factor_sources_of_kind) + + Ok((state, factor_sources_of_kind)) } } diff --git a/src/testing/derivation/test_keys_collector.rs b/src/testing/derivation/test_keys_collector.rs index 5e7e7a3d..73a5b6ce 100644 --- a/src/testing/derivation/test_keys_collector.rs +++ b/src/testing/derivation/test_keys_collector.rs @@ -158,6 +158,7 @@ impl KeysCollector { derivation_paths.into_iter().collect(), Arc::new(TestDerivationInteractors::default()), ) + .unwrap() } pub fn new_test( diff --git a/tests/main.rs b/tests/main.rs index aec79a44..8a11d699 100644 --- a/tests/main.rs +++ b/tests/main.rs @@ -39,7 +39,25 @@ mod key_derivation_tests { use super::*; #[actix_rt::test] - async fn failure() { + async fn failure_unknown_factor() { + let res = KeysCollector::new( + IndexSet::new(), + IndexMap::from_iter([( + FactorSourceIDFromHash::fs0(), + IndexSet::from_iter([DerivationPath::new( + Mainnet, + Account, + T9n, + HDPathComponent::securified(0), + )]), + )]), + Arc::new(TestDerivationInteractors::default()), + ); + assert!(matches!(res, Err(CommonError::UnknownFactorSource))); + } + + #[actix_rt::test] + async fn failure_from_interactor() { let factor_source = fs_at(0); let paths = [0, 1, 2] .into_iter() @@ -51,9 +69,9 @@ mod key_derivation_tests { .into_iter() .collect::>>(), Arc::new(TestDerivationInteractors::fail()), - ); + ) + .unwrap(); let outcome = collector.collect_keys().await; - println!("{:#?}", outcome); assert!(outcome.all_factors().is_empty()) }