qudag-network 0.5.0

P2P networking layer for QuDAG - LibP2P with onion routing, dark addressing, and quantum encryption
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
use blake3::Hasher;
use bs58;
use rand_core::{CryptoRng, RngCore};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::{Arc, RwLock};
use std::time::{SystemTime, UNIX_EPOCH};
use thiserror::Error;

// Import crypto primitives from the crypto module
use qudag_crypto::ml_dsa::{MlDsaError, MlDsaKeyPair, MlDsaPublicKey};
use qudag_crypto::ml_kem::MlKem768;

use crate::types::NetworkAddress;
use crate::types::PeerId;

/// Errors that can occur during dark domain operations
#[derive(Error, Debug)]
pub enum DarkResolverError {
    #[error("Domain name already registered")]
    DomainExists,
    #[error("Domain not found")]
    DomainNotFound,
    #[error("Invalid domain name format")]
    InvalidDomain,
    #[error("Cryptographic operation failed: {0}")]
    CryptoError(String),
    #[error("Domain record access error")]
    StorageError,
    #[error("Domain has expired")]
    DomainExpired,
    #[error("Invalid signature")]
    InvalidSignature,
    #[error("Address generation failed: {0}")]
    AddressGenerationError(String),
    #[error("DHT operation failed: {0}")]
    DhtError(String),
    #[error("ML-DSA error: {0}")]
    MlDsaError(#[from] MlDsaError),
}

/// A resolved dark domain record with quantum-resistant signatures
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct DarkDomainRecord {
    /// ML-DSA public key for signature verification
    pub signing_public_key: Vec<u8>,
    /// ML-KEM public key for encryption
    pub encryption_public_key: Vec<u8>,
    /// Network addresses (can have multiple)
    pub addresses: Vec<NetworkAddress>,
    /// Human-readable alias
    pub alias: Option<String>,
    /// Time-to-live in seconds
    pub ttl: u32,
    /// Registration timestamp
    pub registered_at: u64,
    /// Expiration timestamp
    pub expires_at: u64,
    /// Owner's PeerId
    pub owner_id: PeerId,
    /// Record signature using ML-DSA
    pub signature: Vec<u8>,
    /// Additional metadata
    pub metadata: HashMap<String, String>,
}

/// Dark address derived from ML-DSA public key
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct DarkAddress {
    /// The .dark address (base58 encoded hash)
    pub address: String,
    /// Full domain name (e.g., "mynode.dark")
    pub domain: String,
}

/// Address book entry for human-readable names
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct AddressBookEntry {
    /// Human-readable name
    pub name: String,
    /// Associated dark address
    pub dark_address: DarkAddress,
    /// Optional notes
    pub notes: Option<String>,
    /// Added timestamp
    pub added_at: u64,
}

impl DarkDomainRecord {
    /// Create a new domain record
    pub fn new(
        signing_keypair: &MlDsaKeyPair,
        encryption_public_key: Vec<u8>,
        addresses: Vec<NetworkAddress>,
        alias: Option<String>,
        ttl: u32,
        owner_id: PeerId,
    ) -> Result<Self, DarkResolverError> {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs();

        let mut record = Self {
            signing_public_key: signing_keypair.public_key().to_vec(),
            encryption_public_key,
            addresses,
            alias,
            ttl,
            registered_at: now,
            expires_at: now + ttl as u64,
            owner_id,
            signature: vec![],
            metadata: HashMap::new(),
        };

        // Sign the record
        record.sign(signing_keypair)?;
        Ok(record)
    }

    /// Sign the record with ML-DSA
    fn sign(&mut self, keypair: &MlDsaKeyPair) -> Result<(), DarkResolverError> {
        let mut rng = rand::thread_rng();
        let message = self.to_signable_bytes()?;
        self.signature = keypair
            .sign(&message, &mut rng)
            .map_err(|e| DarkResolverError::MlDsaError(e))?;
        Ok(())
    }

    /// Verify the record's signature
    pub fn verify_signature(&self) -> Result<(), DarkResolverError> {
        let public_key = MlDsaPublicKey::from_bytes(&self.signing_public_key)
            .map_err(|e| DarkResolverError::MlDsaError(e))?;
        let message = self.to_signable_bytes()?;
        public_key
            .verify(&message, &self.signature)
            .map_err(|e| DarkResolverError::MlDsaError(e))?;
        Ok(())
    }

    /// Convert record to bytes for signing (excludes signature field)
    fn to_signable_bytes(&self) -> Result<Vec<u8>, DarkResolverError> {
        let mut hasher = Hasher::new();
        hasher.update(&self.signing_public_key);
        hasher.update(&self.encryption_public_key);
        for addr in &self.addresses {
            hasher.update(
                &bincode::serialize(addr)
                    .map_err(|e| DarkResolverError::CryptoError(e.to_string()))?,
            );
        }
        if let Some(alias) = &self.alias {
            hasher.update(alias.as_bytes());
        }
        hasher.update(&self.ttl.to_le_bytes());
        hasher.update(&self.registered_at.to_le_bytes());
        hasher.update(&self.expires_at.to_le_bytes());
        hasher.update(
            &bincode::serialize(&self.owner_id)
                .map_err(|e| DarkResolverError::CryptoError(e.to_string()))?,
        );
        Ok(hasher.finalize().as_bytes().to_vec())
    }

    /// Check if the record has expired
    pub fn is_expired(&self) -> bool {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_secs();
        now > self.expires_at
    }
}

/// Dark domain resolver that manages .dark domain registrations and lookups
pub struct DarkResolver {
    /// Thread-safe storage for domain records
    domains: Arc<RwLock<HashMap<String, DarkDomainRecord>>>,
    /// Address book for human-readable names
    address_book: Arc<RwLock<HashMap<String, AddressBookEntry>>>,
    /// Reverse lookup: dark address -> domain name
    reverse_lookup: Arc<RwLock<HashMap<String, String>>>,
    /// DHT client for distributed storage (placeholder)
    dht_client: Option<Arc<dyn DhtClient>>,
}

/// Trait for DHT client operations
pub trait DhtClient: Send + Sync {
    /// Store a value in the DHT
    fn put(&self, key: &[u8], value: &[u8]) -> Result<(), DarkResolverError>;
    /// Retrieve a value from the DHT
    fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>, DarkResolverError>;
    /// Remove a value from the DHT
    fn remove(&self, key: &[u8]) -> Result<(), DarkResolverError>;
}

impl Default for DarkResolver {
    fn default() -> Self {
        Self::new()
    }
}

impl DarkResolver {
    /// Creates a new dark domain resolver
    pub fn new() -> Self {
        Self {
            domains: Arc::new(RwLock::new(HashMap::new())),
            address_book: Arc::new(RwLock::new(HashMap::new())),
            reverse_lookup: Arc::new(RwLock::new(HashMap::new())),
            dht_client: None,
        }
    }

    /// Create a resolver with DHT client
    pub fn with_dht(dht_client: Arc<dyn DhtClient>) -> Self {
        Self {
            domains: Arc::new(RwLock::new(HashMap::new())),
            address_book: Arc::new(RwLock::new(HashMap::new())),
            reverse_lookup: Arc::new(RwLock::new(HashMap::new())),
            dht_client: Some(dht_client),
        }
    }

    /// Generate a .dark address from ML-DSA public key
    pub fn generate_dark_address(
        public_key: &[u8],
        custom_name: Option<&str>,
    ) -> Result<DarkAddress, DarkResolverError> {
        // Hash the public key with BLAKE3
        let mut hasher = Hasher::new();
        hasher.update(b"dark_address_v1");
        hasher.update(public_key);
        let hash = hasher.finalize();

        // Take first 20 bytes and encode as base58
        let address_bytes = &hash.as_bytes()[..20];
        let address = bs58::encode(address_bytes).into_string();

        // Generate domain name
        let domain = if let Some(name) = custom_name {
            if !Self::is_valid_custom_name(name) {
                return Err(DarkResolverError::InvalidDomain);
            }
            format!("{}.dark", name)
        } else {
            // Use first 8 chars of address as subdomain
            format!("{}.dark", &address[..8].to_lowercase())
        };

        Ok(DarkAddress { address, domain })
    }

    /// Validate custom name for .dark domain
    fn is_valid_custom_name(name: &str) -> bool {
        // Must be 3-63 chars, alphanumeric + hyphens, not start/end with hyphen
        name.len() >= 3
            && name.len() <= 63
            && name.chars().all(|c| c.is_alphanumeric() || c == '-')
            && !name.starts_with('-')
            && !name.ends_with('-')
    }

    /// Register a new .dark domain with quantum-resistant signatures
    pub fn register_domain<R: CryptoRng + RngCore>(
        &self,
        custom_name: Option<&str>,
        addresses: Vec<NetworkAddress>,
        alias: Option<String>,
        ttl: u32,
        owner_id: PeerId,
        rng: &mut R,
    ) -> Result<DarkAddress, DarkResolverError> {
        // Generate ML-DSA keypair for signing
        let signing_keypair =
            MlDsaKeyPair::generate(rng).map_err(|e| DarkResolverError::MlDsaError(e))?;

        // Generate ML-KEM keypair for encryption
        let (kem_public, _kem_secret) =
            MlKem768::keygen().map_err(|e| DarkResolverError::CryptoError(e.to_string()))?;

        // Generate dark address from signing public key
        let dark_address = Self::generate_dark_address(signing_keypair.public_key(), custom_name)?;

        // Validate domain doesn't exist
        if !Self::is_valid_dark_domain(&dark_address.domain) {
            return Err(DarkResolverError::InvalidDomain);
        }

        // Create domain record
        let record = DarkDomainRecord::new(
            &signing_keypair,
            kem_public.as_bytes().to_vec(),
            addresses,
            alias,
            ttl,
            owner_id,
        )?;

        // Store locally
        {
            let mut domains = self
                .domains
                .write()
                .map_err(|_| DarkResolverError::StorageError)?;

            if domains.contains_key(&dark_address.domain) {
                return Err(DarkResolverError::DomainExists);
            }

            domains.insert(dark_address.domain.clone(), record.clone());
        }

        // Update reverse lookup
        {
            let mut reverse = self
                .reverse_lookup
                .write()
                .map_err(|_| DarkResolverError::StorageError)?;
            reverse.insert(dark_address.address.clone(), dark_address.domain.clone());
        }

        // Store in DHT if available
        if let Some(dht) = &self.dht_client {
            let key = Self::domain_to_dht_key(&dark_address.domain);
            let value = bincode::serialize(&record)
                .map_err(|e| DarkResolverError::DhtError(e.to_string()))?;
            dht.put(&key, &value)?;
        }

        Ok(dark_address)
    }

    /// Convert domain to DHT key
    fn domain_to_dht_key(domain: &str) -> Vec<u8> {
        let mut hasher = Hasher::new();
        hasher.update(b"dark_domain:");
        hasher.update(domain.as_bytes());
        hasher.finalize().as_bytes().to_vec()
    }

    /// Look up a .dark domain and return its record
    pub fn lookup_domain(&self, domain: &str) -> Result<DarkDomainRecord, DarkResolverError> {
        // Validate domain name
        if !Self::is_valid_dark_domain(domain) {
            return Err(DarkResolverError::InvalidDomain);
        }

        // Try local storage first
        {
            let domains = self
                .domains
                .read()
                .map_err(|_| DarkResolverError::StorageError)?;

            if let Some(record) = domains.get(domain) {
                // Check expiration
                if record.is_expired() {
                    return Err(DarkResolverError::DomainExpired);
                }
                // Verify signature
                record.verify_signature()?;
                return Ok(record.clone());
            }
        }

        // Try DHT if not found locally
        if let Some(dht) = &self.dht_client {
            let key = Self::domain_to_dht_key(domain);
            if let Some(value) = dht.get(&key)? {
                let record: DarkDomainRecord = bincode::deserialize(&value)
                    .map_err(|e| DarkResolverError::DhtError(e.to_string()))?;

                // Verify and cache
                if record.is_expired() {
                    return Err(DarkResolverError::DomainExpired);
                }
                record.verify_signature()?;

                // Cache locally
                let mut domains = self
                    .domains
                    .write()
                    .map_err(|_| DarkResolverError::StorageError)?;
                domains.insert(domain.to_string(), record.clone());

                return Ok(record);
            }
        }

        Err(DarkResolverError::DomainNotFound)
    }

    /// Resolve a .dark domain to network addresses
    pub fn resolve_addresses(
        &self,
        domain: &str,
    ) -> Result<Vec<NetworkAddress>, DarkResolverError> {
        let record = self.lookup_domain(domain)?;
        Ok(record.addresses)
    }

    /// Add entry to address book
    pub fn add_to_address_book(
        &self,
        name: String,
        dark_address: DarkAddress,
        notes: Option<String>,
    ) -> Result<(), DarkResolverError> {
        let entry = AddressBookEntry {
            name: name.clone(),
            dark_address,
            notes,
            added_at: SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_secs(),
        };

        let mut book = self
            .address_book
            .write()
            .map_err(|_| DarkResolverError::StorageError)?;
        book.insert(name, entry);
        Ok(())
    }

    /// Look up address book entry by name
    pub fn lookup_address_book(&self, name: &str) -> Result<AddressBookEntry, DarkResolverError> {
        let book = self
            .address_book
            .read()
            .map_err(|_| DarkResolverError::StorageError)?;
        book.get(name)
            .cloned()
            .ok_or(DarkResolverError::DomainNotFound)
    }

    /// List all address book entries
    pub fn list_address_book(&self) -> Result<Vec<AddressBookEntry>, DarkResolverError> {
        let book = self
            .address_book
            .read()
            .map_err(|_| DarkResolverError::StorageError)?;
        Ok(book.values().cloned().collect())
    }

    /// Update domain record (requires signature from owner)
    pub fn update_domain(
        &self,
        domain: &str,
        record: DarkDomainRecord,
    ) -> Result<(), DarkResolverError> {
        // Verify the new record's signature
        record.verify_signature()?;

        // Get existing record to verify ownership
        let existing = self.lookup_domain(domain)?;

        // Verify same owner (by comparing signing public keys)
        if existing.signing_public_key != record.signing_public_key {
            return Err(DarkResolverError::InvalidSignature);
        }

        // Update local storage
        {
            let mut domains = self
                .domains
                .write()
                .map_err(|_| DarkResolverError::StorageError)?;
            domains.insert(domain.to_string(), record.clone());
        }

        // Update DHT
        if let Some(dht) = &self.dht_client {
            let key = Self::domain_to_dht_key(domain);
            let value = bincode::serialize(&record)
                .map_err(|e| DarkResolverError::DhtError(e.to_string()))?;
            dht.put(&key, &value)?;
        }

        Ok(())
    }

    /// Remove expired domains
    pub fn cleanup_expired(&self) -> Result<usize, DarkResolverError> {
        let mut count = 0;
        let mut to_remove = Vec::new();

        // Find expired domains
        {
            let domains = self
                .domains
                .read()
                .map_err(|_| DarkResolverError::StorageError)?;
            for (domain, record) in domains.iter() {
                if record.is_expired() {
                    to_remove.push(domain.clone());
                }
            }
        }

        // Remove expired domains
        {
            let mut domains = self
                .domains
                .write()
                .map_err(|_| DarkResolverError::StorageError)?;
            let mut reverse = self
                .reverse_lookup
                .write()
                .map_err(|_| DarkResolverError::StorageError)?;

            for domain in to_remove {
                if let Some(record) = domains.remove(&domain) {
                    count += 1;
                    // Remove from reverse lookup
                    let addr = Self::generate_dark_address(&record.signing_public_key, None)?;
                    reverse.remove(&addr.address);

                    // Remove from DHT
                    if let Some(dht) = &self.dht_client {
                        let key = Self::domain_to_dht_key(&domain);
                        let _ = dht.remove(&key);
                    }
                }
            }
        }

        Ok(count)
    }

    /// Validates a .dark domain name format
    fn is_valid_dark_domain(domain: &str) -> bool {
        // Must end with .dark
        if !domain.ends_with(".dark") {
            return false;
        }

        // Extract subdomain
        let subdomain = &domain[..domain.len() - 5];

        // Validation rules:
        // - Subdomain length between 3 and 63 chars
        // - Alphanumeric + hyphens
        // - Cannot start or end with hyphen
        // - No consecutive hyphens
        subdomain.len() >= 3
            && subdomain.len() <= 63
            && !subdomain.starts_with('-')
            && !subdomain.ends_with('-')
            && !subdomain.contains("--")
            && subdomain.chars().all(|c| c.is_alphanumeric() || c == '-')
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rand::thread_rng;

    // Mock DHT client for testing
    struct MockDhtClient {
        storage: Arc<RwLock<HashMap<Vec<u8>, Vec<u8>>>>,
    }

    impl MockDhtClient {
        fn new() -> Self {
            Self {
                storage: Arc::new(RwLock::new(HashMap::new())),
            }
        }
    }

    impl DhtClient for MockDhtClient {
        fn put(&self, key: &[u8], value: &[u8]) -> Result<(), DarkResolverError> {
            let mut storage = self
                .storage
                .write()
                .map_err(|_| DarkResolverError::StorageError)?;
            storage.insert(key.to_vec(), value.to_vec());
            Ok(())
        }

        fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>, DarkResolverError> {
            let storage = self
                .storage
                .read()
                .map_err(|_| DarkResolverError::StorageError)?;
            Ok(storage.get(key).cloned())
        }

        fn remove(&self, key: &[u8]) -> Result<(), DarkResolverError> {
            let mut storage = self
                .storage
                .write()
                .map_err(|_| DarkResolverError::StorageError)?;
            storage.remove(key);
            Ok(())
        }
    }

    #[test]
    fn test_valid_dark_domains() {
        assert!(DarkResolver::is_valid_dark_domain("test.dark"));
        assert!(DarkResolver::is_valid_dark_domain("my-domain.dark"));
        assert!(DarkResolver::is_valid_dark_domain("node123.dark"));
        assert!(DarkResolver::is_valid_dark_domain("a2b.dark"));

        // Invalid cases
        assert!(!DarkResolver::is_valid_dark_domain("invalid"));
        assert!(!DarkResolver::is_valid_dark_domain(".dark"));
        assert!(!DarkResolver::is_valid_dark_domain("test.darknet"));
        assert!(!DarkResolver::is_valid_dark_domain("-test.dark"));
        assert!(!DarkResolver::is_valid_dark_domain("test-.dark"));
        assert!(!DarkResolver::is_valid_dark_domain("test--domain.dark"));
        assert!(!DarkResolver::is_valid_dark_domain("ab.dark")); // too short
        assert!(!DarkResolver::is_valid_dark_domain(&format!(
            "{}.dark",
            "a".repeat(64)
        ))); // too long
    }

    #[test]
    fn test_dark_address_generation() {
        let public_key = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

        // Generate without custom name
        let addr1 = DarkResolver::generate_dark_address(&public_key, None).unwrap();
        assert!(addr1.address.len() > 0);
        assert!(addr1.domain.ends_with(".dark"));

        // Generate with custom name
        let addr2 = DarkResolver::generate_dark_address(&public_key, Some("mynode")).unwrap();
        assert_eq!(addr2.domain, "mynode.dark");

        // Same public key should generate same address
        let addr3 = DarkResolver::generate_dark_address(&public_key, None).unwrap();
        assert_eq!(addr1.address, addr3.address);
    }

    #[test]
    fn test_domain_registration_and_resolution() {
        let mut rng = thread_rng();
        let resolver = DarkResolver::with_dht(Arc::new(MockDhtClient::new()));
        let owner_id = PeerId::random();
        let addresses = vec![
            NetworkAddress::new([1, 2, 3, 4], 8080),
            NetworkAddress::new([5, 6, 7, 8], 9090),
        ];

        // Register domain
        let dark_addr = resolver
            .register_domain(
                Some("testnode"),
                addresses.clone(),
                Some("Test Node".to_string()),
                3600, // 1 hour TTL
                owner_id.clone(),
                &mut rng,
            )
            .unwrap();

        assert_eq!(dark_addr.domain, "testnode.dark");

        // Lookup domain
        let record = resolver.lookup_domain(&dark_addr.domain).unwrap();
        assert_eq!(record.addresses, addresses);
        assert_eq!(record.alias, Some("Test Node".to_string()));
        assert_eq!(record.owner_id, owner_id);
        assert_eq!(record.ttl, 3600);

        // Resolve addresses
        let resolved = resolver.resolve_addresses(&dark_addr.domain).unwrap();
        assert_eq!(resolved, addresses);

        // Try to register same custom name (should fail)
        let result = resolver.register_domain(
            Some("testnode"),
            vec![],
            None,
            3600,
            PeerId::random(),
            &mut rng,
        );
        assert!(matches!(result, Err(DarkResolverError::DomainExists)));
    }

    #[test]
    fn test_address_book() {
        let resolver = DarkResolver::new();
        let dark_addr = DarkAddress {
            address: "3HGvnkH2VwR3cD8r7shs7V".to_string(),
            domain: "mynode.dark".to_string(),
        };

        // Add to address book
        resolver
            .add_to_address_book(
                "Alice's Node".to_string(),
                dark_addr.clone(),
                Some("Primary node".to_string()),
            )
            .unwrap();

        // Lookup by name
        let entry = resolver.lookup_address_book("Alice's Node").unwrap();
        assert_eq!(entry.dark_address, dark_addr);
        assert_eq!(entry.notes, Some("Primary node".to_string()));

        // List all entries
        let entries = resolver.list_address_book().unwrap();
        assert_eq!(entries.len(), 1);
    }

    #[test]
    fn test_domain_expiration() {
        let mut rng = thread_rng();
        let resolver = DarkResolver::new();
        let owner_id = PeerId::random();

        // Create an already expired record
        let signing_keypair = MlDsaKeyPair::generate(&mut rng).unwrap();
        let (kem_public, _) = MlKem768::keygen().unwrap();

        let mut record = DarkDomainRecord {
            signing_public_key: signing_keypair.public_key().to_vec(),
            encryption_public_key: kem_public.as_bytes().to_vec(),
            addresses: vec![NetworkAddress::new([1, 2, 3, 4], 8080)],
            alias: None,
            ttl: 60,
            registered_at: 1000,
            expires_at: 1060, // Already expired
            owner_id,
            signature: vec![],
            metadata: HashMap::new(),
        };

        // Sign the record
        record.sign(&signing_keypair).unwrap();

        // Manually insert expired record
        {
            let mut domains = resolver.domains.write().unwrap();
            domains.insert("expired.dark".to_string(), record);
        }

        // Try to lookup - should fail with DomainExpired
        let result = resolver.lookup_domain("expired.dark");
        assert!(matches!(result, Err(DarkResolverError::DomainExpired)));

        // Cleanup should remove it
        let removed = resolver.cleanup_expired().unwrap();
        assert_eq!(removed, 1);

        // Should now be not found
        let result = resolver.lookup_domain("expired.dark");
        assert!(matches!(result, Err(DarkResolverError::DomainNotFound)));
    }

    #[test]
    fn test_signature_verification() {
        let mut rng = thread_rng();
        let signing_keypair = MlDsaKeyPair::generate(&mut rng).unwrap();
        let (kem_public, _) = MlKem768::keygen().unwrap();
        let owner_id = PeerId::random();

        // Create and sign a record
        let record = DarkDomainRecord::new(
            &signing_keypair,
            kem_public.as_bytes().to_vec(),
            vec![NetworkAddress::new([1, 2, 3, 4], 8080)],
            None,
            3600,
            owner_id,
        )
        .unwrap();

        // Verify signature should succeed
        assert!(record.verify_signature().is_ok());

        // Tamper with the record
        let mut tampered = record.clone();
        tampered.ttl = 7200; // Change TTL

        // Verification should fail
        assert!(tampered.verify_signature().is_err());
    }
}