dpp/identity/identity_public_key/
security_level.rs

1use bincode::{Decode, Encode};
2#[cfg(feature = "cbor")]
3use ciborium::value::Value as CborValue;
4
5use serde_repr::{Deserialize_repr, Serialize_repr};
6
7use crate::consensus::basic::data_contract::UnknownSecurityLevelError;
8use crate::consensus::basic::BasicError;
9use crate::consensus::ConsensusError;
10use crate::ProtocolError;
11use std::convert::TryFrom;
12
13#[repr(u8)]
14#[derive(
15    Debug,
16    PartialEq,
17    Eq,
18    Clone,
19    Copy,
20    Hash,
21    Serialize_repr,
22    Deserialize_repr,
23    PartialOrd,
24    Ord,
25    Encode,
26    Decode,
27    Default,
28    strum::EnumIter,
29)]
30pub enum SecurityLevel {
31    MASTER = 0,
32    CRITICAL = 1,
33    #[default]
34    HIGH = 2,
35    MEDIUM = 3,
36}
37
38impl From<SecurityLevel> for [u8; 1] {
39    fn from(security_level: SecurityLevel) -> Self {
40        [security_level as u8]
41    }
42}
43
44impl From<SecurityLevel> for &'static [u8; 1] {
45    fn from(security_level: SecurityLevel) -> Self {
46        match security_level {
47            SecurityLevel::MASTER => &[0],
48            SecurityLevel::CRITICAL => &[1],
49            SecurityLevel::HIGH => &[2],
50            SecurityLevel::MEDIUM => &[3],
51        }
52    }
53}
54
55#[cfg(feature = "cbor")]
56impl Into<CborValue> for SecurityLevel {
57    fn into(self) -> CborValue {
58        CborValue::from(self as u128)
59    }
60}
61
62impl TryFrom<u8> for SecurityLevel {
63    type Error = ProtocolError;
64    fn try_from(value: u8) -> Result<Self, ProtocolError> {
65        match value {
66            0 => Ok(Self::MASTER),
67            1 => Ok(Self::CRITICAL),
68            2 => Ok(Self::HIGH),
69            3 => Ok(Self::MEDIUM),
70            value => Err(ProtocolError::ConsensusError(
71                ConsensusError::BasicError(BasicError::UnknownSecurityLevelError(
72                    UnknownSecurityLevelError::new(vec![0, 1, 2, 3], value),
73                ))
74                .into(),
75            )),
76        }
77    }
78}
79
80impl SecurityLevel {
81    /// The full range of security levels
82    pub fn full_range() -> [SecurityLevel; 4] {
83        [Self::MASTER, Self::CRITICAL, Self::HIGH, Self::MEDIUM]
84    }
85    pub fn last() -> SecurityLevel {
86        Self::MEDIUM
87    }
88    pub fn lowest_level() -> SecurityLevel {
89        Self::MEDIUM
90    }
91    pub fn highest_level() -> SecurityLevel {
92        Self::MASTER
93    }
94    pub fn stronger_security_than(self: SecurityLevel, rhs: SecurityLevel) -> bool {
95        // Example:
96        // self: High 2 rhs: Master 0
97        // Master has a stronger security level than high
98        // We expect False
99        // High < Master
100        // 2 < 0 <=> false
101        (self as u8) < (rhs as u8)
102    }
103
104    pub fn stronger_or_equal_security_than(self: SecurityLevel, rhs: SecurityLevel) -> bool {
105        (self as u8) <= (rhs as u8)
106    }
107}
108
109impl std::fmt::Display for SecurityLevel {
110    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
111        write!(f, "{self:?}")
112    }
113}