Skip to content

Commit

Permalink
better samples
Browse files Browse the repository at this point in the history
  • Loading branch information
CyonAlexRDX committed Nov 29, 2024
1 parent b3bb566 commit 4ed67dd
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 140 deletions.
1 change: 1 addition & 0 deletions .tarpaulin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
exclude-files = [
"crates/rules-uniffi/src/unneeded_when_moved_to_sargon.rs",
"crates/rules-uniffi/src/error_conversion.rs",
"crates/rules/src/move_to_sargon.rs",
]
verbose = false
force-clean = true
Expand Down
61 changes: 38 additions & 23 deletions crates/rules/src/matrices/matrix_of_factor_source_ids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,29 @@ impl MatrixOfFactorSourceIds {
builder.build().unwrap()
}

pub fn sample_config_24() -> Self {
let mut builder = MatrixBuilder::new();

// Primary
// TODO: Ask Matt about this, does he mean Threshold(1) or Override?
builder
.add_factor_source_to_primary_override(FactorSourceID::sample_device())
.unwrap();

// Recovery
builder
.add_factor_source_to_recovery_override(FactorSourceID::sample_ledger())
.unwrap();

// Confirmation
builder
.add_factor_source_to_confirmation_override(FactorSourceID::sample_ledger_other())
.unwrap();

// Build
builder.build().unwrap()
}

pub fn sample_config_11() -> Self {
let mut builder = MatrixBuilder::new();

Expand Down Expand Up @@ -113,7 +136,7 @@ impl HasSampleValues for MatrixOfFactorSourceIds {
}

fn sample_other() -> Self {
Self::sample_config_12()
Self::sample_config_24()
}
}

Expand All @@ -132,6 +155,12 @@ mod tests {
#[test]
fn inequality() {
assert_ne!(SUT::sample(), SUT::sample_other());
assert_ne!(SUT::sample().primary(), SUT::sample_other().primary());
assert_ne!(SUT::sample().recovery(), SUT::sample_other().recovery());
assert_ne!(
SUT::sample().confirmation(),
SUT::sample_other().confirmation()
);
}

#[test]
Expand Down Expand Up @@ -208,36 +237,22 @@ mod tests {
r#"
{
"primary_role": {
"threshold": 2,
"threshold_factors": [
{
"discriminator": "fromHash",
"fromHash": {
"kind": "ledgerHQHardwareWallet",
"body": "ab59987eedd181fe98e512c1ba0f5ff059f11b5c7c56f15614dcc9fe03fec58b"
}
},
"threshold": 0,
"threshold_factors": [],
"override_factors": [
{
"discriminator": "fromHash",
"fromHash": {
"kind": "passphrase",
"body": "181ab662e19fac3ad9f08d5c673b286d4a5ed9cd3762356dc9831dc42427c1b9"
"kind": "device",
"body": "f1a93d324dd0f2bff89963ab81ed6e0c2ee7e18c0827dc1d3576b2d9f26bbd0a"
}
}
],
"override_factors": []
]
},
"recovery_role": {
"threshold": 0,
"threshold_factors": [],
"override_factors": [
{
"discriminator": "fromHash",
"fromHash": {
"kind": "device",
"body": "f1a93d324dd0f2bff89963ab81ed6e0c2ee7e18c0827dc1d3576b2d9f26bbd0a"
}
},
{
"discriminator": "fromHash",
"fromHash": {
Expand All @@ -254,8 +269,8 @@ mod tests {
{
"discriminator": "fromHash",
"fromHash": {
"kind": "passphrase",
"body": "181ab662e19fac3ad9f08d5c673b286d4a5ed9cd3762356dc9831dc42427c1b9"
"kind": "ledgerHQHardwareWallet",
"body": "52ef052a0642a94279b296d6b3b17dedc035a7ae37b76c1d60f11f2725100077"
}
}
]
Expand Down
157 changes: 62 additions & 95 deletions crates/rules/src/roles/role_with_factor_instances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,126 +85,58 @@ pub(crate) type RecoveryRoleWithFactorInstances = RoleWithFactorInstances<{ ROLE
pub(crate) type ConfirmationRoleWithFactorInstances =
RoleWithFactorInstances<{ ROLE_CONFIRMATION }>;

impl PrimaryRoleWithFactorInstances {
// TODO: MFA Rules change this, this might not be compatible with the rules!
pub fn sample_primary() -> Self {
Self::with_factors(
// RoleKind::Primary,
1, [
HierarchicalDeterministicFactorInstance::sample_mainnet_account_device_factor_fs_0_securified_at_index(0).into()
], [
HierarchicalDeterministicFactorInstance::sample_mainnet_account_device_factor_fs_10_securified_at_index(0).into()
])
}

// TODO: MFA Rules change this, this might not be compatible with the rules!
pub fn sample_primary_other() -> Self {
Self::with_factors(
// RoleKind::Primary,
1,
[HierarchicalDeterministicFactorInstance::sample_mainnet_account_device_factor_fs_0_securified_at_index(10).into(),],
[HierarchicalDeterministicFactorInstance::sample_mainnet_account_device_factor_fs_10_securified_at_index(60).into()],
)
}
}

impl RecoveryRoleWithFactorInstances {
// TODO: MFA Rules change this, this might not be compatible with the rules!
pub fn sample_recovery() -> Self {
Self::with_factors(
// RoleKind::Recovery,
0,[], [HierarchicalDeterministicFactorInstance::sample_mainnet_account_device_factor_fs_10_securified_at_index(237).into()]
)
impl HasSampleValues for PrimaryRoleWithFactorInstances {
fn sample() -> Self {
MatrixOfFactorInstances::sample().primary_role
}

// TODO: MFA Rules change this, this might not be compatible with the rules!
pub fn sample_recovery_other() -> Self {
Self::with_factors(
// RoleKind::Recovery,
0,[], [HierarchicalDeterministicFactorInstance::sample_mainnet_account_device_factor_fs_10_securified_at_index(42).into()]
)
fn sample_other() -> Self {
MatrixOfFactorInstances::sample_other().primary_role
}
}

impl ConfirmationRoleWithFactorInstances {
// TODO: MFA Rules change this, this might not be compatible with the rules!
pub fn sample_confirmation() -> Self {
Self::with_factors(
// RoleKind::Confirmation,
0,[], [HierarchicalDeterministicFactorInstance::sample_mainnet_account_device_factor_fs_0_securified_at_index(1).into(), HierarchicalDeterministicFactorInstance::sample_mainnet_account_device_factor_fs_10_securified_at_index(2).into()]
)
impl HasSampleValues for ConfirmationRoleWithFactorInstances {
fn sample() -> Self {
MatrixOfFactorInstances::sample().confirmation_role
}

// TODO: MFA Rules change this, this might not be compatible with the rules!
pub fn sample_confirmation_other() -> Self {
Self::with_factors(
// RoleKind::Confirmation,
0,[], [HierarchicalDeterministicFactorInstance::sample_mainnet_account_device_factor_fs_0_securified_at_index(10).into(), HierarchicalDeterministicFactorInstance::sample_mainnet_account_device_factor_fs_10_securified_at_index(20).into()]
)
fn sample_other() -> Self {
MatrixOfFactorInstances::sample_other().confirmation_role
}
}
/*

impl HasSampleValues for PrimaryRoleWithFactorInstances {
impl HasSampleValues for RecoveryRoleWithFactorInstances {
fn sample() -> Self {
Self::sample_primary()
MatrixOfFactorInstances::sample().recovery_role
}

fn sample_other() -> Self {
Self::sample_primary_other()
MatrixOfFactorInstances::sample_other().recovery_role
}
}

#[cfg(test)]
mod tests {
mod primary_tests {
use super::*;

#[allow(clippy::upper_case_acronyms)]
type SUT = RoleWithFactorInstances;
type SUT = PrimaryRoleWithFactorInstances;

#[test]
fn equality() {
assert_eq!(SUT::sample_primary(), SUT::sample_primary());
assert_eq!(SUT::sample_primary_other(), SUT::sample_primary_other());
assert_eq!(SUT::sample_recovery(), SUT::sample_recovery());
assert_eq!(SUT::sample_recovery_other(), SUT::sample_recovery_other());
assert_eq!(SUT::sample_confirmation(), SUT::sample_confirmation());
assert_eq!(
SUT::sample_confirmation_other(),
SUT::sample_confirmation_other()
);
assert_eq!(SUT::sample(), SUT::sample());
assert_eq!(SUT::sample_other(), SUT::sample_other());
}

#[test]
fn inequality() {
assert_ne!(SUT::sample(), SUT::sample_other());
}

#[test]
fn hash() {
let hash = HashSet::<SUT>::from_iter([
SUT::sample_primary(),
SUT::sample_primary_other(),
SUT::sample_recovery(),
SUT::sample_recovery_other(),
SUT::sample_confirmation(),
SUT::sample_confirmation_other(),
// Duplicates should be removed
SUT::sample_primary(),
SUT::sample_primary_other(),
SUT::sample_recovery(),
SUT::sample_recovery_other(),
SUT::sample_confirmation(),
SUT::sample_confirmation_other(),
]);
assert_eq!(hash.len(), 6);
}
#[test]
#[should_panic]
fn primary_role_non_securified_threshold_instances_is_err() {
let _ = SUT::with_factors(
RoleKind::Primary,
1,
[
HierarchicalDeterministicFactorInstance::sample_mainnet_account_device_factor_fs_10_unsecurified_at_index(0).into()
Expand All @@ -220,8 +152,7 @@ mod tests {
&sut,
r#"
{
"role": "primary",
"threshold": 1,
"threshold": 2,
"threshold_factors": [
{
"factorSourceID": {
Expand All @@ -247,15 +178,13 @@ mod tests {
}
}
}
}
],
"override_factors": [
},
{
"factorSourceID": {
"discriminator": "fromHash",
"fromHash": {
"kind": "device",
"body": "5255999c65076ce9ced5a1881f1a621bba1ce3f1f68a61df462d96822a5190cd"
"kind": "ledgerHQHardwareWallet",
"body": "ab59987eedd181fe98e512c1ba0f5ff059f11b5c7c56f15614dcc9fe03fec58b"
}
},
"badge": {
Expand All @@ -265,7 +194,7 @@ mod tests {
"hierarchicalDeterministicPublicKey": {
"publicKey": {
"curve": "curve25519",
"compressedData": "e0293d4979bc303ea4fe361a62baf9c060c7d90267972b05c61eead9ef3eed3e"
"compressedData": "92cd6838cd4e7b0523ed93d498e093f71139ffd5d632578189b39a26005be56b"
},
"derivationPath": {
"scheme": "cap26",
Expand All @@ -275,10 +204,48 @@ mod tests {
}
}
}
]
],
"override_factors": []
}
"#,
);
}
}
*/

#[cfg(test)]
mod confirmation_tests {
use super::*;

#[allow(clippy::upper_case_acronyms)]
type SUT = ConfirmationRoleWithFactorInstances;

#[test]
fn equality() {
assert_eq!(SUT::sample(), SUT::sample());
assert_eq!(SUT::sample_other(), SUT::sample_other());
}

#[test]
fn inequality() {
assert_ne!(SUT::sample(), SUT::sample_other());
}
}

#[cfg(test)]
mod recovery_tests {
use super::*;

#[allow(clippy::upper_case_acronyms)]
type SUT = RecoveryRoleWithFactorInstances;

#[test]
fn equality() {
assert_eq!(SUT::sample(), SUT::sample());
assert_eq!(SUT::sample_other(), SUT::sample_other());
}

#[test]
fn inequality() {
assert_ne!(SUT::sample(), SUT::sample_other());
}
}
Loading

0 comments on commit 4ed67dd

Please sign in to comment.