@serfab/cadre-core 1.23.0 → 1.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (313) hide show
  1. package/README.md +216 -294
  2. package/dist/arachnode-stub.d.ts +12 -4
  3. package/dist/arachnode-stub.js +15 -8
  4. package/dist/arachnode-stub.js.map +1 -1
  5. package/dist/cadre-invite-protocol.js +5 -4
  6. package/dist/cadre-invite-protocol.js.map +1 -1
  7. package/dist/cadre-node.d.ts +145 -44
  8. package/dist/cadre-node.js +239 -79
  9. package/dist/cadre-node.js.map +1 -1
  10. package/dist/control-authorization.d.ts +14 -2
  11. package/dist/control-authorization.js +8 -3
  12. package/dist/control-authorization.js.map +1 -1
  13. package/dist/control-cohort.d.ts +27 -13
  14. package/dist/control-cohort.js +21 -14
  15. package/dist/control-cohort.js.map +1 -1
  16. package/dist/control-database.d.ts +18 -16
  17. package/dist/control-database.js +57 -22
  18. package/dist/control-database.js.map +1 -1
  19. package/dist/control-protocol-guard.d.ts +1 -1
  20. package/dist/control-protocol-guard.js +7 -3
  21. package/dist/control-protocol-guard.js.map +1 -1
  22. package/dist/control-read-retry.d.ts +49 -11
  23. package/dist/control-read-retry.js +56 -15
  24. package/dist/control-read-retry.js.map +1 -1
  25. package/dist/control-retry.d.ts +32 -3
  26. package/dist/control-retry.js +35 -4
  27. package/dist/control-retry.js.map +1 -1
  28. package/dist/control-schema-version.d.ts +5 -5
  29. package/dist/control-schema-version.js +5 -5
  30. package/dist/control-schema-version.js.map +1 -1
  31. package/dist/control-schema.d.ts +5 -5
  32. package/dist/control-schema.js +1032 -1011
  33. package/dist/control-schema.js.map +1 -1
  34. package/dist/control-write-retry.d.ts +54 -65
  35. package/dist/control-write-retry.js +134 -158
  36. package/dist/control-write-retry.js.map +1 -1
  37. package/dist/delegate-admission.d.ts +1 -1
  38. package/dist/delegate-admission.js +1 -1
  39. package/dist/edge-node/config.d.ts +4 -3
  40. package/dist/edge-node/config.js +11 -9
  41. package/dist/edge-node/config.js.map +1 -1
  42. package/dist/edge-node/dissolve.d.ts +4 -4
  43. package/dist/edge-node/dissolve.js +4 -4
  44. package/dist/edge-node/edge-join.d.ts +1 -1
  45. package/dist/edge-node/edge-join.js +1 -1
  46. package/dist/edge-node/edge-node.js +2 -2
  47. package/dist/edge-node/edge-node.js.map +1 -1
  48. package/dist/edge-node/index.d.ts +1 -0
  49. package/dist/edge-node/index.js +1 -0
  50. package/dist/edge-node/index.js.map +1 -1
  51. package/dist/edge-node/join-credential.d.ts +1 -1
  52. package/dist/edge-node/join-credential.js +1 -1
  53. package/dist/edge-node/lazy-raw-storage.js +12 -0
  54. package/dist/edge-node/lazy-raw-storage.js.map +1 -1
  55. package/dist/edge-node/listen-addrs.d.ts +21 -0
  56. package/dist/edge-node/listen-addrs.js +25 -0
  57. package/dist/edge-node/listen-addrs.js.map +1 -0
  58. package/dist/edge-node/owner-grant.d.ts +1 -1
  59. package/dist/edge-node/owner-grant.js +1 -1
  60. package/dist/edge-node/platform.d.ts +5 -4
  61. package/dist/edge-node/platform.js.map +1 -1
  62. package/dist/edge-node/strand-move.d.ts +1 -1
  63. package/dist/edge-node/strand-move.js +1 -1
  64. package/dist/edge-node/strands.js +27 -11
  65. package/dist/edge-node/strands.js.map +1 -1
  66. package/dist/index.d.ts +7 -5
  67. package/dist/index.js +18 -7
  68. package/dist/index.js.map +1 -1
  69. package/dist/invitation-bootstrap.d.ts +28 -5
  70. package/dist/invitation-bootstrap.js +41 -8
  71. package/dist/invitation-bootstrap.js.map +1 -1
  72. package/dist/link-budget.d.ts +53 -4
  73. package/dist/link-budget.js +58 -5
  74. package/dist/link-budget.js.map +1 -1
  75. package/dist/machine-removal.d.ts +2 -2
  76. package/dist/machine-removal.js +4 -3
  77. package/dist/machine-removal.js.map +1 -1
  78. package/dist/membership-connection-gater.d.ts +2 -2
  79. package/dist/membership-connection-gater.js +2 -2
  80. package/dist/membership-view.d.ts +2 -2
  81. package/dist/membership-view.js +2 -2
  82. package/dist/node-claim-payload.d.ts +15 -7
  83. package/dist/node-claim-payload.js +25 -9
  84. package/dist/node-claim-payload.js.map +1 -1
  85. package/dist/node-class.d.ts +36 -0
  86. package/dist/node-class.js +26 -0
  87. package/dist/node-class.js.map +1 -0
  88. package/dist/node-default-transports.d.ts +50 -3
  89. package/dist/node-default-transports.js +48 -3
  90. package/dist/node-default-transports.js.map +1 -1
  91. package/dist/node-roles.d.ts +118 -0
  92. package/dist/node-roles.js +142 -0
  93. package/dist/node-roles.js.map +1 -0
  94. package/dist/node-transports.d.ts +6 -2
  95. package/dist/node-transports.js +39 -7
  96. package/dist/node-transports.js.map +1 -1
  97. package/dist/owner-anchor-sync.js +1 -1
  98. package/dist/owner-key-export.js +1 -1
  99. package/dist/owner-key-flows.d.ts +1 -1
  100. package/dist/owner-key-store.js +1 -1
  101. package/dist/owner-signer.js +1 -1
  102. package/dist/peer-authorization.d.ts +3 -1
  103. package/dist/peer-authorization.js +3 -1
  104. package/dist/peer-authorization.js.map +1 -1
  105. package/dist/peer-dial.d.ts +74 -10
  106. package/dist/peer-dial.js +89 -17
  107. package/dist/peer-dial.js.map +1 -1
  108. package/dist/peer-join-backfill.js +4 -4
  109. package/dist/peer-join-backfill.js.map +1 -1
  110. package/dist/peer-record.d.ts +34 -9
  111. package/dist/peer-record.js +66 -13
  112. package/dist/peer-record.js.map +1 -1
  113. package/dist/pending-join-runner.d.ts +1 -1
  114. package/dist/pending-join-runner.js +1 -1
  115. package/dist/provider/client.d.ts +6 -5
  116. package/dist/provider/client.js +3 -3
  117. package/dist/provider/client.js.map +1 -1
  118. package/dist/provider/descriptor.d.ts +1 -1
  119. package/dist/provider/descriptor.js +1 -1
  120. package/dist/provider/oauth.d.ts +1 -1
  121. package/dist/provider/oauth.js +1 -1
  122. package/dist/provider/rent-node.d.ts +4 -3
  123. package/dist/provider/rent-node.js +3 -3
  124. package/dist/provider/rent-node.js.map +1 -1
  125. package/dist/relay-addrs.d.ts +62 -7
  126. package/dist/relay-addrs.js +74 -11
  127. package/dist/relay-addrs.js.map +1 -1
  128. package/dist/relay-reservation.d.ts +1 -1
  129. package/dist/relay-reservation.js +1 -1
  130. package/dist/relay-server.d.ts +6 -5
  131. package/dist/relay-server.js +5 -5
  132. package/dist/relay-server.js.map +1 -1
  133. package/dist/schema-verification.d.ts +13 -10
  134. package/dist/schema-verification.js +32 -24
  135. package/dist/schema-verification.js.map +1 -1
  136. package/dist/sealed-party-key.js +1 -1
  137. package/dist/seed-bootstrap.d.ts +4 -3
  138. package/dist/seed-bootstrap.js +6 -7
  139. package/dist/seed-bootstrap.js.map +1 -1
  140. package/dist/strand-addr-protocol.d.ts +8 -6
  141. package/dist/strand-addr-protocol.js +12 -10
  142. package/dist/strand-addr-protocol.js.map +1 -1
  143. package/dist/strand-app-schema-report.d.ts +16 -2
  144. package/dist/strand-app-schema-report.js +15 -0
  145. package/dist/strand-app-schema-report.js.map +1 -1
  146. package/dist/strand-cohort.d.ts +1 -4
  147. package/dist/strand-cohort.js.map +1 -1
  148. package/dist/strand-commit-durability.d.ts +1 -1
  149. package/dist/strand-database.d.ts +13 -6
  150. package/dist/strand-database.js +17 -9
  151. package/dist/strand-database.js.map +1 -1
  152. package/dist/strand-first-sync-gate.d.ts +3 -2
  153. package/dist/strand-first-sync-gate.js +3 -2
  154. package/dist/strand-first-sync-gate.js.map +1 -1
  155. package/dist/strand-formation-deadlines.d.ts +2 -2
  156. package/dist/strand-formation-deadlines.js +2 -2
  157. package/dist/strand-formation-protocol.d.ts +1 -1
  158. package/dist/strand-formation-protocol.js +51 -16
  159. package/dist/strand-formation-protocol.js.map +1 -1
  160. package/dist/strand-instance-manager.d.ts +67 -33
  161. package/dist/strand-instance-manager.js +164 -55
  162. package/dist/strand-instance-manager.js.map +1 -1
  163. package/dist/strand-membership-reconciler.d.ts +8 -3
  164. package/dist/strand-membership-reconciler.js +8 -3
  165. package/dist/strand-membership-reconciler.js.map +1 -1
  166. package/dist/strand-membership-writer.d.ts +2 -2
  167. package/dist/strand-membership-writer.js +41 -39
  168. package/dist/strand-membership-writer.js.map +1 -1
  169. package/dist/strand-move.js +1 -1
  170. package/dist/strand-network-config.d.ts +15 -5
  171. package/dist/strand-network-config.js +7 -6
  172. package/dist/strand-network-config.js.map +1 -1
  173. package/dist/strand-party-key-rotation.d.ts +7 -3
  174. package/dist/strand-party-key-rotation.js +7 -3
  175. package/dist/strand-party-key-rotation.js.map +1 -1
  176. package/dist/strand-party-machines.d.ts +19 -8
  177. package/dist/strand-party-machines.js +26 -11
  178. package/dist/strand-party-machines.js.map +1 -1
  179. package/dist/strand-party-peers.d.ts +36 -14
  180. package/dist/strand-party-peers.js +24 -15
  181. package/dist/strand-party-peers.js.map +1 -1
  182. package/dist/strand-peer-redial.d.ts +1 -1
  183. package/dist/strand-peer-redial.js +1 -1
  184. package/dist/strand-reactivity.d.ts +20 -17
  185. package/dist/strand-reactivity.js +31 -18
  186. package/dist/strand-reactivity.js.map +1 -1
  187. package/dist/strand-revocation-enforcer.d.ts +3 -3
  188. package/dist/strand-revocation-enforcer.js +3 -3
  189. package/dist/strand-wake-protocol.d.ts +19 -7
  190. package/dist/strand-wake-protocol.js +22 -10
  191. package/dist/strand-wake-protocol.js.map +1 -1
  192. package/dist/strand-watcher.d.ts +1 -1
  193. package/dist/strand-watcher.js +1 -1
  194. package/dist/table-change-watch.js +1 -1
  195. package/dist/types.d.ts +116 -55
  196. package/dist/types.js.map +1 -1
  197. package/dist/webrtc-upgrade.d.ts +254 -0
  198. package/dist/webrtc-upgrade.js +355 -0
  199. package/dist/webrtc-upgrade.js.map +1 -0
  200. package/package.json +9 -9
  201. package/src/announce-addrs.ts +93 -93
  202. package/src/arachnode-stub.ts +18 -10
  203. package/src/bootstrap-peer-store-file.ts +44 -44
  204. package/src/bootstrap-peer-store.ts +214 -214
  205. package/src/cadre-invite-protocol.ts +741 -740
  206. package/src/cadre-node.ts +12462 -12280
  207. package/src/claim-proof.ts +147 -147
  208. package/src/control-authorization.ts +290 -276
  209. package/src/control-cohort.ts +223 -204
  210. package/src/control-database.ts +72 -28
  211. package/src/control-formation-recorder.ts +359 -359
  212. package/src/control-protocol-guard.ts +370 -366
  213. package/src/control-read-retry.ts +222 -180
  214. package/src/control-retry.ts +36 -4
  215. package/src/control-schema-version.ts +301 -301
  216. package/src/control-schema.ts +1108 -1087
  217. package/src/control-write-retry.ts +639 -669
  218. package/src/delegate-admission.ts +351 -351
  219. package/src/describe-thrown.ts +60 -60
  220. package/src/ed25519-key.ts +185 -185
  221. package/src/edge-node/config.ts +13 -10
  222. package/src/edge-node/dissolve.ts +252 -252
  223. package/src/edge-node/edge-join.ts +320 -320
  224. package/src/edge-node/edge-node.ts +1095 -1095
  225. package/src/edge-node/index.ts +60 -59
  226. package/src/edge-node/join-credential.ts +16 -16
  227. package/src/edge-node/lazy-raw-storage.ts +194 -178
  228. package/src/edge-node/listen-addrs.ts +26 -0
  229. package/src/edge-node/node-local.ts +84 -84
  230. package/src/edge-node/options.ts +195 -195
  231. package/src/edge-node/owner-grant.ts +63 -63
  232. package/src/edge-node/platform.ts +5 -4
  233. package/src/edge-node/strand-move.ts +266 -266
  234. package/src/edge-node/strands.ts +27 -12
  235. package/src/enrolled-machine-store-file.ts +43 -43
  236. package/src/enrolled-machine-store.ts +280 -280
  237. package/src/fetch-impl.ts +23 -23
  238. package/src/file-durable-slot.ts +64 -64
  239. package/src/file-safe-name.ts +32 -32
  240. package/src/formation-approval.ts +604 -604
  241. package/src/formation-token.ts +63 -63
  242. package/src/fs-atomic.ts +92 -92
  243. package/src/index.ts +987 -957
  244. package/src/invitation-bootstrap.ts +152 -118
  245. package/src/joined-strand-store.ts +553 -553
  246. package/src/key-store-file.ts +142 -142
  247. package/src/link-budget.ts +61 -5
  248. package/src/machine-removal.ts +211 -210
  249. package/src/member-redemption.ts +643 -643
  250. package/src/membership-connection-gater.ts +650 -650
  251. package/src/membership-view.ts +147 -147
  252. package/src/node-claim-payload.ts +34 -12
  253. package/src/node-class.ts +47 -0
  254. package/src/node-default-transports.ts +65 -3
  255. package/src/node-roles.ts +220 -0
  256. package/src/node-transports.ts +86 -49
  257. package/src/owner-anchor-sync.ts +172 -172
  258. package/src/owner-grant-protocol.ts +547 -547
  259. package/src/owner-key-export.ts +348 -348
  260. package/src/owner-key-flows.ts +109 -109
  261. package/src/owner-key-store.ts +1 -1
  262. package/src/owner-keys.ts +179 -179
  263. package/src/owner-signer.ts +92 -92
  264. package/src/peer-addr-book.ts +252 -252
  265. package/src/peer-authorization.ts +584 -582
  266. package/src/peer-dial.ts +698 -605
  267. package/src/peer-join-backfill.ts +961 -961
  268. package/src/peer-record.ts +71 -16
  269. package/src/pending-join-runner.ts +798 -798
  270. package/src/primary-lan-address.ts +47 -47
  271. package/src/provider/client.ts +327 -326
  272. package/src/provider/descriptor.ts +129 -129
  273. package/src/provider/index.ts +59 -59
  274. package/src/provider/oauth.ts +285 -285
  275. package/src/provider/rent-node.ts +276 -275
  276. package/src/relay-addrs.ts +90 -12
  277. package/src/relay-reservation.ts +1 -1
  278. package/src/relay-server.ts +99 -98
  279. package/src/schema-verification.ts +38 -23
  280. package/src/sealed-party-key.ts +190 -190
  281. package/src/seed-bootstrap.ts +8 -11
  282. package/src/seed-trust-policy.ts +353 -353
  283. package/src/self-address-update.ts +19 -19
  284. package/src/strand-addr-protocol.ts +17 -14
  285. package/src/strand-app-schema-report.ts +147 -122
  286. package/src/strand-cohort.ts +1 -5
  287. package/src/strand-commit-durability.ts +165 -165
  288. package/src/strand-database.ts +522 -510
  289. package/src/strand-first-sync-gate.ts +3 -2
  290. package/src/strand-formation-deadlines.ts +227 -227
  291. package/src/strand-formation-manager.ts +731 -731
  292. package/src/strand-formation-protocol.ts +1174 -1138
  293. package/src/strand-fret-addrs.ts +179 -179
  294. package/src/strand-instance-manager.ts +188 -63
  295. package/src/strand-membership-reconciler.ts +1315 -1310
  296. package/src/strand-membership-writer.ts +2152 -2150
  297. package/src/strand-move.ts +1 -1
  298. package/src/strand-network-config.ts +16 -6
  299. package/src/strand-offline.ts +178 -178
  300. package/src/strand-party-key-rotation.ts +440 -436
  301. package/src/strand-party-machines.ts +308 -293
  302. package/src/strand-party-peers.ts +151 -129
  303. package/src/strand-peer-redial.ts +1 -1
  304. package/src/strand-reactivity.ts +36 -19
  305. package/src/strand-revocation-enforcer.ts +758 -758
  306. package/src/strand-wake-protocol.ts +406 -394
  307. package/src/strand-watcher.ts +1 -1
  308. package/src/table-change-watch.ts +285 -285
  309. package/src/timed-step.ts +28 -28
  310. package/src/timeout-scheduler.ts +29 -29
  311. package/src/trusted-owner-store.ts +242 -242
  312. package/src/types.ts +3182 -3121
  313. package/src/webrtc-upgrade.ts +439 -0
@@ -53,11 +53,12 @@
53
53
  * other. A node with configured relays is built with that discovery switched off
54
54
  * altogether when it has a transport list to wrap (`node-transports.ts`), because it
55
55
  * CAN reach a relay-capable peer in its own namespace and take the configured relay's
56
- * slot. See `relay-reservation.ts`'s module doc and `docs/architecture.md`.
56
+ * slot. See `relay-reservation.ts`'s module doc and `docs/network/relay.md`.
57
57
  */
58
58
 
59
59
  import { multiaddr, type Component } from '@multiformats/multiaddr';
60
60
  import { circuitRelayTargetOrThrow } from './delegate-admission.js';
61
+ import { resolveStunServers, type StunServer } from './relay-stun.js';
61
62
  import type { NetworkConfig } from './types.js';
62
63
  import type { RelayReservationState } from './relay-reservation.js';
63
64
 
@@ -83,6 +84,19 @@ export const DEFAULT_DIRECT_LISTEN_ADDR = '/ip4/0.0.0.0/tcp/0';
83
84
  */
84
85
  export const RELAY_SEARCH_LISTEN_ADDR = '/p2p-circuit';
85
86
 
87
+ /**
88
+ * The WebRTC listen entry, for every host that offers one: the edge apps
89
+ * (`edge-node/listen-addrs.ts`) and `cadre-cli`'s `network.webrtc`. One home for the
90
+ * spelling {@link listenTransportKind} classifies, like the two constants around it —
91
+ * a host that mis-spells it gets a refusal naming the transport instead of a listener.
92
+ *
93
+ * It binds no socket: the transport registers libp2p's signalling handler and derives
94
+ * this node's address from each relay reservation it holds, which is why the entry is
95
+ * worth adding to a machine reachable only through a relay and worth nothing on one with
96
+ * a public address.
97
+ */
98
+ export const WEBRTC_LISTEN_ADDR = '/webrtc';
99
+
86
100
  /**
87
101
  * The circuit-listen multiaddr for each configured relay: `<relayAddr>/p2p-circuit`.
88
102
  * An entry that already carries `/p2p-circuit` is passed through unchanged.
@@ -126,7 +140,7 @@ export function resolveListenAddrs(network: NetworkConfig | undefined): string[]
126
140
  if (!listenAddrs && relayEntries.length === 0) {
127
141
  return undefined;
128
142
  }
129
- // An explicitly empty `listenAddrs` (the React Native "cannot listen" case) stays
143
+ // An explicitly empty `listenAddrs` (a device that names no listener of its own) stays
130
144
  // empty — but a relay named alongside it still gets its listen entry, since
131
145
  // acquiring a slot is the whole point of configuring a relay.
132
146
  return dedupe([...(listenAddrs ?? [DEFAULT_DIRECT_LISTEN_ADDR]), ...relayEntries]);
@@ -143,6 +157,37 @@ export interface ListenTransportOptions {
143
157
  * {@link WS_TRANSPORT_SWITCH_PORT}.
144
158
  */
145
159
  wsPort?: number;
160
+ /**
161
+ * Present when some listen entry names `/webrtc` — the switch that makes
162
+ * `nodeDefaultTransports` add `@libp2p/webrtc`'s transport
163
+ * (`node-default-transports.ts`). Unlike {@link wsPort} the value is real: it is the
164
+ * ICE servers that transport needs, derived once here from `network.relayAddrs` and
165
+ * `network.stunUrls` (`relay-stun.ts` → `resolveStunServers`), so the factory reads one
166
+ * field instead of re-deriving them from a `NetworkConfig` it is not given.
167
+ *
168
+ * May be EMPTY, and an empty array is still the switch: WebRTC with no STUN server
169
+ * offers host and LAN candidates only, which is enough between two machines on one
170
+ * network. Absent is the only "no WebRTC" answer.
171
+ */
172
+ webrtcIceServers?: StunServer[];
173
+ }
174
+
175
+ /**
176
+ * The subset of a resolved {@link ListenTransportOptions} that is a `createLibp2pNode` option,
177
+ * for the two sites that spread it into one (`cadre-node.ts`, `strand-instance-manager.ts`).
178
+ *
179
+ * `wsPort` is db-p2p's own switch, so it goes through. {@link ListenTransportOptions.webrtcIceServers}
180
+ * does not: it is read only by `network.defaultTransports`, and db-p2p knows no such key —
181
+ * handing it one is the drift the strand builder already avoids for `relayAddrs`. One home for
182
+ * that distinction rather than a correct spread at each site.
183
+ *
184
+ * NOTE: the allow-list is the destructuring, so a THIRD {@link ListenTransportOptions} field that
185
+ * is a db-p2p option has to be added here too or it is dropped in silence. Deliberate — the
186
+ * opposite default would leak a cadre-only field into db-p2p's options, which is the failure the
187
+ * function exists to prevent.
188
+ */
189
+ export function listenTransportNodeOptions({ wsPort }: ListenTransportOptions): { wsPort?: number } {
190
+ return wsPort === undefined ? {} : { wsPort };
146
191
  }
147
192
 
148
193
  /**
@@ -199,11 +244,18 @@ const WS_TRANSPORT_SWITCH_PORT = 0;
199
244
  * case where cadre passes the transport list itself (`node-transports.ts`, to switch
200
245
  * relay discovery off); when the embedder gave none, that list comes from
201
246
  * `network.defaultTransports`, which reads the same switch.
202
- * - **Everything else is REFUSED.** Deriving `/quic-v1`, `/webrtc` or `/webtransport`
203
- * would mean importing transport packages into every `cadre-core` consumer including
204
- * the React Native and browser bundles (against the cross-platform rule in
205
- * `AGENTS.md`) and duplicating policy `libp2p-node.ts` owns. So name the address, name
206
- * the transport it needs, and refuse to start — matching how `network.relayAddrs` and
247
+ * - **WebRTC is derived ONLY for a node that passed `network.defaultTransports`.** That
248
+ * field is a Node host's injected transport list
249
+ * (`@serfab/cadre-core/node-default-transports`), and it is the only thing in this repo
250
+ * that can supply `@libp2p/webrtc`'s Node build — whose module graph loads
251
+ * `node-datachannel`'s native addon, so this file's graph, which every React Native and
252
+ * browser bundle reaches, must not import it. Without that field a `/webrtc` entry is
253
+ * refused exactly as before.
254
+ * - **Everything else is REFUSED.** Deriving `/quic-v1` or `/webtransport` would mean
255
+ * importing transport packages into every `cadre-core` consumer including the React
256
+ * Native and browser bundles (against the cross-platform rule in `AGENTS.md`) and
257
+ * duplicating policy `libp2p-node.ts` owns. So name the address, name the transport it
258
+ * needs, and refuse to start — matching how `network.relayAddrs` and
207
259
  * `network.announceAddrs` already treat an operator typo.
208
260
  *
209
261
  * Returns `{}` unconditionally when `network.transports` is set: a programmatic embedder
@@ -227,7 +279,8 @@ const WS_TRANSPORT_SWITCH_PORT = 0;
227
279
  * {@link resolveListenAddrs}, or the strand derivation, after any per-node rewriting —
228
280
  * so the check reads what this node will actually bind.
229
281
  * @throws {UnbindableListenAddressError} when a listen entry names a transport outside
230
- * {tcp, ws/wss, p2p-circuit}.
282
+ * {tcp, ws/wss, p2p-circuit}, or names `/webrtc` on a node that passed no
283
+ * `network.defaultTransports`.
231
284
  */
232
285
  export function resolveTransportOptions(
233
286
  network: NetworkConfig | undefined,
@@ -237,11 +290,21 @@ export function resolveTransportOptions(
237
290
  return {};
238
291
  }
239
292
  const kinds = (listenAddrs ?? []).map((addr) => [addr, listenTransportKind(addr)] as const);
240
- const unsupported = kinds.filter(([, kind]) => kind === 'unsupported').map(([addr]) => addr);
293
+ // A `/webrtc` entry is bindable only through an injected default transport list, so
294
+ // without one it joins the refusal rather than being accepted and silently unbound.
295
+ const webrtcBindable = network?.defaultTransports !== undefined;
296
+ const unsupported = kinds
297
+ .filter(([, kind]) => kind === 'unsupported' || (kind === 'webrtc' && !webrtcBindable))
298
+ .map(([addr]) => addr);
241
299
  if (unsupported.length > 0) {
242
300
  throw new UnbindableListenAddressError(unsupported);
243
301
  }
244
- return kinds.some(([, kind]) => kind === 'websockets') ? { wsPort: WS_TRANSPORT_SWITCH_PORT } : {};
302
+ return {
303
+ ...(kinds.some(([, kind]) => kind === 'websockets') && { wsPort: WS_TRANSPORT_SWITCH_PORT }),
304
+ ...(kinds.some(([, kind]) => kind === 'webrtc') && {
305
+ webrtcIceServers: resolveStunServers(network?.relayAddrs ?? [], network?.stunUrls)
306
+ })
307
+ };
245
308
  }
246
309
 
247
310
  /**
@@ -249,6 +312,10 @@ export function resolveTransportOptions(
249
312
  * default transports can bind. Names each offending address alongside the libp2p
250
313
  * transport package it would need — what the operator has without this is a node that
251
314
  * starts and silently never listens there.
315
+ *
316
+ * A `/webrtc` entry reaches this only on a node that passed no
317
+ * `network.defaultTransports`, so it gets the extra sentence naming the setting that
318
+ * does supply WebRTC on a Node host.
252
319
  */
253
320
  export class UnbindableListenAddressError extends Error {
254
321
  constructor(readonly listenAddrs: readonly string[]) {
@@ -256,7 +323,10 @@ export class UnbindableListenAddressError extends Error {
256
323
  'network.listenAddrs names an address this node has no transport for: ' +
257
324
  `${listenAddrs.map((a) => `${a} — ${unbindableReason(a)}`).join('; ')}. ` +
258
325
  'Default transports bind TCP (including /unix/<path>), WebSocket (/ws, /wss) and circuit-relay addresses only. ' +
259
- 'Either drop the address, or supply the transport programmatically via network.transports.'
326
+ 'Either drop the address, or supply the transport programmatically via network.transports.' +
327
+ (listenAddrs.some((a) => listenTransportKind(a) === 'webrtc')
328
+ ? ' A /webrtc listener needs a Node host that passes network.defaultTransports (cadre-cli: network.webrtc).'
329
+ : '')
260
330
  );
261
331
  this.name = 'UnbindableListenAddressError';
262
332
  }
@@ -275,7 +345,7 @@ export class UnbindableListenAddressError extends Error {
275
345
  * (`strand-network-config.ts`): libp2p reports a bad listen addr itself, and this check
276
346
  * must only ever ADD a denial.
277
347
  */
278
- function listenTransportKind(listenAddr: string): 'tcp' | 'websockets' | 'circuit' | 'ignored' | 'unsupported' {
348
+ function listenTransportKind(listenAddr: string): 'tcp' | 'websockets' | 'webrtc' | 'circuit' | 'ignored' | 'unsupported' {
279
349
  let stack: readonly string[];
280
350
  try {
281
351
  stack = transportStack(listenAddr);
@@ -294,6 +364,14 @@ function listenTransportKind(listenAddr: string): 'tcp' | 'websockets' | 'circui
294
364
  if (outermost === 'ws' || outermost === 'wss') {
295
365
  return 'websockets';
296
366
  }
367
+ // Read from the outermost component like every other kind here, so `/webrtc` and a
368
+ // `…/p2p-circuit/webrtc` entry classify alike — they need the same transport, and the
369
+ // relayed form is the shape libp2p itself derives for a `/webrtc` listener once the
370
+ // reservation lands. `/webrtc-direct` is deliberately NOT this kind: it is a separate
371
+ // libp2p transport (`webRTCDirect`) that nothing here supplies.
372
+ if (outermost === 'webrtc') {
373
+ return 'webrtc';
374
+ }
297
375
  // Only a BARE tcp or unix stack is TCP. `@libp2p/tcp` binds both — its `listenFilter`
298
376
  // accepts an exact TCP match OR a `/unix/<path>` address (a named pipe on Windows) —
299
377
  // and nothing layered on top of either, which is why `/tcp/<port>/<anything else>` is
@@ -75,7 +75,7 @@
75
75
  *
76
76
  * ⚠️ WHY LIBP2P'S RELAY DISCOVERY IS SWITCHED OFF. Discovery does reach SOME
77
77
  * peers: every connected peer whose identify lists the relay-hop protocol, and a
78
- * storage-profile member's strand node runs the relay server in the same
78
+ * `'server'` member's strand node runs the relay server in the same
79
79
  * `/optimystic/strand-<id>/…` namespace as this node's strand node (control nodes
80
80
  * share `/optimystic/control-<partyId>/…` the same way). Discovery reserves on such
81
81
  * a peer as soon as it connects, and that takes one of the slots the configured
@@ -1,98 +1,99 @@
1
- /**
2
- * The circuit-relay SERVER a party-run node runs — whether it runs one, and with which
3
- * `@libp2p/circuit-relay-v2` init — resolved once from `NetworkConfig` for both node
4
- * kinds: the control node (`cadre-node.ts` → `buildControlNodeOptions`) and every strand
5
- * node (`strand-instance-manager.ts` → `buildStrandRuntime`). One function decides both
6
- * answers so the two build sites cannot drift apart.
7
- *
8
- * WHY cadre sets an init at all. libp2p's server defaults `applyDefaultLimit` to true,
9
- * which stamps every reservation with `Limit { data: 128 KiB, duration: 2 min }`; libp2p
10
- * marks each connection relayed under it "limited" and resets it once either cap is
11
- * reached, and db-p2p's database protocols refuse a limited connection outright (they do
12
- * not set `runOnLimitedConnection`). A party-run relay exists to carry that party's own
13
- * database traffic, so the cap cut off every sync and chat history forwarded through it
14
- * (gotchoices/sereus#19). The dedicated relays (`ops/docker/libp2p-infra`, and the
15
- * `startDedicatedRelay` test fixture that stands in for it) already turn the limit off;
16
- * this is the same policy for the party-run kind.
17
- */
18
-
19
- import type { CircuitRelayServerInit, ServerReservationStoreInit } from '@libp2p/circuit-relay-v2';
20
- import type { NetworkConfig, NodeProfile } from './types.js';
21
-
22
- /**
23
- * Default size of a party-run relay's reservation store, raised from libp2p's 15.
24
- *
25
- * A NAT'd machine reserves one slot for its control node and one for each strand it
26
- * serves, all on the same relay (`strand-network-config.ts` hands strand nodes the
27
- * control node's `network.relayAddrs`), so the store needs
28
- * `NAT'd machines × (1 + strands each)` slots. At libp2p's 15, three phones serving four
29
- * strands each already fill it. 128 covers, for example, ten NAT'd machines serving eleven
30
- * strands each with the unauthorized-reservation budget's 8 slots
31
- * (`MAX_UNAUTHORIZED_RELAY_RESERVATIONS`) still free on top — one party's worth, where the
32
- * dedicated relay's 500 is sized for many parties. A reservation costs the server a map
33
- * entry and a timer, not a connection's worth of state, so the headroom is cheap.
34
- */
35
- export const PARTY_RELAY_MAX_RESERVATIONS = 128;
36
-
37
- /**
38
- * How long a party-run relay holds a reservation that is not refreshed: libp2p's own
39
- * default (2 h), set EXPLICITLY so that cadre, not a libp2p constant it cannot import,
40
- * owns the value the unauthorized-reservation budget has to agree with (see
41
- * {@link ResolvedRelayServer.init}).
42
- */
43
- export const PARTY_RELAY_RESERVATION_TTL_MS = 2 * 60 * 60 * 1000;
44
-
45
- /**
46
- * The reservation-store settings every party-run relay resolves unless the caller
47
- * overrides them one key at a time through `network.relayServerInit.reservations`.
48
- * `applyDefaultLimit: false` is the point of this module — see the module doc.
49
- */
50
- const PARTY_RELAY_RESERVATION_DEFAULTS = {
51
- applyDefaultLimit: false,
52
- maxReservations: PARTY_RELAY_MAX_RESERVATIONS,
53
- reservationTtl: PARTY_RELAY_RESERVATION_TTL_MS
54
- } as const satisfies ServerReservationStoreInit;
55
-
56
- /** Reservation settings with the three keys cadre always resolves made definite. */
57
- export type ResolvedRelayReservations = ServerReservationStoreInit
58
- & Required<Pick<ServerReservationStoreInit, keyof typeof PARTY_RELAY_RESERVATION_DEFAULTS>>;
59
-
60
- /** What {@link resolveRelayServer} decides for one node. */
61
- export interface ResolvedRelayServer {
62
- /** Does this node run the relay server? Passed as db-p2p's `relay`. */
63
- enabled: boolean;
64
- /**
65
- * The server's init, passed as db-p2p's `relayServerInit` when {@link enabled}: the
66
- * party-run defaults with the caller's `network.relayServerInit` merged over them.
67
- * `reservations.reservationTtl` is always set, which is what lets the control node's
68
- * unauthorized-reservation budget expire its entries on the server's own clock.
69
- */
70
- init: CircuitRelayServerInit & { reservations: ResolvedRelayReservations };
71
- }
72
-
73
- /**
74
- * Resolve the relay server for a node built from `network` with `profile`.
75
- *
76
- * `enabled`: an explicit `network.enableRelay` wins; otherwise storage-profile nodes run
77
- * the server (they are the machines with the connectivity and uptime to be a relay).
78
- *
79
- * `init`: MERGED, not replaced. `reservations` is merged key by key over the party-run
80
- * defaults, so a caller who sets only `maxReservations` does not silently get the data
81
- * limit back; every other top-level key is taken from the caller as given. A key set to
82
- * `undefined` counts as unset, so the default beneath it holds.
83
- */
84
- export function resolveRelayServer(network: NetworkConfig | undefined, profile: NodeProfile): ResolvedRelayServer {
85
- const override = network?.relayServerInit;
86
- return {
87
- enabled: network?.enableRelay ?? (profile === 'storage'),
88
- init: {
89
- ...override,
90
- reservations: { ...PARTY_RELAY_RESERVATION_DEFAULTS, ...definedEntries(override?.reservations) }
91
- }
92
- };
93
- }
94
-
95
- /** `obj` without its `undefined`-valued keys, so spreading it never erases a default. */
96
- function definedEntries<T extends object>(obj: T | undefined): Partial<T> {
97
- return Object.fromEntries(Object.entries(obj ?? {}).filter(([, value]) => value !== undefined)) as Partial<T>;
98
- }
1
+ /**
2
+ * The circuit-relay SERVER a party-run node runs — whether it runs one, and with which
3
+ * `@libp2p/circuit-relay-v2` init — resolved once from `NetworkConfig` for both node
4
+ * kinds: the control node (`cadre-node.ts` → `buildControlNodeOptions`) and every strand
5
+ * node (`strand-instance-manager.ts` → `buildStrandRuntime`). One function decides both
6
+ * answers so the two build sites cannot drift apart.
7
+ *
8
+ * WHY cadre sets an init at all. libp2p's server defaults `applyDefaultLimit` to true,
9
+ * which stamps every reservation with `Limit { data: 128 KiB, duration: 2 min }`; libp2p
10
+ * marks each connection relayed under it "limited" and resets it once either cap is
11
+ * reached, and db-p2p's database protocols refuse a limited connection outright (they do
12
+ * not set `runOnLimitedConnection`). A party-run relay exists to carry that party's own
13
+ * database traffic, so the cap cut off every sync and chat history forwarded through it
14
+ * (gotchoices/sereus#19). The dedicated relays (`ops/docker/libp2p-infra`, and the
15
+ * `startDedicatedRelay` test fixture that stands in for it) already turn the limit off;
16
+ * this is the same policy for the party-run kind.
17
+ */
18
+
19
+ import type { CircuitRelayServerInit, ServerReservationStoreInit } from '@libp2p/circuit-relay-v2';
20
+ import type { NetworkConfig } from './types.js';
21
+ import type { NodeRoles } from './node-roles.js';
22
+
23
+ /**
24
+ * Default size of a party-run relay's reservation store, raised from libp2p's 15.
25
+ *
26
+ * A NAT'd machine reserves one slot for its control node and one for each strand it
27
+ * serves, all on the same relay (`strand-network-config.ts` hands strand nodes the
28
+ * control node's `network.relayAddrs`), so the store needs
29
+ * `NAT'd machines × (1 + strands each)` slots. At libp2p's 15, three phones serving four
30
+ * strands each already fill it. 128 covers, for example, ten NAT'd machines serving eleven
31
+ * strands each with the unauthorized-reservation budget's 8 slots
32
+ * (`MAX_UNAUTHORIZED_RELAY_RESERVATIONS`) still free on top — one party's worth, where the
33
+ * dedicated relay's 500 is sized for many parties. A reservation costs the server a map
34
+ * entry and a timer, not a connection's worth of state, so the headroom is cheap.
35
+ */
36
+ export const PARTY_RELAY_MAX_RESERVATIONS = 128;
37
+
38
+ /**
39
+ * How long a party-run relay holds a reservation that is not refreshed: libp2p's own
40
+ * default (2 h), set EXPLICITLY so that cadre, not a libp2p constant it cannot import,
41
+ * owns the value the unauthorized-reservation budget has to agree with (see
42
+ * {@link ResolvedRelayServer.init}).
43
+ */
44
+ export const PARTY_RELAY_RESERVATION_TTL_MS = 2 * 60 * 60 * 1000;
45
+
46
+ /**
47
+ * The reservation-store settings every party-run relay resolves unless the caller
48
+ * overrides them one key at a time through `network.relayServerInit.reservations`.
49
+ * `applyDefaultLimit: false` is the point of this module — see the module doc.
50
+ */
51
+ const PARTY_RELAY_RESERVATION_DEFAULTS = {
52
+ applyDefaultLimit: false,
53
+ maxReservations: PARTY_RELAY_MAX_RESERVATIONS,
54
+ reservationTtl: PARTY_RELAY_RESERVATION_TTL_MS
55
+ } as const satisfies ServerReservationStoreInit;
56
+
57
+ /** Reservation settings with the three keys cadre always resolves made definite. */
58
+ export type ResolvedRelayReservations = ServerReservationStoreInit
59
+ & Required<Pick<ServerReservationStoreInit, keyof typeof PARTY_RELAY_RESERVATION_DEFAULTS>>;
60
+
61
+ /** What {@link resolveRelayServer} decides for one node. */
62
+ export interface ResolvedRelayServer {
63
+ /** Does this node run the relay server? Passed as db-p2p's `relay`. */
64
+ enabled: boolean;
65
+ /**
66
+ * The server's init, passed as db-p2p's `relayServerInit` when {@link enabled}: the
67
+ * party-run defaults with the caller's `network.relayServerInit` merged over them.
68
+ * `reservations.reservationTtl` is always set, which is what lets the control node's
69
+ * unauthorized-reservation budget expire its entries on the server's own clock.
70
+ */
71
+ init: CircuitRelayServerInit & { reservations: ResolvedRelayReservations };
72
+ }
73
+
74
+ /**
75
+ * Resolve the relay server for a node built from `network` with `roles`.
76
+ *
77
+ * `enabled`: an explicit `network.enableRelay` wins; otherwise an always-on node runs the
78
+ * server (those are the machines with the connectivity and uptime to be a relay).
79
+ *
80
+ * `init`: MERGED, not replaced. `reservations` is merged key by key over the party-run
81
+ * defaults, so a caller who sets only `maxReservations` does not silently get the data
82
+ * limit back; every other top-level key is taken from the caller as given. A key set to
83
+ * `undefined` counts as unset, so the default beneath it holds.
84
+ */
85
+ export function resolveRelayServer(network: NetworkConfig | undefined, roles: NodeRoles): ResolvedRelayServer {
86
+ const override = network?.relayServerInit;
87
+ return {
88
+ enabled: network?.enableRelay ?? roles.alwaysOn,
89
+ init: {
90
+ ...override,
91
+ reservations: { ...PARTY_RELAY_RESERVATION_DEFAULTS, ...definedEntries(override?.reservations) }
92
+ }
93
+ };
94
+ }
95
+
96
+ /** `obj` without its `undefined`-valued keys, so spreading it never erases a default. */
97
+ function definedEntries<T extends object>(obj: T | undefined): Partial<T> {
98
+ return Object.fromEntries(Object.entries(obj ?? {}).filter(([, value]) => value !== undefined)) as Partial<T>;
99
+ }
@@ -4,6 +4,9 @@ import type { SAppConfig } from './types.js';
4
4
 
5
5
  const log = debug('sereus:cadre:schema-verify');
6
6
 
7
+ /** The `SAppConfig` fields the author's signature covers. */
8
+ export type SignedSAppFields = Pick<SAppConfig, 'schema' | 'version' | 'schemaVersion'>;
9
+
7
10
  /**
8
11
  * Error thrown when sApp schema signature verification fails.
9
12
  */
@@ -11,59 +14,65 @@ export class SchemaVerificationError extends Error {
11
14
  constructor(
12
15
  public readonly sAppId: string,
13
16
  public readonly version: string,
17
+ schemaVersion: number,
14
18
  reason: string
15
19
  ) {
16
- super(`sApp schema verification failed for ${sAppId} v${version}: ${reason}`);
20
+ super(`sApp schema verification failed for ${sAppId} v${version} (schema version ${String(schemaVersion)}): ${reason}`);
17
21
  this.name = 'SchemaVerificationError';
18
22
  }
19
23
  }
20
24
 
21
25
  /**
22
- * Compute the canonical digest of a schema + version for signing/verification.
23
- * The payload is deterministic JSON: `{"schema":"...","version":"..."}`.
26
+ * Compute the canonical digest of the signed sApp fields for signing/verification.
27
+ * The payload is deterministic JSON, keys in this order:
28
+ * `{"schema":"...","schemaVersion":N,"version":"..."}`.
24
29
  */
25
- function schemaDigest(schema: string, version: string): string {
26
- const payload = JSON.stringify({ schema, version });
30
+ function schemaDigest({ schema, schemaVersion, version }: SignedSAppFields): string {
31
+ const payload = JSON.stringify({ schema, schemaVersion, version });
27
32
  return digest([payload], 'sha256', 'base64url') as string;
28
33
  }
29
34
 
30
35
  /**
31
- * Sign an sApp schema with the author's ed25519 private key.
32
- * Used by sApp authors when publishing their schema.
36
+ * Sign an sApp schema, its release version and its schema version with the author's ed25519
37
+ * private key. Used by sApp authors when publishing their schema.
33
38
  *
34
- * @param schema - The declarative schema DDL
35
- * @param version - Schema version string
39
+ * @param sApp - The declarative schema DDL, the release version string and the schema version
36
40
  * @param authorPrivateKey - Author's ed25519 private key (base64url)
37
41
  * @returns Signature (base64url)
38
42
  */
39
- export function signSchema(schema: string, version: string, authorPrivateKey: string): string {
40
- const d = schemaDigest(schema, version);
41
- return sign(d, authorPrivateKey, 'ed25519', 'base64url', 'base64url', 'base64url') as string;
43
+ export function signSchema(sApp: SignedSAppFields, authorPrivateKey: string): string {
44
+ return sign(schemaDigest(sApp), authorPrivateKey, 'ed25519', 'base64url', 'base64url', 'base64url') as string;
42
45
  }
43
46
 
44
47
  /**
45
48
  * Verify an sApp schema signature against the author's ed25519 public key.
46
49
  *
47
- * @param schema - The declarative schema DDL
48
- * @param version - Schema version string
50
+ * @param sApp - The declarative schema DDL, the release version string and the schema version
49
51
  * @param signature - Signature to verify (base64url)
50
52
  * @param authorPublicKey - Author's ed25519 public key (base64url)
51
53
  * @returns true if the signature is valid
52
54
  */
53
- export function verifySchema(schema: string, version: string, signature: string, authorPublicKey: string): boolean {
55
+ export function verifySchema(sApp: SignedSAppFields, signature: string, authorPublicKey: string): boolean {
54
56
  try {
55
- const d = schemaDigest(schema, version);
56
- return verify(d, signature, authorPublicKey, 'ed25519', 'base64url', 'base64url', 'base64url');
57
+ return verify(schemaDigest(sApp), signature, authorPublicKey, 'ed25519', 'base64url', 'base64url', 'base64url');
57
58
  } catch (error) {
58
59
  log('Schema verification error: %o', error);
59
60
  return false;
60
61
  }
61
62
  }
62
63
 
64
+ /** A schema version is a positive safe integer; anything else could not be compared against a strand's. */
65
+ function isValidSchemaVersion(schemaVersion: unknown): boolean {
66
+ return Number.isSafeInteger(schemaVersion) && (schemaVersion as number) >= 1;
67
+ }
68
+
63
69
  /**
64
70
  * Assert that an SAppConfig has a valid schema signature.
65
71
  * Throws SchemaVerificationError on failure.
66
72
  *
73
+ * A `schemaVersion` that is not a positive safe integer is refused with reason
74
+ * `'invalid schemaVersion'` under either policy.
75
+ *
67
76
  * Fail-closed by default: when `options.requireSignature` is not explicitly
68
77
  * `false`, an absent signature is rejected with reason `'missing signature'`,
69
78
  * distinct from the `'invalid signature'` (tampered/wrong-key) case. The
@@ -72,28 +81,34 @@ export function verifySchema(schema: string, version: string, signature: string,
72
81
  *
73
82
  * @param sAppConfig - The sApp configuration to verify
74
83
  * @param options - Verification policy; `requireSignature` defaults to `true`
75
- * @throws SchemaVerificationError if the signature is missing (when required) or invalid
84
+ * @throws SchemaVerificationError if the schema version is malformed, or the signature is missing (when required) or invalid
76
85
  */
77
86
  export function assertSchemaSignature(
78
87
  sAppConfig: SAppConfig,
79
88
  options?: { requireSignature?: boolean }
80
89
  ): void {
81
- const { id, version, schema, signature } = sAppConfig;
90
+ const { id, version, schemaVersion, signature } = sAppConfig;
82
91
  const requireSignature = options?.requireSignature ?? true;
92
+ const refuse = (reason: string): SchemaVerificationError =>
93
+ new SchemaVerificationError(id ?? '', version, schemaVersion, reason);
94
+
95
+ if (!isValidSchemaVersion(schemaVersion)) {
96
+ throw refuse('invalid schemaVersion');
97
+ }
83
98
 
84
99
  if (!signature) {
85
100
  if (requireSignature) {
86
- throw new SchemaVerificationError(id ?? '', version, 'missing signature');
101
+ throw refuse('missing signature');
87
102
  }
88
103
  log('No signature provided for %s v%s — skipping verification (policy relaxed)', id, version);
89
104
  return;
90
105
  }
91
106
 
92
107
  if (!id) {
93
- throw new SchemaVerificationError(id ?? '', version, 'missing author public key');
108
+ throw refuse('missing author public key');
94
109
  }
95
110
 
96
- if (!verifySchema(schema, version, signature, id)) {
97
- throw new SchemaVerificationError(id, version, 'invalid signature');
111
+ if (!verifySchema(sAppConfig, signature, id)) {
112
+ throw refuse('invalid signature');
98
113
  }
99
114
  }