aleph-cli 0.13.2

Minimal CLI for Aleph Cloud, built in Rust.
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
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
use aleph_types::chain::Chain;
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};

const KEYRING_SERVICE: &str = "cloud.aleph.cli";

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum AccountKind {
    Local,
    Ledger, // Phase 2
    /// Password-protected Ethereum keystore V3 file (no keyring).
    /// Serialized as "keystore" in the manifest; displayed as "encrypted".
    Keystore,
}

/// One entry in the accounts manifest.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AccountEntry {
    pub name: String,
    pub chain: Chain,
    pub address: String,
    pub kind: AccountKind,
    /// BIP44 derivation path — only for Ledger accounts (Phase 2).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub derivation_path: Option<String>,
}

/// An address alias — a named bookmark for an address without a private key.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AliasEntry {
    pub name: String,
    pub address: String,
}

impl AccountEntry {
    pub fn kind_display(&self) -> &'static str {
        match self.kind {
            AccountKind::Local => "local",
            AccountKind::Ledger => "ledger",
            AccountKind::Keystore => "encrypted",
        }
    }
}

/// The on-disk accounts manifest (`accounts.toml`).
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct AccountsManifest {
    /// Name of the default account (used when no --account flag is provided).
    pub default: Option<String>,
    #[serde(default)]
    pub accounts: Vec<AccountEntry>,
    #[serde(default)]
    pub aliases: Vec<AliasEntry>,
}

/// Convert a `keyring` crate error into a `StoreError::Keyring` with
/// platform-specific guidance when the keyring backend is unavailable.
fn keyring_error(err: keyring::Error) -> StoreError {
    let msg = err.to_string();

    // Detect "no backend" / unavailable secret service — common on headless Linux
    let is_backend_unavailable = matches!(
        err,
        keyring::Error::NoStorageAccess(_) | keyring::Error::PlatformFailure(_)
    ) || msg.contains("secret service")
        || msg.contains("dbus")
        || msg.contains("DBus")
        || msg.contains("No storage");

    if is_backend_unavailable {
        StoreError::Keyring(format!(
            "OS keyring is not available: {msg}\n\
             \n\
             On Linux, the keyring requires a running Secret Service provider\n\
             (GNOME Keyring or KWallet) with an unlocked session.\n\
             \n\
             On headless servers, you can use --private-key or the ALEPH_PRIVATE_KEY\n\
             environment variable instead."
        ))
    } else {
        StoreError::Keyring(format!("failed to access keyring: {msg}"))
    }
}

/// Manages reading/writing the accounts manifest and keyring credentials.
pub struct AccountStore {
    manifest_path: PathBuf,
}

#[derive(Debug, thiserror::Error)]
pub enum StoreError {
    #[error("account '{0}' already exists")]
    AlreadyExists(String),
    #[error("account '{0}' not found")]
    NotFound(String),
    #[error(
        "invalid account name '{0}': names must be non-empty and contain only alphanumeric characters, hyphens, and underscores"
    )]
    InvalidName(String),
    #[error("{0}")]
    Keyring(String),
    #[error("keystore file for account '{name}' not found at {}", path.display())]
    KeystoreMissing { name: String, path: PathBuf },
    #[error(
        "cannot change account '{name}' from {from} to {to}: the address is derived from the key and differs between chain families (EVM vs SVM), so the label cannot simply be switched. Import a separate account for {to} instead."
    )]
    ChainFamilyMismatch {
        name: String,
        from: Chain,
        to: Chain,
    },
    #[error(transparent)]
    Io(#[from] std::io::Error),
    #[error("failed to parse manifest: {0}")]
    Parse(String),
}

impl AccountStore {
    /// Create a store that reads/writes the manifest at the given path.
    /// Used for testing -- production code should use `AccountStore::open()`.
    #[cfg(test)]
    pub fn with_manifest_path(manifest_path: PathBuf) -> Self {
        Self { manifest_path }
    }

    /// Open the default store at `~/.config/aleph/accounts.toml`.
    pub fn open() -> Result<Self, StoreError> {
        let proj = directories::ProjectDirs::from("", "", "aleph").ok_or_else(|| {
            StoreError::Io(std::io::Error::new(
                std::io::ErrorKind::NotFound,
                "could not determine home directory",
            ))
        })?;
        let config_dir = proj.config_dir();
        std::fs::create_dir_all(config_dir)?;
        Ok(Self {
            manifest_path: config_dir.join("accounts.toml"),
        })
    }

    /// Load the manifest from disk (returns default empty manifest if file doesn't exist).
    pub fn load_manifest(&self) -> Result<AccountsManifest, StoreError> {
        match std::fs::read_to_string(&self.manifest_path) {
            Ok(contents) => toml::from_str(&contents).map_err(|e| StoreError::Parse(e.to_string())),
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(AccountsManifest::default()),
            Err(e) => Err(StoreError::Io(e)),
        }
    }

    /// Save the manifest to disk with owner-only permissions (0600 on Unix).
    fn save_manifest(&self, manifest: &AccountsManifest) -> Result<(), StoreError> {
        let content =
            toml::to_string_pretty(manifest).map_err(|e| StoreError::Parse(e.to_string()))?;
        if let Some(parent) = self.manifest_path.parent() {
            std::fs::create_dir_all(parent)?;
        }
        self.write_restricted(&self.manifest_path, &content)?;
        Ok(())
    }

    /// Write a file with restricted permissions (0600 on Unix, default on other platforms).
    fn write_restricted(&self, path: &Path, content: &str) -> Result<(), std::io::Error> {
        use std::io::Write;

        let mut opts = std::fs::OpenOptions::new();
        opts.write(true).create(true).truncate(true);

        #[cfg(unix)]
        {
            use std::os::unix::fs::OpenOptionsExt;
            opts.mode(0o600);
        }

        let mut file = opts.open(path)?;
        file.write_all(content.as_bytes())?;
        Ok(())
    }

    /// Validate an account name.
    fn validate_name(name: &str) -> Result<(), StoreError> {
        if name.is_empty()
            || !name
                .chars()
                .all(|c| c.is_alphanumeric() || c == '-' || c == '_')
        {
            return Err(StoreError::InvalidName(name.to_string()));
        }
        Ok(())
    }

    /// Verify a name is valid and not already taken by an account or alias.
    ///
    /// Useful as a fast pre-flight before slow operations (e.g. talking to a
    /// Ledger device) so the user isn't asked to plug in their device just to
    /// be told the name was invalid afterwards.
    pub fn check_name_available(&self, name: &str) -> Result<(), StoreError> {
        Self::validate_name(name)?;
        let manifest = self.load_manifest()?;
        if manifest.accounts.iter().any(|a| a.name == name)
            || manifest.aliases.iter().any(|a| a.name == name)
        {
            return Err(StoreError::AlreadyExists(name.to_string()));
        }
        Ok(())
    }

    /// Add a local account (private key stored in keyring).
    ///
    /// The manifest is written first, then the key is stored in the keyring.
    /// If the keyring write fails, the manifest entry is rolled back to prevent
    /// orphaned entries. This ordering ensures that a failed manifest write
    /// (disk full, permissions) never leaves a key orphaned in the keyring
    /// with no way to find it — which would mean permanent key loss for
    /// newly generated keys the user has never seen.
    pub fn add_local_account(
        &self,
        name: &str,
        chain: Chain,
        address: String,
        private_key_hex: &str,
    ) -> Result<(), StoreError> {
        Self::validate_name(name)?;

        let mut manifest = self.load_manifest()?;
        if manifest.accounts.iter().any(|a| a.name == name)
            || manifest.aliases.iter().any(|a| a.name == name)
        {
            return Err(StoreError::AlreadyExists(name.to_string()));
        }

        // 1. Write manifest first — a failure here is safe (no key stored yet)
        manifest.accounts.push(AccountEntry {
            name: name.to_string(),
            chain,
            address,
            kind: AccountKind::Local,
            derivation_path: None,
        });
        if manifest.default.is_none() {
            manifest.default = Some(name.to_string());
        }
        self.save_manifest(&manifest)?;

        // 2. Store key in OS keyring — if this fails, roll back the manifest
        let entry = keyring::Entry::new(KEYRING_SERVICE, name).map_err(keyring_error);
        let keyring_result =
            entry.and_then(|e| e.set_password(private_key_hex).map_err(keyring_error));

        if let Err(keyring_err) = keyring_result {
            // Roll back: remove the entry we just added
            manifest.accounts.retain(|a| a.name != name);
            if manifest.default.as_deref() == Some(name) {
                manifest.default = manifest.accounts.first().map(|a| a.name.clone());
            }
            // Best-effort rollback — if this also fails, we log but return the keyring error
            if let Err(rollback_err) = self.save_manifest(&manifest) {
                eprintln!(
                    "warning: failed to roll back manifest after keyring error: {rollback_err}"
                );
            }
            return Err(keyring_err);
        }

        Ok(())
    }

    /// Add a Ledger account (no private key stored — only address and derivation path).
    pub fn add_ledger_account(
        &self,
        name: &str,
        chain: Chain,
        address: String,
        derivation_path: String,
    ) -> Result<(), StoreError> {
        Self::validate_name(name)?;

        let mut manifest = self.load_manifest()?;
        if manifest.accounts.iter().any(|a| a.name == name)
            || manifest.aliases.iter().any(|a| a.name == name)
        {
            return Err(StoreError::AlreadyExists(name.to_string()));
        }

        manifest.accounts.push(AccountEntry {
            name: name.to_string(),
            chain,
            address,
            kind: AccountKind::Ledger,
            derivation_path: Some(derivation_path),
        });

        if manifest.default.is_none() {
            manifest.default = Some(name.to_string());
        }

        self.save_manifest(&manifest)
    }

    fn keystores_dir(&self) -> PathBuf {
        self.manifest_path
            .parent()
            .map(|p| p.join("keystores"))
            .unwrap_or_else(|| PathBuf::from("keystores"))
    }

    /// Path of the keystore file for an account (derived from its name).
    pub fn keystore_path(&self, name: &str) -> PathBuf {
        self.keystores_dir().join(format!("{name}.json"))
    }

    /// Add a keystore (password-encrypted file) account.
    ///
    /// File first, manifest second — the reverse of `add_local_account`.
    ///
    /// The keyring flow writes the manifest first because an orphaned
    /// keyring secret would be undiscoverable. A keystore file lives at a
    /// deterministic path derived from the name, so an orphaned file is
    /// discoverable and harmless; a manifest entry pointing at a missing
    /// file would instead be a broken account. Writing the file first means
    /// any single failure leaves at worst an orphaned file.
    pub fn add_keystore_account(
        &self,
        name: &str,
        chain: Chain,
        address: String,
        keystore_json: &str,
    ) -> Result<(), StoreError> {
        Self::validate_name(name)?;

        let mut manifest = self.load_manifest()?;
        if manifest.accounts.iter().any(|a| a.name == name)
            || manifest.aliases.iter().any(|a| a.name == name)
        {
            return Err(StoreError::AlreadyExists(name.to_string()));
        }

        let dir = self.keystores_dir();
        std::fs::create_dir_all(&dir)?;
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o700))?;
        }
        self.write_restricted(&self.keystore_path(name), keystore_json)?;

        manifest.accounts.push(AccountEntry {
            name: name.to_string(),
            chain,
            address,
            kind: AccountKind::Keystore,
            derivation_path: None,
        });
        if manifest.default.is_none() {
            manifest.default = Some(name.to_string());
        }
        if let Err(save_err) = self.save_manifest(&manifest) {
            // Best-effort cleanup; an orphaned file would be harmless anyway.
            let _ = std::fs::remove_file(self.keystore_path(name));
            return Err(save_err);
        }

        Ok(())
    }

    /// Read the raw keystore JSON for an account.
    pub fn read_keystore_json(&self, name: &str) -> Result<String, StoreError> {
        let path = self.keystore_path(name);
        match std::fs::read_to_string(&path) {
            Ok(contents) => Ok(contents),
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
                Err(StoreError::KeystoreMissing {
                    name: name.to_string(),
                    path,
                })
            }
            Err(e) => Err(StoreError::Io(e)),
        }
    }

    /// Get the private key for a local account from the keyring.
    pub fn get_private_key(&self, name: &str) -> Result<String, StoreError> {
        let manifest = self.load_manifest()?;
        let entry = manifest
            .accounts
            .iter()
            .find(|a| a.name == name)
            .ok_or_else(|| StoreError::NotFound(name.to_string()))?;

        if entry.kind != AccountKind::Local {
            return Err(StoreError::Keyring(format!(
                "account '{name}' is not a local account"
            )));
        }

        let keyring_entry = keyring::Entry::new(KEYRING_SERVICE, name).map_err(keyring_error)?;
        keyring_entry.get_password().map_err(keyring_error)
    }

    /// Look up an account entry by name.
    pub fn get_account(&self, name: &str) -> Result<AccountEntry, StoreError> {
        let manifest = self.load_manifest()?;
        manifest
            .accounts
            .iter()
            .find(|a| a.name == name)
            .cloned()
            .ok_or_else(|| StoreError::NotFound(name.to_string()))
    }

    /// Get the default account name.
    pub fn default_account_name(&self) -> Result<Option<String>, StoreError> {
        Ok(self.load_manifest()?.default)
    }

    /// Set the default account.
    pub fn set_default(&self, name: &str) -> Result<(), StoreError> {
        let mut manifest = self.load_manifest()?;
        if !manifest.accounts.iter().any(|a| a.name == name) {
            return Err(StoreError::NotFound(name.to_string()));
        }
        manifest.default = Some(name.to_string());
        self.save_manifest(&manifest)
    }

    /// Change the chain of an existing account.
    ///
    /// Only changes within the same signature family are allowed (EVM↔EVM,
    /// SVM↔SVM). The account address is derived from the key and is invariant
    /// within a family, so the stored address stays valid. Crossing families
    /// (e.g. EVM→SOL) would silently point the account at a different address,
    /// so it is rejected with `ChainFamilyMismatch`.
    pub fn set_account_chain(&self, name: &str, chain: Chain) -> Result<(), StoreError> {
        let mut manifest = self.load_manifest()?;
        let entry = manifest
            .accounts
            .iter_mut()
            .find(|a| a.name == name)
            .ok_or_else(|| StoreError::NotFound(name.to_string()))?;

        let same_family =
            (entry.chain.is_evm() && chain.is_evm()) || (entry.chain.is_svm() && chain.is_svm());
        if entry.chain != chain && !same_family {
            return Err(StoreError::ChainFamilyMismatch {
                name: name.to_string(),
                from: entry.chain.clone(),
                to: chain,
            });
        }

        entry.chain = chain;
        self.save_manifest(&manifest)
    }

    /// Rename an account, moving any associated secret material.
    ///
    /// The secret (keyring entry for local accounts, keystore file for
    /// encrypted accounts) is copied to the new name *before* the manifest is
    /// updated, and the old copy is removed only after the manifest save
    /// succeeds. A failure at any step therefore leaves the original account
    /// intact and at worst an orphaned (harmless) copy under the new name.
    pub fn rename_account(&self, old: &str, new: &str) -> Result<(), StoreError> {
        Self::validate_name(new)?;
        if old == new {
            return Ok(());
        }

        let mut manifest = self.load_manifest()?;
        if manifest.accounts.iter().any(|a| a.name == new)
            || manifest.aliases.iter().any(|a| a.name == new)
        {
            return Err(StoreError::AlreadyExists(new.to_string()));
        }
        let idx = manifest
            .accounts
            .iter()
            .position(|a| a.name == old)
            .ok_or_else(|| StoreError::NotFound(old.to_string()))?;
        let kind = manifest.accounts[idx].kind;

        // 1. Stage the secret under the new name (old copy untouched for now).
        match kind {
            AccountKind::Local => {
                let old_entry = keyring::Entry::new(KEYRING_SERVICE, old).map_err(keyring_error)?;
                let secret = old_entry.get_password().map_err(keyring_error)?;
                let new_entry = keyring::Entry::new(KEYRING_SERVICE, new).map_err(keyring_error)?;
                new_entry.set_password(&secret).map_err(keyring_error)?;
            }
            AccountKind::Keystore => {
                let json = self.read_keystore_json(old)?;
                let dir = self.keystores_dir();
                std::fs::create_dir_all(&dir)?;
                self.write_restricted(&self.keystore_path(new), &json)?;
            }
            AccountKind::Ledger => {}
        }

        // 2. Update the manifest. On failure, clean up the staged copy.
        manifest.accounts[idx].name = new.to_string();
        if manifest.default.as_deref() == Some(old) {
            manifest.default = Some(new.to_string());
        }
        if let Err(save_err) = self.save_manifest(&manifest) {
            match kind {
                AccountKind::Local => {
                    if let Ok(e) = keyring::Entry::new(KEYRING_SERVICE, new) {
                        let _ = e.delete_credential();
                    }
                }
                AccountKind::Keystore => {
                    let _ = std::fs::remove_file(self.keystore_path(new));
                }
                AccountKind::Ledger => {}
            }
            return Err(save_err);
        }

        // 3. Manifest committed — remove the old secret (best effort).
        match kind {
            AccountKind::Local => {
                if let Ok(e) = keyring::Entry::new(KEYRING_SERVICE, old) {
                    match e.delete_credential() {
                        Ok(()) | Err(keyring::Error::NoEntry) => {}
                        Err(err) => {
                            eprintln!("warning: failed to remove old keyring entry: {err}");
                        }
                    }
                }
            }
            AccountKind::Keystore => {
                if let Err(e) = std::fs::remove_file(self.keystore_path(old))
                    && e.kind() != std::io::ErrorKind::NotFound
                {
                    eprintln!("warning: failed to remove old keystore file: {e}");
                }
            }
            AccountKind::Ledger => {}
        }

        Ok(())
    }

    /// Delete an account (removes from manifest and keyring).
    ///
    /// The manifest is updated first, then the keyring entry is removed.
    /// If keyring deletion fails, the key is orphaned but harmless (just
    /// wasted storage). The reverse ordering would risk permanent key loss
    /// if the manifest save failed after the keyring entry was deleted.
    pub fn delete_account(&self, name: &str) -> Result<(), StoreError> {
        let mut manifest = self.load_manifest()?;
        let idx = manifest
            .accounts
            .iter()
            .position(|a| a.name == name)
            .ok_or_else(|| StoreError::NotFound(name.to_string()))?;

        let kind = manifest.accounts[idx].kind;

        // 1. Update manifest first — remove the entry
        manifest.accounts.remove(idx);
        if manifest.default.as_deref() == Some(name) {
            manifest.default = manifest.accounts.first().map(|a| a.name.clone());
        }
        self.save_manifest(&manifest)?;

        // 2. Clean up the secret store based on account kind
        match kind {
            AccountKind::Local => {
                let keyring_entry =
                    keyring::Entry::new(KEYRING_SERVICE, name).map_err(keyring_error)?;
                match keyring_entry.delete_credential() {
                    Ok(()) => {}
                    Err(keyring::Error::NoEntry) => {}
                    Err(e) => return Err(keyring_error(e)),
                }
            }
            AccountKind::Keystore => {
                // Non-fatal if already gone — the manifest entry is removed,
                // and an orphaned file is only wasted disk.
                if let Err(e) = std::fs::remove_file(self.keystore_path(name))
                    && e.kind() != std::io::ErrorKind::NotFound
                {
                    eprintln!("warning: failed to remove keystore file: {e}");
                }
            }
            AccountKind::Ledger => {}
        }

        Ok(())
    }

    /// Add an address alias (a named bookmark with no private key).
    pub fn add_alias(&self, name: &str, address: String) -> Result<(), StoreError> {
        Self::validate_name(name)?;

        let mut manifest = self.load_manifest()?;
        if manifest.accounts.iter().any(|a| a.name == name)
            || manifest.aliases.iter().any(|a| a.name == name)
        {
            return Err(StoreError::AlreadyExists(name.to_string()));
        }

        manifest.aliases.push(AliasEntry {
            name: name.to_string(),
            address,
        });
        self.save_manifest(&manifest)
    }

    /// Look up an alias by name.
    pub fn get_alias(&self, name: &str) -> Result<AliasEntry, StoreError> {
        let manifest = self.load_manifest()?;
        manifest
            .aliases
            .iter()
            .find(|a| a.name == name)
            .cloned()
            .ok_or_else(|| StoreError::NotFound(name.to_string()))
    }

    /// Remove an alias by name.
    pub fn remove_alias(&self, name: &str) -> Result<(), StoreError> {
        let mut manifest = self.load_manifest()?;
        let idx = manifest
            .aliases
            .iter()
            .position(|a| a.name == name)
            .ok_or_else(|| StoreError::NotFound(name.to_string()))?;
        manifest.aliases.remove(idx);
        self.save_manifest(&manifest)
    }

    /// Return path to the manifest file (for display in CLI output).
    #[allow(dead_code)]
    pub fn manifest_path(&self) -> &Path {
        &self.manifest_path
    }
}

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

    fn temp_store() -> (tempfile::TempDir, AccountStore) {
        let dir = tempfile::tempdir().unwrap();
        let manifest_path = dir.path().join("accounts.toml");
        let store = AccountStore::with_manifest_path(manifest_path);
        (dir, store)
    }

    #[test]
    fn load_empty_manifest_returns_default() {
        let (_dir, store) = temp_store();
        let manifest = store.load_manifest().unwrap();
        assert!(manifest.default.is_none());
        assert!(manifest.accounts.is_empty());
    }

    #[test]
    fn roundtrip_manifest_serde() {
        let manifest = AccountsManifest {
            default: Some("main".to_string()),
            accounts: vec![
                AccountEntry {
                    name: "main".to_string(),
                    chain: Chain::Ethereum,
                    address: "0xABCD".to_string(),
                    kind: AccountKind::Local,
                    derivation_path: None,
                },
                AccountEntry {
                    name: "hw".to_string(),
                    chain: Chain::Sol,
                    address: "7Hg3".to_string(),
                    kind: AccountKind::Ledger,
                    derivation_path: Some("m/44'/501'/0'/0'".to_string()),
                },
            ],
            aliases: vec![],
        };

        let serialized = toml::to_string_pretty(&manifest).unwrap();
        let deserialized: AccountsManifest = toml::from_str(&serialized).unwrap();

        assert_eq!(deserialized.default.as_deref(), Some("main"));
        assert_eq!(deserialized.accounts.len(), 2);
        assert_eq!(deserialized.accounts[0].chain, Chain::Ethereum);
        assert_eq!(deserialized.accounts[1].kind, AccountKind::Ledger);
        assert_eq!(
            deserialized.accounts[1].derivation_path.as_deref(),
            Some("m/44'/501'/0'/0'")
        );
    }

    #[test]
    fn validate_name_rejects_empty() {
        assert!(AccountStore::validate_name("").is_err());
    }

    #[test]
    fn validate_name_rejects_spaces() {
        assert!(AccountStore::validate_name("my wallet").is_err());
    }

    #[test]
    fn validate_name_rejects_special_chars() {
        assert!(AccountStore::validate_name("my@wallet").is_err());
    }

    #[test]
    fn validate_name_accepts_valid() {
        assert!(AccountStore::validate_name("my-wallet_01").is_ok());
    }

    #[test]
    fn save_and_load_manifest() {
        let (_dir, store) = temp_store();
        let manifest = AccountsManifest {
            default: Some("test".to_string()),
            accounts: vec![AccountEntry {
                name: "test".to_string(),
                chain: Chain::Ethereum,
                address: "0x1234".to_string(),
                kind: AccountKind::Local,
                derivation_path: None,
            }],
            aliases: vec![],
        };
        store.save_manifest(&manifest).unwrap();
        let loaded = store.load_manifest().unwrap();
        assert_eq!(loaded.default.as_deref(), Some("test"));
        assert_eq!(loaded.accounts.len(), 1);
    }

    #[test]
    fn add_and_load_ledger_account() {
        let (_dir, store) = temp_store();
        store
            .add_ledger_account(
                "hw",
                Chain::Ethereum,
                "0xABCD".to_string(),
                "m/44'/60'/0'/0/0".to_string(),
            )
            .unwrap();

        let manifest = store.load_manifest().unwrap();
        assert_eq!(manifest.accounts.len(), 1);
        assert_eq!(manifest.accounts[0].kind, AccountKind::Ledger);
        assert_eq!(
            manifest.accounts[0].derivation_path.as_deref(),
            Some("m/44'/60'/0'/0/0")
        );
        assert_eq!(manifest.default.as_deref(), Some("hw"));
    }

    #[test]
    fn add_ledger_account_no_keyring_touched() {
        let (_dir, store) = temp_store();
        store
            .add_ledger_account(
                "hw",
                Chain::Sol,
                "7Hg3test".to_string(),
                "m/44'/501'/0'".to_string(),
            )
            .unwrap();

        let err = store.get_private_key("hw").unwrap_err();
        assert!(err.to_string().contains("not a local account"));
    }

    #[test]
    fn set_default_errors_on_unknown_account() {
        let (_dir, store) = temp_store();
        let err = store.set_default("nonexistent").unwrap_err();
        assert!(matches!(err, StoreError::NotFound(_)));
    }

    #[test]
    fn set_account_chain_within_evm_family_keeps_address() {
        let (_dir, store) = temp_store();
        store
            .add_ledger_account(
                "hw",
                Chain::Base,
                "0xABCD".to_string(),
                "m/44'/60'/0'/0/0".to_string(),
            )
            .unwrap();

        store.set_account_chain("hw", Chain::Ethereum).unwrap();

        let entry = store.get_account("hw").unwrap();
        assert_eq!(entry.chain, Chain::Ethereum);
        assert_eq!(entry.address, "0xABCD");
    }

    #[test]
    fn set_account_chain_rejects_cross_family() {
        let (_dir, store) = temp_store();
        store
            .add_ledger_account(
                "hw",
                Chain::Ethereum,
                "0xABCD".to_string(),
                "m/44'/60'/0'/0/0".to_string(),
            )
            .unwrap();

        let err = store.set_account_chain("hw", Chain::Sol).unwrap_err();
        assert!(matches!(err, StoreError::ChainFamilyMismatch { .. }));
        // Unchanged on rejection.
        assert_eq!(store.get_account("hw").unwrap().chain, Chain::Ethereum);
    }

    #[test]
    fn set_account_chain_unknown_account_errors() {
        let (_dir, store) = temp_store();
        let err = store
            .set_account_chain("ghost", Chain::Ethereum)
            .unwrap_err();
        assert!(matches!(err, StoreError::NotFound(_)));
    }

    #[test]
    fn rename_ledger_account_updates_name_and_default() {
        let (_dir, store) = temp_store();
        store
            .add_ledger_account(
                "old",
                Chain::Ethereum,
                "0xABCD".to_string(),
                "m/44'/60'/0'/0/0".to_string(),
            )
            .unwrap();
        assert_eq!(
            store.default_account_name().unwrap().as_deref(),
            Some("old")
        );

        store.rename_account("old", "new").unwrap();

        assert!(store.get_account("old").is_err());
        assert_eq!(store.get_account("new").unwrap().address, "0xABCD");
        assert_eq!(
            store.default_account_name().unwrap().as_deref(),
            Some("new")
        );
    }

    #[test]
    fn rename_keystore_account_moves_file() {
        let (_dir, store) = temp_store();
        store
            .add_keystore_account("old", Chain::Ethereum, "0x1234".to_string(), KEYSTORE_JSON)
            .unwrap();
        assert!(store.keystore_path("old").exists());

        store.rename_account("old", "new").unwrap();

        assert!(!store.keystore_path("old").exists());
        assert!(store.keystore_path("new").exists());
        assert_eq!(store.read_keystore_json("new").unwrap(), KEYSTORE_JSON);
    }

    #[test]
    fn rename_to_existing_name_rejected() {
        let (_dir, store) = temp_store();
        store
            .add_ledger_account(
                "a",
                Chain::Ethereum,
                "0x1".to_string(),
                "m/44'/60'/0'/0/0".to_string(),
            )
            .unwrap();
        store
            .add_ledger_account(
                "b",
                Chain::Ethereum,
                "0x2".to_string(),
                "m/44'/60'/0'/0/0".to_string(),
            )
            .unwrap();

        let err = store.rename_account("a", "b").unwrap_err();
        assert!(matches!(err, StoreError::AlreadyExists(_)));
        assert!(store.get_account("a").is_ok());
    }

    #[test]
    fn rename_unknown_account_errors() {
        let (_dir, store) = temp_store();
        let err = store.rename_account("ghost", "new").unwrap_err();
        assert!(matches!(err, StoreError::NotFound(_)));
    }

    #[test]
    fn rename_rejects_invalid_new_name() {
        let (_dir, store) = temp_store();
        store
            .add_ledger_account(
                "ok",
                Chain::Ethereum,
                "0x1".to_string(),
                "m/44'/60'/0'/0/0".to_string(),
            )
            .unwrap();
        let err = store.rename_account("ok", "bad name!").unwrap_err();
        assert!(matches!(err, StoreError::InvalidName(_)));
    }

    #[test]
    fn add_and_get_alias() {
        let (_dir, store) = temp_store();
        store
            .add_alias("treasury", "0xABCD1234".to_string())
            .unwrap();

        let alias = store.get_alias("treasury").unwrap();
        assert_eq!(alias.name, "treasury");
        assert_eq!(alias.address, "0xABCD1234");
    }

    #[test]
    fn add_and_remove_alias() {
        let (_dir, store) = temp_store();
        store
            .add_alias("treasury", "0xABCD1234".to_string())
            .unwrap();
        store.remove_alias("treasury").unwrap();

        let err = store.get_alias("treasury").unwrap_err();
        assert!(matches!(err, StoreError::NotFound(_)));
    }

    #[test]
    fn remove_nonexistent_alias_errors() {
        let (_dir, store) = temp_store();
        let err = store.remove_alias("nope").unwrap_err();
        assert!(matches!(err, StoreError::NotFound(_)));
    }

    #[test]
    fn alias_name_collides_with_account() {
        let (_dir, store) = temp_store();
        store
            .add_ledger_account(
                "shared",
                Chain::Ethereum,
                "0x1111".to_string(),
                "m/44'/60'/0'/0/0".to_string(),
            )
            .unwrap();

        let err = store.add_alias("shared", "0x2222".to_string()).unwrap_err();
        assert!(matches!(err, StoreError::AlreadyExists(_)));
    }

    #[test]
    fn account_name_collides_with_alias() {
        let (_dir, store) = temp_store();
        store.add_alias("shared", "0x1111".to_string()).unwrap();

        let err = store
            .add_ledger_account(
                "shared",
                Chain::Ethereum,
                "0x2222".to_string(),
                "m/44'/60'/0'/0/0".to_string(),
            )
            .unwrap_err();
        assert!(matches!(err, StoreError::AlreadyExists(_)));
    }

    #[test]
    fn alias_roundtrip_manifest_serde() {
        let (_dir, store) = temp_store();
        store.add_alias("treasury", "0xABCD".to_string()).unwrap();
        store.add_alias("vault", "0xDEAD".to_string()).unwrap();

        let manifest = store.load_manifest().unwrap();
        assert_eq!(manifest.aliases.len(), 2);
        assert_eq!(manifest.aliases[0].name, "treasury");
        assert_eq!(manifest.aliases[1].address, "0xDEAD");
    }

    #[test]
    fn alias_invalid_name_rejected() {
        let (_dir, store) = temp_store();
        let err = store
            .add_alias("bad name!", "0x1234".to_string())
            .unwrap_err();
        assert!(matches!(err, StoreError::InvalidName(_)));
    }

    const KEYSTORE_JSON: &str = r#"{"version":3,"id":"test","crypto":{}}"#;

    #[test]
    fn add_keystore_account_writes_file_and_manifest() {
        let (_dir, store) = temp_store();
        store
            .add_keystore_account("enc", Chain::Ethereum, "0x1234".to_string(), KEYSTORE_JSON)
            .unwrap();

        let manifest = store.load_manifest().unwrap();
        assert_eq!(manifest.accounts.len(), 1);
        assert_eq!(manifest.accounts[0].kind, AccountKind::Keystore);
        assert_eq!(manifest.default.as_deref(), Some("enc"));

        let contents = store.read_keystore_json("enc").unwrap();
        assert_eq!(contents, KEYSTORE_JSON);
    }

    #[test]
    fn keystore_kind_displays_as_encrypted() {
        let entry = AccountEntry {
            name: "enc".to_string(),
            chain: Chain::Ethereum,
            address: "0x1234".to_string(),
            kind: AccountKind::Keystore,
            derivation_path: None,
        };
        assert_eq!(entry.kind_display(), "encrypted");
    }

    #[test]
    fn keystore_kind_roundtrips_in_manifest() {
        let (_dir, store) = temp_store();
        store
            .add_keystore_account("enc", Chain::Ethereum, "0x1234".to_string(), KEYSTORE_JSON)
            .unwrap();
        let manifest = store.load_manifest().unwrap();
        let serialized = toml::to_string_pretty(&manifest).unwrap();
        assert!(serialized.contains("keystore"));
        let parsed: AccountsManifest = toml::from_str(&serialized).unwrap();
        assert_eq!(parsed.accounts[0].kind, AccountKind::Keystore);
    }

    #[test]
    fn read_keystore_json_missing_file_errors_with_path() {
        let (_dir, store) = temp_store();
        // Manifest entry without a file (e.g. file deleted manually)
        let err = store.read_keystore_json("ghost").unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("ghost"));
        assert!(msg.contains("keystores"));
    }

    #[test]
    fn delete_keystore_account_removes_file() {
        let (_dir, store) = temp_store();
        store
            .add_keystore_account("enc", Chain::Ethereum, "0x1234".to_string(), KEYSTORE_JSON)
            .unwrap();
        let path = store.keystore_path("enc");
        assert!(path.exists());

        store.delete_account("enc").unwrap();
        assert!(!path.exists());
        assert!(store.load_manifest().unwrap().accounts.is_empty());
    }

    #[test]
    fn failed_keystore_write_leaves_no_manifest_entry() {
        let (dir, store) = temp_store();
        // A *file* named "keystores" makes create_dir_all fail deterministically.
        std::fs::write(dir.path().join("keystores"), b"not a dir").unwrap();

        let err = store
            .add_keystore_account("enc", Chain::Ethereum, "0x1234".to_string(), KEYSTORE_JSON)
            .unwrap_err();
        assert!(matches!(err, StoreError::Io(_)));

        let manifest = store.load_manifest().unwrap();
        assert!(manifest.accounts.is_empty());
        assert!(manifest.default.is_none());
    }

    #[test]
    fn keystore_account_name_collision_rejected() {
        let (_dir, store) = temp_store();
        store
            .add_keystore_account("enc", Chain::Ethereum, "0x1234".to_string(), KEYSTORE_JSON)
            .unwrap();
        let err = store
            .add_keystore_account("enc", Chain::Ethereum, "0x5678".to_string(), KEYSTORE_JSON)
            .unwrap_err();
        assert!(matches!(err, StoreError::AlreadyExists(_)));
    }

    #[cfg(unix)]
    #[test]
    fn keystore_file_has_restricted_permissions() {
        use std::os::unix::fs::PermissionsExt;
        let (_dir, store) = temp_store();
        store
            .add_keystore_account("enc", Chain::Ethereum, "0x1234".to_string(), KEYSTORE_JSON)
            .unwrap();
        let mode = std::fs::metadata(store.keystore_path("enc"))
            .unwrap()
            .permissions()
            .mode();
        assert_eq!(mode & 0o777, 0o600);
    }

    #[test]
    fn get_private_key_rejects_keystore_account() {
        let (_dir, store) = temp_store();
        store
            .add_keystore_account("enc", Chain::Ethereum, "0x1234".to_string(), KEYSTORE_JSON)
            .unwrap();
        let err = store.get_private_key("enc").unwrap_err();
        assert!(err.to_string().contains("not a local account"));
    }

    // Integration tests that actually touch the OS keyring are marked #[ignore].
    // Run them manually with: cargo test -p aleph-cli -- --ignored
    #[test]
    #[ignore]
    fn keyring_roundtrip() {
        let (_dir, store) = temp_store();
        store
            .add_local_account(
                "test-keyring-roundtrip",
                Chain::Ethereum,
                "0xtest".to_string(),
                "deadbeef",
            )
            .unwrap();

        let key = store.get_private_key("test-keyring-roundtrip").unwrap();
        assert_eq!(key, "deadbeef");

        // Cleanup
        store.delete_account("test-keyring-roundtrip").unwrap();
    }
}