dpp/identity/accessors/
mod.rs1use 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 fn public_keys(&self) -> &BTreeMap<KeyID, IdentityPublicKey> {
19 match self {
20 Identity::V0(identity) => &identity.public_keys,
21 }
22 }
23
24 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 fn public_keys_owned(self) -> BTreeMap<KeyID, IdentityPublicKey> {
41 match self {
42 Identity::V0(identity) => identity.public_keys,
43 }
44 }
45
46 fn balance(&self) -> u64 {
52 match self {
53 Identity::V0(identity) => identity.balance,
54 }
55 }
56
57 fn revision(&self) -> Revision {
63 match self {
64 Identity::V0(identity) => identity.revision,
65 }
66 }
67
68 fn id(&self) -> Identifier {
74 match self {
75 Identity::V0(identity) => identity.id,
76 }
77 }
78
79 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 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 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 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 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 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 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 fn set_balance(&mut self, new_balance: u64) {
158 match self {
159 Identity::V0(identity) => identity.balance = new_balance,
160 }
161 }
162
163 fn set_revision(&mut self, new_revision: Revision) {
169 match self {
170 Identity::V0(identity) => identity.revision = new_revision,
171 }
172 }
173
174 fn bump_revision(&mut self) {
177 match self {
178 Identity::V0(identity) => identity.revision += 1,
179 }
180 }
181
182 fn set_id(&mut self, new_id: Identifier) {
188 match self {
189 Identity::V0(identity) => identity.id = new_id,
190 }
191 }
192
193 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 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 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}