pnm-cli 0.42.0

CLI Tool for managing a personal Verifiable Trust Agent
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
//! `pnm rooms …` dispatch — thin shim over the shared room commands.
//!
//! The implementations live in `vta_cli_common::commands::rooms`; this maps the
//! parsed subcommand onto them and supplies the one thing the shared layer
//! cannot obtain for itself: the operator's own signing identity.
//!
//! # Why the signer comes from the stored session
//!
//! A room request is signed by the party the presentation was minted **for**,
//! and the VTA mints for the DID the client authenticated as. `VtaClient` does
//! not expose its signing key — correctly; it is held in the session store —
//! so the identity is read from the same place authentication reads it. Taking
//! it from anywhere else would produce a presentation bound to one DID and a
//! signature by another, which the host refuses and which reads as a
//! credential problem rather than a wiring one.

use vta_cli_common::commands::rooms::{self, RoomSigner, RoomTarget};
use vta_cli_common::commands::rooms_custody as custody;
use vta_sdk::client::VtaClient;

use crate::cli::{
    RoomCommands, RoomFileCommands, RoomKeysCommands, RoomOffersCommands, RoomOwnerCommands,
};

/// The operator's DID and key, from the session this CLI authenticates with.
fn signer(keyring_key: &str) -> Result<vta_sdk::session::SessionInfo, Box<dyn std::error::Error>> {
    crate::auth::loaded_session(keyring_key).ok_or_else(|| -> Box<dyn std::error::Error> {
        "not authenticated — run `pnm auth login` first. A room request is signed by the \
         DID your VTA minted the presentation for, so this needs your session."
            .into()
    })
}

pub(crate) async fn run(
    client: &VtaClient,
    keyring_key: &str,
    command: RoomCommands,
) -> Result<(), Box<dyn std::error::Error>> {
    // Sealing and opening locally need no host and no signing identity of their
    // own: the VTA is the only party, and the client is already authenticated to it.
    let command = match command {
        RoomCommands::File {
            command:
                RoomFileCommands::Seal {
                    path,
                    room_id,
                    out,
                    pad,
                },
        } => return rooms::cmd_rooms_file_seal(client, &room_id, &path, &out, pad).await,
        RoomCommands::File {
            command: RoomFileCommands::Open { dir, room_id, out },
        } => return rooms::cmd_rooms_file_open(client, &room_id, &dir, &out).await,
        // The owner's group verbs are calls to this VTA alone: it reaches the host
        // itself, as its principal, so they need no session key of their own.
        RoomCommands::Owner { command } => {
            return match command {
                RoomOwnerCommands::Register {
                    room_id,
                    host_did,
                    owner,
                    visibility,
                    retention_days,
                    retention_policy,
                } => {
                    custody::cmd_rooms_owner_register(
                        client,
                        &room_id,
                        &host_did,
                        &owner,
                        &visibility,
                        retention_days,
                        retention_policy.as_deref(),
                    )
                    .await
                }
                RoomOwnerCommands::Invite {
                    room_id,
                    signing_key,
                    subject,
                    valid_until,
                    valid_for,
                    out,
                } => {
                    let until = custody::validity(valid_until.as_deref(), valid_for.as_deref())?;
                    custody::cmd_rooms_owner_invite(
                        client,
                        &room_id,
                        &signing_key,
                        &subject,
                        until,
                        &out,
                    )
                    .await
                }
                RoomOwnerCommands::IssueMembership {
                    room_id,
                    signing_key,
                    subject,
                    valid_until,
                    valid_for,
                    out,
                    store,
                } => {
                    let until = custody::validity(valid_until.as_deref(), valid_for.as_deref())?;
                    custody::cmd_rooms_owner_issue_membership(
                        client,
                        &room_id,
                        &signing_key,
                        &subject,
                        until,
                        custody::IssuedOutput {
                            out: out.as_deref(),
                            store,
                        },
                    )
                    .await
                }
                RoomOwnerCommands::IssueAuthority {
                    room_id,
                    signing_key,
                    subject,
                    actions,
                    valid_until,
                    valid_for,
                    out,
                    store,
                } => {
                    let until = custody::validity(valid_until.as_deref(), valid_for.as_deref())?;
                    custody::cmd_rooms_owner_issue_authority(
                        client,
                        &room_id,
                        &signing_key,
                        &subject,
                        &actions,
                        until,
                        custody::IssuedOutput {
                            out: out.as_deref(),
                            store,
                        },
                    )
                    .await
                }
                RoomOwnerCommands::GroupCreate {
                    room_id,
                    signing_key,
                    member,
                } => {
                    rooms::cmd_rooms_owner_group_create(
                        client,
                        &room_id,
                        &signing_key,
                        Some(&member),
                    )
                    .await
                }
                RoomOwnerCommands::GroupAdd {
                    room_id,
                    host_did,
                    member,
                    key_package,
                    reason,
                    out,
                } => {
                    rooms::cmd_rooms_owner_group_add(
                        client,
                        &room_id,
                        &host_did,
                        &member,
                        &key_package,
                        reason.as_deref(),
                        &out,
                    )
                    .await
                }
                RoomOwnerCommands::GroupRemove {
                    room_id,
                    host_did,
                    member,
                    reason,
                } => {
                    rooms::cmd_rooms_owner_group_remove(
                        client,
                        &room_id,
                        &host_did,
                        &member,
                        reason.as_deref(),
                    )
                    .await
                }
                RoomOwnerCommands::GroupRenew {
                    room_id,
                    host_did,
                    reason,
                } => {
                    rooms::cmd_rooms_owner_group_renew(
                        client,
                        &room_id,
                        &host_did,
                        reason.as_deref(),
                    )
                    .await
                }
            };
        }
        // A community's offers: this VTA signs each request as the member and this
        // client only carries it, so no session key of its own is used.
        RoomCommands::Offers { command } => {
            return match command {
                RoomOffersCommands::List {
                    vtc,
                    vtc_did,
                    member,
                } => {
                    rooms::cmd_rooms_offers_list(
                        client,
                        rooms::OfferTarget {
                            vtc_url: &vtc,
                            vtc_did: &vtc_did,
                            member: &member,
                        },
                    )
                    .await
                }
                RoomOffersCommands::Accept {
                    room_id,
                    vtc,
                    vtc_did,
                    member,
                    key_package,
                } => {
                    rooms::cmd_rooms_offers_accept(
                        client,
                        rooms::OfferTarget {
                            vtc_url: &vtc,
                            vtc_did: &vtc_did,
                            member: &member,
                        },
                        &room_id,
                        key_package.as_deref(),
                    )
                    .await
                }
            };
        }
        // The key holder's verbs are calls to this VTA alone, all but one: fetching
        // commits from a host signs a request there, so it needs the session below.
        RoomCommands::Keys { command } => match command {
            RoomKeysCommands::KeyPackage {
                room_id,
                invitation,
                out,
            } => {
                return custody::cmd_rooms_keys_key_package(client, &room_id, &invitation, &out)
                    .await;
            }
            RoomKeysCommands::Welcome {
                room_id,
                invitation,
                welcome,
            } => {
                return custody::cmd_rooms_keys_welcome(client, &room_id, &invitation, &welcome)
                    .await;
            }
            RoomKeysCommands::Commit {
                room_id,
                commit: Some(commit),
                epoch,
                ..
            } => {
                let epoch = epoch.ok_or("--commit needs --epoch: the room epoch it produces")?;
                return custody::cmd_rooms_keys_commit_file(client, &room_id, &commit, epoch).await;
            }
            RoomKeysCommands::Backfill { room_id, host_did } => {
                return custody::cmd_rooms_keys_backfill(client, &room_id, &host_did).await;
            }
            RoomKeysCommands::List => return custody::cmd_rooms_keys_list(client).await,
            command @ RoomKeysCommands::Commit { .. } => RoomCommands::Keys { command },
        },
        other => other,
    };

    let session = signer(keyring_key)?;
    let signer = RoomSigner {
        did: &session.client_did,
        key_multibase: &session.private_key_multibase,
    };

    match command {
        RoomCommands::File { command } => match command {
            RoomFileCommands::Put {
                path,
                room_id,
                host,
                host_did,
                title,
                description,
                tags,
                pad,
            } => {
                rooms::cmd_rooms_file_put(
                    client,
                    RoomTarget {
                        host_url: &host,
                        host_did: host_did.as_deref(),
                        room_id: &room_id,
                    },
                    &path,
                    title,
                    description,
                    tags,
                    pad,
                )
                .await
            }
            RoomFileCommands::Get {
                key,
                room_id,
                host,
                host_did,
                out,
            } => {
                rooms::cmd_rooms_file_get(
                    client,
                    RoomTarget {
                        host_url: &host,
                        host_did: host_did.as_deref(),
                        room_id: &room_id,
                    },
                    &key,
                    out.as_deref(),
                )
                .await
            }
            RoomFileCommands::Seal { .. } | RoomFileCommands::Open { .. } => {
                unreachable!("local file commands are dispatched before the signer is read")
            }
        },
        RoomCommands::List {
            room_id,
            host,
            host_did,
            prefix,
            since_version,
            limit,
        } => {
            rooms::cmd_rooms_list(
                client,
                RoomTarget {
                    host_url: &host,
                    host_did: host_did.as_deref(),
                    room_id: &room_id,
                },
                prefix.as_deref(),
                since_version,
                limit,
            )
            .await
        }
        RoomCommands::Get {
            key,
            room_id,
            host,
            host_did,
        } => {
            rooms::cmd_rooms_get(
                client,
                RoomTarget {
                    host_url: &host,
                    host_did: host_did.as_deref(),
                    room_id: &room_id,
                },
                &key,
            )
            .await
        }
        RoomCommands::Put {
            key,
            body,
            room_id,
            host,
            host_did,
            title,
            expected_version,
        } => {
            rooms::cmd_rooms_put(
                client,
                RoomTarget {
                    host_url: &host,
                    host_did: host_did.as_deref(),
                    room_id: &room_id,
                },
                &key,
                title,
                body,
                expected_version,
            )
            .await
        }
        RoomCommands::Curate {
            key,
            room_id,
            host,
            host_did,
            status,
            pin,
            unpin,
            reason,
        } => {
            let pinned = rooms::pinned_from_flags(pin, unpin);
            rooms::cmd_rooms_curate(
                client,
                RoomTarget {
                    host_url: &host,
                    host_did: host_did.as_deref(),
                    room_id: &room_id,
                },
                &key,
                status,
                pinned,
                reason,
            )
            .await
        }
        RoomCommands::Renew {
            epoch,
            room_id,
            host,
            host_did,
            reason,
        } => {
            rooms::cmd_rooms_renew(
                client,
                RoomTarget {
                    host_url: &host,
                    host_did: host_did.as_deref(),
                    room_id: &room_id,
                },
                signer,
                epoch,
                reason,
            )
            .await
        }
        RoomCommands::Keys { command } => match command {
            RoomKeysCommands::Commit {
                room_id,
                host: Some(host),
                host_did,
                ..
            } => {
                custody::cmd_rooms_keys_commit_from_host(
                    client,
                    RoomTarget {
                        host_url: &host,
                        host_did: host_did.as_deref(),
                        room_id: &room_id,
                    },
                )
                .await
            }
            RoomKeysCommands::Commit { .. } => {
                Err("pass --host <url> to fetch the commits, or --commit <file> --epoch <n>".into())
            }
            _ => unreachable!("the key holder's local verbs are answered above"),
        },
        RoomCommands::Owner { .. } => {
            unreachable!("the owner's group verbs are answered above, before the session is read")
        }
        RoomCommands::Offers { .. } => {
            unreachable!("the offer verbs are answered above, before the session is read")
        }
        RoomCommands::Create {
            room_id,
            host,
            host_did,
            visibility,
            retention_days,
        } => {
            rooms::cmd_rooms_create(
                RoomTarget {
                    host_url: &host,
                    host_did: host_did.as_deref(),
                    room_id: &room_id,
                },
                signer,
                &visibility,
                retention_days,
            )
            .await
        }
    }
}