Skip to content

Commit

Permalink
Deriving keys purpose (#294)
Browse files Browse the repository at this point in the history
* Expose vec instead of map

* Expose keys collection reason

* Rename to derivation purpose

* Rename to derivation purpose

* PR suggestions

* Change pre-commit
  • Loading branch information
micbakos-rdx authored Dec 9, 2024
1 parent f88548e commit cfcd8b8
Show file tree
Hide file tree
Showing 19 changed files with 257 additions and 40 deletions.
5 changes: 3 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
fail_fast: true
default_install_hook_types: [pre-push]
default_stages: [pre-push]
default_install_hook_types: [pre-commit]
default_stages: [pre-commit]
repos:
- repo: https://github.com/crate-ci/typos
rev: v1.28.1
Expand Down Expand Up @@ -37,6 +37,7 @@ repos:
pass_filenames: false

- id: unit tests
stages: [pre-push]
name: unit tests
language: system
types: [file, rust]
Expand Down
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/sargon-uniffi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "sargon-uniffi"
# Don't forget to update version in crates/sargon/Cargo.toml
version = "1.1.76"
version = "1.1.77"
edition = "2021"
build = "build.rs"

Expand Down
29 changes: 29 additions & 0 deletions crates/sargon-uniffi/src/keys_collector/derivation_purpose.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::prelude::*;
use sargon::DerivationPurpose as InternalDerivationPurpose;

/// The purpose that initiated an interaction with the host to derive keys.
/// The orchestrator behind this operation is the `KeysCollector`.
#[derive(
Clone, Copy, Debug, PartialEq, Eq, InternalConversion, uniffi::Enum,
)]
pub enum DerivationPurpose {
/// When the create account flow, initiates keys collection
/// for account VECIs
CreatingNewAccount,

/// When the create persona flow, initiates keys collection
/// for identity VECIs
CreatingNewPersona,

/// When applying a security shield to an account, initiates keys collection
/// for account MFA
SecurifyingAccount,

/// When applying a security shield to a persona, initiates keys collection
/// for identity MFA
SecurifyingPersona,

/// When adding a new factor source, initiates keys collection
/// for collecting various factor instances.
PreDerivingKeys,
}
47 changes: 38 additions & 9 deletions crates/sargon-uniffi/src/keys_collector/key_derivation_request.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
use crate::prelude::*;
use sargon::indexmap::IndexMap;
use sargon::IndexMap;
use sargon::{IndexSet, KeyDerivationRequest as InternalKeyDerivationRequest};

/// A collection of derivation paths, on a per-factor-source basis.
#[derive(Clone, PartialEq, Eq, uniffi::Record)]
pub struct KeyDerivationRequest {
pub per_factor_source: HashMap<FactorSourceIDFromHash, Vec<DerivationPath>>,
/// We include this `DerivationPurpose` in dispatched use FactorSource requests to host so
/// that UI can display contextual information as to why the user is prompted to
/// authenticate FactorSource access.
pub derivation_purpose: DerivationPurpose,
pub per_factor_source: Vec<KeyDerivationRequestPerFactorSource>,
}

#[derive(Clone, PartialEq, Eq, uniffi::Record)]
pub struct KeyDerivationRequestPerFactorSource {
pub factor_source_id: FactorSourceIDFromHash,
pub derivation_paths: Vec<DerivationPath>,
}

impl KeyDerivationRequestPerFactorSource {
pub fn new(
factor_source_id: FactorSourceIDFromHash,
derivation_paths: Vec<DerivationPath>,
) -> Self {
Self {
factor_source_id,
derivation_paths,
}
}
}

impl KeyDerivationRequest {
Expand All @@ -17,11 +39,15 @@ impl KeyDerivationRequest {
impl From<InternalKeyDerivationRequest> for KeyDerivationRequest {
fn from(value: InternalKeyDerivationRequest) -> Self {
Self {
derivation_purpose: value.derivation_purpose.into(),
per_factor_source: value
.per_factor_source
.into_iter()
.map(|(k, v)| {
(k.into(), v.into_iter().map(|d| d.into()).collect())
KeyDerivationRequestPerFactorSource::new(
k.into(),
v.into_iter().map(|d| d.into()).collect(),
)
})
.collect(),
}
Expand All @@ -30,15 +56,18 @@ impl From<InternalKeyDerivationRequest> for KeyDerivationRequest {

impl From<KeyDerivationRequest> for InternalKeyDerivationRequest {
fn from(value: KeyDerivationRequest) -> Self {
Self::new(IndexMap::from_iter(
value.per_factor_source.into_iter().map(|(k, v)| {
Self::new(
value.derivation_purpose.into_internal(),
IndexMap::from_iter(value.per_factor_source.into_iter().map(|f| {
(
k.into_internal(),
f.factor_source_id.into_internal(),
IndexSet::from_iter(
v.into_iter().map(|d| d.into_internal()),
f.derivation_paths
.into_iter()
.map(|d| d.into_internal()),
),
)
}),
))
})),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ use sargon::KeyDerivationResponse as InternalKeyDerivationResponse;
/// a single `KeyDerivationPerFactorSource`.
#[derive(Clone, PartialEq, Eq, uniffi::Record)]
pub struct KeyDerivationResponse {
pub per_factor_source: Vec<KeyDerivationPerFactorSource>,
pub per_factor_source: Vec<KeyDerivationResponsePerFactorSource>,
}

#[derive(Clone, PartialEq, Eq, uniffi::Record)]
pub struct KeyDerivationPerFactorSource {
pub struct KeyDerivationResponsePerFactorSource {
pub factor_source_id: FactorSourceIDFromHash,
pub factor_instances: Vec<HierarchicalDeterministicFactorInstance>,
}

impl KeyDerivationPerFactorSource {
impl KeyDerivationResponsePerFactorSource {
pub fn new(
factor_source_id: FactorSourceIDFromHash,
factor_instances: Vec<HierarchicalDeterministicFactorInstance>,
Expand All @@ -42,7 +42,7 @@ impl From<InternalKeyDerivationResponse> for KeyDerivationResponse {
.per_factor_source
.into_iter()
.map(|(k, v)| {
KeyDerivationPerFactorSource::new(
KeyDerivationResponsePerFactorSource::new(
k.into(),
v.into_iter().map(|d| d.into()).collect(),
)
Expand Down
2 changes: 2 additions & 0 deletions crates/sargon-uniffi/src/keys_collector/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
mod derivation_purpose;
mod key_derivation_request;
mod key_derivation_response;

pub use derivation_purpose::*;
pub use key_derivation_request::*;
pub use key_derivation_response::*;
2 changes: 1 addition & 1 deletion crates/sargon/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "sargon"
# Don't forget to update version in crates/sargon-uniffi/Cargo.toml
version = "1.1.76"
version = "1.1.77"
edition = "2021"
build = "build.rs"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,16 @@ impl FactorInstancesProvider {
pub async fn provide(
self,
quantified_derivation_preset: QuantifiedDerivationPreset,
derivation_purpose: DerivationPurpose,
) -> Result<(
InstancesInCacheConsumer,
InternalFactorInstancesProviderOutcome,
)> {
let mut _self = self;

_self._provide(quantified_derivation_preset).await
_self
._provide(quantified_derivation_preset, derivation_purpose)
.await
}
}

Expand Down Expand Up @@ -84,6 +87,7 @@ impl FactorInstancesProvider {
async fn _provide(
&mut self,
quantified_derivation_preset: QuantifiedDerivationPreset,
derivation_purpose: DerivationPurpose,
) -> Result<(
InstancesInCacheConsumer,
InternalFactorInstancesProviderOutcome,
Expand Down Expand Up @@ -124,6 +128,7 @@ impl FactorInstancesProvider {
quantified_derivation_preset,
partial_instances,
quantities_to_derive,
derivation_purpose,
)
.await
}
Expand All @@ -141,11 +146,14 @@ impl FactorInstancesProvider {
FactorSourceIDFromHash,
IndexMap<DerivationPreset, usize>,
>,
derivation_purpose: DerivationPurpose,
) -> Result<(
InstancesInCacheConsumer,
InternalFactorInstancesProviderOutcome,
)> {
let pf_newly_derived = self.derive_more(pf_pdp_qty_to_derive).await?;
let pf_newly_derived = self
.derive_more(pf_pdp_qty_to_derive, derivation_purpose)
.await?;

let Split {
pf_to_use_directly,
Expand Down Expand Up @@ -257,6 +265,7 @@ impl FactorInstancesProvider {
FactorSourceIDFromHash,
IndexMap<DerivationPreset, usize>,
>,
derivation_purpose: DerivationPurpose,
) -> Result<IndexMap<FactorSourceIDFromHash, FactorInstances>> {
let factor_sources = self.factor_sources.clone();
let network_id = self.network_id;
Expand Down Expand Up @@ -303,8 +312,12 @@ impl FactorInstancesProvider {
>>()?;

let interactor = self.interactor.clone();
let collector =
KeysCollector::new(factor_sources, pf_paths.clone(), interactor)?;
let collector = KeysCollector::new(
factor_sources,
pf_paths.clone(),
interactor,
derivation_purpose,
)?;

let pf_derived = collector.collect_keys().await.factors_by_source;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ impl CacheFiller {
.map(|dp| (dp, CACHE_FILLING_QUANTITY))
.collect::<IndexMap<DerivationPreset, usize>>(),
);
let derived = provider.derive_more(quantities).await?;
let derived = provider
.derive_more(quantities, DerivationPurpose::pre_deriving_keys())
.await?;

cache_client.insert_all(&derived).await?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,13 @@ impl SecurifyEntityFactorInstancesProvider {
);

let (instances_in_cache_consumer, outcome) = provider
.provide(QuantifiedDerivationPreset::new(
DerivationPreset::mfa_entity_kind(entity_kind),
addresses_of_entities.len(),
))
.provide(
QuantifiedDerivationPreset::new(
DerivationPreset::mfa_entity_kind(entity_kind),
addresses_of_entities.len(),
),
DerivationPurpose::for_securifying_or_updating(entity_kind),
)
.await?;

Ok((instances_in_cache_consumer, outcome.into()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,13 @@ impl VirtualEntityCreatingInstanceProvider {
interactor,
);
let (instances_in_cache_consumer, outcome) = provider
.provide(QuantifiedDerivationPreset::new(
DerivationPreset::veci_entity_kind(entity_kind),
count,
))
.provide(
QuantifiedDerivationPreset::new(
DerivationPreset::veci_entity_kind(entity_kind),
count,
),
DerivationPurpose::creation_of_new_virtual_entity(entity_kind),
)
.await?;

let outcome = outcome
Expand Down
Loading

0 comments on commit cfcd8b8

Please sign in to comment.