soroban_cli/commands/keys/
secret.rs

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