@serfab/cadre-core 0.8.1 → 0.10.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 (197) hide show
  1. package/README.md +6 -4
  2. package/dist/bootstrap-peer-store-file.d.ts +13 -0
  3. package/dist/bootstrap-peer-store-file.js +42 -0
  4. package/dist/bootstrap-peer-store-file.js.map +1 -0
  5. package/dist/bootstrap-peer-store.d.ts +73 -0
  6. package/dist/bootstrap-peer-store.js +128 -0
  7. package/dist/bootstrap-peer-store.js.map +1 -0
  8. package/dist/cadre-node.d.ts +1006 -101
  9. package/dist/cadre-node.js +1977 -283
  10. package/dist/cadre-node.js.map +1 -1
  11. package/dist/control-authorization.d.ts +93 -0
  12. package/dist/control-authorization.js +57 -0
  13. package/dist/control-authorization.js.map +1 -0
  14. package/dist/control-cohort.d.ts +15 -15
  15. package/dist/control-cohort.js +14 -14
  16. package/dist/control-cohort.js.map +1 -1
  17. package/dist/control-database.d.ts +1033 -76
  18. package/dist/control-database.js +1520 -153
  19. package/dist/control-database.js.map +1 -1
  20. package/dist/control-formation-recorder.d.ts +91 -12
  21. package/dist/control-formation-recorder.js +131 -16
  22. package/dist/control-formation-recorder.js.map +1 -1
  23. package/dist/control-schema.d.ts +1 -1
  24. package/dist/control-schema.js +774 -186
  25. package/dist/control-schema.js.map +1 -1
  26. package/dist/control-stream.d.ts +38 -4
  27. package/dist/control-stream.js +70 -4
  28. package/dist/control-stream.js.map +1 -1
  29. package/dist/control-write-retry.d.ts +151 -0
  30. package/dist/control-write-retry.js +411 -0
  31. package/dist/control-write-retry.js.map +1 -0
  32. package/dist/delegate-admission.d.ts +134 -0
  33. package/dist/delegate-admission.js +244 -0
  34. package/dist/delegate-admission.js.map +1 -0
  35. package/dist/device-token.d.ts +12 -12
  36. package/dist/device-token.js +15 -14
  37. package/dist/device-token.js.map +1 -1
  38. package/dist/ed25519-key.d.ts +68 -0
  39. package/dist/ed25519-key.js +106 -0
  40. package/dist/ed25519-key.js.map +1 -0
  41. package/dist/file-durable-slot.d.ts +20 -0
  42. package/dist/file-durable-slot.js +53 -0
  43. package/dist/file-durable-slot.js.map +1 -0
  44. package/dist/formation-approval.d.ts +157 -0
  45. package/dist/formation-approval.js +375 -0
  46. package/dist/formation-approval.js.map +1 -0
  47. package/dist/fs-atomic.d.ts +38 -0
  48. package/dist/fs-atomic.js +97 -0
  49. package/dist/fs-atomic.js.map +1 -0
  50. package/dist/identity-key.d.ts +49 -0
  51. package/dist/identity-key.js +81 -0
  52. package/dist/identity-key.js.map +1 -0
  53. package/dist/index.d.ts +16 -6
  54. package/dist/index.js +44 -10
  55. package/dist/index.js.map +1 -1
  56. package/dist/key-store-file.d.ts +2 -24
  57. package/dist/key-store-file.js +10 -77
  58. package/dist/key-store-file.js.map +1 -1
  59. package/dist/key-store.d.ts +1 -1
  60. package/dist/key-store.js +1 -1
  61. package/dist/membership-connection-gater.d.ts +146 -0
  62. package/dist/membership-connection-gater.js +180 -0
  63. package/dist/membership-connection-gater.js.map +1 -0
  64. package/dist/node-local-snapshot.d.ts +109 -0
  65. package/dist/node-local-snapshot.js +182 -0
  66. package/dist/node-local-snapshot.js.map +1 -0
  67. package/dist/peer-authorization.d.ts +150 -11
  68. package/dist/peer-authorization.js +170 -13
  69. package/dist/peer-authorization.js.map +1 -1
  70. package/dist/peer-record.d.ts +24 -11
  71. package/dist/peer-record.js +29 -13
  72. package/dist/peer-record.js.map +1 -1
  73. package/dist/push-node.d.ts +38 -0
  74. package/dist/push-node.js +48 -0
  75. package/dist/push-node.js.map +1 -0
  76. package/dist/push-notifier-apns.d.ts +10 -2
  77. package/dist/push-notifier-apns.js +14 -10
  78. package/dist/push-notifier-apns.js.map +1 -1
  79. package/dist/push-notifier-fcm.d.ts +7 -1
  80. package/dist/push-notifier-fcm.js +9 -10
  81. package/dist/push-notifier-fcm.js.map +1 -1
  82. package/dist/push-notifier.d.ts +16 -35
  83. package/dist/push-notifier.js +16 -44
  84. package/dist/push-notifier.js.map +1 -1
  85. package/dist/relay-addrs.d.ts +45 -0
  86. package/dist/relay-addrs.js +103 -0
  87. package/dist/relay-addrs.js.map +1 -0
  88. package/dist/relay-reservation.d.ts +266 -0
  89. package/dist/relay-reservation.js +613 -0
  90. package/dist/relay-reservation.js.map +1 -0
  91. package/dist/seed-bootstrap.d.ts +263 -79
  92. package/dist/seed-bootstrap.js +413 -211
  93. package/dist/seed-bootstrap.js.map +1 -1
  94. package/dist/seed-trust-policy.d.ts +50 -24
  95. package/dist/seed-trust-policy.js +28 -19
  96. package/dist/seed-trust-policy.js.map +1 -1
  97. package/dist/strand-addr-protocol.d.ts +42 -13
  98. package/dist/strand-addr-protocol.js +58 -52
  99. package/dist/strand-addr-protocol.js.map +1 -1
  100. package/dist/strand-backfill.d.ts +109 -0
  101. package/dist/strand-backfill.js +279 -0
  102. package/dist/strand-backfill.js.map +1 -0
  103. package/dist/strand-database.d.ts +5 -5
  104. package/dist/strand-database.js +4 -4
  105. package/dist/strand-database.js.map +1 -1
  106. package/dist/strand-formation-manager.d.ts +38 -9
  107. package/dist/strand-formation-manager.js +120 -17
  108. package/dist/strand-formation-manager.js.map +1 -1
  109. package/dist/strand-formation-protocol.d.ts +104 -5
  110. package/dist/strand-formation-protocol.js +233 -5
  111. package/dist/strand-formation-protocol.js.map +1 -1
  112. package/dist/strand-instance-manager.d.ts +40 -2
  113. package/dist/strand-instance-manager.js +93 -15
  114. package/dist/strand-instance-manager.js.map +1 -1
  115. package/dist/strand-member-key.d.ts +6 -6
  116. package/dist/strand-member-key.js +6 -6
  117. package/dist/strand-member-key.js.map +1 -1
  118. package/dist/strand-member-registry.d.ts +12 -12
  119. package/dist/strand-member-registry.js +22 -14
  120. package/dist/strand-member-registry.js.map +1 -1
  121. package/dist/strand-membership-writer.d.ts +532 -138
  122. package/dist/strand-membership-writer.js +800 -179
  123. package/dist/strand-membership-writer.js.map +1 -1
  124. package/dist/strand-solicitation.d.ts +99 -33
  125. package/dist/strand-solicitation.js +100 -52
  126. package/dist/strand-solicitation.js.map +1 -1
  127. package/dist/strand-transport-key.d.ts +40 -0
  128. package/dist/strand-transport-key.js +56 -0
  129. package/dist/strand-transport-key.js.map +1 -0
  130. package/dist/strand-wake-protocol.d.ts +9 -7
  131. package/dist/strand-wake-protocol.js +22 -46
  132. package/dist/strand-wake-protocol.js.map +1 -1
  133. package/dist/strand-watcher.d.ts +17 -2
  134. package/dist/strand-watcher.js +47 -3
  135. package/dist/strand-watcher.js.map +1 -1
  136. package/dist/trusted-owner-store-file.d.ts +12 -0
  137. package/dist/trusted-owner-store-file.js +39 -0
  138. package/dist/trusted-owner-store-file.js.map +1 -0
  139. package/dist/trusted-owner-store.d.ts +64 -0
  140. package/dist/trusted-owner-store.js +126 -0
  141. package/dist/trusted-owner-store.js.map +1 -0
  142. package/dist/types.d.ts +325 -50
  143. package/dist/types.js +13 -0
  144. package/dist/types.js.map +1 -1
  145. package/package.json +23 -8
  146. package/src/bootstrap-peer-store-file.ts +44 -0
  147. package/src/bootstrap-peer-store.ts +172 -0
  148. package/src/cadre-node.ts +2129 -295
  149. package/src/control-authorization.ts +114 -0
  150. package/src/control-cohort.ts +17 -17
  151. package/src/control-database.ts +1766 -191
  152. package/src/control-formation-recorder.ts +174 -21
  153. package/src/control-schema.ts +786 -198
  154. package/src/control-stream.ts +81 -4
  155. package/src/control-write-retry.ts +473 -0
  156. package/src/delegate-admission.ts +302 -0
  157. package/src/device-token.ts +19 -14
  158. package/src/ed25519-key.ts +134 -0
  159. package/src/file-durable-slot.ts +61 -0
  160. package/src/formation-approval.ts +604 -0
  161. package/src/fs-atomic.ts +107 -0
  162. package/src/identity-key.ts +109 -0
  163. package/src/index.ts +157 -24
  164. package/src/key-store-file.ts +10 -81
  165. package/src/key-store.ts +1 -1
  166. package/src/membership-connection-gater.ts +204 -0
  167. package/src/node-local-snapshot.ts +260 -0
  168. package/src/peer-authorization.ts +219 -13
  169. package/src/peer-record.ts +31 -13
  170. package/src/push-node.ts +64 -0
  171. package/src/push-notifier-apns.ts +16 -12
  172. package/src/push-notifier-fcm.ts +9 -10
  173. package/src/push-notifier.ts +16 -57
  174. package/src/relay-addrs.ts +111 -0
  175. package/src/relay-reservation.ts +823 -0
  176. package/src/seed-bootstrap.ts +490 -226
  177. package/src/seed-trust-policy.ts +65 -32
  178. package/src/strand-addr-protocol.ts +84 -53
  179. package/src/strand-backfill.ts +366 -0
  180. package/src/strand-database.ts +6 -6
  181. package/src/strand-formation-manager.ts +145 -19
  182. package/src/strand-formation-protocol.ts +310 -9
  183. package/src/strand-instance-manager.ts +113 -16
  184. package/src/strand-member-key.ts +7 -7
  185. package/src/strand-member-registry.ts +28 -23
  186. package/src/strand-membership-writer.ts +1430 -640
  187. package/src/strand-solicitation.ts +155 -90
  188. package/src/strand-transport-key.ts +58 -0
  189. package/src/strand-wake-protocol.ts +29 -46
  190. package/src/strand-watcher.ts +66 -3
  191. package/src/trusted-owner-store-file.ts +41 -0
  192. package/src/trusted-owner-store.ts +171 -0
  193. package/src/types.ts +342 -51
  194. package/dist/authority-key.d.ts +0 -41
  195. package/dist/authority-key.js +0 -52
  196. package/dist/authority-key.js.map +0 -1
  197. package/src/authority-key.ts +0 -68
@@ -0,0 +1,114 @@
1
+ /**
2
+ * The single definition of the field vector every signed control-plane message
3
+ * covers. Both producers of signed bytes — `control-database.ts`'s
4
+ * `buildAuthorizationMessage` (raw digest bytes) and `peer-authorization.ts`'s
5
+ * digest helpers (base64url digest strings) — build their vector here, so the
6
+ * byte layout cannot drift between them or away from the SQL constraints in
7
+ * `schemas/control.qsql`.
8
+ *
9
+ * Every vector leads with two fixed literals:
10
+ *
11
+ * digest(<domain>, <action>, <row field 1>, ..., <row field n>)
12
+ *
13
+ * so a signature verifies ONLY against the one rule it was minted for. Without
14
+ * the tags, several rules built byte-identical tuples (e.g. `ValidationKey`
15
+ * insert and `OwnerKey` insert both signed `digest(Key, StampId)`), so an
16
+ * approval for a narrow grant doubled as an approval for full ownership.
17
+ *
18
+ * This module deliberately has no Quereus / Optimystic / libp2p imports so the
19
+ * lightweight verifiers (`peer-authorization.ts`, consumed by the offline
20
+ * `cadre enroll register` check) can use it without pulling in the runtime.
21
+ */
22
+
23
+ /**
24
+ * Names of the CadreControl tables, in schema order. The single list: the
25
+ * {@link ControlTable} union is derived from it and `ControlDatabase.countRows`
26
+ * guards its dynamic `from` clause against it (keeping the table name off the
27
+ * SQL-injection surface), so a new table cannot be added to one and missed in
28
+ * the other.
29
+ *
30
+ * `Revocation` is here for the same two reasons as every other entry: it
31
+ * derives a `'CadreControl.Revocation'` domain tag — its `Authorized` CHECK
32
+ * verifies an owner signature over the `'remove'`-tagged digest that
33
+ * `peer-authorization.ts`'s `revocationDigest` mints, and its `AuthorizedReissue`
34
+ * CHECK the `'reissue'`-tagged digest `ControlDatabase.reissueRevocations`
35
+ * signs — and `countRows` counts it.
36
+ */
37
+ export const CONTROL_TABLES = [
38
+ 'OwnerKey',
39
+ 'ValidationKey',
40
+ 'Strand',
41
+ 'CadrePeer',
42
+ 'DeviceToken',
43
+ 'FormationInvite',
44
+ 'FormationUsage',
45
+ 'Revocation',
46
+ ] as const;
47
+
48
+ /**
49
+ * Name of a CadreControl table. Doubles as the table half of a
50
+ * {@link ControlDomain} tag.
51
+ */
52
+ export type ControlTable = typeof CONTROL_TABLES[number];
53
+
54
+ /**
55
+ * The control tables whose rows carry a single-use `StampId` retired into
56
+ * `CadreControl.Revocation` on delete — i.e. the ones the schema's
57
+ * `NotRevoked` / `RevocationRecorded` CHECK pair guards, and the only values
58
+ * `Revocation.RowIsGone` accepts in `TableName`. `Extract` from
59
+ * {@link ControlTable} rather than a fresh literal list, so a renamed table is a
60
+ * compile error here instead of a silently dead branch.
61
+ *
62
+ * Lives in this import-free module rather than beside its main consumer
63
+ * (`control-database.ts`) so the lightweight signers — `peer-authorization.ts`'s
64
+ * `revocationDigest` — can type against it without pulling in the runtime.
65
+ */
66
+ export type RevocableTable = Extract<ControlTable, 'OwnerKey' | 'CadrePeer' | 'ValidationKey' | 'Strand' | 'DeviceToken'>;
67
+
68
+ /**
69
+ * What a signature authorizes, scoped to one table rule — or, for
70
+ * `'Cadre.Enrollment'`, the offline peer vouch that `cadre enroll register`
71
+ * verifies (no table checks it, so it needs its own domain to stay disjoint
72
+ * from every table rule).
73
+ */
74
+ export type ControlDomain = `CadreControl.${ControlTable}` | 'Cadre.Enrollment';
75
+
76
+ /**
77
+ * The action half of the tag:
78
+ * - `'add'` / `'remove'` — insert / delete of the named row.
79
+ * - `'vouch'` — an owner (or validation key) vouches the row's semantics
80
+ * without adding or removing it: the `CadrePeer` membership vouch (shared,
81
+ * deliberately, by its insert and its owner-update branch), the
82
+ * `FormationUsage` disclosure validation, and the offline enrollment vouch.
83
+ * `DeviceToken` has no `'vouch'` digest — its owner re-touch branch was
84
+ * removed (it rewrote the row outside the monotonicity guard), so an owner
85
+ * correcting a token deletes and re-inserts it.
86
+ * - `'publish'` — a peer self-signs its OWN record (the `CadrePeer` /
87
+ * `DeviceToken` self-update branches), with its own key rather than an
88
+ * owner key.
89
+ * - `'consent'` — a peer self-signs its OWN `FormationUsage` redemption (the
90
+ * joiner proving it agreed to join), with its own key. Distinct from the
91
+ * approver's `'vouch'` over the same table so the two stored signatures are
92
+ * never interchangeable.
93
+ * - `'reissue'` — an owner re-writes an existing `Revocation` tombstone,
94
+ * bumping its `ReissuedAt` counter so a tombstone committed while the node
95
+ * was alone can be re-broadcast. Distinct from `'remove'` so a tombstone
96
+ * append approval can never be replayed as a re-issue and vice versa.
97
+ */
98
+ export type ControlAction = 'add' | 'remove' | 'vouch' | 'publish' | 'consent' | 'reissue';
99
+
100
+ /**
101
+ * The full ordered field vector a control-plane signature covers. Digest this
102
+ * with the crypto plugin's injective multi-field encoding (every field TEXT);
103
+ * the SQL mirror passes the same literals as leading `digest(...)` arguments:
104
+ *
105
+ * TS: digest(controlAuthorizationFields('CadreControl.X', 'add', [a, b]), 'sha256', ...)
106
+ * SQL: digest('CadreControl.X', 'add', new.A, new.B)
107
+ */
108
+ export function controlAuthorizationFields(
109
+ domain: ControlDomain,
110
+ action: ControlAction,
111
+ rowFields: string[],
112
+ ): string[] {
113
+ return [domain, action, ...rowFields];
114
+ }
@@ -5,14 +5,14 @@
5
5
  * connected (and thus replicating), without devolving into an N² mesh.
6
6
  *
7
7
  * FRET only needs a *connected* graph with the block clusters reachable, not a
8
- * full mesh. Storage/authority nodes are the stable, publicly-dialable backbone
8
+ * full mesh. Storage/owner nodes are the stable, publicly-dialable backbone
9
9
  * that hold the control blocks, so the policy is **backbone-preferential with a
10
10
  * bounded out-degree**:
11
11
  *
12
- * - **Always dial every backbone (authority) member** — the authority set is
12
+ * - **Always dial every backbone (owner) member** — the owner set is
13
13
  * small by design, and routing cohort formation through publicly-dialable
14
- * authority nodes keeps the cohort ≥2 where authority-signed writes originate.
15
- * - **Fill the remainder** up to `targetDegree` with non-authority members,
14
+ * owner nodes keeps the cohort ≥2 where owner-signed writes originate.
15
+ * - **Fill the remainder** up to `targetDegree` with non-owner members,
16
16
  * ordered deterministically (peerId sort) so every node makes the same stable
17
17
  * choice across passes and load spreads predictably.
18
18
  *
@@ -32,8 +32,8 @@ import type { CohortPeerRow } from './strand-cohort.js';
32
32
  export const DEFAULT_CONTROL_COHORT_RECONCILE_MS = 15_000;
33
33
 
34
34
  /**
35
- * Default cap on the number of **non-authority** members a single reconcile pass
36
- * proactively dials. Backbone (authority) members are always dialed and do NOT
35
+ * Default cap on the number of **non-owner** members a single reconcile pass
36
+ * proactively dials. Backbone (owner) members are always dialed and do NOT
37
37
  * count against this cap.
38
38
  */
39
39
  export const DEFAULT_CONTROL_COHORT_TARGET_DEGREE = 6;
@@ -41,14 +41,14 @@ export const DEFAULT_CONTROL_COHORT_TARGET_DEGREE = 6;
41
41
  /** Outcome of {@link selectControlCohortDials}: who to dial + what the cap dropped. */
42
42
  export interface ControlCohortSelection {
43
43
  /**
44
- * Siblings to dial this pass, backbone first then the bounded non-authority
44
+ * Siblings to dial this pass, backbone first then the bounded non-owner
45
45
  * fill. Already self-excluded (the caller must pass self-excluded `siblings`).
46
46
  * Still includes peers that may turn out to be already-connected — the caller
47
47
  * diffs against live connections separately.
48
48
  */
49
49
  dials: CohortPeerRow[];
50
- /** Non-authority members dropped by the `targetDegree` cap (0 when none). */
51
- cappedNonAuthority: number;
50
+ /** Non-owner members dropped by the `targetDegree` cap (0 when none). */
51
+ cappedNonOwner: number;
52
52
  }
53
53
 
54
54
  /** Stable peerId ordering so the bounded fill is identical across passes/nodes. */
@@ -63,19 +63,19 @@ function byPeerId(a: CohortPeerRow, b: CohortPeerRow): number {
63
63
  * to dial.
64
64
  *
65
65
  * A member is **backbone** iff the ed25519 key derived from its peerId
66
- * (see {@link ed25519PublicKeyB64FromPeerId}) is in `authorityKeys`. All backbone
67
- * members are selected; non-authority members fill up to `targetDegree` in
66
+ * (see {@link ed25519PublicKeyB64FromPeerId}) is in `ownerKeys`. All backbone
67
+ * members are selected; non-owner members fill up to `targetDegree` in
68
68
  * deterministic peerId order.
69
69
  *
70
70
  * @param siblings - cadre members EXCLUDING self (the caller filters self out).
71
- * @param authorityKeys - the converged `AuthorityKey` set; may be empty/partial
72
- * before authority convergence, in which case no member classifies as backbone
71
+ * @param ownerKeys - the converged `OwnerKey` set; may be empty/partial
72
+ * before owner convergence, in which case no member classifies as backbone
73
73
  * yet and the bounded fill still makes progress (preference sharpens later).
74
- * @param targetDegree - cap on non-authority dials (negative is treated as 0).
74
+ * @param targetDegree - cap on non-owner dials (negative is treated as 0).
75
75
  */
76
76
  export function selectControlCohortDials(
77
77
  siblings: CohortPeerRow[],
78
- authorityKeys: ReadonlySet<string>,
78
+ ownerKeys: ReadonlySet<string>,
79
79
  targetDegree: number,
80
80
  ): ControlCohortSelection {
81
81
  const backbone: CohortPeerRow[] = [];
@@ -83,7 +83,7 @@ export function selectControlCohortDials(
83
83
 
84
84
  for (const peer of siblings) {
85
85
  const pubKey = ed25519PublicKeyB64FromPeerId(peer.peerId);
86
- if (pubKey !== null && authorityKeys.has(pubKey)) {
86
+ if (pubKey !== null && ownerKeys.has(pubKey)) {
87
87
  backbone.push(peer);
88
88
  } else {
89
89
  rest.push(peer);
@@ -98,6 +98,6 @@ export function selectControlCohortDials(
98
98
 
99
99
  return {
100
100
  dials: [...backbone, ...fill],
101
- cappedNonAuthority: rest.length - fill.length,
101
+ cappedNonOwner: rest.length - fill.length,
102
102
  };
103
103
  }