Skip to content

Commit

Permalink
nostrs: Add keypair get command
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Feb 26, 2023
1 parent ea51a61 commit 6e9301c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions nostrs/src/handler/keypair.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod create;
mod get;

pub use self::create::handle as create;
pub use self::get::handle as get;
14 changes: 14 additions & 0 deletions nostrs/src/handler/keypair/get.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use nostr_sdk::prelude::{FromSkStr, Keys, ToBech32};

use crate::keypair;

pub async fn handle(private_key: bool) -> anyhow::Result<()> {
let private_key_string = keypair::load()?;
let keys = Keys::from_sk_str(private_key_string.as_str())?;
if private_key {
println!("{}", keys.secret_key()?.to_bech32()?);
} else {
println!("{}", keys.public_key().to_bech32()?);
}
Ok(())
}
24 changes: 24 additions & 0 deletions nostrs/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,27 @@ enum KeypairCommand {
#[arg(long, env)]
private_key: String,
},
/// Get the keypair
Get {
#[arg(value_enum, default_value_t = PrivateKeyOrPublicKey::default())]
private_key_or_public_key: PrivateKeyOrPublicKey,
},
}

#[derive(Clone, Default, clap::ValueEnum)]
enum PrivateKeyOrPublicKey {
PrivateKey,
#[default]
PublicKey,
}

impl PrivateKeyOrPublicKey {
fn is_private_key(&self) -> bool {
match self {
PrivateKeyOrPublicKey::PrivateKey => true,
PrivateKeyOrPublicKey::PublicKey => false,
}
}
}

#[derive(clap::Subcommand)]
Expand All @@ -69,6 +90,9 @@ async fn main() -> anyhow::Result<()> {
},
Resource::Keypair { command } => match command {
KeypairCommand::Create { private_key } => handler::keypair::create(private_key).await,
KeypairCommand::Get {
private_key_or_public_key,
} => handler::keypair::get(private_key_or_public_key.is_private_key()).await,
},
Resource::Metadata => handler::metadata::get().await,
Resource::TextNote { command } => match command {
Expand Down

0 comments on commit 6e9301c

Please sign in to comment.