agntcy-shadi-cli 0.1.9

Command-line interface for SHADI policy, secrets, memory, and SLIM operations.
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
use super::*;

pub(crate) fn run_derive_agent_did_command(parsed: DeriveAgentDidArgs) -> ExitCode {
    match run_derive_agent_did(parsed) {
        Ok(()) => ExitCode::from(0),
        Err(err) => {
            eprintln!("{}", err);
            ExitCode::from(2)
        }
    }
}

pub(crate) fn run_derive_agent_identity_command(parsed: DeriveAgentIdentityArgs) -> ExitCode {
    match run_derive_agent_identity(parsed) {
        Ok(()) => ExitCode::from(0),
        Err(err) => {
            eprintln!("{}", err);
            ExitCode::from(2)
        }
    }
}

pub(crate) fn run_verify_agent_identity_command(parsed: VerifyAgentIdentityArgs) -> ExitCode {
    match run_verify_agent_identity(parsed) {
        Ok(()) => ExitCode::from(0),
        Err(err) => {
            eprintln!("{}", err);
            ExitCode::from(2)
        }
    }
}

pub(crate) fn run_get_secret_command(parsed: GetSecretArgs) -> ExitCode {
    match run_get_secret(parsed) {
        Ok(()) => ExitCode::from(0),
        Err(err) => {
            eprintln!("{}", err);
            ExitCode::from(2)
        }
    }
}

pub(crate) fn run_did_from_github_command(parsed: DidFromGitHubArgs) -> ExitCode {
    match run_did_from_github(parsed) {
        Ok(()) => ExitCode::from(0),
        Err(err) => {
            eprintln!("{}", err);
            ExitCode::from(2)
        }
    }
}

pub(crate) fn run_did_from_ssh_command(parsed: DidFromSshArgs) -> ExitCode {
    match run_did_from_ssh(parsed) {
        Ok(()) => ExitCode::from(0),
        Err(err) => {
            eprintln!("{}", err);
            ExitCode::from(2)
        }
    }
}

pub(crate) fn run_did_from_gpg_command(parsed: DidFromGpgArgs) -> ExitCode {
    match run_did_from_gpg(parsed) {
        Ok(()) => ExitCode::from(0),
        Err(err) => {
            eprintln!("{}", err);
            ExitCode::from(2)
        }
    }
}

pub(crate) fn run_did_from_gpg(args: DidFromGpgArgs) -> Result<(), String> {
    let public_key = read_openpgp_input("--key", args.key_ref.as_deref(), args.input.as_ref())?;

    let pkey = extract_ed25519_public_key(&public_key)?;

    let (did, vm_id, doc) = build_did_document(&pkey)?;
    let output = serde_json::to_string_pretty(&doc).map_err(|err| err.to_string())?;
    std::fs::write(&args.out_file, format!("{}\n", output))
        .map_err(|err| format!("failed to write {}: {}", args.out_file.display(), err))?;

    println!("DID: {}", did);
    println!("Verification Method ID: {}", vm_id);
    println!("Wrote DID Document: {}", args.out_file.display());
    Ok(())
}

pub(crate) fn run_did_from_github(args: DidFromGitHubArgs) -> Result<(), String> {
    let pkey = match args.key_type {
        GitHubKeyType::Gpg => {
            let public_key = fetch_github_gpg_key(&args.user)?;
            extract_ed25519_public_key(&public_key)?
        }
        GitHubKeyType::Ssh => {
            let listing = fetch_github_ssh_keys(&args.user)?;
            let vk = shadi_identity::ssh::first_ed25519_in_authorized_keys(&listing)
                .map_err(|err| err.to_string())?;
            vk.as_bytes().to_vec()
        }
    };

    let (did, vm_id, doc) = build_did_document(&pkey)?;
    let output = serde_json::to_string_pretty(&doc).map_err(|err| err.to_string())?;

    let did_key = format!("github/{}/did", args.user);
    let did_doc_key = format!("github/{}/diddoc", args.user);

    let store = default_secret_store();
    store
        .put(&did_key, did.as_bytes(), SecretPolicy::default())
        .map_err(|err| format!("failed to store secret {}: {}", did_key, err))?;
    store
        .put(&did_doc_key, output.as_bytes(), SecretPolicy::default())
        .map_err(|err| format!("failed to store secret {}: {}", did_doc_key, err))?;

    if let Some(out_file) = args.out_file.as_ref() {
        std::fs::write(out_file, format!("{}\n", output))
            .map_err(|err| format!("failed to write {}: {}", out_file.display(), err))?;
    }

    println!("DID: {}", did);
    println!("Verification Method ID: {}", vm_id);
    println!("Stored DID in secret key: {}", did_key);
    println!("Stored DID Document in secret key: {}", did_doc_key);
    if let Some(out_file) = args.out_file.as_ref() {
        println!("Wrote DID Document: {}", out_file.display());
    }
    Ok(())
}

pub(crate) fn run_get_secret(args: GetSecretArgs) -> Result<(), String> {
    let store = default_secret_store();
    let secret = store
        .get(&args.key)
        .map_err(|_| format!("keychain lookup failed for {}", args.key))?;
    let value = secret.expose(|bytes| bytes.to_vec());
    let value = secret_bytes_to_utf8(&value)?;
    println!("{}", value);
    Ok(())
}

pub(crate) fn run_derive_agent_did(args: DeriveAgentDidArgs) -> Result<(), String> {
    let secret_key = read_openpgp_input("--secret", args.secret.as_deref(), args.input.as_ref())?;
    let (private_key, public_key) = derive_agent_keypair(&secret_key, &args.agent_name)?;
    let (did, vm_id, doc) = build_did_document(&public_key)?;
    let output = serde_json::to_string_pretty(&doc).map_err(|err| err.to_string())?;

    store_derived_agent_identity(
        args.prefix.trim_end_matches('/'),
        &args.agent_name,
        &private_key,
        &public_key,
        &did,
        &output,
        None,
    )?;

    if let Some(out_file) = args.out_file.as_ref() {
        std::fs::write(out_file, format!("{}\n", output))
            .map_err(|err| format!("failed to write {}: {}", out_file.display(), err))?;
    }

    println!("DID: {}", did);
    println!("Verification Method ID: {}", vm_id);
    println!(
        "Stored private key: {}/{}/private",
        args.prefix.trim_end_matches('/'),
        args.agent_name
    );
    println!(
        "Stored public key: {}/{}/public",
        args.prefix.trim_end_matches('/'),
        args.agent_name
    );
    println!(
        "Stored DID: {}/{}/did",
        args.prefix.trim_end_matches('/'),
        args.agent_name
    );
    println!(
        "Stored DID Document: {}/{}/diddoc",
        args.prefix.trim_end_matches('/'),
        args.agent_name
    );
    if let Some(out_file) = args.out_file.as_ref() {
        println!("Wrote DID Document: {}", out_file.display());
    }
    Ok(())
}

pub(crate) fn run_derive_agent_identity(args: DeriveAgentIdentityArgs) -> Result<(), String> {
    let seed_material = match args.source {
        HumanIdentitySource::Gpg => {
            read_openpgp_input("--human-secret", args.human_secret.as_deref(), args.input.as_ref())?
        }
        HumanIdentitySource::Seed => {
            read_seed_input("--human-secret", args.human_secret.as_deref(), args.input.as_ref())?
        }
        HumanIdentitySource::Ssh => read_ssh_seed_input(
            "--human-secret",
            args.human_secret.as_deref(),
            args.input.as_ref(),
            args.ssh_passphrase_secret.as_deref(),
        )?,
    };

    let human_did = match args.human_did_key.as_deref() {
        Some(key) => {
            let store = default_secret_store();
            let secret = store
                .get(key)
                .map_err(|_| format!("keychain lookup failed for {}", key))?;
            Some(secret_bytes_to_utf8(&secret.expose(|bytes| bytes.to_vec()))?)
        }
        None => None,
    };

    let prefix = args.prefix.trim_end_matches('/');
    if let Some(out_dir) = args.out_dir.as_ref() {
        std::fs::create_dir_all(out_dir)
            .map_err(|err| format!("failed to create {}: {}", out_dir.display(), err))?;
    }

    for agent_name in &args.agent_names {
        let (private_key, public_key) = derive_agent_keypair(&seed_material, agent_name)?;
        let (did, vm_id, doc) = build_did_document(&public_key)?;
        let output = serde_json::to_string_pretty(&doc).map_err(|err| err.to_string())?;

        store_derived_agent_identity(
            prefix,
            agent_name,
            &private_key,
            &public_key,
            &did,
            &output,
            human_did.as_deref(),
        )?;

        if let Some(out_dir) = args.out_dir.as_ref() {
            let out_file = out_dir.join(format!("{}.did.json", agent_name));
            std::fs::write(&out_file, format!("{}\n", output))
                .map_err(|err| format!("failed to write {}: {}", out_file.display(), err))?;
            println!("Wrote DID Document: {}", out_file.display());
        }

        println!("Agent: {}", agent_name);
        println!("DID: {}", did);
        println!("Verification Method ID: {}", vm_id);
        println!("Stored private key: {}/{}/private", prefix, agent_name);
        println!("Stored public key: {}/{}/public", prefix, agent_name);
        println!("Stored DID: {}/{}/did", prefix, agent_name);
        println!("Stored DID Document: {}/{}/diddoc", prefix, agent_name);
        if args.human_did_key.is_some() {
            println!("Stored human binding: {}/{}/human_did", prefix, agent_name);
        }
    }

    Ok(())
}

pub(crate) fn run_verify_agent_identity(args: VerifyAgentIdentityArgs) -> Result<(), String> {
    let seed_material = match args.source {
        HumanIdentitySource::Gpg => {
            read_openpgp_input("--human-secret", args.human_secret.as_deref(), args.input.as_ref())?
        }
        HumanIdentitySource::Seed => {
            read_seed_input("--human-secret", args.human_secret.as_deref(), args.input.as_ref())?
        }
        HumanIdentitySource::Ssh => read_ssh_seed_input(
            "--human-secret",
            args.human_secret.as_deref(),
            args.input.as_ref(),
            args.ssh_passphrase_secret.as_deref(),
        )?,
    };

    let (_private_key, expected_public_key) = derive_agent_keypair(&seed_material, &args.agent_name)?;
    let (expected_did, _vm_id, _doc) = build_did_document(&expected_public_key)?;

    let prefix = args.prefix.trim_end_matches('/');
    let public_key_name = args
        .public_key_key
        .clone()
        .unwrap_or_else(|| format!("{}/{}/public", prefix, args.agent_name));
    let did_key_name = args
        .did_key
        .clone()
        .unwrap_or_else(|| format!("{}/{}/did", prefix, args.agent_name));

    let store = default_secret_store();
    let stored_public_b64 = store
        .get(&public_key_name)
        .map_err(|_| format!("keychain lookup failed for {}", public_key_name))?
        .expose(|bytes| bytes.to_vec());
    let stored_public_b64 = secret_bytes_to_utf8(&stored_public_b64)?;
    let stored_public_key = base64::engine::general_purpose::STANDARD
        .decode(stored_public_b64.as_bytes())
        .map_err(|err| format!("failed to decode {}: {}", public_key_name, err))?;

    if stored_public_key != expected_public_key {
        return Err("agent public key mismatch: derived key does not match stored key".to_string());
    }

    let stored_did = store
        .get(&did_key_name)
        .map_err(|_| format!("keychain lookup failed for {}", did_key_name))?
        .expose(|bytes| bytes.to_vec());
    let stored_did = secret_bytes_to_utf8(&stored_did)?;

    if stored_did != expected_did {
        return Err("agent DID mismatch: derived DID does not match stored DID".to_string());
    }

    if args.require_human_binding || args.human_did_key.is_some() {
        let binding_key = format!("{}/{}/human_did", prefix, args.agent_name);
        let bound_human_did = store
            .get(&binding_key)
            .map_err(|_| format!("missing human binding at {}", binding_key))?
            .expose(|bytes| bytes.to_vec());
        let bound_human_did = secret_bytes_to_utf8(&bound_human_did)?;

        if let Some(human_did_key) = args.human_did_key.as_deref() {
            let expected_human_did = store
                .get(human_did_key)
                .map_err(|_| format!("keychain lookup failed for {}", human_did_key))?
                .expose(|bytes| bytes.to_vec());
            let expected_human_did = secret_bytes_to_utf8(&expected_human_did)?;

            if bound_human_did != expected_human_did {
                return Err(
                    "human binding mismatch: agent bound DID does not match expected human DID"
                        .to_string(),
                );
            }
        }
    }

    println!("verified: true");
    println!("agent: {}", args.agent_name);
    println!("stored_public_key: {}", public_key_name);
    println!("stored_did: {}", did_key_name);
    println!("derived_did: {}", expected_did);

    Ok(())
}

pub(crate) fn store_derived_agent_identity(
    prefix: &str,
    agent_name: &str,
    private_key: &[u8],
    public_key: &[u8],
    did: &str,
    diddoc_json: &str,
    human_did: Option<&str>,
) -> Result<(), String> {
    let private_key_name = format!("{}/{}/private", prefix, agent_name);
    let public_key_name = format!("{}/{}/public", prefix, agent_name);
    let did_key_name = format!("{}/{}/did", prefix, agent_name);
    let diddoc_key_name = format!("{}/{}/diddoc", prefix, agent_name);

    let store = default_secret_store();
    let private_b64 = base64::engine::general_purpose::STANDARD.encode(private_key);
    let public_b64 = base64::engine::general_purpose::STANDARD.encode(public_key);

    store
        .put(&private_key_name, private_b64.as_bytes(), SecretPolicy::default())
        .map_err(|err| format!("failed to store secret {}: {}", private_key_name, err))?;
    store
        .put(&public_key_name, public_b64.as_bytes(), SecretPolicy::default())
        .map_err(|err| format!("failed to store secret {}: {}", public_key_name, err))?;
    store
        .put(&did_key_name, did.as_bytes(), SecretPolicy::default())
        .map_err(|err| format!("failed to store secret {}: {}", did_key_name, err))?;
    store
        .put(&diddoc_key_name, diddoc_json.as_bytes(), SecretPolicy::default())
        .map_err(|err| format!("failed to store secret {}: {}", diddoc_key_name, err))?;

    if let Some(human_did) = human_did {
        let binding_key = format!("{}/{}/human_did", prefix, agent_name);
        store
            .put(&binding_key, human_did.as_bytes(), SecretPolicy::default())
            .map_err(|err| format!("failed to store secret {}: {}", binding_key, err))?;
    }

    Ok(())
}

pub(crate) fn read_seed_input(
    label: &str,
    secret_key: Option<&str>,
    input: Option<&PathBuf>,
) -> Result<Vec<u8>, String> {
    if let Some(secret_key) = secret_key {
        let store = default_secret_store();
        let secret = store
            .get(secret_key)
            .map_err(|_| format!("keychain lookup failed for {}", secret_key))?;
        return Ok(secret.expose(|bytes| bytes.to_vec()));
    }

    if let Some(input) = input {
        return std::fs::read(input)
            .map_err(|err| format!("failed to read {}: {}", input.display(), err));
    }

    Err(format!("missing {} or --in", label))
}

pub(crate) fn build_did_document(pkey: &[u8]) -> Result<(String, String, serde_json::Value), String> {
    let pubkey = if pkey.len() == 33 && pkey[0] == 0x40 {
        pkey[1..].to_vec()
    } else if pkey.len() == 32 {
        pkey.to_vec()
    } else {
        return Err(format!("unexpected Ed25519 key material length: {}", pkey.len()));
    };

    let mut multicodec = Vec::with_capacity(2 + pubkey.len());
    multicodec.push(0xED);
    multicodec.push(0x01);
    multicodec.extend_from_slice(&pubkey);
    let fingerprint = format!("z{}", bs58::encode(multicodec).into_string());

    let did = format!("did:key:{}", fingerprint);
    let vm_id = format!("{}#{}", did, fingerprint);

    let doc = json!({
        "@context": [
            "https://www.w3.org/ns/did/v1",
            "https://w3id.org/security/suites/ed25519-2020/v1"
        ],
        "id": did,
        "verificationMethod": [
            {
                "id": vm_id,
                "type": "Ed25519VerificationKey2020",
                "controller": did,
                "publicKeyMultibase": fingerprint
            }
        ],
        "authentication": [vm_id],
        "assertionMethod": [vm_id],
        "capabilityDelegation": [vm_id],
        "capabilityInvocation": [vm_id]
    });

    Ok((did, vm_id, doc))
}

pub(crate) fn run_put_key_command(parsed: PutKeyArgs) -> ExitCode {
    match run_put_key(parsed) {
        Ok(()) => ExitCode::from(0),
        Err(err) => {
            eprintln!("{}", err);
            ExitCode::from(2)
        }
    }
}

pub(crate) fn run_put_key(args: PutKeyArgs) -> Result<(), String> {
    let payload = std::fs::read(&args.input)
        .map_err(|err| format!("failed to read {}: {}", args.input.display(), err))?;
    let store = default_secret_store();
    store
        .put(&args.key, &payload, SecretPolicy::default())
        .map_err(|err| format!("failed to store secret {}: {}", args.key, err))?;
    println!("Stored OpenPGP key in secret: {}", args.key);
    Ok(())
}

pub(crate) fn read_openpgp_input(
    label: &str,
    secret_key: Option<&str>,
    input: Option<&PathBuf>,
) -> Result<Vec<u8>, String> {
    if let Some(secret_key) = secret_key {
        let store = default_secret_store();
        let secret = store
            .get(secret_key)
            .map_err(|_| format!("keychain lookup failed for {}", secret_key))?;
        return Ok(secret.expose(|bytes| bytes.to_vec()));
    }

    if let Some(input) = input {
        return std::fs::read(input)
            .map_err(|err| format!("failed to read {}: {}", input.display(), err));
    }

    Err(format!("missing {} or --in", label))
}

pub(crate) fn fetch_github_gpg_key(user: &str) -> Result<Vec<u8>, String> {
    let payload = github_api_get_gpg_keys(user)?;
    extract_github_public_key(&payload).and_then(decode_github_public_key)
}

pub(crate) fn extract_github_public_key(payload: &str) -> Result<String, String> {
    let value: Value = serde_json::from_str(payload).map_err(|err| err.to_string())?;
    let keys = value
        .as_array()
        .ok_or_else(|| "unexpected GitHub response format".to_string())?;
    let first = keys
        .first()
        .ok_or_else(|| "no GPG keys found for GitHub user".to_string())?;
    let public_key = first
        .get("public_key")
        .and_then(|value| value.as_str())
        .ok_or_else(|| "missing public_key in GitHub response".to_string())?;
    Ok(public_key.to_string())
}

pub(crate) fn decode_github_public_key(public_key: String) -> Result<Vec<u8>, String> {
    if public_key.contains("BEGIN PGP PUBLIC KEY BLOCK") {
        return Ok(public_key.into_bytes());
    }

    let compact = public_key.lines().map(str::trim).collect::<Vec<_>>().join("");
    if compact.is_empty() {
        return Err("GitHub public_key is empty".to_string());
    }

    base64::engine::general_purpose::STANDARD
        .decode(compact.as_bytes())
        .map_err(|err| format!("failed to decode GitHub public_key: {}", err))
}

#[cfg(test)]
static TEST_GITHUB_PAYLOAD: OnceLock<Mutex<Option<String>>> = OnceLock::new();

#[cfg(test)]
fn test_github_payload_slot() -> &'static Mutex<Option<String>> {
    TEST_GITHUB_PAYLOAD.get_or_init(|| Mutex::new(None))
}

#[cfg(test)]
pub(crate) fn set_test_github_payload(payload: Option<String>) {
    let mut guard = test_github_payload_slot().lock().expect("github payload lock");
    *guard = payload;
}

#[cfg(test)]
fn github_api_get_gpg_keys(_user: &str) -> Result<String, String> {
    let guard = test_github_payload_slot().lock().expect("github payload lock");
    guard
        .clone()
        .ok_or_else(|| "test github payload not set".to_string())
}

/// One GET against GitHub. `token` is `Some` only for the REST API;
/// `github.com/<u>.keys` is public.
#[cfg(not(test))]
fn github_get_text(url: &str, token: Option<String>) -> Result<String, String> {
    let mut headers = HeaderMap::new();
    headers.insert(USER_AGENT, HeaderValue::from_static("shadi-shadictl"));
    if let Some(token) = token {
        headers.insert(ACCEPT, HeaderValue::from_static("application/vnd.github+json"));
        headers.insert(
            AUTHORIZATION,
            HeaderValue::from_str(&format!("Bearer {}", token))
                .map_err(|_| "invalid GitHub token".to_string())?,
        );
    }

    let response = Client::builder()
        .default_headers(headers)
        .build()
        .map_err(|err| format!("failed to build HTTP client: {}", err))?
        .get(url)
        .send()
        .map_err(|err| format!("GitHub request failed: {}", err))?;

    if !response.status().is_success() {
        let status = response.status();
        let body = response.text().unwrap_or_default();
        return Err(format!("GitHub error {} for {}: {}", status, url, body));
    }

    response
        .text()
        .map_err(|err| format!("failed to read GitHub response: {}", err))
}

#[cfg(not(test))]
fn github_api_get_gpg_keys(user: &str) -> Result<String, String> {
    let token = std::env::var("GH_TOKEN")
        .or_else(|_| std::env::var("GITHUB_TOKEN"))
        .map_err(|_| "GH_TOKEN or GITHUB_TOKEN must be set for GitHub API".to_string())?;
    github_get_text(
        &format!("https://api.github.com/users/{}/gpg_keys", user),
        Some(token),
    )
}

pub(crate) fn derive_agent_keypair(secret_key: &[u8], agent_name: &str) -> Result<(Vec<u8>, Vec<u8>), String> {
    // Canonical HKDF agent derivation lives in shadi_identity; delegate so there
    // is a single did:key derivation code path across the workspace.
    let id = shadi_identity::AgentIdentity::derive(secret_key, agent_name)
        .map_err(|err| err.to_string())?;
    Ok((
        id.signing_key_bytes().to_vec(),
        id.verifying_key_bytes().to_vec(),
    ))
}

pub(crate) fn extract_ed25519_public_key(openpgp_bytes: &[u8]) -> Result<Vec<u8>, String> {
    use openpgp::crypto::mpi::PublicKey as MpiPublicKey;
    use openpgp::crypto::Curve;
    use openpgp::parse::Parse;
    use openpgp::policy::StandardPolicy;

    let cert = openpgp::Cert::from_reader(openpgp_bytes)
        .map_err(|err| format!("failed to parse OpenPGP certificate: {}", err))?;
    let policy = &StandardPolicy::new();

    for key in cert
        .keys()
        .with_policy(policy, None)
        .supported()
        .alive()
        .revoked(false)
    {
        match key.key().mpis() {
            MpiPublicKey::Ed25519 { a } => return Ok(a.to_vec()),
            MpiPublicKey::EdDSA { curve, q } if *curve == Curve::Ed25519 => {
                return Ok(q.value().to_vec());
            }
            _ => {}
        }
    }

    Err("no Ed25519 public key found in OpenPGP certificate".to_string())
}

/// Never a CLI argument: that would be visible via `ps`.
pub(crate) fn resolve_ssh_passphrase(secret_ref: Option<&str>) -> Result<Option<String>, String> {
    if let Some(secret_ref) = secret_ref {
        let store = default_secret_store();
        let secret = store
            .get(secret_ref)
            .map_err(|_| format!("keychain lookup failed for {}", secret_ref))?;
        let bytes = secret.expose(|b| b.to_vec());
        return Ok(Some(secret_bytes_to_utf8(&bytes)?));
    }
    match std::env::var("SHADI_SSH_PASSPHRASE") {
        Ok(value) if !value.is_empty() => Ok(Some(value)),
        _ => Ok(None),
    }
}

/// Reduce an OpenSSH private key to the seed agent derivation roots in.
pub(crate) fn read_ssh_seed_input(
    label: &str,
    secret_key: Option<&str>,
    input: Option<&PathBuf>,
    passphrase_secret: Option<&str>,
) -> Result<Vec<u8>, String> {
    let key_bytes = read_seed_input(label, secret_key, input)?;
    let passphrase = resolve_ssh_passphrase(passphrase_secret)?;
    let seed = shadi_identity::ssh::seed_from_openssh_private_key(&key_bytes, passphrase.as_deref())
        .map_err(|err| err.to_string())?;
    Ok(seed.to_vec())
}

/// Human `did:key` from an SSH key. Public line or private key — detected from
/// the content, so there is no flag to get wrong.
pub(crate) fn run_did_from_ssh(args: DidFromSshArgs) -> Result<(), String> {
    let raw = read_seed_input("--key", args.key_ref.as_deref(), args.input.as_ref())?;
    let text = String::from_utf8_lossy(&raw);

    let vk = if text.contains("OPENSSH PRIVATE KEY") {
        let passphrase = resolve_ssh_passphrase(args.passphrase_secret.as_deref())?;
        shadi_identity::ssh::verifying_key_from_openssh_private_key(&raw, passphrase.as_deref())
            .map_err(|err| err.to_string())?
    } else {
        shadi_identity::ssh::first_ed25519_in_authorized_keys(&text)
            .map_err(|err| err.to_string())?
    };

    let (did, vm_id, doc) = build_did_document(vk.as_bytes())?;
    let output = serde_json::to_string_pretty(&doc).map_err(|err| err.to_string())?;
    std::fs::write(&args.out_file, format!("{}\n", output))
        .map_err(|err| format!("failed to write {}: {}", args.out_file.display(), err))?;

    println!("Wrote DID Document: {}", args.out_file.display());
    println!("DID: {}", did);
    println!("Verification Method ID: {}", vm_id);
    Ok(())
}

/// `github.com/<user>.keys`. Unauthenticated, unlike `/users/<u>/gpg_keys`.
#[cfg(not(test))]
fn fetch_github_ssh_keys(user: &str) -> Result<String, String> {
    github_get_text(&format!("https://github.com/{}.keys", user), None)
}

#[cfg(test)]
fn fetch_github_ssh_keys(_user: &str) -> Result<String, String> {
    let guard = test_github_payload_slot().lock().expect("github payload lock");
    guard
        .clone()
        .ok_or_else(|| "test github payload not set".to_string())
}