summaryrefslogtreecommitdiffstats
path: root/docs/meshbay-draft-v4.md
blob: fe6a521bfb831dde62f46d222f014c400d29c8b3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
# MeshBay — Architecture Draft v4

> Status: active development — Phases 1–10 complete (except 10.9 → Phase 13), 155 tests.
> Changes from v3: web client transport (WebRTC DataChannel), web UI architecture, hub roles (admin/moderator), hub mirror design, browser-specific NAT traversal, chat storage clarified, Phase 8 security items resolved, Phase 10 site overlay + admin/moderation UI + notifications + group search + version endpoint.

---

## Changes from v3

The following items are **architectural decisions** driven by Phase 8 implementation and web client design (2026-08-10). They supersede the corresponding text in v3.

| # | Category | What changed | Source |
|---|---|---|---|
| 1 | Browser transport | Web browsers use **WebRTC DataChannel** (with ICE/STUN) for P2P to nodes behind NAT. WebTransport cannot work because browsers cannot choose their UDP source port — Port-Restricted Cone NAT requires exact port matching. Native clients (desktop, Android) continue using QUIC with `punch_nat()`. | Web client design session |
| 2 | Hub signaling | Hub WebSocket extended to relay WebRTC signaling (SDP/ICE) between browser and node. <1 KB per message, stateless, no content. Same channel as jti denylist push and `client_incoming`. | Web client design session |
| 3 | Hub role | Reinforced: hub is registrar + signaling facilitator ONLY. Never proxies, stores, or touches content (files, streams, chat, indexes). All data lives on nodes. Clients connect E2E to nodes. | Design constraint |
| 4 | Chat storage | Chat messages stored on node(s) hosting the group, not on the hub. Browser retrieves chat from node via DataChannel. If no node is online, group is unavailable. | Web client design session |
| 5 | Web UI | Preact SPA (~3 KB gzipped), dark/light theme, responsive, i18n (JSON translations). ESM modules, esbuild for minification. No heavy frameworks. | Web client design session |
| 6 | Hub roles | Three roles: `user`, `moderator`, `admin`. Moderator can review reports and suspend content/groups/users. Admin has full hub management. | Web client design session |
| 7 | Site overlay | meshbay.org serves both generic hub functionality and site-specific pages (landing, /downloads, /about). Separated via Caddy static file priority. | Web client design session |
| 8 | Hub mirror | Design defined (future implementation): active-active with shared signing key, PostgreSQL logical replication, DNS round-robin. Not implemented yet. | Web client design session |
| 9 | Security items | S1 (admin authz), S2 (email encryption), S5 (refresh token rotation) resolved in Phase 8. Argon2id bumped to 256 MB with transparent rehash. | Phase 8 implementation |
| 10 | File search | Client-side search on cached indexes (IndexedDB). No hub involvement. Private group indexes are GEK-encrypted — hub stores opaque, client decrypts locally. | Web client design session |

---

## Changes from v2

The following items are **mandatory corrections** driven by POC findings (spikes 1–6). They supersede the corresponding text in v2.

| # | Category | What changed | Source |
|---|---|---|---|
| 1 | JWT | `jti` (UUID4) is now **required** in every access token — prevents replay and enables individual revocation. Without it, two tokens issued in the same second are bit-for-bit identical (Ed25519 is deterministic). | Spike 3 |
| 2 | Argon2id | Parameters updated: `iterations=4`, `memory_cost=262144` (256 MB). Previous params (iterations=3, 64 MB) gave 78 ms — too fast. Target is 500 ms on a home server. CLI calibration command added. | Spike 1 |
| 3 | NAT traversal | Order corrected: IPv6 → **STUN/hole-punching** → UPnP → TURN relay. UPnP moved to step 3 (disabled on tested SFR box). STUN is now priority 2, not UPnP. | Spike 4 |
| 4 | Transport | TCP + TLS 1.3 is now the **v1 implementation**. QUIC is the v2 target. The v2 architecture doc had this reversed (QUIC primary, TCP fallback). A `Transport` abstraction layer ensures the switch requires no protocol-layer changes. | Spike 5 |
| 5 | GEK wrapping | Exact protocol confirmed: ephemeral X25519 + `HKDF(salt=pk_eph, info="meshbay:gek_wrap:v1")` + `ChaCha20-Poly1305(aad=pk_recipient)`. Hub stores opaque 48-byte blobs. | Spike 6 |
| 6 | Hub API | Four new endpoints validated in Spike 6: `GET /v1/users/{username}/pubkeys`, `POST /v1/groups`, `POST /v1/groups/{group_id}/members/{username}/gek`, `GET /v1/groups/{group_id}/gek`. Full table added as §4.1.5. | Spike 6 |
| 7 | Packages | Repository structure decided: 3 packages (`meshbay-common`, `meshbay-hub`, `meshbay-node`) in a uv workspace monorepo. RPM package names defined. | POC structure |
| 8 | Key persistence | X25519 keypairs **must be persisted** client-side before the first hub contact. Lesson from Spike 6 (`bob_state.json` fix). | Spike 6 |

---

## 1. Project Overview

MeshBay is a decentralized, peer-to-peer platform for file sharing, video streaming, and group messaging. It combines identity federation (via Mesh Hubs) with truly peer-to-peer data exchange (via Mesh Nodes), designed to be resilient, censorship-resistant, and user-friendly.

**Core principles:**
- Data never transits through a central server — only identity and routing do
- End-to-end encryption for all private content (files, indexes, messages)
- The node operator is the legal host and is fully responsible for their content
- The hub is a lightweight registrar, not a content host or indexer
- Open source, self-hostable at every level

**Domain:** meshbay.org (configurable at compile/deploy time throughout the codebase)

---

## 2. Terminology

| Term | Role |
|---|---|
| **Mesh Hub** | Identity authority and group registry server |
| **Mesh Node** | Local program on the host user's machine |
| **Mesh Client** | Web browser or Android app (end user) |
| **Mesh Relay** | Community-operated TURN fallback relay [future] |
| **MNP** | Mesh Node Protocol — P2P protocol between nodes and clients |
| **MHP** | Mesh Bay Hub Protocol — inter-hub federation protocol |
| **GEK** | Group Encryption Key — symmetric key for private group content |
| **Mesh Directory** | Public registry of groups (hub level) |
| **Mesh Group Index** | File listing for a group (node level, encrypted for private groups) |

---

## 3. Protocol Versioning

All protocols (MNP, MHP, hub REST API) carry explicit version information.

**Format:** `MAJOR.MINOR`
- MAJOR bump: breaking change, backward incompatible
- MINOR bump: backward-compatible addition

**Negotiation:** during handshake, both parties declare their supported version range. The highest mutually supported MINOR within the same MAJOR is used. If no common version exists, connection is refused with an explicit error.

**Support policy:** a release supports the current MAJOR and at least the two previous MINOR versions (N-2).

**Implementation:** a `version` field in every msgpack message header. Handshake step precedes all other exchanges.

---

## 4. System Components

### 4.1 Mesh Hub

A lightweight server acting as a registrar. Intentionally minimal to limit legal exposure and operational cost.

**What the hub stores:**
- User accounts: username, email (stored for account recovery — see §4.1.1), optional phone number, `PK_user` (Ed25519 + X25519), hub ID, status, creation timestamp
- Group registry: name, `PK_group`, hosting node identifier, visibility, join policy, member list with encrypted GEK bundles (private groups only)
- Mandatory connection logs (see §4.1.2)
- Revocation lists (users and groups)
- Registered peer hubs (explicit allowlist — no auto-discovery)

**What the hub never stores:**
- File content or metadata
- Private group indexes
- Message content
- Node current IP (handled by ephemeral signaling — see §4.1.3)

#### 4.1.1 Account Data

Email is kept in full (not hashed) to support:
- Account recovery (password reset)
- Legal notifications
- Abuse contact

Phone number: optional, associable after account creation. On Android, both collected at registration. Accounts are fusionable (email + phone pointing to same account).

Email and phone are stored encrypted at rest in the database, using a server-side key derived from the hub's configuration secret (not the database). **[NOT YET IMPLEMENTED — currently stored in plaintext. Tracked as open question #10.]**

#### 4.1.2 Mandatory IP Logging (Legal Compliance)

Legal frameworks (LCEN in France, EU e-Commerce Directive, DSA) require service providers to retain connection logs. The hub logs the following with timestamp and IP address:

| Event | Retention |
|---|---|
| Account creation | 1 year minimum |
| Login (success and failure) | 1 year minimum |
| Group creation | 1 year minimum |
| Group join / leave | 1 year minimum |
| Group deletion | 1 year minimum |
| Revocation actions | 1 year minimum |

Logs are stored in a separate, access-controlled log table. They are not used for any purpose other than legal compliance and are not exposed to users or operators beyond legal requests.

#### 4.1.3 Signaling Service

NAT traversal coordination is handled by a lightweight WebSocket endpoint, logically separate from the main hub API. It is stateless: connection state is held in memory only and discarded after P2P connection establishment (typically within seconds). No persistent storage of node IP addresses.

**Hub interaction summary:**

| Event | Hub crypto load | Frequency |
|---|---|---|
| Account creation | Argon2 hash, store PK | Once |
| Login | Verify password, issue JWT (Ed25519 sign) | Per session |
| Group creation | Register metadata | Once per group |
| Member add/remove | Store/remove GEK bundle | On admin action |
| Group discovery | Return node address + PK_node + GEK bundle | Per initial access |
| NAT signaling | Relay WebSocket messages (<1 KB) | Per new P2P connection |
| Public search | Delegate to nodes, 60s in-memory cache | On demand |
| MHP federation sync | Exchange Mesh Directory | Background, periodic |
| Revocation | Ed25519-sign revocation token | Rare |

**The hub is never in the data path after connection setup. JWT verification by nodes is local (Ed25519, no hub roundtrip). Confirmed at 884 µs in Spike 3.**

#### 4.1.4 JWT Strategy

Two tokens issued at login:

**Access token** (JWT, signed Ed25519):
- Validity: 1 hour
- Payload: `jti` (UUID4, **mandatory** — unique per token, enables individual revocation and prevents replay), `user_id`, `PK_user`, `PK_user_x25519`, `hub_id`, `issued_at`, `expires_at`, `groups` (list of group_ids the user is a member of — hub-signed membership claim)
- The `groups` claim is **mandatory** for node-side authorization: the node checks that the requested group_id appears in the JWT before serving any content. Without this claim, any authenticated user could access any group on the node.
- Presented to nodes for authentication and group access verification
- Verified locally by nodes using the hub's known public key — no hub roundtrip
- Compromise window: 1 hour maximum

> **Why `jti` is mandatory:** Ed25519 signing is deterministic. Two tokens with identical payloads issued within the same second produce the same byte sequence. Without a `jti`, they are indistinguishable — a captured token is replayable forever within its validity window, and individual revocation is impossible. The `jti` also provides the revocation handle: hub stores `jti` of invalidated tokens in a server-side denylist.
>
> This bug was found and fixed during Spike 3.

**Refresh token** (opaque, random 256-bit):
- Validity: 30–90 days [TBD exact duration]
- Stored securely on client only
- Used exclusively with the hub to obtain a new access token
- Revocable immediately by the hub (invalidates all future refreshes for this token)
- Stored server-side as a hashed value

**Revocation flow:** hub invalidates the refresh token → next access token renewal fails → node access expires within 1 hour at most. For immediate revocation of an active access token: hub adds its `jti` to the token denylist; nodes that cache hub public key will periodically fetch the denylist.

**Tech stack:**
- Language: Python
- Framework: FastAPI + Uvicorn
- Database: PostgreSQL + SQLAlchemy + Alembic
- Deployment: Apache reverse proxy (ProxyPass + SSL termination)
- Authentication: own system (Ed25519 JWT, Argon2id for password hashing)
- Hub accessible via domain and directly by IP (self-signed cert warning expected for IP access; documented)

#### 4.1.5 Hub API Reference

Complete table of validated and planned hub REST API endpoints. Endpoints marked ✓ were validated in the POC; endpoints marked [TBD] are designed but not yet implemented.

**Hub metadata:**

| Method | Path | Auth | Description | Status |
|---|---|---|---|---|
| GET | `/v1/hub/info` | None | Hub metadata: hub_id, versions, counters | ✓ Spike 2 |
| GET | `/v1/hub/pubkey` | None | Hub Ed25519 public key (PEM) | ✓ Spike 2 |

**User management:**

| Method | Path | Auth | Description | Status |
|---|---|---|---|---|
| POST | `/v1/users/register` | None | Create user account (username, email, password, pk_ed25519, pk_x25519) | ✓ Spike 2 |
| POST | `/v1/users/login` | None | Authenticate; returns access token + refresh token | ✓ Spike 2 |
| POST | `/v1/users/token/refresh` | Refresh token | Issue new access token | ✓ Spike 2 |
| GET | `/v1/users/{username}/pubkeys` | Access token | Fetch `pk_ed25519` and `pk_x25519` for a user (used for GEK wrapping) | ✓ Spike 6 |

**Node management:**

| Method | Path | Auth | Description | Status |
|---|---|---|---|---|
| POST | `/v1/nodes/announce` | Access token | Register node with endpoint_hint; returns node_id | ✓ Spike 2 |
| GET | `/v1/nodes/{node_id}` | Access token | Retrieve node record (pk_node, endpoint_hint) | ✓ Spike 2 |

**Group management:**

| Method | Path | Auth | Description | Status |
|---|---|---|---|---|
| POST | `/v1/groups` | Access token | Create group (name, visibility, join_policy, pk_group) | ✓ Spike 6 |
| GET | `/v1/groups` | None / Access token | List/search public groups; private groups require membership | [TBD] |
| GET | `/v1/groups/{group_id}` | None / Access token | Group metadata | [TBD] |
| DELETE | `/v1/groups/{group_id}` | Access token (admin) | Revoke group | [TBD] |

**GEK distribution (private groups):**

| Method | Path | Auth | Description | Status |
|---|---|---|---|---|
| POST | `/v1/groups/{group_id}/members/{username}/gek` | Access token (admin) | Upload opaque 48-byte GEK bundle for a member | ✓ Spike 6 |
| GET | `/v1/groups/{group_id}/gek` | Access token (member) | Retrieve caller's GEK bundle | ✓ Spike 6 |

**Revocation:**

| Method | Path | Auth | Description | Status |
|---|---|---|---|---|
| POST | `/v1/admin/revoke` | Access token (admin) | Revoke a user or group | ✓ Phase 8 |
| GET | `/v1/revoke/denylist` | None | Current `jti` denylist for active access tokens | [TBD] |

**Admin / moderation:**

| Method | Path | Auth | Description | Status |
|---|---|---|---|---|
| GET | `/v1/users/me` | Access token | Current user info (id, username, role, status) | ✓ Phase 10 |
| GET | `/v1/admin/stats` | Moderator+ | Hub stats (user/group/node counts, online nodes) | ✓ Phase 10 |
| GET | `/v1/admin/users` | Moderator+ | List users (paginated, searchable by username) | ✓ Phase 10 |
| GET | `/v1/admin/users/{user_id}` | Moderator+ | User detail (email, role, status, group count) | ✓ Phase 10 |
| PATCH | `/v1/admin/users/{user_id}` | Moderator+ | Update user role or status | ✓ Phase 10 |
| GET | `/v1/admin/groups` | Moderator+ | List all groups with member count | ✓ Phase 10 |
| PATCH | `/v1/admin/groups/{group_id}` | Moderator+ | Update group status | ✓ Phase 10 |
| GET | `/v1/admin/logs` | Moderator+ | IP audit logs (filterable by event, user_id) | ✓ Phase 10 |
| GET | `/v1/admin/blocklist` | Admin | List blocked content hashes | ✓ Phase 8 |
| POST | `/v1/admin/blocklist` | Admin | Manually block a content hash | ✓ Phase 8 |
| DELETE | `/v1/admin/blocklist/{hash}` | Admin | Unblock a content hash | ✓ Phase 8 |
| GET | `/v1/notifications` | Access token | List notifications (unread_only, paginated) | ✓ Phase 10 |
| POST | `/v1/notifications/{id}/read` | Access token | Mark notification as read | ✓ Phase 10 |
| POST | `/v1/notifications/read-all` | Access token | Mark all notifications as read | ✓ Phase 10 |
| GET | `/v1/groups?q=` | None | Search public groups by name (ilike) | ✓ Phase 10 |
| GET | `/v1/hub/version` | None | Client version check (hub, MNP, MHP) | ✓ Phase 10 |

### 4.2 Mesh Node

A local program running on the host user's machine. The node is the actual host of all content.

**Responsibilities:**
- Watch and index shared directories (Mesh Group Index) — one directory per group
- Serve files, video streams, and group chat to members
- Manage all cryptographic keys locally (encrypted keystore)
- Handle P2P connections and NAT traversal (STUN + QUIC hole punching)
- Run the MNP protocol (QUIC v2, TCP+TLS v1)
- Host the Python extension module sandbox
- Serve the local web UI (localhost:18000)

**Multi-group architecture (decided Phase 7):**
A node exposes **one QUIC port** for all groups it hosts. Groups are not isolated
by port — the MNP handshake identifies the target group via the `group_id` claim
in the client JWT. The server routes each connection to the appropriate
DirectoryIndexer and GEK after JWT verification.
Rationale: one NAT hole to maintain, one port to forward manually if needed.

**Authorization invariant:** the node MUST verify that the JWT's `groups` claim
contains the requested group_id before serving any content. Without this check,
any authenticated user could access any group on the node. This is enforced at
the MNP handshake layer, not the transport layer.

**Platform:** Linux primary, cross-platform from the start (Windows/macOS). Python ensures portability.

#### 4.2.1 Keystore and Unlock

Private keys (user identity Ed25519, user exchange X25519, group identity Ed25519, GEK copies) are stored in a local encrypted keystore file.

**Format:** msgpack container encrypted with AES-256-GCM, key derived from master password using Argon2id.

**Argon2id parameters (production):**
- `iterations = 4`
- `memory_cost = 262144` (256 MB)
- `parallelism = 1` (or match CPU count — tune to target hardware)
- Target derivation time: ~500 ms on a home server

> **Why these parameters:** Spike 1 measured iterations=3, memory=64 MB at 78 ms — far too fast. At 78 ms an attacker can attempt millions of guesses per second-equivalent with a GPU cluster. The target of 500 ms on a home server limits offline dictionary attacks to a tractable rate while remaining acceptable for a node that unlocks once at startup.

**CLI calibration:**
```
meshbay-node --calibrate-argon2
```
This command iterates through parameter combinations and reports the derivation time on the current hardware. The operator selects parameters meeting the 500 ms target and stores them in `~/.config/meshbay/node.toml`. Recommended starting point: `iterations=4, memory_cost=262144`.

**Key persistence requirement:** All keypairs (Ed25519 + X25519) **must be written to the keystore before the first hub contact.** If keypairs are generated at registration time but not persisted before the hub call, subsequent runs will regenerate different keypairs, making all stored GEK bundles on the hub undecryptable. This was identified as a real failure mode in Spike 6 (`bob_state.json` fix).

**Three unlock modes:**

| Mode | How it works | Security level |
|---|---|---|
| **Secure (default)** | Password prompted at startup via terminal or local web UI | High |
| **Lazy file** | Password or derived key stored in `~/.config/meshbay/unlock.key` (chmod 600), read automatically at startup | Medium — acceptable for physically secure home machines. Risk documented at setup. |
| **Service (headless)** | `MESHBAY_UNLOCK_KEY` environment variable, set via systemd `EnvironmentFile=` pointing to a chmod 600 file | Medium-high — standard practice for server deployments |

Future: OS keychain integration (libsecret/GNOME Keyring on Linux, Windows Credential Manager, macOS Keychain).

#### 4.2.2 Hardware Sizing

The main constraint is **upload bandwidth**, not CPU or RAM.

| Scenario | Simultaneous users | Upload needed | CPU | RAM |
|---|---|---|---|---|
| Files + chat, minimal streaming | 10 | 20–50 Mbps | 2 cores | 512 MB |
| Active 1080p streaming (5–6 streams) | 10 | 50–80 Mbps | 2–4 cores | 1 GB |
| Mixed use | 50 | 200–300 Mbps | 4 cores | 2 GB |
| Active streaming | 50 | 400 Mbps | 4–8 cores | 2–4 GB |
| All use cases | 100 | 800 Mbps–1 Gbps | 8 cores | 4–8 GB |

Beyond 20–30 active streaming users, a dedicated server is required. A home fiber connection (100–500 Mbps symmetric) is suitable for small groups.

Crypto overhead is confirmed negligible: Spike 5 measured full encrypt+sign and verify+decrypt at under 10 ms for a 1 MB chunk. Network latency dominates.

**Tech stack:**
- Language: Python (primary). Rust extension only if a specific hot path proves insufficient.
- Transport abstraction layer: `Transport` interface decouples TCP+TLS 1.3 (v1) from QUIC (v2). Application protocol is identical across both transports.
- v1 transport: **TCP + TLS 1.3** (`asyncio` + `ssl` module, standard library)
- v2 transport (future): **QUIC** (`aioquic`, Cloudflare-maintained)
- ICE/STUN: `aioice` (already a dependency)
- WebRTC: `aiortc` (browser P2P transport — Phase 9)
- Crypto: `cryptography` (PyCA, OpenSSL-backed, hardware-accelerated AES-NI/ChaCha)
- Serialization: `msgpack`
- Compression: `zstandard` (zstd)
- File watching: `watchdog`
- Local DB: SQLite
- Local web UI: served by node on `localhost:18000`

### 4.3 Mesh Client

Web browser or Android app. Consumes content from nodes; manages account via hub.
The hub is never in the data path — clients connect E2E to nodes for all content.

**Hub-side operations (HTTPS, lightweight):**
- Account creation, login, token refresh
- Public group search and discovery
- Group membership management, GEK bundle retrieval
- WebRTC signaling relay (SDP/ICE — <1 KB per connection, stateless)
- Notification metadata (invitations, new content indicators)

**Node-side operations (direct P2P via QUIC or WebRTC DataChannel):**
- File browsing via Mesh Group Index
- File download (chunked, E2E encrypted)
- Video streaming (HLS segments via DataChannel or QUIC stream)
- Group chat (Sender Keys encrypted, stored on node)
- File/photo/video upload (client → node push)

#### 4.3.1 Web Browser Client

**Transport:** WebRTC DataChannel with ICE/STUN for NAT traversal.
WebTransport (HTTP/3) is not suitable because browsers cannot choose their UDP
source port — Port-Restricted Cone NAT (confirmed on SFR residential) requires
the client to connect from the exact port the node probed. WebRTC's ICE handles
this automatically via simultaneous STUN binding requests.

**UI:** Preact SPA (~3 KB gzipped) served by the hub.
- Dark/light theme (CSS `prefers-color-scheme` + user toggle in localStorage)
- Responsive design (sidebar → hamburger menu on mobile)
- i18n: JSON translation files, English default
- Build: esbuild for minification (single binary, no npm dependency)
- Crypto: SubtleCrypto (AES-GCM) for E2E decryption in browser

**Layout:**
- Left sidebar: group list (ordered by usage — private groups first), navigation
- Top bar: logo ("MeshBay") left, user menu right (settings, profile, language, logout)
- Main content area: file explorer, chat view, or settings depending on context

**Client modes:**
- Explorer: file/folder browser for group content (read-only browse, download, stream)
- Chat/forum: per-group discussion thread with photo/video posting
- Settings: general, per-group, notifications, privacy, theme, language

**Local storage:**
- IndexedDB: cached group indexes for instant local search (~50–100 MB quota)
- localStorage: theme preference, language, session state
- `keypair_bundle`: encrypted keypair retrieved from hub, decrypted locally with password

**File search:** entirely client-side on cached indexes. No hub involvement.
Private group indexes are GEK-encrypted — stored opaque on the hub, decrypted
by the client locally. Search runs against the decrypted index in IndexedDB.

#### 4.3.2 Android Client

**Transport:** QUIC with `punch_nat()` — same as desktop native clients.
Android has full UDP access; no WebRTC needed. Uses `quiche` (Cloudflare, Rust
via JNI) for QUIC transport.

**Stack:** Kotlin + Jetpack Compose. Bouncy Castle JVM for crypto.

**Capabilities:** same as web browser (browse, download, stream, chat, upload).
Additional: contact list integration (Android Contacts API, permission-gated).
Account creation from app. No node functionality on mobile (client-only).

**Cross-device compatibility:** the `keypair_bundle` (encrypted, stored on hub)
enables seamless switching between web and Android with the same credentials.
Notification state and read markers sync via hub (small encrypted blob per user).

**Out of scope:** Mac/iPhone support. Node on mobile.

### 4.4 Mesh Relay

**[Future feature]** Community-operated TURN relay. Used only as last-resort fallback when all P2P connection methods fail (symmetric NAT behind CGNAT, approximately 15–20% of connections in the worst case). Traffic is always E2E encrypted — the relay sees only opaque ciphertext.

Not operated by meshbay.org. A relay registration protocol (hub-mediated) will be designed when this feature is introduced. It does not affect the current design.

### 4.5 Package Structure and Repository

**Repository layout:** monorepo managed with [uv workspaces](https://docs.astral.sh/uv/concepts/workspaces/).

```
meshbay/
├── packages/
│   ├── meshbay-common/       # Shared crypto, serialization, protocol types
│   ├── meshbay-hub/          # Hub server (FastAPI + Uvicorn)
│   └── meshbay-node/         # Node daemon + local web UI
├── poc/                      # POC and spikes — reference implementation
│   ├── spike1_crypto/
│   ├── spike2_hub/
│   ├── spike3_node_reg/
│   ├── spike4_nat/
│   ├── spike5_transfer/
│   ├── spike6_gek/
│   └── spike-results.md
├── docs/
│   └── meshbay-draft-v3.md
└── pyproject.toml            # Workspace root
```

**Three packages:**

| Package | RPM name | Contents |
|---|---|---|
| `meshbay-common` | `python3-meshbay-common` | Crypto primitives (Ed25519, X25519, ChaCha20, Argon2, HKDF), msgpack schemas, protocol constants, MNP message types |
| `meshbay-hub` | `python3-meshbay-hub` | FastAPI hub application, database models (SQLAlchemy), Alembic migrations, JWT issuance, GEK bundle storage |
| `meshbay-node` | `python3-meshbay-node` | Node daemon, keystore, file watcher, TCP+TLS transport, local web UI, extension module sandbox |

**`meshbay-hub` and `meshbay-node` both depend on `meshbay-common`.** There is no runtime dependency between hub and node packages.

**POC directory as reference implementation:** The `poc/` directory contains the working code from spikes 1–6. It is not production code and not packaged, but serves as the canonical reference for:
- Exact crypto parameter choices (Spike 1)
- GEK wrapping/unwrapping implementation (Spike 6)
- Hub API skeleton (Spike 2)
- NAT detection and STUN interaction (Spike 4)
- TCP file transfer pipeline (Spike 5)

Developers implementing production features should read the corresponding spike before writing production code.

---

## 5. Group Model

Groups are the core organizational unit.

| Parameter | Options |
|---|---|
| Visibility | Public / Private |
| Join policy | Open / On request / By invitation only |
| Admin | The hosting node operator (legal host) |

A public group functions like a themed forum: files, chat thread, member list. Join policy is independent of visibility (a public group can require approval to join).

A private group's content (files, index, messages) is always E2E encrypted with the GEK. Only members holding the GEK can decrypt anything.

**Group addressing:**
```
meshbay.org/u/username/groupname    — public group via hub
meshbay.org/g/groupname             — public group (shorthand)
group://<PK_group_fingerprint>@<node_addr>  — hub-less direct access
```
`meshbay.org` is fully configurable throughout the codebase (constant/config file). The hub is reachable via domain or IP (IP access requires self-signed cert; browsers will warn — expected and documented behavior).

---

## 6. Cryptographic Architecture

### 6.1 Key Hierarchy

```
User Identity Key      Ed25519     Signing, authentication
User Exchange Key      X25519      Key agreement (GEK wrapping, session ECDH)
Group Identity Key     Ed25519     Group metadata signing (held by admin node)
Group Encryption Key   ChaCha20    Private content and index encryption (symmetric, 256-bit)
Session Keys           X25519/HKDF Perfect forward secrecy per P2P connection
```

All private keys stored exclusively on the node (or client device) in the encrypted keystore. The hub never sees any private key.

Both `PK_ed25519` and `PK_x25519` are registered with the hub at account creation. The hub exposes them via `GET /v1/users/{username}/pubkeys` so that group admins can wrap GEK bundles for members without any direct contact between nodes.

### 6.1.1 Key Generation Strategies

Three strategies, depending on client type:

**A — CLI / native node (Argon2id derivation)**
Keys are derived deterministically from `username + password`:
```
salt    = SHA-256("meshbay:v1:" + username)
seed    = Argon2id(password, salt, length=64)
sk_ed25519 = Ed25519.from_private_bytes(seed[:32])
sk_x25519  = X25519.from_private_bytes(seed[32:])
```
Same credentials → same keys on any machine. Password recovery = key recovery.
Implemented in `meshbay_common/keyderive.py::derive_keys_from_password()`.

**B — Web browser (random keypairs + encrypted bundle)**
Browser generates random keypairs via WebCrypto `generateKey()`, encrypts them
with a PBKDF2-SHA512 derived key, and uploads the encrypted bundle to the hub
alongside the public keys. On subsequent logins, the hub returns the bundle
and the browser decrypts it locally with the password.

The hub stores `keypair_bundle` (AES-256-GCM ciphertext) — opaque, cannot decrypt it.
Implemented in `static/keyderive.js`. Python side in `keyderive.py::encrypt_keypair_bundle()`.

**C — Native node with keystore file**
Random keypairs generated once, stored in the Argon2id-encrypted keystore file
(`~/.config/meshbay/keystore.enc`). Standard operating mode for `meshbay-node`.

**Algorithm mismatch note:** strategies A and B use different KDFs (Argon2id vs PBKDF2).
A user who registered via CLI (A) and later tries to recover via web (B) with the same
password will get different keypairs. This is by design: users pick one registration path.
Cross-path recovery requires the admin to issue new GEK bundles.

### 6.2 GEK Management

**Scope:** GEK applies to private groups only. Public groups use TLS transport only (no application-layer encryption).

**GEK wrapping protocol (ECIES-like, confirmed in Spike 6):**

```
Admin side (wrap_gek):
  sk_eph, pk_eph  = X25519.generate()                        # fresh ephemeral keypair per bundle
  shared          = X25519(sk_eph, pk_recipient)
  wrap_key        = HKDF(shared, salt=pk_eph,
                         info="meshbay:gek_wrap:v1",
                         length=32)
  nonce           = random_bytes(12)
  wrapped         = ChaCha20-Poly1305(wrap_key).encrypt(
                         nonce, gek, aad=pk_recipient)        # aad binds bundle to recipient
  bundle          = pk_eph || nonce || wrapped                # 32 + 12 + 32+16 = 92 bytes on wire
                                                              # hub stores as opaque 48-byte blob
                                                              # (without pk_eph in compact form — see note)

Member side (unwrap_gek):
  shared          = X25519(sk_recipient, pk_eph)
  wrap_key        = HKDF(shared, salt=pk_eph,
                         info="meshbay:gek_wrap:v1",
                         length=32)
  gek             = ChaCha20-Poly1305(wrap_key).decrypt(
                         nonce, wrapped, aad=pk_recipient)
```

> **Hub-stored blob size:** the hub stores the opaque bundle. Spike 6 confirmed the hub stores 48-byte blobs (nonce=12 + ciphertext=20 + tag=16 in the compact wire format used in the spike — `pk_eph` is stored separately in the bundle record). Production schema: hub bundle record = `{ pk_eph (32B), nonce (12B), ciphertext (32B), tag (16B) }` = 92 bytes total per member per group, stored as a single column.

**Security properties confirmed in Spike 6:**
- Hub never sees the GEK in cleartext
- Ephemeral keypair is unique per bundle — same GEK and same recipient produce different ciphertext across calls
- AAD (`pk_recipient`) binds the bundle to its intended recipient — reuse for a different member is detected and rejected
- Wrong private key → AEAD authentication tag failure → immediate rejection

**Group creation:**
1. Admin node generates GEK (ChaCha20-Poly1305, 256-bit, CSPRNG)
2. GEK wrapped for each initial member via the protocol above
3. Wrapped bundles uploaded to hub via `POST /v1/groups/{group_id}/members/{username}/gek`
4. Members retrieve their bundle via `GET /v1/groups/{group_id}/gek`

**Member addition:**
- Admin fetches new member's `pk_x25519` from hub
- Wraps GEK for them and uploads bundle

**Member revocation:**
- Admin node generates new GEK
- Re-encrypts for all remaining members, uploads new bundles
- New content encrypted with new GEK from this point
- Former member can still decrypt previously received content (acceptable trade-off — full retroactive re-encryption not planned)

**Key persistence requirement:** before uploading a GEK bundle, the recipient's keypairs must already be registered on the hub and persisted locally. If a user registers, generates keypairs, but does not persist them before the first hub contact, subsequent sessions will regenerate different keypairs and all bundles will be undecryptable. The node initializes and persists all keypairs to the keystore before any hub API call.

### 6.3 On-the-Fly Encryption for File Transfer

Files are stored in plaintext on the host's disk. The node encrypts at read time.

```
Disk (plaintext) → zstd compress → GEK encrypt (per-chunk) → TCP+TLS 1.3 session → Client → TLS decrypt → GEK decrypt → plaintext
```

(In v2 transport: replace TCP+TLS 1.3 with QUIC — application pipeline is identical.)

**Chunking:**
- Chunk size: 1 MB (amortizes AEAD overhead; enables seeking)
- Per-chunk key derivation: `chunk_key = HKDF(GEK, salt=None, info="file:" || blake3(file) || ":chunk:" || index)` — salt is omitted because the GEK is a CSPRNG output (already uniform); the file/chunk context goes in `info` for domain separation, which is the correct HKDF usage per RFC 5869
- Each chunk independently decryptable → enables VOD seeking
- Compress before encrypt (compression is ineffective on ciphertext)

**Chunk authentication:** each chunk signed with the node's Ed25519 key. Client verifies before decryption. Prevents data injection by compromised relay.

**Encryption performance (Spike 5, 1 MB chunk, TCP, Fedora → OVH VPS):**

| Operation | Time |
|---|---|
| Encrypt + sign (node side) | 3.2 ms |
| Verify + decrypt (client side) | 3.9 ms |
| Total crypto overhead (1 MB) | < 10 ms |
| Network transfer | 99–234 ms (network-limited) |

Encryption is not the bottleneck. Network latency and bandwidth dominate.

**Pipeline optimization:**
- `cryptography` (PyCA) uses OpenSSL under the hood, bypasses Python GIL for crypto ops
- ChaCha20-Poly1305: ~1750 MB/s (Spike 1); AES-256-GCM: >2 GB/s with AES-NI
- asyncio pipeline (read → compress → encrypt → send) without loading full files into memory
- GEK-derived chunk keys computed in batch at transfer start, not per-chunk

### 6.4 Transport Security

**Implementation phases:**

| Phase | Transport | Status | Notes |
|---|---|---|---|
| v1 | TCP + TLS 1.3 | Current implementation target | Standard library (`asyncio` + `ssl`), well-understood, works everywhere |
| v2 | QUIC (TLS 1.3 integrated, UDP, multiplexed streams) | Future upgrade | `aioquic`, no protocol changes needed — only transport layer |

The `Transport` abstraction interface in `meshbay-node` decouples the application protocol from the underlying transport. Switching from TCP+TLS to QUIC requires implementing a new `Transport` backend with no changes to MNP message handling, GEK pipeline, or NAT traversal logic.

**Per-connection session keys:** X25519 ECDH + HKDF, independent of the GEK layer. Provides forward secrecy per connection regardless of transport.

**Rationale for TCP+TLS 1.3 first:** UDP hole-punching (required for QUIC in NAT scenarios) adds complexity in the early implementation. TCP outbound from behind NAT (as used in Spike 5) works without any NAT coordination. TLS 1.3 provides equivalent confidentiality guarantees to QUIC's integrated TLS. QUIC's benefits (0-RTT, multiplexing, no head-of-line blocking) are meaningful for performance but not for correctness — they belong in v2 once the application protocol is stable.

### 6.5 TCP+TLS 1.3 Transport Implementation (v1)

**Connection model:**
- Node listens on a configurable TCP port (default: 18000, same as local web UI port — separate socket)
- Clients connect outbound; nodes behind NAT connect outbound to other nodes via hole-punching signaling (see §7.1)
- TLS 1.3 mandatory; TLS 1.2 rejected
- Node presents a self-signed Ed25519 certificate pinned to its `PK_node` (registered on hub)
- Client validates certificate against `PK_node` retrieved from hub — not against a CA chain

**Handshake sequence:**
```
Client → Node: TCP SYN
Node → Client: TLS ServerHello (self-signed cert, PK_node)
Client: verify cert against hub-fetched PK_node
Client → Node: TLS ClientFinished
Node → Client: MNP handshake request (version negotiation)
Client → Node: MNP handshake response (JWT access token, version)
Node: verify JWT offline (Ed25519, hub public key)
Node → Client: session established
```

**Message framing over TCP:**
- Length-prefixed frames: `[4-byte big-endian length][msgpack payload]`
- Maximum frame size: 2 MB (prevents memory exhaustion; larger transfers use chunked `file_chunk` messages)
- Each frame carries the MNP `version` field in its header

**QUIC migration path (v2):**
- Replace TCP length-framing with QUIC streams (one stream per logical exchange)
- MNP handshake maps 1:1 to a QUIC handshake stream
- File transfer maps to a dedicated QUIC stream per file (multiplexed, no head-of-line blocking)
- Chat messages map to a persistent QUIC stream
- No changes to JWT verification, GEK decryption, or Index sync logic

**Port allocation:**
- `18000/tcp` — local web UI (loopback only, not exposed externally)
- `18001/tcp` — MNP P2P listener (exposed externally, TLS required)
- Configurable via `~/.config/meshbay/node.toml`

### 6.6 Chat Encryption and Model

Group chat is a **core feature** (not an extension module).

**Model (decided):** between a forum and Signal.
- **Persistent:** messages stored on the node (not ephemeral like Signal by default)
- **Structured:** optional threads/topics for longer discussions, flat stream for quick messages
- **Scope:** per group (not per user pair)
- **Attachments:** files and images, shared like regular group files
- **Push/pull:** connected members get real-time push (WebSocket); offline members pull history on reconnect
- **Retention:** managed by the group admin (no automatic expiry)

**Encryption — Sender Keys protocol (decided in first security review, 2026-08-10):**

The Double Ratchet (implemented in `meshbay_common.ratchet`) is a **pairwise** (1:1) protocol. Using a shared ratchet state for N group members would cause chain key desynchronization and nonce/key reuse — a catastrophic AEAD failure. The architecture uses **Sender Keys** instead (same approach as Signal Groups):

- Each group member generates a **sender key** (random symmetric chain key + signing keypair)
- On joining a group, the new member's sender key is distributed to all existing members via pairwise channels (GEK-wrapped or direct)
- Each existing member sends their current sender key to the new member
- Messages are encrypted with the sender's chain key (symmetric ratchet, one direction)
- Forward secrecy at **member rotation** granularity: when a member is removed, all remaining members rotate their sender keys
- O(N) state per member (one chain per group member), not O(N^2)
- The existing Double Ratchet implementation is kept for future 1:1 direct messaging

Attachment files: encrypted with GEK-derived key (same as file chunks), hash referenced in the message.

> **Why not MLS (RFC 9420)?** MLS provides O(log N) message overhead and per-message forward secrecy via tree-based ratcheting. It is the superior long-term choice, but its complexity is not justified for v1 group sizes (< 50 members). Sender Keys is proven at scale (Signal, WhatsApp) and simpler to implement. Migration to MLS is a v2 option if group sizes grow.

---

## 7. Network and Connectivity

### 7.1 NAT Traversal — Attempt Order

```
1. IPv6 available on both sides         → direct connection (preferred)
2. STUN / ICE + UDP hole punching       → ~80–85% success rate (Cone NAT confirmed in Spike 4)
3. UPnP / NAT-PMP on router            → port mapping if available (NOT reliable — disabled on tested SFR box)
4. Mesh Relay (TURN)                    → [future feature] — symmetric NAT, CGNAT mobile
```

> **Correction from v2:** UPnP was listed as step 2 in v2. Spike 4 showed UPnP disabled on the tested SFR residential gateway. STUN + hole-punching (step 2) is more reliable and does not require router cooperation. UPnP is demoted to step 3 as a best-effort supplement, not a dependency.

**Spike 4 findings:**
- Cone NAT confirmed on SFR residential (same external port 51250 for two different STUN servers)
- UDP hole punching functional: bidirectional echo received from OVH VPS
- STUN servers tested: `stun.cloudflare.com`, `stun.l.google.com` — both returned consistent results
- No CGNAT: stable public IPv4 (81.220.170.32)

Without step 4 (Mesh Relay), approximately 15% of connections between symmetric-NAT peers will fail. This is documented behavior until Mesh Relay is implemented.

**Signaling punch/connect (Phase 7.2 — reduces handshake from 12.7s to < 200ms):**
Currently the node punches blindly at startup; the client may connect 10-20s later
on an aging NAT entry, causing retransmissions. The coordinated flow uses the
existing hub→node WebSocket (revocation channel):
```
Client → Hub : POST /v1/nodes/{id}/incoming  {peer_ip, peer_port}
Hub → Node  (WS) : {type: "client_incoming", peer_ip, peer_port}
Node        : punch_nat(peer_ip, peer_port) immediately
Node → Hub  (WS) : {type: "punch_ready"}
Hub → Client: 200 OK "connect now"
Client → QUIC: first packet < 2s after probe → fresh NAT entry
```
demo-v2 finding: SFR residential is **Port-Restricted Cone NAT**.
The probe must come from the QUIC server's own socket (`punch_nat()` via
`_transport.sendto()`). The QUIC client must connect from the same port
as the probe's destination (`local_port=QUIC_PORT`). Handshake time
with proper signaling: < 200ms (vs 12.7s without).

#### 7.1.1 Browser-Specific NAT Traversal (WebRTC DataChannel)

Browsers cannot use the QUIC `punch_nat()` mechanism because WebTransport does
not allow the browser to choose its UDP source port. Port-Restricted Cone NAT
requires exact port matching on both IP and port — impossible for browsers.

**Solution:** WebRTC DataChannel with ICE/STUN. The browser's built-in WebRTC
stack handles NAT traversal automatically:

1. Browser gathers ICE candidates via STUN (discovers its external IP:port)
2. Node gathers ICE candidates via `aioice` (discovers its external IP:port)
3. Candidates exchanged via hub signaling (WebSocket relay, <1 KB)
4. ICE connectivity checks: both sides send STUN binding requests simultaneously
5. STUN binding requests serve as NAT hole-punching (both directions)
6. ICE finds a valid candidate pair — DataChannel established
7. MNP protocol runs over DataChannel (same messages, same E2E encryption)

**Signaling flow:**
```
Browser → Hub (HTTPS) : POST /v1/nodes/{id}/webrtc/offer  {sdp, ice_candidates}
Hub → Node  (WS)      : {type: "webrtc_offer", sdp, ice_candidates, peer_id}
Node (aiortc)          : creates PeerConnection, gathers answer candidates
Node → Hub  (WS)      : {type: "webrtc_answer", sdp, ice_candidates, peer_id}
Hub → Browser (SSE)   : answer SDP + ICE candidates
DataChannel            : P2P established — hub no longer involved
```

ICE is strictly superior to custom `punch_nat()` for browser use:
- No need for the client to pre-announce its port
- Handles both sides behind NAT simultaneously
- Automatic candidate prioritization and fallback
- Battle-tested by billions of daily users (Google Meet, Discord, Zoom)

**Node dual transport:** the node listens on both:
- QUIC (UDP port 19000) — native clients (desktop, Android)
- WebRTC — browsers (via `aiortc`, separate UDP socket managed by ICE)

The MNP application protocol is identical on both transports. Same handshake,
same file_request/file_chunk, same chat_message, same E2E encryption.

### 7.2 MNP — Mesh Node Protocol

Application-level protocol over TCP+TLS 1.3 (v1) or QUIC (v2). All messages carry a `version` field. The protocol is transport-agnostic — the `Transport` abstraction layer handles framing differences.

**Defined message types:**

| Type | Description |
|---|---|
| `handshake` | Key exchange, JWT presentation, version negotiation |
| `index_sync` | Encrypted Mesh Group Index delta |
| `file_request` | Request chunk(s) of a file by hash + chunk index |
| `file_chunk` | Chunk data + Ed25519 signature |
| `stream_segment` | HLS/DASH segment (VOD), encrypted with GEK-derived key |
| `chat_message` | Double Ratchet encrypted message frame |
| `chat_attachment` | Attachment metadata + key; data transferred as file chunks |
| `ephemeral_stream` | [reserved, future] Ephemeral video with TTL metadata |

### 7.3 Public Content Delivery — Swarm

Public files identified by `blake3` hash. Multiple nodes can serve the same file:

1. Any node that has a public file and chooses to mirror it registers: `{ hash → node_address }` with the hub
2. Hub maintains a source table: `{ blake3_hash → [node_A, node_B, ...] }`
3. Client requests file → hub returns source list → client fetches chunks in parallel from multiple nodes
4. Integrity verified by blake3 hash on each chunk

**Transport:** TLS only for public content (no GEK). Content signed with the original node's Ed25519 key — clients verify authenticity even when served from a mirror.

---

## 8. Indexes

### 8.1 Mesh Directory (hub level)

Public registry of groups, exchanged between hubs via MHP.

Format: `msgpack`, signed with hub's Ed25519 key, carries `version` field.

Fields per entry: group name, `PK_group`, hosting hub, description, content type tags, join policy, creation date.

### 8.2 Mesh Group Index (node level)

File listing for a group. Generated and maintained by the hosting node.

Format: `msgpack` → `zstd` → GEK-encrypted (private groups) or plaintext + Ed25519 signature (public groups).

Entry structure:
```python
{
  "version":    1,
  "id":         "<blake3_hash>",
  "name":       "filename.mkv",
  "path":       "Movies/2024/",
  "size":       4294967296,
  "type":       "video",           # video | audio | image | document | archive | other
  "duration":   7245,              # seconds, for media
  "thumb_hash": "<blake3>",        # thumbnail also GEK-encrypted
  "added_at":   1720000000
}
```

Delta updates: `{ base_version, additions, deletions }` — no full re-encryption on each change.

Transit: nodes push index deltas to connected members on change; members pull full index on first connection. Hub stores no index content.

### 8.3 Search

**Private groups:** entirely local on the client device. Client maintains a local encrypted cache of all group indexes it has received. No network call, no hub involvement, instant.

**Public groups:** client queries nodes directly at request time. Hub provides routing only.

**Hub web UI search:** delegates query to relevant nodes at request time. Hub stores nothing from this. In-memory micro-cache: **60-second TTL, RAM only, never persisted to disk, public content only.** Qualifies as technical caching under EU DSA Article 13 — not indexing.

---

## 9. Web Client UI

### 9.1 Architecture

The web client is a Preact SPA served by the hub at `/app/`. It communicates
with the hub via HTTPS (auth, group management, signaling) and with nodes via
WebRTC DataChannel (file transfer, streaming, chat). The hub is never in the
data path.

**Technology choices:**
- **Preact** (~3 KB gzipped): lightweight React-compatible framework
- **preact-router**: client-side routing (no server round-trips)
- **esbuild**: minification/bundling (single binary, no npm/node_modules)
- **SubtleCrypto**: browser-native AES-GCM for E2E decryption
- **IndexedDB**: local cache for group indexes (client-side search)

No heavy frameworks (React, Vue, Angular). No build toolchain dependencies beyond
esbuild. ESM modules loaded natively by modern browsers.

### 9.2 UI Structure

```
┌─────────────────────────────────────────────────────────┐
│  [MeshBay]                              [User ▾] [⚙]  │
├──────────┬──────────────────────────────────────────────┤
│          │                                              │
│ Groups   │  Main content area                           │
│          │                                              │
│ ● Private│  - File explorer (folders, files, download)  │
│   Group1 │  - Chat/forum view                           │
│   Group2 │  - Video player (HLS via MediaSource API)    │
│          │  - Settings                                  │
│ ○ Public │  - Notifications feed                        │
│   Group3 │                                              │
│          │                                              │
└──────────┴──────────────────────────────────────────────┘
```

- **Left sidebar:** group list, ordered by usage frequency. Private groups first.
  Collapses to hamburger menu on mobile viewports.
- **Top bar:** logo (left), user menu dropdown (right) — settings, profile,
  language, online/offline status, logout.
- **Main area:** context-dependent content based on selected group and view.

### 9.3 Views

**Front page (no group selected):**
- Notification feed, prioritized: known contacts → private group activity → public
- System notifications (maintenance, updates)
- Quick access to recent groups

**Group view — File Explorer:**
- Directory tree (folders, subfolders) — read-only browsing
- File metadata: name, size, type, date added
- Actions: download, stream (for media files)
- Files fetched directly from node via DataChannel
- Upload: photos/videos posted to the group's shared upload folder
  (only the uploader or group admin can modify/delete)

**Group view — Chat/Forum:**
- Sender Keys encrypted messages, fetched from node
- Post text, photos, videos (uploads go to node, not hub)
- Optional thread/topic structure for organized discussions
- Real-time push for connected members, pull history on reconnect

**Group view — Video Player:**
- HLS segments fetched via DataChannel from node
- Decrypted client-side (GEK-derived key per segment)
- Played via MediaSource API (browser-native, no plugins)

**Settings:**
- General: theme (dark/light/auto), language, notification preferences
- Per-group: notification mute, display options, filtering/blocking
- Privacy: online/offline status, profile visibility
- Profile: display name, avatar, account details

### 9.4 Theming and i18n

**Theme:** CSS custom properties for colors, toggled via:
1. `prefers-color-scheme` media query (OS default)
2. User override stored in localStorage
3. Toggle button in top bar or settings

**i18n:** JSON translation files loaded client-side.
```
static/i18n/
├── en.json    # English (default, always loaded)
├── fr.json    # French (loaded on demand)
└── ...        # Other languages added later
```

Keys are identifiers, not English text. Translation function: `t('group.join')`.

### 9.5 meshbay.org Site Overlay

meshbay.org serves both the generic hub application and site-specific pages:

```
site/                           # meshbay.org-specific (not packaged with hub)
├── index.html                  # Landing page — project promotion, features
├── downloads.html              # Package repos: Ubuntu, Fedora, Android APK
├── about.html                  # Project info, team, GitHub, contact
└── assets/                     # Landing-specific CSS, images, icons
```

Caddy serves `site/` with priority. Requests not matching a static file fall
through to the hub FastAPI application. The hub serves `/app/` (SPA) and `/v1/`
(API). This separation ensures the hub package remains generic and deployable
by any operator, while meshbay.org has its own public-facing identity.

### 9.6 Hub Mirror (future — design only)

A mirror hub is a complete active-active replica of the primary hub.

**Purpose:** load distribution for growing traffic. DNS round-robin (2+ A records).

**Design:**
- Shared Ed25519 signing key (transferred once, securely)
- PostgreSQL logical replication for bidirectional read/write
- Both mirrors issue JWTs with the same key
- Both mirrors accept registrations, logins, and group operations
- If one mirror goes down, the other serves all traffic

**Implementation constraints (must not violate in current development):**
- Hub config and key paths must be externalizable (already the case)
- No hub-instance-specific state that cannot be replicated
- JWT verification must not depend on hub-local state (already the case)
- Session state (refresh tokens, IP logs) must be in PostgreSQL (already the case)

**Not implemented now.** Design documented to avoid blocking decisions.

---

## 10. Hub Federation (MHP)  <!-- was §9 in v3 -->

### 9.1 Hub Hierarchy

```
Root Hub (meshbay.org)
    ├── Full Hub (self-hosted, delegated CA)
    │     └── issues user credentials, manages own groups
    │     └── federates with other Full Hubs via MHP
    └── Mirror Hub
          └── hosts public Mesh Directory only (no user accounts, no key issuance)
```

A Full Hub receives a certificate signed by the Root Hub (or a parent Full Hub). Mirror Hubs can only replicate public directory data. Promotion/demotion is possible without breaking the protocol.

### 9.2 MHP Design

- Explicit peer selection: each hub maintains an allowlist of trusted peers
- No automatic hub discovery
- Exchanged: Mesh Directory (public groups), revocation lists, cross-hub user authentication data
- All MHP messages carry `version` field

### 9.3 Cross-Hub Client Access

1. Client (Hub A user) discovers a group on Hub B via Mesh Directory or direct link
2. Client presents Hub A JWT directly to Hub B
3. Hub B verifies JWT using Hub A's public key (fetched once, cached)
4. Hub B issues short-lived local session token
5. Client connects to node as normal

---

## 11. Moderation  <!-- was §10 in v3 -->

### 10.1 Public Content

```
Report #1  → automatic suspension of public access
           → node operator notified
One republication allowed
Report #2  → escalated to hub moderators
Confirmed  → group revoked on local hub
           → revocation propagated to federated hubs via MHP
```

Mechanism: `blake3` hash added to hub blocklist. Signed revocation token sent to node.

### 10.2 CSAM

Hash matching against NCMEC/IWF database on public content at registration time. No scanning of private/encrypted content. Participation is mandatory for hub operators and reduces legal exposure.

### 10.3 Copyright

DMCA/legal notice framework. Takedown on notification. No automated technical blocking (false positive risk, fair use). Hub can revoke on confirmed legal request.

### 10.4 Private Content

Not directly moderatable (E2E encrypted). Action available: revoke user or group at hub level on formal legal request. Hub issues Ed25519-signed revocation token verifiable by all member nodes offline.

---

## 12. Python Extension Module System  <!-- was §11 in v3 -->

The node loads extension modules (Python) in a sandboxed subprocess. **Chat is a core built-in feature, not a module.**

**Module manifest:**
```python
{
  "name": "my-extension",
  "version": "1.0.0",
  "mnp_version": ">=1.0",
  "permissions": ["read_index", "send_message", "receive_events"]
}
```

**Available APIs:**
- `read_index()` — read current group index (read-only)
- `send_message(content)` — post to group thread
- `receive_events(handler)` — subscribe to group events

**Unavailable:** arbitrary network, filesystem access outside group context, system calls.

---

## 13. Legal Framework  <!-- was §12 in v3 -->

**Node operator:** primary legal host of content. Fully responsible for what they share. Node setup communicates this explicitly.

**Hub operator (meshbay.org):** registrar, not content host. Stores minimal data. Operates takedown mechanism. Participates in CSAM hash matching. Legal exposure analogous to a domain registrar.

**Protocol/software author:** protected by substantial non-infringing uses.

**Hub data:**
- Email and optional phone: kept for account recovery and legal compliance
- Password: Argon2id hash, never stored in cleartext
- Connection logs: retained per legal requirements (minimum 1 year)
- Content metadata: never stored
- Node current IP: not persisted (signaling is ephemeral)
- GEK bundles: opaque 48-byte ciphertext blobs; hub cannot decrypt them

---

## 14. Future Features  <!-- was §13 in v3 -->

- **Mesh Relay:** community TURN relays, E2E encrypted traffic. Low priority — typical residential NAT works with ICE/STUN. Needed only for symmetric NAT (CGNAT mobile, ~15% of connections).
- ~~**QUIC transport (v2)**~~ ✅ DONE (Phase 5) — QUIC replaces TCP+TLS.
- **Content replication between nodes:** node-to-node, admin-authorized, no hub involvement
- **Hub mirror (load balancing):** design documented in §9.6. Active-active with shared key, PostgreSQL replication, DNS round-robin. Implementation deferred.
- **Mobile video push → node:** mobile films → pushes to hosting node → ephemeral stream with TTL. MNP `ephemeral_stream` type reserved.
- **Node–mobile pairing:** QR code from local web UI
- **Multi-source download:** parallel chunk fetching from swarm for public files
- **At-rest encryption on node:** optional for server-deployed nodes
- **OS keychain integration for keystore unlock**
- ~~**WebRTC**~~ ✅ Validated (Phase 9.1–9.5) — `aiortc` for browser-to-node P2P via DataChannel. Tested on SFR residential NAT (Port-Restricted Cone) + 4G CGNAT. No TURN needed.
- **Extension-triggered views:** local apps providing custom views for group content (gallery, kanban). MNP extension hook reserved.

---

## 15. Open Questions [TBD]

**Resolved by POC (no longer open):**

| # | Question | Resolution | Source |
|---|---|---|---|
| R1 | Argon2id parameters: what values target ~500ms? | `iterations=3, memory_cost=262144` (256 MB). pw_version=2, transparent rehash on login. | Spike 1 + Phase 8.10 |
| R2 | JWT payload claims: what fields for offline node verification? | `jti` (UUID4), `user_id`, `PK_user`, `hub_id`, `issued_at`, `expires_at`, `groups` claim. | Spike 3 + Phase 7 |
| R3 | GEK wrapping protocol: exact algorithm? | ECIES-like: ephemeral X25519 + HKDF(salt=pk_eph, info="meshbay:gek_wrap:v1") + ChaCha20-Poly1305(aad=pk_recipient). | Spike 6 |
| R4 | NAT traversal: is STUN/hole-punching sufficient for residential users? | Yes for Cone NAT (SFR, Orange, Free). Relay needed only for symmetric NAT (CGNAT mobile). | Spike 4 |
| R5 | Transport: QUIC or TCP+TLS 1.3 for v1? | TCP+TLS 1.3 for v1, QUIC for v2. QUIC is now the active transport (Phase 5). | Spike 5 |
| R6 | Hub API: which endpoints for GEK distribution? | 4 endpoints confirmed. | Spike 6 |
| R7 | Package structure? | 3 packages: `meshbay-common`, `meshbay-hub`, `meshbay-node`. | POC |

**Resolved by first security review (2026-08-10):**

| # | Question | Resolution | Source |
|---|---|---|---|
| R8 | Group chat encryption model? | Sender Keys protocol. Double Ratchet kept for future 1:1 DM. | Security review C1 |
| R9 | Token denylist distribution? | Push via hub→node WebSocket. In-memory jti set on node. | Security review S3 |
| R10 | Chunk key HKDF: salt or info? | `info` (domain separation), `salt=None`. RFC 5869 compliant. | Security review M5 |
| R11 | AES-GCM keystore IV size? | 96-bit (12 bytes), per NIST SP 800-38D. | Security review S4 |

**Resolved by Phase 8 implementation (2026-08-10):**

| # | Question | Resolution | Source |
|---|---|---|---|
| R12 | Refresh token rotation? | One-time-use with family-based reuse detection. Old token reuse revokes entire family. | Phase 8.3 |
| R13 | Email encryption at rest? | AES-256-GCM, key derived from hub Ed25519 private key via HKDF(info="meshbay:email:v1"). | Phase 8.2 |
| R14 | Admin authorization model? | Config-based: `admin_usernames` in hub.toml + `MESHBAY_ADMIN_USERS` env var. | Phase 8.1 |
| R15 | QUIC migration timeline? | Done — QUIC is the active transport since Phase 5. | Phase 5 |

**Resolved by web client design session (2026-08-10):**

| # | Question | Resolution | Source |
|---|---|---|---|
| R16 | Browser transport for NAT traversal? | WebRTC DataChannel with ICE/STUN. WebTransport cannot work (port-restricted cone NAT). | Design session |
| R17 | Chat storage location? | On nodes, not hub. Hub never stores content. | Design session |
| R18 | Web UI framework? | Preact SPA (~3 KB), esbuild, dark/light theme, i18n, responsive. | Design session |
| R19 | Hub mirror design? | Active-active, shared signing key, PostgreSQL replication, DNS round-robin. | Design session |

**Resolved by Phase 9 spike (2026-08-10):**

| # | Question | Resolution | Source |
|---|---|---|---|
| R20 | WebRTC DataChannel validation? | Confirmed: browser→NAT→node file transfer works. Tested 3 scenarios on SFR residential (Port-Restricted Cone NAT) + 4G CGNAT: WiFi LAN (IPv6 direct, ~100ms), 4G IPv6 inter-network (~600ms), 4G IPv4 STUN hole-punch (~650ms). No TURN relay needed. | Phase 9.5 spike |

**Still open:**

1. **Refresh token validity:** 30 or 90 days?
2. **Group address scheme:** final URL format confirmation
3. **GEK bundle location for groups with mixed access** (public-restricted): hub or node?
4. **MHP federation sync frequency and conflict resolution**
5. **Chat attachment storage:** stored on node like regular files, or separate store?
6. **Relay registration protocol design** (when implemented)