From 03d5c8020ed3c87a6f6e49caf731f9bfd2719ef8 Mon Sep 17 00:00:00 2001
From: Awiteb
Date: Sun, 27 Jul 2025 00:47:18 +0000
Subject: [PATCH] feat: Keyring the secret key `n34 config keyring --enable`
Signed-off-by: Awiteb
---
CHANGELOG.md | 1 +
src/cli/commands/config/keyring.rs | 63 ++++++++++++++++++++++++++++++
src/cli/commands/config/mod.rs | 8 +++-
src/cli/commands/mod.rs | 10 ++++-
src/cli/config.rs | 13 +++---
src/cli/mod.rs | 45 +++++++++++++++++----
src/error.rs | 5 +++
src/nostr_utils/traits.rs | 9 +++++
8 files changed, 139 insertions(+), 15 deletions(-)
create mode 100644 src/cli/commands/config/keyring.rs
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 89ce01c..1437b8d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Support signing using NIP-46 bunker - by Awiteb
+- Keyring the secret key `n34 config keyring --enable` - by Awiteb
### Dependencies
diff --git a/src/cli/commands/config/keyring.rs b/src/cli/commands/config/keyring.rs
new file mode 100644
index 0000000..6e27acc
--- /dev/null
+++ b/src/cli/commands/config/keyring.rs
@@ -0,0 +1,63 @@
+// n34 - A CLI to interact with NIP-34 and other stuff related to codes in nostr
+// Copyright (C) 2025 Awiteb
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program. If not, see .
+
+use clap::{ArgGroup, Args};
+
+use crate::{
+ cli::{Cli, CliOptions, traits::CommandRunner},
+ error::N34Result,
+};
+
+#[derive(Args, Debug)]
+#[clap(
+ group(
+ ArgGroup::new("options")
+ .required(true)
+ )
+)]
+pub struct KeyringArgs {
+ /// Turns on secret key keyring. Requires entering the key once when
+ /// enabled.
+ #[arg(long, group = "options")]
+ enable: bool,
+ /// Turns off secret key keyring. Removes any existing key and prevents
+ /// storing new ones.
+ #[arg(long, group = "options")]
+ disable: bool,
+ /// Deletes current key and stores the next provided key.
+ #[arg(long, group = "options")]
+ reset: bool,
+}
+
+impl CommandRunner for KeyringArgs {
+ const NEED_SIGNER: bool = false;
+
+ async fn run(self, mut options: CliOptions) -> N34Result<()> {
+ let keyring = nostr_keyring::NostrKeyring::new(Cli::N34_KEYRING_SERVICE_NAME);
+
+ if self.enable {
+ options.config.keyring_secret_key = true;
+ } else if self.disable {
+ options.config.keyring_secret_key = false;
+ }
+
+ if self.reset || self.disable {
+ let _ = keyring.delete(Cli::USER_KEY_PAIR_ENTRY);
+ }
+
+ options.config.dump()
+ }
+}
diff --git a/src/cli/commands/config/mod.rs b/src/cli/commands/config/mod.rs
index 2cb2c45..24968be 100644
--- a/src/cli/commands/config/mod.rs
+++ b/src/cli/commands/config/mod.rs
@@ -16,6 +16,8 @@
/// `config bunker` subcommand
mod bunker;
+/// `config keyring` subcommand
+mod keyring;
/// `config pow` subcommand
mod pow;
/// `config relays` subcommand
@@ -24,6 +26,7 @@ mod relays;
use clap::Subcommand;
use self::bunker::BunkerArgs;
+use self::keyring::KeyringArgs;
use self::pow::PowArgs;
use self::relays::RelaysArgs;
use super::CliOptions;
@@ -39,10 +42,13 @@ pub enum ConfigSubcommands {
Relays(RelaysArgs),
/// Sets a URL of NIP-46 bunker server used for signing events.
Bunker(BunkerArgs),
+ /// Managing the secret key keyring, including enabling, disabling, or
+ /// resetting it.
+ Keyring(KeyringArgs),
}
impl CommandRunner for ConfigSubcommands {
async fn run(self, options: CliOptions) -> N34Result<()> {
- crate::run_command!(self, options, & Pow Relays Bunker)
+ crate::run_command!(self, options, & Pow Relays Bunker Keyring)
}
}
diff --git a/src/cli/commands/mod.rs b/src/cli/commands/mod.rs
index b27692c..5fa3341 100644
--- a/src/cli/commands/mod.rs
+++ b/src/cli/commands/mod.rs
@@ -137,9 +137,16 @@ impl CliOptions {
/// Returns the signer
pub fn signer(&self) -> N34Result