1use std::io::{self, Write};
2
3use ratatui::{
4 layout::Constraint,
5 style::{Color, Modifier, Style},
6 widgets::{Block, Cell, Row, Table},
7};
8use vta_sdk::client::{ContextResponse, CreateDidWebvhRequest, UpdateContextRequest};
9use vta_sdk::context_provision::{ContextProvisionBundle, ProvisionedDid};
10use vta_sdk::prelude::*;
11use vta_sdk::sealed_transfer::SealedPayloadV1;
12
13use crate::render::{is_full_display, print_full_entry, print_full_list_title, print_widget};
14use crate::sealed_producer::{SealedRecipient, seal_for_recipient};
15
16pub struct ProvisionDidOptions {
17 pub server_id: Option<String>,
18 pub did_url: Option<String>,
19 pub portable: bool,
20 pub add_mediator_service: bool,
21 pub pre_rotation_count: u32,
22}
23
24pub async fn cmd_context_bootstrap(
25 client: &VtaClient,
26 id: &str,
27 name: &str,
28 description: Option<String>,
29 admin_label: Option<String>,
30 recipient: SealedRecipient,
31) -> Result<(), Box<dyn std::error::Error>> {
32 let mut ctx_req = CreateContextRequest::new(id, name);
33 if let Some(desc) = description {
34 ctx_req = ctx_req.description(desc);
35 }
36 let ctx = client.create_context(ctx_req).await?;
37 println!("Context created:");
38 println!(" ID: {}", ctx.id);
39 println!(" Name: {}", ctx.name);
40 println!(" Base Path: {}", ctx.base_path);
41
42 let config = client.get_config().await?;
44 let vta_did = config
45 .community_vta_did
46 .clone()
47 .ok_or("VTA DID not configured — cannot mint admin credential")?;
48 let vta_url = config.public_url.clone();
49
50 let (admin_bundle, admin_did) = crate::local_keygen::generate_admin_did_key(vta_did, vta_url);
53 let mut acl_req =
54 vta_sdk::client::CreateAclRequest::new(&admin_did, "admin").contexts(vec![id.to_string()]);
55 if let Some(l) = admin_label {
56 acl_req = acl_req.label(l);
57 }
58 client.create_acl(acl_req).await?;
59
60 let sealed = seal_for_recipient(
61 &recipient,
62 &SealedPayloadV1::AdminCredential(Box::new(admin_bundle)),
63 )
64 .await?;
65 println!();
66 println!("Admin credential created:");
67 println!(" DID: {admin_did}");
68 println!(" Role: admin");
69 if let Some(ref label) = recipient.label {
70 println!(" Recipient: {label}");
71 }
72 println!();
73
74 crate::sealed_producer::emit_sealed_output(&sealed, None)?;
75 Ok(())
76}
77
78pub fn render_context_list(contexts: &[ContextResponse]) {
85 if contexts.is_empty() {
86 println!("No contexts found.");
87 return;
88 }
89
90 if is_full_display() {
91 print_full_list_title("Contexts", contexts.len());
92 for ctx in contexts {
93 let did = ctx.did.as_deref().unwrap_or("—");
94 let created = ctx
95 .created_at
96 .with_timezone(&chrono::Local)
97 .format("%Y-%m-%d %H:%M:%S %:z")
98 .to_string();
99 print_full_entry(&[
100 ("ID", &ctx.id),
101 ("Name", &ctx.name),
102 ("DID", did),
103 ("Base Path", &ctx.base_path),
104 ("Created", &created),
105 ]);
106 }
107 return;
108 }
109
110 let header_style = Style::default()
111 .fg(Color::White)
112 .add_modifier(Modifier::BOLD);
113 let header = Row::new(vec!["ID", "Name", "DID", "Base Path", "Created"])
114 .style(header_style)
115 .bottom_margin(1);
116
117 let rows: Vec<Row> = contexts
118 .iter()
119 .map(|ctx| {
120 let did = ctx.did.clone().unwrap_or_else(|| "\u{2014}".into());
121 let created = ctx
122 .created_at
123 .with_timezone(&chrono::Local)
124 .format("%Y-%m-%d")
125 .to_string();
126
127 Row::new(vec![
128 Cell::from(ctx.id.clone()),
129 Cell::from(ctx.name.clone()),
130 Cell::from(did).style(Style::default().fg(Color::DarkGray)),
131 Cell::from(ctx.base_path.clone()),
132 Cell::from(created),
133 ])
134 })
135 .collect();
136
137 let title = format!(" Contexts ({}) ", contexts.len());
138
139 let table = Table::new(
143 rows,
144 [
145 Constraint::Min(16), Constraint::Min(20), Constraint::Min(40), Constraint::Length(16), Constraint::Length(10), ],
151 )
152 .header(header)
153 .column_spacing(2)
154 .block(
155 Block::bordered()
156 .title(title)
157 .border_style(Style::default().fg(Color::DarkGray)),
158 );
159
160 let height = contexts.len() as u16 + 4;
161 print_widget(table, height);
162}
163
164pub fn render_context_record(ctx: &ContextResponse) {
167 println!("ID: {}", ctx.id);
168 println!("Name: {}", ctx.name);
169 println!("DID: {}", ctx.did.as_deref().unwrap_or("(not set)"));
170 println!(
171 "Description: {}",
172 ctx.description.as_deref().unwrap_or("(not set)")
173 );
174 println!("Base Path: {}", ctx.base_path);
175 println!(
176 "Created At: {}",
177 crate::duration::format_local_datetime(ctx.created_at)
178 );
179 println!(
180 "Updated At: {}",
181 crate::duration::format_local_datetime(ctx.updated_at)
182 );
183}
184
185pub async fn cmd_context_list(client: &VtaClient) -> Result<(), Box<dyn std::error::Error>> {
186 let resp = client.list_contexts().await?;
187 if crate::render::is_json_output() {
188 crate::render::print_json(&resp.contexts)?;
189 return Ok(());
190 }
191 render_context_list(&resp.contexts);
192 Ok(())
193}
194
195pub async fn cmd_context_get(
196 client: &VtaClient,
197 id: &str,
198) -> Result<(), Box<dyn std::error::Error>> {
199 let resp = client.get_context(id).await?;
200 render_context_record(&resp);
201 Ok(())
202}
203
204#[derive(Debug, Default, Clone)]
216pub struct AdminAclOptions {
217 pub did: Option<String>,
219 pub label: Option<String>,
221 pub expires_at: Option<u64>,
223 pub expires_duration: Option<String>,
227}
228
229impl AdminAclOptions {
230 fn is_requested(&self) -> bool {
231 self.did.is_some()
232 }
233}
234
235pub async fn cmd_context_create(
236 client: &VtaClient,
237 id: &str,
238 name: &str,
239 description: Option<String>,
240 admin: AdminAclOptions,
241) -> Result<(), Box<dyn std::error::Error>> {
242 use crate::render::{RESET, YELLOW};
243 use vta_sdk::error::VtaError;
244
245 let req = CreateContextRequest {
246 id: id.to_string(),
247 name: name.to_string(),
248 description,
249 };
250 let resp = match client.create_context(req).await {
251 Ok(r) => r,
252 Err(VtaError::Conflict(_)) if admin.is_requested() => {
258 let did = admin.did.as_deref().unwrap_or_default();
259 let bin = crate::render::bin_name();
260 eprintln!(
261 "{YELLOW}\u{26a0}{RESET} Context '{id}' already exists — skipping context creation."
262 );
263 eprintln!();
264 eprintln!(" The --admin-did was NOT added. To grant admin access to an existing");
265 eprintln!(" context, use the ACL command directly:");
266 eprintln!();
267 let mut hint = format!(" {bin} acl create --did {did} --role admin --contexts {id}");
268 if let Some(label) = admin.label.as_deref() {
269 hint.push_str(&format!(" --label '{label}'"));
270 }
271 match (admin.expires_duration.as_deref(), admin.expires_at) {
272 (Some(raw), _) => hint.push_str(&format!(" --expires {raw}")),
276 (None, Some(expires_at)) => {
277 let remaining = expires_at.saturating_sub(crate::duration::now_unix());
278 hint.push_str(&format!(" --expires {remaining}s"));
279 }
280 (None, None) => {}
281 }
282 eprintln!("{hint}");
283 return Ok(());
284 }
285 Err(e) => return Err(e.into()),
286 };
287 println!("Context created:");
288 println!(" ID: {}", resp.id);
289 println!(" Name: {}", resp.name);
290 println!(" Base Path: {}", resp.base_path);
291
292 if admin.is_requested() {
293 let did = admin.did.as_deref().unwrap_or_default();
294 if !did.starts_with("did:") {
295 return Err(format!(
296 "--admin-did must start with `did:` (got {did:?}) — context was created but no ACL entry was added"
297 )
298 .into());
299 }
300 let mut acl_req =
301 vta_sdk::client::CreateAclRequest::new(did, "admin").contexts(vec![id.to_string()]);
302 if let Some(label) = admin.label.as_deref() {
303 acl_req = acl_req.label(label);
304 }
305 if let Some(expires_at) = admin.expires_at {
306 acl_req = acl_req.expires_at(expires_at);
307 }
308 let acl = client.create_acl(acl_req).await?;
309
310 println!();
311 println!("Admin ACL entry created:");
312 println!(" DID: {}", acl.did);
313 println!(" Role: {}", acl.role);
314 println!(" Contexts: {}", acl.allowed_contexts.join(", "));
315 if let Some(ref label) = acl.label {
316 println!(" Label: {label}");
317 }
318 match acl.expires_at {
319 Some(secs) => {
320 println!(
321 " Expires at: {} ({}) — setup ACL",
322 crate::duration::format_local_time(secs),
323 crate::duration::format_remaining(secs),
324 );
325 println!();
326 println!(" The admin should authenticate before expiry. On first successful");
327 println!(" connect PNM rotates to a fresh long-lived did:key and replaces this");
328 println!(" temporary entry with a permanent one.");
329 }
330 None => println!(" Expires at: (permanent)"),
331 }
332 }
333
334 Ok(())
335}
336
337pub async fn cmd_context_update(
338 client: &VtaClient,
339 id: &str,
340 name: Option<String>,
341 did: Option<String>,
342 description: Option<String>,
343) -> Result<(), Box<dyn std::error::Error>> {
344 let req = UpdateContextRequest {
345 name,
346 did,
347 description,
348 };
349 let resp = client.update_context(id, req).await?;
350 println!("Context updated:");
351 render_context_record(&resp);
352 Ok(())
353}
354
355pub async fn cmd_context_update_did(
356 client: &VtaClient,
357 id: &str,
358 did: &str,
359) -> Result<(), Box<dyn std::error::Error>> {
360 let resp = client.update_context_did(id, did).await?;
361 println!("Context DID updated:");
362 println!(" ID: {}", resp.id);
363 println!(
364 " DID: {}",
365 resp.did.as_deref().unwrap_or("(not set)")
366 );
367 println!(
368 " Updated At: {}",
369 crate::duration::format_local_datetime(resp.updated_at)
370 );
371 Ok(())
372}
373
374pub fn render_delete_context_preview(
381 id: &str,
382 preview: &vta_sdk::protocols::context_management::delete::DeleteContextPreviewResultBody,
383) -> bool {
384 let has_resources = !preview.keys.is_empty()
385 || !preview.webvh_dids.is_empty()
386 || !preview.acl_entries_removed.is_empty()
387 || !preview.acl_entries_updated.is_empty();
388
389 if !has_resources {
390 return false;
391 }
392
393 println!(
394 "Deleting context '{}' will remove the following resources:\n",
395 id
396 );
397
398 if !preview.keys.is_empty() {
399 println!(" Keys ({}):", preview.keys.len());
400 for key in &preview.keys {
401 println!(" - {key}");
402 }
403 }
404
405 if !preview.webvh_dids.is_empty() {
406 println!(" WebVH DIDs ({}):", preview.webvh_dids.len());
407 for did in &preview.webvh_dids {
408 println!(" - {did}");
409 }
410 }
411
412 if !preview.acl_entries_removed.is_empty() {
413 println!(
414 " ACL entries removed ({}):",
415 preview.acl_entries_removed.len()
416 );
417 for did in &preview.acl_entries_removed {
418 println!(" - {did}");
419 }
420 }
421
422 if !preview.acl_entries_updated.is_empty() {
423 println!(
424 " ACL entries updated (context removed from access list) ({}):",
425 preview.acl_entries_updated.len()
426 );
427 for did in &preview.acl_entries_updated {
428 println!(" - {did}");
429 }
430 }
431
432 println!();
433 true
434}
435
436pub fn confirm_destructive(prompt: &str) -> Result<bool, Box<dyn std::error::Error>> {
440 print!("{prompt} [y/N] ");
441 io::stdout().flush()?;
442 let mut input = String::new();
443 io::stdin().read_line(&mut input)?;
444 let input = input.trim().to_lowercase();
445 Ok(input == "y" || input == "yes")
446}
447
448pub async fn cmd_context_delete(
449 client: &VtaClient,
450 id: &str,
451 force: bool,
452) -> Result<(), Box<dyn std::error::Error>> {
453 let preview = client.preview_delete_context(id).await?;
455
456 let has_resources = render_delete_context_preview(id, &preview);
457
458 if has_resources && !force && !confirm_destructive("Proceed with deletion?")? {
459 println!("Aborted.");
460 return Ok(());
461 }
462
463 client.delete_context(id, true).await?;
464 println!("Context deleted: {id}");
465 Ok(())
466}
467
468pub async fn cmd_context_provision(
469 client: &VtaClient,
470 id: &str,
471 name: &str,
472 description: Option<String>,
473 admin_label: Option<String>,
474 did_opts: Option<ProvisionDidOptions>,
475 recipient: SealedRecipient,
476) -> Result<(), Box<dyn std::error::Error>> {
477 eprintln!("Creating context '{id}'...");
479 let mut ctx_req = CreateContextRequest::new(id, name);
480 if let Some(desc) = description {
481 ctx_req = ctx_req.description(desc);
482 }
483 client.create_context(ctx_req).await?;
484
485 let config = client.get_config().await?;
487 let vta_did = config
488 .community_vta_did
489 .clone()
490 .ok_or("VTA DID not configured — cannot mint admin credential")?;
491 let vta_url = config.public_url.clone();
492
493 eprintln!("Minting local admin credential and registering ACL...");
498 let (admin_credential, admin_did) =
499 crate::local_keygen::generate_admin_did_key(vta_did, vta_url);
500 let mut acl_req =
501 vta_sdk::client::CreateAclRequest::new(&admin_did, "admin").contexts(vec![id.to_string()]);
502 if let Some(l) = admin_label {
503 acl_req = acl_req.label(l);
504 }
505 client.create_acl(acl_req).await?;
506
507 let provisioned_did = if let Some(opts) = did_opts {
509 eprintln!("Creating WebVH DID...");
510 let req = CreateDidWebvhRequest {
511 context_id: id.to_string(),
512 server_id: opts.server_id,
513 url: opts.did_url,
514 path: None,
515 domain: None,
518 label: Some(id.to_string()),
519 portable: opts.portable,
520 add_mediator_service: opts.add_mediator_service,
521 additional_services: None,
522 pre_rotation_count: opts.pre_rotation_count,
523 did_document: None,
524 did_log: None,
525 set_primary: true,
526 signing_key_id: None,
527 ka_key_id: None,
528 template: None,
529 template_context: None,
530 template_vars: std::collections::HashMap::new(),
531 };
532 let did_result = client.create_did_webvh(req).await?;
533
534 eprintln!("Fetching DID key secrets...");
536 let mut secrets: Vec<SecretEntry> = Vec::new();
537 secrets.push(
539 client
540 .get_key_secret(&did_result.signing_key_id)
541 .await?
542 .into(),
543 );
544 secrets.push(client.get_key_secret(&did_result.ka_key_id).await?.into());
546 for i in 0..did_result.pre_rotation_key_count {
548 let pre_rot_id = format!("{}#pre-rotation-{i}", did_result.did);
549 secrets.push(client.get_key_secret(&pre_rot_id).await?.into());
550 }
551
552 Some(ProvisionedDid {
553 id: did_result.did,
554 did_document: did_result.did_document,
555 log_entry: did_result.log_entry,
556 secrets,
557 })
558 } else {
559 None
560 };
561
562 let bundle = ContextProvisionBundle {
564 context_id: id.to_string(),
565 context_name: name.to_string(),
566 vta_url: config.public_url,
567 vta_did: config.community_vta_did,
568 credential: admin_credential,
569 admin_did,
570 did: provisioned_did,
571 };
572
573 crate::sealed_producer::emit_context_provision_bundle(bundle, &recipient, None).await
575}
576
577async fn credential_from_key(
584 client: &VtaClient,
585 key_id: &str,
586 vta_did: &str,
587 vta_url: Option<&str>,
588) -> Result<(CredentialBundle, String), Box<dyn std::error::Error>> {
589 let secret = client.get_key_secret(key_id).await?;
590 CredentialBundle::from_ed25519_seed_multibase(&secret.private_key_multibase, vta_did, vta_url)
591 .map_err(|e| format!("Cannot decode key secret: {e}").into())
592}
593
594pub async fn cmd_context_reprovision(
595 client: &VtaClient,
596 id: &str,
597 key_id: Option<String>,
598 admin_label: Option<String>,
599 recipient: SealedRecipient,
600) -> Result<(), Box<dyn std::error::Error>> {
601 eprintln!("Fetching context '{id}'...");
603 let ctx = client.get_context(id).await?;
604
605 let config = client.get_config().await?;
607 let vta_did = config
608 .community_vta_did
609 .as_deref()
610 .ok_or("VTA DID not configured")?;
611
612 let (admin_credential, admin_did) = if let Some(ref kid) = key_id {
614 eprintln!("Using key '{kid}'...");
616 credential_from_key(client, kid, vta_did, config.public_url.as_deref()).await?
617 } else {
618 let keys_resp = client.list_keys(0, 10000, Some("active"), Some(id)).await?;
620 let ed25519_keys: Vec<_> = keys_resp
621 .keys
622 .iter()
623 .filter(|k| k.key_type == KeyType::Ed25519)
624 .collect();
625
626 eprintln!();
627 eprintln!("Select an admin credential key for context '{id}':");
628 eprintln!();
629 for (i, key) in ed25519_keys.iter().enumerate() {
630 let label = key
631 .label
632 .as_deref()
633 .map(|l| format!(" ({l})"))
634 .unwrap_or_default();
635 eprintln!(" [{}] {}{}", i + 1, key.key_id, label);
636 }
637 let new_option = ed25519_keys.len() + 1;
638 eprintln!(" [{}] Create a new admin key", new_option);
639 eprintln!();
640 eprint!("Choice [{}]: ", new_option);
641 io::stderr().flush()?;
642
643 let mut input = String::new();
644 io::stdin().read_line(&mut input)?;
645 let input = input.trim();
646
647 let choice: usize = if input.is_empty() {
649 new_option
650 } else {
651 input
652 .parse()
653 .map_err(|_| format!("Invalid choice: {input}"))?
654 };
655
656 if choice == new_option {
657 eprintln!("Creating new admin key...");
659 let key_resp = client
660 .create_key(CreateKeyRequest {
661 key_type: KeyType::Ed25519,
662 derivation_path: None,
663 key_id: None,
664 mnemonic: None,
665 label: admin_label.or_else(|| Some("admin".to_string())),
666 context_id: Some(id.to_string()),
667 })
668 .await?;
669 credential_from_key(
670 client,
671 &key_resp.key_id,
672 vta_did,
673 config.public_url.as_deref(),
674 )
675 .await?
676 } else if choice >= 1 && choice <= ed25519_keys.len() {
677 let selected = &ed25519_keys[choice - 1];
678 eprintln!("Using key '{}'...", selected.key_id);
679 credential_from_key(
680 client,
681 &selected.key_id,
682 vta_did,
683 config.public_url.as_deref(),
684 )
685 .await?
686 } else {
687 return Err(format!("Invalid choice: {choice}").into());
688 }
689 };
690
691 if client.get_acl(&admin_did).await.is_err() {
693 eprintln!("Creating ACL entry for {admin_did}...");
694 client
695 .create_acl(
696 vta_sdk::client::CreateAclRequest::new(&admin_did, "admin")
697 .contexts(vec![id.to_string()]),
698 )
699 .await?;
700 }
701
702 let provisioned_did = if let Some(ref did_id) = ctx.did {
704 eprintln!("Fetching DID material...");
705
706 let log_resp = client.get_did_webvh_log(did_id).await?;
708 let (did_document, log_entry) = if let Some(ref log_str) = log_resp.log {
709 let parsed: serde_json::Value = serde_json::from_str(log_str)
710 .map_err(|e| format!("failed to parse DID log: {e}"))?;
711 let doc = parsed.get("state").cloned();
712 (doc, Some(log_str.clone()))
713 } else {
714 (None, None)
715 };
716
717 let secrets_bundle = client.fetch_did_secrets_bundle(id).await?;
719
720 Some(ProvisionedDid {
721 id: did_id.clone(),
722 did_document,
723 log_entry,
724 secrets: secrets_bundle.secrets,
725 })
726 } else {
727 None
728 };
729
730 let bundle = ContextProvisionBundle {
732 context_id: id.to_string(),
733 context_name: ctx.name.clone(),
734 vta_url: config.public_url,
735 vta_did: config.community_vta_did,
736 credential: admin_credential,
737 admin_did,
738 did: provisioned_did,
739 };
740
741 crate::sealed_producer::emit_context_provision_bundle(bundle, &recipient, None).await
743}