@serfab/cadre-core 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 (230) hide show
  1. package/README.md +293 -274
  2. package/dist/cadre-invite-protocol.d.ts +383 -0
  3. package/dist/cadre-invite-protocol.js +774 -0
  4. package/dist/cadre-invite-protocol.js.map +1 -0
  5. package/dist/cadre-join-request.d.ts +42 -0
  6. package/dist/cadre-join-request.js +144 -0
  7. package/dist/cadre-join-request.js.map +1 -0
  8. package/dist/cadre-node.d.ts +849 -210
  9. package/dist/cadre-node.js +1574 -374
  10. package/dist/cadre-node.js.map +1 -1
  11. package/dist/claim-proof.d.ts +45 -0
  12. package/dist/claim-proof.js +132 -0
  13. package/dist/claim-proof.js.map +1 -0
  14. package/dist/control-authorization.d.ts +80 -19
  15. package/dist/control-authorization.js +55 -13
  16. package/dist/control-authorization.js.map +1 -1
  17. package/dist/control-cohort.js +1 -1
  18. package/dist/control-cohort.js.map +1 -1
  19. package/dist/control-database.d.ts +605 -108
  20. package/dist/control-database.js +1299 -280
  21. package/dist/control-database.js.map +1 -1
  22. package/dist/control-protocol-guard.d.ts +142 -0
  23. package/dist/control-protocol-guard.js +293 -0
  24. package/dist/control-protocol-guard.js.map +1 -0
  25. package/dist/control-schema.d.ts +1 -1
  26. package/dist/control-schema.js +817 -1067
  27. package/dist/control-schema.js.map +1 -1
  28. package/dist/control-stream.d.ts +4 -0
  29. package/dist/control-stream.js +22 -7
  30. package/dist/control-stream.js.map +1 -1
  31. package/dist/delegate-admission.d.ts +1 -1
  32. package/dist/delegate-admission.js +1 -1
  33. package/dist/device-token.d.ts +11 -9
  34. package/dist/device-token.js +14 -12
  35. package/dist/device-token.js.map +1 -1
  36. package/dist/ed25519-key.d.ts +22 -9
  37. package/dist/ed25519-key.js +47 -10
  38. package/dist/ed25519-key.js.map +1 -1
  39. package/dist/fetch-impl.d.ts +12 -0
  40. package/dist/fetch-impl.js +23 -0
  41. package/dist/fetch-impl.js.map +1 -0
  42. package/dist/file-durable-slot.js +2 -1
  43. package/dist/file-durable-slot.js.map +1 -1
  44. package/dist/file-safe-name.d.ts +25 -0
  45. package/dist/file-safe-name.js +29 -0
  46. package/dist/file-safe-name.js.map +1 -0
  47. package/dist/formation-approval.js +5 -11
  48. package/dist/formation-approval.js.map +1 -1
  49. package/dist/fs-atomic.d.ts +0 -19
  50. package/dist/fs-atomic.js +3 -24
  51. package/dist/fs-atomic.js.map +1 -1
  52. package/dist/index.d.ts +22 -10
  53. package/dist/index.js +54 -11
  54. package/dist/index.js.map +1 -1
  55. package/dist/invitation-bootstrap.d.ts +50 -12
  56. package/dist/invitation-bootstrap.js +76 -13
  57. package/dist/invitation-bootstrap.js.map +1 -1
  58. package/dist/joined-strand-store.d.ts +7 -0
  59. package/dist/joined-strand-store.js +9 -0
  60. package/dist/joined-strand-store.js.map +1 -1
  61. package/dist/key-store-file.js +2 -1
  62. package/dist/key-store-file.js.map +1 -1
  63. package/dist/link-budget.d.ts +25 -33
  64. package/dist/link-budget.js +25 -35
  65. package/dist/link-budget.js.map +1 -1
  66. package/dist/machine-removal.d.ts +78 -0
  67. package/dist/machine-removal.js +166 -0
  68. package/dist/machine-removal.js.map +1 -0
  69. package/dist/membership-connection-gater.d.ts +191 -151
  70. package/dist/membership-connection-gater.js +236 -173
  71. package/dist/membership-connection-gater.js.map +1 -1
  72. package/dist/node-claim-payload.d.ts +74 -0
  73. package/dist/node-claim-payload.js +224 -0
  74. package/dist/node-claim-payload.js.map +1 -0
  75. package/dist/node-default-transports.d.ts +27 -0
  76. package/dist/node-default-transports.js +35 -0
  77. package/dist/node-default-transports.js.map +1 -0
  78. package/dist/node-transports.d.ts +30 -0
  79. package/dist/node-transports.js +40 -0
  80. package/dist/node-transports.js.map +1 -0
  81. package/dist/owner-anchor-sync.d.ts +22 -0
  82. package/dist/owner-anchor-sync.js +143 -0
  83. package/dist/owner-anchor-sync.js.map +1 -0
  84. package/dist/peer-authorization.d.ts +113 -18
  85. package/dist/peer-authorization.js +225 -29
  86. package/dist/peer-authorization.js.map +1 -1
  87. package/dist/peer-dial.d.ts +143 -27
  88. package/dist/peer-dial.js +276 -31
  89. package/dist/peer-dial.js.map +1 -1
  90. package/dist/peer-join-backfill.d.ts +26 -2
  91. package/dist/peer-join-backfill.js +60 -12
  92. package/dist/peer-join-backfill.js.map +1 -1
  93. package/dist/peer-record.d.ts +17 -52
  94. package/dist/peer-record.js +20 -58
  95. package/dist/peer-record.js.map +1 -1
  96. package/dist/pending-join-runner.d.ts +66 -58
  97. package/dist/pending-join-runner.js +136 -145
  98. package/dist/pending-join-runner.js.map +1 -1
  99. package/dist/primary-lan-address.d.ts +8 -0
  100. package/dist/primary-lan-address.js +46 -0
  101. package/dist/primary-lan-address.js.map +1 -0
  102. package/dist/provider/client.d.ts +131 -0
  103. package/dist/provider/client.js +226 -0
  104. package/dist/provider/client.js.map +1 -0
  105. package/dist/provider/descriptor.d.ts +41 -0
  106. package/dist/provider/descriptor.js +103 -0
  107. package/dist/provider/descriptor.js.map +1 -0
  108. package/dist/provider/index.d.ts +9 -0
  109. package/dist/provider/index.js +10 -0
  110. package/dist/provider/index.js.map +1 -0
  111. package/dist/provider/oauth.d.ts +125 -0
  112. package/dist/provider/oauth.js +199 -0
  113. package/dist/provider/oauth.js.map +1 -0
  114. package/dist/provider/rent-node.d.ts +101 -0
  115. package/dist/provider/rent-node.js +220 -0
  116. package/dist/provider/rent-node.js.map +1 -0
  117. package/dist/push-fanout.d.ts +8 -2
  118. package/dist/push-fanout.js +8 -2
  119. package/dist/push-fanout.js.map +1 -1
  120. package/dist/relay-addrs.d.ts +9 -4
  121. package/dist/relay-addrs.js +9 -4
  122. package/dist/relay-addrs.js.map +1 -1
  123. package/dist/relay-reservation.d.ts +51 -16
  124. package/dist/relay-reservation.js +107 -24
  125. package/dist/relay-reservation.js.map +1 -1
  126. package/dist/seed-bootstrap.d.ts +165 -123
  127. package/dist/seed-bootstrap.js +313 -275
  128. package/dist/seed-bootstrap.js.map +1 -1
  129. package/dist/seed-trust-policy.d.ts +98 -37
  130. package/dist/seed-trust-policy.js +188 -2
  131. package/dist/seed-trust-policy.js.map +1 -1
  132. package/dist/strand-addr-protocol.d.ts +24 -26
  133. package/dist/strand-addr-protocol.js +19 -26
  134. package/dist/strand-addr-protocol.js.map +1 -1
  135. package/dist/strand-commit-durability.d.ts +54 -0
  136. package/dist/strand-commit-durability.js +74 -0
  137. package/dist/strand-commit-durability.js.map +1 -0
  138. package/dist/strand-database.d.ts +23 -8
  139. package/dist/strand-database.js +41 -13
  140. package/dist/strand-database.js.map +1 -1
  141. package/dist/strand-first-sync-gate.d.ts +7 -1
  142. package/dist/strand-first-sync-gate.js +7 -1
  143. package/dist/strand-first-sync-gate.js.map +1 -1
  144. package/dist/strand-formation-protocol.js +5 -2
  145. package/dist/strand-formation-protocol.js.map +1 -1
  146. package/dist/strand-fret-addrs.d.ts +19 -1
  147. package/dist/strand-fret-addrs.js +27 -9
  148. package/dist/strand-fret-addrs.js.map +1 -1
  149. package/dist/strand-instance-manager.d.ts +81 -19
  150. package/dist/strand-instance-manager.js +216 -105
  151. package/dist/strand-instance-manager.js.map +1 -1
  152. package/dist/strand-move.d.ts +124 -0
  153. package/dist/strand-move.js +438 -0
  154. package/dist/strand-move.js.map +1 -0
  155. package/dist/strand-network-config.js +7 -6
  156. package/dist/strand-network-config.js.map +1 -1
  157. package/dist/strand-peer-redial.d.ts +113 -0
  158. package/dist/strand-peer-redial.js +236 -0
  159. package/dist/strand-peer-redial.js.map +1 -0
  160. package/dist/strand-revocation-enforcer.d.ts +11 -11
  161. package/dist/strand-revocation-enforcer.js +11 -11
  162. package/dist/strand-wake-protocol.d.ts +13 -16
  163. package/dist/strand-wake-protocol.js +23 -27
  164. package/dist/strand-wake-protocol.js.map +1 -1
  165. package/dist/trusted-owner-store.d.ts +35 -5
  166. package/dist/trusted-owner-store.js +50 -8
  167. package/dist/trusted-owner-store.js.map +1 -1
  168. package/dist/types.d.ts +512 -89
  169. package/dist/types.js +10 -0
  170. package/dist/types.js.map +1 -1
  171. package/package.json +19 -10
  172. package/src/cadre-invite-protocol.ts +1052 -0
  173. package/src/cadre-join-request.ts +167 -0
  174. package/src/cadre-node.ts +1703 -386
  175. package/src/claim-proof.ts +147 -0
  176. package/src/control-authorization.ts +203 -117
  177. package/src/control-cohort.ts +1 -1
  178. package/src/control-database.ts +4663 -3453
  179. package/src/control-protocol-guard.ts +348 -0
  180. package/src/control-schema.ts +828 -1078
  181. package/src/control-stream.ts +26 -7
  182. package/src/delegate-admission.ts +1 -1
  183. package/src/device-token.ts +14 -10
  184. package/src/ed25519-key.ts +47 -10
  185. package/src/fetch-impl.ts +23 -0
  186. package/src/file-durable-slot.ts +64 -63
  187. package/src/file-safe-name.ts +32 -0
  188. package/src/formation-approval.ts +604 -610
  189. package/src/fs-atomic.ts +92 -117
  190. package/src/index.ts +789 -648
  191. package/src/invitation-bootstrap.ts +91 -13
  192. package/src/joined-strand-store.ts +508 -498
  193. package/src/key-store-file.ts +142 -141
  194. package/src/link-budget.ts +25 -36
  195. package/src/machine-removal.ts +216 -0
  196. package/src/membership-connection-gater.ts +259 -191
  197. package/src/node-claim-payload.ts +259 -0
  198. package/src/node-default-transports.ts +38 -0
  199. package/src/node-transports.ts +49 -0
  200. package/src/owner-anchor-sync.ts +171 -0
  201. package/src/peer-authorization.ts +258 -29
  202. package/src/peer-dial.ts +604 -315
  203. package/src/peer-join-backfill.ts +62 -11
  204. package/src/peer-record.ts +159 -197
  205. package/src/pending-join-runner.ts +729 -735
  206. package/src/primary-lan-address.ts +47 -0
  207. package/src/provider/client.ts +326 -0
  208. package/src/provider/descriptor.ts +129 -0
  209. package/src/provider/index.ts +59 -0
  210. package/src/provider/oauth.ts +285 -0
  211. package/src/provider/rent-node.ts +275 -0
  212. package/src/push-fanout.ts +8 -2
  213. package/src/relay-addrs.ts +9 -4
  214. package/src/relay-reservation.ts +1211 -1121
  215. package/src/seed-bootstrap.ts +1502 -1448
  216. package/src/seed-trust-policy.ts +353 -140
  217. package/src/strand-addr-protocol.ts +26 -35
  218. package/src/strand-commit-durability.ts +116 -0
  219. package/src/strand-database.ts +46 -14
  220. package/src/strand-first-sync-gate.ts +7 -1
  221. package/src/strand-formation-protocol.ts +1039 -1036
  222. package/src/strand-fret-addrs.ts +179 -145
  223. package/src/strand-instance-manager.ts +2026 -1883
  224. package/src/strand-move.ts +585 -0
  225. package/src/strand-network-config.ts +7 -6
  226. package/src/strand-peer-redial.ts +307 -0
  227. package/src/strand-revocation-enforcer.ts +11 -11
  228. package/src/strand-wake-protocol.ts +23 -30
  229. package/src/trusted-owner-store.ts +242 -171
  230. package/src/types.ts +2687 -2242
@@ -1,18 +1,27 @@
1
1
  import debug from 'debug';
2
2
  import { digest, verify } from '@optimystic/quereus-plugin-crypto';
3
- import { controlAuthorizationFields } from './control-authorization.js';
4
- import type { ControlAction, ControlDomain, RevocableTable } from './control-authorization.js';
3
+ import { controlAuthorizationFields, unboundAuthorizationFields, cadreInviteRowFields } from './control-authorization.js';
4
+ import type { BoundControlDomain, CadreInviteSignedFields, ControlAction, RevocableTable, UnboundControlDomain } from './control-authorization.js';
5
+ import { ed25519PublicKeyB64FromPeerId } from './ed25519-key.js';
6
+ import type { CadreInviteRow, CadreInviteUsageRow, CadrePeerVoucherFields, OwnerKeyRow, RevocationRow } from './types.js';
5
7
 
6
8
  const log = debug('sereus:cadre:peer-authorization');
7
9
 
8
10
  /**
9
- * base64url SHA-256 digest over the shared domain-tagged field vector (see
11
+ * base64url SHA-256 digest over the shared domain-tagged, party-bound field vector (see
10
12
  * control-authorization.ts). The base64url twin of
11
13
  * control-database.ts:buildAuthorizationMessage (which returns the same digest as raw
12
14
  * bytes): sign either encoding with input encoding to match and the signed bytes agree.
15
+ * `partyId` is the party the verifier is configured for: a stored proof minted for
16
+ * another party fails here exactly as it fails the schema's `party_id()`-bound check.
13
17
  */
14
- function taggedDigest(domain: ControlDomain, action: ControlAction, rowFields: string[]): string {
15
- return digest(controlAuthorizationFields(domain, action, rowFields), 'sha256', 'base64url') as string;
18
+ function taggedDigest(domain: BoundControlDomain, action: ControlAction, partyId: string, rowFields: string[]): string {
19
+ return digest(controlAuthorizationFields(domain, action, partyId, rowFields), 'sha256', 'base64url') as string;
20
+ }
21
+
22
+ /** {@link taggedDigest} for the two party-free digests (`UnboundControlDomain`). */
23
+ function unboundTaggedDigest(domain: UnboundControlDomain, action: ControlAction, rowFields: string[]): string {
24
+ return digest(unboundAuthorizationFields(domain, action, rowFields), 'sha256', 'base64url') as string;
16
25
  }
17
26
 
18
27
  /**
@@ -27,7 +36,7 @@ function taggedDigest(domain: ControlDomain, action: ControlAction, rowFields: s
27
36
  * here and both move together.
28
37
  */
29
38
  export function peerAuthorizationDigest(peerId: string): string {
30
- return taggedDigest('Cadre.Enrollment', 'vouch', [peerId]);
39
+ return unboundTaggedDigest('Cadre.Enrollment', 'vouch', [peerId]);
31
40
  }
32
41
 
33
42
  /**
@@ -54,7 +63,7 @@ export interface DeviceTokenAuthorizedRow {
54
63
  /**
55
64
  * Canonical digest an owner signs to authorize a `DeviceToken` INSERT — the WHOLE row,
56
65
  * ending in its single-use `StampId` nonce. SQL mirror:
57
- * `digest('CadreControl.DeviceToken', 'add', new.PeerId, new.Platform, new.Token,
66
+ * `digest('CadreControl.DeviceToken', 'add', party_id(), new.PeerId, new.Platform, new.Token,
58
67
  * coalesce(cast(new.UpdatedAt as text), ''), coalesce(new.Sig, ''), new.StampId)` in
59
68
  * `DeviceToken.AuthorizedInsert`.
60
69
  *
@@ -70,8 +79,8 @@ export interface DeviceTokenAuthorizedRow {
70
79
  * Distinct from {@link deviceTokenRemoveDigest} so a captured insert approval can never
71
80
  * be replayed to delete the token, and vice versa.
72
81
  */
73
- export function deviceTokenAddDigest(row: DeviceTokenAuthorizedRow): string {
74
- return taggedDigest('CadreControl.DeviceToken', 'add', [
82
+ export function deviceTokenAddDigest(partyId: string, row: DeviceTokenAuthorizedRow): string {
83
+ return taggedDigest('CadreControl.DeviceToken', 'add', partyId, [
75
84
  row.peerId,
76
85
  row.platform,
77
86
  row.token,
@@ -84,7 +93,7 @@ export function deviceTokenAddDigest(row: DeviceTokenAuthorizedRow): string {
84
93
  /**
85
94
  * Canonical digest an owner signs to authorize a `DeviceToken` DELETE, bound to the
86
95
  * STORED row's (PeerId, StampId). SQL mirror:
87
- * `digest('CadreControl.DeviceToken', 'remove', old.PeerId, old.StampId)` in
96
+ * `digest('CadreControl.DeviceToken', 'remove', party_id(), old.PeerId, old.StampId)` in
88
97
  * `DeviceToken.AuthorizedDelete`.
89
98
  *
90
99
  * A narrower vector than {@link deviceTokenAddDigest} on purpose (the same split
@@ -92,8 +101,8 @@ export function deviceTokenAddDigest(row: DeviceTokenAuthorizedRow): string {
92
101
  * cannot be re-cut into an insert approval, and it is dead the moment the stamp it
93
102
  * names is tombstoned.
94
103
  */
95
- export function deviceTokenRemoveDigest(peerId: string, stampId: string): string {
96
- return taggedDigest('CadreControl.DeviceToken', 'remove', [peerId, stampId]);
104
+ export function deviceTokenRemoveDigest(partyId: string, peerId: string, stampId: string): string {
105
+ return taggedDigest('CadreControl.DeviceToken', 'remove', partyId, [peerId, stampId]);
97
106
  }
98
107
 
99
108
  /**
@@ -107,10 +116,10 @@ export function deviceTokenRemoveDigest(peerId: string, stampId: string): string
107
116
  * a delete. The domain tag keeps the stored, replicated `VouchSig` useless against
108
117
  * every OTHER table's rules.
109
118
  *
110
- * SQL mirror: `digest('CadreControl.CadrePeer', 'vouch', new.PeerId, new.StampId)`.
119
+ * SQL mirror: `digest('CadreControl.CadrePeer', 'vouch', party_id(), new.PeerId, new.StampId)`.
111
120
  */
112
- export function cadrePeerVoucherDigest(peerId: string, stampId: string): string {
113
- return taggedDigest('CadreControl.CadrePeer', 'vouch', [peerId, stampId]);
121
+ export function cadrePeerVoucherDigest(partyId: string, peerId: string, stampId: string): string {
122
+ return taggedDigest('CadreControl.CadrePeer', 'vouch', partyId, [peerId, stampId]);
114
123
  }
115
124
 
116
125
  /**
@@ -122,10 +131,10 @@ export function cadrePeerVoucherDigest(peerId: string, stampId: string): string
122
131
  * delete lands, because a re-added row carries a FRESH `StampId` (the removed row's
123
132
  * stamp is retired into `CadreControl.Revocation` and never reused).
124
133
  *
125
- * SQL mirror: `digest('CadreControl.CadrePeer', 'remove', old.PeerId, old.StampId)`.
134
+ * SQL mirror: `digest('CadreControl.CadrePeer', 'remove', party_id(), old.PeerId, old.StampId)`.
126
135
  */
127
- export function cadrePeerRemoveDigest(peerId: string, stampId: string): string {
128
- return taggedDigest('CadreControl.CadrePeer', 'remove', [peerId, stampId]);
136
+ export function cadrePeerRemoveDigest(partyId: string, peerId: string, stampId: string): string {
137
+ return taggedDigest('CadreControl.CadrePeer', 'remove', partyId, [peerId, stampId]);
129
138
  }
130
139
 
131
140
  /**
@@ -134,7 +143,7 @@ export function cadrePeerRemoveDigest(peerId: string, stampId: string): string {
134
143
  * (the removed row's primary key: OwnerKey.Key / ValidationKey.Key /
135
144
  * CadrePeer.PeerId / DeviceToken.PeerId / Strand.Id) as which row was retired.
136
145
  * SQL mirror:
137
- * `digest('CadreControl.Revocation', 'remove', new.TableName, new.RowKey, new.StampId)`
146
+ * `digest('CadreControl.Revocation', 'remove', party_id(), new.TableName, new.RowKey, new.StampId)`
138
147
  * in `Revocation.Authorized`.
139
148
  *
140
149
  * Its own `'CadreControl.Revocation'` domain tag makes it disjoint from the
@@ -143,8 +152,48 @@ export function cadrePeerRemoveDigest(peerId: string, stampId: string): string {
143
152
  * this tombstone accompanies — a removal signature is not a retirement
144
153
  * signature and cannot be replayed as one.
145
154
  */
146
- export function revocationDigest(tableName: RevocableTable, rowKey: string, stampId: string): string {
147
- return taggedDigest('CadreControl.Revocation', 'remove', [tableName, rowKey, stampId]);
155
+ export function revocationDigest(partyId: string, tableName: RevocableTable, rowKey: string, stampId: string): string {
156
+ return taggedDigest('CadreControl.Revocation', 'remove', partyId, [tableName, rowKey, stampId]);
157
+ }
158
+
159
+ /**
160
+ * Does a `Revocation` row's stored signer pair (`SignerKey` / `SignerSig`) verify over the
161
+ * tombstone's own triple, for `partyId`? The read-side mirror of `Revocation.Authorized`,
162
+ * for a node that received the row by replication and must decide whether to honour it.
163
+ * Whether `row.signerKey` is an owner this node trusts is the caller's question, as for
164
+ * {@link verifyCadrePeerVoucher}. Never throws: a malformed row verifies as `false`.
165
+ */
166
+ export function verifyRevocationSigner(partyId: string, row: RevocationRow): boolean {
167
+ try {
168
+ return verifyB64(revocationDigest(partyId, row.tableName, row.rowKey, row.stampId), row.signerSig, row.signerKey);
169
+ } catch (error) {
170
+ log('verifyRevocationSigner failed: %o', error);
171
+ return false;
172
+ }
173
+ }
174
+
175
+ /**
176
+ * Canonical digest an owner signs to seat a further `OwnerKey` row — the signed-add branch
177
+ * of `OwnerKey.Authorized`, which stores the pair as `VouchOwner` / `VouchSig`. SQL mirror:
178
+ * `digest('CadreControl.OwnerKey', 'add', party_id(), new.Key, new.StampId)`. The removal
179
+ * digest needs no builder here: it is the generic guarded `'remove'` digest over (Key, StampId).
180
+ */
181
+ export function ownerKeyAddDigest(partyId: string, key: string, stampId: string): string {
182
+ return taggedDigest('CadreControl.OwnerKey', 'add', partyId, [key, stampId]);
183
+ }
184
+
185
+ /**
186
+ * Does an owner-signed `OwnerKey` row's stored voucher verify — `vouchSig` by `vouchOwner`
187
+ * over {@link ownerKeyAddDigest} for (`partyId`, `key`, `stampId`)? Whether `vouchOwner` is
188
+ * anchored is the caller's question, as for {@link verifyCadrePeerVoucher}. Never throws.
189
+ */
190
+ export function verifyOwnerKeyVoucher(partyId: string, key: string, stampId: string, vouchOwner: string, vouchSig: string): boolean {
191
+ try {
192
+ return verifyB64(ownerKeyAddDigest(partyId, key, stampId), vouchSig, vouchOwner);
193
+ } catch (error) {
194
+ log('verifyOwnerKeyVoucher failed: %o', error);
195
+ return false;
196
+ }
148
197
  }
149
198
 
150
199
  /**
@@ -184,12 +233,13 @@ export function verifyPeerAuthorization(
184
233
 
185
234
  /**
186
235
  * Verify that `signature` is a valid owner ed25519 signature over the
187
- * `CadrePeer` voucher digest for (`peerId`, `stampId`) — the read-side mirror
188
- * of the voucher {@link ControlDatabase.insertCadrePeer} signs and persists into
236
+ * `CadrePeer` voucher digest for (`partyId`, `peerId`, `stampId`) — the read-side
237
+ * mirror of the voucher {@link ControlDatabase.insertCadrePeer} signs and persists into
189
238
  * `VouchOwner`/`VouchSig` (see {@link cadrePeerVoucherDigest}).
190
239
  *
191
240
  * A `true` result means the holder of `ownerPublicKey` vouched THIS membership
192
- * row (the peer id bound to the row's single-use `StampId` nonce). It says
241
+ * row (the peer id bound to the row's single-use `StampId` nonce) in THIS party: a
242
+ * voucher copied from another party's table that shares the owner key fails. It says
193
243
  * nothing about whether that owner key is itself trustworthy — the caller must
194
244
  * separately check the key against the node-local trusted-owner anchor
195
245
  * (`TrustedOwnerStore`), never the replicated `OwnerKey` table.
@@ -199,6 +249,7 @@ export function verifyPeerAuthorization(
199
249
  * resolves to `false`, logged at debug.
200
250
  */
201
251
  export function verifyCadrePeerVoucher(
252
+ partyId: string,
202
253
  peerId: string,
203
254
  stampId: string,
204
255
  ownerPublicKey: string,
@@ -206,7 +257,7 @@ export function verifyCadrePeerVoucher(
206
257
  ): boolean {
207
258
  try {
208
259
  return verify(
209
- cadrePeerVoucherDigest(peerId, stampId),
260
+ cadrePeerVoucherDigest(partyId, peerId, stampId),
210
261
  signature,
211
262
  ownerPublicKey,
212
263
  'ed25519',
@@ -224,14 +275,14 @@ export function verifyCadrePeerVoucher(
224
275
  * Canonical digest a JOINING peer signs to consent to ONE `FormationUsage`
225
276
  * redemption — the read-side mirror of `formationConsentMessage` in
226
277
  * control-database.ts, base64url-encoded instead of raw bytes (see
227
- * {@link taggedDigest}). That doc comment carries the field-vector rationale
228
- * (notably why `strandId` is not signed); the two vectors must not drift, which
229
- * peer-authorization.spec.ts pins by signing one form and verifying the other.
278
+ * {@link unboundTaggedDigest}). That doc comment carries the field-vector rationale
279
+ * (notably why neither `strandId` nor the party id is signed); the two vectors must not
280
+ * drift, which peer-authorization.spec.ts pins by signing one form and verifying the other.
230
281
  */
231
282
  export function formationConsentDigest(
232
283
  token: string, usageStampId: string, peerKey: string, disclosure: string
233
284
  ): string {
234
- return taggedDigest('CadreControl.FormationUsage', 'consent', [token, usageStampId, peerKey, disclosure]);
285
+ return unboundTaggedDigest('CadreControl.FormationUsage', 'consent', [token, usageStampId, peerKey, disclosure]);
235
286
  }
236
287
 
237
288
  /**
@@ -257,3 +308,181 @@ export function verifyFormationConsent(row: {
257
308
  return false;
258
309
  }
259
310
  }
311
+
312
+ /**
313
+ * Canonical digest an owner signs to seat a `CadreInvite` row — the read-side mirror
314
+ * of `cadreInviteAddMessage` in control-database.ts, base64url instead of raw bytes
315
+ * (see {@link taggedDigest}). The whole row is bound, nullable columns as `''` and
316
+ * `ExpiresAt` in its stored canonical form, through the one field builder both sides
317
+ * share (`cadreInviteRowFields`). SQL mirror: `CadreInvite.AuthorizedInsert`.
318
+ */
319
+ export function cadreInviteAddDigest(partyId: string, row: CadreInviteSignedFields): string {
320
+ return taggedDigest('CadreControl.CadreInvite', 'add', partyId, cadreInviteRowFields(row));
321
+ }
322
+
323
+ /**
324
+ * Canonical digest the HOLDER of a `CadreInvite`'s private key signs to redeem it once:
325
+ * proof of possession, verified against the invitation key itself
326
+ * (`CadreInviteUsage.InvitePossessed`). Binds the redemption's single-use nonce and the
327
+ * device being admitted, so one redemption cannot be re-presented for another device
328
+ * or another use. Mirror of `cadreInviteRedeemMessage` in control-database.ts.
329
+ */
330
+ export function cadreInviteRedeemDigest(partyId: string, inviteKey: string, usageStampId: string, peerKey: string): string {
331
+ return taggedDigest('CadreControl.CadreInviteUsage', 'redeem', partyId, [inviteKey, usageStampId, peerKey]);
332
+ }
333
+
334
+ /**
335
+ * Canonical digest the DEVICE signs to consent to being admitted by one redemption
336
+ * (`CadreInviteUsage.PeerConsented`), over the same fields as
337
+ * {@link cadreInviteRedeemDigest} under a distinct action tag, so the holder's and
338
+ * the device's signatures are never interchangeable. Mirror of
339
+ * `cadreInviteConsentMessage` in control-database.ts.
340
+ */
341
+ export function cadreInviteConsentDigest(partyId: string, inviteKey: string, usageStampId: string, peerKey: string): string {
342
+ return taggedDigest('CadreControl.CadreInviteUsage', 'consent', partyId, [inviteKey, usageStampId, peerKey]);
343
+ }
344
+
345
+ /** ed25519 verify over a base64url digest, with the siblings' never-throws contract left to the caller. */
346
+ function verifyB64(digestB64: string, signature: string, publicKey: string): boolean {
347
+ return verify(digestB64, signature, publicKey, 'ed25519', 'base64url', 'base64url', 'base64url');
348
+ }
349
+
350
+ /**
351
+ * Does a `CadreInvite` row carry a valid `'add'` signature by its own `issuerKey`, for
352
+ * `partyId`? The redeeming device runs this over the row a member replies with, after
353
+ * checking the issuer against its pinned owner keys and the reply's party against the
354
+ * invitation's, and a member over a bundle's copy it does not hold yet
355
+ * (`ControlDatabase.isCadreInviteLive`); whether that issuer IS an owner is the caller's question.
356
+ * Never throws: a malformed row verifies as `false`.
357
+ */
358
+ export function verifyCadreInviteRow(partyId: string, invite: CadreInviteRow): boolean {
359
+ try {
360
+ return verifyB64(cadreInviteAddDigest(partyId, invite), invite.issuerSig, invite.issuerKey);
361
+ } catch (error) {
362
+ log('verifyCadreInviteRow failed: %o', error);
363
+ return false;
364
+ }
365
+ }
366
+
367
+ /**
368
+ * The two signatures a redemption carries, checked before any database work: the holder's
369
+ * `'redeem'` signature with the invitation key, and the device's `'consent'` signature with
370
+ * its own key, both over (`inviteKey`, `usageStampId`, `peerKey`). The pre-check twin of
371
+ * `CadreInviteUsage.InvitePossessed` / `PeerConsented`, as `verifyFormationConsent` is for
372
+ * formation: the schema stays the authority, this turns a bad signature into a clean
373
+ * refusal instead of a constraint failure at commit. Never throws.
374
+ */
375
+ export function verifyCadreInviteRedemption(
376
+ partyId: string,
377
+ fields: { inviteKey: string; usageStampId: string; peerKey: string },
378
+ inviteSig: string,
379
+ peerSig: string,
380
+ ): boolean {
381
+ try {
382
+ return verifyB64(cadreInviteRedeemDigest(partyId, fields.inviteKey, fields.usageStampId, fields.peerKey), inviteSig, fields.inviteKey)
383
+ && verifyB64(cadreInviteConsentDigest(partyId, fields.inviteKey, fields.usageStampId, fields.peerKey), peerSig, fields.peerKey);
384
+ } catch (error) {
385
+ log('verifyCadreInviteRedemption failed: %o', error);
386
+ return false;
387
+ }
388
+ }
389
+
390
+ /**
391
+ * The links of an invitation admission that do not depend on WHICH table's row was
392
+ * admitted — shared by {@link verifyInvitationAdmission} (`CadrePeer`) and
393
+ * {@link verifyInvitationOwnerAdmission} (`OwnerKey`), which differ only in the row
394
+ * columns the usage must name and, for an owner row, in `grantsOwner`. The chain is
395
+ *
396
+ * row --vouchUsage--> usage --inviteKey--> invitation --issuerKey--> anchored owner
397
+ *
398
+ * and every link here is verified: the issuer is anchored (`isAnchored`) and is the row's
399
+ * `vouchOwner`; the usage names this invitation and is the row's `vouchUsage`; a targeted
400
+ * invitation names the redeeming device; the stored `peerKey` really is the key behind the
401
+ * usage's `peerId` (the schema cannot unwrap a multihash, so a writer could assert any
402
+ * pair); the invitation's stored `'add'` signature verifies over the row rebuilt from its
403
+ * columns; the holder's `'redeem'` signature verifies with the invitation key and the
404
+ * device's `'consent'` signature with its own key. All three signatures are judged for
405
+ * `partyId`. Throws on malformed input; the two callers turn that into `false`.
406
+ */
407
+ function verifyAdmissionChain(
408
+ partyId: string,
409
+ vouchOwner: string,
410
+ vouchUsage: string,
411
+ usage: CadreInviteUsageRow,
412
+ invite: CadreInviteRow,
413
+ isAnchored: (ownerKey: string) => boolean,
414
+ ): boolean {
415
+ return isAnchored(vouchOwner)
416
+ && invite.issuerKey === vouchOwner
417
+ && usage.usageStampId === vouchUsage
418
+ && usage.inviteKey === invite.key
419
+ && (invite.peerId === null || invite.peerId === usage.peerId)
420
+ && ed25519PublicKeyB64FromPeerId(usage.peerId) === usage.peerKey
421
+ && verifyB64(cadreInviteAddDigest(partyId, invite), invite.issuerSig, invite.issuerKey)
422
+ && verifyB64(cadreInviteRedeemDigest(partyId, invite.key, usage.usageStampId, usage.peerKey), usage.inviteSig, invite.key)
423
+ && verifyB64(cadreInviteConsentDigest(partyId, invite.key, usage.usageStampId, usage.peerKey), usage.peerSig, usage.peerKey);
424
+ }
425
+
426
+ /**
427
+ * Is an invitation-admitted `CadrePeer` row (`vouchSig` null, `vouchUsage` set) a
428
+ * member this node should trust? The read-side mirror of the consent branch of
429
+ * `CadrePeer.AuthorizedInsert`, re-checked against THIS node's anchor because the
430
+ * replicated `OwnerKey` table can be polluted. The usage must name this peer id and this
431
+ * exact row incarnation (`peerStampId`); every other link is {@link verifyAdmissionChain}.
432
+ *
433
+ * Expiry, use count and withdrawal are deliberately NOT re-checked: they were conditions
434
+ * at redemption (`CadreInviteUsage.Authorized`), and membership persists until an owner
435
+ * removes the row — exactly as an owner-vouched row's membership does. Keeping a device
436
+ * out is removing its row AND withdrawing the invitation.
437
+ *
438
+ * Returns a boolean and never throws (the siblings' contract): malformed input or any
439
+ * crypto failure resolves to `false`, logged at debug.
440
+ */
441
+ export function verifyInvitationAdmission(
442
+ partyId: string,
443
+ row: CadrePeerVoucherFields,
444
+ usage: CadreInviteUsageRow,
445
+ invite: CadreInviteRow,
446
+ isAnchored: (ownerKey: string) => boolean,
447
+ ): boolean {
448
+ try {
449
+ return row.stampId !== null
450
+ && row.vouchOwner !== null
451
+ && row.vouchUsage !== null
452
+ && usage.peerStampId === row.stampId
453
+ && usage.peerId === row.peerId
454
+ && verifyAdmissionChain(partyId, row.vouchOwner, row.vouchUsage, usage, invite, isAnchored);
455
+ } catch (error) {
456
+ log('verifyInvitationAdmission failed: %o', error);
457
+ return false;
458
+ }
459
+ }
460
+
461
+ /**
462
+ * Is an invitation-admitted `OwnerKey` row (`vouchSig` null, `vouchUsage` set) an owner
463
+ * this node should derive into its anchor? The read-side mirror of the consent branch of
464
+ * `OwnerKey.Authorized` (`owner-anchor-sync.ts` → `deriveOwnerAnchor`): the invitation
465
+ * must grant ownership, and the usage must name this key (`peerKey`) and this exact row
466
+ * incarnation (`ownerStampId`); every other link is {@link verifyAdmissionChain}. Same
467
+ * never-throws contract and the same deliberate omissions as
468
+ * {@link verifyInvitationAdmission}.
469
+ */
470
+ export function verifyInvitationOwnerAdmission(
471
+ partyId: string,
472
+ row: OwnerKeyRow,
473
+ usage: CadreInviteUsageRow,
474
+ invite: CadreInviteRow,
475
+ isAnchored: (ownerKey: string) => boolean,
476
+ ): boolean {
477
+ try {
478
+ return row.vouchOwner !== null
479
+ && row.vouchUsage !== null
480
+ && invite.grantsOwner
481
+ && usage.peerKey === row.key
482
+ && usage.ownerStampId === row.stampId
483
+ && verifyAdmissionChain(partyId, row.vouchOwner, row.vouchUsage, usage, invite, isAnchored);
484
+ } catch (error) {
485
+ log('verifyInvitationOwnerAdmission failed: %o', error);
486
+ return false;
487
+ }
488
+ }