@serfab/cadre-host 1.13.0 → 1.15.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 (352) hide show
  1. package/README.md +347 -423
  2. package/dist/bin/host.d.ts +6 -3
  3. package/dist/bin/host.js +332 -524
  4. package/dist/bin/host.js.map +1 -1
  5. package/dist/bin/join-output.d.ts +56 -0
  6. package/dist/bin/join-output.js +92 -0
  7. package/dist/bin/join-output.js.map +1 -0
  8. package/dist/bin/nat-output.d.ts +54 -0
  9. package/dist/bin/nat-output.js +138 -0
  10. package/dist/bin/nat-output.js.map +1 -0
  11. package/dist/bin/ui-access.d.ts +22 -0
  12. package/dist/bin/ui-access.js +44 -0
  13. package/dist/bin/ui-access.js.map +1 -0
  14. package/dist/hosted/claim-details.d.ts +66 -0
  15. package/dist/hosted/claim-details.js +60 -0
  16. package/dist/hosted/claim-details.js.map +1 -0
  17. package/dist/hosted/hosted-node-service.d.ts +325 -0
  18. package/dist/hosted/hosted-node-service.js +678 -0
  19. package/dist/hosted/hosted-node-service.js.map +1 -0
  20. package/dist/hosted/hosted-node-store.d.ts +40 -0
  21. package/dist/hosted/hosted-node-store.js +126 -0
  22. package/dist/hosted/hosted-node-store.js.map +1 -0
  23. package/dist/{donation/donation-supervisor.d.ts → hosted/hosted-node-supervisor.d.ts} +70 -53
  24. package/dist/hosted/hosted-node-supervisor.js +396 -0
  25. package/dist/hosted/hosted-node-supervisor.js.map +1 -0
  26. package/dist/hosted/hosted-node-watcher.d.ts +95 -0
  27. package/dist/hosted/hosted-node-watcher.js +260 -0
  28. package/dist/hosted/hosted-node-watcher.js.map +1 -0
  29. package/dist/hosted/index.d.ts +16 -0
  30. package/dist/hosted/index.js +13 -0
  31. package/dist/hosted/index.js.map +1 -0
  32. package/dist/hosted/node-status.d.ts +51 -0
  33. package/dist/hosted/node-status.js +77 -0
  34. package/dist/hosted/node-status.js.map +1 -0
  35. package/dist/hosted/types.d.ts +141 -0
  36. package/dist/hosted/types.js +22 -0
  37. package/dist/hosted/types.js.map +1 -0
  38. package/dist/index.d.ts +12 -24
  39. package/dist/index.js +9 -19
  40. package/dist/index.js.map +1 -1
  41. package/dist/installer/config.d.ts +27 -34
  42. package/dist/installer/config.js +46 -15
  43. package/dist/installer/config.js.map +1 -1
  44. package/dist/installer/index.d.ts +9 -19
  45. package/dist/installer/index.js +21 -85
  46. package/dist/installer/index.js.map +1 -1
  47. package/dist/installer/paths.d.ts +0 -2
  48. package/dist/installer/paths.js +0 -4
  49. package/dist/installer/paths.js.map +1 -1
  50. package/dist/installer/wizard.d.ts +12 -9
  51. package/dist/installer/wizard.js +15 -14
  52. package/dist/installer/wizard.js.map +1 -1
  53. package/dist/nat/address-resolver.d.ts +18 -17
  54. package/dist/nat/address-resolver.js +79 -25
  55. package/dist/nat/address-resolver.js.map +1 -1
  56. package/dist/nat/address-watch.d.ts +37 -0
  57. package/dist/nat/address-watch.js +84 -0
  58. package/dist/nat/address-watch.js.map +1 -0
  59. package/dist/nat/ddns/duckdns.d.ts +2 -1
  60. package/dist/nat/ddns/duckdns.js +5 -1
  61. package/dist/nat/ddns/duckdns.js.map +1 -1
  62. package/dist/nat/ddns/index.d.ts +6 -0
  63. package/dist/nat/ddns/index.js.map +1 -1
  64. package/dist/nat/index.d.ts +13 -9
  65. package/dist/nat/index.js +7 -5
  66. package/dist/nat/index.js.map +1 -1
  67. package/dist/nat/nat-service.d.ts +220 -77
  68. package/dist/nat/nat-service.js +653 -239
  69. package/dist/nat/nat-service.js.map +1 -1
  70. package/dist/nat/nat-store.d.ts +12 -5
  71. package/dist/nat/nat-store.js +124 -29
  72. package/dist/nat/nat-store.js.map +1 -1
  73. package/dist/nat/network-mode.d.ts +19 -0
  74. package/dist/nat/network-mode.js +32 -0
  75. package/dist/nat/network-mode.js.map +1 -0
  76. package/dist/nat/port-mapper.d.ts +83 -79
  77. package/dist/nat/port-mapper.js +148 -189
  78. package/dist/nat/port-mapper.js.map +1 -1
  79. package/dist/nat/reachability.d.ts +32 -15
  80. package/dist/nat/reachability.js +110 -22
  81. package/dist/nat/reachability.js.map +1 -1
  82. package/dist/nat/types.d.ts +98 -27
  83. package/dist/nat/types.js +8 -5
  84. package/dist/nat/types.js.map +1 -1
  85. package/dist/orchestrator/host-process-orchestrator.d.ts +32 -74
  86. package/dist/orchestrator/host-process-orchestrator.js +84 -233
  87. package/dist/orchestrator/host-process-orchestrator.js.map +1 -1
  88. package/dist/{installer/identity.d.ts → orchestrator/identity-file.d.ts} +5 -5
  89. package/dist/{installer/identity.js → orchestrator/identity-file.js} +6 -6
  90. package/dist/orchestrator/identity-file.js.map +1 -0
  91. package/dist/orchestrator/index.d.ts +2 -3
  92. package/dist/orchestrator/index.js +1 -1
  93. package/dist/orchestrator/index.js.map +1 -1
  94. package/dist/orchestrator/node-identity.d.ts +6 -6
  95. package/dist/orchestrator/node-identity.js +7 -7
  96. package/dist/orchestrator/node-identity.js.map +1 -1
  97. package/dist/orchestrator/port-allocator.d.ts +5 -6
  98. package/dist/orchestrator/port-allocator.js +9 -11
  99. package/dist/orchestrator/port-allocator.js.map +1 -1
  100. package/dist/orchestrator/state-store.d.ts +3 -5
  101. package/dist/orchestrator/state-store.js +2 -10
  102. package/dist/orchestrator/state-store.js.map +1 -1
  103. package/dist/orchestrator/types.d.ts +33 -22
  104. package/dist/orchestrator/types.js.map +1 -1
  105. package/dist/package-version.d.ts +1 -0
  106. package/dist/package-version.js +4 -0
  107. package/dist/package-version.js.map +1 -0
  108. package/dist/server/error-handler.d.ts +5 -25
  109. package/dist/server/error-handler.js +11 -74
  110. package/dist/server/error-handler.js.map +1 -1
  111. package/dist/server/events/types.d.ts +7 -11
  112. package/dist/server/events/types.js +2 -1
  113. package/dist/server/events/types.js.map +1 -1
  114. package/dist/server/index.d.ts +11 -37
  115. package/dist/server/index.js +30 -48
  116. package/dist/server/index.js.map +1 -1
  117. package/dist/server/routes/hosted-nodes.d.ts +28 -0
  118. package/dist/server/routes/hosted-nodes.js +68 -0
  119. package/dist/server/routes/hosted-nodes.js.map +1 -0
  120. package/dist/server/routes/nat.d.ts +4 -1
  121. package/dist/server/routes/nat.js +13 -2
  122. package/dist/server/routes/nat.js.map +1 -1
  123. package/dist/server/routes/nodes.d.ts +6 -20
  124. package/dist/server/routes/nodes.js +7 -86
  125. package/dist/server/routes/nodes.js.map +1 -1
  126. package/dist/server/routes/settings.d.ts +3 -7
  127. package/dist/server/routes/settings.js +18 -8
  128. package/dist/server/routes/settings.js.map +1 -1
  129. package/dist/server/routes/status.d.ts +2 -25
  130. package/dist/server/routes/status.js +3 -43
  131. package/dist/server/routes/status.js.map +1 -1
  132. package/dist/server/server.js +8 -8
  133. package/dist/server/settings-store.d.ts +2 -2
  134. package/dist/server/settings-store.js +2 -2
  135. package/dist/server/static.js +1 -2
  136. package/dist/server/static.js.map +1 -1
  137. package/dist/ui/assets/index-BHd8CWZA.css +1 -0
  138. package/dist/ui/assets/index-Ccr2GjPT.js +23 -0
  139. package/dist/ui/assets/index-Ccr2GjPT.js.map +1 -0
  140. package/dist/ui/index.html +2 -2
  141. package/dist/update/index.d.ts +2 -2
  142. package/dist/update/index.js +2 -2
  143. package/dist/update/store.d.ts +1 -1
  144. package/dist/update/store.js +1 -1
  145. package/dist/update/types.d.ts +1 -1
  146. package/dist/update/types.js +1 -1
  147. package/package.json +5 -7
  148. package/src/__tests__/cli-nat.smoke.test.ts +219 -212
  149. package/src/__tests__/cli.smoke.test.ts +2 -2
  150. package/src/__tests__/global-setup.ts +2 -3
  151. package/src/__tests__/orchestrator-claim-secret.test.ts +111 -0
  152. package/src/__tests__/orchestrator-env-scrub.test.ts +160 -163
  153. package/src/__tests__/orchestrator-handle-contract.ts +134 -134
  154. package/src/__tests__/orchestrator-node-identity.test.ts +177 -203
  155. package/src/__tests__/orchestrator-ports.test.ts +124 -143
  156. package/src/__tests__/orchestrator-push.test.ts +138 -146
  157. package/src/__tests__/orchestrator.test.ts +1017 -1063
  158. package/src/__tests__/ui-access.test.ts +30 -0
  159. package/src/bin/host.ts +397 -572
  160. package/src/bin/join-output.ts +120 -0
  161. package/src/bin/nat-output.ts +188 -0
  162. package/src/bin/ui-access.ts +50 -0
  163. package/src/{donation → hosted}/__tests__/fake-orchestrator.test.ts +172 -173
  164. package/src/{donation → hosted}/__tests__/fake-orchestrator.ts +43 -23
  165. package/src/hosted/__tests__/hosted-node-service.test.ts +708 -0
  166. package/src/hosted/__tests__/hosted-node-supervisor.test.ts +475 -0
  167. package/src/hosted/__tests__/test-invitation.ts +33 -0
  168. package/src/hosted/claim-details.ts +103 -0
  169. package/src/hosted/hosted-node-service.ts +814 -0
  170. package/src/hosted/hosted-node-store.ts +144 -0
  171. package/src/hosted/hosted-node-supervisor.ts +439 -0
  172. package/src/hosted/hosted-node-watcher.ts +288 -0
  173. package/src/hosted/index.ts +53 -0
  174. package/src/hosted/node-status.ts +97 -0
  175. package/src/hosted/types.ts +160 -0
  176. package/src/index.ts +72 -91
  177. package/src/installer/__tests__/config.test.ts +15 -40
  178. package/src/installer/__tests__/installer.smoke.test.ts +184 -263
  179. package/src/installer/__tests__/wizard.test.ts +25 -13
  180. package/src/installer/config.ts +201 -175
  181. package/src/installer/index.ts +252 -327
  182. package/src/installer/paths.ts +104 -109
  183. package/src/installer/wizard.ts +24 -26
  184. package/src/nat/__tests__/address-resolver.test.ts +107 -82
  185. package/src/nat/__tests__/nat-service.test.ts +608 -515
  186. package/src/nat/__tests__/nat-store.test.ts +49 -12
  187. package/src/nat/__tests__/network-mode.test.ts +38 -0
  188. package/src/nat/__tests__/port-mapper.test.ts +30 -191
  189. package/src/nat/__tests__/reachability.test.ts +96 -13
  190. package/src/nat/address-resolver.ts +94 -52
  191. package/src/nat/address-watch.ts +92 -0
  192. package/src/nat/ddns/duckdns.ts +5 -1
  193. package/src/nat/ddns/index.ts +6 -0
  194. package/src/nat/index.ts +33 -13
  195. package/src/nat/nat-service.ts +942 -501
  196. package/src/nat/nat-store.ts +126 -31
  197. package/src/nat/network-mode.ts +36 -0
  198. package/src/nat/port-mapper.ts +241 -329
  199. package/src/nat/reachability.ts +155 -34
  200. package/src/nat/types.ts +110 -37
  201. package/src/{installer/__tests__/identity.test.ts → orchestrator/__tests__/identity-file.test.ts} +34 -34
  202. package/src/orchestrator/__tests__/node-identity.test.ts +62 -62
  203. package/src/orchestrator/host-process-orchestrator.ts +1089 -1260
  204. package/src/{installer/identity.ts → orchestrator/identity-file.ts} +56 -56
  205. package/src/orchestrator/index.ts +9 -10
  206. package/src/orchestrator/node-identity.ts +41 -41
  207. package/src/orchestrator/port-allocator.ts +71 -73
  208. package/src/orchestrator/state-store.ts +76 -90
  209. package/src/orchestrator/types.ts +34 -23
  210. package/src/package-version.ts +4 -0
  211. package/src/server/__tests__/bus.test.ts +38 -38
  212. package/src/server/__tests__/error-handler.test.ts +81 -118
  213. package/src/server/__tests__/fakes.ts +15 -18
  214. package/src/server/__tests__/hosted-nodes-route.test.ts +161 -0
  215. package/src/server/__tests__/nodes-route.test.ts +114 -283
  216. package/src/server/__tests__/publishers.test.ts +193 -180
  217. package/src/server/__tests__/server.smoke.test.ts +134 -223
  218. package/src/server/__tests__/settings-route.test.ts +210 -195
  219. package/src/server/__tests__/sse-route.test.ts +119 -119
  220. package/src/server/__tests__/status-route.test.ts +82 -121
  221. package/src/server/error-handler.ts +112 -178
  222. package/src/server/events/types.ts +20 -24
  223. package/src/server/index.ts +223 -267
  224. package/src/server/routes/hosted-nodes.ts +83 -0
  225. package/src/server/routes/nat.ts +75 -63
  226. package/src/server/routes/nodes.ts +96 -188
  227. package/src/server/routes/settings.ts +20 -15
  228. package/src/server/routes/status.ts +5 -67
  229. package/src/server/server.ts +8 -8
  230. package/src/server/settings-store.ts +2 -2
  231. package/src/server/static.ts +1 -2
  232. package/src/update/index.ts +2 -2
  233. package/src/update/store.ts +1 -1
  234. package/src/update/types.ts +1 -1
  235. package/dist/auth/duration.d.ts +0 -1
  236. package/dist/auth/duration.js +0 -32
  237. package/dist/auth/duration.js.map +0 -1
  238. package/dist/auth/index.d.ts +0 -11
  239. package/dist/auth/index.js +0 -9
  240. package/dist/auth/index.js.map +0 -1
  241. package/dist/auth/self-label.d.ts +0 -36
  242. package/dist/auth/self-label.js +0 -57
  243. package/dist/auth/self-label.js.map +0 -1
  244. package/dist/auth/trust-circle-store.d.ts +0 -26
  245. package/dist/auth/trust-circle-store.js +0 -129
  246. package/dist/auth/trust-circle-store.js.map +0 -1
  247. package/dist/auth/trust-circle.d.ts +0 -156
  248. package/dist/auth/trust-circle.js +0 -307
  249. package/dist/auth/trust-circle.js.map +0 -1
  250. package/dist/auth/types.d.ts +0 -84
  251. package/dist/auth/types.js +0 -24
  252. package/dist/auth/types.js.map +0 -1
  253. package/dist/donation/donation-service.d.ts +0 -389
  254. package/dist/donation/donation-service.js +0 -828
  255. package/dist/donation/donation-service.js.map +0 -1
  256. package/dist/donation/donation-store.d.ts +0 -51
  257. package/dist/donation/donation-store.js +0 -152
  258. package/dist/donation/donation-store.js.map +0 -1
  259. package/dist/donation/donation-supervisor.js +0 -364
  260. package/dist/donation/donation-supervisor.js.map +0 -1
  261. package/dist/donation/grant-service.d.ts +0 -80
  262. package/dist/donation/grant-service.js +0 -210
  263. package/dist/donation/grant-service.js.map +0 -1
  264. package/dist/donation/grant-store.d.ts +0 -38
  265. package/dist/donation/grant-store.js +0 -135
  266. package/dist/donation/grant-store.js.map +0 -1
  267. package/dist/donation/index.d.ts +0 -20
  268. package/dist/donation/index.js +0 -17
  269. package/dist/donation/index.js.map +0 -1
  270. package/dist/donation/types.d.ts +0 -224
  271. package/dist/donation/types.js +0 -40
  272. package/dist/donation/types.js.map +0 -1
  273. package/dist/installer/identity.js.map +0 -1
  274. package/dist/owner/index.d.ts +0 -11
  275. package/dist/owner/index.js +0 -11
  276. package/dist/owner/index.js.map +0 -1
  277. package/dist/owner/owner-node-client.d.ts +0 -110
  278. package/dist/owner/owner-node-client.js +0 -184
  279. package/dist/owner/owner-node-client.js.map +0 -1
  280. package/dist/server/routes/bootstrap-node-validation.d.ts +0 -115
  281. package/dist/server/routes/bootstrap-node-validation.js +0 -197
  282. package/dist/server/routes/bootstrap-node-validation.js.map +0 -1
  283. package/dist/server/routes/grants-admin.d.ts +0 -29
  284. package/dist/server/routes/grants-admin.js +0 -58
  285. package/dist/server/routes/grants-admin.js.map +0 -1
  286. package/dist/server/routes/grants.d.ts +0 -28
  287. package/dist/server/routes/grants.js +0 -165
  288. package/dist/server/routes/grants.js.map +0 -1
  289. package/dist/server/routes/provision-request-validation.d.ts +0 -45
  290. package/dist/server/routes/provision-request-validation.js +0 -97
  291. package/dist/server/routes/provision-request-validation.js.map +0 -1
  292. package/dist/server/routes/strands.d.ts +0 -20
  293. package/dist/server/routes/strands.js +0 -38
  294. package/dist/server/routes/strands.js.map +0 -1
  295. package/dist/server/routes/trust-circle.d.ts +0 -17
  296. package/dist/server/routes/trust-circle.js +0 -35
  297. package/dist/server/routes/trust-circle.js.map +0 -1
  298. package/dist/strands/index.d.ts +0 -7
  299. package/dist/strands/index.js +0 -6
  300. package/dist/strands/index.js.map +0 -1
  301. package/dist/strands/strand-service.d.ts +0 -50
  302. package/dist/strands/strand-service.js +0 -103
  303. package/dist/strands/strand-service.js.map +0 -1
  304. package/dist/strands/types.d.ts +0 -93
  305. package/dist/strands/types.js +0 -23
  306. package/dist/strands/types.js.map +0 -1
  307. package/dist/ui/assets/index-C6nELNfC.js +0 -23
  308. package/dist/ui/assets/index-C6nELNfC.js.map +0 -1
  309. package/dist/ui/assets/index-TtIYSeZO.css +0 -1
  310. package/src/__tests__/cli-invite.smoke.test.ts +0 -192
  311. package/src/__tests__/host-owner.smoke.test.ts +0 -123
  312. package/src/__tests__/orchestrator-owner.test.ts +0 -234
  313. package/src/__tests__/orchestrator-pin-keys.test.ts +0 -152
  314. package/src/auth/__tests__/duration.test.ts +0 -28
  315. package/src/auth/__tests__/self-label.test.ts +0 -85
  316. package/src/auth/__tests__/trust-circle-integration.test.ts +0 -152
  317. package/src/auth/__tests__/trust-circle-store.test.ts +0 -115
  318. package/src/auth/__tests__/trust-circle.test.ts +0 -445
  319. package/src/auth/duration.ts +0 -33
  320. package/src/auth/index.ts +0 -21
  321. package/src/auth/self-label.ts +0 -74
  322. package/src/auth/trust-circle-store.ts +0 -155
  323. package/src/auth/trust-circle.ts +0 -395
  324. package/src/auth/types.ts +0 -99
  325. package/src/donation/__tests__/donation-service.test.ts +0 -1193
  326. package/src/donation/__tests__/donation-supervisor.test.ts +0 -548
  327. package/src/donation/__tests__/grant-service.test.ts +0 -213
  328. package/src/donation/__tests__/grant-store.test.ts +0 -113
  329. package/src/donation/donation-service.ts +0 -1033
  330. package/src/donation/donation-store.ts +0 -176
  331. package/src/donation/donation-supervisor.ts +0 -411
  332. package/src/donation/grant-service.ts +0 -252
  333. package/src/donation/grant-store.ts +0 -152
  334. package/src/donation/index.ts +0 -65
  335. package/src/donation/types.ts +0 -260
  336. package/src/owner/__tests__/owner-node-client.test.ts +0 -194
  337. package/src/owner/index.ts +0 -12
  338. package/src/owner/owner-node-client.ts +0 -259
  339. package/src/server/__tests__/bootstrap-node-validation.test.ts +0 -138
  340. package/src/server/__tests__/grants-admin-route.test.ts +0 -171
  341. package/src/server/__tests__/grants-route.test.ts +0 -394
  342. package/src/server/__tests__/strands-route.test.ts +0 -234
  343. package/src/server/routes/bootstrap-node-validation.ts +0 -206
  344. package/src/server/routes/grants-admin.ts +0 -74
  345. package/src/server/routes/grants.ts +0 -193
  346. package/src/server/routes/provision-request-validation.ts +0 -105
  347. package/src/server/routes/strands.ts +0 -54
  348. package/src/server/routes/trust-circle.ts +0 -49
  349. package/src/strands/__tests__/strand-service.test.ts +0 -165
  350. package/src/strands/index.ts +0 -14
  351. package/src/strands/strand-service.ts +0 -135
  352. package/src/strands/types.ts +0 -102
@@ -1,206 +0,0 @@
1
- /**
2
- * Shape validation for the requester-supplied `bootstrapNodes` of `POST /grants`
3
- * — the requester's control-network addresses the donated node is started with
4
- * (written into the child's `cadre.json` as `controlNetwork.bootstrapNodes` by
5
- * `orchestrator/host-process-orchestrator.ts`) and hands to `@libp2p/bootstrap`.
6
- *
7
- * Its own module rather than part of `grants.ts`: the per-entry rule is a duplicate
8
- * of cadre-provider's copy (`packages/cadre-provider/src/server/bootstrap-node-validation.ts`)
9
- * and must stay identical to it, so it is worth reading, testing and changing on
10
- * its own, without a routing file around it — the same arrangement cadre-provider
11
- * uses for its restatement of cadre-core's owner-key rule.
12
- *
13
- * ## An empty list is accepted — where this copy differs, on purpose
14
- *
15
- * The field may be absent or `[]`; cadre-provider's copy still requires at least one
16
- * address. A lent node's requester is typically a phone, and a phone has no address
17
- * to give — its node listens on nothing and can only dial out. Such a requester sends
18
- * no addresses and dials the node itself, at the WebSocket address
19
- * `GET /grants/:id/peer` reports. The node starts with no bootstrap peers and waits,
20
- * and admits that first inbound connection because it holds no authorized members
21
- * yet (`admitInboundControlConnection` / `authorizeInboundControlStream` in
22
- * `cadre-core/src/cadre-node.ts`). A provider container has no such requester yet, so
23
- * there an empty list is still the node-comes-up-alone failure described below
24
- * (`backlog/feat-provider-drone-reachable-by-phone`).
25
- *
26
- * The difference is at the list level only: every entry that IS given must pass the
27
- * rule below, which is identical in both packages.
28
- *
29
- * ## The rule, and why each clause is there
30
- *
31
- * An entry is usable exactly when it is a string, parses as a multiaddr, carries
32
- * a `/p2p/<peerId>` component whose peer id decodes, and says where that peer is.
33
- * Each clause corresponds to a distinct downstream failure the caller would
34
- * otherwise never be told about — the value is forwarded verbatim into the spawned
35
- * child, so every one of these surfaces where the caller cannot see it:
36
- *
37
- * - **Not parsable as a multiaddr → the child dies at boot.** `@libp2p/bootstrap`'s
38
- * constructor maps `multiaddr()` over the whole list *before* filtering anything,
39
- * and `multiaddr()` throws on a string that does not start with `/` (and on a
40
- * non-string outright). libp2p construction throws and the node never starts.
41
- * - **No `/p2p/<peerId>` → the node starts and never joins.** The quiet one.
42
- * `/ip4/127.0.0.1/tcp/4001` parses fine, then `@libp2p/bootstrap` filters it out
43
- * ("invalid bootstrap multiaddr without peer id", logged inside the child) and
44
- * the node comes up with *zero* bootstrap peers: healthy-looking, permanently
45
- * alone, never reaching the party it was created for. cadre-core's
46
- * `getBootstrapPeerIds` (`packages/cadre-core/src/cadre-node.ts`) skips such an
47
- * entry silently for the same reason.
48
- * - **Malformed peer id → the child dies at boot.** `multiaddr()` does *not*
49
- * validate the `/p2p/` value — parsing `/ip4/1.2.3.4/tcp/1/p2p/12D3KooReq` yields
50
- * a `p2p` component whose value is the junk string unchanged — but
51
- * `@libp2p/bootstrap` then calls `peerIdFromString` on it, unguarded, and it
52
- * throws `Incorrect length`. A truncated copy-paste of a 52-character peer id is
53
- * the likeliest human typo in this field.
54
- * - **No location at all → the node starts and never joins.** `/p2p/<peerId>` (and
55
- * `/p2p/<relay>/p2p-circuit/p2p/<target>`) parses and names a decodable peer, and
56
- * `@libp2p/bootstrap` keeps it — but it says nothing about *where* the peer is, so
57
- * libp2p finds no transport for it and the dial fails with no valid addresses.
58
- * The node comes up with a bootstrap peer it can never reach, which looks the same
59
- * from outside as having none. Only the total absence of a location is rejected:
60
- * which transports the child can actually dial is the embedder's choice
61
- * (`NetworkConfig.transports`), invisible from here, so a partial address like
62
- * `/ip4/1.2.3.4/p2p/<peerId>` is deliberately left to the child.
63
- *
64
- * The peer id is read the way `@libp2p/bootstrap` reads it — the `p2p` components
65
- * of `getComponents()` — rather than through the deprecated `getPeerId()`, so the
66
- * two cannot disagree about which part of the address is the peer. Bootstrap uses
67
- * only the *last* `p2p` component; this checks **every** one, so the relay half of
68
- * a `…/p2p/<relay>/p2p-circuit/p2p/<target>` address must decode too. An address
69
- * naming an undecodable relay is not dialable either, and bootstrap's own
70
- * `P2P.matches` filter accepts any address carrying a `p2p` component, so nothing
71
- * this accepts is dropped there.
72
- *
73
- * Requiring a multiaddr also closes a downstream ambiguity on the *other* side of
74
- * this rule: cadre-provider joins its list with `,` into `CADRE_BOOTSTRAP_NODES`
75
- * and cadre-cli's env loader splits it back on `,`, so an entry containing a comma
76
- * would silently become two addresses there. This host writes JSON rather than an
77
- * env var and so is not exposed to that, but the two per-entry rules are kept
78
- * identical — see the note about manual upkeep below.
79
- *
80
- * Reachability is deliberately NOT checked: a well-formed address for a peer that
81
- * happens to be down is indistinguishable here from one that is up, and no
82
- * boundary check can tell them apart. This validates the *shape* the child must be
83
- * able to parse, nothing more.
84
- *
85
- * Keeping the two per-entry rules in step is **manual** — neither package can see
86
- * the other's rule, so no test can compare them. What the tests give instead is a
87
- * tripwire on each side: `../__tests__/bootstrap-node-validation.test.ts` here and
88
- * `packages/cadre-provider/src/server/__tests__/bootstrap-node-validation.test.ts`
89
- * there each pin their own copy to the same per-entry accept/reject table (the
90
- * empty-list row is the one that differs), so changing either rule fails that
91
- * package's own suite — and the comment above the rule you just changed is what
92
- * points at the other copy.
93
- */
94
-
95
- import debug from 'debug';
96
- import { CODE_P2P, CODE_P2P_CIRCUIT, multiaddr, type Multiaddr } from '@multiformats/multiaddr';
97
- import { peerIdFromString } from '@libp2p/peer-id';
98
-
99
- const log = debug('cadre:host:bootstrap-nodes');
100
-
101
- /**
102
- * Longest rejected address echoed back in an error message. Generous compared to
103
- * the owner-key limit because a legitimate address is long: a plain
104
- * `/ip4/…/tcp/…/p2p/<52 chars>` runs ~80 characters and a relayed
105
- * `…/p2p-circuit/p2p/<52 chars>` about 150, so a smaller cap would truncate the
106
- * very values an operator needs to read back.
107
- */
108
- const REJECTED_ADDRESS_ECHO_LIMIT = 192;
109
-
110
- /**
111
- * Render a rejected address for an error message, capped: `bootstrapNodes`
112
- * arrives from a caller over the network, so an unbounded echo would let one junk
113
- * string become a megabyte of log line.
114
- */
115
- function describeRejectedAddress(value: string): string {
116
- return value.length <= REJECTED_ADDRESS_ECHO_LIMIT
117
- ? value
118
- : `${value.slice(0, REJECTED_ADDRESS_ECHO_LIMIT)}… (${value.length} chars)`;
119
- }
120
-
121
- /** The message of a thrown parse/decode failure, whatever the thrower used. */
122
- function reasonOf(error: unknown): string {
123
- return error instanceof Error ? error.message : String(error);
124
- }
125
-
126
- /**
127
- * Apply the rule above to one entry. Returns the trimmed value, so the create
128
- * provision request carries exactly what was validated.
129
- */
130
- function validateBootstrapNode(value: string): { node: string } | { error: string } {
131
- const trimmed = value.trim();
132
- if (trimmed.length === 0) {
133
- return { error: 'bootstrapNodes entries must not be empty or whitespace-only' };
134
- }
135
-
136
- let address: Multiaddr;
137
- try {
138
- address = multiaddr(trimmed);
139
- } catch (error) {
140
- log('rejecting bootstrapNodes entry: %s', reasonOf(error));
141
- return {
142
- error: `bootstrapNodes entries must be multiaddrs (could not parse "${describeRejectedAddress(trimmed)}": ${reasonOf(error)})`,
143
- };
144
- }
145
-
146
- const components = address.getComponents();
147
- const peerIds = components.filter(component => component.code === CODE_P2P);
148
- if (peerIds.length === 0) {
149
- return {
150
- error: `bootstrapNodes entries must include a /p2p/<peerId> component ("${describeRejectedAddress(trimmed)}" names no peer, so the node would drop it and start with no bootstrap peers)`,
151
- };
152
- }
153
-
154
- if (!components.some(component => component.code !== CODE_P2P && component.code !== CODE_P2P_CIRCUIT)) {
155
- return {
156
- error: `bootstrapNodes entries must name where to reach the peer ("${describeRejectedAddress(trimmed)}" names only peer ids, so no transport can dial it and the node would start with no reachable bootstrap peers)`,
157
- };
158
- }
159
-
160
- for (const { value } of peerIds) {
161
- try {
162
- peerIdFromString(value ?? '');
163
- } catch (error) {
164
- log('rejecting bootstrapNodes peer id: %s', reasonOf(error));
165
- return {
166
- error: `bootstrapNodes entries must carry a decodable peer id ("${describeRejectedAddress(trimmed)}" names peer "${describeRejectedAddress(value ?? '')}", which does not decode: ${reasonOf(error)})`,
167
- };
168
- }
169
- }
170
-
171
- return { node: trimmed };
172
- }
173
-
174
- /**
175
- * Validate the optional `bootstrapNodes` field of a provision request: absent, or an
176
- * array of dialable control-network addresses (see {@link validateBootstrapNode}).
177
- * Absent and `[]` alike mean the requester dials the node itself — see "An empty list
178
- * is accepted" in the module comment.
179
- *
180
- * Provisioning is the last point at which the requester can still fix a typo, so
181
- * the shape is checked here: without it a bad address is answered 201 and then
182
- * produces a donated node that either dies at boot or comes up permanently alone
183
- * — the requester learning about it, if ever, from donation status rather than
184
- * from a 400 naming the bad address.
185
- *
186
- * Every rejection names the offending entry: with several addresses in one
187
- * request the message is the only thing that says WHICH one.
188
- *
189
- * @returns the trimmed addresses (`[]` when none were given), so the provision request carries exactly what was validated.
190
- */
191
- export function validateBootstrapNodes(value: unknown): { nodes: string[] } | { error: string } {
192
- if (value === undefined) {
193
- return { nodes: [] };
194
- }
195
- if (!Array.isArray(value) || !value.every((entry): entry is string => typeof entry === 'string')) {
196
- return { error: 'bootstrapNodes must be an array of strings' };
197
- }
198
-
199
- const nodes: string[] = [];
200
- for (const entry of value) {
201
- const checked = validateBootstrapNode(entry);
202
- if ('error' in checked) return checked;
203
- nodes.push(checked.node);
204
- }
205
- return { nodes };
206
- }
@@ -1,74 +0,0 @@
1
- /**
2
- * Donation grant **admin** HTTP routes — thin adapters around
3
- * `GrantAdminHandlers` (`createGrantAdminHandlers(service, donations)`).
4
- *
5
- * Mount path: `/grants-admin` — the admin surface the `cadre-host grant` CLI
6
- * targets. It is **loopback, no bearer**: same-machine admin, matching
7
- * cadre-host's local-UI "no login" posture (see docs/cadre-host.md §
8
- * Security posture). This is distinct from the grantee-facing `/grants`
9
- * provisioning surface (`routes/grants.ts`), which carries the bearer gate.
10
- *
11
- * Clients: the `cadre-host grant` CLI and the local UI's Grants page. The list
12
- * reports each grant's live-node count and the donations a revoke would end.
13
- *
14
- * Revoking a grant also terminates the nodes donated under it unless the
15
- * caller passes `?keepNodes=true`; `DELETE /grants-admin/donations/:id` ends a
16
- * single donated node. Those are the host's only teardown paths once a grant is
17
- * revoked — the grantee's own `DELETE /grants/:id` is refused from then on.
18
- *
19
- * Each successful mutation publishes a `grants-changed` bus event, so an open
20
- * Grants page refreshes when the CLI acts.
21
- */
22
-
23
- import type { FastifyInstance } from 'fastify';
24
-
25
- import type { GrantAdminHandlers } from '../../donation/types.js';
26
- import type { EventBus } from '../events/bus.js';
27
-
28
- export interface GrantsAdminRoutesOptions {
29
- handlers: GrantAdminHandlers;
30
- events: EventBus;
31
- }
32
-
33
- export function registerGrantsAdminRoutes(app: FastifyInstance, opts: GrantsAdminRoutesOptions): void {
34
- const { handlers, events } = opts;
35
-
36
- app.get('/grants-admin', async () => {
37
- return handlers.listGrants();
38
- });
39
-
40
- app.post('/grants-admin', async (request) => {
41
- const body = (request.body ?? {}) as { label?: unknown; maxNodes?: unknown; ttlMs?: unknown };
42
- const label = typeof body.label === 'string' ? body.label : '';
43
- const args: { label: string; maxNodes?: number; ttlMs?: number } = { label };
44
- if (typeof body.maxNodes === 'number') args.maxNodes = body.maxNodes;
45
- if (typeof body.ttlMs === 'number') args.ttlMs = body.ttlMs;
46
- const result = await handlers.postGrant(args);
47
- events.publish({ type: 'grants-changed', kind: 'issued' });
48
- return result;
49
- });
50
-
51
- app.delete<{ Params: { token: string }; Querystring: { keepNodes?: string } }>(
52
- '/grants-admin/:token',
53
- async (request) => {
54
- const keepNodes = isTrueFlag(request.query.keepNodes);
55
- const { terminated } = await handlers.deleteGrant(request.params.token, { keepNodes });
56
- events.publish({ type: 'grants-changed', kind: 'revoked' });
57
- return { ok: true, terminated };
58
- },
59
- );
60
-
61
- // Two path segments past `/grants-admin`, so it never collides with `/:token`.
62
- const { terminateDonation } = handlers;
63
- if (terminateDonation) {
64
- app.delete<{ Params: { id: string } }>('/grants-admin/donations/:id', async (request) => {
65
- await terminateDonation(request.params.id);
66
- events.publish({ type: 'grants-changed', kind: 'terminated' });
67
- return { ok: true };
68
- });
69
- }
70
- }
71
-
72
- function isTrueFlag(value: string | undefined): boolean {
73
- return value === 'true' || value === '1';
74
- }
@@ -1,193 +0,0 @@
1
- /**
2
- * Grantee-facing donation routes — the HTTP surface an external cadre authority
3
- * (a friend's phone) drives to have this host donate a node into *its* cadre.
4
- *
5
- * Mount path: `/grants`. Distinct from the loopback-only `/grants-admin` surface
6
- * (issue/list/revoke, no bearer): this one is **bearer-gated** — every request
7
- * carries `Authorization: Bearer <grant-token>`, and a grantee sees only the
8
- * donations its own grant authorized (the single cross-grantee boundary,
9
- * analogous to cadre-provider's per-customer ownership check).
10
- *
11
- * Reachability is out of scope here: in v1 this mounts on the loopback
12
- * management server, same as the trust-circle / NAT surfaces. Physically exposing
13
- * it to a remote phone is `backlog/feat-cadre-host-wan-grant-reachability`.
14
- *
15
- * Errors: handlers either send a `{ ok:false, error:{code,message} }` envelope
16
- * inline (auth / body validation) or let the `DonationService` throw a
17
- * `DonationError`, which `server/error-handler.ts` maps to the matching HTTP
18
- * status and the same envelope.
19
- */
20
-
21
- import type { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';
22
-
23
- import type { DonationService } from '../../donation/donation-service.js';
24
- import type { DonationView, GrantValidator } from '../../donation/types.js';
25
- import { validateProvisionRequest } from './provision-request-validation.js';
26
-
27
- export interface GrantsRoutesOptions {
28
- donations: DonationService;
29
- /** Validates the presented bearer for non-provisioning ops (provision revalidates internally). */
30
- grants: GrantValidator;
31
- }
32
-
33
- /** Uniform error envelope (matches `error-handler.ts` and the provider API). */
34
- function errorResponse(reply: FastifyReply, code: string, message: string, status: number) {
35
- return reply.status(status).send({ ok: false, error: { code, message } });
36
- }
37
-
38
- /** Pull the `Bearer <token>` value out of the Authorization header, or undefined. */
39
- function parseBearer(header: string | undefined): string | undefined {
40
- if (!header) return undefined;
41
- const match = /^Bearer\s+(.+)$/i.exec(header.trim());
42
- return match ? match[1].trim() : undefined;
43
- }
44
-
45
- /** Map a grant denial reason to an HTTP status for the bearer gate. */
46
- function denyStatus(reason: string | undefined): number {
47
- if (reason === 'expired' || reason === 'revoked') return 403;
48
- return 401; // unknown_token / missing
49
- }
50
-
51
- /**
52
- * Authenticate the grant bearer for a non-provisioning op (identity + not
53
- * expired/revoked; no quota). Returns the token, or null after sending the
54
- * failure response. Provision does its own quota-aware validation inside
55
- * `DonationService.provision`.
56
- */
57
- function authenticate(
58
- request: FastifyRequest,
59
- reply: FastifyReply,
60
- grants: GrantValidator,
61
- ): string | null {
62
- const token = parseBearer(request.headers.authorization);
63
- if (!token) {
64
- errorResponse(reply, 'unauthorized', 'Missing bearer grant token', 401);
65
- return null;
66
- }
67
- const validation = grants.validate(token);
68
- if (!validation.ok) {
69
- const status = denyStatus(validation.reason);
70
- // Keep the envelope code aligned with the status (and with the POST path,
71
- // which surfaces an unknown token as `unauthorized`): 401 → unauthorized,
72
- // 403 → forbidden. A 401 labelled `forbidden` is self-contradictory.
73
- const code = status === 403 ? 'forbidden' : 'unauthorized';
74
- errorResponse(reply, code, `Grant is ${validation.reason ?? 'invalid'}`, status);
75
- return null;
76
- }
77
- return token;
78
- }
79
-
80
- /**
81
- * Resolve a donation the presented grant owns. Returns the redacted view, or
82
- * null after sending a 404 — an unknown id and one owned by a *different* grant
83
- * are deliberately indistinguishable (a grantee never learns another grantee's
84
- * donations exist).
85
- */
86
- function requireOwned(
87
- donations: DonationService,
88
- id: string,
89
- token: string,
90
- reply: FastifyReply,
91
- ): DonationView | null {
92
- const donation = donations.get(id);
93
- if (!donation || donation.grantToken !== token) {
94
- errorResponse(reply, 'not_found', `No such donation: ${id}`, 404);
95
- return null;
96
- }
97
- return donation;
98
- }
99
-
100
- export function registerGrantsRoutes(app: FastifyInstance, opts: GrantsRoutesOptions): void {
101
- const { donations, grants } = opts;
102
-
103
- // POST /grants — provision a donated node. The bearer's identity/expiry/
104
- // revocation AND quota are validated inside provision(), which throws a
105
- // DonationError (unauthorized / forbidden / quota_exceeded) the error handler
106
- // maps. We only parse the bearer and validate the request body here.
107
- app.post('/grants', async (request, reply) => {
108
- const token = parseBearer(request.headers.authorization);
109
- if (!token) {
110
- return errorResponse(reply, 'unauthorized', 'Missing bearer grant token', 401);
111
- }
112
-
113
- // One validator owns the whole body — see `provision-request-validation.ts`
114
- // for why no per-field check lives here. Nothing is provisioned on a
115
- // rejection: this runs before the service is touched.
116
- const validated = validateProvisionRequest(request.body ?? {}, token);
117
- if ('error' in validated) {
118
- return errorResponse(reply, 'invalid_request', validated.error, 400);
119
- }
120
-
121
- const donation = await donations.provision(validated.request);
122
- return reply.status(201).send({ ok: true, data: { donation } });
123
- });
124
-
125
- // GET /grants — list only the donations this grant authorized.
126
- app.get('/grants', async (request, reply) => {
127
- const token = authenticate(request, reply, grants);
128
- if (!token) return reply;
129
- return reply.send({ ok: true, data: { donations: donations.list(token) } });
130
- });
131
-
132
- // GET /grants/:id — one donation (redacted), owned by this grant.
133
- app.get<{ Params: { id: string } }>('/grants/:id', async (request, reply) => {
134
- const token = authenticate(request, reply, grants);
135
- if (!token) return reply;
136
- const donation = requireOwned(donations, request.params.id, token, reply);
137
- if (!donation) return reply;
138
- return reply.send({ ok: true, data: { donation } });
139
- });
140
-
141
- // GET /grants/:id/peer — live { peerId, multiaddrs } for the requester's addDrone.
142
- app.get<{ Params: { id: string } }>('/grants/:id/peer', async (request, reply) => {
143
- const token = authenticate(request, reply, grants);
144
- if (!token) return reply;
145
- if (!requireOwned(donations, request.params.id, token, reply)) return reply;
146
- const peer = await donations.getPeer(request.params.id);
147
- return reply.send({ ok: true, data: peer });
148
- });
149
-
150
- // PUT /grants/:id/seed — apply the requester's phone-signed seed.
151
- app.put<{ Params: { id: string } }>('/grants/:id/seed', async (request, reply) => {
152
- const token = authenticate(request, reply, grants);
153
- if (!token) return reply;
154
- if (!requireOwned(donations, request.params.id, token, reply)) return reply;
155
-
156
- const body = (request.body ?? {}) as { seed?: unknown };
157
- if (typeof body.seed !== 'string' || body.seed.length === 0) {
158
- return errorResponse(reply, 'invalid_request', 'seed is required', 400);
159
- }
160
- const result = await donations.applySeed(request.params.id, body.seed);
161
- // NOTE: exhaustive over today's three outcomes, and nothing forces it to stay
162
- // that way — an unhandled fourth would fall out of the handler as `undefined`
163
- // and hang the request rather than fail to compile. If `DonationSeedResult`
164
- // ever grows a variant, add a `default:` that 500s here.
165
- switch (result.outcome) {
166
- case 'rejected':
167
- return errorResponse(reply, 'seed_failed', result.error ?? 'Node rejected the seed', 502);
168
- // The loan ended while the seed was in flight. The ending won, so the
169
- // caller is told the record it seeded is no longer seedable (409) — or is
170
- // gone entirely (404) — rather than a 200 that implies a live node.
171
- case 'abandoned':
172
- return result.status
173
- ? errorResponse(
174
- reply,
175
- 'invalid_state',
176
- `Donation ${request.params.id} ended (${result.status}) while the seed was in flight`,
177
- 409,
178
- )
179
- : errorResponse(reply, 'not_found', `No such donation: ${request.params.id}`, 404);
180
- case 'seeded':
181
- return reply.send({ ok: true, data: { peersAdded: result.peersAdded } });
182
- }
183
- });
184
-
185
- // DELETE /grants/:id — terminate the donated node.
186
- app.delete<{ Params: { id: string } }>('/grants/:id', async (request, reply) => {
187
- const token = authenticate(request, reply, grants);
188
- if (!token) return reply;
189
- if (!requireOwned(donations, request.params.id, token, reply)) return reply;
190
- await donations.terminate(request.params.id);
191
- return reply.send({ ok: true });
192
- });
193
- }
@@ -1,105 +0,0 @@
1
- /**
2
- * The whole shape check for `POST /grants` — one call that either yields the
3
- * typed {@link DonationProvisionRequest} the `DonationService` is handed, or the
4
- * message the route answers 400 with.
5
- *
6
- * **Why a validator rather than a few more `if`s in the route.** Every field in
7
- * this body is forwarded into a child process the requester cannot see:
8
- * `partyId`, `bootstrapNodes` and `profile` are written into the child's
9
- * `cadre.json` (`orchestrator/host-process-orchestrator.ts`) and `ownerKeys`
10
- * become its cold-start seed-trust anchors. A field the route forgets to check is
11
- * not a 400 — it is a donated node that dies at boot, or comes up subtly wrong,
12
- * with a 201 already sent. Routing the body through one function that *constructs*
13
- * the request object means a field can only reach the spawn path by having been
14
- * through here; adding a field to `DonationProvisionRequest` without validating it
15
- * stops compiling rather than silently shipping.
16
- *
17
- * **Division of labour with `DonationService.provision`.** The Ed25519 rule for
18
- * `ownerKeys` stays in the service, where it is applied by cadre-core's own
19
- * `requireEd25519PublicKeyB64` — the service is also reached by the respawn path,
20
- * which replays a persisted record rather than an HTTP body, so the crypto rule
21
- * has to live there to cover both. What this validator owns for `ownerKeys` is the
22
- * *container* shape (present, non-empty, all strings), which the service's rule
23
- * assumes: a number reaching it surfaces as an internal `.trim is not a function`
24
- * rather than as a message naming what is wrong with the request.
25
- *
26
- * The multiaddr rule has no such home — it is duplicated in cadre-provider — so it
27
- * lives in `bootstrap-node-validation.ts` next to the comment saying so.
28
- */
29
-
30
- import type { DonationProvisionRequest } from '../../donation/donation-service.js';
31
- import { validateBootstrapNodes } from './bootstrap-node-validation.js';
32
-
33
- /** `partyId` becomes `controlNetwork.partyId` in the child's config. */
34
- function validatePartyId(value: unknown): { partyId: string } | { error: string } {
35
- if (typeof value !== 'string' || value.trim().length === 0) {
36
- return { error: 'partyId is required' };
37
- }
38
- return { partyId: value.trim() };
39
- }
40
-
41
- /**
42
- * Container shape only — see the module comment: the per-key Ed25519 rule is
43
- * `DonationService.provision`'s, so the respawn path gets it too.
44
- */
45
- function validateOwnerKeys(value: unknown): { ownerKeys: string[] } | { error: string } {
46
- if (!Array.isArray(value) || value.length === 0) {
47
- return { error: 'ownerKeys is required' };
48
- }
49
- if (!value.every((key): key is string => typeof key === 'string')) {
50
- return { error: 'ownerKeys must be an array of strings' };
51
- }
52
- return { ownerKeys: value };
53
- }
54
-
55
- /** Absent means `storage` — see {@link DonationProvisionRequest.profile}. */
56
- function validateProfile(value: unknown): { profile?: DonationProvisionRequest['profile'] } | { error: string } {
57
- if (value === undefined) return {};
58
- if (value !== 'storage' && value !== 'transaction') {
59
- return { error: 'profile must be "storage" or "transaction"' };
60
- }
61
- return { profile: value };
62
- }
63
-
64
- /**
65
- * Validate a `POST /grants` body and build the request `DonationService.provision`
66
- * is handed.
67
- *
68
- * `grantToken` comes from the `Authorization` bearer rather than the body — a
69
- * requester does not get to name which grant pays for this — so it is a
70
- * parameter, not a validated field. Whether that grant is live and within quota is
71
- * `provision`'s call, not this function's.
72
- *
73
- * @returns the typed provision request, or the message to answer `invalid_request` with.
74
- */
75
- export function validateProvisionRequest(
76
- body: unknown,
77
- grantToken: string,
78
- ): { request: DonationProvisionRequest } | { error: string } {
79
- if (typeof body !== 'object' || body === null || Array.isArray(body)) {
80
- return { error: 'request body must be a JSON object' };
81
- }
82
- const fields = body as Record<string, unknown>;
83
-
84
- const partyId = validatePartyId(fields.partyId);
85
- if ('error' in partyId) return partyId;
86
-
87
- const bootstrap = validateBootstrapNodes(fields.bootstrapNodes);
88
- if ('error' in bootstrap) return bootstrap;
89
-
90
- const ownerKeys = validateOwnerKeys(fields.ownerKeys);
91
- if ('error' in ownerKeys) return ownerKeys;
92
-
93
- const profile = validateProfile(fields.profile);
94
- if ('error' in profile) return profile;
95
-
96
- return {
97
- request: {
98
- grantToken,
99
- partyId: partyId.partyId,
100
- bootstrapNodes: bootstrap.nodes,
101
- ownerKeys: ownerKeys.ownerKeys,
102
- ...(profile.profile ? { profile: profile.profile } : {}),
103
- },
104
- };
105
- }
@@ -1,54 +0,0 @@
1
- /**
2
- * Strand HTTP routes — thin adapters around `StrandHandlers`
3
- * (`createStrandHandlers(service)`).
4
- *
5
- * Mount path: `/api/strands` — a UI-only surface with no CLI contract to
6
- * preserve. (The `/auth/*` and `/nat/*` mounts sit outside `/api` only because
7
- * the existing CLI already targets those URLs.) Envelope form therefore follows
8
- * the other `/api/*` routes: `{ ok: true, data }`.
9
- *
10
- * A removal that actually issued a delete publishes `strands-changed` so other
11
- * open tabs refresh. A call that found no row publishes nothing — nothing changed.
12
- */
13
-
14
- import type { FastifyInstance } from 'fastify';
15
-
16
- import type { StrandHandlers } from '../../strands/types.js';
17
- import type { EventBus } from '../events/bus.js';
18
-
19
- export interface StrandRoutesOptions {
20
- handlers: StrandHandlers;
21
- events: EventBus;
22
- }
23
-
24
- /**
25
- * The only accepted `confirm` values — the same strict pair the admin channel
26
- * accepts. Not a general truthiness parser: `yes`, `on` and an empty value all
27
- * count as NOT confirmed, because a guessy parser here would turn a typo into a
28
- * destroyed membership key.
29
- *
30
- * NOTE: deliberately duplicated from cadre-cli's `admin-server.ts` rather than
31
- * imported — cadre-host depends on cadre-cli for its bin path only, never its
32
- * source. Keep the two in step by hand; do not DRY them into an import.
33
- */
34
- const CONFIRM_VALUES = new Set(['1', 'true']);
35
-
36
- export function registerStrandRoutes(app: FastifyInstance, opts: StrandRoutesOptions): void {
37
- const { handlers, events } = opts;
38
-
39
- app.get('/api/strands', async () => {
40
- return { ok: true, data: await handlers.listStrands() };
41
- });
42
-
43
- app.delete<{ Params: { id: string }; Querystring: { confirm?: string } }>(
44
- '/api/strands/:id',
45
- async (request) => {
46
- const confirm = typeof request.query.confirm === 'string' && CONFIRM_VALUES.has(request.query.confirm);
47
- const result = await handlers.removeStrand(request.params.id, { confirm });
48
- if (result.removed) {
49
- events.publish({ type: 'strands-changed', kind: 'removed' });
50
- }
51
- return { ok: true, data: result };
52
- },
53
- );
54
- }