|
| 1 | +//! # Charon P2P K1 |
| 2 | +
|
| 3 | +use std::path::{Path, PathBuf}; |
| 4 | + |
| 5 | +use charon_k1util as k1util; |
| 6 | +use k256::{SecretKey, elliptic_curve::rand_core::OsRng}; |
| 7 | +use rand::RngCore; |
| 8 | + |
| 9 | +const KEY_FILE_NAME: &str = "charon-enr-private-key"; |
| 10 | +const KEY_BACKUP_DIR: &str = "charon-enr-private-key-backups"; |
| 11 | + |
| 12 | +type Result<T> = std::result::Result<T, K1Error>; |
| 13 | + |
| 14 | +/// An error that can occur when loading a private key. |
| 15 | +#[derive(Debug, thiserror::Error)] |
| 16 | +pub enum K1Error { |
| 17 | + /// K1 utility error. |
| 18 | + #[error("K1 utility error: {0}")] |
| 19 | + K1UtilError(#[from] k1util::K1UtilError), |
| 20 | + |
| 21 | + /// IOError. |
| 22 | + #[error("IO error: {0}")] |
| 23 | + IoError(#[from] std::io::Error), |
| 24 | +} |
| 25 | + |
| 26 | +/// Returns the charon-enr-private-key path relative to the data dir. |
| 27 | +pub fn key_path(data_dir: &Path) -> PathBuf { |
| 28 | + data_dir.join(KEY_FILE_NAME) |
| 29 | +} |
| 30 | + |
| 31 | +/// Loads the private key from the data dir. |
| 32 | +pub fn load_priv_key(data_dir: &Path) -> Result<SecretKey> { |
| 33 | + k1util::load(&key_path(data_dir)).map_err(K1Error::K1UtilError) |
| 34 | +} |
| 35 | + |
| 36 | +/// Generates a new private key and saves it to the data dir. |
| 37 | +pub fn new_saved_priv_key(data_dir: &Path) -> Result<SecretKey> { |
| 38 | + backup_priv_key(data_dir)?; |
| 39 | + |
| 40 | + std::fs::create_dir_all(data_dir)?; |
| 41 | + |
| 42 | + let key = SecretKey::random(&mut OsRng); |
| 43 | + |
| 44 | + k1util::save(&key, &key_path(data_dir)).map_err(K1Error::K1UtilError)?; |
| 45 | + |
| 46 | + Ok(key) |
| 47 | +} |
| 48 | + |
| 49 | +/// Backs up the private key to the backup directory. |
| 50 | +/// |
| 51 | +/// The backup directory is created if it doesn't exist. |
| 52 | +fn backup_priv_key(data_dir: &Path) -> Result<()> { |
| 53 | + let key_path = key_path(data_dir); |
| 54 | + |
| 55 | + if !key_path.exists() { |
| 56 | + // Nothing to backup |
| 57 | + return Ok(()); |
| 58 | + } |
| 59 | + |
| 60 | + let current_time = chrono::Utc::now(); |
| 61 | + let nonce = OsRng.next_u64(); |
| 62 | + let backup_path = data_dir.join(KEY_BACKUP_DIR).join(format!( |
| 63 | + "{}_{}", |
| 64 | + current_time.format("%Y-%m-%d_%H-%M-%S_%f"), |
| 65 | + nonce |
| 66 | + )); |
| 67 | + std::fs::create_dir_all( |
| 68 | + backup_path |
| 69 | + .parent() |
| 70 | + .expect("Backup path parent should exist"), |
| 71 | + ) |
| 72 | + .map_err(K1Error::IoError)?; |
| 73 | + if backup_path.is_dir() { |
| 74 | + panic!("Backup path is a directory: {:?}", backup_path); |
| 75 | + } |
| 76 | + std::fs::copy(key_path, backup_path).map_err(K1Error::IoError)?; |
| 77 | + Ok(()) |
| 78 | +} |
| 79 | + |
| 80 | +#[cfg(test)] |
| 81 | +mod tests { |
| 82 | + use super::*; |
| 83 | + use k256::elliptic_curve::rand_core::OsRng; |
| 84 | + use std::{collections::HashSet, fs}; |
| 85 | + use tempfile::TempDir; |
| 86 | + |
| 87 | + fn setup_temp_dir() -> TempDir { |
| 88 | + tempfile::tempdir().expect("Failed to create temp dir") |
| 89 | + } |
| 90 | + |
| 91 | + fn create_test_key_file(data_dir: &Path) -> Result<SecretKey> { |
| 92 | + let key = SecretKey::random(&mut OsRng); |
| 93 | + k1util::save(&key, &key_path(data_dir))?; |
| 94 | + Ok(key) |
| 95 | + } |
| 96 | + |
| 97 | + #[test] |
| 98 | + fn test_key_path() { |
| 99 | + let data_dir = Path::new("/test/data"); |
| 100 | + let path = key_path(data_dir); |
| 101 | + assert_eq!(path, PathBuf::from("/test/data/charon-enr-private-key")); |
| 102 | + } |
| 103 | + |
| 104 | + #[test] |
| 105 | + fn test_new_saved_priv_key_creates_key() -> Result<()> { |
| 106 | + let temp_dir = setup_temp_dir(); |
| 107 | + let data_dir = temp_dir.path(); |
| 108 | + |
| 109 | + let key = new_saved_priv_key(data_dir)?; |
| 110 | + |
| 111 | + let key_file = key_path(data_dir); |
| 112 | + assert!(key_file.exists()); |
| 113 | + |
| 114 | + let loaded_key = load_priv_key(data_dir)?; |
| 115 | + assert_eq!(key.to_bytes(), loaded_key.to_bytes()); |
| 116 | + |
| 117 | + Ok(()) |
| 118 | + } |
| 119 | + |
| 120 | + #[test] |
| 121 | + fn test_new_saved_priv_key_creates_data_dir() -> Result<()> { |
| 122 | + let temp_dir = setup_temp_dir(); |
| 123 | + let data_dir = temp_dir.path().join("new_dir"); |
| 124 | + |
| 125 | + assert!(!data_dir.exists()); |
| 126 | + |
| 127 | + new_saved_priv_key(&data_dir)?; |
| 128 | + |
| 129 | + assert!(data_dir.exists()); |
| 130 | + assert!(data_dir.is_dir()); |
| 131 | + |
| 132 | + assert!(key_path(&data_dir).exists()); |
| 133 | + |
| 134 | + Ok(()) |
| 135 | + } |
| 136 | + |
| 137 | + #[test] |
| 138 | + fn test_load_priv_key_success() -> Result<()> { |
| 139 | + let temp_dir = setup_temp_dir(); |
| 140 | + let data_dir = temp_dir.path(); |
| 141 | + |
| 142 | + let original_key = create_test_key_file(data_dir)?; |
| 143 | + |
| 144 | + let loaded_key = load_priv_key(data_dir)?; |
| 145 | + |
| 146 | + assert_eq!(original_key.to_bytes(), loaded_key.to_bytes()); |
| 147 | + |
| 148 | + Ok(()) |
| 149 | + } |
| 150 | + |
| 151 | + #[test] |
| 152 | + fn test_load_priv_key_file_not_found() { |
| 153 | + let temp_dir = setup_temp_dir(); |
| 154 | + let data_dir = temp_dir.path(); |
| 155 | + |
| 156 | + let result = load_priv_key(data_dir); |
| 157 | + |
| 158 | + assert!(result.is_err()); |
| 159 | + assert!(matches!(result, Err(K1Error::K1UtilError(_)))); |
| 160 | + } |
| 161 | + |
| 162 | + #[test] |
| 163 | + fn test_backup_priv_key_creates_backup() -> Result<()> { |
| 164 | + let temp_dir = setup_temp_dir(); |
| 165 | + let data_dir = temp_dir.path(); |
| 166 | + |
| 167 | + create_test_key_file(data_dir)?; |
| 168 | + |
| 169 | + backup_priv_key(data_dir)?; |
| 170 | + |
| 171 | + let backup_dir = data_dir.join(KEY_BACKUP_DIR); |
| 172 | + assert!(backup_dir.exists()); |
| 173 | + assert!(backup_dir.is_dir()); |
| 174 | + |
| 175 | + let entries: Vec<_> = fs::read_dir(&backup_dir)?.filter_map(|e| e.ok()).collect(); |
| 176 | + assert_eq!(entries.len(), 1); |
| 177 | + |
| 178 | + Ok(()) |
| 179 | + } |
| 180 | + |
| 181 | + #[test] |
| 182 | + fn test_new_saved_priv_key_with_existing_key() -> Result<()> { |
| 183 | + let temp_dir = setup_temp_dir(); |
| 184 | + let data_dir = temp_dir.path(); |
| 185 | + |
| 186 | + let first_key = new_saved_priv_key(data_dir)?; |
| 187 | + |
| 188 | + let second_key = new_saved_priv_key(data_dir)?; |
| 189 | + |
| 190 | + assert_ne!(first_key.to_bytes(), second_key.to_bytes()); |
| 191 | + |
| 192 | + let loaded_key = load_priv_key(data_dir)?; |
| 193 | + assert_eq!(second_key.to_bytes(), loaded_key.to_bytes()); |
| 194 | + |
| 195 | + let backup_dir = data_dir.join(KEY_BACKUP_DIR); |
| 196 | + assert!(backup_dir.exists()); |
| 197 | + |
| 198 | + let entries: Vec<_> = fs::read_dir(&backup_dir)?.filter_map(|e| e.ok()).collect(); |
| 199 | + assert_eq!(entries.len(), 1); |
| 200 | + |
| 201 | + Ok(()) |
| 202 | + } |
| 203 | + |
| 204 | + #[test] |
| 205 | + fn test_backup_uniqueness() -> Result<()> { |
| 206 | + const NUM_BACKUPS: usize = 5; |
| 207 | + |
| 208 | + let temp_dir = setup_temp_dir(); |
| 209 | + let data_dir = temp_dir.path(); |
| 210 | + |
| 211 | + create_test_key_file(data_dir)?; |
| 212 | + |
| 213 | + for _ in 0..NUM_BACKUPS { |
| 214 | + backup_priv_key(data_dir)?; |
| 215 | + } |
| 216 | + |
| 217 | + let backup_dir = data_dir.join(KEY_BACKUP_DIR); |
| 218 | + let entries: Vec<_> = fs::read_dir(&backup_dir)?.filter_map(|e| e.ok()).collect(); |
| 219 | + let backup_names: HashSet<_> = entries.iter().map(|e| e.file_name()).collect(); |
| 220 | + |
| 221 | + assert_eq!( |
| 222 | + backup_names.len(), |
| 223 | + NUM_BACKUPS, |
| 224 | + "Should have 5 unique backup names" |
| 225 | + ); |
| 226 | + |
| 227 | + Ok(()) |
| 228 | + } |
| 229 | +} |
0 commit comments