mirror of
https://github.com/PR0M3TH3AN/SeedPass.git
synced 2025-09-09 07:48:57 +00:00
Add relay management settings
This commit is contained in:
11
README.md
11
README.md
@@ -170,12 +170,15 @@ wss://nostr.oxtr.dev
|
|||||||
wss://relay.primal.net
|
wss://relay.primal.net
|
||||||
```
|
```
|
||||||
|
|
||||||
You can update the relay list or change the PIN through the **Settings** menu:
|
You can manage the relay list or change the PIN through the **Settings** menu:
|
||||||
|
|
||||||
1. From the main menu, choose option `13` (**Settings**).
|
1. From the main menu, choose option `13` (**Settings**).
|
||||||
2. Select `1` to enter a comma-separated list of relay URLs.
|
2. Select `1` to view your current relays.
|
||||||
3. Choose `2` to change the settings PIN.
|
3. Choose `2` to add a new relay URL.
|
||||||
4. Select `3` to go back to the main menu.
|
4. Select `3` to remove a relay by number.
|
||||||
|
5. Choose `4` to reset to the default relay list.
|
||||||
|
6. Select `5` to change the settings PIN.
|
||||||
|
7. Choose `6` to return to the main menu.
|
||||||
|
|
||||||
## Running Tests
|
## Running Tests
|
||||||
|
|
||||||
|
147
src/main.py
147
src/main.py
@@ -247,35 +247,144 @@ def handle_retrieve_from_nostr(password_manager: PasswordManager):
|
|||||||
print(colored(f"Error: Failed to retrieve from Nostr: {e}", "red"))
|
print(colored(f"Error: Failed to retrieve from Nostr: {e}", "red"))
|
||||||
|
|
||||||
|
|
||||||
def handle_settings(password_manager: PasswordManager):
|
def handle_view_relays(cfg_mgr: "ConfigManager") -> None:
|
||||||
"""Display settings menu for relay list and PIN changes."""
|
"""Display the currently configured Nostr relays."""
|
||||||
|
try:
|
||||||
|
cfg = cfg_mgr.load_config(require_pin=False)
|
||||||
|
relays = cfg.get("relays", [])
|
||||||
|
if not relays:
|
||||||
|
print(colored("No relays configured.", "yellow"))
|
||||||
|
return
|
||||||
|
print(colored("\nCurrent Relays:", "cyan"))
|
||||||
|
for idx, relay in enumerate(relays, start=1):
|
||||||
|
print(colored(f"{idx}. {relay}", "cyan"))
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(f"Error displaying relays: {e}")
|
||||||
|
print(colored(f"Error: {e}", "red"))
|
||||||
|
|
||||||
|
|
||||||
|
def _reload_relays(password_manager: PasswordManager, relays: list) -> None:
|
||||||
|
"""Reload NostrClient with the updated relay list."""
|
||||||
|
try:
|
||||||
|
password_manager.nostr_client.close_client_pool()
|
||||||
|
except Exception as exc:
|
||||||
|
logging.warning(f"Failed to close client pool: {exc}")
|
||||||
|
try:
|
||||||
|
password_manager.nostr_client.relays = relays
|
||||||
|
password_manager.nostr_client.initialize_client_pool()
|
||||||
|
except Exception as exc:
|
||||||
|
logging.error(f"Failed to reinitialize NostrClient: {exc}")
|
||||||
|
|
||||||
|
|
||||||
|
def handle_add_relay(password_manager: PasswordManager) -> None:
|
||||||
|
"""Prompt for a relay URL and add it to the config."""
|
||||||
|
cfg_mgr = password_manager.config_manager
|
||||||
|
if cfg_mgr is None:
|
||||||
|
print(colored("Configuration manager unavailable.", "red"))
|
||||||
|
return
|
||||||
|
url = input("Enter relay URL to add: ").strip()
|
||||||
|
if not url:
|
||||||
|
print(colored("No URL entered.", "yellow"))
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
cfg = cfg_mgr.load_config(require_pin=False)
|
||||||
|
relays = cfg.get("relays", [])
|
||||||
|
if url in relays:
|
||||||
|
print(colored("Relay already present.", "yellow"))
|
||||||
|
return
|
||||||
|
relays.append(url)
|
||||||
|
cfg_mgr.set_relays(relays)
|
||||||
|
_reload_relays(password_manager, relays)
|
||||||
|
print(colored("Relay added.", "green"))
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(f"Error adding relay: {e}")
|
||||||
|
print(colored(f"Error: {e}", "red"))
|
||||||
|
|
||||||
|
|
||||||
|
def handle_remove_relay(password_manager: PasswordManager) -> None:
|
||||||
|
"""Remove a relay from the config by its index."""
|
||||||
cfg_mgr = password_manager.config_manager
|
cfg_mgr = password_manager.config_manager
|
||||||
if cfg_mgr is None:
|
if cfg_mgr is None:
|
||||||
print(colored("Configuration manager unavailable.", "red"))
|
print(colored("Configuration manager unavailable.", "red"))
|
||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
cfg = cfg_mgr.load_config()
|
cfg = cfg_mgr.load_config(require_pin=False)
|
||||||
|
relays = cfg.get("relays", [])
|
||||||
|
if not relays:
|
||||||
|
print(colored("No relays configured.", "yellow"))
|
||||||
|
return
|
||||||
|
for idx, relay in enumerate(relays, start=1):
|
||||||
|
print(colored(f"{idx}. {relay}", "cyan"))
|
||||||
|
choice = input("Select relay number to remove: ").strip()
|
||||||
|
if not choice.isdigit() or not (1 <= int(choice) <= len(relays)):
|
||||||
|
print(colored("Invalid selection.", "red"))
|
||||||
|
return
|
||||||
|
relays.pop(int(choice) - 1)
|
||||||
|
cfg_mgr.set_relays(relays)
|
||||||
|
_reload_relays(password_manager, relays)
|
||||||
|
print(colored("Relay removed.", "green"))
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(f"Error removing relay: {e}")
|
||||||
|
print(colored(f"Error: {e}", "red"))
|
||||||
|
|
||||||
|
|
||||||
|
def handle_reset_relays(password_manager: PasswordManager) -> None:
|
||||||
|
"""Reset relay list to defaults."""
|
||||||
|
cfg_mgr = password_manager.config_manager
|
||||||
|
if cfg_mgr is None:
|
||||||
|
print(colored("Configuration manager unavailable.", "red"))
|
||||||
|
return
|
||||||
|
from nostr.client import DEFAULT_RELAYS
|
||||||
|
|
||||||
|
try:
|
||||||
|
cfg_mgr.set_relays(list(DEFAULT_RELAYS))
|
||||||
|
_reload_relays(password_manager, list(DEFAULT_RELAYS))
|
||||||
|
print(colored("Relays reset to defaults.", "green"))
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(f"Error resetting relays: {e}")
|
||||||
|
print(colored(f"Error: {e}", "red"))
|
||||||
|
|
||||||
|
|
||||||
|
def handle_settings(password_manager: PasswordManager) -> None:
|
||||||
|
"""Interactive settings menu for relay list and PIN changes."""
|
||||||
|
cfg_mgr = password_manager.config_manager
|
||||||
|
if cfg_mgr is None:
|
||||||
|
print(colored("Configuration manager unavailable.", "red"))
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
cfg_mgr.load_config()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(colored(f"Error loading settings: {e}", "red"))
|
print(colored(f"Error loading settings: {e}", "red"))
|
||||||
return
|
return
|
||||||
|
|
||||||
print("\nSettings:")
|
while True:
|
||||||
print("1. Set Nostr relays")
|
print("\nSettings:")
|
||||||
print("2. Change settings PIN")
|
print("1. View current relays")
|
||||||
print("3. Back")
|
print("2. Add a relay URL")
|
||||||
choice = input("Select an option: ").strip()
|
print("3. Remove a relay by number")
|
||||||
if choice == "1":
|
print("4. Reset to default relays")
|
||||||
relays = input("Enter comma-separated relay URLs: ").split(",")
|
print("5. Change settings PIN")
|
||||||
relays = [r.strip() for r in relays if r.strip()]
|
print("6. Back")
|
||||||
cfg_mgr.set_relays(relays)
|
choice = input("Select an option: ").strip()
|
||||||
print(colored("Relays updated.", "green"))
|
if choice == "1":
|
||||||
elif choice == "2":
|
handle_view_relays(cfg_mgr)
|
||||||
old_pin = getpass.getpass("Current PIN: ")
|
elif choice == "2":
|
||||||
new_pin = getpass.getpass("New PIN: ")
|
handle_add_relay(password_manager)
|
||||||
if cfg_mgr.change_pin(old_pin, new_pin):
|
elif choice == "3":
|
||||||
print(colored("PIN changed successfully.", "green"))
|
handle_remove_relay(password_manager)
|
||||||
|
elif choice == "4":
|
||||||
|
handle_reset_relays(password_manager)
|
||||||
|
elif choice == "5":
|
||||||
|
old_pin = getpass.getpass("Current PIN: ")
|
||||||
|
new_pin = getpass.getpass("New PIN: ")
|
||||||
|
if cfg_mgr.change_pin(old_pin, new_pin):
|
||||||
|
print(colored("PIN changed successfully.", "green"))
|
||||||
|
else:
|
||||||
|
print(colored("Incorrect current PIN.", "red"))
|
||||||
|
elif choice == "6":
|
||||||
|
break
|
||||||
else:
|
else:
|
||||||
print(colored("Incorrect current PIN.", "red"))
|
print(colored("Invalid choice.", "red"))
|
||||||
|
|
||||||
|
|
||||||
def display_menu(password_manager: PasswordManager):
|
def display_menu(password_manager: PasswordManager):
|
||||||
|
Reference in New Issue
Block a user