Skip to content
This repository was archived by the owner on Feb 4, 2025. It is now read-only.

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
CyonAlexRDX committed Oct 10, 2024
1 parent 15c423c commit 20b0810
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,80 @@ use crate::{factor_instances_provider::provider::test_sargon_os::SargonOS, prelu

type Sut = FactorInstancesProvider;

#[should_panic]
#[actix_rt::test]
async fn mfa_panics_if_entities_empty() {
let fs = HDFactorSource::fs0();
let a = Account::sample_unsecurified();
let _ = Sut::for_account_mfa(
&mut FactorInstancesCache::default(),
MatrixOfFactorSources::new([], 1, [fs.clone()]),
Profile::new([fs], [&a], []),
IndexSet::new(), // <---- EMPTY => should_panic
Arc::new(TestDerivationInteractors::default()),
)
.await
.unwrap();
}

#[should_panic]
#[actix_rt::test]
async fn mfa_panics_if_entity_unknown() {
let fs = HDFactorSource::fs0();
let a = Account::sample_unsecurified();
let _ = Sut::for_account_mfa(
&mut FactorInstancesCache::default(),
MatrixOfFactorSources::new([], 1, [fs.clone()]),
Profile::new([fs], [&a], []),
IndexSet::just(Account::a1().entity_address()), // <---- unknown => should_panic
Arc::new(TestDerivationInteractors::default()),
)
.await
.unwrap();
}

#[should_panic]
#[actix_rt::test]
async fn mfa_panics_if_wrong_network() {
let fs = HDFactorSource::fs0();
let network = NetworkID::Mainnet;
let mainnet_account = Account::unsecurified_on_network(
"main",
network,
HierarchicalDeterministicFactorInstance::tx_on_network(
CAP26EntityKind::Account,
network,
HDPathComponent::unsecurified_hardening_base_index(0),
fs.factor_source_id(),
),
);
let network = NetworkID::Stokenet;
let stokenet_account = Account::unsecurified_on_network(
"stoknet",
network,
HierarchicalDeterministicFactorInstance::tx_on_network(
CAP26EntityKind::Account,
network,
HDPathComponent::unsecurified_hardening_base_index(0),
fs.factor_source_id(),
),
);
let profile = Profile::new([fs.clone()], [&mainnet_account, &stokenet_account], []);
assert_eq!(profile.networks.len(), 2);
let _ = Sut::for_account_mfa(
&mut FactorInstancesCache::default(),
MatrixOfFactorSources::new([], 1, [fs.clone()]),
profile,
IndexSet::from_iter([
mainnet_account.entity_address(),
stokenet_account.entity_address(),
]), // <---- wrong network => should_panic
Arc::new(TestDerivationInteractors::default()),
)
.await
.unwrap();
}

#[actix_rt::test]
async fn create_accounts_when_last_is_used_cache_is_fill_only_with_account_vecis_and_if_profile_is_used_a_new_account_is_created(
) {
Expand Down
32 changes: 26 additions & 6 deletions src/types/sargon_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1383,14 +1383,12 @@ pub trait IsEntity:
}
}

fn unsecurified_mainnet(
fn unsecurified_on_network(
name: impl AsRef<str>,
network_id: NetworkID,
genesis_factor_instance: HierarchicalDeterministicFactorInstance,
) -> Self {
let address = Self::Address::new(
NetworkID::Mainnet,
genesis_factor_instance.public_key_hash(),
);
let address = Self::Address::new(network_id, genesis_factor_instance.public_key_hash());
Self::new(
name,
address,
Expand All @@ -1399,7 +1397,14 @@ pub trait IsEntity:
)
}

fn securified_mainnet(
fn unsecurified_mainnet(
name: impl AsRef<str>,
genesis_factor_instance: HierarchicalDeterministicFactorInstance,
) -> Self {
Self::unsecurified_on_network(name, NetworkID::Mainnet, genesis_factor_instance)
}

fn securified(
name: impl AsRef<str>,
address: Self::Address,
make_matrix: impl Fn() -> MatrixOfFactorInstances,
Expand All @@ -1422,6 +1427,21 @@ pub trait IsEntity:
)
}

fn securified_mainnet(
name: impl AsRef<str>,
address: Self::Address,
make_matrix: impl Fn() -> MatrixOfFactorInstances,
) -> Self {
assert_eq!(address.network_id(), NetworkID::Mainnet);
let matrix = make_matrix();
assert!(matrix
.clone()
.all_factors()
.into_iter()
.all(|f| f.derivation_path().network_id == NetworkID::Mainnet));
Self::securified(name, address, || matrix.clone())
}

fn all_factor_instances(&self) -> HashSet<HierarchicalDeterministicFactorInstance> {
self.security_state()
.all_factor_instances()
Expand Down

0 comments on commit 20b0810

Please sign in to comment.