Skip to content

Commit c4f6853

Browse files
added unit tests
1 parent a32b867 commit c4f6853

File tree

2 files changed

+134
-1
lines changed

2 files changed

+134
-1
lines changed

packages/rs-dpp/src/identity/identity_public_key/key_type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ impl KeyType {
305305
KeyType::EDDSA_25519_HASH160 => {
306306
let key_pair = ed25519_dalek::SigningKey::generate(rng);
307307
(
308-
key_pair.verifying_key().to_bytes().to_vec(),
308+
ripemd160_sha256(key_pair.verifying_key().to_bytes().as_slice()).to_vec(),
309309
key_pair.to_bytes().to_vec(),
310310
)
311311
}

packages/rs-dpp/src/identity/identity_public_key/v0/methods/mod.rs

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,136 @@ impl IdentityPublicKeyHashMethodsV0 for IdentityPublicKeyV0 {
124124
}
125125
}
126126
}
127+
128+
#[cfg(test)]
129+
mod tests {
130+
use super::*;
131+
use crate::identity::{Purpose, SecurityLevel};
132+
use dashcore::Network;
133+
use dpp::version::PlatformVersion;
134+
use rand::rngs::StdRng;
135+
use rand::SeedableRng;
136+
137+
#[cfg(feature = "random-public-keys")]
138+
#[test]
139+
fn test_validate_private_key_bytes_with_random_keys() {
140+
let platform_version = PlatformVersion::latest();
141+
let mut rng = StdRng::from_entropy();
142+
143+
// Test for ECDSA_SECP256K1
144+
let key_type = KeyType::ECDSA_SECP256K1;
145+
let (public_key_data, private_key_data) = key_type
146+
.random_public_and_private_key_data(&mut rng, &platform_version)
147+
.expect("expected to generate random keys");
148+
149+
let identity_public_key = IdentityPublicKeyV0 {
150+
id: 1,
151+
purpose: Purpose::AUTHENTICATION,
152+
security_level: SecurityLevel::HIGH,
153+
contract_bounds: None,
154+
key_type,
155+
data: public_key_data.into(),
156+
read_only: false,
157+
disabled_at: None,
158+
};
159+
160+
// Validate that the private key matches the public key
161+
assert_eq!(
162+
identity_public_key
163+
.validate_private_key_bytes(&private_key_data, Network::Testnet)
164+
.unwrap(),
165+
true
166+
);
167+
168+
// Test with an invalid private key
169+
let invalid_private_key_bytes = vec![0u8; private_key_data.len()];
170+
assert_eq!(
171+
identity_public_key
172+
.validate_private_key_bytes(&invalid_private_key_bytes, Network::Testnet)
173+
.unwrap(),
174+
false
175+
);
176+
}
177+
178+
#[cfg(all(feature = "random-public-keys", feature = "bls-signatures"))]
179+
#[test]
180+
fn test_validate_private_key_bytes_with_random_keys_bls12_381() {
181+
let platform_version = PlatformVersion::latest();
182+
let mut rng = StdRng::from_entropy();
183+
184+
// Test for BLS12_381
185+
let key_type = KeyType::BLS12_381;
186+
let (public_key_data, private_key_data) = key_type
187+
.random_public_and_private_key_data(&mut rng, &platform_version)
188+
.expect("expected to generate random keys");
189+
190+
let identity_public_key = IdentityPublicKeyV0 {
191+
id: 2,
192+
purpose: Purpose::AUTHENTICATION,
193+
security_level: SecurityLevel::HIGH,
194+
contract_bounds: None,
195+
key_type,
196+
data: public_key_data.into(),
197+
read_only: false,
198+
disabled_at: None,
199+
};
200+
201+
// Validate that the private key matches the public key
202+
assert_eq!(
203+
identity_public_key
204+
.validate_private_key_bytes(&private_key_data, Network::Testnet)
205+
.unwrap(),
206+
true
207+
);
208+
209+
// Test with an invalid private key
210+
let invalid_private_key_bytes = vec![0u8; private_key_data.len()];
211+
assert_eq!(
212+
identity_public_key
213+
.validate_private_key_bytes(&invalid_private_key_bytes, Network::Testnet)
214+
.unwrap(),
215+
false
216+
);
217+
}
218+
219+
#[cfg(all(feature = "random-public-keys", feature = "ed25519-dalek"))]
220+
#[test]
221+
fn test_validate_private_key_bytes_with_random_keys_eddsa_25519_hash160() {
222+
let platform_version = PlatformVersion::latest();
223+
let mut rng = StdRng::from_entropy();
224+
225+
// Test for EDDSA_25519_HASH160
226+
let key_type = KeyType::EDDSA_25519_HASH160;
227+
let (public_key_data, private_key_data) = key_type
228+
.random_public_and_private_key_data(&mut rng, &platform_version)
229+
.expect("expected to generate random keys");
230+
231+
let identity_public_key = IdentityPublicKeyV0 {
232+
id: 3,
233+
purpose: Purpose::AUTHENTICATION,
234+
security_level: SecurityLevel::HIGH,
235+
contract_bounds: None,
236+
key_type,
237+
data: public_key_data.into(),
238+
read_only: false,
239+
disabled_at: None,
240+
};
241+
242+
// Validate that the private key matches the public key
243+
assert_eq!(
244+
identity_public_key
245+
.validate_private_key_bytes(&private_key_data, Network::Testnet)
246+
.unwrap(),
247+
true
248+
);
249+
250+
// Test with an invalid private key
251+
let invalid_private_key_bytes = vec![0u8; private_key_data.len()];
252+
assert_eq!(
253+
identity_public_key
254+
.validate_private_key_bytes(&invalid_private_key_bytes, Network::Testnet)
255+
.unwrap(),
256+
false
257+
);
258+
}
259+
}

0 commit comments

Comments
 (0)