@serfab/cadre-core 1.16.0 → 1.18.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 (268) hide show
  1. package/README.md +4 -3
  2. package/dist/bootstrap-peer-store-file.js +1 -1
  3. package/dist/bootstrap-peer-store.d.ts +1 -1
  4. package/dist/bootstrap-peer-store.js +4 -4
  5. package/dist/cadre-invite-protocol.d.ts +81 -142
  6. package/dist/cadre-invite-protocol.js +168 -458
  7. package/dist/cadre-invite-protocol.js.map +1 -1
  8. package/dist/cadre-join-request.d.ts +15 -2
  9. package/dist/cadre-join-request.js +22 -3
  10. package/dist/cadre-join-request.js.map +1 -1
  11. package/dist/cadre-node.d.ts +1089 -475
  12. package/dist/cadre-node.js +2170 -1037
  13. package/dist/cadre-node.js.map +1 -1
  14. package/dist/control-authorization.d.ts +36 -14
  15. package/dist/control-authorization.js +20 -3
  16. package/dist/control-authorization.js.map +1 -1
  17. package/dist/control-cohort.d.ts +88 -32
  18. package/dist/control-cohort.js +91 -33
  19. package/dist/control-cohort.js.map +1 -1
  20. package/dist/control-database.d.ts +856 -318
  21. package/dist/control-database.js +1422 -701
  22. package/dist/control-database.js.map +1 -1
  23. package/dist/control-formation-recorder.d.ts +46 -43
  24. package/dist/control-formation-recorder.js +77 -78
  25. package/dist/control-formation-recorder.js.map +1 -1
  26. package/dist/control-protocol-guard.d.ts +11 -4
  27. package/dist/control-protocol-guard.js +20 -5
  28. package/dist/control-protocol-guard.js.map +1 -1
  29. package/dist/control-schema-version.d.ts +97 -0
  30. package/dist/control-schema-version.js +225 -0
  31. package/dist/control-schema-version.js.map +1 -0
  32. package/dist/control-schema.d.ts +45 -5
  33. package/dist/control-schema.js +262 -78
  34. package/dist/control-schema.js.map +1 -1
  35. package/dist/control-write-retry.d.ts +45 -10
  36. package/dist/control-write-retry.js +92 -22
  37. package/dist/control-write-retry.js.map +1 -1
  38. package/dist/delegate-admission.d.ts +13 -0
  39. package/dist/delegate-admission.js +27 -0
  40. package/dist/delegate-admission.js.map +1 -1
  41. package/dist/ed25519-key.d.ts +10 -7
  42. package/dist/ed25519-key.js +23 -20
  43. package/dist/ed25519-key.js.map +1 -1
  44. package/dist/edge-node/config.d.ts +122 -0
  45. package/dist/edge-node/config.js +199 -0
  46. package/dist/edge-node/config.js.map +1 -0
  47. package/dist/edge-node/dissolve.d.ts +95 -0
  48. package/dist/edge-node/dissolve.js +184 -0
  49. package/dist/edge-node/dissolve.js.map +1 -0
  50. package/dist/edge-node/edge-join.d.ts +44 -0
  51. package/dist/edge-node/edge-join.js +227 -0
  52. package/dist/edge-node/edge-join.js.map +1 -0
  53. package/dist/edge-node/edge-node.d.ts +295 -0
  54. package/dist/edge-node/edge-node.js +704 -0
  55. package/dist/edge-node/edge-node.js.map +1 -0
  56. package/dist/edge-node/index.d.ts +18 -0
  57. package/dist/edge-node/index.js +18 -0
  58. package/dist/edge-node/index.js.map +1 -0
  59. package/dist/edge-node/join-credential.d.ts +11 -0
  60. package/dist/edge-node/join-credential.js +11 -0
  61. package/dist/edge-node/join-credential.js.map +1 -0
  62. package/dist/edge-node/lazy-raw-storage.d.ts +40 -0
  63. package/dist/edge-node/lazy-raw-storage.js +134 -0
  64. package/dist/edge-node/lazy-raw-storage.js.map +1 -0
  65. package/dist/edge-node/node-local.d.ts +60 -0
  66. package/dist/edge-node/node-local.js +58 -0
  67. package/dist/edge-node/node-local.js.map +1 -0
  68. package/dist/edge-node/options.d.ts +83 -0
  69. package/dist/edge-node/options.js +110 -0
  70. package/dist/edge-node/options.js.map +1 -0
  71. package/dist/edge-node/owner-grant.d.ts +41 -0
  72. package/dist/edge-node/owner-grant.js +38 -0
  73. package/dist/edge-node/owner-grant.js.map +1 -0
  74. package/dist/edge-node/platform.d.ts +140 -0
  75. package/dist/edge-node/platform.js +12 -0
  76. package/dist/edge-node/platform.js.map +1 -0
  77. package/dist/edge-node/strand-move.d.ts +115 -0
  78. package/dist/edge-node/strand-move.js +223 -0
  79. package/dist/edge-node/strand-move.js.map +1 -0
  80. package/dist/edge-node/strands.d.ts +28 -0
  81. package/dist/edge-node/strands.js +60 -0
  82. package/dist/edge-node/strands.js.map +1 -0
  83. package/dist/enrolled-machine-store.d.ts +3 -3
  84. package/dist/enrolled-machine-store.js +3 -3
  85. package/dist/formation-token.d.ts +48 -0
  86. package/dist/formation-token.js +26 -0
  87. package/dist/formation-token.js.map +1 -0
  88. package/dist/index.d.ts +27 -17
  89. package/dist/index.js +39 -17
  90. package/dist/index.js.map +1 -1
  91. package/dist/joined-strand-store.d.ts +29 -12
  92. package/dist/joined-strand-store.js +66 -27
  93. package/dist/joined-strand-store.js.map +1 -1
  94. package/dist/key-store.d.ts +9 -4
  95. package/dist/key-store.js +9 -4
  96. package/dist/key-store.js.map +1 -1
  97. package/dist/link-budget.d.ts +9 -7
  98. package/dist/link-budget.js +9 -7
  99. package/dist/link-budget.js.map +1 -1
  100. package/dist/machine-removal.d.ts +19 -39
  101. package/dist/machine-removal.js +24 -98
  102. package/dist/machine-removal.js.map +1 -1
  103. package/dist/member-redemption.d.ts +256 -0
  104. package/dist/member-redemption.js +490 -0
  105. package/dist/member-redemption.js.map +1 -0
  106. package/dist/membership-view.d.ts +91 -0
  107. package/dist/membership-view.js +110 -0
  108. package/dist/membership-view.js.map +1 -0
  109. package/dist/owner-anchor-sync.d.ts +4 -4
  110. package/dist/owner-anchor-sync.js +16 -15
  111. package/dist/owner-anchor-sync.js.map +1 -1
  112. package/dist/owner-grant-protocol.d.ts +251 -0
  113. package/dist/owner-grant-protocol.js +344 -0
  114. package/dist/owner-grant-protocol.js.map +1 -0
  115. package/dist/owner-key-export.d.ts +70 -0
  116. package/dist/owner-key-export.js +279 -0
  117. package/dist/owner-key-export.js.map +1 -0
  118. package/dist/owner-key-flows.d.ts +55 -0
  119. package/dist/owner-key-flows.js +60 -0
  120. package/dist/owner-key-flows.js.map +1 -0
  121. package/dist/owner-key-store.d.ts +31 -0
  122. package/dist/owner-key-store.js +81 -0
  123. package/dist/owner-key-store.js.map +1 -0
  124. package/dist/owner-keys.d.ts +78 -0
  125. package/dist/owner-keys.js +133 -0
  126. package/dist/owner-keys.js.map +1 -0
  127. package/dist/owner-signer.d.ts +54 -0
  128. package/dist/owner-signer.js +51 -0
  129. package/dist/owner-signer.js.map +1 -0
  130. package/dist/peer-addr-book.d.ts +10 -1
  131. package/dist/peer-addr-book.js +4 -3
  132. package/dist/peer-addr-book.js.map +1 -1
  133. package/dist/peer-authorization.d.ts +83 -18
  134. package/dist/peer-authorization.js +123 -53
  135. package/dist/peer-authorization.js.map +1 -1
  136. package/dist/peer-join-backfill.d.ts +89 -20
  137. package/dist/peer-join-backfill.js +265 -108
  138. package/dist/peer-join-backfill.js.map +1 -1
  139. package/dist/pending-join-runner.d.ts +48 -18
  140. package/dist/pending-join-runner.js +74 -56
  141. package/dist/pending-join-runner.js.map +1 -1
  142. package/dist/provider/rent-node.js +1 -1
  143. package/dist/provider/rent-node.js.map +1 -1
  144. package/dist/seed-bootstrap.d.ts +162 -133
  145. package/dist/seed-bootstrap.js +300 -283
  146. package/dist/seed-bootstrap.js.map +1 -1
  147. package/dist/seed-trust-policy.js +4 -4
  148. package/dist/strand-addr-protocol.d.ts +4 -1
  149. package/dist/strand-addr-protocol.js +8 -2
  150. package/dist/strand-addr-protocol.js.map +1 -1
  151. package/dist/strand-cohort.d.ts +3 -0
  152. package/dist/strand-cohort.js.map +1 -1
  153. package/dist/strand-database.d.ts +24 -2
  154. package/dist/strand-database.js +29 -6
  155. package/dist/strand-database.js.map +1 -1
  156. package/dist/strand-first-sync-gate.d.ts +176 -88
  157. package/dist/strand-first-sync-gate.js +288 -121
  158. package/dist/strand-first-sync-gate.js.map +1 -1
  159. package/dist/strand-formation-deadlines.d.ts +9 -9
  160. package/dist/strand-formation-deadlines.js +9 -9
  161. package/dist/strand-formation-manager.d.ts +23 -17
  162. package/dist/strand-formation-manager.js +46 -44
  163. package/dist/strand-formation-manager.js.map +1 -1
  164. package/dist/strand-formation-protocol.d.ts +5 -8
  165. package/dist/strand-formation-protocol.js +101 -16
  166. package/dist/strand-formation-protocol.js.map +1 -1
  167. package/dist/strand-instance-manager.d.ts +27 -18
  168. package/dist/strand-instance-manager.js +119 -50
  169. package/dist/strand-instance-manager.js.map +1 -1
  170. package/dist/strand-membership-reconciler.d.ts +16 -13
  171. package/dist/strand-membership-reconciler.js +30 -22
  172. package/dist/strand-membership-reconciler.js.map +1 -1
  173. package/dist/strand-membership-writer.d.ts +45 -22
  174. package/dist/strand-membership-writer.js +226 -131
  175. package/dist/strand-membership-writer.js.map +1 -1
  176. package/dist/strand-move.d.ts +6 -5
  177. package/dist/strand-move.js +36 -32
  178. package/dist/strand-move.js.map +1 -1
  179. package/dist/strand-peer-redial.js +2 -3
  180. package/dist/strand-peer-redial.js.map +1 -1
  181. package/dist/strand-revocation-enforcer.d.ts +134 -60
  182. package/dist/strand-revocation-enforcer.js +144 -71
  183. package/dist/strand-revocation-enforcer.js.map +1 -1
  184. package/dist/strand-solicitation.d.ts +50 -39
  185. package/dist/strand-solicitation.js +10 -3
  186. package/dist/strand-solicitation.js.map +1 -1
  187. package/dist/strand-watcher.d.ts +80 -13
  188. package/dist/strand-watcher.js +152 -48
  189. package/dist/strand-watcher.js.map +1 -1
  190. package/dist/table-change-watch.d.ts +63 -0
  191. package/dist/table-change-watch.js +209 -0
  192. package/dist/table-change-watch.js.map +1 -0
  193. package/dist/timeout-scheduler.d.ts +4 -3
  194. package/dist/timeout-scheduler.js +4 -3
  195. package/dist/timeout-scheduler.js.map +1 -1
  196. package/dist/types.d.ts +374 -150
  197. package/dist/types.js +4 -4
  198. package/dist/types.js.map +1 -1
  199. package/package.json +17 -8
  200. package/src/bootstrap-peer-store-file.ts +44 -44
  201. package/src/bootstrap-peer-store.ts +214 -214
  202. package/src/cadre-invite-protocol.ts +740 -1052
  203. package/src/cadre-join-request.ts +24 -3
  204. package/src/cadre-node.ts +2402 -1079
  205. package/src/control-authorization.ts +47 -14
  206. package/src/control-cohort.ts +204 -128
  207. package/src/control-database.ts +1749 -820
  208. package/src/control-formation-recorder.ts +359 -353
  209. package/src/control-protocol-guard.ts +23 -6
  210. package/src/control-schema-version.ts +301 -0
  211. package/src/control-schema.ts +273 -78
  212. package/src/control-write-retry.ts +641 -567
  213. package/src/delegate-admission.ts +351 -314
  214. package/src/ed25519-key.ts +174 -171
  215. package/src/edge-node/config.ts +277 -0
  216. package/src/edge-node/dissolve.ts +251 -0
  217. package/src/edge-node/edge-join.ts +319 -0
  218. package/src/edge-node/edge-node.ts +1045 -0
  219. package/src/edge-node/index.ts +59 -0
  220. package/src/edge-node/join-credential.ts +16 -0
  221. package/src/edge-node/lazy-raw-storage.ts +178 -0
  222. package/src/edge-node/node-local.ts +84 -0
  223. package/src/edge-node/options.ts +179 -0
  224. package/src/edge-node/owner-grant.ts +63 -0
  225. package/src/edge-node/platform.ts +151 -0
  226. package/src/edge-node/strand-move.ts +270 -0
  227. package/src/edge-node/strands.ts +72 -0
  228. package/src/enrolled-machine-store.ts +3 -3
  229. package/src/formation-token.ts +63 -0
  230. package/src/index.ts +899 -789
  231. package/src/joined-strand-store.ts +73 -28
  232. package/src/key-store.ts +10 -4
  233. package/src/link-budget.ts +9 -7
  234. package/src/machine-removal.ts +26 -111
  235. package/src/member-redemption.ts +649 -0
  236. package/src/membership-view.ts +144 -0
  237. package/src/owner-anchor-sync.ts +19 -18
  238. package/src/owner-grant-protocol.ts +547 -0
  239. package/src/owner-key-export.ts +348 -0
  240. package/src/owner-key-flows.ts +109 -0
  241. package/src/owner-key-store.ts +85 -0
  242. package/src/owner-keys.ts +178 -0
  243. package/src/owner-signer.ts +94 -0
  244. package/src/peer-addr-book.ts +4 -3
  245. package/src/peer-authorization.ts +558 -488
  246. package/src/peer-join-backfill.ts +971 -761
  247. package/src/pending-join-runner.ts +76 -53
  248. package/src/provider/rent-node.ts +1 -1
  249. package/src/seed-bootstrap.ts +343 -349
  250. package/src/seed-trust-policy.ts +4 -4
  251. package/src/strand-addr-protocol.ts +547 -538
  252. package/src/strand-cohort.ts +4 -0
  253. package/src/strand-database.ts +352 -319
  254. package/src/strand-first-sync-gate.ts +334 -133
  255. package/src/strand-formation-deadlines.ts +227 -227
  256. package/src/strand-formation-manager.ts +731 -725
  257. package/src/strand-formation-protocol.ts +122 -24
  258. package/src/strand-instance-manager.ts +2092 -2026
  259. package/src/strand-membership-reconciler.ts +33 -25
  260. package/src/strand-membership-writer.ts +2093 -1974
  261. package/src/strand-move.ts +50 -38
  262. package/src/strand-peer-redial.ts +2 -3
  263. package/src/strand-revocation-enforcer.ts +740 -654
  264. package/src/strand-solicitation.ts +61 -34
  265. package/src/strand-watcher.ts +175 -56
  266. package/src/table-change-watch.ts +285 -0
  267. package/src/timeout-scheduler.ts +29 -28
  268. package/src/types.ts +391 -152
@@ -0,0 +1,344 @@
1
+ /**
2
+ * Owner grant redemption transport: `/sereus/owner-grant/1.0.0`.
3
+ *
4
+ * A holder of an owner grant (an owner-signed `OwnerGrant` row plus the private half of the
5
+ * grant keypair, see {@link OwnerGrantBundle}) dials ANY member machine the grant names and
6
+ * proves possession of the grant and of the key it wants seated; the member seats the row if
7
+ * it does not hold it yet and writes the holder's `OwnerKey` row through
8
+ * `ControlDatabase.redeemOwnerGrant`. No machine is admitted and no peer id is named: the
9
+ * holder's network identity has nothing to do with the key it seats, so unlike the invitation
10
+ * protocol there is no connecting-identity check — both signatures already bind the grant, the
11
+ * nonce and the key. The owner that minted the grant need not be online.
12
+ *
13
+ * The exchange shape — one frame each way, the member's hardening and admission of the
14
+ * holder, the holder's address-by-address dialing in passes — is `member-redemption.ts`'s,
15
+ * shared with the invitation protocol; this module supplies the grant's bundle, request,
16
+ * checks, writes and refusal codes. Stranger-facing by design, classed `stranger-open` in
17
+ * `control-protocol-guard.ts`; the handler's own trust decision is the two signatures.
18
+ */
19
+ import debug from 'debug';
20
+ import { toString as uint8ArrayToString, fromString as uint8ArrayFromString } from 'uint8arrays';
21
+ import { sign } from '@optimystic/quereus-plugin-crypto';
22
+ import { ownerGrantRedeemMessage, ownerGrantConsentMessage, OwnerGrantIssuerUnknownError, InvitationExhaustedError } from './control-database.js';
23
+ import { verifyOwnerGrantRow, verifyOwnerGrantRedemption } from './peer-authorization.js';
24
+ import { OWNER_KEY_MAX_LABEL } from './owner-key-export.js';
25
+ import { CADRE_INVITATION_PREFIX, CADRE_JOIN_REQUEST_PREFIX, OWNER_GRANT_PREFIX, OWNER_KEY_EXPORT_STEM } from './cadre-join-request.js';
26
+ import { MemberRedemptionHandler, constraintRefusal, dialRedemption, dialRedemptionInPasses, isBoundedString, isEd25519KeyB64, isNullableBoundedString, isNullableUseCap, rejection, writeFailureRefusal } from './member-redemption.js';
27
+ const log = debug('sereus:cadre:owner-grant-proto');
28
+ /** Protocol id for owner grant redemption (parallel to `/sereus/cadre-invite/1.0.0`). */
29
+ export const OWNER_GRANT_PROTOCOL = '/sereus/owner-grant/1.0.0';
30
+ /** What the dialer calls the credential in log lines, deadline labels and error text. */
31
+ const LABEL = 'Owner grant';
32
+ /** Most machines a grant bundle may name; the issuer caps its own list far below this. */
33
+ const MAX_BUNDLE_MEMBERS = 64;
34
+ /**
35
+ * Encode a bundle for out-of-band delivery: {@link OWNER_GRANT_PREFIX}, then base64url over
36
+ * JSON. The prefix lets an app tell a grant from an invitation, a join request or an owner key
37
+ * and name the mistake when one is pasted for the other.
38
+ */
39
+ export function encodeOwnerGrant(bundle) {
40
+ return `${OWNER_GRANT_PREFIX}${uint8ArrayToString(new TextEncoder().encode(JSON.stringify(bundle)), 'base64url')}`;
41
+ }
42
+ /**
43
+ * Decode a bundle, refusing anything that is not a well-formed version-1
44
+ * {@link OwnerGrantBundle} behind {@link OWNER_GRANT_PREFIX}. Standalone, like
45
+ * `decodeCadreInvitation`, so a client without a node can decode one.
46
+ *
47
+ * @throws on an owner key, a cadre invitation or a join request, a string without the prefix
48
+ * or that is not base64url JSON after it, or one that decodes to another shape or version.
49
+ */
50
+ export function decodeOwnerGrant(encoded) {
51
+ const trimmed = encoded.trim();
52
+ if (trimmed.startsWith(OWNER_KEY_EXPORT_STEM)) {
53
+ throw new Error('This is an owner key, not an owner grant; it is private, so import it only where the app asks for an owner key');
54
+ }
55
+ if (trimmed.startsWith(CADRE_INVITATION_PREFIX)) {
56
+ throw new Error('This is a cadre invitation, not an owner grant; paste it where the app asks for an invitation');
57
+ }
58
+ if (trimmed.startsWith(CADRE_JOIN_REQUEST_PREFIX)) {
59
+ throw new Error('This is a join request, not an owner grant; paste it into the app that manages your cadre');
60
+ }
61
+ if (!trimmed.startsWith(OWNER_GRANT_PREFIX)) {
62
+ throw new Error(`Owner grant must start with "${OWNER_GRANT_PREFIX}" (this one starts with "${trimmed.slice(0, OWNER_GRANT_PREFIX.length)}")`);
63
+ }
64
+ let parsed;
65
+ try {
66
+ parsed = JSON.parse(new TextDecoder().decode(uint8ArrayFromString(trimmed.slice(OWNER_GRANT_PREFIX.length), 'base64url')));
67
+ }
68
+ catch (error) {
69
+ throw new Error('Owner grant does not decode as base64url JSON', { cause: error });
70
+ }
71
+ if (!isWellFormedOwnerGrantBundle(parsed)) {
72
+ throw new Error('Owner grant is malformed or of an unsupported version');
73
+ }
74
+ return parsed;
75
+ }
76
+ function isWellFormedOwnerGrantBundle(value) {
77
+ if (typeof value !== 'object' || value === null)
78
+ return false;
79
+ const { v, partyId, grantPrivateKey, grant, ownerKeys, members } = value;
80
+ return v === 1
81
+ && isBoundedString(partyId)
82
+ && isEd25519KeyB64(grantPrivateKey)
83
+ && isWellFormedOwnerGrantRow(grant)
84
+ && Array.isArray(ownerKeys) && ownerKeys.length > 0 && ownerKeys.every((key) => isEd25519KeyB64(key))
85
+ && Array.isArray(members) && members.length <= MAX_BUNDLE_MEMBERS && members.every((addr) => isBoundedString(addr));
86
+ }
87
+ /** Is `value` an {@link OwnerGrantRow} with every column in its stored form and bounded? */
88
+ export function isWellFormedOwnerGrantRow(value) {
89
+ if (typeof value !== 'object' || value === null)
90
+ return false;
91
+ const row = value;
92
+ return isEd25519KeyB64(row.key)
93
+ && isNullableBoundedString(row.label, OWNER_KEY_MAX_LABEL)
94
+ && isNullableBoundedString(row.expiresAt)
95
+ && isNullableUseCap(row.totalUses)
96
+ && isBoundedString(row.stampId)
97
+ && isEd25519KeyB64(row.issuerKey)
98
+ && isBoundedString(row.issuerSig);
99
+ }
100
+ /** Which refusals are worth repeating at the next address — the one place that says so. */
101
+ export const OWNER_GRANT_REJECTION_RETRYABLE = {
102
+ 'issuer-unknown': true,
103
+ 'grant-invalid': false,
104
+ 'grant-spent': false,
105
+ 'party-mismatch': false,
106
+ 'busy': true,
107
+ 'conflict': true,
108
+ 'internal': true
109
+ };
110
+ /** Is `value` a code this build knows? Own keys only, so a peer's `'constructor'` does not pass. */
111
+ export function isOwnerGrantRejectionCode(value) {
112
+ return typeof value === 'string' && Object.prototype.hasOwnProperty.call(OWNER_GRANT_REJECTION_RETRYABLE, value);
113
+ }
114
+ // ── Errors the holder sees ───────────────────────────────────────────────────
115
+ /**
116
+ * A member answered and refused (on `CadreNode.redeemOwnerGrant`'s local path, the member is
117
+ * this node). `code` is `'unrecognized'` when the frame carried none this build knows, and such
118
+ * a refusal counts as retryable, as the invitation's does.
119
+ */
120
+ export class OwnerGrantRejectedError extends Error {
121
+ /** Both arguments come straight off the wire, so neither is trusted to have its declared type. */
122
+ constructor(code, reason) {
123
+ const text = typeof reason === 'string' ? reason : 'no reason provided';
124
+ super(`Owner grant refused: ${text}`);
125
+ this.name = 'OwnerGrantRejectedError';
126
+ this.code = isOwnerGrantRejectionCode(code) ? code : 'unrecognized';
127
+ this.reason = text;
128
+ this.retryable = this.code === 'unrecognized' || OWNER_GRANT_REJECTION_RETRYABLE[this.code];
129
+ }
130
+ }
131
+ /** Every address the bundle names was tried and none accepted. Always retryable; `outcomes` says what each address did. */
132
+ export class OwnerGrantUnreachableError extends Error {
133
+ constructor(outcomes) {
134
+ super(outcomes.length === 0
135
+ ? 'Owner grant names no dialable member'
136
+ : `Owner grant could not be redeemed at any of its ${outcomes.length} address(es): `
137
+ + outcomes.map((outcome) => `${outcome.addr}: ${outcome.error.message}`).join('; '));
138
+ this.outcomes = outcomes;
139
+ this.retryable = true;
140
+ this.name = 'OwnerGrantUnreachableError';
141
+ }
142
+ }
143
+ /**
144
+ * A member accepted but its reply does not check out: another party id, another grant key, or
145
+ * a row not signed by one of the pinned owner keys. Final, for the invitation's reason: such a
146
+ * machine is not a member of the cadre the grant names.
147
+ */
148
+ export class OwnerGrantReplyInvalidError extends Error {
149
+ constructor(message) {
150
+ super(`Owner grant reply refused: ${message}`);
151
+ this.retryable = false;
152
+ this.name = 'OwnerGrantReplyInvalidError';
153
+ }
154
+ }
155
+ // ── Shape checks ─────────────────────────────────────────────────────────────
156
+ function isWellFormedRedeemRequest(value) {
157
+ if (typeof value !== 'object' || value === null)
158
+ return false;
159
+ const { v, partyId, grant, ownerKey, usageStampId, grantSig, ownerSig } = value;
160
+ return v === 1
161
+ && isBoundedString(partyId)
162
+ && isWellFormedOwnerGrantRow(grant)
163
+ && isEd25519KeyB64(ownerKey)
164
+ && isBoundedString(usageStampId)
165
+ && isBoundedString(grantSig)
166
+ && isBoundedString(ownerSig);
167
+ }
168
+ /** An acceptance frame's fields. Throws on a malformed one. */
169
+ function decodeAccepted(reply) {
170
+ if (!isBoundedString(reply.partyId) || !isWellFormedOwnerGrantRow(reply.grant)) {
171
+ throw new Error('Owner grant acceptance is missing its party id or a well-formed row');
172
+ }
173
+ return { accepted: true, partyId: reply.partyId, grant: reply.grant, alreadyOwner: reply.alreadyOwner === true };
174
+ }
175
+ // ── Request construction (holder side) ───────────────────────────────────────
176
+ /**
177
+ * Build and sign the request for `bundle`: the holder's `'redeem'` signature with the grant's
178
+ * private key (held in memory for the length of this call) and the seated key's `'consent'`
179
+ * signature through `signConsent`, the `sign` of an open owner approval on the signer that
180
+ * holds `ownerKey`; both over (grant key, `usageStampId`, `ownerKey`) for the grant's party. The
181
+ * same bytes the schema verifies at the member (`ownerGrantRedeemMessage` /
182
+ * `ownerGrantConsentMessage`), and the same request goes to every member and every pass.
183
+ */
184
+ export async function signOwnerGrantRequest(bundle, ownerKey, signConsent, usageStampId) {
185
+ const fields = { grantKey: bundle.grant.key, usageStampId, seatedKey: ownerKey };
186
+ const grantSig = sign(ownerGrantRedeemMessage(bundle.partyId, fields), bundle.grantPrivateKey, 'ed25519', 'bytes', 'base64url', 'base64url');
187
+ const ownerSig = await signConsent(ownerGrantConsentMessage(bundle.partyId, fields));
188
+ return { v: 1, partyId: bundle.partyId, grant: bundle.grant, ownerKey, usageStampId, grantSig, ownerSig };
189
+ }
190
+ /**
191
+ * The holder's check on an acceptance: the same party, the same grant key, and a row signed by
192
+ * an issuer the holder pinned from the bundle. Throws {@link OwnerGrantReplyInvalidError} naming
193
+ * the first failure. Bounded as `verifyRedeemReply` is: it catches a wrong-party or wrong-row
194
+ * answer, not a forged member, which the holder's anchor settles once its store syncs.
195
+ */
196
+ export function verifyOwnerGrantReply(reply, bundle, isTrustedIssuer) {
197
+ if (reply.partyId !== bundle.partyId) {
198
+ throw new OwnerGrantReplyInvalidError(`the member serves party ${reply.partyId}, the grant names ${bundle.partyId}`);
199
+ }
200
+ if (reply.grant.key !== bundle.grant.key) {
201
+ throw new OwnerGrantReplyInvalidError('the member answered for a different grant');
202
+ }
203
+ if (!isTrustedIssuer(reply.grant.issuerKey)) {
204
+ throw new OwnerGrantReplyInvalidError('the row the member holds is not issued by one of the grant\'s owner keys');
205
+ }
206
+ if (!verifyOwnerGrantRow(bundle.partyId, reply.grant)) {
207
+ throw new OwnerGrantReplyInvalidError('the row the member holds is not signed by its issuer');
208
+ }
209
+ }
210
+ /** Constraint names the schema refuses a redemption with, and the code each maps to. */
211
+ const REDEEM_CONSTRAINT_CODES = [
212
+ [/CHECK constraint failed: Authorized\b/, 'grant-spent', 'The grant is expired, withdrawn, exhausted, or its issuer is no longer an owner'],
213
+ [/CHECK constraint failed: (GrantPossessed|OwnerConsented|SeatedExists)\b/, 'grant-invalid', 'The redemption does not satisfy the grant'],
214
+ [/CHECK constraint failed: NotRevoked\b/, 'grant-spent', 'The grant was withdrawn'],
215
+ ];
216
+ /**
217
+ * Member side of the grant protocol (`MemberRedemptionHandler` has the shape): after the
218
+ * request's shape, it checks the party and both signatures — nothing about the connecting
219
+ * peer, whose identity is unrelated to the key it seats — then seats the row from the bundle
220
+ * when this member holds none and redeems. An issuer this member does not know as an owner is
221
+ * the one retryable refusal left: the holder tries the next address, which may hold the
222
+ * issuer's row.
223
+ */
224
+ export class OwnerGrantHandler extends MemberRedemptionHandler {
225
+ constructor(options) {
226
+ super('Owner grant', OWNER_GRANT_PROTOCOL, options);
227
+ this.busyRefusal = rejection('busy', 'Too many concurrent redemptions');
228
+ this.internalRefusal = rejection('internal', 'Internal redemption error');
229
+ this.malformedRefusal = rejection('grant-invalid', 'Malformed redemption request');
230
+ this.store = options.store;
231
+ }
232
+ isWellFormedRequest(value) {
233
+ return isWellFormedRedeemRequest(value);
234
+ }
235
+ checkRequest(request) {
236
+ if (request.partyId !== this.partyId) {
237
+ return rejection('party-mismatch', 'This machine serves another cadre');
238
+ }
239
+ const fields = { grantKey: request.grant.key, usageStampId: request.usageStampId, seatedKey: request.ownerKey };
240
+ if (!verifyOwnerGrantRedemption(this.partyId, fields, request.grantSig, request.ownerSig)) {
241
+ return rejection('grant-invalid', 'The redemption signatures do not verify');
242
+ }
243
+ return null;
244
+ }
245
+ readHeld(request) {
246
+ return this.store.queryOwnerGrant(request.grant.key);
247
+ }
248
+ isLive(request, held) {
249
+ return this.store.isOwnerGrantLive(request.grant, held);
250
+ }
251
+ /** The two writes — the seat only when this member holds no row, and the redemption, carrying the held row — as `CadreInviteHandler.redeemAdmitted`. */
252
+ async redeemAdmitted(request, remotePeerId, held) {
253
+ if (held === null) {
254
+ const refused = await this.seat(request, remotePeerId);
255
+ if (refused)
256
+ return refused;
257
+ }
258
+ let alreadyOwner;
259
+ try {
260
+ const result = await this.store.redeemOwnerGrant({
261
+ grantKey: request.grant.key,
262
+ ...(held ? { held } : {}),
263
+ seatedKey: request.ownerKey,
264
+ usageStampId: request.usageStampId,
265
+ grantSig: request.grantSig,
266
+ ownerSig: request.ownerSig
267
+ });
268
+ alreadyOwner = result.alreadyOwner;
269
+ log('grant %s redeemed for %s by %s (alreadyOwner=%s)', request.grant.key, request.ownerKey, remotePeerId, alreadyOwner);
270
+ }
271
+ catch (error) {
272
+ if (error instanceof InvitationExhaustedError) {
273
+ return rejection('grant-spent', 'The grant has no uses left');
274
+ }
275
+ log('redeeming grant %s for %s failed: %o', request.grant.key, request.ownerKey, error);
276
+ return constraintRefusal(error, REDEEM_CONSTRAINT_CODES) ?? writeFailureRefusal(error);
277
+ }
278
+ // The row as held: read before the write, or the bundle's copy the seat inserted verbatim or found.
279
+ return { accepted: true, partyId: this.partyId, grant: held ?? request.grant, alreadyOwner };
280
+ }
281
+ /** Seat the bundle's copy of the row; a refusal answered by name, or null once it is held. */
282
+ async seat(request, remotePeerId) {
283
+ try {
284
+ await this.store.seatOwnerGrant(request.grant);
285
+ return null;
286
+ }
287
+ catch (error) {
288
+ if (error instanceof OwnerGrantIssuerUnknownError) {
289
+ return rejection('issuer-unknown', 'The grant\'s issuer is not known as an owner here');
290
+ }
291
+ log('seating grant %s from %s failed: %o', request.grant.key, remotePeerId, error);
292
+ return constraintRefusal(error, REDEEM_CONSTRAINT_CODES) ?? writeFailureRefusal(error);
293
+ }
294
+ }
295
+ }
296
+ /** The grant's half of the shared dial loop: its label, protocol, bundle, codec, checks and errors. */
297
+ function grantRedemption(options) {
298
+ return {
299
+ label: LABEL,
300
+ protocolId: options.protocolId ?? OWNER_GRANT_PROTOCOL,
301
+ members: options.bundle.members,
302
+ request: options.request,
303
+ decodeAccepted,
304
+ refusal: (code, reason) => new OwnerGrantRejectedError(code, reason),
305
+ verifyAccepted: (reply) => verifyOwnerGrantReply(reply, options.bundle, options.isTrustedIssuer),
306
+ unreachable: (outcomes) => new OwnerGrantUnreachableError(outcomes),
307
+ ...(options.linkRoundTripMs !== undefined ? { linkRoundTripMs: options.linkRoundTripMs } : {}),
308
+ ...(options.addressBudgetMs !== undefined ? { addressBudgetMs: options.addressBudgetMs } : {}),
309
+ ...(options.signal ? { signal: options.signal } : {}),
310
+ };
311
+ }
312
+ /**
313
+ * Holder side: try the bundle's addresses one at a time and stop at the first acceptance or
314
+ * the first final refusal (`dialRedemption` in `member-redemption.ts`). Every address gets the
315
+ * SAME request, with one `usageStampId`: `OwnerGrantUsage` is keyed by it, so a member the
316
+ * holder gave up on that was still committing cannot spend a second use, and a retry after a
317
+ * dropped reply is answered `alreadyOwner`.
318
+ *
319
+ * @throws {OwnerGrantRejectedError} on a final refusal (its `retryable` is false).
320
+ * @throws {OwnerGrantReplyInvalidError} on an acceptance that fails {@link verifyOwnerGrantReply}.
321
+ * @throws {OwnerGrantUnreachableError} when every address was tried and none accepted.
322
+ * @throws the reason of `options.signal` once it aborts.
323
+ */
324
+ export function redeemOwnerGrantAtMembers(node, options) {
325
+ return dialRedemption(node, grantRedemption(options));
326
+ }
327
+ /**
328
+ * Holder side, in passes (`dialRedemptionInPasses` in `member-redemption.ts`): one
329
+ * {@link redeemOwnerGrantAtMembers} pass, or, with `waitForMember`, as many as it takes,
330
+ * pausing between them and never past the grant's expiry, which by this holder's clock is
331
+ * thrown as the `grant-spent` refusal a member would give. A backup grant has no expiry, so a
332
+ * wait on one ends only by acceptance, a final refusal, the signal or the node stopping.
333
+ */
334
+ export function redeemOwnerGrantInPasses(node, options) {
335
+ const passes = {
336
+ ...grantRedemption(options),
337
+ waitForMember: options.waitForMember,
338
+ expiresAt: options.bundle.grant.expiresAt,
339
+ expired: () => new OwnerGrantRejectedError('grant-spent', 'the grant expired before a member accepted it'),
340
+ ...(options.onProgress ? { onProgress: options.onProgress } : {}),
341
+ };
342
+ return dialRedemptionInPasses(node, passes);
343
+ }
344
+ //# sourceMappingURL=owner-grant-protocol.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"owner-grant-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AAC,IAAI,EAAE,eAAe,CAAC,OAAO,CAAC,CAAC,CAAC;AACxD,CAAC;AASD;;;;;;GAMG;AACH,MAAM,UAAU,wBAAwB,CAAC,IAAY,EAAE,OAAwC;IAC7F,MAAM,MAAM,GAAoD;QAC9D,GAAG,eAAe,CAAC,OAAO,CAAC;QAC3B,aAAa,EAAE,OAAO,CAAC,aAAa;QACpC,SAAS,EAAE,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,SAAS;QACzC,OAAO,EAAE,GAAG,EAAE,CAAC,IAAI,uBAAuB,CAAC,aAAa,EAAE,+CAA+C,CAAC;QAC1G,GAAG,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,OAAO,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KAClE,CAAC;IACF,OAAO,sBAAsB,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC;AAC9C,CAAC"}
@@ -0,0 +1,70 @@
1
+ /** Version 1 of the plain export. */
2
+ export declare const OWNER_KEY_EXPORT_PREFIX = "sereus-owner-key:1.";
3
+ /** Version 1 of the passphrase-locked export. */
4
+ export declare const LOCKED_OWNER_KEY_EXPORT_PREFIX = "sereus-owner-key-locked:1.";
5
+ /** Longest label an owner key carries, the user's name for it ("phone", "paper backup"): in an export and on its `OwnerKey` row (the control schema's `LabelLength` check). */
6
+ export declare const OWNER_KEY_MAX_LABEL = 64;
7
+ export type OwnerKeyExportErrorCode =
8
+ /** Owner-key text whose body does not decode, or a version this build does not read. */
9
+ 'malformed'
10
+ /** The passphrase does not open a locked export, or the locked text was altered. */
11
+ | 'wrong-passphrase'
12
+ /** A locked export given with no passphrase (`importOwnerKeyInto` in `owner-key-flows.ts`). */
13
+ | 'passphrase-required'
14
+ /** Not owner-key text; the message names an invitation, an owner grant, a join request or a join code. */
15
+ | 'not-an-owner-key'
16
+ /** A locked export asks for more scrypt work than {@link MAX_SCRYPT_N} and {@link MAX_SCRYPT_R_TIMES_P} allow. */
17
+ | 'cost-too-high';
18
+ /** Every refusal from this module. The message is for the user and never includes key material. */
19
+ export declare class OwnerKeyExportError extends Error {
20
+ readonly code: OwnerKeyExportErrorCode;
21
+ constructor(code: OwnerKeyExportErrorCode, message: string, options?: {
22
+ cause?: unknown;
23
+ });
24
+ }
25
+ /** What an export holds once read. */
26
+ export interface OwnerKeyExportContents {
27
+ /** The 32-byte Ed25519 seed, base64url: the input to `importOwnerKey`. */
28
+ privateKeyB64: string;
29
+ /** The matching public key, base64url: the `OwnerKey.Key` it signs for. */
30
+ publicKey: string;
31
+ label?: string;
32
+ }
33
+ /** {@link decodeOwnerKeyExport}'s answer: the key, or word that a passphrase is needed ({@link unlockOwnerKeyExport}). */
34
+ export type DecodedOwnerKeyExport = ({
35
+ locked: false;
36
+ } & OwnerKeyExportContents) | {
37
+ locked: true;
38
+ };
39
+ export interface OwnerKeyExportInput {
40
+ /** The 32-byte Ed25519 seed, base64url (`readOwnerPrivateKey`). */
41
+ privateKeyB64: string;
42
+ /** Trimmed; at most {@link OWNER_KEY_MAX_LABEL} characters; a blank one is left out. */
43
+ label?: string;
44
+ /** Locks the export when present; must not be empty. */
45
+ passphrase?: string;
46
+ }
47
+ /**
48
+ * Encode an owner key as text, locked under `passphrase` when one is given.
49
+ *
50
+ * @throws {OwnerKeyExportError} `malformed` for a key that is not a 32-byte seed, an over-long
51
+ * label or an empty passphrase.
52
+ */
53
+ export declare function encodeOwnerKeyExport(input: OwnerKeyExportInput): Promise<string>;
54
+ /**
55
+ * Read owner-key text without a passphrase: a plain export's key, or `{ locked: true }` for a
56
+ * locked one whose envelope is well formed and whose scrypt cost is within the cap, so an app
57
+ * refuses a hostile file before asking for the passphrase.
58
+ *
59
+ * @throws {OwnerKeyExportError} `not-an-owner-key` (naming an invitation, an owner grant, a join
60
+ * request or a join code), `malformed` or `cost-too-high`.
61
+ */
62
+ export declare function decodeOwnerKeyExport(text: string): DecodedOwnerKeyExport;
63
+ /**
64
+ * Read owner-key text with its passphrase. A plain export is read as {@link decodeOwnerKeyExport}
65
+ * reads it, the passphrase unused.
66
+ *
67
+ * @throws {OwnerKeyExportError} as {@link decodeOwnerKeyExport}, and `wrong-passphrase` when the
68
+ * passphrase does not open it or the sealed part, nonce or scrypt parameters were altered.
69
+ */
70
+ export declare function unlockOwnerKeyExport(text: string, passphrase: string): Promise<OwnerKeyExportContents>;