dpp/identity/accessors/
mod.rs

1use crate::identity::identity_public_key::accessors::v0::IdentityPublicKeyGettersV0;
2use crate::identity::{Identity, IdentityPublicKey, KeyID, KeyType, Purpose, SecurityLevel};
3
4use crate::prelude::Revision;
5use crate::ProtocolError;
6use platform_value::Identifier;
7use std::collections::{BTreeMap, HashSet};
8pub use v0::{IdentityGettersV0, IdentitySettersV0};
9
10mod v0;
11
12impl IdentityGettersV0 for Identity {
13    /// Returns a reference to the public keys of the identity.
14    ///
15    /// # Returns
16    ///
17    /// A reference to a `BTreeMap` containing the `KeyID` as keys and `IdentityPublicKey` as values.
18    fn public_keys(&self) -> &BTreeMap<KeyID, IdentityPublicKey> {
19        match self {
20            Identity::V0(identity) => &identity.public_keys,
21        }
22    }
23
24    /// Returns a mutable reference to the public keys of the identity.
25    ///
26    /// # Returns
27    ///
28    /// A mutable reference to a `BTreeMap` containing the `KeyID` as keys and `IdentityPublicKey` as values.
29    fn public_keys_mut(&mut self) -> &mut BTreeMap<KeyID, IdentityPublicKey> {
30        match self {
31            Identity::V0(identity) => &mut identity.public_keys,
32        }
33    }
34
35    /// Consumes the `Identity` and returns the owned public keys.
36    ///
37    /// # Returns
38    ///
39    /// A `BTreeMap` containing the `KeyID` as keys and `IdentityPublicKey` as values.
40    fn public_keys_owned(self) -> BTreeMap<KeyID, IdentityPublicKey> {
41        match self {
42            Identity::V0(identity) => identity.public_keys,
43        }
44    }
45
46    /// Returns the balance of the identity.
47    ///
48    /// # Returns
49    ///
50    /// The balance as a `u64`.
51    fn balance(&self) -> u64 {
52        match self {
53            Identity::V0(identity) => identity.balance,
54        }
55    }
56
57    /// Returns the revision of the identity.
58    ///
59    /// # Returns
60    ///
61    /// The revision as a `Revision`.
62    fn revision(&self) -> Revision {
63        match self {
64            Identity::V0(identity) => identity.revision,
65        }
66    }
67
68    /// Returns the identifier of the identity.
69    ///
70    /// # Returns
71    ///
72    /// The identifier as an `Identifier`.
73    fn id(&self) -> Identifier {
74        match self {
75            Identity::V0(identity) => identity.id,
76        }
77    }
78
79    /// Returns a public key for a given id
80    fn get_public_key_by_id(&self, key_id: KeyID) -> Option<&IdentityPublicKey> {
81        match self {
82            Identity::V0(identity) => identity.public_keys.get(&key_id),
83        }
84    }
85
86    /// Returns a public key for a given id
87    fn get_public_key_by_id_mut(&mut self, key_id: KeyID) -> Option<&mut IdentityPublicKey> {
88        match self {
89            Identity::V0(identity) => identity.public_keys.get_mut(&key_id),
90        }
91    }
92
93    /// Get the biggest public KeyID
94    fn get_public_key_max_id(&self) -> KeyID {
95        match self {
96            Identity::V0(identity) => identity
97                .public_keys
98                .keys()
99                .copied()
100                .max()
101                .unwrap_or_default(),
102        }
103    }
104
105    /// Add an identity public key
106    fn add_public_key(&mut self, key: IdentityPublicKey) {
107        match self {
108            Identity::V0(identity) => identity.public_keys.insert(key.id(), key),
109        };
110    }
111
112    /// Add identity public keys
113    fn add_public_keys(&mut self, keys: impl IntoIterator<Item = IdentityPublicKey>) {
114        match self {
115            Identity::V0(identity) => identity
116                .public_keys
117                .extend(keys.into_iter().map(|a| (a.id(), a))),
118        }
119    }
120
121    /// Get first public key matching a purpose, security levels, or key types, optionally allowing disabled keys
122    fn get_first_public_key_matching(
123        &self,
124        purpose: Purpose,
125        security_levels: HashSet<SecurityLevel>,
126        key_types: HashSet<KeyType>,
127        allow_disabled: bool,
128    ) -> Option<&IdentityPublicKey> {
129        match self {
130            Identity::V0(identity) => identity.public_keys.values().find(|key| {
131                key.purpose() == purpose
132                    && security_levels.contains(&key.security_level())
133                    && key_types.contains(&key.key_type())
134                    && (allow_disabled || !key.is_disabled())
135            }),
136        }
137    }
138}
139
140impl IdentitySettersV0 for Identity {
141    /// Sets the public keys of the identity.
142    ///
143    /// # Arguments
144    ///
145    /// * `new_public_keys` - A `BTreeMap` containing the new `KeyID` as keys and `IdentityPublicKey` as values.
146    fn set_public_keys(&mut self, new_public_keys: BTreeMap<KeyID, IdentityPublicKey>) {
147        match self {
148            Identity::V0(identity) => identity.public_keys = new_public_keys,
149        }
150    }
151
152    /// Sets the balance of the identity.
153    ///
154    /// # Arguments
155    ///
156    /// * `new_balance` - The new balance as a `u64`.
157    fn set_balance(&mut self, new_balance: u64) {
158        match self {
159            Identity::V0(identity) => identity.balance = new_balance,
160        }
161    }
162
163    /// Sets the revision of the identity.
164    ///
165    /// # Arguments
166    ///
167    /// * `new_revision` - The new revision as a `Revision`.
168    fn set_revision(&mut self, new_revision: Revision) {
169        match self {
170            Identity::V0(identity) => identity.revision = new_revision,
171        }
172    }
173
174    /// Sets the revision of the identity to +1.
175    ///
176    fn bump_revision(&mut self) {
177        match self {
178            Identity::V0(identity) => identity.revision += 1,
179        }
180    }
181
182    /// Sets the identifier of the identity.
183    ///
184    /// # Arguments
185    ///
186    /// * `new_id` - The new identifier as an `Identifier`.
187    fn set_id(&mut self, new_id: Identifier) {
188        match self {
189            Identity::V0(identity) => identity.id = new_id,
190        }
191    }
192
193    /// Increase Identity balance
194    fn increase_balance(&mut self, amount: u64) -> u64 {
195        match self {
196            Identity::V0(identity) => {
197                identity.balance += amount;
198                identity.balance
199            }
200        }
201    }
202
203    /// Reduce the Identity balance
204    fn reduce_balance(&mut self, amount: u64) -> u64 {
205        match self {
206            Identity::V0(identity) => {
207                identity.balance -= amount;
208                identity.balance
209            }
210        }
211    }
212
213    /// Increment revision
214    fn increment_revision(&mut self) -> Result<(), ProtocolError> {
215        match self {
216            Identity::V0(identity) => {
217                let result = identity
218                    .revision
219                    .checked_add(1)
220                    .ok_or(ProtocolError::Generic(
221                        "identity revision is at max level".to_string(),
222                    ))?;
223
224                identity.revision = result;
225
226                Ok(())
227            }
228        }
229    }
230}