aleph-cli 0.8.4

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
use aleph_types::chain::Chain;
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};

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

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum AccountKind {
    Local,
    Ledger, // Phase 2
}

/// 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",
        }
    }
}

/// 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(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(())
    }

    /// 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)
    }

    /// 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)
    }

    /// 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 is_local = manifest.accounts[idx].kind == AccountKind::Local;

        // 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. Remove from keyring if local — failure here is non-fatal
        //    (orphaned keyring entry is harmless)
        if is_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)),
            }
        }

        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 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(_)));
    }

    // 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();
    }
}