libkeri 0.1.0

A Rust library for KERI (Key Event Receipt Infrastructure)
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
use crate::cesr::prefixer::Prefixer;
use crate::cesr::signing::Cipher;
use crate::keri::app::keeping::creators::Algos;
use crate::keri::core::filing::{BaseFiler, Filer, FilerDefaults};
use crate::keri::db::dbing::LMDBer;
use crate::keri::db::errors::DBError;
use crate::keri::db::koming::{Komer, SerialKind};
use crate::keri::db::subing::cesr::CesrSuber;
use crate::keri::db::subing::signer::CryptSignerSuber;
use crate::keri::db::subing::Suber;
use serde::{Deserialize, Serialize};
use std::iter::IntoIterator;
use std::path::PathBuf;
use std::sync::Arc;

/// Public key list with indexes and datetime created
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PubLot {
    /// List of fully qualified Base64 public keys
    #[serde(default)]
    pub pubs: Vec<String>,

    /// Rotation index of set of public keys at establishment event
    /// Index of key set at inception event is 0
    #[serde(default)]
    pub ridx: usize,

    /// Key index of starting key in key set in sequence wrt to all public keys
    /// Example if each set has 3 keys then ridx 2 has kidx of 2*3 = 6
    #[serde(default)]
    pub kidx: usize,

    /// Datetime in ISO8601 format of when key set was first created
    #[serde(default)]
    pub dt: String,
}

impl Default for PubLot {
    fn default() -> Self {
        Self {
            pubs: Vec::new(),
            ridx: 0,
            kidx: 0,
            dt: String::new(),
        }
    }
}

impl IntoIterator for PubLot {
    type Item = (String, serde_json::Value);
    type IntoIter = std::vec::IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        let mut result = Vec::new();

        // Convert the struct to key-value pairs similar to Python's asdict
        result.push((
            "pubs".to_string(),
            serde_json::to_value(&self.pubs).unwrap(),
        ));
        result.push((
            "ridx".to_string(),
            serde_json::to_value(&self.ridx).unwrap(),
        ));
        result.push((
            "kidx".to_string(),
            serde_json::to_value(&self.kidx).unwrap(),
        ));
        result.push(("dt".to_string(), serde_json::to_value(&self.dt).unwrap()));

        result.into_iter()
    }
}

/// Prefix's public key situation (sets of public keys)
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PreSit {
    /// Previous public key lot
    #[serde(default)]
    pub old: Option<PubLot>,

    /// Newly current public key lot
    #[serde(default)]
    pub new: PubLot,

    /// Next public key lot
    #[serde(default)]
    pub nxt: PubLot,
}

impl Default for PreSit {
    fn default() -> Self {
        Self {
            old: Some(PubLot::default()),
            new: PubLot::default(),
            nxt: PubLot::default(),
        }
    }
}

impl IntoIterator for PreSit {
    type Item = (String, serde_json::Value);
    type IntoIter = std::vec::IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        let mut result = Vec::new();

        // Convert the struct to key-value pairs similar to Python's asdict
        result.push(("old".to_string(), serde_json::to_value(&self.old).unwrap()));
        result.push(("new".to_string(), serde_json::to_value(&self.new).unwrap()));
        result.push(("nxt".to_string(), serde_json::to_value(&self.nxt).unwrap()));

        result.into_iter()
    }
}

/// Prefix's parameters for creating new key pairs
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PrePrm {
    /// Prefix index for this keypair sequence
    #[serde(default)]
    pub pidx: usize,

    /// Algorithm used to create new key pairs (default is salty)
    #[serde(default = "default_algo")]
    pub algo: String,

    /// Salt used for salty algorithm
    #[serde(default)]
    pub salt: String,

    /// Default unique path stem for salty algorithm
    #[serde(default)]
    pub stem: String,

    /// Security tier for stretch index salty algorithm
    #[serde(default)]
    pub tier: String,
}

fn default_algo() -> String {
    Algos::Salty.to_string()
}

impl Default for PrePrm {
    fn default() -> Self {
        Self {
            pidx: 0,
            algo: Algos::Salty.to_string(),
            salt: String::new(),
            stem: String::new(),
            tier: String::new(),
        }
    }
}

impl IntoIterator for PrePrm {
    type Item = (String, serde_json::Value);
    type IntoIter = std::vec::IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        let mut result = Vec::new();

        // Convert the struct to key-value pairs similar to Python's asdict
        result.push((
            "pidx".to_string(),
            serde_json::to_value(&self.pidx).unwrap(),
        ));
        result.push((
            "algo".to_string(),
            serde_json::to_value(&self.algo).unwrap(),
        ));
        result.push((
            "salt".to_string(),
            serde_json::to_value(&self.salt).unwrap(),
        ));
        result.push((
            "stem".to_string(),
            serde_json::to_value(&self.stem).unwrap(),
        ));
        result.push((
            "tier".to_string(),
            serde_json::to_value(&self.tier).unwrap(),
        ));

        result.into_iter()
    }
}

/// Prefix's public key set (list) at rotation index ridx
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PubSet {
    /// List of fully qualified Base64 public keys
    #[serde(default)]
    pub pubs: Vec<String>,
}

impl Default for PubSet {
    fn default() -> Self {
        Self { pubs: Vec::new() }
    }
}

impl IntoIterator for PubSet {
    type Item = (String, serde_json::Value);
    type IntoIter = std::vec::IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        let mut result = Vec::new();

        // Convert the struct to key-value pairs similar to Python's asdict
        result.push((
            "pubs".to_string(),
            serde_json::to_value(&self.pubs).unwrap(),
        ));

        result.into_iter()
    }
}

/// Keeper struct for key pair storage (KS)
/// Sets up named sub databases for key pair storage.
/// Methods provide key pair creation, storage, and data signing.
pub struct Keeper<'db> {
    /// Base database
    lmdber: Arc<&'db LMDBer>, // The base LMDB database

    /// Global parameters for all prefixes
    /// Key is parameter label, Value is parameter
    pub gbls: Suber<'db>,

    /// Private keys database
    /// Key is public key (fully qualified qb64)
    /// Value is private key (fully qualified qb64)
    pub pris: CryptSignerSuber<'db>,

    /// Encrypted private keys database
    /// Key is identifier prefix (fully qualified qb64)
    /// Value is encrypted private key
    pub prxs: CesrSuber<'db, Cipher>,

    /// Next key digests database
    /// Key is identifier prefix (fully qualified qb64)
    /// Value is next key commitment (digest)
    pub nxts: CesrSuber<'db, Cipher>,

    /// Prefixes database
    /// Key is first public key in key sequence for a prefix (fully qualified qb64)
    /// Value is prefix or first public key (temporary) (fully qualified qb64)
    pub pres: CesrSuber<'db, Prefixer>,

    /// Prefix parameters database
    /// Key is identifier prefix (fully qualified qb64)
    /// Value is serialized parameter dict of public key parameters
    pub prms: Komer<'db, PrePrm>,

    /// Prefix situation database
    /// Key is identifier prefix (fully qualified qb64)
    /// Value is serialized parameter dict of public key situation
    pub sits: Komer<'db, PreSit>,

    /// Public keys database
    /// Key is prefix.ridx (rotation index as 32 char hex string)
    /// Value is serialized list of fully qualified public keys
    pub pubs: Komer<'db, PubSet>,
}

impl<'db> Filer for Keeper<'db> {
    fn defaults() -> FilerDefaults {
        BaseFiler::defaults()
    }

    #[cfg(target_os = "windows")]
    const TAIL_DIR_PATH: &'static str = "keri\\ks";
    #[cfg(not(target_os = "windows"))]
    const TAIL_DIR_PATH: &'static str = "keri/ks";

    #[cfg(target_os = "windows")]
    const ALT_TAIL_DIR_PATH: &'static str = ".keri\\ks";
    #[cfg(not(target_os = "windows"))]
    const ALT_TAIL_DIR_PATH: &'static str = ".keri/ks";

    const TEMP_PREFIX: &'static str = "keri_ks_";
}

impl<'db> Keeper<'db> {
    /// Maximum number of named databases
    pub const MAX_NAMED_DBS: u32 = 10;

    /// Create a new Keeper instance
    pub fn new(lmdber: Arc<&'db LMDBer>) -> Result<Self, DBError> {
        // Create the keeper instance
        let keeper = Keeper {
            lmdber: lmdber.clone(),
            // These will be initialized in the reopen method
            gbls: Suber::new(lmdber.clone(), "gbls.", None, false)
                .map_err(|e| DBError::DatabaseError(format!("SuberError: {}", e)))?,
            pris: CryptSignerSuber::new(lmdber.clone(), "pris.", None, false)
                .map_err(|e| DBError::DatabaseError(format!("SuberError: {}", e)))?,
            prxs: CesrSuber::new(lmdber.clone(), "prxs.", None, false)
                .map_err(|e| DBError::DatabaseError(format!("SuberError: {}", e)))?,
            nxts: CesrSuber::new(lmdber.clone(), "nxts.", None, false)
                .map_err(|e| DBError::DatabaseError(format!("SuberError: {}", e)))?,
            pres: CesrSuber::new(lmdber.clone(), "pres.", None, false)
                .map_err(|e| DBError::DatabaseError(format!("SuberError: {}", e)))?,
            prms: Komer::new(lmdber.clone(), "prms.", SerialKind::Json)
                .map_err(|e| DBError::DatabaseError(format!("SuberError: {}", e)))?,
            sits: Komer::new(lmdber.clone(), "sits.", SerialKind::Json)
                .map_err(|e| DBError::DatabaseError(format!("SuberError: {}", e)))?,
            pubs: Komer::new(lmdber.clone(), "pubs.", SerialKind::Json)
                .map_err(|e| DBError::DatabaseError(format!("SuberError: {}", e)))?,
        };

        Ok(keeper)
    }

    /// Check if database is opened
    pub fn opened(&self) -> bool {
        self.lmdber.opened()
    }

    /// Get name of database
    pub fn name(&self) -> String {
        self.lmdber.name()
    }

    /// Get database path
    pub fn path(&self) -> Option<PathBuf> {
        self.lmdber.path()
    }

    /// Close the database
    /// Check if database is temporary
    pub fn temp(&self) -> bool {
        self.lmdber.temp()
    }

    /// Create a key to access the pubs database with prefix and rotation index
    pub fn ri_key(pre: &str, ri: u64) -> String {
        format!("{}.{:032x}", pre, ri)
    }
}

impl<'db> Drop for Keeper<'db> {
    fn drop(&mut self) {
        // This is a no-op, as the LMDBer will be dropped automatically
        // and it has its own Drop implementation
    }
}

/// Trait for key pair storage and cryptographic key management
pub trait KeeperTrait: Send + Sync {
    /// Check if database is opened
    fn opened(&self) -> bool;

    /// Get name of database
    fn name(&self) -> String;

    /// Get database path
    fn path(&self) -> Option<PathBuf>;

    /// Check if database is temporary
    fn temp(&self) -> bool;

    /// Create a key to access the pubs database with prefix and rotation index
    fn ri_key(&self, pre: &str, ri: u64) -> String;
}

impl<'db> KeeperTrait for Keeper<'db> {
    fn opened(&self) -> bool {
        self.lmdber.opened()
    }

    fn name(&self) -> String {
        self.lmdber.name()
    }

    fn path(&self) -> Option<PathBuf> {
        self.lmdber.path()
    }

    fn temp(&self) -> bool {
        self.lmdber.temp()
    }

    fn ri_key(&self, pre: &str, ri: u64) -> String {
        Self::ri_key(pre, ri) // Calling the static method
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cesr::Parsable;
    use crate::Matter;

    #[test]
    fn test_keeper_basics() -> Result<(), DBError> {
        // Create a temporary Keeper instance
        let lmdber = LMDBer::builder().name("test_keeper").temp(true).build()?;

        // Create "seen." database
        assert_eq!(lmdber.name(), "test_keeper");
        assert!(lmdber.opened());
        let keeper = Keeper::new(Arc::new(&lmdber))?;
        assert!(keeper.opened());

        // Check it was created properly
        assert_eq!(keeper.name(), "test_keeper");
        assert!(keeper.opened());
        assert!(keeper.temp());

        // Check path exists
        let path = keeper.path().unwrap();
        assert!(path.exists());

        // Test dropping the keeper (should clean up if temp=true)
        drop(keeper);
        drop(lmdber);

        //TODO: Fix lmdber clean up dir on temp=true
        // assert!(!path.exists());

        Ok(())
    }

    #[test]
    fn test_keeper_subdb() -> Result<(), DBError> {
        // Create a temporary Keeper instance
        let mut lmdber = LMDBer::builder()
            .name("test_keeper_subdb")
            .temp(true)
            .build()?;

        // Create "seen." database
        assert_eq!(lmdber.name(), "test_keeper_subdb");
        assert!(lmdber.opened());
        let keeper = Keeper::new(Arc::new(&lmdber))?;

        // Test the gbls database
        let key = "aeid";
        let val = "BDzwEHHzq7K0gzQPYGGwTmuupUhPx5_yZ-Wk1x4ejhcc";

        keeper
            .gbls
            .put(&[key], &val.as_bytes())
            .map_err(|e| DBError::DatabaseError(format!("SuberError: {}", e)))?;

        let result: Option<Vec<u8>> = keeper
            .gbls
            .get(&[key])
            .map_err(|e| DBError::DatabaseError(format!("SuberError: {}", e)))?;
        assert!(result.is_some());

        let val_bytes = result.unwrap();
        let val_str = String::from_utf8(val_bytes).unwrap();
        assert_eq!(val_str, val);

        // Test the pres database
        let pub_key = b"BDzwEHHzq7K0gzQPYGGwTmuupUhPx5_yZ-Wk1x4ejhcc";
        let pre = Prefixer::from_qb64b(&mut pub_key.to_vec(), None).expect("Invalid prefix");

        keeper
            .pres
            .put(&[pub_key], &pre)
            .expect("Failed to put prefix");

        let result = keeper.pres.get(&[pub_key]).expect("Failed to get prefix");
        assert!(result.is_some());

        let retrieved_pre = result.unwrap();
        assert_eq!(retrieved_pre.qb64(), pre.qb64());

        // Test the prms Komer database (would require complete PrePrm implementation)
        // For brevity, we'll just check it exists
        let prms = PrePrm::default();
        let key = "prms.00000000000000000000000000000000";
        let result = keeper.prms.put(&[key], &prms).expect("Failed to put prms");
        assert!(result);

        let result = keeper.prms.get(&[key]).expect("Failed to get prms");
        assert!(result.is_some());
        assert_eq!(result.unwrap().pidx, 0);

        assert!(
            keeper
                .prms
                .cnt_all()
                .expect("there to be some values in the database")
                > 0
        );

        Ok(())
    }

    #[test]
    fn test_keeper_ri_key() {
        let pre = "EBfxc1UdwEMYy_g8Ldekf-aQEzBSkb3A5-vASBrV0qs4";
        let ri = 0;
        assert_eq!(
            Keeper::ri_key(pre, ri),
            "EBfxc1UdwEMYy_g8Ldekf-aQEzBSkb3A5-vASBrV0qs4.00000000000000000000000000000000"
        );

        let ri = 1;
        assert_eq!(
            Keeper::ri_key(pre, ri),
            "EBfxc1UdwEMYy_g8Ldekf-aQEzBSkb3A5-vASBrV0qs4.00000000000000000000000000000001"
        );
    }
}