Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ABW-3969] Shield Setup Select Factors - utility functions #297

Merged
merged 7 commits into from
Dec 11, 2024

Conversation

danvleju-rdx
Copy link
Contributor

Changelog

  • Added sorted_factor_sources_for_selection function, which handles the logic for sorting a list of factor sources on the Select Factors screen.
  • Added selected_factor_sources_status function, which returns a newly added enum, SelectedFactorSourcesStatus, representing the status of selected factor sources in the Security Shield building process.
  • [Swift] Fixes the allCases for SecurityProblemKind

factor_sources: Vec<FactorSource>,
) -> SelectedFactorSourcesStatus {
if factor_sources.is_empty() {
SelectedFactorSourcesStatus::Insufficient
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm 1 is never enough for a whole shield. So this must be PER ROLE and which role is it? Primary ("Access")?

If so the enum and this method should make it clear that it is for Primary.

(Or if it is dynamic it should be for_role: RoleKind as input, and then we should not return just an enum but rather a struct with the status and the role)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Post-our sync:
Delegate this to MatrixBuilder

In MatrixBuilder:
We should probably NOT let this be a STATIC, even if we wont use the internal state of the MatrixBuilder initially we still might want to in the future. So I would do these changes:

  • change it to be on &self (i.e. not "static")
  • change it to take a for_role: RoleKind

factor_sources: Vec<FactorSource>,
) -> Vec<FactorSource> {
let mut sorted = factor_sources;
sorted.sort_by_key(|fs| fs.factor_source_kind().display_order());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the perpetual naming challenge for these kind of things... :) I think let mut sorted = factor_sources is misleading, since it is in fact not sorted yet!

Luckily In Rust we have ownership!

so:

let mut unsorted = factor_sources;
unsorted.sort....
let sorted = unsorted;
// no `unsorted` can never be referenced again! it has been consumed! Rust owns!
sorted

InternalSecurityShieldBuilder::sorted_factor_sources_for_selection(
factor_sources.into_internal(),
);
factors.into_iter().map(FactorSource::from).collect()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think:

    factors.into_iter().map(FactorSource::from).collect()

can be changed to just:

    factors.into_type()

import Foundation
import SargonUniFFI

extension SecurityShieldBuilder {
Copy link
Contributor

@Sajjon Sajjon Dec 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably not needed since should be member method, not global free standing func.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kept sortedFactorSourcesForSelection as a static function since it’s just a utility function and doesn’t need to involve the shield builder or imply that it can be modified.

Copy link

codecov bot commented Dec 9, 2024

Codecov Report

Attention: Patch coverage is 97.61905% with 1 line in your changes missing coverage. Please review.

Project coverage is 93.3%. Comparing base (217f10a) to head (636caf9).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...ityCenter/SecurityProblemKind+Wrap+Functions.swift 0.0% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@          Coverage Diff          @@
##            main    #297   +/-   ##
=====================================
  Coverage   93.3%   93.3%           
=====================================
  Files       1095    1095           
  Lines      22922   22962   +40     
  Branches      77      77           
=====================================
+ Hits       21387   21427   +40     
  Misses      1521    1521           
  Partials      14      14           
Flag Coverage Δ
kotlin 97.9% <ø> (ø)
rust 92.7% <100.0%> (+<0.1%) ⬆️
swift 94.9% <0.0%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...rity_structures/matrices/builder/matrix_builder.rs 100.0% <100.0%> (ø)
...mfa/security_structures/security_shield_builder.rs 94.5% <100.0%> (+0.7%) ⬆️
...gon/src/profile/v100/factors/factor_source_kind.rs 100.0% <100.0%> (ø)
...ityCenter/SecurityProblemKind+Wrap+Functions.swift 0.0% <0.0%> (ø)

#[derive(
Clone, Copy, Debug, PartialEq, Eq, InternalConversion, uniffi::Enum,
)]
pub enum SelectedFactorSourcesStatus {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SelectedFactorSourcesForRoleStatus ?

factor_sources: Vec<FactorSource>,
) -> SelectedFactorSourcesStatus {
if factor_sources.is_empty() {
SelectedFactorSourcesStatus::Insufficient
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Post-our sync:
Delegate this to MatrixBuilder

In MatrixBuilder:
We should probably NOT let this be a STATIC, even if we wont use the internal state of the MatrixBuilder initially we still might want to in the future. So I would do these changes:

  • change it to be on &self (i.e. not "static")
  • change it to take a for_role: RoleKind

/// The selected factor sources form an invalid combination for the specified role
/// in the Security Shield building process.
///
/// Example: A Password factor source cannot be used alone for the Primary role.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example seems to fall more under the Insufficient category

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the host’s perspective, this should fall under the Invalid category and display a generic error message. If the Primary role has threshold set to 1, then adding a Password factor source satisfies the threshold condition, but it’s still invalid. It could fall under the Insufficient category if we have a dynamic threshold that updates based on the available factor sources. Do we?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is threshold important in the context of selecting the factors before going in the Access setup build screen?

Also, if there are no roles selected the status is Insufficient, but if you have password it is considered Invalid, when IMO it is a case of insufficient - you need one more factor.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a very UI specific type and we have in the UI 3 error/warning states:

  1. User need to select at least 1 factor -> Insufficient
  2. It is recommended to select at least 2 factors -> Suboptimal
  3. Any other invalid combinations for a specific role (Access) -> Invalid

Based on that, this case falls under Invalid, even if it doesn't sound correct

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am confused here - what is any other combinations? Shouldn't we display only the factors that are allowed to be selected in treshold Access role?
So then the only "Invalid" scenario is that the password cannot be alone, some other might be added in the future sure.

  1. User need to select at least 1 factor -> Insufficient
  2. It is recommended to select at least 2 factors -> Suboptimal
  3. Any other invalid combinations for a specific role (Access) -> Invalid

So the Wallet will display red for 1 and 3?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So then the only "Invalid" scenario is that the password cannot be alone, some other might be added in the future sure.

Exactly

So the Wallet will display red for 1 and 3?

Yes. 3 will have a "read more" where the user can read more about the rules

})
}

pub fn selected_factor_sources_for_role_status(
Copy link
Contributor

@GhenadieVP GhenadieVP Dec 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is meant to be used in Select Factors for Transactions screen? If so, do we need an instance of a builder to validate the selected combination of the factors?

Additionally, the fact that it returns SelectedFactorSourcesForRoleStatus, implies that the builder might contain invalid factors, but AFAIK we are actively trying to prevent this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need an instance of a builder

Not necessarily, @CyonAlexRDX suggested this. Also, it seems easier since all the current validation functions are on a builder instance.

implies that the builder might contain invalid factors

Not the factors themselves, but in the context of a specific role.

Comment on lines 425 to 427
pub fn security_shield_builder_sorted_factor_sources_for_selection(
factor_sources: Vec<FactorSource>,
) -> Vec<FactorSource> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't Sargon load the list of factor sources itself, sort and filter the appropriate factor sources?

// ==== STATIC ====
// ================
impl SecurityShieldBuilder {
pub fn sorted_factor_sources_for_selection(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this specific for Select Factors for Transactions, aka for Access role? If so, then it should be specific. Also not all factor sources can be used for Access role, thus some filtering need to happen as well AFAIK.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Security questions and trusted contact (not supported in MVP) cannot be used for Access role, we can filter them out

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correct, these should be filtered out accordingly.

@@ -118,6 +118,20 @@ impl FactorSourceKind {
}
}

impl FactorSourceKind {
pub fn display_order(&self) -> u8 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO in the context of this PR, it should be specific display order for Select Factor for Access Role, not generic order.

Copy link
Contributor

@CyonAlexRDX CyonAlexRDX left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM .

We can always iterate if we need adjust it

Copy link
Contributor

@GhenadieVP GhenadieVP left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs to include the filtering also

@danvleju-rdx danvleju-rdx merged commit a2b1692 into main Dec 11, 2024
13 checks passed
@danvleju-rdx danvleju-rdx deleted the ABW-3969-shield-setup-select-factors branch December 11, 2024 16:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants