dpp/identity/random.rs
1use crate::identity::v0::IdentityV0;
2use crate::identity::{Identity, IdentityPublicKey, KeyCount};
3use crate::version::PlatformVersion;
4use crate::ProtocolError;
5use rand::prelude::StdRng;
6use std::iter::FromIterator;
7
8impl Identity {
9 /// Generates a random identity using the specified version and key count, with a provided RNG.
10 ///
11 /// # Arguments
12 ///
13 /// * `version` - An optional feature version for the identity. If not provided, the latest version will be used.
14 /// * `key_count` - The number of keys to generate for the identity.
15 /// * `rng` - A mutable reference to a random number generator to use for generating the identity.
16 ///
17 /// # Returns
18 ///
19 /// A randomly generated identity of the specified version.
20 ///
21 /// # Panics
22 ///
23 /// This function will panic if an unsupported version is provided.
24 pub fn random_identity_with_rng(
25 key_count: KeyCount,
26 rng: &mut StdRng,
27 platform_version: &PlatformVersion,
28 ) -> Result<Self, ProtocolError> {
29 match platform_version
30 .dpp
31 .identity_versions
32 .identity_structure_version
33 {
34 0 => Ok(IdentityV0::random_identity_with_rng(key_count, rng, platform_version)?.into()),
35 version => Err(ProtocolError::UnknownVersionMismatch {
36 method: "Identity::random_identity_with_rng".to_string(),
37 known_versions: vec![0],
38 received: version,
39 }),
40 }
41 }
42
43 /// Generates a random identity with main keys and their corresponding private keys, using the specified version and key count, with a provided RNG.
44 ///
45 /// # Arguments
46 ///
47 /// * `version` - An optional feature version for the identity. If not provided, the latest version will be used.
48 /// * `key_count` - The number of keys to generate for the identity.
49 /// * `rng` - A mutable reference to a random number generator to use for generating the identity.
50 ///
51 /// # Returns
52 ///
53 /// A tuple containing the randomly generated identity and a collection of main keys with their corresponding private keys.
54 ///
55 /// # Panics
56 ///
57 /// This function will panic if an unsupported version is provided.
58 ///
59 /// # Errors
60 ///
61 /// This function may return a `ProtocolError` if an error occurs during identity generation.
62 pub fn random_identity_with_main_keys_with_private_key<I>(
63 key_count: KeyCount,
64 rng: &mut StdRng,
65 platform_version: &PlatformVersion,
66 ) -> Result<(Self, I), ProtocolError>
67 where
68 I: Default
69 + IntoIterator<Item = (IdentityPublicKey, [u8; 32])>
70 + Extend<(IdentityPublicKey, [u8; 32])>,
71 {
72 match platform_version
73 .dpp
74 .identity_versions
75 .identity_structure_version
76 {
77 0 => IdentityV0::random_identity_with_main_keys_with_private_key(
78 key_count,
79 rng,
80 platform_version,
81 )
82 .map(|(a, b)| (a.into(), b)),
83 version => Err(ProtocolError::UnknownVersionMismatch {
84 method: "Identity::random_identity_with_main_keys_with_private_key".to_string(),
85 known_versions: vec![0],
86 received: version,
87 }),
88 }
89 }
90
91 /// Generates a random identity using the specified version and key count, with an optional seed for reproducibility.
92 ///
93 /// # Arguments
94 ///
95 /// * `version` - An optional feature version for the identity. If not provided, the latest version will be used.
96 /// * `key_count` - The number of keys to generate for the identity.
97 /// * `seed` - An optional seed for reproducibility. If provided, the RNG will be seeded with this value.
98 ///
99 /// # Returns
100 ///
101 /// A randomly generated identity of the specified version.
102 ///
103 /// # Panics
104 ///
105 /// This function will panic if an unsupported version is provided.
106 pub fn random_identity(
107 key_count: KeyCount,
108 seed: Option<u64>,
109 platform_version: &PlatformVersion,
110 ) -> Result<Self, ProtocolError> {
111 match platform_version
112 .dpp
113 .identity_versions
114 .identity_structure_version
115 {
116 0 => Ok(IdentityV0::random_identity(key_count, seed, platform_version)?.into()),
117 version => Err(ProtocolError::UnknownVersionMismatch {
118 method: "Identity::random_identity".to_string(),
119 known_versions: vec![0],
120 received: version,
121 }),
122 }
123 }
124
125 /// Generates a specified number of random identities using the specified version and key count, with an optional seed for reproducibility.
126 ///
127 /// # Arguments
128 ///
129 /// * `version` - An optional feature version for the identity. If not provided, the latest version will be used.
130 /// * `count` - The number of identities to generate.
131 /// * `key_count` - The number of keys to generate for each identity.
132 /// * `seed` - An optional seed for reproducibility. If provided, the RNG will be seeded with this value.
133 ///
134 /// # Returns
135 ///
136 /// A vector of randomly generated identities of the specified version.
137 ///
138 /// # Panics
139 ///
140 /// This function will panic if an unsupported version is provided.
141 pub fn random_identities(
142 count: u16,
143 key_count: KeyCount,
144 seed: Option<u64>,
145 platform_version: &PlatformVersion,
146 ) -> Result<Vec<Self>, ProtocolError> {
147 match platform_version
148 .dpp
149 .identity_versions
150 .identity_structure_version
151 {
152 0 => Ok(
153 IdentityV0::random_identities(count, key_count, seed, platform_version)?
154 .into_iter()
155 .map(|identity| identity.into())
156 .collect(),
157 ),
158 version => Err(ProtocolError::UnknownVersionMismatch {
159 method: "Identity::random_identities".to_string(),
160 known_versions: vec![0],
161 received: version,
162 }),
163 }
164 }
165
166 /// Generates a specified number of random identities using the specified version and key count, with a provided RNG.
167 ///
168 /// # Arguments
169 /// * `version` - An optional feature version for the identity. If not provided, the latest version will be used.
170 /// * `count` - The number of identities to generate.
171 /// * `key_count` - The number of keys to generate for each identity.
172 /// * `rng` - A mutable reference to a random number generator to use for generating the identities.
173 ///
174 /// # Returns
175 ///
176 /// A vector of randomly generated identities of the specified version.
177 ///
178 /// # Panics
179 ///
180 /// This function will panic if an unsupported version is provided.
181 pub fn random_identities_with_rng(
182 count: u16,
183 key_count: KeyCount,
184 rng: &mut StdRng,
185 platform_version: &PlatformVersion,
186 ) -> Result<Vec<Self>, ProtocolError> {
187 match platform_version
188 .dpp
189 .identity_versions
190 .identity_structure_version
191 {
192 0 => Ok(IdentityV0::random_identities_with_rng(
193 count,
194 key_count,
195 rng,
196 platform_version,
197 )?
198 .into_iter()
199 .map(|identity| identity.into())
200 .collect()),
201 version => Err(ProtocolError::UnknownVersionMismatch {
202 method: "Identity::random_identities_with_rng".to_string(),
203 known_versions: vec![0],
204 received: version,
205 }),
206 }
207 }
208
209 /// Generates a specified number of random identities with their corresponding private keys, using the specified version, key count, and a provided RNG.
210 ///
211 /// # Arguments
212 ///
213 /// * `version` - An optional feature version for the identity. If not provided, the latest version will be used.
214 /// * `count` - The number of identities to generate.
215 /// * `key_count` - The number of keys to generate for each identity.
216 /// * `rng` - A mutable reference to a random number generator to use for generating the identities.
217 ///
218 /// # Returns
219 ///
220 /// A tuple containing a vector of randomly generated identities and a collection of main keys with their corresponding private keys.
221 ///
222 /// # Panics
223 ///
224 /// This function will panic if an unsupported version is provided.
225 ///
226 /// # Errors
227 ///
228 /// This function may return a `ProtocolError` if an error occurs during identity generation.
229 pub fn random_identities_with_private_keys_with_rng<I>(
230 count: u16,
231 key_count: KeyCount,
232 rng: &mut StdRng,
233 platform_version: &PlatformVersion,
234 ) -> Result<(Vec<Self>, I), ProtocolError>
235 where
236 I: Default
237 + FromIterator<(IdentityPublicKey, [u8; 32])>
238 + Extend<(IdentityPublicKey, [u8; 32])>,
239 {
240 match platform_version
241 .dpp
242 .identity_versions
243 .identity_structure_version
244 {
245 0 => IdentityV0::random_identities_with_private_keys_with_rng(
246 count,
247 key_count,
248 rng,
249 platform_version,
250 )
251 .map(|(identities, keys)| {
252 (
253 identities
254 .into_iter()
255 .map(|identity| identity.into())
256 .collect(),
257 keys,
258 )
259 }),
260 version => Err(ProtocolError::UnknownVersionMismatch {
261 method: "Identity::random_identities_with_private_keys_with_rng".to_string(),
262 known_versions: vec![0],
263 received: version,
264 }),
265 }
266 }
267}