holochain_integrity_types/countersigning/
error.rs

1use crate::Role;
2
3/// Errors related to the secure primitive macro.
4#[derive(Debug, PartialEq, Eq)]
5pub enum CounterSigningError {
6    /// Agent index is out of bounds for the signing session.
7    AgentIndexOutOfBounds,
8    /// An empty vector was used to build session data.
9    MissingResponse,
10    /// Session responses needs to be same length as the signing agents.
11    CounterSigningSessionResponsesLength(usize, usize),
12    /// Session response agents all need to be in the correct positions.
13    CounterSigningSessionResponsesOrder(u8, usize),
14    /// Enzyme must match for required and optional signers if set.
15    EnzymeMismatch(
16        Option<(holo_hash::AgentPubKey, Vec<Role>)>,
17        Option<(holo_hash::AgentPubKey, Vec<Role>)>,
18    ),
19    /// If there are optional signers the session MUST be enzymatic.
20    NonEnzymaticOptionalSigners,
21    /// Agents length cannot be longer than max or less than min.
22    AgentsLength(usize),
23    /// Optional agents length cannot be shorter then minimum.
24    OptionalAgentsLength(u8, usize),
25    /// Optional agents length must be majority of the signers list.
26    MinOptionalAgents(u8, usize),
27    /// There cannot be duplicates in the agents list.
28    AgentsDupes(Vec<holo_hash::AgentPubKey>),
29    /// The session times must validate.
30    CounterSigningSessionTimes(crate::CounterSigningSessionTimes),
31}
32
33impl std::error::Error for CounterSigningError {}
34
35impl core::fmt::Display for CounterSigningError {
36    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37        match self {
38            CounterSigningError::AgentIndexOutOfBounds => {
39                write!(f, "Agent index is out of bounds for the signing session.")
40            }
41            CounterSigningError::MissingResponse => write!(
42                f,
43                "Attempted to build CounterSigningSessionData with an empty response vector."
44            ),
45            CounterSigningError::CounterSigningSessionResponsesLength(resp, num_agents) => {
46                write!(f,
47                    "The countersigning session responses ({resp}) did not match the number of signing agents ({num_agents})"
48                )
49            }
50            CounterSigningError::CounterSigningSessionResponsesOrder(index, pos) => write!(f,
51                    "The countersigning session response with agent index {index} was found in index position {pos}"
52            ),
53            CounterSigningError::EnzymeMismatch(required_signer, optional_signer) => write!(f,
54                "The enzyme is mismatche for required signer {required_signer:?} and optional signer {optional_signer:?}"
55
56            ),
57            CounterSigningError::NonEnzymaticOptionalSigners => write!(f, "There are optional signers without an enzyme."),
58            CounterSigningError::AgentsLength(len) => {
59                write!(f, "The signing agents list is too long or short {len}")
60            },
61            CounterSigningError::OptionalAgentsLength(min, len) => {
62                write!(f, "The optional signing agents list length is {len} which is less than the minimum {min} required to sign")
63            },
64            CounterSigningError::MinOptionalAgents(min, len) => {
65                write!(f, "The minimum optional agents {min} is not a majority of {len}")
66            },
67            CounterSigningError::AgentsDupes(agents) => write!(
68                f,
69                "The signing agents list contains duplicates {agents:?}"
70            ),
71            CounterSigningError::CounterSigningSessionTimes(times) => write!(
72                f,
73                "The countersigning session times were not valid {times:?}"
74            ),
75        }
76    }
77}