mirror of
https://github.com/PR0M3TH3AN/SeedPass.git
synced 2025-09-08 15:28:44 +00:00
Merge branch 'beta' of https://github.com/PR0M3TH3AN/SeedPass into beta
This commit is contained in:
@@ -265,8 +265,13 @@ class PasswordManager:
|
|||||||
Prompts the user to select an existing fingerprint or add a new one.
|
Prompts the user to select an existing fingerprint or add a new one.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
print(colored("\nAvailable Seed Profiles:", "cyan"))
|
|
||||||
fingerprints = self.fingerprint_manager.list_fingerprints()
|
fingerprints = self.fingerprint_manager.list_fingerprints()
|
||||||
|
current = self.fingerprint_manager.current_fingerprint
|
||||||
|
if current and current in fingerprints:
|
||||||
|
self.select_fingerprint(current)
|
||||||
|
return
|
||||||
|
|
||||||
|
print(colored("\nAvailable Seed Profiles:", "cyan"))
|
||||||
for idx, fp in enumerate(fingerprints, start=1):
|
for idx, fp in enumerate(fingerprints, start=1):
|
||||||
print(colored(f"{idx}. {fp}", "cyan"))
|
print(colored(f"{idx}. {fp}", "cyan"))
|
||||||
|
|
||||||
|
60
src/tests/test_last_used_fingerprint.py
Normal file
60
src/tests/test_last_used_fingerprint.py
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
import importlib
|
||||||
|
from pathlib import Path
|
||||||
|
from tempfile import TemporaryDirectory
|
||||||
|
|
||||||
|
import constants
|
||||||
|
import password_manager.manager as manager_module
|
||||||
|
from utils.fingerprint_manager import FingerprintManager
|
||||||
|
from password_manager.manager import EncryptionMode
|
||||||
|
|
||||||
|
from helpers import TEST_SEED
|
||||||
|
|
||||||
|
|
||||||
|
def test_last_used_fingerprint(monkeypatch):
|
||||||
|
with TemporaryDirectory() as tmpdir:
|
||||||
|
tmp_path = Path(tmpdir)
|
||||||
|
monkeypatch.setattr(Path, "home", lambda: tmp_path)
|
||||||
|
|
||||||
|
importlib.reload(constants)
|
||||||
|
importlib.reload(manager_module)
|
||||||
|
|
||||||
|
fm = FingerprintManager(constants.APP_DIR)
|
||||||
|
fp = fm.add_fingerprint(TEST_SEED)
|
||||||
|
assert fm.current_fingerprint == fp
|
||||||
|
|
||||||
|
# Ensure persistence on reload
|
||||||
|
fm2 = FingerprintManager(constants.APP_DIR)
|
||||||
|
assert fm2.current_fingerprint == fp
|
||||||
|
|
||||||
|
def init_fm(self):
|
||||||
|
self.fingerprint_manager = fm2
|
||||||
|
|
||||||
|
monkeypatch.setattr(
|
||||||
|
manager_module.PasswordManager, "initialize_fingerprint_manager", init_fm
|
||||||
|
)
|
||||||
|
monkeypatch.setattr(
|
||||||
|
manager_module.PasswordManager,
|
||||||
|
"setup_encryption_manager",
|
||||||
|
lambda *a, **k: True,
|
||||||
|
)
|
||||||
|
monkeypatch.setattr(
|
||||||
|
manager_module.PasswordManager, "initialize_bip85", lambda self: None
|
||||||
|
)
|
||||||
|
monkeypatch.setattr(
|
||||||
|
manager_module.PasswordManager, "initialize_managers", lambda self: None
|
||||||
|
)
|
||||||
|
monkeypatch.setattr(
|
||||||
|
manager_module.PasswordManager,
|
||||||
|
"sync_index_from_nostr_if_missing",
|
||||||
|
lambda self: None,
|
||||||
|
)
|
||||||
|
monkeypatch.setattr(
|
||||||
|
manager_module.PasswordManager, "verify_password", lambda *a, **k: True
|
||||||
|
)
|
||||||
|
monkeypatch.setattr(
|
||||||
|
"builtins.input",
|
||||||
|
lambda *a, **k: (_ for _ in ()).throw(AssertionError("prompted")),
|
||||||
|
)
|
||||||
|
|
||||||
|
pm = manager_module.PasswordManager()
|
||||||
|
assert pm.current_fingerprint == fp
|
@@ -34,8 +34,7 @@ class FingerprintManager:
|
|||||||
self.app_dir = app_dir
|
self.app_dir = app_dir
|
||||||
self.fingerprints_file = self.app_dir / "fingerprints.json"
|
self.fingerprints_file = self.app_dir / "fingerprints.json"
|
||||||
self._ensure_app_directory()
|
self._ensure_app_directory()
|
||||||
self.fingerprints = self._load_fingerprints()
|
self.fingerprints, self.current_fingerprint = self._load_fingerprints()
|
||||||
self.current_fingerprint: Optional[str] = None
|
|
||||||
|
|
||||||
def get_current_fingerprint_dir(self) -> Optional[Path]:
|
def get_current_fingerprint_dir(self) -> Optional[Path]:
|
||||||
"""
|
"""
|
||||||
@@ -63,28 +62,25 @@ class FingerprintManager:
|
|||||||
)
|
)
|
||||||
raise
|
raise
|
||||||
|
|
||||||
def _load_fingerprints(self) -> List[str]:
|
def _load_fingerprints(self) -> tuple[list[str], Optional[str]]:
|
||||||
"""
|
"""Return stored fingerprints and the last used fingerprint."""
|
||||||
Loads the list of fingerprints from the fingerprints.json file.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
List[str]: A list of fingerprint strings.
|
|
||||||
"""
|
|
||||||
try:
|
try:
|
||||||
if self.fingerprints_file.exists():
|
if self.fingerprints_file.exists():
|
||||||
with open(self.fingerprints_file, "r") as f:
|
with open(self.fingerprints_file, "r") as f:
|
||||||
data = json.load(f)
|
data = json.load(f)
|
||||||
fingerprints = data.get("fingerprints", [])
|
fingerprints = data.get("fingerprints", [])
|
||||||
logger.debug(f"Loaded fingerprints: {fingerprints}")
|
current = data.get("last_used")
|
||||||
return fingerprints
|
|
||||||
else:
|
|
||||||
logger.debug(
|
logger.debug(
|
||||||
"fingerprints.json not found. Initializing empty fingerprint list."
|
f"Loaded fingerprints: {fingerprints} (last used: {current})"
|
||||||
)
|
)
|
||||||
return []
|
return fingerprints, current
|
||||||
|
logger.debug(
|
||||||
|
"fingerprints.json not found. Initializing empty fingerprint list."
|
||||||
|
)
|
||||||
|
return [], None
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Failed to load fingerprints: {e}", exc_info=True)
|
logger.error(f"Failed to load fingerprints: {e}", exc_info=True)
|
||||||
return []
|
return [], None
|
||||||
|
|
||||||
def _save_fingerprints(self):
|
def _save_fingerprints(self):
|
||||||
"""
|
"""
|
||||||
@@ -92,8 +88,17 @@ class FingerprintManager:
|
|||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
with open(self.fingerprints_file, "w") as f:
|
with open(self.fingerprints_file, "w") as f:
|
||||||
json.dump({"fingerprints": self.fingerprints}, f, indent=4)
|
json.dump(
|
||||||
logger.debug(f"Fingerprints saved: {self.fingerprints}")
|
{
|
||||||
|
"fingerprints": self.fingerprints,
|
||||||
|
"last_used": self.current_fingerprint,
|
||||||
|
},
|
||||||
|
f,
|
||||||
|
indent=4,
|
||||||
|
)
|
||||||
|
logger.debug(
|
||||||
|
f"Fingerprints saved: {self.fingerprints} (last used: {self.current_fingerprint})"
|
||||||
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Failed to save fingerprints: {e}", exc_info=True)
|
logger.error(f"Failed to save fingerprints: {e}", exc_info=True)
|
||||||
raise
|
raise
|
||||||
@@ -111,6 +116,7 @@ class FingerprintManager:
|
|||||||
fingerprint = generate_fingerprint(seed_phrase)
|
fingerprint = generate_fingerprint(seed_phrase)
|
||||||
if fingerprint and fingerprint not in self.fingerprints:
|
if fingerprint and fingerprint not in self.fingerprints:
|
||||||
self.fingerprints.append(fingerprint)
|
self.fingerprints.append(fingerprint)
|
||||||
|
self.current_fingerprint = fingerprint
|
||||||
self._save_fingerprints()
|
self._save_fingerprints()
|
||||||
logger.info(f"Fingerprint {fingerprint} added successfully.")
|
logger.info(f"Fingerprint {fingerprint} added successfully.")
|
||||||
# Create fingerprint directory
|
# Create fingerprint directory
|
||||||
@@ -138,6 +144,10 @@ class FingerprintManager:
|
|||||||
if fingerprint in self.fingerprints:
|
if fingerprint in self.fingerprints:
|
||||||
try:
|
try:
|
||||||
self.fingerprints.remove(fingerprint)
|
self.fingerprints.remove(fingerprint)
|
||||||
|
if self.current_fingerprint == fingerprint:
|
||||||
|
self.current_fingerprint = (
|
||||||
|
self.fingerprints[0] if self.fingerprints else None
|
||||||
|
)
|
||||||
self._save_fingerprints()
|
self._save_fingerprints()
|
||||||
# Remove fingerprint directory
|
# Remove fingerprint directory
|
||||||
fingerprint_dir = self.app_dir / fingerprint
|
fingerprint_dir = self.app_dir / fingerprint
|
||||||
@@ -181,6 +191,7 @@ class FingerprintManager:
|
|||||||
"""
|
"""
|
||||||
if fingerprint in self.fingerprints:
|
if fingerprint in self.fingerprints:
|
||||||
self.current_fingerprint = fingerprint
|
self.current_fingerprint = fingerprint
|
||||||
|
self._save_fingerprints()
|
||||||
logger.info(f"Fingerprint {fingerprint} selected.")
|
logger.info(f"Fingerprint {fingerprint} selected.")
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
|
Reference in New Issue
Block a user