sync from nostr on unlock

This commit is contained in:
thePR0M3TH3AN
2025-07-05 18:45:15 -04:00
parent de3972ad1a
commit 540950d06f
3 changed files with 51 additions and 0 deletions

View File

@@ -187,6 +187,7 @@ class PasswordManager:
self.initialize_managers()
self.locked = False
self.update_activity()
self.sync_index_from_nostr()
def initialize_fingerprint_manager(self):
"""
@@ -833,6 +834,29 @@ class PasswordManager:
print(colored(f"Error: Failed to initialize managers: {e}", "red"))
sys.exit(1)
def sync_index_from_nostr(self) -> None:
"""Always fetch the latest vault data from Nostr and update the local index."""
try:
result = asyncio.run(self.nostr_client.fetch_latest_snapshot())
if not result:
return
manifest, chunks = result
encrypted = gzip.decompress(b"".join(chunks))
if manifest.delta_since:
try:
version = int(manifest.delta_since)
deltas = asyncio.run(self.nostr_client.fetch_deltas_since(version))
if deltas:
encrypted = deltas[-1]
except ValueError:
pass
current = self.vault.get_encrypted_index()
if current != encrypted:
self.vault.decrypt_and_save_index_from_nostr(encrypted)
logger.info("Local database synchronized from Nostr.")
except Exception as e:
logger.warning(f"Unable to sync index from Nostr: {e}")
def sync_index_from_nostr_if_missing(self) -> None:
"""Retrieve the password database from Nostr if it doesn't exist locally."""
index_file = self.fingerprint_dir / "seedpass_entries_db.json.enc"