From 20fec9968fcff08b21c49bd4eb22870117a2132e Mon Sep 17 00:00:00 2001 From: Alexander Cyon Date: Thu, 29 Aug 2024 09:46:43 +0200 Subject: [PATCH] cleanup --- .../collector/key_derivation_outcome.rs | 4 ++ .../factor_source_referencing.rs | 2 +- .../petition_factors_types/mod.rs | 2 +- .../petition_types/petition_of_transaction.rs | 12 ++--- src/signing/signatures_outcome_types/mod.rs | 4 ++ .../petition_transaction_outcome.rs | 54 +++++++++++++++++++ .../sign_with_factors_outcome.rs} | 4 -- src/types/mod.rs | 2 - 8 files changed, 67 insertions(+), 17 deletions(-) create mode 100644 src/signing/signatures_outcome_types/petition_transaction_outcome.rs rename src/{types/sign_with_factor_source_or_sources_outcome.rs => signing/signatures_outcome_types/sign_with_factors_outcome.rs} (90%) diff --git a/src/derivation/collector/key_derivation_outcome.rs b/src/derivation/collector/key_derivation_outcome.rs index 57f95785..7a2d0400 100644 --- a/src/derivation/collector/key_derivation_outcome.rs +++ b/src/derivation/collector/key_derivation_outcome.rs @@ -1,10 +1,14 @@ use crate::prelude::*; +/// A collection of all `HierarchicalDeterministicFactorInstance` +/// (Public Keys) which were derived from the referenced +/// `FactorSource`s at the specified `DerivationPath`s #[derive(Debug, PartialEq, Eq, Clone)] pub struct KeyDerivationOutcome { pub factors_by_source: IndexMap>, } + impl KeyDerivationOutcome { pub fn new( factors_by_source: IndexMap< diff --git a/src/signing/petition_types/petition_factors_types/factor_source_referencing.rs b/src/signing/petition_types/petition_factors_types/factor_source_referencing.rs index 14ad5024..f4ab2087 100644 --- a/src/signing/petition_types/petition_factors_types/factor_source_referencing.rs +++ b/src/signing/petition_types/petition_factors_types/factor_source_referencing.rs @@ -1,6 +1,6 @@ use crate::prelude::*; -pub trait FactorSourceReferencing: std::hash::Hash + PartialEq + Eq + Clone { +pub(crate) trait FactorSourceReferencing: std::hash::Hash + PartialEq + Eq + Clone { fn factor_source_id(&self) -> FactorSourceIDFromHash; } diff --git a/src/signing/petition_types/petition_factors_types/mod.rs b/src/signing/petition_types/petition_factors_types/mod.rs index e8527b90..c5053a06 100644 --- a/src/signing/petition_types/petition_factors_types/mod.rs +++ b/src/signing/petition_types/petition_factors_types/mod.rs @@ -12,7 +12,7 @@ use petition_factors_state::*; use petition_factors_state_snapshot::*; use petition_factors_sub_state::*; -pub use factor_source_referencing::*; +pub(crate) use factor_source_referencing::*; pub use neglected_factor_instance::*; pub use petition_factors::*; pub use petition_factors_status::*; diff --git a/src/signing/petition_types/petition_of_transaction.rs b/src/signing/petition_types/petition_of_transaction.rs index 0f667b50..5a0b839a 100644 --- a/src/signing/petition_types/petition_of_transaction.rs +++ b/src/signing/petition_types/petition_of_transaction.rs @@ -11,13 +11,6 @@ pub(crate) struct PetitionTransaction { pub for_entities: RefCell>, } -#[derive(Clone, PartialEq, Eq)] -pub struct PetitionTransactionOutcome { - pub transaction_valid: bool, - pub signatures: IndexSet, - pub neglected_factors: IndexSet, -} - impl PetitionTransaction { pub(crate) fn new( intent_hash: IntentHash, @@ -62,11 +55,12 @@ impl PetitionTransaction { .flat_map(|x| x.all_neglected_factor_sources()) .collect::>(); - PetitionTransactionOutcome { + PetitionTransactionOutcome::new( transaction_valid, + self.intent_hash.clone(), signatures, neglected_factors, - } + ) } fn _all_factor_instances(&self) -> IndexSet { diff --git a/src/signing/signatures_outcome_types/mod.rs b/src/signing/signatures_outcome_types/mod.rs index 3c6324c4..7342ea65 100644 --- a/src/signing/signatures_outcome_types/mod.rs +++ b/src/signing/signatures_outcome_types/mod.rs @@ -1,5 +1,9 @@ mod maybe_signed_transactions; +mod petition_transaction_outcome; +mod sign_with_factors_outcome; mod signatures_outcome; pub use maybe_signed_transactions::*; +pub(crate) use petition_transaction_outcome::*; +pub use sign_with_factors_outcome::*; pub use signatures_outcome::*; diff --git a/src/signing/signatures_outcome_types/petition_transaction_outcome.rs b/src/signing/signatures_outcome_types/petition_transaction_outcome.rs new file mode 100644 index 00000000..72458fa2 --- /dev/null +++ b/src/signing/signatures_outcome_types/petition_transaction_outcome.rs @@ -0,0 +1,54 @@ +use crate::prelude::*; + +/// The outcome of collecting signatures for a specific +/// transasction - either valid or invalid - and a +/// set of collected signatues (might be empty) and +/// a set of neglected factors (might be empty). +#[derive(Clone, PartialEq, Eq)] +pub struct PetitionTransactionOutcome { + intent_hash: IntentHash, + pub transaction_valid: bool, + pub signatures: IndexSet, + pub neglected_factors: IndexSet, +} + +impl PetitionTransactionOutcome { + /// # Panics + /// Panics if the intent hash in any signatures does not + /// match `intent_hash` + pub fn new( + transaction_valid: bool, + intent_hash: IntentHash, + signatures: IndexSet, + neglected_factors: IndexSet, + ) -> Self { + assert!( + signatures.iter().all(|s| *s.intent_hash() == intent_hash), + "Discprenacy! Mismatching intent hash found in a signature." + ); + Self { + intent_hash, + transaction_valid, + signatures, + neglected_factors, + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + type Sut = PetitionTransactionOutcome; + + #[test] + #[should_panic(expected = "Discprenacy! Mismatching intent hash found in a signature.")] + fn panic() { + Sut::new( + true, + IntentHash::sample(), + IndexSet::from_iter([HDSignature::sample_other()]), + IndexSet::new(), + ); + } +} diff --git a/src/types/sign_with_factor_source_or_sources_outcome.rs b/src/signing/signatures_outcome_types/sign_with_factors_outcome.rs similarity index 90% rename from src/types/sign_with_factor_source_or_sources_outcome.rs rename to src/signing/signatures_outcome_types/sign_with_factors_outcome.rs index fe9ab1a0..776f0443 100644 --- a/src/types/sign_with_factor_source_or_sources_outcome.rs +++ b/src/signing/signatures_outcome_types/sign_with_factors_outcome.rs @@ -36,8 +36,4 @@ impl SignWithFactorsOutcome { pub fn user_skipped_factor(id: FactorSourceIDFromHash) -> Self { Self::user_skipped_factors(IndexSet::from_iter([id])) } - - pub fn failure_with_factor(id: FactorSourceIDFromHash) -> Self { - Self::failure_with_factors(IndexSet::from_iter([id])) - } } diff --git a/src/types/mod.rs b/src/types/mod.rs index b14ad4a1..740badb7 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -5,7 +5,6 @@ mod invalid_transaction_if_neglected; mod new_methods_on_sargon_types; mod owned_types; mod sargon_types; -mod sign_with_factor_source_or_sources_outcome; pub(crate) use factor_sources_of_kind::*; pub use hd_signature::*; @@ -13,4 +12,3 @@ pub use hd_signature_input::*; pub use invalid_transaction_if_neglected::*; pub use owned_types::*; pub use sargon_types::*; -pub use sign_with_factor_source_or_sources_outcome::*;