mirror of
https://github.com/PR0M3TH3AN/bitvid.git
synced 2026-10-27 07:52:24 +00:00
walletSyncService and storageSyncService were near-identical copies (the "two sync systems"). Extract the shared machinery into createEncryptedSyncItem() — per-pubkey enable flags, conflict-checked push, cheap remote-exists check, enable/disable/pull — so each service is now a thin config differing only in d-tag, kind, availability, and buildPayload/applyPayload. Public surface (isAvailable/isEnabled/hasRemote/push/pull/enable/disable + the *_DTAG export) preserved; both dedicated suites pass (8). Next (step 2): shared confirm dialog + consistent notifications + seamless auto-sync in the two profile controllers (the user-facing UX unification). Co-Authored-By: Claude Opus 4.8 <[email protected]>
43 lines
1.6 KiB
JavaScript
43 lines
1.6 KiB
JavaScript
// Opt-in encrypted cross-login sync for STORAGE credentials (todo #15, step 2).
|
|
// Wraps the encrypted-sync core: the on-disk storage account record (already
|
|
// self-protected, but with plaintext meta) is re-encrypted to self and published
|
|
// as a NIP-78 replaceable note under a stable d-tag, then pulled + imported on
|
|
// another device.
|
|
//
|
|
// Opt-in only, off by default. A per-pubkey local flag remembers the choice so
|
|
// the UI toggle persists and a future "re-push on change" knows whether to sync.
|
|
|
|
import { encryptedSync as defaultEncryptedSync } from "../nostr/encryptedSyncFacade.js";
|
|
import defaultStorageService from "./storageService.js";
|
|
import { createEncryptedSyncItem } from "./encryptedSyncItem.js";
|
|
|
|
export const STORAGE_SYNC_DTAG = "bitvid:storage-connections";
|
|
const SYNC_KIND = "storage";
|
|
|
|
export function createStorageSyncService({
|
|
encryptedSync = defaultEncryptedSync,
|
|
storage = defaultStorageService,
|
|
} = {}) {
|
|
const item = createEncryptedSyncItem({
|
|
id: SYNC_KIND,
|
|
dtag: STORAGE_SYNC_DTAG,
|
|
encryptedSync,
|
|
logLabel: "storageSync",
|
|
// Export the local storage account record (self-protected). null => nothing
|
|
// to sync. An export error surfaces as { error: "export-failed" }.
|
|
buildPayload: async (pubkey) => {
|
|
const record = await storage.exportAccountRecord(pubkey);
|
|
return record || null;
|
|
},
|
|
applyPayload: async (pubkey, payload) => {
|
|
await storage.importAccountRecord(pubkey, payload);
|
|
},
|
|
});
|
|
|
|
return { STORAGE_SYNC_DTAG, ...item };
|
|
}
|
|
|
|
export const storageSyncService = createStorageSyncService();
|
|
|
|
export default storageSyncService;
|