tap-agent 0.7.0

Rust implementation of the Transaction Authorization Protocol (TAP)
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
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
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
//! CLI tool for managing DIDs and keys
//!
//! This module provides command-line utilities for creating and managing
//! Decentralized Identifiers (DIDs) and associated cryptographic keys.
//!
//! This module is only available when the `native` feature is enabled.
#![cfg(feature = "native")]

use crate::did::{
    DIDGenerationOptions, DIDKeyGenerator, GeneratedKey, KeyType, MultiResolver, SyncDIDResolver,
    VerificationMaterial,
};
use crate::error::{Error, Result};
use crate::message::SecurityMode;
use crate::message_packing::{PackOptions, Packable, Unpackable};
use crate::storage::{KeyStorage, StoredKey};
use base64::Engine;
use clap::{Parser, Subcommand};
use std::fs;
use std::path::PathBuf;
use std::sync::Arc;
use tap_msg::didcomm::PlainMessage;

/// TAP Agent CLI Tool for DID and Key Management
#[derive(Parser, Debug)]
#[command(name = "tap-agent-cli")]
#[command(about = "CLI tool for managing DIDs and keys for TAP protocol", long_about = None)]
pub struct Cli {
    #[command(subcommand)]
    pub command: Commands,
}

/// Available CLI commands
#[derive(Subcommand, Debug)]
pub enum Commands {
    /// Generate a new DID
    #[command(name = "generate")]
    Generate {
        /// The DID method to use (key or web)
        #[arg(short, long, default_value = "key")]
        method: String,

        /// The key type to use
        #[arg(short = 't', long, default_value = "ed25519")]
        key_type: String,

        /// Domain for did:web (required if method is 'web')
        #[arg(short, long)]
        domain: Option<String>,

        /// Output file path for the DID document
        #[arg(short, long)]
        output: Option<PathBuf>,

        /// Output file for private key (if not specified, key is shown only in console)
        #[arg(short = 'k', long)]
        key_output: Option<PathBuf>,

        /// Save key to default location (~/.tap/keys.json)
        #[arg(short = 's', long)]
        save: bool,

        /// Set as default key
        #[arg(long)]
        default: bool,

        /// Label for the key (defaults to agent-{n})
        #[arg(short = 'l', long)]
        label: Option<String>,
    },

    /// Lookup and resolve a DID to its DID Document
    #[command(name = "lookup")]
    Lookup {
        /// The DID to resolve
        #[arg(required = true)]
        did: String,

        /// Output file path for the resolved DID document
        #[arg(short, long)]
        output: Option<PathBuf>,
    },

    /// List all stored keys
    #[command(name = "keys", about = "List, view, and manage stored keys")]
    Keys {
        #[command(subcommand)]
        subcommand: Option<KeysCommands>,
    },

    /// Import an existing key into storage
    #[command(name = "import", about = "Import an existing key into storage")]
    Import {
        /// The JSON file containing the key to import
        #[arg(required = true)]
        key_file: PathBuf,

        /// Set as default key
        #[arg(long)]
        default: bool,

        /// Label for the imported key (defaults to agent-{n})
        #[arg(short = 'l', long)]
        label: Option<String>,
    },

    /// Pack a plaintext DIDComm message
    #[command(name = "pack", about = "Pack a plaintext DIDComm message")]
    Pack {
        /// The input file containing the plaintext message
        #[arg(short, long, required = true)]
        input: PathBuf,

        /// The output file for the packed message
        #[arg(short, long)]
        output: Option<PathBuf>,

        /// The DID of the sender (uses default if not specified)
        #[arg(short, long)]
        sender: Option<String>,

        /// The DID of the recipient
        #[arg(short, long)]
        recipient: Option<String>,

        /// The security mode to use (plain, signed, authcrypt, or anoncrypt)
        #[arg(short, long, default_value = "signed")]
        mode: String,
    },

    /// Unpack a signed or encrypted DIDComm message
    #[command(
        name = "unpack",
        about = "Unpack a signed or encrypted DIDComm message"
    )]
    Unpack {
        /// The input file containing the packed message
        #[arg(short, long, required = true)]
        input: PathBuf,

        /// The output file for the unpacked message
        #[arg(short, long)]
        output: Option<PathBuf>,

        /// The DID of the recipient (uses default if not specified)
        #[arg(short, long)]
        recipient: Option<String>,
    },
}

/// Subcommands for key management
#[derive(Subcommand, Debug)]
pub enum KeysCommands {
    /// List all stored keys
    #[command(name = "list")]
    List,

    /// View details of a specific key
    #[command(name = "view")]
    View {
        /// The DID or label of the key to view
        #[arg(required = true)]
        did_or_label: String,
    },

    /// Set a key as the default
    #[command(name = "set-default")]
    SetDefault {
        /// The DID or label of the key to set as default
        #[arg(required = true)]
        did_or_label: String,
    },

    /// Delete a key from storage
    #[command(name = "delete")]
    Delete {
        /// The DID or label of the key to delete
        #[arg(required = true)]
        did_or_label: String,

        /// Force deletion without confirmation
        #[arg(short, long)]
        force: bool,
    },

    /// Update the label of a key
    #[command(name = "relabel")]
    Relabel {
        /// The DID or label of the key to relabel
        #[arg(required = true)]
        did_or_label: String,

        /// The new label for the key
        #[arg(required = true)]
        new_label: String,
    },
}

/// Run the CLI with the given arguments
pub fn run() -> Result<()> {
    let cli = Cli::parse();
    match cli.command {
        Commands::Generate {
            method,
            key_type,
            domain,
            output,
            key_output,
            save,
            default,
            label,
        } => {
            generate_did(GenerateDIDOptions {
                method: &method,
                key_type: &key_type,
                domain: domain.as_deref(),
                output,
                key_output,
                save,
                set_default: default,
                label: label.as_deref(),
            })?;
        }
        Commands::Lookup { did, output } => {
            lookup_did(&did, output)?;
        }
        Commands::Keys { subcommand } => {
            manage_keys(subcommand)?;
        }
        Commands::Import {
            key_file,
            default,
            label,
        } => {
            import_key(&key_file, default, label.as_deref())?;
        }
        Commands::Pack {
            input,
            output,
            sender,
            recipient,
            mode,
        } => {
            pack_message(&input, output, sender, recipient, &mode)?;
        }
        Commands::Unpack {
            input,
            output,
            recipient,
        } => {
            unpack_message(&input, output, recipient)?;
        }
    }

    Ok(())
}

/// Options for DID generation
struct GenerateDIDOptions<'a> {
    method: &'a str,
    key_type: &'a str,
    domain: Option<&'a str>,
    output: Option<PathBuf>,
    key_output: Option<PathBuf>,
    save: bool,
    set_default: bool,
    label: Option<&'a str>,
}

/// Generate a DID of the specified method and key type
fn generate_did(options: GenerateDIDOptions) -> Result<()> {
    // Parse key type
    let key_type = match options.key_type.to_lowercase().as_str() {
        #[cfg(feature = "crypto-ed25519")]
        "ed25519" => KeyType::Ed25519,
        #[cfg(feature = "crypto-p256")]
        "p256" => KeyType::P256,
        #[cfg(feature = "crypto-secp256k1")]
        "secp256k1" => KeyType::Secp256k1,
        _ => {
            eprintln!(
                "Unsupported key type: {}. Using Ed25519 as default.",
                options.key_type
            );
            #[cfg(feature = "crypto-ed25519")]
            {
                KeyType::Ed25519
            }
            #[cfg(not(feature = "crypto-ed25519"))]
            {
                return Err(Error::Cryptography(format!(
                    "Unsupported key type: {}",
                    options.key_type
                )));
            }
        }
    };

    // Create DID generation options
    let did_options = DIDGenerationOptions { key_type };

    // Generate DID using the specified method
    let generator = DIDKeyGenerator::new();
    let generated_key = match options.method.to_lowercase().as_str() {
        "key" => generator.generate_did(did_options)?,
        "web" => {
            // For did:web, domain is required
            let domain = options.domain.ok_or_else(|| {
                crate::error::Error::MissingConfig("Domain is required for did:web".to_string())
            })?;
            generator.generate_web_did(domain, did_options)?
        }
        _ => {
            eprintln!(
                "Unsupported DID method: {}. Using did:key as default.",
                options.method
            );
            generator.generate_did(did_options)?
        }
    };

    // Display DID information
    display_generated_did(&generated_key, options.method, options.domain);

    // Save DID document if output path is specified
    if let Some(output_path) = options.output {
        save_did_document(&generated_key, &output_path)?;
    }

    // Save private key if key output path is specified
    if let Some(key_path) = options.key_output {
        save_private_key(&generated_key, &key_path)?;
    }

    // Save key to default storage if requested
    if options.save {
        save_key_to_storage(&generated_key, options.set_default, options.label)?;
    }

    Ok(())
}

/// Display information about the generated DID
fn display_generated_did(generated_key: &GeneratedKey, method: &str, domain: Option<&str>) {
    println!("\n=== Generated DID ===");
    println!("DID: {}", generated_key.did);
    println!("Key Type: {:?}", generated_key.key_type);

    // For did:web, show where to place the DID document
    if method == "web" {
        if let Some(d) = domain {
            println!("\nTo use this did:web, place the DID document at:");
            println!("https://{}/.well-known/did.json", d);
        }
    }

    // Display the private key
    println!("\n=== Private Key (keep this secure!) ===");
    println!(
        "Private Key (Base64): {}",
        base64::engine::general_purpose::STANDARD.encode(&generated_key.private_key)
    );

    println!("\n=== Public Key ===");
    println!(
        "Public Key (Base64): {}",
        base64::engine::general_purpose::STANDARD.encode(&generated_key.public_key)
    );
}

/// Save DID document to a file
fn save_did_document(generated_key: &GeneratedKey, output_path: &PathBuf) -> Result<()> {
    let did_doc_json = serde_json::to_string_pretty(&generated_key.did_doc)
        .map_err(|e| crate::error::Error::Serialization(e.to_string()))?;

    fs::write(output_path, did_doc_json).map_err(crate::error::Error::Io)?;

    println!("\nDID document saved to: {}", output_path.display());
    Ok(())
}

/// Save private key to a file
fn save_private_key(generated_key: &GeneratedKey, key_path: &PathBuf) -> Result<()> {
    // Create a JSON object with key information
    let key_info = serde_json::json!({
        "did": generated_key.did,
        "keyType": format!("{:?}", generated_key.key_type),
        "privateKey": base64::engine::general_purpose::STANDARD.encode(&generated_key.private_key),
        "publicKey": base64::engine::general_purpose::STANDARD.encode(&generated_key.public_key),
    });

    let key_json = serde_json::to_string_pretty(&key_info)
        .map_err(|e| crate::error::Error::Serialization(e.to_string()))?;

    fs::write(key_path, key_json).map_err(crate::error::Error::Io)?;

    println!("Private key saved to: {}", key_path.display());
    Ok(())
}

/// Save a key to the default storage location
fn save_key_to_storage(
    generated_key: &GeneratedKey,
    set_as_default: bool,
    label: Option<&str>,
) -> Result<()> {
    // Convert GeneratedKey to StoredKey
    let stored_key = if let Some(label) = label {
        KeyStorage::from_generated_key_with_label(generated_key, label)
    } else {
        KeyStorage::from_generated_key(generated_key)
    };

    // Load existing storage or create a new one
    let mut storage = match KeyStorage::load_default() {
        Ok(storage) => storage,
        Err(_) => KeyStorage::new(),
    };

    // Add the key to storage
    storage.add_key(stored_key);

    // If requested to set as default, update the default DID
    if set_as_default {
        storage.default_did = Some(generated_key.did.clone());
    }

    // Save the updated storage
    storage.save_default()?;

    println!("Key saved to default storage (~/.tap/keys.json)");
    if set_as_default {
        println!("Key set as default agent key");
    }

    Ok(())
}

/// Import a key from a file into the key storage
fn import_key(key_file: &PathBuf, set_as_default: bool, label: Option<&str>) -> Result<()> {
    // Read and parse the key file
    let key_json = fs::read_to_string(key_file)
        .map_err(|e| Error::Storage(format!("Failed to read key file: {}", e)))?;

    let key_info: serde_json::Value = serde_json::from_str(&key_json)
        .map_err(|e| Error::Storage(format!("Failed to parse key file: {}", e)))?;

    // Extract key information
    let did = key_info["did"]
        .as_str()
        .ok_or_else(|| Error::Storage("Missing 'did' field in key file".to_string()))?;

    let key_type_str = key_info["keyType"]
        .as_str()
        .ok_or_else(|| Error::Storage("Missing 'keyType' field in key file".to_string()))?;

    let private_key = key_info["privateKey"]
        .as_str()
        .ok_or_else(|| Error::Storage("Missing 'privateKey' field in key file".to_string()))?;

    let public_key = key_info["publicKey"]
        .as_str()
        .ok_or_else(|| Error::Storage("Missing 'publicKey' field in key file".to_string()))?;

    // Parse key type
    let key_type = match key_type_str {
        #[cfg(feature = "crypto-ed25519")]
        "Ed25519" => KeyType::Ed25519,
        #[cfg(feature = "crypto-p256")]
        "P256" => KeyType::P256,
        #[cfg(feature = "crypto-secp256k1")]
        "Secp256k1" => KeyType::Secp256k1,
        _ => {
            return Err(Error::Storage(format!(
                "Unsupported key type: {}",
                key_type_str
            )))
        }
    };

    // Create a StoredKey
    let stored_key = StoredKey {
        did: did.to_string(),
        label: label.unwrap_or("").to_string(),
        key_type,
        private_key: private_key.to_string(),
        public_key: public_key.to_string(),
        metadata: std::collections::HashMap::new(),
    };

    // Load existing storage or create a new one
    let mut storage = match KeyStorage::load_default() {
        Ok(storage) => storage,
        Err(_) => KeyStorage::new(),
    };

    // Add the key to storage
    storage.add_key(stored_key);

    // If requested to set as default, update the default DID
    if set_as_default {
        storage.default_did = Some(did.to_string());
    }

    // Save the updated storage
    storage.save_default()?;

    println!("Key '{}' imported to default storage", did);
    if set_as_default {
        println!("Key set as default agent key");
    }

    Ok(())
}

/// Manage stored keys
fn manage_keys(subcommand: Option<KeysCommands>) -> Result<()> {
    // Load key storage
    let mut storage = match KeyStorage::load_default() {
        Ok(storage) => storage,
        Err(e) => {
            eprintln!("Error loading key storage: {}", e);
            eprintln!("Creating new key storage.");
            KeyStorage::new()
        }
    };

    match subcommand {
        Some(KeysCommands::List) => {
            list_keys(&storage)?;
        }
        Some(KeysCommands::View { did_or_label }) => {
            view_key(&storage, &did_or_label)?;
        }
        Some(KeysCommands::SetDefault { did_or_label }) => {
            set_default_key(&mut storage, &did_or_label)?;
        }
        Some(KeysCommands::Delete {
            did_or_label,
            force,
        }) => {
            delete_key(&mut storage, &did_or_label, force)?;
        }
        Some(KeysCommands::Relabel {
            did_or_label,
            new_label,
        }) => {
            relabel_key(&mut storage, &did_or_label, &new_label)?;
        }
        None => {
            // Default to list if no subcommand is provided
            list_keys(&storage)?;
        }
    }

    Ok(())
}

/// Relabel a key in storage
fn relabel_key(storage: &mut KeyStorage, did_or_label: &str, new_label: &str) -> Result<()> {
    // Try to find by label first, then by DID
    let did = if let Some(key) = storage.find_by_label(did_or_label) {
        key.did.clone()
    } else if storage.keys.contains_key(did_or_label) {
        did_or_label.to_string()
    } else {
        return Err(Error::Storage(format!(
            "Key '{}' not found in storage",
            did_or_label
        )));
    };

    // Update the label
    storage.update_label(&did, new_label)?;

    // Save the updated storage
    storage.save_default()?;

    println!("Key relabeled successfully to '{}'", new_label);

    Ok(())
}

/// List all keys in storage
fn list_keys(storage: &KeyStorage) -> Result<()> {
    // Check if storage is empty
    if storage.keys.is_empty() {
        println!("No keys found in storage.");
        println!("Generate a key with: tap-agent-cli generate --save");
        return Ok(());
    }

    println!("Keys in storage:");
    println!("{:-<60}", "");

    // Get the default DID for marking
    let default_did = storage.default_did.as_deref();

    // Print header
    println!("{:<15} {:<40} {:<10} Default", "Label", "DID", "Key Type");
    println!("{:-<75}", "");

    // Print each key
    for (did, key) in &storage.keys {
        let is_default = if Some(did.as_str()) == default_did {
            "*"
        } else {
            ""
        };
        println!(
            "{:<15} {:<40} {:<10} {}",
            key.label,
            did,
            format!("{:?}", key.key_type),
            is_default
        );
    }

    println!("\nTotal keys: {}", storage.keys.len());

    Ok(())
}

/// View details for a specific key
fn view_key(storage: &KeyStorage, did_or_label: &str) -> Result<()> {
    // Try to find by label first, then by DID
    let key = storage
        .find_by_label(did_or_label)
        .or_else(|| storage.keys.get(did_or_label))
        .ok_or_else(|| Error::Storage(format!("Key '{}' not found in storage", did_or_label)))?;

    // Display key information
    println!("\n=== Key Details ===");
    println!("Label: {}", key.label);
    println!("DID: {}", key.did);
    println!("Key Type: {:?}", key.key_type);
    println!("Public Key (Base64): {}", key.public_key);

    // Check if this is the default key
    if storage.default_did.as_deref() == Some(&key.did) {
        println!("Default: Yes");
    } else {
        println!("Default: No");
    }

    // Print metadata if any
    if !key.metadata.is_empty() {
        println!("\nMetadata:");
        for (k, v) in &key.metadata {
            println!("  {}: {}", k, v);
        }
    }

    Ok(())
}

/// Set a key as the default
fn set_default_key(storage: &mut KeyStorage, did_or_label: &str) -> Result<()> {
    // Try to find by label first, then by DID
    let did = if let Some(key) = storage.find_by_label(did_or_label) {
        key.did.clone()
    } else if storage.keys.contains_key(did_or_label) {
        did_or_label.to_string()
    } else {
        return Err(Error::Storage(format!(
            "Key '{}' not found in storage",
            did_or_label
        )));
    };

    // Set as default
    storage.default_did = Some(did.clone());

    // Save the updated storage
    storage.save_default()?;

    println!("Key '{}' set as default", did);

    Ok(())
}

/// Delete a key from storage
fn delete_key(storage: &mut KeyStorage, did_or_label: &str, force: bool) -> Result<()> {
    // Try to find by label first, then by DID
    let did = if let Some(key) = storage.find_by_label(did_or_label) {
        key.did.clone()
    } else if storage.keys.contains_key(did_or_label) {
        did_or_label.to_string()
    } else {
        return Err(Error::Storage(format!(
            "Key '{}' not found in storage",
            did_or_label
        )));
    };

    // Confirm deletion if not forced
    if !force {
        println!("Are you sure you want to delete key '{}'? (y/N): ", did);
        let mut input = String::new();
        std::io::stdin().read_line(&mut input).map_err(Error::Io)?;

        if !input.trim().eq_ignore_ascii_case("y") {
            println!("Deletion cancelled.");
            return Ok(());
        }
    }

    // Remove the key
    storage.keys.remove(&did);

    // If this was the default key, clear the default
    if storage.default_did.as_deref() == Some(&did) {
        storage.default_did = storage.keys.keys().next().cloned();
    }

    // Save the updated storage
    storage.save_default()?;

    println!("Key '{}' deleted from storage", did);

    Ok(())
}

/// Lookup and resolve a DID to its corresponding DID document
fn lookup_did(did: &str, output: Option<PathBuf>) -> Result<()> {
    println!("Looking up DID: {}", did);

    // Create a resolver
    let resolver = Arc::new(MultiResolver::default());

    // Create a Tokio runtime for async resolution
    let rt = tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .map_err(|e| Error::DIDResolution(format!("Failed to create runtime: {}", e)))?;

    // Resolve the DID
    let did_doc = rt.block_on(async { resolver.resolve(did).await })?;

    // Check if DID Document was found
    match did_doc {
        Some(doc) => {
            println!("\n=== DID Document ===");

            // Pretty print the DID Document details
            println!("DID: {}", doc.id);

            println!("\nVerification Methods:");
            for (i, vm) in doc.verification_method.iter().enumerate() {
                println!("  [{}] ID: {}", i + 1, vm.id);
                println!("      Type: {:?}", vm.type_);
                println!("      Controller: {}", vm.controller);

                match &vm.verification_material {
                    VerificationMaterial::JWK { public_key_jwk } => {
                        println!("      Material: JWK");
                        if let Some(kty) = public_key_jwk.get("kty") {
                            println!("        Key Type: {}", kty);
                        }
                        if let Some(crv) = public_key_jwk.get("crv") {
                            println!("        Curve: {}", crv);
                        }
                    }
                    VerificationMaterial::Base58 { public_key_base58 } => {
                        println!("      Material: Base58");
                        println!("        Key: {}", public_key_base58);
                    }
                    VerificationMaterial::Multibase {
                        public_key_multibase,
                    } => {
                        println!("      Material: Multibase");
                        println!("        Key: {}", public_key_multibase);
                    }
                }
                println!();
            }

            if !doc.authentication.is_empty() {
                println!("Authentication Methods:");
                for auth in &doc.authentication {
                    println!("  {}", auth);
                }
                println!();
            }

            if !doc.key_agreement.is_empty() {
                println!("Key Agreement Methods:");
                for ka in &doc.key_agreement {
                    println!("  {}", ka);
                }
                println!();
            }

            if !doc.service.is_empty() {
                println!("Services:");
                for (i, svc) in doc.service.iter().enumerate() {
                    println!("  [{}] ID: {}", i + 1, svc.id);
                    println!("      Endpoint: {:?}", svc.service_endpoint);
                    println!();
                }
            }

            // Save DID document if output path is specified
            if let Some(output_path) = output {
                let did_doc_json = serde_json::to_string_pretty(&doc)
                    .map_err(|e| Error::Serialization(e.to_string()))?;

                fs::write(&output_path, did_doc_json).map_err(Error::Io)?;
                println!("DID document saved to: {}", output_path.display());
            }

            Ok(())
        }
        None => {
            println!("No DID Document found for: {}", did);
            println!("The DID may not exist or the resolver might not support this DID method.");

            // Extract method to provide better feedback
            let parts: Vec<&str> = did.split(':').collect();
            if parts.len() >= 2 {
                let method = parts[1];
                println!(
                    "DID method '{}' may not be supported by the default resolver.",
                    method
                );
                println!("Currently, only the following methods are supported:");
                println!("  - did:key");
                println!("  - did:web");

                if method == "web" {
                    println!("\nFor did:web, ensure:");
                    println!("  - The domain is correctly formatted");
                    println!("  - The DID document is hosted at the expected location:");
                    println!(
                        "    - https://example.com/.well-known/did.json for did:web:example.com"
                    );
                    println!("    - https://example.com/path/to/resource/did.json for did:web:example.com:path:to:resource");
                }
            }

            Err(Error::DIDResolution(format!("DID not found: {}", did)))
        }
    }
}

/// Pack a plaintext DIDComm message
async fn pack_message_async(
    input_file: &PathBuf,
    output_file: Option<PathBuf>,
    sender_did: Option<String>,
    recipient_did: Option<String>,
    mode: &str,
) -> Result<()> {
    // Read the plaintext message from the input file
    let plaintext = fs::read_to_string(input_file).map_err(Error::Io)?;

    // Parse the plaintext message
    let plain_message: PlainMessage = serde_json::from_str(&plaintext)
        .map_err(|e| Error::Serialization(format!("Failed to parse plaintext message: {}", e)))?;

    // Load keys from storage
    let storage = KeyStorage::load_default()?;

    // Get the sender DID
    let sender = if let Some(did_or_label) = sender_did {
        // Try to find by label first, then by DID
        let did = if let Some(key) = storage.find_by_label(&did_or_label) {
            key.did.clone()
        } else if storage.keys.contains_key(&did_or_label) {
            did_or_label
        } else {
            return Err(Error::Storage(format!(
                "Sender '{}' not found in storage",
                did_or_label
            )));
        };
        Some(did)
    } else if let Some(default_did) = storage.default_did.clone() {
        // Use default DID if available
        Some(default_did)
    } else if let Some(first_key) = storage.keys.keys().next() {
        // Fallback to first key
        Some(first_key.clone())
    } else {
        // No keys available
        return Err(Error::Storage("No keys found in storage".to_string()));
    };

    if let Some(ref sender_did) = sender {
        println!("Using sender DID: {}", sender_did);
    }

    // Create key manager with the loaded keys
    let key_manager_builder =
        crate::agent_key_manager::AgentKeyManagerBuilder::new().load_from_default_storage();
    let key_manager = Arc::new(key_manager_builder.build()?);

    // Determine security mode
    let security_mode = match mode.to_lowercase().as_str() {
        "plain" => SecurityMode::Plain,
        "signed" => SecurityMode::Signed,
        "authcrypt" | "auth" | "encrypted" => SecurityMode::AuthCrypt,
        "anoncrypt" | "anon" => SecurityMode::AnonCrypt,
        _ => {
            eprintln!(
                "Unknown security mode: {}. Using 'signed' as default.",
                mode
            );
            SecurityMode::Signed
        }
    };

    // Get the actual key IDs from the key manager
    let sender_kid = if let Some(ref s) = sender {
        // Try to get the actual key ID from the key manager
        if let Ok(key) = key_manager.get_generated_key(s) {
            // Get the first authentication method ID from the DID document
            key.did_doc.authentication.first().cloned().or_else(|| {
                key.did_doc
                    .verification_method
                    .first()
                    .map(|vm| vm.id.clone())
            })
        } else {
            // Fallback to the DID-based format for did:key
            if let Some(key_part) = s.strip_prefix("did:key:") {
                Some(format!("{}#{}", s, key_part))
            } else {
                Some(format!("{}#keys-1", s))
            }
        }
    } else {
        None
    };

    let recipient_kid = if let Some(did) = recipient_did {
        // For recipients, we don't have their keys, so use the standard format
        if let Some(key_part) = did.strip_prefix("did:key:") {
            Some(format!("{}#{}", did, key_part))
        } else {
            Some(format!("{}#keys-1", did))
        }
    } else {
        None
    };

    // Create pack options
    let pack_options = PackOptions {
        security_mode,
        sender_kid,
        recipient_kid,
    };

    // Pack the message directly using the PlainMessage's Packable implementation
    let packed = plain_message.pack(&*key_manager, pack_options).await?;

    // Write the packed message to the output file or display it
    if let Some(output) = output_file {
        fs::write(&output, &packed).map_err(Error::Io)?;
        println!("Packed message saved to: {}", output.display());
    } else {
        // Try to pretty-print if it's valid JSON
        match serde_json::from_str::<serde_json::Value>(&packed) {
            Ok(json) => println!("{}", serde_json::to_string_pretty(&json).unwrap_or(packed)),
            Err(_) => println!("{}", packed),
        }
    }

    Ok(())
}

/// Pack a plaintext DIDComm message (synchronous wrapper)
fn pack_message(
    input_file: &PathBuf,
    output_file: Option<PathBuf>,
    sender_did: Option<String>,
    recipient_did: Option<String>,
    mode: &str,
) -> Result<()> {
    // Create a tokio runtime to run async function
    let rt = tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .map_err(|e| Error::Runtime(format!("Failed to create runtime: {}", e)))?;

    // Run the async function in the runtime
    rt.block_on(pack_message_async(
        input_file,
        output_file,
        sender_did,
        recipient_did,
        mode,
    ))
}

/// Unpack a signed or encrypted DIDComm message
async fn unpack_message_async(
    input_file: &PathBuf,
    output_file: Option<PathBuf>,
    recipient_did: Option<String>,
) -> Result<()> {
    // Read the packed message from the input file
    let packed = fs::read_to_string(input_file).map_err(Error::Io)?;

    // Load keys from storage
    let storage = KeyStorage::load_default()?;

    // Get the recipient DID
    let recipient = if let Some(did_or_label) = recipient_did {
        // Try to find by label first, then by DID
        if let Some(key) = storage.find_by_label(&did_or_label) {
            key.did.clone()
        } else if storage.keys.contains_key(&did_or_label) {
            did_or_label
        } else {
            return Err(Error::Storage(format!(
                "Recipient '{}' not found in storage",
                did_or_label
            )));
        }
    } else if let Some(default_did) = storage.default_did.clone() {
        // Use default DID if available
        default_did
    } else if let Some(first_key) = storage.keys.keys().next() {
        // Otherwise use first available DID
        first_key.clone()
    } else {
        // No keys found
        return Err(Error::Storage("No keys found in storage".to_string()));
    };

    println!("Using recipient DID: {}", recipient);

    // Create key manager with the loaded keys
    let key_manager_builder =
        crate::agent_key_manager::AgentKeyManagerBuilder::new().load_from_default_storage();
    let key_manager = Arc::new(key_manager_builder.build()?);

    // Get the actual key ID for the recipient
    let expected_recipient_kid = if let Ok(key) = key_manager.get_generated_key(&recipient) {
        // Get the first authentication method ID from the DID document
        key.did_doc.authentication.first().cloned().or_else(|| {
            key.did_doc
                .verification_method
                .first()
                .map(|vm| vm.id.clone())
        })
    } else {
        // Fallback to the DID-based format for did:key
        if let Some(key_part) = recipient.strip_prefix("did:key:") {
            Some(format!("{}#{}", recipient, key_part))
        } else {
            Some(format!("{}#keys-1", recipient))
        }
    };

    // Create unpack options
    use crate::message_packing::UnpackOptions;
    let unpack_options = UnpackOptions {
        expected_security_mode: SecurityMode::Any,
        expected_recipient_kid,
        require_signature: false,
    };

    // Unpack the message using the String's Unpackable implementation
    let unpacked: PlainMessage = String::unpack(&packed, &*key_manager, unpack_options).await?;

    // Convert to pretty JSON
    let unpacked_json = serde_json::to_string_pretty(&unpacked)
        .map_err(|e| Error::Serialization(format!("Failed to format unpacked message: {}", e)))?;

    // Write the unpacked message to the output file or display it
    if let Some(output) = output_file {
        fs::write(&output, &unpacked_json).map_err(Error::Io)?;
        println!("Unpacked message saved to: {}", output.display());
    } else {
        println!("{}", unpacked_json);
    }

    Ok(())
}

/// Unpack a signed or encrypted DIDComm message (synchronous wrapper)
fn unpack_message(
    input_file: &PathBuf,
    output_file: Option<PathBuf>,
    recipient_did: Option<String>,
) -> Result<()> {
    // Create a tokio runtime to run async function
    let rt = tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()
        .map_err(|e| Error::Runtime(format!("Failed to create runtime: {}", e)))?;

    // Run the async function in the runtime
    rt.block_on(unpack_message_async(input_file, output_file, recipient_did))
}