-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge 'Keyrings' into KeysCollectorState. Break out 'Keyring' type in…
…to seperate file
- Loading branch information
1 parent
c66a239
commit a477b85
Showing
5 changed files
with
81 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use crate::prelude::*; | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct Keyring { | ||
pub factor_source_id: FactorSourceIDFromHash, | ||
pub paths: IndexSet<DerivationPath>, | ||
derived: RefCell<IndexSet<HierarchicalDeterministicFactorInstance>>, | ||
} | ||
|
||
impl Keyring { | ||
pub fn new(factor_source_id: FactorSourceIDFromHash, paths: IndexSet<DerivationPath>) -> Self { | ||
Self { | ||
factor_source_id, | ||
paths, | ||
derived: RefCell::new(IndexSet::new()), | ||
} | ||
} | ||
pub fn factors(&self) -> IndexSet<HierarchicalDeterministicFactorInstance> { | ||
self.derived.borrow().clone() | ||
} | ||
|
||
pub(crate) fn process_response( | ||
&self, | ||
response: IndexSet<HierarchicalDeterministicFactorInstance>, | ||
) { | ||
assert!(response | ||
.iter() | ||
.all(|f| f.factor_source_id == self.factor_source_id | ||
&& !self | ||
.derived | ||
.borrow() | ||
.iter() | ||
.any(|x| x.public_key == f.public_key))); | ||
|
||
self.derived.borrow_mut().extend(response) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,50 @@ | ||
use crate::prelude::*; | ||
|
||
pub struct KeysCollectorState { | ||
pub(super) keyrings: RefCell<Keyrings>, | ||
pub(super) keyrings: RefCell<IndexMap<FactorSourceIDFromHash, Keyring>>, | ||
} | ||
|
||
impl KeysCollectorState { | ||
pub fn new(keyrings: Keyrings) -> Self { | ||
pub fn new( | ||
derivation_paths: IndexMap<FactorSourceIDFromHash, IndexSet<DerivationPath>>, | ||
) -> Self { | ||
let keyrings = derivation_paths | ||
.into_iter() | ||
.map(|(factor_source_id, derivation_paths)| { | ||
( | ||
factor_source_id, | ||
Keyring::new(factor_source_id, derivation_paths), | ||
) | ||
}) | ||
.collect::<IndexMap<FactorSourceIDFromHash, Keyring>>(); | ||
Self { | ||
keyrings: RefCell::new(keyrings), | ||
} | ||
} | ||
|
||
pub fn outcome(self) -> KeyDerivationOutcome { | ||
let key_rings = self.keyrings.into_inner(); | ||
KeyDerivationOutcome::new( | ||
key_rings | ||
.into_iter() | ||
.map(|(k, v)| (k, v.factors())) | ||
.collect(), | ||
) | ||
} | ||
|
||
pub fn keyring_for(&self, factor_source_id: &FactorSourceIDFromHash) -> Option<Keyring> { | ||
self.keyrings | ||
.borrow() | ||
.get(factor_source_id) | ||
.cloned() | ||
.inspect(|k| assert_eq!(k.factor_source_id, *factor_source_id)) | ||
} | ||
|
||
pub(crate) fn process_batch_response(&self, response: BatchDerivationResponse) { | ||
self.keyrings.borrow_mut().process_batch_response(response) | ||
for (factor_source_id, factors) in response.per_factor_source.into_iter() { | ||
let mut rings = self.keyrings.borrow_mut(); | ||
let keyring = rings.get_mut(&factor_source_id).unwrap(); | ||
keyring.process_response(factors) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters