Skip to content

Add encrypted sync via Nostr relay with NIP-CF changes feed - #148

Merged
ChristianChiarulli merged 4 commits into
masterfrom
sync
Mar 14, 2026
Merged

Add encrypted sync via Nostr relay with NIP-CF changes feed#148
ChristianChiarulli merged 4 commits into
masterfrom
sync

Conversation

@ChristianChiarulli

@ChristianChiarulli ChristianChiarulli commented Mar 14, 2026

Copy link
Copy Markdown
Member

Sync engine (sync.rs) connects to a configured relay via WebSocket, authenticates with NIP-42, and subscribes to NIP-CF CHANGES feed for gift-wrapped (NIP-59) events. Notes are encrypted end-to-end — the relay only sees opaque kind:1059 blobs.

  • SyncManager as Tauri managed state with auto-start on app launch
  • Note-to-event mapping: notes become kind:30023 rumors, sealed and gift-wrapped to self with NIP-44 encryption
  • Bidirectional sync: local mutations push to relay with 2s debounce, remote changes upsert locally with last-write-wins resolution
  • Echo prevention via recently-pushed event ID tracking
  • Exponential backoff reconnection on disconnect
  • Sync state reset on relay or identity change
  • DB migration adds sync_event_id column for deletion targeting
  • Sync status indicator in sidebar (cloud icons for connected/syncing/error/disconnected)
  • Seed script updated with stable test keypair and sync_event_id column

Summary by CodeRabbit

  • New Features

    • Nostr-based note synchronization with relay support for remote backup and live syncing
    • Real-time sync status indicator in the sidebar with distinct visuals for connected/syncing/error/disconnected
    • Relay management and manual sync restart available in Settings; app now pushes note changes and deletions to relays
  • Documentation

    • Added a “Commit Style” guideline to project documentation (commit message formatting)

Sync engine (sync.rs) connects to a configured relay via WebSocket,
authenticates with NIP-42, and subscribes to NIP-CF CHANGES feed for
gift-wrapped (NIP-59) events. Notes are encrypted end-to-end — the
relay only sees opaque kind:1059 blobs.

- SyncManager as Tauri managed state with auto-start on app launch
- Note-to-event mapping: notes become kind:30023 rumors, sealed and
  gift-wrapped to self with NIP-44 encryption
- Bidirectional sync: local mutations push to relay with 2s debounce,
  remote changes upsert locally with last-write-wins resolution
- Echo prevention via recently-pushed event ID tracking
- Exponential backoff reconnection on disconnect
- Sync state reset on relay or identity change
- DB migration adds sync_event_id column for deletion targeting
- Sync status indicator in sidebar (cloud icons for connected/syncing/error/disconnected)
- Seed script updated with stable test keypair and sync_event_id column
@coderabbitai

coderabbitai Bot commented Mar 14, 2026

Copy link
Copy Markdown

Warning

Rate limit exceeded

@ChristianChiarulli has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 11 minutes and 53 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 2ed45418-b675-41e0-9008-ce53e3ea3404

📥 Commits

Reviewing files that changed from the base of the PR and between 276cfd1 and 7265a14.

📒 Files selected for processing (2)
  • src-tauri/src/lib.rs
  • src-tauri/src/sync.rs
📝 Walkthrough

Walkthrough

Introduces a Nostr-based sync subsystem: DB schema additions for identities and sync IDs, new dependencies for WebSocket/Nostr support, a SyncManager module with lifecycle and commands, UI sync status indicator, and a small docs commit-style note.

Changes

Cohort / File(s) Summary
Documentation
CLAUDE.md
Added "Commit Style" section advising commit messages must not include Co-Authored-By lines.
Database Schema & Seed
scripts/seed-db.sh, src-tauri/src/db.rs
Added nostr_identity table and seeded identities; added sync_event_id TEXT column to notes; split migrations into two steps and set PRAGMA user_version = 2.
Rust Dependencies
src-tauri/Cargo.toml
Updated nostr-sdk to enable nip59 feature; added tokio-tungstenite (native-tls), futures-util, and log.
Sync Subsystem (Backend)
src-tauri/src/sync.rs, src-tauri/src/lib.rs
Added SyncManager, SyncState/SyncCommand enums, push/delete commands, auth/signing, checkpointing, event handling, start/stop lifecycle integration, and Tauri commands get_sync_status and restart_sync; added helpers to push note changes and deletions; restart/clear behavior on identity or relay changes.
Frontend UI
src/features/shell/sidebar-pane.tsx
Replaced static profile icon with dynamic sync status button; fetches get_sync_status, listens for sync-status events, maps states to Cloud icons, and opens settings on click.
JS Types
package.json
Added @types/react-highlight-words to dependencies and devDependencies.

Sequence Diagram(s)

sequenceDiagram
    participant UI as App/UI
    participant SM as SyncManager
    participant DB as Local DB
    participant Relay as Nostr Relay (WS)

    UI->>SM: start() / push(PushNote(id))
    SM->>DB: read relay URL & identity
    SM->>Relay: open WebSocket
    Relay->>SM: AUTH challenge
    SM->>SM: sign challenge with nsec
    SM->>Relay: AUTH response
    Relay->>SM: EOSE / ready
    SM->>Relay: subscribe CHANGES with checkpoint
    alt Local note push
        UI->>SM: push(PushNote(id))
        SM->>DB: fetch note
        SM->>Relay: send signed rumor/note
        Relay->>SM: ack / CHANGES
        SM->>DB: update sync_event_id
    else Remote change arrives
        Relay->>SM: CHANGES/EVENTS
        SM->>DB: insert/update note
        SM->>UI: emit sync-status / change
    end
    UI->>SM: stop()
    SM->>Relay: close connection
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Poem

🐰 I thumped my paw on keys tonight,

whispers signed and wrapped up tight.
Relays hum and checkpoints sing,
little notes take flight on spring.
Hooray — our sync brings everything! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main feature being added: encrypted sync via Nostr relay with NIP-CF changes feed. It directly aligns with the primary objective of implementing a bidirectional sync engine using Nostr relay.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch sync
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Tip

Migrating from UI to YAML configuration.

Use the @coderabbitai configuration command in a PR comment to get a dump of all your UI settings in YAML format. You can then edit this YAML file and upload it to the root of your repository to configure CodeRabbit programmatically.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src-tauri/src/lib.rs`:
- Around line 111-114: Move the remote-delete enqueue to run only after the
local deletion succeeds: in delete_note_permanently, query the sync_event_id
(the same lookup currently inside push_deletion) before calling
notes::delete_note_permanently, then call sync_push_deletion after
notes::delete_note_permanently returns Ok; update sync_push_deletion (or add an
overload) to accept the pre-fetched sync_event_id (or accept Option<String>) so
the lookup is not repeated and the push is skipped if the local delete failed.

In `@src/features/shell/sidebar-pane.tsx`:
- Around line 1-4: The file has formatting/style issues flagged by CI; run the
project's formatter (npm run format) or Prettier to reformat
src/features/shell/sidebar-pane.tsx and commit the changes. Ensure imports and
other code in the SidebarPane component (and the top-level imports using
useEffect, useRef, useState, MouseEvent, LogicalPosition, invoke, listen) are
reformatted according to the repo's Prettier/ESLint configuration so CI style
checks pass.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 4835048c-4d94-41f2-9e64-87fcf2572982

📥 Commits

Reviewing files that changed from the base of the PR and between e03fb87 and 7fd59d0.

⛔ Files ignored due to path filters (1)
  • src-tauri/Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (7)
  • CLAUDE.md
  • scripts/seed-db.sh
  • src-tauri/Cargo.toml
  • src-tauri/src/db.rs
  • src-tauri/src/lib.rs
  • src-tauri/src/sync.rs
  • src/features/shell/sidebar-pane.tsx

Comment thread src-tauri/src/lib.rs
Comment thread src/features/shell/sidebar-pane.tsx

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@package.json`:
- Line 59: The runtime package react-highlight-words is at ^0.21.0 while the
type package `@types/react-highlight-words` is ^0.20.1 (no 0.21.x types exist); to
resolve the mismatch, pin the runtime dependency react-highlight-words to
^0.20.0 (or another 0.20.x) so it matches the `@types` version, update
package.json accordingly, and run install to verify types resolve cleanly;
alternatively, if you prefer to keep 0.21.0, remove/replace
`@types/react-highlight-words` and add appropriate manual type declarations or a
compatible typed wrapper.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: ac5a814e-6cbc-4027-ae5b-5bcabea5f766

📥 Commits

Reviewing files that changed from the base of the PR and between 7fd59d0 and 276cfd1.

⛔ Files ignored due to path filters (1)
  • package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (2)
  • package.json
  • src/features/shell/sidebar-pane.tsx
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/features/shell/sidebar-pane.tsx

Comment thread package.json
"@types/node": "^25.4.0",
"@types/react": "^19.1.8",
"@types/react-dom": "^19.1.6",
"@types/react-highlight-words": "^0.20.1",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🌐 Web query:

@types/react-highlight-words latest version npm

💡 Result:

Latest published version of @types/react-highlight-words on npm is 0.20.1. [1][2]

npm i -D `@types/react-highlight-words`@latest

🌐 Web query:

react-highlight-words npm package versions

💡 Result:

react-highlight-words currently has 32 published versions on npm, with the latest version 0.21.0 (published ~8 months ago on the npm package page). [1]

To list all available versions from the command line:

npm view react-highlight-words versions --json

Or to see the latest only:

npm view react-highlight-words version

Source: npm package page for react-highlight-words. [1]


🌐 Web query:

@types/react-highlight-words all versions 0.21

💡 Result:

There are no 0.21.x releases of @types/react-highlight-words published to the npm registry (the published versions top out at 0.20.1, with recent versions 0.20.1 and 0.20.0). [1]

If you’re looking for 0.21.0, that version exists for the runtime package react-highlight-words (not the @types/* package). [2]

Sources: [1] [2]


Type definition package version lags behind runtime package.

The @types/react-highlight-words package is pinned to ^0.20.1, but the runtime react-highlight-words package (line 45) is at ^0.21.0. No 0.21.x version of the types package exists on npm—only 0.20.1 is available. This version gap could cause type mismatches or missing type coverage for features added in 0.21.0.

Consider pinning the runtime package to ^0.20.0 to match available type definitions, or be prepared to handle type gaps when using 0.21.0-specific APIs.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@package.json` at line 59, The runtime package react-highlight-words is at
^0.21.0 while the type package `@types/react-highlight-words` is ^0.20.1 (no
0.21.x types exist); to resolve the mismatch, pin the runtime dependency
react-highlight-words to ^0.20.0 (or another 0.20.x) so it matches the `@types`
version, update package.json accordingly, and run install to verify types
resolve cleanly; alternatively, if you prefer to keep 0.21.0, remove/replace
`@types/react-highlight-words` and add appropriate manual type declarations or a
compatible typed wrapper.

The sync_event_id must be read before delete_note_permanently destroys
the row, and the deletion push should only fire after the local delete
succeeds. Pass the pre-fetched ID through the SyncCommand to avoid a
redundant query against a now-missing row.
@ChristianChiarulli
ChristianChiarulli merged commit 48a2902 into master Mar 14, 2026
4 checks passed
This was referenced Mar 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant