soroban_cli/commands/keys/
secret.rs

1use crate::config::{
2    key::{self, Key},
3    locator,
4    secret::Secret,
5};
6
7#[derive(thiserror::Error, Debug)]
8pub enum Error {
9    #[error(transparent)]
10    Config(#[from] locator::Error),
11
12    #[error(transparent)]
13    Key(#[from] key::Error),
14
15    #[error("identity is not tied to a seed phrase")]
16    UnknownSeedPhrase,
17}
18
19#[derive(Debug, clap::Parser, Clone)]
20#[group(skip)]
21#[command(name = "secret", alias = "show")]
22pub struct Cmd {
23    /// Name of identity to lookup, default is test identity
24    pub name: String,
25
26    /// Output seed phrase instead of private key
27    #[arg(long, conflicts_with = "hd_path")]
28    pub phrase: bool,
29
30    /// If identity is a seed phrase use this hd path, default is 0
31    #[arg(long, conflicts_with = "phrase")]
32    pub hd_path: Option<usize>,
33
34    #[command(flatten)]
35    pub locator: locator::Args,
36}
37
38impl Cmd {
39    pub fn run(&self) -> Result<(), Error> {
40        if self.phrase {
41            println!("{}", self.seed_phrase()?);
42        } else {
43            println!("{}", self.private_key()?);
44        }
45
46        Ok(())
47    }
48
49    pub fn seed_phrase(&self) -> Result<String, Error> {
50        let key = self.locator.read_identity(&self.name)?;
51
52        if let Key::Secret(Secret::SeedPhrase { seed_phrase }) = key {
53            Ok(seed_phrase)
54        } else {
55            Err(Error::UnknownSeedPhrase)
56        }
57    }
58
59    pub fn private_key(&self) -> Result<stellar_strkey::ed25519::PrivateKey, Error> {
60        Ok(self
61            .locator
62            .read_identity(&self.name)?
63            .private_key(self.hd_path)?)
64    }
65}