dpp/identity/v0/
random.rs1use crate::identity::v0::IdentityV0;
2use crate::identity::{IdentityPublicKey, KeyID};
3use crate::prelude::Identifier;
4
5use crate::ProtocolError;
6
7use crate::identity::identity_public_key::accessors::v0::IdentityPublicKeyGettersV0;
8use crate::identity::identity_public_key::KeyCount;
9use crate::version::PlatformVersion;
10use rand::rngs::StdRng;
11use rand::{Rng, SeedableRng};
12use std::collections::BTreeMap;
13use std::iter::FromIterator;
14
15impl IdentityV0 {
16 pub fn random_identity_with_rng(
17 key_count: KeyCount,
18 rng: &mut StdRng,
19 platform_version: &PlatformVersion,
20 ) -> Result<Self, ProtocolError> {
21 let id = Identifier::new(rng.gen::<[u8; 32]>());
22 let revision = rng.gen_range(0..100);
23 let balance = rng.gen::<u64>() >> 20; let public_keys = IdentityPublicKey::random_authentication_keys_with_rng(
27 key_count,
28 rng,
29 platform_version,
30 )?
31 .into_iter()
32 .map(|key| (key.id(), key))
33 .collect();
34
35 Ok(IdentityV0 {
36 id,
37 revision,
38 balance,
39 public_keys,
40 })
41 }
42
43 pub fn random_identity_with_main_keys_with_private_key<I>(
44 key_count: KeyCount,
45 rng: &mut StdRng,
46 platform_version: &PlatformVersion,
47 ) -> Result<(Self, I), ProtocolError>
48 where
49 I: Default
50 + IntoIterator<Item = (IdentityPublicKey, [u8; 32])>
51 + Extend<(IdentityPublicKey, [u8; 32])>,
52 {
53 let id = Identifier::new(rng.gen::<[u8; 32]>());
54 let revision = 0;
55 let balance = rng.gen::<u64>() >> 20; let (public_keys, private_keys): (BTreeMap<KeyID, IdentityPublicKey>, I) =
59 IdentityPublicKey::main_keys_with_random_authentication_keys_with_private_keys_with_rng(
60 key_count,
61 rng,
62 platform_version,
63 )?
64 .into_iter()
65 .map(|(key, private_key)| ((key.id(), key.clone()), (key, private_key)))
66 .unzip();
67
68 Ok((
69 IdentityV0 {
70 id,
71 revision,
72 balance,
73 public_keys,
74 },
75 private_keys,
76 ))
77 }
78
79 pub fn random_identity(
80 key_count: KeyCount,
81 seed: Option<u64>,
82 platform_version: &PlatformVersion,
83 ) -> Result<Self, ProtocolError> {
84 let mut rng = match seed {
85 None => StdRng::from_entropy(),
86 Some(seed_value) => StdRng::seed_from_u64(seed_value),
87 };
88 Self::random_identity_with_rng(key_count, &mut rng, platform_version)
89 }
90
91 pub fn random_identities(
92 count: u16,
93 key_count: KeyCount,
94 seed: Option<u64>,
95 platform_version: &PlatformVersion,
96 ) -> Result<Vec<Self>, ProtocolError> {
97 let mut rng = match seed {
98 None => StdRng::from_entropy(),
99 Some(seed_value) => StdRng::seed_from_u64(seed_value),
100 };
101 Self::random_identities_with_rng(count, key_count, &mut rng, platform_version)
102 }
103
104 pub fn random_identities_with_rng(
105 count: u16,
106 key_count: KeyCount,
107 rng: &mut StdRng,
108 platform_version: &PlatformVersion,
109 ) -> Result<Vec<Self>, ProtocolError> {
110 let mut vec: Vec<IdentityV0> = vec![];
111 for _i in 0..count {
112 vec.push(Self::random_identity_with_rng(
113 key_count,
114 rng,
115 platform_version,
116 )?);
117 }
118 Ok(vec)
119 }
120
121 pub fn random_identities_with_private_keys_with_rng<I>(
122 count: u16,
123 key_count: KeyCount,
124 rng: &mut StdRng,
125 platform_version: &PlatformVersion,
126 ) -> Result<(Vec<Self>, I), ProtocolError>
127 where
128 I: Default
129 + FromIterator<(IdentityPublicKey, [u8; 32])>
130 + Extend<(IdentityPublicKey, [u8; 32])>,
131 {
132 let mut vec: Vec<IdentityV0> = vec![];
133 let mut private_key_map: Vec<(IdentityPublicKey, [u8; 32])> = vec![];
134 for _i in 0..count {
135 let (identity, mut map) = Self::random_identity_with_main_keys_with_private_key(
136 key_count,
137 rng,
138 platform_version,
139 )?;
140 vec.push(identity);
141 private_key_map.append(&mut map);
142 }
143 Ok((vec, private_key_map.into_iter().collect()))
144 }
145}