diff --git a/FULL_HELP_DOCS.md b/FULL_HELP_DOCS.md index e3eaf83c8..e12687892 100644 --- a/FULL_HELP_DOCS.md +++ b/FULL_HELP_DOCS.md @@ -1116,6 +1116,7 @@ Output an identity's secret key ###### **Options:** +* `--phrase` — Output seed phrase instead of private key * `--hd-path ` — If identity is a seed phrase use this hd path, default is 0 * `--global` — Use global config * `--config-dir ` — Location of config directory, default is "." diff --git a/cmd/soroban-cli/src/commands/keys/secret.rs b/cmd/soroban-cli/src/commands/keys/secret.rs index 38ac83116..8facc2427 100644 --- a/cmd/soroban-cli/src/commands/keys/secret.rs +++ b/cmd/soroban-cli/src/commands/keys/secret.rs @@ -1,6 +1,10 @@ use clap::arg; -use crate::config::{key, locator}; +use crate::config::{ + key::{self, Key}, + locator, + secret::Secret, +}; #[derive(thiserror::Error, Debug)] pub enum Error { @@ -9,6 +13,9 @@ pub enum Error { #[error(transparent)] Key(#[from] key::Error), + + #[error("identity is not tied to a seed phrase")] + UnknownSeedPhrase, } #[derive(Debug, clap::Parser, Clone)] @@ -18,8 +25,12 @@ pub struct Cmd { /// Name of identity to lookup, default is test identity pub name: String, + /// Output seed phrase instead of private key + #[arg(long, conflicts_with = "hd_path")] + pub phrase: bool, + /// If identity is a seed phrase use this hd path, default is 0 - #[arg(long)] + #[arg(long, conflicts_with = "phrase")] pub hd_path: Option, #[command(flatten)] @@ -28,10 +39,25 @@ pub struct Cmd { impl Cmd { pub fn run(&self) -> Result<(), Error> { - println!("{}", self.private_key()?.to_string()); + if self.phrase { + println!("{}", self.seed_phrase()?); + } else { + println!("{}", self.private_key()?); + } + Ok(()) } + pub fn seed_phrase(&self) -> Result { + let key = self.locator.read_identity(&self.name)?; + + if let Key::Secret(Secret::SeedPhrase { seed_phrase }) = key { + Ok(seed_phrase) + } else { + Err(Error::UnknownSeedPhrase) + } + } + pub fn private_key(&self) -> Result { Ok(self .locator