Create a NodeId using a random Ed25519 key pair
()
| 289 | /// Create a NodeId using a random Ed25519 key pair |
| 290 | /// |
| 291 | pub fn random() -> NodeKey { |
| 292 | // Generate random secret key and corresponding key_pair |
| 293 | let random_secret = rand::thread_rng().gen::<[u8; 32]>(); |
| 294 | let key_pair = Ed25519KeyPair::from_seed_unchecked(random_secret.as_ref()) |
| 295 | .expect("expect 32 bytes to be well-formed"); |
| 296 | // Encode the public key and secret key |
| 297 | let public_ed25519_key_bytes: [u8; 32] = key_pair |
| 298 | .public_key() |
| 299 | .as_ref() |
| 300 | .try_into() |
| 301 | .expect("expect public key to be 32 bytes"); |
| 302 | NodeKey::new( |
| 303 | NodeId { |
| 304 | public_ed25519_key_bytes, |
| 305 | }, |
| 306 | random_secret, |
| 307 | ) |
| 308 | } |
| 309 | /// Sign data with this key |
| 310 | pub fn sign(&self, data: &[u8]) -> Signature { |
| 311 | let key_pair = Ed25519KeyPair::from_seed_unchecked(&self.private_key_bytes) |