Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for returning seed phrases on stellar keys secret. #1853

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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