guardian-db 0.15.0

High-performance, local-first decentralized database built on Rust and Iroh
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
/// Sistema de Sincronização de Chaves para Guardian DB
///
/// Sincronização robusta de chaves entre peers,
/// garantindo consistência criptográfica e prevenindo ataques de replay.
use crate::guardian::error::{GuardianError, Result};
use crate::keystore::SledKeystore;
use crate::log::identity_provider::Keystore;
use crate::p2p::network::config::ClientConfig;
use chrono::{DateTime, Utc};
use ed25519_dalek::{Signature, Signer, SigningKey, Verifier, VerifyingKey};
use iroh::NodeId;
use rand_core::OsRng;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, VecDeque};
use std::sync::Arc;
use std::time::{Duration, SystemTime};
use tokio::sync::{Mutex, RwLock};
use tracing::{debug, info, warn};
use uuid::Uuid;

/// Versão do protocolo de sincronização
const SYNC_PROTOCOL_VERSION: u32 = 1;

/// Tempo máximo para aceitar mensagens (previne replay attacks)
const MAX_MESSAGE_AGE: Duration = Duration::from_secs(300); // 5 minutos

/// Número máximo de tentativas de sincronização (reservado para uso futuro)
#[allow(dead_code)]
const MAX_SYNC_RETRIES: u8 = 3;

/// Tamanho máximo da fila de sincronização
const MAX_SYNC_QUEUE_SIZE: usize = 1000;

/// Status de sincronização de uma chave
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum KeySyncStatus {
    /// Chave está sincronizada
    Synchronized,
    /// Chave em processo de sincronização
    Synchronizing,
    /// Sincronização pendente
    Pending,
    /// Erro na sincronização
    Failed(String),
    /// Conflito detectado (necessária resolução manual)
    Conflict(String),
}

/// Tipo de operação de sincronização
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum SyncOperation {
    /// Criar nova chave
    Create,
    /// Atualizar chave existente
    Update,
    /// Deletar chave
    Delete,
    /// Sincronizar metadados
    MetadataSync,
}

/// Metadados de uma chave sincronizada
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KeyMetadata {
    /// ID único da chave
    pub key_id: String,
    /// Versão da chave (para controle de conflitos)
    pub version: u64,
    /// Timestamp da última modificação
    pub last_modified: DateTime<Utc>,
    /// NodeID que criou a chave
    pub creator: NodeId,
    /// Assinatura dos metadados
    pub signature: Vec<u8>,
    /// Algoritmo de criptografia usado
    pub crypto_algorithm: String,
    /// Hash da chave pública
    pub public_key_hash: Vec<u8>,
}

/// Mensagem de sincronização entre peers
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SyncMessage {
    /// ID único da mensagem
    pub message_id: Uuid,
    /// Versão do protocolo
    pub protocol_version: u32,
    /// Timestamp da mensagem
    pub timestamp: SystemTime,
    /// NodeID do remetente
    pub sender: NodeId,
    /// Tipo de operação
    pub operation: SyncOperation,
    /// Metadados da chave
    pub metadata: KeyMetadata,
    /// Dados da chave (encriptados)
    pub key_data: Option<Vec<u8>>,
    /// Assinatura da mensagem completa
    pub message_signature: Vec<u8>,
}

/// Entrada na fila de sincronização (reservado para uso futuro)
#[derive(Debug, Clone)]
#[allow(dead_code)]
struct SyncQueueEntry {
    /// Mensagem a ser sincronizada
    #[allow(dead_code)]
    message: SyncMessage,
    /// Número de tentativas
    #[allow(dead_code)]
    retry_count: u8,
    /// Próxima tentativa
    #[allow(dead_code)]
    next_retry: SystemTime,
    /// Peers que devem receber a mensagem
    #[allow(dead_code)]
    target_peers: Vec<NodeId>,
}

/// Estatísticas de sincronização
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct SyncStatistics {
    /// Total de mensagens sincronizadas
    pub messages_synced: u64,
    /// Mensagens pendentes na fila
    pub pending_messages: u64,
    /// Conflitos detectados
    pub conflicts_detected: u64,
    /// Conflitos resolvidos
    pub conflicts_resolved: u64,
    /// Taxa de sucesso de sincronização
    pub success_rate: f64,
    /// Latência média de sincronização (ms)
    pub avg_sync_latency_ms: f64,
    /// Peers ativos na sincronização
    pub active_peers: u32,
}

/// Sistema principal de sincronização de chaves
pub struct KeySynchronizer {
    /// Configuração do sistema (reservado para uso futuro)
    #[allow(dead_code)]
    client_config: ClientConfig,
    /// Keystore local
    local_keystore: Arc<SledKeystore>,
    /// Keypair principal do nó (Ed25519)
    node_signing_key: SigningKey,
    /// NodeID do nó
    node_id: NodeId,
    /// Mapeamento de chaves sincronizadas
    synchronized_keys: Arc<RwLock<HashMap<String, KeyMetadata>>>,
    /// Status de sincronização por chave
    sync_status: Arc<RwLock<HashMap<String, KeySyncStatus>>>,
    /// Fila de sincronização
    sync_queue: Arc<Mutex<VecDeque<SyncQueueEntry>>>,
    /// Cache de mensagens recentes (previne replay)
    message_cache: Arc<RwLock<HashMap<Uuid, SystemTime>>>,
    /// Estatísticas de sincronização
    statistics: Arc<RwLock<SyncStatistics>>,
    /// Chaves de confiança (peers autorizados)
    trusted_peers: Arc<RwLock<HashMap<NodeId, VerifyingKey>>>,
}

impl KeySynchronizer {
    /// Cria nova instância do sincronizador de chaves
    pub async fn new(client_config: &ClientConfig) -> Result<Self> {
        let keystore_path = client_config
            .data_store_path
            .as_ref()
            .map(|p| p.join("keystore"))
            .unwrap_or_else(|| std::env::temp_dir().join("guardian_keystore"));

        let local_keystore = Arc::new(SledKeystore::new(Some(keystore_path))?);

        // Carregar ou gerar keypair principal
        let node_signing_key = Self::load_or_generate_keypair(&local_keystore).await?;
        let node_id = NodeId::from(node_signing_key.verifying_key());

        info!(
            "Inicializando sincronizador de chaves para NodeID: {}",
            node_id
        );

        Ok(Self {
            client_config: client_config.clone(),
            local_keystore,
            node_signing_key,
            node_id,
            synchronized_keys: Arc::new(RwLock::new(HashMap::new())),
            sync_status: Arc::new(RwLock::new(HashMap::new())),
            sync_queue: Arc::new(Mutex::new(VecDeque::new())),
            message_cache: Arc::new(RwLock::new(HashMap::new())),
            statistics: Arc::new(RwLock::new(SyncStatistics::default())),
            trusted_peers: Arc::new(RwLock::new(HashMap::new())),
        })
    }

    /// Retorna o NodeID do nó
    pub fn node_id(&self) -> NodeId {
        self.node_id
    }

    /// Retorna a chave de assinatura do nó
    pub fn signing_key(&self) -> &SigningKey {
        &self.node_signing_key
    }

    /// Carrega ou gera keypair principal
    async fn load_or_generate_keypair(keystore: &SledKeystore) -> Result<SigningKey> {
        const MAIN_KEYPAIR_KEY: &str = "main_node_keypair";

        // Tentar carregar keypair existente
        if let Some(data) = keystore.get(MAIN_KEYPAIR_KEY).await?
            && data.len() == 32
        {
            debug!("Carregando keypair principal existente");
            return SigningKey::try_from(&data[..32])
                .map_err(|e| GuardianError::Other(format!("Erro ao carregar keypair: {}", e)));
        }

        // Gerar novo keypair
        let signing_key = SigningKey::generate(&mut OsRng);
        keystore
            .put(MAIN_KEYPAIR_KEY, signing_key.as_bytes())
            .await?;

        info!("Novo keypair principal gerado e salvo");
        Ok(signing_key)
    }

    /// Adiciona peer confiável para sincronização
    pub async fn add_trusted_peer(&self, node_id: NodeId, public_key: VerifyingKey) -> Result<()> {
        let mut trusted = self.trusted_peers.write().await;
        trusted.insert(node_id, public_key);
        info!("Peer confiável adicionado: {}", node_id);
        Ok(())
    }

    /// Remove peer confiável
    pub async fn remove_trusted_peer(&self, node_id: &NodeId) -> Result<bool> {
        let mut trusted = self.trusted_peers.write().await;
        let removed = trusted.remove(node_id).is_some();
        if removed {
            info!("Peer removido da lista de confiança: {}", node_id);
        }
        Ok(removed)
    }

    /// Sincroniza uma chave específica com peers
    pub async fn sync_key(&self, key_id: &str, operation: SyncOperation) -> Result<()> {
        debug!(
            "Iniciando sincronização da chave: {} (operação: {:?})",
            key_id, operation
        );

        // Obter metadados da chave
        let metadata = self.get_key_metadata(key_id).await?;

        // Criar mensagem de sincronização
        let message = self.create_sync_message(operation, metadata, None).await?;

        // Adicionar à fila de sincronização
        self.enqueue_sync_message(message).await?;

        // Atualizar status
        self.update_sync_status(key_id, KeySyncStatus::Synchronizing)
            .await;

        Ok(())
    }

    /// Processa mensagem de sincronização recebida
    pub async fn handle_sync_message(&self, message: SyncMessage) -> Result<()> {
        // Verificar idade da mensagem (prevenir replay attacks)
        if self.is_message_too_old(&message)? {
            warn!(
                "Mensagem de sincronização rejeitada (muito antiga): {:?}",
                message.message_id
            );
            return Err(GuardianError::Other("Mensagem muito antiga".to_string()));
        }

        // Verificar se já processamos esta mensagem
        if self.is_message_duplicate(&message).await? {
            debug!("Mensagem duplicada ignorada: {:?}", message.message_id);
            return Ok(());
        }

        // Verificar assinatura da mensagem
        self.verify_message_signature(&message).await?;

        // Processar operação
        match message.operation {
            SyncOperation::Create => self.handle_key_create(&message).await?,
            SyncOperation::Update => self.handle_key_update(&message).await?,
            SyncOperation::Delete => self.handle_key_delete(&message).await?,
            SyncOperation::MetadataSync => self.handle_metadata_sync(&message).await?,
        }

        // Adicionar à cache de mensagens processadas
        self.cache_processed_message(&message).await;

        // Atualizar estatísticas
        self.update_statistics().await;

        Ok(())
    }

    /// Obtém metadados de uma chave
    async fn get_key_metadata(&self, key_id: &str) -> Result<KeyMetadata> {
        let synchronized_keys = self.synchronized_keys.read().await;

        if let Some(metadata) = synchronized_keys.get(key_id) {
            return Ok(metadata.clone());
        }

        // Chave não encontrada, criar metadados iniciais
        let key_data = self
            .local_keystore
            .get(key_id)
            .await?
            .ok_or_else(|| GuardianError::Other(format!("Chave não encontrada: {}", key_id)))?;

        let public_key_hash = blake3::hash(&key_data).as_bytes().to_vec();

        let metadata = KeyMetadata {
            key_id: key_id.to_string(),
            version: 1,
            last_modified: Utc::now(),
            creator: self.node_id,
            signature: Vec::new(), // Será preenchida depois
            crypto_algorithm: "Ed25519".to_string(),
            public_key_hash,
        };

        Ok(metadata)
    }

    /// Cria mensagem de sincronização
    async fn create_sync_message(
        &self,
        operation: SyncOperation,
        metadata: KeyMetadata,
        key_data: Option<Vec<u8>>,
    ) -> Result<SyncMessage> {
        let message = SyncMessage {
            message_id: Uuid::new_v4(),
            protocol_version: SYNC_PROTOCOL_VERSION,
            timestamp: SystemTime::now(),
            sender: self.node_id,
            operation,
            metadata,
            key_data,
            message_signature: Vec::new(), // Será preenchida depois
        };

        // Assinar mensagem
        let signed_message = self.sign_sync_message(message).await?;

        Ok(signed_message)
    }

    /// Assina mensagem de sincronização
    async fn sign_sync_message(&self, mut message: SyncMessage) -> Result<SyncMessage> {
        // Serializar mensagem sem assinatura
        let mut message_copy = message.clone();
        message_copy.message_signature.clear();

        let message_bytes = postcard::to_allocvec(&message_copy)
            .map_err(|e| GuardianError::Other(format!("Erro ao serializar mensagem: {}", e)))?;

        // Assinar com keypair do nó
        let signature = self
            .node_signing_key
            .sign(&message_bytes)
            .to_bytes()
            .to_vec();

        message.message_signature = signature;
        Ok(message)
    }

    /// Verifica assinatura de mensagem
    async fn verify_message_signature(&self, message: &SyncMessage) -> Result<()> {
        // Obter chave pública do remetente
        let trusted_peers = self.trusted_peers.read().await;
        let verifying_key = trusted_peers.get(&message.sender).ok_or_else(|| {
            GuardianError::Other(format!("Peer não confiável: {}", message.sender))
        })?;

        // Reconstruir mensagem sem assinatura
        let mut message_copy = message.clone();
        message_copy.message_signature.clear();

        let message_bytes = postcard::to_allocvec(&message_copy)
            .map_err(|e| GuardianError::Other(format!("Erro ao serializar mensagem: {}", e)))?;

        // Verificar assinatura
        let signature = Signature::from_slice(&message.message_signature)
            .map_err(|e| GuardianError::Other(format!("Assinatura inválida: {}", e)))?;

        verifying_key
            .verify(&message_bytes, &signature)
            .map_err(|e| {
                GuardianError::Other(format!("Verificação de assinatura falhou: {}", e))
            })?;

        Ok(())
    }

    /// Verifica se mensagem é muito antiga
    fn is_message_too_old(&self, message: &SyncMessage) -> Result<bool> {
        let now = SystemTime::now();
        let age = now
            .duration_since(message.timestamp)
            .map_err(|_| GuardianError::Other("Timestamp inválido".to_string()))?;

        Ok(age > MAX_MESSAGE_AGE)
    }

    /// Verifica se mensagem é duplicata
    async fn is_message_duplicate(&self, message: &SyncMessage) -> Result<bool> {
        let cache = self.message_cache.read().await;
        Ok(cache.contains_key(&message.message_id))
    }

    /// Adiciona mensagem à fila de sincronização
    async fn enqueue_sync_message(&self, message: SyncMessage) -> Result<()> {
        let mut queue = self.sync_queue.lock().await;

        // Verificar se a fila não está cheia
        if queue.len() >= MAX_SYNC_QUEUE_SIZE {
            // Remove mensagem mais antiga
            queue.pop_front();
            warn!("Fila de sincronização cheia, removendo mensagem mais antiga");
        }

        let entry = SyncQueueEntry {
            message,
            retry_count: 0,
            next_retry: SystemTime::now(),
            target_peers: Vec::new(), // Será preenchido baseado em peers conectados
        };

        queue.push_back(entry);
        debug!("Mensagem adicionada à fila de sincronização");

        Ok(())
    }

    /// Processa criação de chave
    async fn handle_key_create(&self, message: &SyncMessage) -> Result<()> {
        let key_id = &message.metadata.key_id;

        // Verificar se a chave já existe
        if self.local_keystore.has(key_id).await? {
            // Verificar versões para detectar conflitos
            let local_metadata = self.get_key_metadata(key_id).await?;
            if local_metadata.version >= message.metadata.version {
                debug!("Chave já existe com versão igual ou superior: {}", key_id);
                return Ok(());
            }
        }

        // Criar/atualizar chave
        if let Some(key_data) = &message.key_data {
            self.local_keystore.put(key_id, key_data).await?;
        }

        // Atualizar metadados
        let mut synchronized_keys = self.synchronized_keys.write().await;
        synchronized_keys.insert(key_id.clone(), message.metadata.clone());

        self.update_sync_status(key_id, KeySyncStatus::Synchronized)
            .await;

        info!("Chave criada via sincronização: {}", key_id);
        Ok(())
    }

    /// Processa atualização de chave
    async fn handle_key_update(&self, message: &SyncMessage) -> Result<()> {
        let key_id = &message.metadata.key_id;

        // Verificar se a chave existe
        if !self.local_keystore.has(key_id).await? {
            warn!("Tentativa de atualizar chave inexistente: {}", key_id);
            return Err(GuardianError::Other(format!(
                "Chave não encontrada: {}",
                key_id
            )));
        }

        // Verificar versão para detectar conflitos
        let local_metadata = self.get_key_metadata(key_id).await?;
        if local_metadata.version > message.metadata.version {
            warn!("Conflito de versão detectado para chave: {}", key_id);
            self.update_sync_status(
                key_id,
                KeySyncStatus::Conflict(format!(
                    "Local: v{}, Remoto: v{}",
                    local_metadata.version, message.metadata.version
                )),
            )
            .await;
            return Ok(());
        }

        // Atualizar chave
        if let Some(key_data) = &message.key_data {
            self.local_keystore.put(key_id, key_data).await?;
        }

        // Atualizar metadados
        let mut synchronized_keys = self.synchronized_keys.write().await;
        synchronized_keys.insert(key_id.clone(), message.metadata.clone());

        self.update_sync_status(key_id, KeySyncStatus::Synchronized)
            .await;

        info!("Chave atualizada via sincronização: {}", key_id);
        Ok(())
    }

    /// Processa exclusão de chave
    async fn handle_key_delete(&self, message: &SyncMessage) -> Result<()> {
        let key_id = &message.metadata.key_id;

        // Deletar chave local
        self.local_keystore.delete(key_id).await?;

        // Remover metadados
        let mut synchronized_keys = self.synchronized_keys.write().await;
        synchronized_keys.remove(key_id);

        let mut sync_status = self.sync_status.write().await;
        sync_status.remove(key_id);

        info!("Chave deletada via sincronização: {}", key_id);
        Ok(())
    }

    /// Processa sincronização de metadados
    async fn handle_metadata_sync(&self, message: &SyncMessage) -> Result<()> {
        let key_id = &message.metadata.key_id;

        // Atualizar apenas metadados (não os dados da chave)
        let mut synchronized_keys = self.synchronized_keys.write().await;
        synchronized_keys.insert(key_id.clone(), message.metadata.clone());

        debug!("Metadados sincronizados para chave: {}", key_id);
        Ok(())
    }

    /// Adiciona mensagem processada à cache
    async fn cache_processed_message(&self, message: &SyncMessage) {
        let mut cache = self.message_cache.write().await;
        cache.insert(message.message_id, SystemTime::now());

        // Limpar mensagens antigas da cache
        let cutoff = SystemTime::now() - MAX_MESSAGE_AGE;
        cache.retain(|_, timestamp| *timestamp > cutoff);
    }

    /// Atualiza status de sincronização de uma chave
    async fn update_sync_status(&self, key_id: &str, status: KeySyncStatus) {
        let mut sync_status = self.sync_status.write().await;
        sync_status.insert(key_id.to_string(), status);
    }

    /// Atualiza estatísticas de sincronização
    async fn update_statistics(&self) {
        let mut stats = self.statistics.write().await;
        stats.messages_synced += 1;

        let queue = self.sync_queue.lock().await;
        stats.pending_messages = queue.len() as u64;

        let trusted_peers = self.trusted_peers.read().await;
        stats.active_peers = trusted_peers.len() as u32;

        // Calcular taxa de sucesso
        let sync_status = self.sync_status.read().await;
        let total_keys = sync_status.len() as u64;
        let synchronized_keys = sync_status
            .values()
            .filter(|status| matches!(status, KeySyncStatus::Synchronized))
            .count() as u64;

        stats.success_rate = if total_keys > 0 {
            (synchronized_keys as f64 / total_keys as f64) * 100.0
        } else {
            100.0
        };
    }

    /// Obtém estatísticas de sincronização
    pub async fn get_statistics(&self) -> SyncStatistics {
        self.statistics.read().await.clone()
    }

    /// Obtém status de sincronização de uma chave
    pub async fn get_key_sync_status(&self, key_id: &str) -> Option<KeySyncStatus> {
        let sync_status = self.sync_status.read().await;
        sync_status.get(key_id).cloned()
    }

    /// Lista todas as chaves sincronizadas
    pub async fn list_synchronized_keys(&self) -> Vec<String> {
        let synchronized_keys = self.synchronized_keys.read().await;
        synchronized_keys.keys().cloned().collect()
    }

    /// Lista todos os peers confiáveis
    pub async fn list_trusted_peers(&self) -> Vec<NodeId> {
        let trusted = self.trusted_peers.read().await;
        trusted.keys().copied().collect()
    }

    /// Força sincronização completa com peers
    pub async fn force_full_sync(&self) -> Result<()> {
        info!("Iniciando sincronização completa forçada");

        let keys = self.local_keystore.list_keys().await?;
        for key_id in keys {
            self.sync_key(&key_id, SyncOperation::MetadataSync).await?;
        }

        info!(
            "Sincronização completa forçada iniciada para {} chaves",
            self.synchronized_keys.read().await.len()
        );
        Ok(())
    }

    /// Exporta configuração de sincronização
    pub async fn export_sync_config(&self) -> Result<Vec<u8>> {
        let sync_config = SyncExportConfig {
            node_id: self.node_id,
            trusted_peers: self.trusted_peers.read().await.clone(),
            synchronized_keys: self.synchronized_keys.read().await.clone(),
            statistics: self.statistics.read().await.clone(),
        };

        postcard::to_allocvec(&sync_config)
            .map_err(|e| GuardianError::Other(format!("Erro ao exportar configuração: {}", e)))
    }
}

/// Configuração para exportação
#[derive(Debug, Serialize, Deserialize)]
struct SyncExportConfig {
    node_id: NodeId,
    trusted_peers: HashMap<NodeId, VerifyingKey>,
    synchronized_keys: HashMap<String, KeyMetadata>,
    statistics: SyncStatistics,
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    #[tokio::test]
    async fn test_key_synchronizer_creation() {
        let temp_dir = TempDir::new().unwrap();
        let client_config = ClientConfig {
            data_store_path: Some(temp_dir.path().to_path_buf()),
            ..Default::default()
        };

        let synchronizer = KeySynchronizer::new(&client_config).await.unwrap();
        assert!(!synchronizer.node_id().to_string().is_empty());
    }

    #[tokio::test]
    async fn test_sync_message_creation_and_verification() {
        let temp_dir = TempDir::new().unwrap();
        let client_config = ClientConfig {
            data_store_path: Some(temp_dir.path().to_path_buf()),
            ..Default::default()
        };

        let synchronizer = KeySynchronizer::new(&client_config).await.unwrap();

        // Criar metadados de teste
        let metadata = KeyMetadata {
            key_id: "test_key".to_string(),
            version: 1,
            last_modified: Utc::now(),
            creator: synchronizer.node_id(),
            signature: Vec::new(),
            crypto_algorithm: "Ed25519".to_string(),
            public_key_hash: vec![1, 2, 3, 4],
        };

        // Criar mensagem
        let message = synchronizer
            .create_sync_message(SyncOperation::Create, metadata, Some(b"test_data".to_vec()))
            .await
            .unwrap();

        // Verificar estrutura da mensagem
        assert_eq!(message.protocol_version, SYNC_PROTOCOL_VERSION);
        assert_eq!(message.sender, synchronizer.node_id());
        assert_eq!(message.operation, SyncOperation::Create);
        assert!(!message.message_signature.is_empty());
    }
}