djinn 0.0.2

Local-first desktop resident composition for the Mere stack: identity, local sessions, personal sync, Knot, credential custody, and endpoint ownership.
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
//! Recording, forgetting and disclosing paired devices.
//!
//! These edit the settings file and compute public key material; none of them
//! open the personal-graph store. That separation is deliberate, and it is why
//! they live apart from [`crate::personal_sync`], which brings the store
//! and the transport up: the store is single-writer and the resident host
//! holds its lock, so a management verb that needed it could not run while the
//! host was up. Everything here works with the host running, which is when you
//! actually want it.

use std::path::{Path, PathBuf};

use personae::{IdentityProvider, ProfileId};

use crate::personal_sync::{DeviceSyncError, personal_graph_id};
use crate::settings::{self as owner_settings, OwnerSettings, SyncSettings};
use graphshell::native::personal_sync_host::PersonalSyncHostError;

/// What a pairing attempt did.
#[derive(Debug, PartialEq, Eq)]
pub enum PairOutcome {
    Added {
        path: PathBuf,
        /// True when no roster root was supplied, so the device will receive
        /// this graph but its writes will be refused.
        receive_only: bool,
    },
    AlreadyPaired,
}

/// What an unpairing attempt did.
#[derive(Debug, PartialEq, Eq)]
pub enum UnpairOutcome {
    Removed { path: PathBuf },
    NotPaired,
}

/// Record a paired device in a profile's settings.
///
/// This and [`unpair_device`] are the only writers of the settings file. The
/// resident host reads it and reconciles its live transport against it but
/// never writes back, so two processes never race to rewrite it.
pub fn pair_device(
    app_dir: &Path,
    profile: &ProfileId,
    node_id: [u8; 32],
    root: Option<[u8; 32]>,
    label: &str,
    at_ms: u64,
) -> Result<PairOutcome, DeviceSyncError> {
    pair_device_with_prekey(app_dir, profile, node_id, root, label, at_ms, None)
}

/// Pair a device, carrying a pre-key it has no way to publish itself.
///
/// The pre-key is only meaningful for a device with no roster root: it authors
/// nothing, so nothing it says reaches the lane, and without a relay it would
/// be reachable and never readable.
pub fn pair_device_with_prekey(
    app_dir: &Path,
    profile: &ProfileId,
    node_id: [u8; 32],
    root: Option<[u8; 32]>,
    label: &str,
    at_ms: u64,
    prekey: Option<String>,
) -> Result<PairOutcome, DeviceSyncError> {
    let path = owner_settings::settings_path(app_dir, profile);
    let mut settings = OwnerSettings::load(&path)?;
    let sync = settings.sync.get_or_insert_with(SyncSettings::default);
    // Refuse to record a peer for a graph that does not exist: the pairing
    // would look accepted and then never join anything.
    if sync.graph.trim().is_empty() {
        return Err(DeviceSyncError::NoGraphConfigured {
            profile: profile.0.clone(),
            path: path.display().to_string(),
        });
    }
    if !sync.pair_with_prekey(node_id, root, label, at_ms, prekey) {
        return Ok(PairOutcome::AlreadyPaired);
    }
    settings.save(&path)?;
    Ok(PairOutcome::Added {
        path,
        receive_only: root.is_none(),
    })
}

/// Forget a paired device.
///
/// The resident host notices the removal and drops the device from the live
/// overlay, so unpairing does not wait for a restart.
pub fn unpair_device(
    app_dir: &Path,
    profile: &ProfileId,
    node_id: [u8; 32],
) -> Result<UnpairOutcome, DeviceSyncError> {
    let path = owner_settings::settings_path(app_dir, profile);
    let mut settings = OwnerSettings::load(&path)?;
    let Some(sync) = settings.sync.as_mut() else {
        return Ok(UnpairOutcome::NotPaired);
    };
    if !sync.unpair(node_id) {
        return Ok(UnpairOutcome::NotPaired);
    }
    settings.save(&path)?;
    Ok(UnpairOutcome::Removed { path })
}

/// What the other device needs in order to pair with this one.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PairingFacts {
    pub graph: [u8; 32],
    /// Reachability. Derived per graph, so it is unlinkable across graphs.
    pub node_id: [u8; 32],
    /// Authority. Common to every graph this profile joins, so it is disclosed
    /// on request rather than logged.
    pub root: [u8; 32],
    /// Readability: this device's group pre-key bundle, hex encoded.
    ///
    /// Here because a receive-only device cannot put it on the lane itself. It
    /// has no roster root, so every operation it authors is refused, including
    /// the one that would announce it. Without somebody carrying this, such a
    /// device could be admitted as a reader and never actually keyed, which
    /// would make read-without-write a promise the design could not keep.
    ///
    /// Safe to paste. The bundle attests back to `root`, so relaying it proves
    /// nothing about the relay and everything about its subject; a forged one
    /// is refused when the lane admits it.
    ///
    /// Empty when this device has no key group session yet.
    pub prekey: String,
}

/// Compute this device's pairing facts without opening the store.
pub fn pairing_facts<P: IdentityProvider + ?Sized>(
    identity: &P,
    app_dir: &Path,
    profile: &ProfileId,
    data_root: Option<&Path>,
) -> Result<Option<PairingFacts>, DeviceSyncError> {
    let stored = OwnerSettings::load(&owner_settings::settings_path(app_dir, profile))?;
    let Some(sync) = stored.sync.filter(|sync| !sync.graph.trim().is_empty()) else {
        return Ok(None);
    };
    let graph = personal_graph_id(&sync.graph);
    let transport_key = identity
        .derive_keypair(&graphshell::personal_sync::personal_graph_identity_salt(
            graph,
        ))
        .map_err(|error| {
            DeviceSyncError::Host(PersonalSyncHostError::Transport(error.to_string()))
        })?;
    // Read from the session this device already has rather than minting one:
    // creating a second session would orphan the first, and this device's seat
    // is whichever one the lane has heard of.
    let prekey = match data_root {
        Some(root) => {
            let key_root = key_group_root(root, &sync, graph);
            match graphshell::native::graph_keys::GraphKeyGroup::open(identity, graph, &key_root) {
                Ok(opened) => hex(opened.group.published_bundle()),
                Err(error) => {
                    tracing::warn!(
                        %error,
                        "could not read this device's group pre-key; a device pairing with                          these facts will be reachable but not readable"
                    );
                    String::new()
                }
            }
        }
        None => String::new(),
    };
    Ok(Some(PairingFacts {
        graph,
        node_id: transport_key.public_key().to_bytes(),
        root: identity.master_public_key().to_bytes(),
        prekey,
    }))
}

/// Where the key group's sealed session lives, which is beside the graph
/// store and named for the same graph.
fn key_group_root(
    data_root: &Path,
    sync: &owner_settings::SyncSettings,
    graph: [u8; 32],
) -> std::path::PathBuf {
    sync.store_path
        .clone()
        .unwrap_or_else(|| {
            data_root
                .join("personal-sync")
                .join(format!("{}.redb", owner_settings::hex32(&graph)))
        })
        .with_extension("keys")
}

fn hex(bytes: &[u8]) -> String {
    bytes.iter().map(|byte| format!("{byte:02x}")).collect()
}

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

    fn profile() -> ProfileId {
        ProfileId("default".into())
    }

    fn configured(directory: &Path) -> PathBuf {
        let path = owner_settings::settings_path(directory, &profile());
        OwnerSettings {
            sync: Some(SyncSettings {
                graph: "personal".into(),
                ..SyncSettings::default()
            }),
            knot: None,
            content: Default::default(),
        }
        .save(&path)
        .unwrap();
        path
    }

    #[test]
    fn pairing_refuses_when_no_graph_is_configured() {
        let directory = tempfile::tempdir().unwrap();
        let error =
            pair_device(directory.path(), &profile(), [0x31; 32], None, "qpc", 1).unwrap_err();
        assert!(
            matches!(error, DeviceSyncError::NoGraphConfigured { .. }),
            "pairing into a profile with no graph must fail rather than \
             record a peer that could never join anything: {error}"
        );
    }

    #[test]
    fn pairing_writes_once_and_is_idempotent() {
        let directory = tempfile::tempdir().unwrap();
        let path = configured(directory.path());

        assert!(matches!(
            pair_device(
                directory.path(),
                &profile(),
                [0x31; 32],
                Some([0xa1; 32]),
                "qpc",
                100
            )
            .unwrap(),
            PairOutcome::Added { .. }
        ));
        assert_eq!(
            pair_device(
                directory.path(),
                &profile(),
                [0x31; 32],
                Some([0xa1; 32]),
                "qpc",
                200
            )
            .unwrap(),
            PairOutcome::AlreadyPaired
        );

        let reloaded = OwnerSettings::load(&path).unwrap().sync.unwrap();
        assert_eq!(reloaded.paired_devices.len(), 1);
        assert_eq!(reloaded.paired_devices[0].label, "qpc");
        assert_eq!(reloaded.paired_devices[0].added_ms, 100);
        assert_eq!(
            reloaded.graph, "personal",
            "pairing must not disturb the rest of the settings"
        );
    }

    #[test]
    fn unpairing_revokes_write_authority_along_with_reachability() {
        let directory = tempfile::tempdir().unwrap();
        let path = owner_settings::settings_path(directory.path(), &profile());
        OwnerSettings {
            sync: Some(SyncSettings {
                graph: "personal".into(),
                roster_roots: vec![owner_settings::hex32(&[0xc0; 32])],
                ..SyncSettings::default()
            }),
            knot: None,
            content: Default::default(),
        }
        .save(&path)
        .unwrap();
        pair_device(
            directory.path(),
            &profile(),
            [0x51; 32],
            Some([0xd1; 32]),
            "thinkpad",
            1,
        )
        .unwrap();

        let paired = OwnerSettings::load(&path).unwrap().sync.unwrap();
        let mut roster = paired.roster_root_keys().unwrap();
        roster.sort_unstable();
        assert_eq!(
            roster,
            vec![[0xc0; 32], [0xd1; 32]],
            "a paired device's root must join the roster, or its writes are \
             refused while it looks paired"
        );

        unpair_device(directory.path(), &profile(), [0x51; 32]).unwrap();
        let dropped = OwnerSettings::load(&path).unwrap().sync.unwrap();
        assert_eq!(
            dropped.roster_root_keys().unwrap(),
            vec![[0xc0; 32]],
            "unpairing must revoke write authority too: a root left behind is \
             authority the owner believes they removed"
        );
        assert!(
            dropped.paired_node_keys().unwrap().is_empty(),
            "and reachability goes with it"
        );
    }

    #[test]
    fn a_device_paired_without_a_root_is_receive_only_and_says_so() {
        let directory = tempfile::tempdir().unwrap();
        let path = configured(directory.path());

        assert_eq!(
            pair_device(directory.path(), &profile(), [0x61; 32], None, "reader", 1).unwrap(),
            PairOutcome::Added {
                path: path.clone(),
                receive_only: true
            }
        );
        let stored = OwnerSettings::load(&path).unwrap().sync.unwrap();
        assert!(
            stored.roster_root_keys().unwrap().is_empty(),
            "no root was given, so nothing joins the roster"
        );
        assert_eq!(
            stored.receive_only_devices().len(),
            1,
            "and the device is reportable as receive-only rather than looking \
             like a fully paired peer that mysteriously cannot write"
        );
    }

    #[test]
    fn unpairing_twice_and_unpairing_an_unconfigured_profile_are_not_errors() {
        let directory = tempfile::tempdir().unwrap();
        assert_eq!(
            unpair_device(directory.path(), &profile(), [0x43; 32]).unwrap(),
            UnpairOutcome::NotPaired,
            "a profile with no sync section has nothing to forget"
        );
        configured(directory.path());
        pair_device(
            directory.path(),
            &profile(),
            [0x41; 32],
            Some([0xa1; 32]),
            "imac",
            1,
        )
        .unwrap();
        unpair_device(directory.path(), &profile(), [0x41; 32]).unwrap();
        assert_eq!(
            unpair_device(directory.path(), &profile(), [0x41; 32]).unwrap(),
            UnpairOutcome::NotPaired
        );
    }

    #[test]
    fn a_device_can_be_paired_again_after_being_unpaired() {
        let directory = tempfile::tempdir().unwrap();
        let path = configured(directory.path());

        pair_device(
            directory.path(),
            &profile(),
            [0x44; 32],
            Some([0xa4; 32]),
            "imac",
            1,
        )
        .unwrap();
        unpair_device(directory.path(), &profile(), [0x44; 32]).unwrap();
        assert!(
            matches!(
                pair_device(
                    directory.path(),
                    &profile(),
                    [0x44; 32],
                    Some([0xa4; 32]),
                    "imac again",
                    3
                )
                .unwrap(),
                PairOutcome::Added { .. }
            ),
            "unpairing must not leave a tombstone that blocks re-pairing"
        );
        let again = OwnerSettings::load(&path).unwrap().sync.unwrap();
        assert_eq!(again.paired_devices.len(), 1);
        assert_eq!(again.paired_devices[0].added_ms, 3);
    }
}