libsession 0.1.3

Session messenger core library - cryptography, config management, networking
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
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
//! ConfigBase: lifecycle management (push/merge/confirm/dump/load) around ConfigMessage.
//!
//! Port of `libsession-util/include/session/config/base.hpp` and `src/config/base.cpp`.
//!
//! This provides the generic config wrapper that adds:
//! - Config state tracking: Clean, Dirty, Waiting
//! - Encryption with per-namespace keys
//! - Dump/load for persistence
//! - Push produces encrypted messages for swarm upload
//! - Confirm acknowledges successful upload
//! - Merge integrates incoming messages from other clients

use std::collections::HashSet;

use crate::util::bencode::{self, BtValue};

use crate::config::config_message::{
    ConfigData, ConfigValue, MutableConfigMessage, SignCallable, VerifyCallable,
};
use crate::config::encrypt::{config_decrypt, config_encrypt, pad_message, ENCRYPT_DATA_OVERHEAD};
use crate::config::namespaces::Namespace;

// ── Config state ────────────────────────────────────────────────────────────

/// Current config lifecycle state.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConfigState {
    /// Config is confirmed stored on the server and we haven't changed anything.
    Clean = 0,
    /// We have local changes that haven't been serialized yet for sending to the server.
    Dirty = 1,
    /// The caller has serialized the data, but hasn't yet reported back that it has been
    /// stored, *and* we haven't made any changes since serialization.
    Waiting = 2,
}

// ── ConfigType trait ────────────────────────────────────────────────────────

/// Trait that each config sub-type must implement to define its namespace and encryption domain.
pub trait ConfigType: Default {
    /// The storage namespace for this config type.
    fn namespace() -> Namespace;

    /// The encryption domain string (e.g., "UserProfile", "Contacts").
    fn encryption_domain() -> &'static str;

    /// Number of lagged diffs to keep (default: 5).
    fn config_lags() -> i64 {
        5
    }

    /// Whether this config type accepts protobuf-wrapped messages (legacy types).
    fn accepts_protobuf() -> bool {
        false
    }

    /// Whether this config type is read-only (cannot push changes).
    fn is_readonly() -> bool {
        false
    }

    /// Extract this config type's fields from the shared ConfigData.
    fn load_from_data(&mut self, data: &ConfigData);

    /// Store this config type's fields into ConfigData.
    fn store_to_data(&self, data: &mut ConfigData);
}

// ── PushData ────────────────────────────────────────────────────────────────

/// Data returned from a push() call, ready for upload to the swarm.
#[derive(Debug, Clone)]
pub struct PushData {
    /// Sequence number of the pushed config.
    pub seqno: i64,
    /// Encrypted messages to upload (usually one, but can be multiple for large configs).
    pub messages: Vec<Vec<u8>>,
    /// Obsolete message hashes that should be deleted from the server.
    pub obsolete_hashes: Vec<String>,
}

// ── ConfigBase ──────────────────────────────────────────────────────────────

/// Generic config wrapper providing lifecycle management around a config type.
///
/// This handles encryption, push/merge/confirm, and persistence (dump/load).
pub struct ConfigBase<T: ConfigType> {
    /// The specific config type (UserProfile, Contacts, etc.).
    pub config_type: T,

    /// The underlying mutable config message holding the key-value data.
    message: MutableConfigMessage,

    /// Encryption keys. The first key is used for encrypting new messages.
    /// All keys are tried when decrypting.
    keys: Vec<[u8; 32]>,

    /// Current lifecycle state.
    state: ConfigState,

    /// Whether a dump is needed (any mutation since last dump).
    needs_dump: bool,

    /// Currently active message hashes (from the last successful push/confirm).
    curr_hashes: HashSet<String>,

    /// Obsolete message hashes to be deleted on next push.
    old_hashes: HashSet<String>,

    /// Verifier callback for incoming signatures.
    verifier: Option<VerifyCallable>,

    /// Signer callback for outgoing signatures.
    signer: Option<SignCallable>,
}

impl<T: ConfigType> ConfigBase<T> {
    /// Creates a new config from an Ed25519 secret key and optional dump data.
    ///
    /// The secret key is used to derive the encryption key for this namespace.
    /// If `dump` is provided, previous state is restored from it.
    pub fn new(secret_key: &[u8], dump: Option<&[u8]>) -> Result<Self, String> {
        let enc_key = derive_enc_key(secret_key)?;

        let mut base = ConfigBase {
            config_type: T::default(),
            message: MutableConfigMessage::new_empty(),
            keys: vec![enc_key],
            state: ConfigState::Clean,
            needs_dump: false,
            curr_hashes: HashSet::new(),
            old_hashes: HashSet::new(),
            verifier: None,
            signer: None,
        };

        if let Some(dump_data) = dump {
            base.load_dump(dump_data)?;
        }

        // Load the config type's fields from the message data
        let data = base.message.data().clone();
        base.config_type.load_from_data(&data);

        Ok(base)
    }

    /// Creates a new config with explicit public key and optional secret key.
    /// Used for group configs where admin status determines write access.
    pub fn new_group(
        ed25519_pubkey: &[u8; 32],
        ed25519_secretkey: Option<&[u8]>,
        dump: Option<&[u8]>,
    ) -> Result<Self, String> {
        let mut base = ConfigBase {
            config_type: T::default(),
            message: MutableConfigMessage::new_empty(),
            keys: Vec::new(),
            state: ConfigState::Clean,
            needs_dump: false,
            curr_hashes: HashSet::new(),
            old_hashes: HashSet::new(),
            verifier: None,
            signer: None,
        };

        // Set up signature verification with the group's public key
        let pubkey = *ed25519_pubkey;
        base.verifier = Some(std::sync::Arc::new(move |data: &[u8], sig: &[u8]| {
            if sig.len() != 64 {
                return false;
            }
            crate::crypto::ed25519::verify(sig, &pubkey, data).unwrap_or(false)
        }));

        // If we have the secret key, set up signing
        if let Some(sk) = ed25519_secretkey {
            let sk_owned: Vec<u8> = sk.to_vec();
            base.signer = Some(std::sync::Arc::new(move |data: &[u8]| {
                crate::crypto::ed25519::sign(&sk_owned, data)
                    .expect("ed25519 signing failed")
                    .to_vec()
            }));
        }

        if let Some(dump_data) = dump {
            base.load_dump(dump_data)?;
        }

        let data = base.message.data().clone();
        base.config_type.load_from_data(&data);

        Ok(base)
    }

    /// Adds an encryption key. The first key added is used for encryption;
    /// all keys are tried for decryption.
    pub fn add_key(&mut self, key: [u8; 32], high_priority: bool) {
        if high_priority {
            self.keys.insert(0, key);
        } else {
            self.keys.push(key);
        }
    }

    /// Clears all encryption keys.
    pub fn clear_keys(&mut self) {
        self.keys.clear();
    }

    /// Returns the number of encryption keys.
    pub fn key_count(&self) -> usize {
        self.keys.len()
    }

    /// Returns true if there are local changes that need pushing.
    pub fn needs_push(&self) -> bool {
        if T::is_readonly() {
            return false;
        }
        self.state == ConfigState::Dirty
    }

    /// Returns true if the config state has changed and needs to be dumped for persistence.
    pub fn needs_dump(&self) -> bool {
        self.needs_dump
    }

    /// Returns the current state.
    pub fn state(&self) -> ConfigState {
        self.state
    }

    /// Returns true if the config is dirty (has local changes).
    pub fn is_dirty(&self) -> bool {
        self.state == ConfigState::Dirty
    }

    /// Returns the current sequence number.
    pub fn seqno(&self) -> i64 {
        self.message.seqno()
    }

    /// Returns the currently known message hashes.
    pub fn current_hashes(&self) -> Vec<String> {
        self.curr_hashes.iter().cloned().collect()
    }

    /// Returns the obsolete message hashes that should be deleted.
    pub fn old_hashes(&self) -> Vec<String> {
        self.old_hashes.iter().cloned().collect()
    }

    /// Direct read access to the underlying config data.
    pub fn data(&self) -> &ConfigData {
        self.message.data()
    }

    /// Returns a mutable reference to the underlying config message data.
    /// This marks the config as dirty.
    pub fn dirty_data(&mut self) -> &mut ConfigData {
        self.mark_dirty();
        self.message.data_mut()
    }

    /// Marks the config as dirty, indicating local changes.
    fn mark_dirty(&mut self) {
        if self.state != ConfigState::Dirty {
            self.set_state(ConfigState::Dirty);
            // When we go dirty, we need to increment the underlying message
            // The C++ code creates a new MutableConfigMessage from the old config.
            // We already have a MutableConfigMessage, so we just track that we're dirty.
        }
    }

    /// Sets the config state, tracking old hashes.
    fn set_state(&mut self, s: ConfigState) {
        if s == ConfigState::Dirty && self.state == ConfigState::Clean && !self.curr_hashes.is_empty()
        {
            // Move current hashes to old hashes when transitioning clean -> dirty
            for h in self.curr_hashes.drain() {
                self.old_hashes.insert(h);
            }
        }
        self.state = s;
        self.needs_dump = true;
    }

    /// Applies config type changes to the message data before pushing.
    fn sync_to_message(&mut self) {
        let mut data = self.message.data().clone();
        self.config_type.store_to_data(&mut data);
        *self.message.data_mut() = data;
    }

    /// Serializes the current config, encrypts it, and returns data for pushing to the swarm.
    ///
    /// After calling this, the config enters the Waiting state (unless it was already clean).
    pub fn push(&mut self) -> PushData {
        if T::is_readonly() {
            return PushData {
                seqno: 0,
                messages: vec![],
                obsolete_hashes: vec![],
            };
        }

        // Sync config_type changes into the message
        self.sync_to_message();

        let seqno = self.message.seqno();

        // Serialize and encrypt
        let serialized = self.message.serialize();
        let mut padded = serialized;
        pad_message(&mut padded, ENCRYPT_DATA_OVERHEAD);

        let encrypted = if let Some(key) = self.keys.first() {
            config_encrypt(&padded, key, T::encryption_domain())
        } else {
            padded
        };

        let obsolete: Vec<String> = self.old_hashes.drain().collect();

        if self.state == ConfigState::Dirty {
            self.set_state(ConfigState::Waiting);
        }

        PushData {
            seqno,
            messages: vec![encrypted],
            obsolete_hashes: obsolete,
        }
    }

    /// Confirms that a push was successfully stored on the server.
    ///
    /// `seqno` should match the seqno returned by push(). `msg_hash` is the server-assigned
    /// message hash.
    pub fn confirm_pushed(&mut self, seqno: i64, msg_hash: &str) {
        // Only confirm if seqno matches and we're in Waiting state
        if seqno == self.message.seqno() && self.state == ConfigState::Waiting {
            self.curr_hashes.clear();
            self.curr_hashes.insert(msg_hash.to_string());
            self.state = ConfigState::Clean;
            self.needs_dump = true;
        }
    }

    /// Merges incoming config messages from other clients.
    ///
    /// `messages` is a list of (hash, encrypted_data) pairs.
    /// Returns the hashes that were successfully processed.
    pub fn merge(&mut self, messages: &[(&str, &[u8])]) -> Result<Vec<String>, String> {
        if self.keys.is_empty() {
            return Err("Cannot merge configs without any decryption keys".into());
        }

        let mut good_hashes = Vec::new();
        let mut decrypted_messages: Vec<Vec<u8>> = Vec::new();
        let mut decrypted_hashes: Vec<String> = Vec::new();

        // Decrypt each incoming message
        for (hash, encrypted) in messages {
            let mut decrypted = None;
            for key in &self.keys {
                match config_decrypt(encrypted, key, T::encryption_domain()) {
                    Ok(plain) => {
                        decrypted = Some(plain);
                        break;
                    }
                    Err(_) => continue,
                }
            }

            if let Some(mut plain) = decrypted {
                // Strip null-byte padding from the beginning
                if let Some(pos) = plain.iter().position(|&b| b != 0)
                    && pos > 0 {
                        plain = plain[pos..].to_vec();
                    }

                if plain.is_empty() {
                    continue;
                }

                // Check for bencode dict marker 'd'
                if plain[0] == b'd' {
                    good_hashes.push(hash.to_string());
                    decrypted_hashes.push(hash.to_string());
                    decrypted_messages.push(plain);
                }
            }
        }

        if decrypted_messages.is_empty() {
            return Ok(good_hashes);
        }

        // Include our current config in the merge
        let mine = self.message.serialize();
        let old_seqno = self.message.seqno();

        let mut all_configs: Vec<&[u8]> = Vec::new();
        all_configs.push(&mine);
        for msg in &decrypted_messages {
            all_configs.push(msg);
        }

        // Build verifier/signer for merging
        let verifier_for_merge = self.verifier.clone();
        let signer_for_merge = self.signer.clone();

        // Merge using ConfigMessage
        match MutableConfigMessage::from_multiple(
            &all_configs,
            verifier_for_merge,
            signer_for_merge,
            T::config_lags(),
            Some(&|_i, _e| {
                // Error handler for individual message parse failures -- just skip
            }),
        ) {
            Ok(new_msg) => {
                let merged = new_msg.merged();
                let new_seqno = new_msg.seqno();

                // Track which hashes are now obsolete
                for hash in &decrypted_hashes {
                    self.old_hashes.insert(hash.clone());
                }

                self.message = new_msg;

                if new_seqno != old_seqno {
                    if merged {
                        // Merge conflict resolution -- mark dirty
                        self.set_state(ConfigState::Dirty);
                    } else {
                        // Remote was newer, we adopted it
                        self.state = ConfigState::Clean;
                        self.needs_dump = true;
                    }
                } else {
                    self.needs_dump = true;
                }

                // Reload config type from updated data
                let data = self.message.data().clone();
                self.config_type.load_from_data(&data);
            }
            Err(e) => {
                return Err(format!("Merge failed: {}", e));
            }
        }

        Ok(good_hashes)
    }

    /// Produces a dump of the current config state for persistence.
    ///
    /// The dump includes the serialized config message and metadata (hashes, state, etc.).
    pub fn dump(&mut self) -> Vec<u8> {
        self.sync_to_message();
        self.needs_dump = false;

        let serialized = self.message.serialize();

        // Build dump as a bencode dict
        let mut dump_dict = std::collections::BTreeMap::new();

        // "!" = body (the serialized config message)
        dump_dict.insert(b"!".to_vec(), BtValue::String(serialized));

        // "+" = current hashes
        if !self.curr_hashes.is_empty() {
            let hash_list: Vec<BtValue> = self
                .curr_hashes
                .iter()
                .map(|h| BtValue::String(h.as_bytes().to_vec()))
                .collect();
            dump_dict.insert(b"+".to_vec(), BtValue::List(hash_list));
        }

        // "-" = old hashes
        if !self.old_hashes.is_empty() {
            let hash_list: Vec<BtValue> = self
                .old_hashes
                .iter()
                .map(|h| BtValue::String(h.as_bytes().to_vec()))
                .collect();
            dump_dict.insert(b"-".to_vec(), BtValue::List(hash_list));
        }

        bencode::encode(&BtValue::Dict(dump_dict))
    }

    /// Restores config state from a dump.
    fn load_dump(&mut self, dump_data: &[u8]) -> Result<(), String> {
        let top = bencode::decode(dump_data).map_err(|e| format!("Invalid dump: {}", e))?;

        let dict = match &top {
            BtValue::Dict(d) => d,
            _ => return Err("Dump must be a bencode dict".into()),
        };

        // Load body "!"
        if let Some(BtValue::String(body)) = dict.get(b"!".as_ref()) {
            let verifier = self.verifier.clone();
            let signer = self.signer.clone();

            match MutableConfigMessage::from_bytes(body, verifier, signer, T::config_lags()) {
                Ok(msg) => {
                    self.message = msg;
                }
                Err(e) => {
                    return Err(format!("Failed to parse dump body: {}", e));
                }
            }
        }

        // Load current hashes "+"
        if let Some(BtValue::List(hashes)) = dict.get(b"+".as_ref()) {
            for h in hashes {
                if let BtValue::String(s) = h
                    && let Ok(hash_str) = String::from_utf8(s.clone()) {
                        self.curr_hashes.insert(hash_str);
                    }
            }
        }

        // Load old hashes "-"
        if let Some(BtValue::List(hashes)) = dict.get(b"-".as_ref()) {
            for h in hashes {
                if let BtValue::String(s) = h
                    && let Ok(hash_str) = String::from_utf8(s.clone()) {
                        self.old_hashes.insert(hash_str);
                    }
            }
        }

        Ok(())
    }

    /// Direct access to the config type.
    pub fn get(&self) -> &T {
        &self.config_type
    }

    /// Direct mutable access to the config type. Marks the config as dirty.
    pub fn get_mut(&mut self) -> &mut T {
        self.mark_dirty();
        &mut self.config_type
    }
}

// ── Helper: config data field accessors ─────────────────────────────────────

/// Helper functions for reading and writing individual fields within ConfigData.
/// These mirror the C++ set_nonempty_str, set_nonzero_int, set_flag, etc.
pub mod field_helpers {
    use super::*;

    /// Gets a string value from config data at the given key path.
    pub fn get_string(data: &ConfigData, key: &[u8]) -> Option<String> {
        match data.get(key) {
            Some(ConfigValue::String(s)) => {
                if s.is_empty() {
                    None
                } else {
                    String::from_utf8(s.clone()).ok()
                }
            }
            _ => None,
        }
    }

    /// Gets a byte-string value from config data at the given key.
    pub fn get_bytes(data: &ConfigData, key: &[u8]) -> Option<Vec<u8>> {
        match data.get(key) {
            Some(ConfigValue::String(s)) if !s.is_empty() => Some(s.clone()),
            _ => None,
        }
    }

    /// Gets an integer value from config data at the given key.
    pub fn get_int(data: &ConfigData, key: &[u8]) -> Option<i64> {
        match data.get(key) {
            Some(ConfigValue::Integer(n)) => Some(*n),
            _ => None,
        }
    }

    /// Gets an integer, returning 0 if not present.
    pub fn get_int_or_zero(data: &ConfigData, key: &[u8]) -> i64 {
        get_int(data, key).unwrap_or(0)
    }

    /// Gets a nested dict from config data.
    pub fn get_dict<'a>(data: &'a ConfigData, key: &[u8]) -> Option<&'a ConfigData> {
        match data.get(key) {
            Some(ConfigValue::Dict(d)) => Some(d),
            _ => None,
        }
    }

    /// Gets or creates a nested dict.
    pub fn get_or_create_dict<'a>(data: &'a mut ConfigData, key: &[u8]) -> &'a mut ConfigData {
        data.entry(key.to_vec())
            .or_insert_with(|| ConfigValue::Dict(ConfigData::new()));
        match data.get_mut(key) {
            Some(ConfigValue::Dict(d)) => d,
            _ => unreachable!(),
        }
    }

    /// Sets a string value, or removes the key if the string is empty.
    pub fn set_nonempty_str(data: &mut ConfigData, key: &[u8], val: &str) {
        if val.is_empty() {
            data.remove(key);
        } else {
            data.insert(key.to_vec(), ConfigValue::String(val.as_bytes().to_vec()));
        }
    }

    /// Sets a string value (always, even if empty, to keep dict alive).
    pub fn set_str_always(data: &mut ConfigData, key: &[u8], val: &str) {
        data.insert(key.to_vec(), ConfigValue::String(val.as_bytes().to_vec()));
    }

    /// Sets a byte-string value, or removes the key if empty.
    pub fn set_nonempty_bytes(data: &mut ConfigData, key: &[u8], val: &[u8]) {
        if val.is_empty() {
            data.remove(key);
        } else {
            data.insert(key.to_vec(), ConfigValue::String(val.to_vec()));
        }
    }

    /// Sets an integer value, or removes the key if zero.
    pub fn set_nonzero_int(data: &mut ConfigData, key: &[u8], val: i64) {
        if val == 0 {
            data.remove(key);
        } else {
            data.insert(key.to_vec(), ConfigValue::Integer(val));
        }
    }

    /// Sets an integer value, or removes the key if non-positive.
    pub fn set_positive_int(data: &mut ConfigData, key: &[u8], val: i64) {
        if val <= 0 {
            data.remove(key);
        } else {
            data.insert(key.to_vec(), ConfigValue::Integer(val));
        }
    }

    /// Sets a flag (1 if true, removes if false).
    pub fn set_flag(data: &mut ConfigData, key: &[u8], val: bool) {
        if val {
            data.insert(key.to_vec(), ConfigValue::Integer(1));
        } else {
            data.remove(key);
        }
    }

    /// Sets a pair of fields if a condition is met, removes both otherwise.
    pub fn set_pair_if(
        data: &mut ConfigData,
        condition: bool,
        key1: &[u8],
        val1: &[u8],
        key2: &[u8],
        val2: &[u8],
    ) {
        if condition {
            data.insert(key1.to_vec(), ConfigValue::String(val1.to_vec()));
            data.insert(key2.to_vec(), ConfigValue::String(val2.to_vec()));
        } else {
            data.remove(key1);
            data.remove(key2);
        }
    }
}

// ── Key derivation ──────────────────────────────────────────────────────────

/// Derives the encryption key from an Ed25519 secret key.
///
/// Uses BLAKE2b keyed hash with the key "SessionConfig", hashing the seed (first 32 bytes).
fn derive_enc_key(secret_key: &[u8]) -> Result<[u8; 32], String> {
    let seed = if secret_key.len() == 64 {
        &secret_key[..32]
    } else if secret_key.len() == 32 {
        secret_key
    } else {
        return Err(format!(
            "Invalid secret key length: expected 32 or 64, got {}",
            secret_key.len()
        ));
    };

    let hash = blake2b_simd::Params::new()
        .hash_length(32)
        .key(b"SessionConfig")
        .hash(seed);

    let mut key = [0u8; 32];
    key.copy_from_slice(hash.as_bytes());
    Ok(key)
}

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

    /// A minimal test config type
    #[derive(Default)]
    struct TestConfig {
        name: Option<String>,
    }

    impl ConfigType for TestConfig {
        fn namespace() -> Namespace {
            Namespace::UserProfile
        }
        fn encryption_domain() -> &'static str {
            "UserProfile"
        }
        fn load_from_data(&mut self, data: &ConfigData) {
            self.name = field_helpers::get_string(data, b"n");
        }
        fn store_to_data(&self, data: &mut ConfigData) {
            if let Some(ref name) = self.name {
                field_helpers::set_nonempty_str(data, b"n", name);
            }
        }
    }

    #[test]
    fn test_new_config_is_clean() {
        let seed = [0u8; 32];
        let base: ConfigBase<TestConfig> = ConfigBase::new(&seed, None).unwrap();
        assert_eq!(base.state(), ConfigState::Clean);
        assert!(!base.needs_push());
        assert!(!base.needs_dump());
    }

    #[test]
    fn test_dirty_after_mutation() {
        let seed = [0u8; 32];
        let mut base: ConfigBase<TestConfig> = ConfigBase::new(&seed, None).unwrap();
        base.get_mut().name = Some("Test".to_string());
        assert_eq!(base.state(), ConfigState::Dirty);
        assert!(base.needs_push());
        assert!(base.needs_dump());
    }

    #[test]
    fn test_push_transitions_to_waiting() {
        let seed = [0u8; 32];
        let mut base: ConfigBase<TestConfig> = ConfigBase::new(&seed, None).unwrap();
        base.get_mut().name = Some("Test".to_string());
        let push_data = base.push();
        assert_eq!(base.state(), ConfigState::Waiting);
        assert!(push_data.seqno > 0 || push_data.seqno == 0);
        assert!(!push_data.messages.is_empty());
    }

    #[test]
    fn test_confirm_transitions_to_clean() {
        let seed = [0u8; 32];
        let mut base: ConfigBase<TestConfig> = ConfigBase::new(&seed, None).unwrap();
        base.get_mut().name = Some("Test".to_string());
        let push_data = base.push();
        base.confirm_pushed(push_data.seqno, "abc123");
        assert_eq!(base.state(), ConfigState::Clean);
        assert_eq!(base.current_hashes(), vec!["abc123"]);
    }

    #[test]
    fn test_dump_and_load_roundtrip() {
        let seed = [0u8; 32];
        let mut base: ConfigBase<TestConfig> = ConfigBase::new(&seed, None).unwrap();
        base.get_mut().name = Some("Test User".to_string());
        let push_data = base.push();
        base.confirm_pushed(push_data.seqno, "hash1");

        let dump = base.dump();
        assert!(!dump.is_empty());

        // Load from dump
        let base2: ConfigBase<TestConfig> = ConfigBase::new(&seed, Some(&dump)).unwrap();
        assert_eq!(base2.get().name.as_deref(), Some("Test User"));
    }

    #[test]
    fn test_derive_enc_key_deterministic() {
        let seed1 = [1u8; 32];
        let key1 = derive_enc_key(&seed1).unwrap();
        let key2 = derive_enc_key(&seed1).unwrap();
        assert_eq!(key1, key2);

        let seed2 = [2u8; 32];
        let key3 = derive_enc_key(&seed2).unwrap();
        assert_ne!(key1, key3);
    }

    #[test]
    fn test_derive_enc_key_from_64_byte_key() {
        let seed = [1u8; 32];
        let mut full_key = [0u8; 64];
        full_key[..32].copy_from_slice(&seed);
        full_key[32..].copy_from_slice(&[2u8; 32]); // pubkey portion

        let key_from_seed = derive_enc_key(&seed).unwrap();
        let key_from_full = derive_enc_key(&full_key).unwrap();
        assert_eq!(key_from_seed, key_from_full);
    }

    #[test]
    fn test_field_helpers() {
        let mut data = ConfigData::new();

        field_helpers::set_nonempty_str(&mut data, b"n", "Alice");
        assert_eq!(field_helpers::get_string(&data, b"n"), Some("Alice".into()));

        field_helpers::set_nonempty_str(&mut data, b"n", "");
        assert_eq!(field_helpers::get_string(&data, b"n"), None);

        field_helpers::set_nonzero_int(&mut data, b"x", 42);
        assert_eq!(field_helpers::get_int(&data, b"x"), Some(42));

        field_helpers::set_nonzero_int(&mut data, b"x", 0);
        assert_eq!(field_helpers::get_int(&data, b"x"), None);

        field_helpers::set_flag(&mut data, b"f", true);
        assert_eq!(field_helpers::get_int(&data, b"f"), Some(1));

        field_helpers::set_flag(&mut data, b"f", false);
        assert_eq!(field_helpers::get_int(&data, b"f"), None);
    }
}