Skip to content

Commit

Permalink
Add support for returning seed phrases on stellar keys secret. (#1853)
Browse files Browse the repository at this point in the history
  • Loading branch information
fnando authored Jan 29, 2025
1 parent a0c5dbb commit ec6d03a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
1 change: 1 addition & 0 deletions FULL_HELP_DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -1116,6 +1116,7 @@ Output an identity's secret key

###### **Options:**

* `--phrase` — Output seed phrase instead of private key
* `--hd-path <HD_PATH>` — If identity is a seed phrase use this hd path, default is 0
* `--global` — Use global config
* `--config-dir <CONFIG_DIR>` — Location of config directory, default is "."
Expand Down
32 changes: 29 additions & 3 deletions cmd/soroban-cli/src/commands/keys/secret.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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)]
Expand All @@ -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<usize>,

#[command(flatten)]
Expand All @@ -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<String, Error> {
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<stellar_strkey::ed25519::PrivateKey, Error> {
Ok(self
.locator
Expand Down

0 comments on commit ec6d03a

Please sign in to comment.