You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Implementation Plan: Offline-First Direct IPFS (Helia)
2
+
3
+
This plan outlines the implementation of a direct, client-side IPFS node within FeatherNote using **Helia**. The focus is on a lightweight, serverless, and offline-first synchronization strategy.
4
+
5
+
## 1. Goal
6
+
Enable true decentralized synchronization by running an IPFS node directly in the browser. This eliminates the need for third-party services like Pinata or self-hosted RPC endpoints, allowing the app to function entirely offline and sync when a connection is available.
7
+
8
+
## 2. Technical Stack
9
+
-**Helia**: A modular, lightweight IPFS implementation for JS.
10
+
-**Blockstore-IDB / Datastore-IDB**: To persist IPFS data in the browser's IndexedDB.
11
+
-**Helia JSON**: For efficient handling of note objects as IPFS blocks.
12
+
-**libp2p**: Configured with WebRTC and WebSockets for browser-to-browser and browser-to-node connectivity.
13
+
14
+
## 3. Core Logic: The "Index File" Approach
15
+
Instead of relying on an external metadata API, the app will maintain a local IPFS state:
16
+
1.**Local Index (`.feathernote.json`)**: A JSON file containing:
17
+
- Map of `noteId` -> `noteCid`
18
+
-`updatedAt` timestamp
19
+
-`deletedNoteIds` list
20
+
2.**Root CID**: The CID of the latest version of this index file.
21
+
3.**Persistence**: The Root CID is stored in the app's encrypted settings. When the settings sync (via S3, Git, etc.), other devices receive the Root CID and can "pull" the data from the IPFS network.
22
+
23
+
## 4. Offline-First Strategy
24
+
-**Local Cache**: IPFS blocks are stored in IndexedDB. Adding a note to IPFS while offline will still generate a valid CID and update the local index.
25
+
-**Deferred Syncing**: When the app regains connectivity, the Helia node will:
26
+
- Connect to bootstrap nodes/relays.
27
+
- Announce the Root CID to the network.
28
+
- Fetch any missing blocks required by a newer Root CID received from settings.
29
+
30
+
## 5. Proposed Changes
31
+
32
+
### A. IPFS Module (`src/ipfs.js`)
33
+
-**Initialization**: Logic to start Helia with IndexedDB persistence.
34
+
-**`addNoteToHelia(note)`**:
35
+
- Convert note to JSON block -> get `noteCid`.
36
+
- Update the local Index file -> get `newRootCid`.
37
+
- Persist `newRootCid` to app settings.
38
+
-**`getNoteFromHelia(noteCid)`**: Retrieve and parse JSON from the IPFS network/local cache.
39
+
-**`syncIndex(remoteRootCid)`**:
40
+
- Fetch the remote index.
41
+
- Compare with local index.
42
+
- Download missing note CIDs.
43
+
44
+
### B. UI & Settings (`src/index.html`, `src/index.js`)
45
+
-**Toggle**: "Enable Direct IPFS Node".
46
+
-**Status Indicator**: Show if the node is starting, online (connected to peers), or offline.
47
+
-**Root CID Display**: Read-only field showing the current state's hash.
48
+
49
+
### C. Integration (`src/helpers.js`)
50
+
-**Sync Flow**: Modify the `synchronize` function to check the local Helia node for updates if enabled.
51
+
-**Conflict Resolution**: Use the `updatedAt` field within the `.feathernote.json` index to determine the latest global state.
52
+
53
+
## 6. Connectivity Requirements
54
+
Since browsers cannot accept direct incoming connections, we will use:
55
+
-**Public Gateways**: To fetch the index if no direct peers are found.
56
+
-**Circuit Relays**: To allow browser nodes to talk to each other through a middleman.
57
+
-**WST (Websocket-Star)** or **WebRTC-Star**: For peer discovery.
58
+
59
+
## 7. Verification Plan
60
+
1.**Offline Add**: Disable network, add a note, and verify it exists in the local IPFS blockstore (via CID).
61
+
2.**Local Peer Sync**: Open two different browsers on the same machine and verify they can sync notes via the local node.
62
+
3.**Persistence**: Refresh the page and ensure the Helia node recovers its previous state from IndexedDB.
0 commit comments