| … | … | @@ -0,0 +1,419 @@ |
| 1 | # Wirecore Pear App - Implementation Plan |
| 2 | |
| 3 | **Date**: 2026-01-20 |
| 4 | **Status**: Planning Phase |
| 5 | |
| 6 | --- |
| 7 | |
| 8 | ## Overview |
| 9 | |
| 10 | Build a Pear desktop application for Wirecore that provides: |
| 11 | 1. **Local-first** P2P messaging using Hypercore |
| 12 | 2. **APDS-compatible** protocol (like wiredove) |
| 13 | 3. **No central server required** (fully P2P via Hyperswarm) |
| 14 | 4. **Terminal UI** (Pear terminal app, like hyperident) |
| 15 | |
| 16 | --- |
| 17 | |
| 18 | ## Architecture |
| 19 | |
| 20 | ### Core Components |
| 21 | |
| 22 | ``` |
| 23 | ┌─────────────────────────────────────────────────────┐ |
| 24 | │ Wirecore Pear App (Bare Runtime) │ |
| 25 | ├─────────────────────────────────────────────────────┤ |
| 26 | │ │ |
| 27 | │ ┌──────────────┐ ┌──────────────┐ ┌──────────┐ │ |
| 28 | │ │ Terminal │ │ APDS │ │ Hypercore│ │ |
| 29 | │ │ UI │ │ Protocol │ │ Storage │ │ |
| 30 | │ └──────────────┘ └──────────────┘ └──────────┘ │ |
| 31 | │ │ |
| 32 | │ ┌──────────────┐ ┌──────────────┐ ┌──────────┐ │ |
| 33 | │ │ Ed25519 │ │ Hyperswarm │ │ Corestore│ │ |
| 34 | │ │ Identity │ │ P2P Network │ │ Manager │ │ |
| 35 | │ └──────────────┘ └──────────────┘ └──────────┘ │ |
| 36 | │ │ |
| 37 | └─────────────────────────────────────────────────────┘ |
| 38 | ``` |
| 39 | |
| 40 | ### Data Flow |
| 41 | |
| 42 | 1. **User posts message** |
| 43 | - Sign with Ed25519 (ANProto) |
| 44 | - Store in local Hypercore |
| 45 | - Announce to Hyperswarm DHT |
| 46 | - Replicate to peers |
| 47 | |
| 48 | 2. **User follows author** |
| 49 | - Join Hyperswarm topic for author's pubkey |
| 50 | - Replicate author's Hypercore |
| 51 | - Display messages in terminal UI |
| 52 | |
| 53 | 3. **P2P Replication** |
| 54 | - Hyperswarm discovers peers |
| 55 | - Corestore replicates cores |
| 56 | - No central relay needed |
| 57 | |
| 58 | --- |
| 59 | |
| 60 | ## Differences from Relay |
| 61 | |
| 62 | | Feature | Relay | Pear App | |
| 63 | |---------|-------|----------| |
| 64 | | Runtime | Node.js | Bare (via Pear) | |
| 65 | | Network | HTTP + WebSocket | Hyperswarm P2P | |
| 66 | | Storage | Local Hypercore | Local Hypercore | |
| 67 | | Bridge | Optional to wiredove.net | Pure P2P | |
| 68 | | UI | Web browser | Terminal | |
| 69 | | Discovery | Central server | DHT | |
| 70 | |
| 71 | --- |
| 72 | |
| 73 | ## Implementation Steps |
| 74 | |
| 75 | ### Phase 1: Basic Structure (Day 1) |
| 76 | |
| 77 | **Files to create:** |
| 78 | ``` |
| 79 | wirecore-pear/ |
| 80 | ├── package.json |
| 81 | ├── index.js # Main entry point |
| 82 | ├── lib/ |
| 83 | │ ├── identity.js # Ed25519 keypair management |
| 84 | │ ├── apds.js # APDS protocol (reuse from relay) |
| 85 | │ └── ui.js # Terminal UI (blessed/ink) |
| 86 | └── README.md |
| 87 | ``` |
| 88 | |
| 89 | **Core functionality:** |
| 90 | - [x] Identity generation (Ed25519) |
| 91 | - [ ] Local Hypercore storage |
| 92 | - [ ] Basic APDS message creation |
| 93 | - [ ] Terminal UI for viewing/posting |
| 94 | |
| 95 | ### Phase 2: P2P Networking (Day 2-3) |
| 96 | |
| 97 | **Add:** |
| 98 | - [ ] Hyperswarm integration |
| 99 | - [ ] DHT topic announcement (user's pubkey) |
| 100 | - [ ] Peer discovery |
| 101 | - [ ] Core replication between peers |
| 102 | - [ ] "Follow" command to replicate author cores |
| 103 | |
| 104 | ### Phase 3: Message Protocol (Day 3-4) |
| 105 | |
| 106 | **Implement:** |
| 107 | - [ ] ANProto signature verification |
| 108 | - [ ] YAML message format (like wiredove) |
| 109 | - [ ] Message validation |
| 110 | - [ ] Content-addressable blobs (images) |
| 111 | - [ ] Thread/reply support (previous hash) |
| 112 | |
| 113 | ### Phase 4: UI Polish (Day 4-5) |
| 114 | |
| 115 | **Features:** |
| 116 | - [ ] Message list view (reverse chronological) |
| 117 | - [ ] Author filtering |
| 118 | - [ ] Search functionality |
| 119 | - [ ] Notifications for new messages |
| 120 | - [ ] Status bar (peers, storage size) |
| 121 | |
| 122 | ### Phase 5: Testing & Packaging (Day 5-6) |
| 123 | |
| 124 | **Tasks:** |
| 125 | - [ ] Test with multiple peers |
| 126 | - [ ] Test identity persistence |
| 127 | - [ ] Package with `pear package` |
| 128 | - [ ] Create pear:// link for distribution |
| 129 | - [ ] Write user documentation |
| 130 | |
| 131 | --- |
| 132 | |
| 133 | ## Key Design Decisions |
| 134 | |
| 135 | ### 1. Identity Storage |
| 136 | |
| 137 | **Location**: `~/.wirecore/identity.json` |
| 138 | |
| 139 | ```json |
| 140 | { |
| 141 | "publicKey": "base64...", |
| 142 | "secretKey": "base64...", |
| 143 | "name": "username" |
| 144 | } |
| 145 | ``` |
| 146 | |
| 147 | **Rationale**: Same pattern as wiredove-CLI for consistency. |
| 148 | |
| 149 | ### 2. Hypercore Storage |
| 150 | |
| 151 | **Location**: `~/.wirecore/storage/` |
| 152 | |
| 153 | ``` |
| 154 | ~/.wirecore/storage/ |
| 155 | ├── [core-key-1]/ # Messages core |
| 156 | ├── [core-key-2]/ # Blobs core |
| 157 | └── [core-key-3]/ # Metadata core |
| 158 | ``` |
| 159 | |
| 160 | **Rationale**: Separate from relay storage to avoid conflicts. |
| 161 | |
| 162 | ### 3. Discovery Strategy |
| 163 | |
| 164 | **Primary**: Hyperswarm DHT |
| 165 | - Topic = SHA256(author_pubkey) |
| 166 | - Join topic when following an author |
| 167 | - Announce own topic when posting |
| 168 | |
| 169 | **Fallback**: Known relay servers (optional) |
| 170 | - Can connect to wirecore-relay via WebSocket |
| 171 | - Acts as "super peer" for message discovery |
| 172 | - Not required but helpful for bootstrapping |
| 173 | |
| 174 | ### 4. Terminal UI |
| 175 | |
| 176 | **Library**: `blessed` (proven, used by hyperident) |
| 177 | |
| 178 | **Layout**: |
| 179 | ``` |
| 180 | ┌─ Wirecore ────────────────────────────────────────┐ |
| 181 | │ Peers: 3 | Storage: 2.1MB | Following: 5 │ |
| 182 | ├───────────────────────────────────────────────────┤ |
| 183 | │ [Messages] │ |
| 184 | │ │ |
| 185 | │ @alice... (2 mins ago) │ |
| 186 | │ > Hello from Hypercore! │ |
| 187 | │ │ |
| 188 | │ @bob... (5 mins ago) │ |
| 189 | │ > Testing P2P messaging │ |
| 190 | │ │ |
| 191 | ├───────────────────────────────────────────────────┤ |
| 192 | │ > Type message... (Ctrl+C to exit) │ |
| 193 | └───────────────────────────────────────────────────┘ |
| 194 | ``` |
| 195 | |
| 196 | --- |
| 197 | |
| 198 | ## Code Reuse from Existing Projects |
| 199 | |
| 200 | ### From wirecore-relay: |
| 201 | |
| 202 | ✅ **lib/wirecore-apds.js** - Core APDS logic |
| 203 | - Identity generation |
| 204 | - Message signing/verification |
| 205 | - Blob storage |
| 206 | - Query functions |
| 207 | |
| 208 | **Changes needed**: |
| 209 | - Remove HTTP-specific code |
| 210 | - Add Hyperswarm replication |
| 211 | - Use local config directory |
| 212 | |
| 213 | ### From hyperident: |
| 214 | |
| 215 | ✅ **Terminal UI patterns** |
| 216 | - blessed setup |
| 217 | - Input handling |
| 218 | - Screen layout |
| 219 | |
| 220 | ✅ **Identity management** |
| 221 | - Keypair storage |
| 222 | - Config directory creation |
| 223 | |
| 224 | ### From wiredove-CLI: |
| 225 | |
| 226 | ✅ **Message creation** |
| 227 | - YAML format |
| 228 | - ANProto signing |
| 229 | - Previous hash linking |
| 230 | |
| 231 | --- |
| 232 | |
| 233 | ## Dependencies |
| 234 | |
| 235 | **Essential**: |
| 236 | - `hypercore` - Append-only log |
| 237 | - `hyperbee` - Key-value store |
| 238 | - `corestore` - Multi-core manager |
| 239 | - `hyperswarm` - P2P networking |
| 240 | - `hypercore-crypto` - Ed25519 keys |
| 241 | - `tweetnacl` - Signature verification |
| 242 | - `blessed` - Terminal UI |
| 243 | |
| 244 | **Testing**: |
| 245 | - `brittle` - Test framework (holepunchto standard) |
| 246 | |
| 247 | --- |
| 248 | |
| 249 | ## Commands |
| 250 | |
| 251 | ```bash |
| 252 | # Initial setup |
| 253 | pear run -d . # Run in dev mode |
| 254 | wirecore init [name] # Create identity |
| 255 | wirecore whoami # Show identity |
| 256 | |
| 257 | # Posting |
| 258 | wirecore post "message" # Post message |
| 259 | wirecore post --image path.png # Post with image |
| 260 | |
| 261 | # Following |
| 262 | wirecore follow <pubkey> # Follow author |
| 263 | wirecore unfollow <pubkey> # Unfollow |
| 264 | wirecore following # List followed authors |
| 265 | |
| 266 | # Viewing |
| 267 | wirecore timeline # Show all messages |
| 268 | wirecore timeline @author # Filter by author |
| 269 | wirecore search "query" # Search messages |
| 270 | |
| 271 | # Network |
| 272 | wirecore peers # Show connected peers |
| 273 | wirecore sync # Force sync with peers |
| 274 | wirecore relay add <url> # Add relay server (optional) |
| 275 | ``` |
| 276 | |
| 277 | --- |
| 278 | |
| 279 | ## Security Considerations |
| 280 | |
| 281 | 1. **No plaintext private keys** |
| 282 | - Store in `~/.wirecore/` with 600 permissions |
| 283 | - Never log or transmit private keys |
| 284 | |
| 285 | 2. **Signature verification required** |
| 286 | - Always verify ANProto signatures |
| 287 | - Reject unsigned messages |
| 288 | - Validate timestamp ranges |
| 289 | |
| 290 | 3. **Blob size limits** |
| 291 | - Max 1MB per message |
| 292 | - Max 10MB per image |
| 293 | - Prevent DoS via large blobs |
| 294 | |
| 295 | 4. **DHT poisoning protection** |
| 296 | - Validate core keys match expected format |
| 297 | - Don't replicate untrusted cores |
| 298 | - Rate limit new peer connections |
| 299 | |
| 300 | --- |
| 301 | |
| 302 | ## Testing Plan |
| 303 | |
| 304 | ### Unit Tests |
| 305 | |
| 306 | ```bash |
| 307 | brittle test/identity.test.js # Identity creation/loading |
| 308 | brittle test/apds.test.js # APDS protocol functions |
| 309 | brittle test/signing.test.js # ANProto signing/verification |
| 310 | ``` |
| 311 | |
| 312 | ### Integration Tests |
| 313 | |
| 314 | ```bash |
| 315 | brittle test/integration/two-peers.test.js # 2-peer message exchange |
| 316 | brittle test/integration/follow.test.js # Follow/unfollow flow |
| 317 | brittle test/integration/replication.test.js # Core replication |
| 318 | ``` |
| 319 | |
| 320 | ### Manual Testing |
| 321 | |
| 322 | 1. Run 2 instances in separate terminals |
| 323 | 2. Post from instance A |
| 324 | 3. Verify message appears in instance B |
| 325 | 4. Test follow/unfollow |
| 326 | 5. Test offline -> online sync |
| 327 | |
| 328 | --- |
| 329 | |
| 330 | ## Deployment |
| 331 | |
| 332 | ### Pear Packaging |
| 333 | |
| 334 | ```bash |
| 335 | # Build |
| 336 | pear package |
| 337 | |
| 338 | # Test packaged version |
| 339 | pear run pear://[key] |
| 340 | |
| 341 | # Publish (when ready) |
| 342 | pear publish |
| 343 | ``` |
| 344 | |
| 345 | ### Distribution |
| 346 | |
| 347 | **Link format**: `pear://[64-char-hex-key]` |
| 348 | |
| 349 | **Installation**: |
| 350 | ```bash |
| 351 | pear install pear://[key] |
| 352 | wirecore init myname |
| 353 | ``` |
| 354 | |
| 355 | --- |
| 356 | |
| 357 | ## Migration from Relay |
| 358 | |
| 359 | Users can transition from relay to Pear app: |
| 360 | |
| 361 | 1. **Export from relay**: |
| 362 | ```bash |
| 363 | curl http://localhost:9000/all > messages.json |
| 364 | ``` |
| 365 | |
| 366 | 2. **Import to Pear app**: |
| 367 | ```bash |
| 368 | wirecore import messages.json |
| 369 | ``` |
| 370 | |
| 371 | 3. **Verify**: |
| 372 | ```bash |
| 373 | wirecore timeline |
| 374 | ``` |
| 375 | |
| 376 | --- |
| 377 | |
| 378 | ## Future Enhancements |
| 379 | |
| 380 | **P2P Extensions**: |
| 381 | - [ ] Encrypted direct messages (1-on-1) |
| 382 | - [ ] Group messaging (shared cores) |
| 383 | - [ ] File attachments (larger files) |
| 384 | - [ ] Voice notes (audio blobs) |
| 385 | |
| 386 | **Network Improvements**: |
| 387 | - [ ] Bridge to ActivityPub/Fediverse |
| 388 | - [ ] Mobile app (React Native + Hypercore) |
| 389 | - [ ] Web interface (Pear web runtime) |
| 390 | |
| 391 | **Discovery**: |
| 392 | - [ ] Username registry (DHT + Hyperbee) |
| 393 | - [ ] Discovery feed (curated authors) |
| 394 | - [ ] Hashtag indexing |
| 395 | |
| 396 | --- |
| 397 | |
| 398 | ## Next Steps |
| 399 | |
| 400 | 1. ✅ Create this plan |
| 401 | 2. [ ] Set up wirecore-pear/ directory |
| 402 | 3. [ ] Create package.json with dependencies |
| 403 | 4. [ ] Copy lib/wirecore-apds.js from relay |
| 404 | 5. [ ] Implement basic identity management |
| 405 | 6. [ ] Build terminal UI skeleton |
| 406 | 7. [ ] Add Hyperswarm integration |
| 407 | 8. [ ] Test 2-peer communication |
| 408 | |
| 409 | --- |
| 410 | |
| 411 | **References**: |
| 412 | - hyperident project (terminal UI patterns) |
| 413 | - wirecore-relay (APDS protocol) |
| 414 | - wiredove-CLI (message format) |
| 415 | - Pear deployment lessons (pear-deployment-lessons.md) |
| 416 | |
| 417 | --- |
| 418 | |
| 419 | Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com> |