@941design/marmot 0.0.0-stage → 0.2.0-alpha.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 (192) hide show
  1. package/CHANGELOG.md +709 -0
  2. package/LICENSE +21 -0
  3. package/README.md +33 -2
  4. package/dist/account/admin.d.ts +72 -0
  5. package/dist/account/admin.js +209 -0
  6. package/dist/account/device-deps.d.ts +379 -0
  7. package/dist/account/device-deps.js +36 -0
  8. package/dist/account/device-lineage.d.ts +199 -0
  9. package/dist/account/device-lineage.js +390 -0
  10. package/dist/account/device-registry.d.ts +93 -0
  11. package/dist/account/device-registry.js +1290 -0
  12. package/dist/account/device-rotation.d.ts +61 -0
  13. package/dist/account/device-rotation.js +827 -0
  14. package/dist/account/enrolment.d.ts +339 -0
  15. package/dist/account/enrolment.js +1247 -0
  16. package/dist/account/identity-proof.d.ts +79 -0
  17. package/dist/account/identity-proof.js +264 -0
  18. package/dist/account/index.d.ts +17 -0
  19. package/dist/account/index.js +16 -0
  20. package/dist/account/restore-rotation.d.ts +21 -0
  21. package/dist/account/restore-rotation.js +22 -0
  22. package/dist/account/same-account-authorization.d.ts +86 -0
  23. package/dist/account/same-account-authorization.js +180 -0
  24. package/dist/account/teardown.d.ts +127 -0
  25. package/dist/account/teardown.js +856 -0
  26. package/dist/client/before-create-group.d.ts +18 -0
  27. package/dist/client/before-create-group.js +65 -0
  28. package/dist/client/before-publish-hooks.d.ts +121 -0
  29. package/dist/client/before-publish-hooks.js +180 -0
  30. package/dist/client/client-config.d.ts +42 -0
  31. package/dist/client/client-config.js +13 -0
  32. package/dist/client/commit-redrive.d.ts +68 -0
  33. package/dist/client/commit-redrive.js +221 -0
  34. package/dist/client/component-update.d.ts +43 -0
  35. package/dist/client/component-update.js +53 -0
  36. package/dist/client/confirm-hooks.d.ts +50 -0
  37. package/dist/client/confirm-hooks.js +59 -0
  38. package/dist/client/create-client.d.ts +1022 -0
  39. package/dist/client/create-client.js +1125 -0
  40. package/dist/client/group-disband.d.ts +28 -0
  41. package/dist/client/group-disband.js +64 -0
  42. package/dist/client/group-history.d.ts +435 -0
  43. package/dist/client/group-history.js +1295 -0
  44. package/dist/client/group-profile.d.ts +31 -0
  45. package/dist/client/group-profile.js +62 -0
  46. package/dist/client/group-records.d.ts +485 -0
  47. package/dist/client/group-records.js +1034 -0
  48. package/dist/client/group-removal.d.ts +36 -0
  49. package/dist/client/group-removal.js +127 -0
  50. package/dist/client/group-remove-member.d.ts +49 -0
  51. package/dist/client/group-remove-member.js +163 -0
  52. package/dist/client/group-runtime.d.ts +388 -0
  53. package/dist/client/group-runtime.js +2701 -0
  54. package/dist/client/group-snapshot.d.ts +98 -0
  55. package/dist/client/group-snapshot.js +453 -0
  56. package/dist/client/history-pages.d.ts +107 -0
  57. package/dist/client/history-pages.js +194 -0
  58. package/dist/client/inbox-send.d.ts +19 -0
  59. package/dist/client/inbox-send.js +71 -0
  60. package/dist/client/index.d.ts +21 -0
  61. package/dist/client/index.js +13 -0
  62. package/dist/client/ingest.d.ts +82 -0
  63. package/dist/client/ingest.js +1550 -0
  64. package/dist/client/lifecycle-events.d.ts +63 -0
  65. package/dist/client/lifecycle-events.js +35 -0
  66. package/dist/client/opaque-candidates.d.ts +81 -0
  67. package/dist/client/opaque-candidates.js +169 -0
  68. package/dist/client/own-change-outbox.d.ts +66 -0
  69. package/dist/client/own-change-outbox.js +156 -0
  70. package/dist/client/pending-leaf.d.ts +116 -0
  71. package/dist/client/pending-leaf.js +378 -0
  72. package/dist/client/permissions.d.ts +9 -0
  73. package/dist/client/permissions.js +12 -0
  74. package/dist/client/pipeline.d.ts +554 -0
  75. package/dist/client/pipeline.js +3545 -0
  76. package/dist/client/reentrancy-guard.d.ts +96 -0
  77. package/dist/client/reentrancy-guard.js +153 -0
  78. package/dist/client/rekey.d.ts +58 -0
  79. package/dist/client/rekey.js +149 -0
  80. package/dist/client/transport-policy.d.ts +181 -0
  81. package/dist/client/transport-policy.js +214 -0
  82. package/dist/history/equivocation.d.ts +55 -0
  83. package/dist/history/equivocation.js +113 -0
  84. package/dist/history/fencing.d.ts +25 -0
  85. package/dist/history/fencing.js +61 -0
  86. package/dist/history/hex.d.ts +3 -0
  87. package/dist/history/hex.js +10 -0
  88. package/dist/history/index.d.ts +73 -0
  89. package/dist/history/index.js +31 -0
  90. package/dist/history/notifications.d.ts +270 -0
  91. package/dist/history/notifications.js +264 -0
  92. package/dist/history/ordering-key.d.ts +24 -0
  93. package/dist/history/ordering-key.js +101 -0
  94. package/dist/history/own-change-fork.d.ts +112 -0
  95. package/dist/history/own-change-fork.js +196 -0
  96. package/dist/history/own-change.d.ts +106 -0
  97. package/dist/history/own-change.js +146 -0
  98. package/dist/history/policy.d.ts +48 -0
  99. package/dist/history/policy.js +52 -0
  100. package/dist/history/retained-window.d.ts +215 -0
  101. package/dist/history/retained-window.js +565 -0
  102. package/dist/inbox/demux.d.ts +90 -0
  103. package/dist/inbox/demux.js +78 -0
  104. package/dist/inbox/handler-registry.d.ts +42 -0
  105. package/dist/inbox/handler-registry.js +37 -0
  106. package/dist/inbox/index.d.ts +11 -0
  107. package/dist/inbox/index.js +10 -0
  108. package/dist/inbox/seen-set.d.ts +82 -0
  109. package/dist/inbox/seen-set.js +202 -0
  110. package/dist/inbox/subscription.d.ts +236 -0
  111. package/dist/inbox/subscription.js +644 -0
  112. package/dist/index.d.ts +10 -0
  113. package/dist/index.js +9 -0
  114. package/dist/invitations/competing-welcome.d.ts +223 -0
  115. package/dist/invitations/competing-welcome.js +128 -0
  116. package/dist/invitations/decline.d.ts +61 -0
  117. package/dist/invitations/decline.js +143 -0
  118. package/dist/invitations/expect-welcome.d.ts +66 -0
  119. package/dist/invitations/expect-welcome.js +138 -0
  120. package/dist/invitations/index.d.ts +16 -0
  121. package/dist/invitations/index.js +16 -0
  122. package/dist/invitations/invite.d.ts +303 -0
  123. package/dist/invitations/invite.js +966 -0
  124. package/dist/invitations/offers.d.ts +79 -0
  125. package/dist/invitations/offers.js +115 -0
  126. package/dist/invitations/on-proposal.d.ts +47 -0
  127. package/dist/invitations/on-proposal.js +40 -0
  128. package/dist/invitations/pending-proposal-policy.d.ts +52 -0
  129. package/dist/invitations/pending-proposal-policy.js +139 -0
  130. package/dist/invitations/proposal-deadline.d.ts +133 -0
  131. package/dist/invitations/proposal-deadline.js +164 -0
  132. package/dist/invitations/relay-planes.d.ts +43 -0
  133. package/dist/invitations/relay-planes.js +70 -0
  134. package/dist/invitations/welcome-processor.d.ts +253 -0
  135. package/dist/invitations/welcome-processor.js +1180 -0
  136. package/dist/key-packages/device-key.d.ts +131 -0
  137. package/dist/key-packages/device-key.js +272 -0
  138. package/dist/key-packages/device-rotation-records.d.ts +83 -0
  139. package/dist/key-packages/device-rotation-records.js +173 -0
  140. package/dist/key-packages/group-refs.d.ts +40 -0
  141. package/dist/key-packages/group-refs.js +79 -0
  142. package/dist/key-packages/index.d.ts +18 -0
  143. package/dist/key-packages/index.js +18 -0
  144. package/dist/key-packages/key-package-ref.d.ts +16 -0
  145. package/dist/key-packages/key-package-ref.js +21 -0
  146. package/dist/key-packages/observation-stream.d.ts +4 -0
  147. package/dist/key-packages/observation-stream.js +120 -0
  148. package/dist/key-packages/offer-rumor.d.ts +26 -0
  149. package/dist/key-packages/offer-rumor.js +69 -0
  150. package/dist/key-packages/publisher.d.ts +174 -0
  151. package/dist/key-packages/publisher.js +1247 -0
  152. package/dist/key-packages/select-key-package.d.ts +24 -0
  153. package/dist/key-packages/select-key-package.js +90 -0
  154. package/dist/key-packages/slot-manager.d.ts +167 -0
  155. package/dist/key-packages/slot-manager.js +327 -0
  156. package/dist/key-packages/validate-key-package-event.d.ts +3 -0
  157. package/dist/key-packages/validate-key-package-event.js +49 -0
  158. package/dist/media/cache.d.ts +14 -0
  159. package/dist/media/cache.js +10 -0
  160. package/dist/media/index.d.ts +52 -0
  161. package/dist/media/index.js +71 -0
  162. package/dist/mls.d.ts +15 -0
  163. package/dist/mls.js +15 -0
  164. package/dist/shared/account-pubkey.d.ts +4 -0
  165. package/dist/shared/account-pubkey.js +10 -0
  166. package/dist/shared/errors.d.ts +21 -0
  167. package/dist/shared/errors.js +85 -0
  168. package/dist/shared/hex.d.ts +4 -0
  169. package/dist/shared/hex.js +13 -0
  170. package/dist/shared/index.d.ts +14 -0
  171. package/dist/shared/index.js +14 -0
  172. package/dist/shared/kinds.d.ts +8 -0
  173. package/dist/shared/kinds.js +28 -0
  174. package/dist/shared/publish-classification.d.ts +26 -0
  175. package/dist/shared/publish-classification.js +31 -0
  176. package/dist/storage/envelope.d.ts +39 -0
  177. package/dist/storage/envelope.js +201 -0
  178. package/dist/storage/index.d.ts +227 -0
  179. package/dist/storage/index.js +459 -0
  180. package/dist/storage/indexeddb/idb-types.d.ts +68 -0
  181. package/dist/storage/indexeddb/idb-types.js +1 -0
  182. package/dist/storage/indexeddb/index.d.ts +17 -0
  183. package/dist/storage/indexeddb/index.js +141 -0
  184. package/dist/storage/lease.d.ts +41 -0
  185. package/dist/storage/lease.js +117 -0
  186. package/dist/storage/memory/deterministic-signer.d.ts +27 -0
  187. package/dist/storage/memory/deterministic-signer.js +65 -0
  188. package/dist/storage/memory/index.d.ts +32 -0
  189. package/dist/storage/memory/index.js +87 -0
  190. package/dist/storage/memory/lock-manager.d.ts +8 -0
  191. package/dist/storage/memory/lock-manager.js +71 -0
  192. package/package.json +66 -3
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Markus Rother
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,34 @@
1
- # Temporary Holding Version
1
+ # `@941design/marmot`
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ A Marmot (MLS-over-Nostr) group-messaging client. `createMarmotClient` builds
4
+ a client over `@941design/marmot-engine`, signing through a
5
+ `@941design/nostr-events` signer and talking to relays through a
6
+ `@941design/nostr-events` transport.
7
+
8
+ > **Pre-1.0.** Every `0.x` release may break the API. Pin an exact version.
9
+
10
+ ## Install
11
+
12
+ ```sh
13
+ pnpm add @941design/marmot @941design/nostr-events nostr-tools
14
+ ```
15
+
16
+ `@941design/nostr-events` is a peer dependency, so the client and your
17
+ application share one signer and transport implementation.
18
+
19
+ ## Subpaths
20
+
21
+ | Subpath | Surface |
22
+ | ------------------------------------- | ----------------------------------------------------------- |
23
+ | `@941design/marmot` | `createMarmotClient`, the client types, errors and events |
24
+ | `@941design/marmot/mls` | The MLS types the client exposes (one shared `ts-mls`) |
25
+ | `@941design/marmot/storage` | The storage contract a client is built over |
26
+ | `@941design/marmot/storage/memory` | In-memory stores (tests, ephemeral sessions) |
27
+ | `@941design/marmot/storage/indexeddb` | IndexedDB stores (browsers) |
28
+
29
+ Import MLS types from `@941design/marmot/mls`, never from `ts-mls` directly:
30
+ the client and your code must see the same `ts-mls` instance.
31
+
32
+ ## License
33
+
34
+ MIT — see [`LICENSE`](./LICENSE).
@@ -0,0 +1,72 @@
1
+ /**
2
+ * The admin model (S9; ADR-006, AC-ADMIN-1..4, AC-ADMIN-8/9): the vendored
3
+ * admin policy plus ONE deviation — a SelfRemove from a leaf of an admin
4
+ * account is legal when that account keeps another leaf ("the last active
5
+ * leaf of an admin account" is what still counts as an active admin leaver).
6
+ *
7
+ * Everything here is a pure function of a ratchet tree and the admin list,
8
+ * so the handle's queries (`activeAdmins`, `isActiveAdmin`,
9
+ * `isLastActiveAdmin`) and the three engine seams answer from the SAME
10
+ * code (AC-ADMIN-8):
11
+ *
12
+ * - `adminPolicyCallback` ({@link createAdminDeviationPolicy}, receive
13
+ * path): a SelfRemove proposal from the last leaf of an admin account is
14
+ * rejected before it is staged (the engine hands standalone proposals to
15
+ * the callback since `seam-engine-ingest-3-proposal-policy`); a commit
16
+ * carrying a SelfRemove of an admin account's leaf is accepted iff that
17
+ * account keeps a leaf in the commit's projected tree and the whole
18
+ * union is one the vendored authorization rule lets its committer make —
19
+ * the vendored callback, which refuses every admin's SelfRemove, judges
20
+ * the rest;
21
+ * - `authorizeCommit` ({@link createAdminAuthorizeCommit}, send path): a
22
+ * union carrying a SelfRemove of the last leaf of an admin account is
23
+ * refused (every receiver would reject it);
24
+ * - `isActiveAdminLeaver` ({@link isLastActiveLeafOfAdmin}, the engine's
25
+ * SelfRemove auto-commit election, `seam-engine-group-engine-4-...`):
26
+ * a leaver counts as an active admin only when the batch removes its
27
+ * admin account's last leaf, so a peer auto-commits an admin sibling
28
+ * leaf's `leave()` (AC-ADMIN-9).
29
+ *
30
+ * The engine defaults (vendored callback, `decideCommitAuthorization`,
31
+ * `isAdminAccountLeaver`) reject the AC-ADMIN-4 case: that is the seam proof.
32
+ */
33
+ import { decideCommitAuthorization } from '@941design/marmot-engine/core';
34
+ import { type ActiveAdminLeaverPredicate } from '@941design/marmot-engine/engine';
35
+ import { type Proposal, type RatchetTree } from '@941design/marmot-engine/mls';
36
+ import { type AdminPolicyFactory } from './identity-proof.js';
37
+ /** Every leaf index `account` holds in `ratchetTree`, ascending. */
38
+ export declare function accountLeafIndexes(ratchetTree: RatchetTree, account: string): number[];
39
+ /** AC-ADMIN-2's "active admins": the listed admin accounts that hold a current leaf, in list order. */
40
+ export declare function activeAdmins(ratchetTree: RatchetTree, adminPubkeys: readonly string[]): string[];
41
+ export declare function isActiveAdmin(ratchetTree: RatchetTree, adminPubkeys: readonly string[], account: string): boolean;
42
+ /** AC-ADMIN-2: `true` iff `account` is the only listed account with a current leaf. */
43
+ export declare function isLastActiveAdmin(ratchetTree: RatchetTree, adminPubkeys: readonly string[], account: string): boolean;
44
+ /**
45
+ * The engine's `isActiveAdminLeaver` seam: `leaverLeafIndex` is the last
46
+ * active leaf of an admin account — the account is listed and no leaf of it
47
+ * survives the removal of `leaverLeafIndices` (the whole batch). A leaf
48
+ * naming no account is treated as an active admin (fail-closed).
49
+ */
50
+ export declare const isLastActiveLeafOfAdmin: ActiveAdminLeaverPredicate;
51
+ /**
52
+ * The receive-path deviation (AC-ADMIN-4) as an `adminPolicyCallback`
53
+ * factory wrapping `inner` (default: the vendored callback), which judges
54
+ * everything else. Wrapped in turn by the same-account rules and S3's proof
55
+ * gate (`client/client-config.ts`).
56
+ */
57
+ export declare function createAdminDeviationPolicy(inner?: AdminPolicyFactory): AdminPolicyFactory;
58
+ /**
59
+ * The send-path counterpart over the tree `ratchetTree()` returns (the
60
+ * engine's current state — the commit's parent): a union carrying a
61
+ * SelfRemove of the last leaf of an admin account is refused; everything
62
+ * else is `inner`'s decision.
63
+ */
64
+ export declare function createAdminAuthorizeCommit(inner: typeof decideCommitAuthorization, ratchetTree: () => RatchetTree): typeof decideCommitAuthorization;
65
+ /**
66
+ * AC-ADMIN-1: `accounts` as an admin list for `ratchetTree` — rejected
67
+ * `invalid-admin-policy` when empty, when it names an account (or a string
68
+ * that is no account) holding no current leaf, or names one twice.
69
+ */
70
+ export declare function validateAdminList(ratchetTree: RatchetTree, accounts: unknown): readonly string[];
71
+ /** The AppDataUpdate proposal that sets the group's admin list (admin-policy-v1). */
72
+ export declare function adminPolicyProposal(admins: readonly string[]): Proposal;
@@ -0,0 +1,209 @@
1
+ /**
2
+ * The admin model (S9; ADR-006, AC-ADMIN-1..4, AC-ADMIN-8/9): the vendored
3
+ * admin policy plus ONE deviation — a SelfRemove from a leaf of an admin
4
+ * account is legal when that account keeps another leaf ("the last active
5
+ * leaf of an admin account" is what still counts as an active admin leaver).
6
+ *
7
+ * Everything here is a pure function of a ratchet tree and the admin list,
8
+ * so the handle's queries (`activeAdmins`, `isActiveAdmin`,
9
+ * `isLastActiveAdmin`) and the three engine seams answer from the SAME
10
+ * code (AC-ADMIN-8):
11
+ *
12
+ * - `adminPolicyCallback` ({@link createAdminDeviationPolicy}, receive
13
+ * path): a SelfRemove proposal from the last leaf of an admin account is
14
+ * rejected before it is staged (the engine hands standalone proposals to
15
+ * the callback since `seam-engine-ingest-3-proposal-policy`); a commit
16
+ * carrying a SelfRemove of an admin account's leaf is accepted iff that
17
+ * account keeps a leaf in the commit's projected tree and the whole
18
+ * union is one the vendored authorization rule lets its committer make —
19
+ * the vendored callback, which refuses every admin's SelfRemove, judges
20
+ * the rest;
21
+ * - `authorizeCommit` ({@link createAdminAuthorizeCommit}, send path): a
22
+ * union carrying a SelfRemove of the last leaf of an admin account is
23
+ * refused (every receiver would reject it);
24
+ * - `isActiveAdminLeaver` ({@link isLastActiveLeafOfAdmin}, the engine's
25
+ * SelfRemove auto-commit election, `seam-engine-group-engine-4-...`):
26
+ * a leaver counts as an active admin only when the batch removes its
27
+ * admin account's last leaf, so a peer auto-commits an admin sibling
28
+ * leaf's `leave()` (AC-ADMIN-9).
29
+ *
30
+ * The engine defaults (vendored callback, `decideCommitAuthorization`,
31
+ * `isAdminAccountLeaver`) reject the AC-ADMIN-4 case: that is the seam proof.
32
+ */
33
+ import { decideCommitAuthorization, encodeAdminPolicyV1, getCredentialPubkey, GROUP_ADMIN_POLICY_COMPONENT_ID } from '@941design/marmot-engine/core';
34
+ import { createAdminCommitPolicyCallback } from '@941design/marmot-engine/engine';
35
+ import { appDataUpdateProposalType, nodeTypes, selfRemoveProposalType, } from '@941design/marmot-engine/mls';
36
+ import { MarmotError } from '../shared/index.js';
37
+ import { projectCommitLeaves } from './identity-proof.js';
38
+ const PUBKEY = /^[0-9a-f]{64}$/;
39
+ function leafAt(ratchetTree, leafIndex) {
40
+ const node = ratchetTree[2 * leafIndex];
41
+ return node?.nodeType === nodeTypes.leaf ? node.leaf : undefined;
42
+ }
43
+ /** The account (credential pubkey) of a leaf, or `undefined` when it names none. */
44
+ function accountOf(leaf) {
45
+ if (leaf === undefined)
46
+ return undefined;
47
+ try {
48
+ return getCredentialPubkey(leaf.credential);
49
+ }
50
+ catch {
51
+ return undefined;
52
+ }
53
+ }
54
+ /** Leaf index → account of every occupied leaf of `ratchetTree`. */
55
+ function leafAccounts(ratchetTree) {
56
+ const out = new Map();
57
+ const leafCount = Math.ceil(ratchetTree.length / 2);
58
+ for (let i = 0; i < leafCount; i++) {
59
+ const leaf = leafAt(ratchetTree, i);
60
+ if (leaf !== undefined)
61
+ out.set(i, accountOf(leaf));
62
+ }
63
+ return out;
64
+ }
65
+ /** Every leaf index `account` holds in `ratchetTree`, ascending. */
66
+ export function accountLeafIndexes(ratchetTree, account) {
67
+ return [...leafAccounts(ratchetTree)].filter(([, a]) => a === account).map(([i]) => i);
68
+ }
69
+ /** AC-ADMIN-2's "active admins": the listed admin accounts that hold a current leaf, in list order. */
70
+ export function activeAdmins(ratchetTree, adminPubkeys) {
71
+ const holders = new Set(leafAccounts(ratchetTree).values());
72
+ return adminPubkeys.filter((a) => holders.has(a));
73
+ }
74
+ export function isActiveAdmin(ratchetTree, adminPubkeys, account) {
75
+ return activeAdmins(ratchetTree, adminPubkeys).includes(account);
76
+ }
77
+ /** AC-ADMIN-2: `true` iff `account` is the only listed account with a current leaf. */
78
+ export function isLastActiveAdmin(ratchetTree, adminPubkeys, account) {
79
+ const active = activeAdmins(ratchetTree, adminPubkeys);
80
+ return active.length === 1 && active[0] === account;
81
+ }
82
+ /**
83
+ * The engine's `isActiveAdminLeaver` seam: `leaverLeafIndex` is the last
84
+ * active leaf of an admin account — the account is listed and no leaf of it
85
+ * survives the removal of `leaverLeafIndices` (the whole batch). A leaf
86
+ * naming no account is treated as an active admin (fail-closed).
87
+ */
88
+ export const isLastActiveLeafOfAdmin = ({ ratchetTree, adminPubkeys, leaverLeafIndex, leaverLeafIndices }) => {
89
+ const account = accountOf(leafAt(ratchetTree, leaverLeafIndex));
90
+ if (account === undefined)
91
+ return true;
92
+ if (!adminPubkeys.includes(account))
93
+ return false;
94
+ const leaving = new Set([leaverLeafIndex, ...leaverLeafIndices]);
95
+ return !accountLeafIndexes(ratchetTree, account).some((i) => !leaving.has(i));
96
+ };
97
+ /** The SelfRemove senders of a proposal union. */
98
+ function selfRemoveSenders(proposals) {
99
+ return proposals.filter((p) => p.proposal.proposalType === selfRemoveProposalType).map((p) => (p.senderLeafIndex === undefined ? undefined : Number(p.senderLeafIndex)));
100
+ }
101
+ /**
102
+ * Whether a commit's SelfRemoves include the last leaf of an admin account,
103
+ * judged over the commit's projected tree (Removes, SelfRemoves and Adds of
104
+ * the same union applied): `true` also for a SelfRemove without a sender.
105
+ */
106
+ function removesLastAdminLeaf(ratchetTree, adminPubkeys, proposals) {
107
+ const senders = selfRemoveSenders(proposals);
108
+ if (senders.length === 0)
109
+ return false;
110
+ const projected = new Set(projectCommitLeaves(ratchetTree, proposals).map(({ leaf }) => accountOf(leaf)));
111
+ for (const sender of senders) {
112
+ if (sender === undefined)
113
+ return true;
114
+ const account = accountOf(leafAt(ratchetTree, sender));
115
+ if (account === undefined)
116
+ return true;
117
+ if (adminPubkeys.includes(account) && !projected.has(account))
118
+ return true;
119
+ }
120
+ return false;
121
+ }
122
+ /**
123
+ * The receive-path deviation (AC-ADMIN-4) as an `adminPolicyCallback`
124
+ * factory wrapping `inner` (default: the vendored callback), which judges
125
+ * everything else. Wrapped in turn by the same-account rules and S3's proof
126
+ * gate (`client/client-config.ts`).
127
+ */
128
+ export function createAdminDeviationPolicy(inner = createAdminCommitPolicyCallback) {
129
+ return (args) => {
130
+ const innerCallback = inner(args);
131
+ return (incoming) => {
132
+ if (incoming.kind === 'proposal') {
133
+ const { proposal, senderLeafIndex } = incoming.proposal;
134
+ if (proposal.proposalType === selfRemoveProposalType) {
135
+ if (senderLeafIndex === undefined)
136
+ return 'reject';
137
+ const sender = Number(senderLeafIndex);
138
+ if (isLastActiveLeafOfAdmin({ ratchetTree: args.ratchetTree, adminPubkeys: args.adminPubkeys, leaverLeafIndex: sender, leaverLeafIndices: [sender] }))
139
+ return 'reject';
140
+ }
141
+ return innerCallback(incoming);
142
+ }
143
+ const adminLeavers = selfRemoveSenders(incoming.proposals).filter((s) => s === undefined || args.adminPubkeys.includes(accountOf(leafAt(args.ratchetTree, s)) ?? ''));
144
+ if (adminLeavers.length === 0)
145
+ return innerCallback(incoming);
146
+ if (removesLastAdminLeaf(args.ratchetTree, args.adminPubkeys, incoming.proposals))
147
+ return 'reject';
148
+ // The deviation lifts only the vendored "no admin SelfRemove" refusal: the committer must still be
149
+ // allowed the WHOLE union (a non-admin: SelfRemoves only, or its own Update only).
150
+ if (incoming.senderLeafIndex === undefined)
151
+ return 'reject';
152
+ const committerIndex = Number(incoming.senderLeafIndex);
153
+ const committer = accountOf(leafAt(args.ratchetTree, committerIndex));
154
+ if (committer === undefined)
155
+ return 'reject';
156
+ const union = decideCommitAuthorization({ actorPubkey: committer, actorLeafIndex: committerIndex, adminPubkeys: args.adminPubkeys, proposals: incoming.proposals });
157
+ if (!union.authorized)
158
+ return 'reject';
159
+ const deviating = new Set(adminLeavers);
160
+ return innerCallback({
161
+ ...incoming,
162
+ proposals: incoming.proposals.filter((p) => !(p.proposal.proposalType === selfRemoveProposalType && p.senderLeafIndex !== undefined && deviating.has(Number(p.senderLeafIndex)))),
163
+ });
164
+ };
165
+ };
166
+ }
167
+ /**
168
+ * The send-path counterpart over the tree `ratchetTree()` returns (the
169
+ * engine's current state — the commit's parent): a union carrying a
170
+ * SelfRemove of the last leaf of an admin account is refused; everything
171
+ * else is `inner`'s decision.
172
+ */
173
+ export function createAdminAuthorizeCommit(inner, ratchetTree) {
174
+ return (args) => {
175
+ if (removesLastAdminLeaf(ratchetTree(), args.adminPubkeys, args.proposals))
176
+ return { authorized: false, reason: 'non-admin-proposal-union' };
177
+ return inner(args);
178
+ };
179
+ }
180
+ /**
181
+ * AC-ADMIN-1: `accounts` as an admin list for `ratchetTree` — rejected
182
+ * `invalid-admin-policy` when empty, when it names an account (or a string
183
+ * that is no account) holding no current leaf, or names one twice.
184
+ */
185
+ export function validateAdminList(ratchetTree, accounts) {
186
+ if (!Array.isArray(accounts))
187
+ throw new MarmotError('invalid-options', 'setAdmins: accounts must be an array of pubkeys');
188
+ if (accounts.length === 0) {
189
+ throw new MarmotError('invalid-admin-policy', 'setAdmins: the admin list must not be empty', { details: { reason: 'empty' } });
190
+ }
191
+ const holders = new Set(leafAccounts(ratchetTree).values());
192
+ const seen = new Set();
193
+ for (const account of accounts) {
194
+ if (typeof account !== 'string' || !PUBKEY.test(account) || !holders.has(account)) {
195
+ throw new MarmotError('invalid-admin-policy', `setAdmins: ${JSON.stringify(account)} holds no current leaf of the group`, { details: { reason: 'no-current-leaf', account } });
196
+ }
197
+ if (seen.has(account))
198
+ throw new MarmotError('invalid-admin-policy', `setAdmins: ${account} is listed twice`, { details: { reason: 'duplicate', account } });
199
+ seen.add(account);
200
+ }
201
+ return Object.freeze([...seen]);
202
+ }
203
+ /** The AppDataUpdate proposal that sets the group's admin list (admin-policy-v1). */
204
+ export function adminPolicyProposal(admins) {
205
+ return {
206
+ proposalType: appDataUpdateProposalType,
207
+ appDataUpdate: { componentId: GROUP_ADMIN_POLICY_COMPONENT_ID, operation: 'update', update: encodeAdminPolicyV1([...admins]) },
208
+ };
209
+ }