weirdnet-sync 0.7.0

Weirdnet sync: hybrid logical clocks, an op log, LWW and OR-set merge, tombstone GC, SyncKey page sealing, a device roster with self-cert, revocation, key rotation with re-admission, user-signed device grants and service devices linked without the seed, and the weirdnet-sync/v1 pull protocol over weirdnet-net.
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
//! This device's place in the user's Weirdnet: the user identity, its own node key and
//! certificate, and the linked-devices roster it shares with the user's other devices (Morph,
//! Ymir, Bragi, …), kept current by sync passes.
//!
//! An [`Identity`] lives in one directory (0700 on Unix), secrets in 0600 files:
//!
//! - `seed`: the user key seed, which a full device link delivers (a service device, linked
//!   with a certificate only, has none and keeps `credential.json`, the user's public key,
//!   instead; see [`Identity::adopt_service`]);
//! - `node.key`: this node's key (libp2p protobuf encoding);
//! - `keys`: the epoch keys ([`EpochKeys::export`]);
//! - `roster.json`: the public roster, in the format Morph links with.
//!
//! The secrets (`seed`, `node.key`, `keys`) go through a [`SecretStore`]: 0600 files in the
//! directory by default ([`FileSecrets`]); apps that keep secrets in the OS keystore pass
//! their own store to the `*_with` constructors. `roster.json` and `credential.json` are
//! public and stay files.
//!
//! Each identity announces one sync type (`ymir.pool`, `bragi.app`, …). [`Identity::tick`] runs
//! a pass of that type against every other active device with [`pull_from_shared`], so devices
//! linked or revoked elsewhere reach this one even when the type carries no records yet: the
//! roster and the key chain ride along with every pass.
//!
//! ```no_run
//! # fn main() -> weirdnet_sync::Result<()> {
//! use weirdnet_sync::identity::Identity;
//! let dir = std::path::Path::new("/tmp/example-identity");
//! let me = match Identity::open(dir, "example.app")? {
//!     Some(me) => me,
//!     None => Identity::create(dir, "my laptop", "example.app")?,
//! };
//! println!("{} on {}", me.user_id(), me.peer_id());
//! # Ok(()) }
//! ```

use std::io::Write;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::sync::{Arc, Mutex, MutexGuard};

use serde::{Deserialize, Serialize};
use weirdnet_net::user::NodeCert;
use weirdnet_net::{Keypair, Multiaddr, Node, PeerId, PublicKey, WEIRDNET};

use crate::net::{LinkBundle, SyncState, pull_from_shared};
use crate::roster::{DeviceClass, DeviceGrant, Roster};
use crate::rotation::{Device, EpochKeys};
use crate::{NodeId, OpLog, Result, SyncError};

/// What a link transfer carries besides the identity: the roster, in Morph's format, so any two
/// Weirdnet apps can link each other.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[non_exhaustive]
pub struct LinkSettings {
    #[serde(default)]
    pub roster: Roster,
}

/// The node type sync passes run on.
pub type SyncNode = Node<Vec<u8>, Vec<u8>>;

/// What one [`Identity::tick`] did.
#[derive(Debug, Default)]
#[non_exhaustive]
pub struct Tick {
    /// Devices a pass finished against.
    pub reached: usize,
    /// New log entries stored, over all devices.
    pub applied: usize,
    /// Devices a pass did not finish against (peer id) and why.
    pub failed: Vec<(String, SyncError)>,
}

/// Where an [`Identity`] keeps its secrets (`seed`, `node.key`, `keys`): bytes by name.
/// Implement it over the OS keystore to keep them out of files.
pub trait SecretStore: Send + Sync {
    /// The secret, or `None` when it is not stored.
    fn read(&self, name: &str) -> Result<Option<zeroize::Zeroizing<Vec<u8>>>>;
    /// Store (replace) the secret.
    fn write(&self, name: &str, bytes: &[u8]) -> Result<()>;
    /// Forget the secret (no error when it is not stored).
    fn remove(&self, name: &str) -> Result<()>;
}

/// The default [`SecretStore`]: one 0600 file per secret in the identity's directory.
#[derive(Debug, Clone)]
pub struct FileSecrets {
    dir: PathBuf,
}

impl FileSecrets {
    pub fn new(dir: &Path) -> Self {
        Self { dir: dir.to_path_buf() }
    }
}

impl SecretStore for FileSecrets {
    fn read(&self, name: &str) -> Result<Option<zeroize::Zeroizing<Vec<u8>>>> {
        let path = self.dir.join(name);
        match std::fs::read(&path) {
            Ok(b) => Ok(Some(zeroize::Zeroizing::new(b))),
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(None),
            Err(e) => Err(io("reading", &path, e)),
        }
    }

    fn write(&self, name: &str, bytes: &[u8]) -> Result<()> {
        ensure_dir(&self.dir)?;
        write_private(&self.dir.join(name), bytes)
    }

    fn remove(&self, name: &str) -> Result<()> {
        let path = self.dir.join(name);
        match std::fs::remove_file(&path) {
            Ok(()) => Ok(()),
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(()),
            Err(e) => Err(io("removing", &path, e)),
        }
    }
}

/// `credential.json` on a service device: the user's public key (protobuf, hex), which a
/// full device derives from its seed.
#[derive(Serialize, Deserialize)]
struct Credential {
    user_key: String,
}

const CREDENTIAL: &str = "credential.json";

/// The user identity, this node's key and certificate, and the roster, stored in a directory.
pub struct Identity {
    dir: PathBuf,
    node_key: Keypair,
    sync_type: String,
    secrets: Arc<dyn SecretStore>,
    state: Arc<Mutex<SyncState>>,
}

impl std::fmt::Debug for Identity {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        // Never the seed or keys.
        f.debug_struct("Identity").field("dir", &self.dir).field("peer_id", &self.peer_id()).field("sync_type", &self.sync_type).finish()
    }
}

fn now_ms() -> i64 {
    std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_millis() as i64).unwrap_or(0)
}

fn err(msg: impl Into<String>) -> SyncError {
    SyncError::Identity(msg.into())
}

fn io(what: &str, path: &Path, e: std::io::Error) -> SyncError {
    SyncError::Io(format!("{what} {}: {e}", path.display()))
}

/// The user key from its 32-byte seed.
fn user_from_seed(seed: &[u8]) -> Result<Keypair> {
    let mut bytes: [u8; 32] = seed.try_into().map_err(|_| err("the identity seed is not 32 bytes"))?;
    Keypair::ed25519_from_bytes(&mut bytes).map_err(|e| err(format!("identity seed: {e}")))
}

fn write_private(path: &Path, bytes: &[u8]) -> Result<()> {
    let tmp = path.with_extension("tmp");
    {
        let mut o = std::fs::OpenOptions::new();
        o.write(true).create(true).truncate(true);
        #[cfg(unix)]
        {
            use std::os::unix::fs::OpenOptionsExt;
            o.mode(0o600);
        }
        let mut f = o.open(&tmp).map_err(|e| io("writing", &tmp, e))?;
        f.write_all(bytes).and_then(|()| f.sync_all()).map_err(|e| io("writing", &tmp, e))?;
    }
    std::fs::rename(&tmp, path).map_err(|e| io("writing", path, e))
}

fn ensure_dir(dir: &Path) -> Result<()> {
    std::fs::create_dir_all(dir).map_err(|e| io("creating", dir, e))?;
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        std::fs::set_permissions(dir, std::fs::Permissions::from_mode(0o700)).map_err(|e| io("securing", dir, e))?;
    }
    Ok(())
}

impl Identity {
    /// Whether `dir` holds an identity (with its secrets in files).
    pub fn exists(dir: &Path) -> bool {
        Self::exists_with(dir, &FileSecrets::new(dir))
    }

    /// Whether `dir` holds an identity whose secrets are in `secrets`.
    pub fn exists_with(dir: &Path, secrets: &dyn SecretStore) -> bool {
        let has = |n: &str| secrets.read(n).ok().flatten().is_some();
        (has("seed") || dir.join(CREDENTIAL).is_file()) && has("node.key")
    }

    /// A new Weirdnet user identity with this device as its first node, announcing `sync_type`.
    /// To join the user's existing devices, link from one of them and [`Identity::adopt`].
    /// Never overwrites an identity.
    pub fn create(dir: &Path, label: &str, sync_type: impl Into<String>) -> Result<Self> {
        Self::create_with(dir, Arc::new(FileSecrets::new(dir)), label, sync_type)
    }

    /// [`Identity::create`] with the secrets in `secrets`.
    pub fn create_with(dir: &Path, secrets: Arc<dyn SecretStore>, label: &str, sync_type: impl Into<String>) -> Result<Self> {
        if Self::exists_with(dir, secrets.as_ref()) {
            return Err(err(format!("{} already holds an identity", dir.display())));
        }
        let mut seed = zeroize::Zeroizing::new([0u8; 32]);
        getrandom::fill(seed.as_mut()).map_err(|e| err(format!("random seed: {e}")))?;
        let user = user_from_seed(seed.as_ref())?;
        let node_key = Keypair::generate_ed25519();
        let cert = Roster::self_certify(&WEIRDNET, &user, &node_key.public().to_peer_id().to_string(), label, now_ms())
            .map_err(|e| err(format!("certifying this node: {e}")))?;
        let mut roster = Roster::new();
        roster.upsert(cert.clone());
        let me = Self::build(dir, secrets, Some(seed.to_vec()), user.public(), node_key, cert, roster, EpochKeys::default(), sync_type.into())?;
        me.save()?;
        Ok(me)
    }

    /// The identity a device link delivered: `seed`, the node certificate the existing device
    /// made for `node_key`, and its [`LinkSettings`] (JSON; empty or unreadable means none).
    pub fn adopt(dir: &Path, node_key: Keypair, seed: &[u8], cert: NodeCert, settings: &str, sync_type: impl Into<String>) -> Result<Self> {
        Self::adopt_with(dir, Arc::new(FileSecrets::new(dir)), node_key, seed, cert, settings, sync_type)
    }

    /// [`Identity::adopt`] with the secrets in `secrets`.
    pub fn adopt_with(
        dir: &Path,
        secrets: Arc<dyn SecretStore>,
        node_key: Keypair,
        seed: &[u8],
        cert: NodeCert,
        settings: &str,
        sync_type: impl Into<String>,
    ) -> Result<Self> {
        if Self::exists_with(dir, secrets.as_ref()) {
            return Err(err(format!("{} already holds an identity", dir.display())));
        }
        let user = user_from_seed(seed)?;
        let peer = node_key.public().to_peer_id().to_string();
        if cert.peer_id != peer || cert.user_id != weirdnet_net::user::user_id(&user.public()) || !WEIRDNET.verify_cert(&user.public(), &cert) {
            return Err(err("the link's certificate is not for this node and identity"));
        }
        let given: LinkSettings = serde_json::from_str(settings).unwrap_or_default();
        let mut roster = Roster::new();
        roster.merge(&given.roster, &user.public(), &WEIRDNET);
        roster.upsert(cert.clone());
        let me = Self::build(dir, secrets, Some(seed.to_vec()), user.public(), node_key, cert, roster, EpochKeys::default(), sync_type.into())?;
        me.save()?;
        Ok(me)
    }

    /// The identity a **service** link delivered (`weirdnet-device-link`'s credential): the
    /// user's public key, the node certificate the linking device made for `node_key`, and
    /// the [`LinkBundle`] (its bytes). No seed: this node verifies, serves and syncs, but
    /// cannot admit, revoke, grant or rotate, and with a roster-only grant holds no data keys.
    pub fn adopt_service(
        dir: &Path,
        node_key: Keypair,
        user_key: &[u8],
        cert: NodeCert,
        bundle: &[u8],
        sync_type: impl Into<String>,
    ) -> Result<Self> {
        Self::adopt_service_with(dir, Arc::new(FileSecrets::new(dir)), node_key, user_key, cert, bundle, sync_type)
    }

    /// [`Identity::adopt_service`] with the secrets in `secrets`.
    pub fn adopt_service_with(
        dir: &Path,
        secrets: Arc<dyn SecretStore>,
        node_key: Keypair,
        user_key: &[u8],
        cert: NodeCert,
        bundle: &[u8],
        sync_type: impl Into<String>,
    ) -> Result<Self> {
        if Self::exists_with(dir, secrets.as_ref()) {
            return Err(err(format!("{} already holds an identity", dir.display())));
        }
        let user_key = PublicKey::try_decode_protobuf(user_key).map_err(|e| err(format!("user key: {e}")))?;
        if cert.user_id != weirdnet_net::user::user_id(&user_key) {
            return Err(err("the link's certificate is not for this identity"));
        }
        let bundle = LinkBundle::from_bytes(bundle)?;
        let sync_type = sync_type.into();
        let st = SyncState::new_service(user_key, cert, &node_key, vec![sync_type.clone()], &bundle)
            .map_err(|e| err(format!("the service link did not check out: {e}")))?;
        ensure_dir(dir)?;
        let me = Self { dir: dir.to_path_buf(), node_key, sync_type, secrets, state: Arc::new(Mutex::new(st)) };
        me.save()?;
        Ok(me)
    }

    /// The identity stored in `dir`, if any, announcing `sync_type`.
    pub fn open(dir: &Path, sync_type: impl Into<String>) -> Result<Option<Self>> {
        Self::open_with(dir, Arc::new(FileSecrets::new(dir)), sync_type)
    }

    /// [`Identity::open`] with the secrets in `secrets`.
    pub fn open_with(dir: &Path, secrets: Arc<dyn SecretStore>, sync_type: impl Into<String>) -> Result<Option<Self>> {
        if !Self::exists_with(dir, secrets.as_ref()) {
            return Ok(None);
        }
        let seed = secrets.read("seed")?.map(|s| s.to_vec());
        let user_key = match &seed {
            Some(s) => user_from_seed(s)?.public(),
            None => {
                let path = dir.join(CREDENTIAL);
                let c: Credential = serde_json::from_slice(&std::fs::read(&path).map_err(|e| io("reading", &path, e))?)
                    .map_err(|e| err(format!("{CREDENTIAL}: {e}")))?;
                let bytes = hex::decode(&c.user_key).map_err(|e| err(format!("{CREDENTIAL}: {e}")))?;
                PublicKey::try_decode_protobuf(&bytes).map_err(|e| err(format!("{CREDENTIAL}: {e}")))?
            }
        };
        let key_bytes = secrets.read("node.key")?.ok_or_else(|| err("no node key"))?;
        let node_key = Keypair::from_protobuf_encoding(&key_bytes).map_err(|e| err(format!("node key: {e}")))?;
        let roster: Roster = match std::fs::read(dir.join("roster.json")) {
            Ok(b) => serde_json::from_slice(&b).map_err(|e| err(format!("roster.json: {e}")))?,
            Err(_) => Roster::new(),
        };
        let keys = secrets.read("keys")?.and_then(|b| EpochKeys::import(&b).ok()).unwrap_or_default();
        let peer = node_key.public().to_peer_id().to_string();
        let cert = roster.get(&peer).cloned().ok_or_else(|| err("the roster has no certificate for this node"))?;
        Self::build(dir, secrets, seed, user_key, node_key, cert, roster, keys, sync_type.into()).map(Some)
    }

    #[allow(clippy::too_many_arguments)]
    fn build(
        dir: &Path,
        secrets: Arc<dyn SecretStore>,
        seed: Option<Vec<u8>>,
        user_key: PublicKey,
        node_key: Keypair,
        our_cert: NodeCert,
        roster: Roster,
        keys: EpochKeys,
        sync_type: String,
    ) -> Result<Self> {
        ensure_dir(dir)?;
        let service = seed.is_none();
        let mut st = SyncState {
            user_id: our_cert.user_id.clone(),
            user_key,
            our_node: NodeId::new(our_cert.peer_id.clone()),
            our_cert,
            seed,
            types: vec![sync_type.clone()],
            roster,
            log: OpLog::new(),
            clock: None,
            device: Device::from_node_key(&node_key).ok(),
            keys,
            auto_first_rotation: !service,
            records: Default::default(),
        };
        st.publish_device_key(now_ms())?;
        st.refresh_keys();
        Ok(Self { dir: dir.to_path_buf(), node_key, sync_type, secrets, state: Arc::new(Mutex::new(st)) })
    }

    fn lock(&self) -> MutexGuard<'_, SyncState> {
        self.state.lock().unwrap_or_else(|e| e.into_inner())
    }

    /// Writes the roster and keys (and, the first time, the seed or credential and the node key).
    pub fn save(&self) -> Result<()> {
        ensure_dir(&self.dir)?;
        let (seed, user_key, roster, keys) = {
            let st = self.lock();
            let roster = serde_json::to_vec_pretty(&st.roster).map_err(|e| err(format!("roster.json: {e}")))?;
            (st.seed.clone().map(zeroize::Zeroizing::new), st.user_key.encode_protobuf(), roster, st.keys.export())
        };
        match &seed {
            Some(seed) if self.secrets.read("seed")?.is_none() => self.secrets.write("seed", seed)?,
            Some(_) => {}
            None if !self.dir.join(CREDENTIAL).is_file() => {
                let c = serde_json::to_vec_pretty(&Credential { user_key: hex::encode(user_key) }).map_err(|e| err(format!("{CREDENTIAL}: {e}")))?;
                write_private(&self.dir.join(CREDENTIAL), &c)?;
            }
            None => {}
        }
        if self.secrets.read("node.key")?.is_none() {
            let key = zeroize::Zeroizing::new(self.node_key.to_protobuf_encoding().map_err(|e| err(format!("node key: {e}")))?);
            self.secrets.write("node.key", &key)?;
        }
        write_private(&self.dir.join("roster.json"), &roster)?;
        self.secrets.write("keys", &keys)
    }

    /// Whether this is a service device (linked without the seed).
    pub fn is_service(&self) -> bool {
        self.lock().is_service()
    }

    /// The directory it is stored in.
    pub fn dir(&self) -> &Path {
        &self.dir
    }

    /// The sync type it announces.
    pub fn sync_type(&self) -> &str {
        &self.sync_type
    }

    pub fn node_key(&self) -> &Keypair {
        &self.node_key
    }

    pub fn peer_id(&self) -> PeerId {
        self.node_key.public().to_peer_id()
    }

    /// The shared sync state, for a [`crate::SyncHandler`] answering peers that pull from here.
    pub fn state(&self) -> Arc<Mutex<SyncState>> {
        self.state.clone()
    }

    pub fn user_key(&self) -> PublicKey {
        self.lock().user_key.clone()
    }

    pub fn user_id(&self) -> String {
        self.lock().user_id.clone()
    }

    pub fn our_cert(&self) -> NodeCert {
        self.lock().our_cert.clone()
    }

    pub fn roster(&self) -> Roster {
        self.lock().roster.clone()
    }

    /// The roster as it is now, for callers that check peers later (an inference server's
    /// roster check, the pool's linked nodes).
    pub fn roster_view(&self) -> Arc<dyn Fn() -> Roster + Send + Sync> {
        let st = self.state.clone();
        Arc::new(move || st.lock().unwrap_or_else(|e| e.into_inner()).roster.clone())
    }

    /// The [`LinkSettings`] payload (JSON) for a device linked from here.
    pub fn link_settings(&self) -> String {
        serde_json::to_string(&LinkSettings { roster: self.roster() }).unwrap_or_else(|_| "{}".into())
    }

    /// The seed, for a full device link sent from here. A service device has none.
    pub fn seed(&self) -> Result<weirdnet_net::Key32> {
        let st = self.lock();
        let seed = st.seed.as_ref().ok_or_else(|| SyncError::NotPermitted("the seed".into()))?;
        weirdnet_net::Key32::from_slice(seed).map_err(|e| err(format!("identity seed: {e}")))
    }

    /// The user keypair (secret), for minting the certificate of a device linked from here
    /// (a service link). A service device has none.
    pub fn user_keypair(&self) -> Result<Keypair> {
        let st = self.lock();
        let seed = st.seed.as_ref().ok_or_else(|| SyncError::NotPermitted("the user key".into()))?;
        user_from_seed(seed)
    }

    /// Grant `peer` a class and roles (and, for a service device, sync types), then save.
    /// Needs the seed.
    pub fn grant(&self, peer: &str, class: DeviceClass, roles: &[String], types: &[String]) -> Result<DeviceGrant> {
        let g = self.lock().grant(peer, class, roles, types, now_ms())?;
        self.save()?;
        Ok(g)
    }

    /// The [`LinkBundle`] bytes for a service link to `peer` (see
    /// [`SyncState::service_bundle`]), then save (the grant, and a first rotation if one was
    /// needed). After the link, [`Identity::approve`] the certificate it returns.
    pub fn service_bundle(&self, peer: &str, roles: &[String], types: &[String]) -> Result<zeroize::Zeroizing<Vec<u8>>> {
        let bytes = self.lock().service_bundle(peer, roles, types, now_ms())?.to_bytes()?;
        self.save()?;
        Ok(bytes)
    }

    /// The user linked `cert`'s device from here: it joins the roster (and the key chain once
    /// its device key is known), and the roster is saved.
    pub fn approve(&self, cert: NodeCert) -> Result<()> {
        self.lock().approve(cert, now_ms())?;
        self.save()
    }

    /// Revokes a device everywhere this roster reaches (and rotates the keys), then saves.
    /// This node's own certificate can't be revoked here.
    pub fn revoke(&self, peer: &str) -> Result<NodeCert> {
        let cert = {
            let mut st = self.lock();
            if peer == st.our_cert.peer_id {
                return Err(err("this is this node's own certificate"));
            }
            let seed = st.seed.as_ref().ok_or_else(|| SyncError::NotPermitted("revoke".into()))?;
            let user = user_from_seed(seed)?;
            st.revoke(&user, peer, now_ms())?
        };
        self.save()?;
        Ok(cert)
    }

    /// One sync pass of [`Identity::sync_type`] against every other active device in the roster,
    /// then a save. Blocking; call it from a blocking task. `addrs` gives the addresses to dial a
    /// device at. Each pull goes into a snapshot that is merged back
    /// ([`pull_from_shared`]), so a handler on the same state can answer a peer pulling from
    /// here at the same moment. Errors only if saving fails.
    pub fn tick(&self, node: &SyncNode, addrs: &dyn Fn(&NodeCert) -> Vec<Multiaddr>) -> Result<Tick> {
        let me = node.peer_id().to_string();
        let peers: Vec<NodeCert> = self.lock().roster.active().filter(|c| c.peer_id != me).cloned().collect();
        let mut tick = Tick::default();
        for cert in peers {
            let Ok(peer) = PeerId::from_str(&cert.peer_id) else { continue };
            let out = pull_from_shared(&self.state, node, peer, &addrs(&cert), &self.sync_type);
            tick.applied += out.applied;
            match out.error {
                None => tick.reached += 1,
                Some(e) => tick.failed.push((cert.peer_id.clone(), e)),
            }
        }
        self.save()?;
        Ok(tick)
    }
}

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

    const TYPE: &str = "test.app";

    #[test]
    fn create_open_link_and_revoke() {
        let tmp = tempfile::tempdir().unwrap();
        let a_dir = tmp.path().join("a");
        let a = Identity::create(&a_dir, "box-a", TYPE).unwrap();
        assert!(Identity::create(&a_dir, "again", TYPE).is_err(), "never overwrites an identity");
        assert_eq!(a.sync_type(), TYPE);
        assert_eq!(a.state().lock().unwrap().types, [TYPE]);
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            for f in ["seed", "node.key", "keys", "roster.json"] {
                assert_eq!(std::fs::metadata(a_dir.join(f)).unwrap().permissions().mode() & 0o777, 0o600, "{f}");
            }
            assert_eq!(std::fs::metadata(&a_dir).unwrap().permissions().mode() & 0o777, 0o700);
        }
        assert!(!format!("{a:?}").contains("seed:"), "Debug shows no secrets");

        // Link a second device the way a device link does: a cert from the seed holder.
        let b_key = Keypair::generate_ed25519();
        let user = user_from_seed(&a.seed().unwrap().0).unwrap();
        let b_cert = Roster::self_certify(&WEIRDNET, &user, &b_key.public().to_peer_id().to_string(), "box-b", now_ms()).unwrap();
        a.approve(b_cert.clone()).unwrap();
        let (seed, settings) = (a.seed().unwrap(), a.link_settings());
        let wrong_key = Keypair::generate_ed25519();
        assert!(Identity::adopt(&tmp.path().join("x"), wrong_key, &seed.0, b_cert.clone(), &settings, TYPE).is_err(), "cert for another node");
        let b = Identity::adopt(&tmp.path().join("b"), b_key, &seed.0, b_cert.clone(), &settings, "other.app").unwrap();
        assert_eq!(b.user_id(), a.user_id());
        let a_peer = a.peer_id().to_string();
        let b_peer = b.peer_id().to_string();
        assert_eq!(b.roster().check_caller(&WEIRDNET, &b.user_key(), &a_peer, None), CallerCheck::Linked);
        assert_eq!(a.roster().check_caller(&WEIRDNET, &a.user_key(), &b_peer, Some(&b_cert)), CallerCheck::Linked);

        // A stranger with another identity is not admitted.
        let c = Identity::create(&tmp.path().join("c"), "stranger", TYPE).unwrap();
        assert_eq!(a.roster().check_caller(&WEIRDNET, &a.user_key(), &c.peer_id().to_string(), Some(&c.our_cert())), CallerCheck::WrongUser);

        // Reopening keeps the roster; revoking sticks.
        assert!(Identity::open(&tmp.path().join("none"), TYPE).unwrap().is_none());
        let again = Identity::open(&a_dir, TYPE).unwrap().unwrap();
        assert_eq!(again.peer_id(), a.peer_id());
        again.revoke(&b_peer).unwrap();
        let reopened = Identity::open(&a_dir, TYPE).unwrap().unwrap();
        assert_eq!(reopened.roster().check_caller(&WEIRDNET, &reopened.user_key(), &b_peer, None), CallerCheck::Revoked);
        assert!(reopened.revoke(&a_peer).is_err());
        assert!((reopened.roster_view())().get(&b_peer).is_some_and(|c| !c.active()));
    }

    /// A links B; B links C. One tick on A (against B, over memory nodes) teaches A about C.
    #[test]
    fn tick_brings_devices_linked_elsewhere() {
        use crate::net::{node_config, start_node};
        let tmp = tempfile::tempdir().unwrap();
        let a = Identity::create(&tmp.path().join("a"), "a", TYPE).unwrap();
        let user = user_from_seed(&a.seed().unwrap().0).unwrap();
        let link = |from: &Identity, name: &str| {
            let key = Keypair::generate_ed25519();
            let cert = Roster::self_certify(&WEIRDNET, &user, &key.public().to_peer_id().to_string(), name, now_ms()).unwrap();
            from.approve(cert.clone()).unwrap();
            Identity::adopt(&tmp.path().join(name), key, &from.seed().unwrap().0, cert, &from.link_settings(), TYPE).unwrap()
        };
        let b = link(&a, "b");
        let c = link(&b, "c");
        let c_peer = c.peer_id().to_string();
        assert!(a.roster().get(&c_peer).is_none());

        let node_a = start_node(a.node_key().clone(), node_config(true), a.state()).unwrap();
        let node_b = start_node(b.node_key().clone(), node_config(true), b.state()).unwrap();
        let b_addrs: Vec<Multiaddr> = node_b.reachable_addrs().into_iter().map(|s| s.parse().unwrap()).collect();
        let b_peer = b.peer_id().to_string();
        let tick = a.tick(&node_a, &|cert| if cert.peer_id == b_peer { b_addrs.clone() } else { Vec::new() }).unwrap();
        assert_eq!((tick.reached, tick.failed.len()), (1, 0), "{:?}", tick.failed);
        assert!(a.roster().get(&c_peer).is_some_and(|c| c.active()), "A learned C");
        let saved = Identity::open(a.dir(), TYPE).unwrap().unwrap();
        assert!(saved.roster().get(&c_peer).is_some(), "the tick saved the roster");
    }
}