@941design/marmot-engine 0.0.0-stage → 0.2.0-alpha.1

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 (198) hide show
  1. package/CHANGELOG.md +477 -0
  2. package/LICENSE +53 -0
  3. package/README.md +33 -2
  4. package/dist/audit/emitter.d.ts +17 -0
  5. package/dist/audit/emitter.js +30 -0
  6. package/dist/audit/helpers.d.ts +16 -0
  7. package/dist/audit/helpers.js +103 -0
  8. package/dist/audit/index.d.ts +6 -0
  9. package/dist/audit/index.js +6 -0
  10. package/dist/audit/recorder.d.ts +13 -0
  11. package/dist/audit/recorder.js +24 -0
  12. package/dist/audit/sink.d.ts +16 -0
  13. package/dist/audit/sink.js +27 -0
  14. package/dist/audit/types-internal.d.ts +7 -0
  15. package/dist/audit/types-internal.js +1 -0
  16. package/dist/audit/types.d.ts +289 -0
  17. package/dist/audit/types.js +2 -0
  18. package/dist/core/account-identity-proof.d.ts +165 -0
  19. package/dist/core/account-identity-proof.js +277 -0
  20. package/dist/core/application-rumor.d.ts +45 -0
  21. package/dist/core/application-rumor.js +120 -0
  22. package/dist/core/auth-service.d.ts +2 -0
  23. package/dist/core/auth-service.js +52 -0
  24. package/dist/core/binary.d.ts +142 -0
  25. package/dist/core/binary.js +363 -0
  26. package/dist/core/capabilities.d.ts +51 -0
  27. package/dist/core/capabilities.js +102 -0
  28. package/dist/core/client-state.d.ts +119 -0
  29. package/dist/core/client-state.js +226 -0
  30. package/dist/core/commit-authorization.d.ts +26 -0
  31. package/dist/core/commit-authorization.js +26 -0
  32. package/dist/core/components/admin-policy.d.ts +4 -0
  33. package/dist/core/components/admin-policy.js +69 -0
  34. package/dist/core/components/agent-text-stream.d.ts +51 -0
  35. package/dist/core/components/agent-text-stream.js +112 -0
  36. package/dist/core/components/app-components-list.d.ts +17 -0
  37. package/dist/core/components/app-components-list.js +35 -0
  38. package/dist/core/components/avatar-url.d.ts +12 -0
  39. package/dist/core/components/avatar-url.js +85 -0
  40. package/dist/core/components/bytes.d.ts +5 -0
  41. package/dist/core/components/bytes.js +18 -0
  42. package/dist/core/components/dictionary.d.ts +80 -0
  43. package/dist/core/components/dictionary.js +175 -0
  44. package/dist/core/components/disband-validation.d.ts +21 -0
  45. package/dist/core/components/disband-validation.js +173 -0
  46. package/dist/core/components/encrypted-media.d.ts +44 -0
  47. package/dist/core/components/encrypted-media.js +210 -0
  48. package/dist/core/components/group-lifecycle.d.ts +10 -0
  49. package/dist/core/components/group-lifecycle.js +32 -0
  50. package/dist/core/components/group-profile.d.ts +24 -0
  51. package/dist/core/components/group-profile.js +35 -0
  52. package/dist/core/components/host-safety.d.ts +9 -0
  53. package/dist/core/components/host-safety.js +148 -0
  54. package/dist/core/components/ids.d.ts +93 -0
  55. package/dist/core/components/ids.js +92 -0
  56. package/dist/core/components/index.d.ts +14 -0
  57. package/dist/core/components/index.js +14 -0
  58. package/dist/core/components/integrity.d.ts +141 -0
  59. package/dist/core/components/integrity.js +293 -0
  60. package/dist/core/components/message-retention.d.ts +17 -0
  61. package/dist/core/components/message-retention.js +26 -0
  62. package/dist/core/components/nostr-routing.d.ts +8 -0
  63. package/dist/core/components/nostr-routing.js +92 -0
  64. package/dist/core/components/url.d.ts +26 -0
  65. package/dist/core/components/url.js +44 -0
  66. package/dist/core/convergence-status.d.ts +84 -0
  67. package/dist/core/convergence-status.js +82 -0
  68. package/dist/core/convergence.d.ts +567 -0
  69. package/dist/core/convergence.js +1150 -0
  70. package/dist/core/credential.d.ts +16 -0
  71. package/dist/core/credential.js +64 -0
  72. package/dist/core/default-capabilities.d.ts +9 -0
  73. package/dist/core/default-capabilities.js +34 -0
  74. package/dist/core/extensions.d.ts +9 -0
  75. package/dist/core/extensions.js +5 -0
  76. package/dist/core/grease.d.ts +2 -0
  77. package/dist/core/grease.js +6 -0
  78. package/dist/core/group-event.d.ts +18 -0
  79. package/dist/core/group-event.js +30 -0
  80. package/dist/core/group-lifecycle.d.ts +42 -0
  81. package/dist/core/group-lifecycle.js +66 -0
  82. package/dist/core/group-members.d.ts +37 -0
  83. package/dist/core/group-members.js +99 -0
  84. package/dist/core/group-message-classify.d.ts +42 -0
  85. package/dist/core/group-message-classify.js +57 -0
  86. package/dist/core/group-message-crypto.d.ts +53 -0
  87. package/dist/core/group-message-crypto.js +140 -0
  88. package/dist/core/group-message.d.ts +5 -0
  89. package/dist/core/group-message.js +11 -0
  90. package/dist/core/group.d.ts +56 -0
  91. package/dist/core/group.js +95 -0
  92. package/dist/core/inbound.d.ts +102 -0
  93. package/dist/core/inbound.js +79 -0
  94. package/dist/core/index.d.ts +29 -0
  95. package/dist/core/index.js +27 -0
  96. package/dist/core/key-package-eligibility.d.ts +31 -0
  97. package/dist/core/key-package-eligibility.js +116 -0
  98. package/dist/core/key-package-event-decode.d.ts +43 -0
  99. package/dist/core/key-package-event-decode.js +118 -0
  100. package/dist/core/key-package-event-delete.d.ts +16 -0
  101. package/dist/core/key-package-event-delete.js +46 -0
  102. package/dist/core/key-package-event-encode.d.ts +28 -0
  103. package/dist/core/key-package-event-encode.js +117 -0
  104. package/dist/core/key-package-event.d.ts +4 -0
  105. package/dist/core/key-package-event.js +8 -0
  106. package/dist/core/key-package.d.ts +66 -0
  107. package/dist/core/key-package.js +147 -0
  108. package/dist/core/leaf-succession.d.ts +83 -0
  109. package/dist/core/leaf-succession.js +97 -0
  110. package/dist/core/media/canonical.d.ts +34 -0
  111. package/dist/core/media/canonical.js +57 -0
  112. package/dist/core/media/crypto.d.ts +79 -0
  113. package/dist/core/media/crypto.js +215 -0
  114. package/dist/core/media/imeta.d.ts +33 -0
  115. package/dist/core/media/imeta.js +188 -0
  116. package/dist/core/media/locator.d.ts +44 -0
  117. package/dist/core/media/locator.js +69 -0
  118. package/dist/core/media/types.d.ts +83 -0
  119. package/dist/core/media/types.js +12 -0
  120. package/dist/core/media.d.ts +6 -0
  121. package/dist/core/media.js +12 -0
  122. package/dist/core/protocol-profile.d.ts +262 -0
  123. package/dist/core/protocol-profile.js +535 -0
  124. package/dist/core/protocol.d.ts +51 -0
  125. package/dist/core/protocol.js +41 -0
  126. package/dist/core/relay-lists.d.ts +52 -0
  127. package/dist/core/relay-lists.js +86 -0
  128. package/dist/core/retained-history.d.ts +78 -0
  129. package/dist/core/retained-history.js +59 -0
  130. package/dist/core/transport.d.ts +43 -0
  131. package/dist/core/transport.js +21 -0
  132. package/dist/core/welcome-event.d.ts +61 -0
  133. package/dist/core/welcome-event.js +116 -0
  134. package/dist/core/welcome-join.d.ts +155 -0
  135. package/dist/core/welcome-join.js +243 -0
  136. package/dist/core/welcome.d.ts +3 -0
  137. package/dist/core/welcome.js +6 -0
  138. package/dist/engine/admin-policy.d.ts +75 -0
  139. package/dist/engine/admin-policy.js +137 -0
  140. package/dist/engine/auto-committer.d.ts +39 -0
  141. package/dist/engine/auto-committer.js +42 -0
  142. package/dist/engine/delivered-payloads.d.ts +61 -0
  143. package/dist/engine/delivered-payloads.js +84 -0
  144. package/dist/engine/disband-request.d.ts +41 -0
  145. package/dist/engine/disband-request.js +86 -0
  146. package/dist/engine/disband-tombstone.d.ts +14 -0
  147. package/dist/engine/disband-tombstone.js +66 -0
  148. package/dist/engine/fork-recovery.d.ts +672 -0
  149. package/dist/engine/fork-recovery.js +1615 -0
  150. package/dist/engine/group-engine.d.ts +571 -0
  151. package/dist/engine/group-engine.js +5134 -0
  152. package/dist/engine/history-tree.d.ts +232 -0
  153. package/dist/engine/history-tree.js +651 -0
  154. package/dist/engine/index.d.ts +16 -0
  155. package/dist/engine/index.js +15 -0
  156. package/dist/engine/ingest-disposition.d.ts +5 -0
  157. package/dist/engine/ingest-disposition.js +63 -0
  158. package/dist/engine/ingest.d.ts +390 -0
  159. package/dist/engine/ingest.js +1258 -0
  160. package/dist/engine/ingestion-pool.d.ts +74 -0
  161. package/dist/engine/ingestion-pool.js +95 -0
  162. package/dist/engine/message-dedup.d.ts +16 -0
  163. package/dist/engine/message-dedup.js +20 -0
  164. package/dist/engine/own-commit-stamp.d.ts +30 -0
  165. package/dist/engine/own-commit-stamp.js +71 -0
  166. package/dist/engine/retained-store.d.ts +79 -0
  167. package/dist/engine/retained-store.js +133 -0
  168. package/dist/engine/sender-ratchet.d.ts +50 -0
  169. package/dist/engine/sender-ratchet.js +196 -0
  170. package/dist/engine/state-notifications.d.ts +122 -0
  171. package/dist/engine/state-notifications.js +173 -0
  172. package/dist/engine/tree-convergence.d.ts +41 -0
  173. package/dist/engine/tree-convergence.js +90 -0
  174. package/dist/engine/types.d.ts +366 -0
  175. package/dist/engine/types.js +14 -0
  176. package/dist/engine/wire-format.d.ts +77 -0
  177. package/dist/engine/wire-format.js +120 -0
  178. package/dist/index.d.ts +12 -0
  179. package/dist/index.js +1 -0
  180. package/dist/mls.d.ts +11 -0
  181. package/dist/mls.js +11 -0
  182. package/dist/utils/debug.d.ts +3 -0
  183. package/dist/utils/debug.js +3 -0
  184. package/dist/utils/encoding.d.ts +38 -0
  185. package/dist/utils/encoding.js +91 -0
  186. package/dist/utils/key-value.d.ts +18 -0
  187. package/dist/utils/key-value.js +2 -0
  188. package/dist/utils/nostr.d.ts +35 -0
  189. package/dist/utils/nostr.js +26 -0
  190. package/dist/utils/relay-url.d.ts +10 -0
  191. package/dist/utils/relay-url.js +23 -0
  192. package/dist/utils/tag-cardinality.d.ts +50 -0
  193. package/dist/utils/tag-cardinality.js +91 -0
  194. package/dist/utils/timestamp.d.ts +50 -0
  195. package/dist/utils/timestamp.js +95 -0
  196. package/dist/version.d.ts +112 -0
  197. package/dist/version.js +112 -0
  198. package/package.json +68 -3
@@ -0,0 +1,5 @@
1
+ /** @module @category Core - App Components */
2
+ /** Lexicographic comparison over raw bytes (matches Rust `[u8]`/`&[u8]` Ord). */
3
+ export declare function compareBytes(a: Uint8Array, b: Uint8Array): number;
4
+ /** Equality for optional byte arrays; two absent values are equal. */
5
+ export declare function bytesEqual(a: Uint8Array | undefined, b: Uint8Array | undefined): boolean;
@@ -0,0 +1,18 @@
1
+ /** @module @category Core - App Components */
2
+ /** Lexicographic comparison over raw bytes (matches Rust `[u8]`/`&[u8]` Ord). */
3
+ export function compareBytes(a, b) {
4
+ const n = Math.min(a.length, b.length);
5
+ for (let i = 0; i < n; i++) {
6
+ if (a[i] !== b[i])
7
+ return a[i] - b[i];
8
+ }
9
+ return a.length - b.length;
10
+ }
11
+ /** Equality for optional byte arrays; two absent values are equal. */
12
+ export function bytesEqual(a, b) {
13
+ if (a === undefined && b === undefined)
14
+ return true;
15
+ if (a === undefined || b === undefined)
16
+ return false;
17
+ return compareBytes(a, b) === 0;
18
+ }
@@ -0,0 +1,80 @@
1
+ /** @module @category Core - App Components */
2
+ import { AppDataDictionary, ComponentData, CustomExtension, GroupContextExtension } from "ts-mls";
3
+ import { AppComponentId } from "./ids.js";
4
+ import { GroupProfileV1 } from "./group-profile.js";
5
+ import { NostrRoutingV1 } from "./nostr-routing.js";
6
+ import { GroupAvatarUrlV1 } from "./avatar-url.js";
7
+ import { EncryptedMediaPolicyV1 } from "./encrypted-media.js";
8
+ import { AgentTextStreamQuicPolicyV1 } from "./agent-text-stream.js";
9
+ import { GroupProtocolLifecycleValue } from "./group-lifecycle.js";
10
+ /**
11
+ * Read + build helpers over the Marmot v2 app components carried in the MLS
12
+ * `app_data_dictionary` GroupContext extension (`0x0006`).
13
+ *
14
+ * The dictionary container itself — `ComponentData { componentId, data }` sorted
15
+ * by id, wrapped in the extension — is owned by ts-mls
16
+ * ({@link getAppDataDictionary} / {@link makeAppDataDictionaryExtension}), which
17
+ * binds it to the MLS transcript. This module is the generic registry over the
18
+ * opaque `data` bytes plus typed accessors that run each component's codec.
19
+ *
20
+ * Mutation (emitting `app_data_update` proposals) lives alongside the commit
21
+ * path; this module only reads existing state and builds the create-time
22
+ * dictionary.
23
+ */
24
+ /** Returns the raw component `data` bytes for a component id, or undefined. */
25
+ export declare function getComponentData(extensions: GroupContextExtension[], componentId: AppComponentId): Uint8Array | undefined;
26
+ /** Builds a single {@link ComponentData} entry. */
27
+ export declare function componentEntry(componentId: AppComponentId, data: Uint8Array): ComponentData;
28
+ /**
29
+ * Builds an {@link AppDataDictionary} from entries, sorted ascending by
30
+ * componentId. Throws on a duplicate component id.
31
+ */
32
+ export declare function buildAppDataDictionary(entries: ComponentData[]): AppDataDictionary;
33
+ /**
34
+ * Builds the `app_data_dictionary` GroupContext extension from component
35
+ * entries (sorting them first). Use at group creation to seed initial state.
36
+ */
37
+ export declare function makeAppComponentsExtension(entries: ComponentData[]): CustomExtension;
38
+ /**
39
+ * Builds the `app_data_dictionary` extension carried on a key package's LeafNode
40
+ * to advertise the component ids this member supports. The dictionary holds a
41
+ * single `app_components` (`0x0001`) entry listing {@link SUPPORTED_APP_COMPONENT_IDS}
42
+ * (or the given override). Mirrors darkmatter's `leaf_app_components_extension`.
43
+ */
44
+ export declare function makeLeafAppComponentsExtension(supportedIds?: readonly AppComponentId[]): CustomExtension;
45
+ /** The `app_components` advertising list (`0x0001`). */
46
+ export declare function getAppComponents(extensions: GroupContextExtension[]): AppComponentId[] | undefined;
47
+ /** The `group.profile.v1` component (`0x8001`). */
48
+ export declare function getGroupProfile(extensions: GroupContextExtension[]): GroupProfileV1 | undefined;
49
+ /** The `admin-policy.v1` admin pubkey set (`0x8003`). */
50
+ export declare function getAdminPolicy(extensions: GroupContextExtension[]): string[] | undefined;
51
+ /** The `transport.nostr.routing.v1` component (`0x8004`). */
52
+ export declare function getNostrRouting(extensions: GroupContextExtension[]): NostrRoutingV1 | undefined;
53
+ /** The `message-retention.v1` timer in seconds (`0x8005`). */
54
+ export declare function getMessageRetention(extensions: GroupContextExtension[]): bigint | undefined;
55
+ /** The `agent-text-stream.quic.v1` policy (`0x8006`). */
56
+ export declare function getAgentTextStreamPolicy(extensions: GroupContextExtension[]): AgentTextStreamQuicPolicyV1 | undefined;
57
+ /** The `group.avatar-url.v1` component (`0x8007`). */
58
+ export declare function getGroupAvatarUrl(extensions: GroupContextExtension[]): GroupAvatarUrlV1 | undefined;
59
+ /** The `group.encrypted-media.v1` policy (`0x8008`). */
60
+ export declare function getEncryptedMediaPolicy(extensions: GroupContextExtension[]): EncryptedMediaPolicyV1 | undefined;
61
+ /** The `group.lifecycle.v1` protocol state (`0x800c`). */
62
+ export declare function getGroupLifecycle(extensions: GroupContextExtension[]): GroupProtocolLifecycleValue | undefined;
63
+ /** Builds the `app_components` advertising entry from a list of ids. */
64
+ export declare function appComponentsEntry(ids: AppComponentId[]): ComponentData;
65
+ /** Builds the `group.profile.v1` entry. */
66
+ export declare function groupProfileEntry(profile: GroupProfileV1): ComponentData;
67
+ /** Builds the `admin-policy.v1` entry from hex admin pubkeys. */
68
+ export declare function adminPolicyEntry(adminPubkeys: string[]): ComponentData;
69
+ /** Builds the `transport.nostr.routing.v1` entry. */
70
+ export declare function nostrRoutingEntry(routing: NostrRoutingV1): ComponentData;
71
+ /** Builds the `message-retention.v1` entry. */
72
+ export declare function messageRetentionEntry(seconds: number | bigint): ComponentData;
73
+ /** Builds the `agent-text-stream.quic.v1` entry. */
74
+ export declare function agentTextStreamEntry(policy: AgentTextStreamQuicPolicyV1): ComponentData;
75
+ /** Builds the `group.avatar-url.v1` entry. */
76
+ export declare function groupAvatarUrlEntry(avatar: GroupAvatarUrlV1): ComponentData;
77
+ /** Builds the `group.encrypted-media.v1` entry. */
78
+ export declare function encryptedMediaEntry(policy: EncryptedMediaPolicyV1): ComponentData;
79
+ /** Builds the `group.lifecycle.v1` entry. */
80
+ export declare function groupLifecycleEntry(value: GroupProtocolLifecycleValue): ComponentData;
@@ -0,0 +1,175 @@
1
+ /** @module @category Core - App Components */
2
+ import { getAppDataDictionary, makeAppDataDictionaryExtension, } from "ts-mls";
3
+ import { UsageError } from "ts-mls";
4
+ import { APP_COMPONENTS_COMPONENT_ID, GROUP_ADMIN_POLICY_COMPONENT_ID, GROUP_AVATAR_URL_COMPONENT_ID, GROUP_ENCRYPTED_MEDIA_COMPONENT_ID, GROUP_MESSAGE_RETENTION_COMPONENT_ID, GROUP_LIFECYCLE_COMPONENT_ID, GROUP_PROFILE_COMPONENT_ID, AGENT_TEXT_STREAM_QUIC_COMPONENT_ID, NOSTR_ROUTING_COMPONENT_ID, SAFE_AAD_COMPONENT_ID, SUPPORTED_APP_COMPONENT_IDS, } from "./ids.js";
5
+ import { decodeComponentsList, encodeComponentsList, } from "./app-components-list.js";
6
+ import { decodeGroupProfileV1, encodeGroupProfileV1, } from "./group-profile.js";
7
+ import { decodeAdminPolicyV1, encodeAdminPolicyV1 } from "./admin-policy.js";
8
+ import { decodeNostrRoutingV1, encodeNostrRoutingV1, } from "./nostr-routing.js";
9
+ import { decodeMessageRetentionV1, encodeMessageRetentionV1, } from "./message-retention.js";
10
+ import { decodeGroupAvatarUrlV1, encodeGroupAvatarUrlV1, } from "./avatar-url.js";
11
+ import { decodeEncryptedMediaPolicyV1, encodeEncryptedMediaPolicyV1, } from "./encrypted-media.js";
12
+ import { decodeAgentTextStreamQuicPolicyV1, encodeAgentTextStreamQuicPolicyV1, } from "./agent-text-stream.js";
13
+ import { decodeGroupLifecycleV1, encodeGroupLifecycleV1, } from "./group-lifecycle.js";
14
+ /**
15
+ * Read + build helpers over the Marmot v2 app components carried in the MLS
16
+ * `app_data_dictionary` GroupContext extension (`0x0006`).
17
+ *
18
+ * The dictionary container itself — `ComponentData { componentId, data }` sorted
19
+ * by id, wrapped in the extension — is owned by ts-mls
20
+ * ({@link getAppDataDictionary} / {@link makeAppDataDictionaryExtension}), which
21
+ * binds it to the MLS transcript. This module is the generic registry over the
22
+ * opaque `data` bytes plus typed accessors that run each component's codec.
23
+ *
24
+ * Mutation (emitting `app_data_update` proposals) lives alongside the commit
25
+ * path; this module only reads existing state and builds the create-time
26
+ * dictionary.
27
+ */
28
+ // ---------------------------------------------------------------------------
29
+ // Generic core
30
+ // ---------------------------------------------------------------------------
31
+ /** Returns the raw component `data` bytes for a component id, or undefined. */
32
+ export function getComponentData(extensions, componentId) {
33
+ const dictionary = getAppDataDictionary(extensions);
34
+ return dictionary?.find((c) => c.componentId === componentId)?.data;
35
+ }
36
+ /** Builds a single {@link ComponentData} entry. */
37
+ export function componentEntry(componentId, data) {
38
+ return { componentId, data };
39
+ }
40
+ /**
41
+ * Builds an {@link AppDataDictionary} from entries, sorted ascending by
42
+ * componentId. Throws on a duplicate component id.
43
+ */
44
+ export function buildAppDataDictionary(entries) {
45
+ const sorted = [...entries].sort((a, b) => a.componentId - b.componentId);
46
+ for (let i = 1; i < sorted.length; i++) {
47
+ if (sorted[i - 1].componentId === sorted[i].componentId) {
48
+ throw new UsageError(`Duplicate app component id 0x${sorted[i].componentId.toString(16)}`);
49
+ }
50
+ }
51
+ return sorted;
52
+ }
53
+ /**
54
+ * Builds the `app_data_dictionary` GroupContext extension from component
55
+ * entries (sorting them first). Use at group creation to seed initial state.
56
+ */
57
+ export function makeAppComponentsExtension(entries) {
58
+ if (entries.some((entry) => entry.componentId === SAFE_AAD_COMPONENT_ID)) {
59
+ throw new UsageError("SafeAAD is LeafNode-only advertisement data and is not supported as group-component state");
60
+ }
61
+ return makeAppDataDictionaryExtension(buildAppDataDictionary(entries));
62
+ }
63
+ /**
64
+ * Builds the `app_data_dictionary` extension carried on a key package's LeafNode
65
+ * to advertise the component ids this member supports. The dictionary holds a
66
+ * single `app_components` (`0x0001`) entry listing {@link SUPPORTED_APP_COMPONENT_IDS}
67
+ * (or the given override). Mirrors darkmatter's `leaf_app_components_extension`.
68
+ */
69
+ export function makeLeafAppComponentsExtension(supportedIds = SUPPORTED_APP_COMPONENT_IDS) {
70
+ return makeAppDataDictionaryExtension(buildAppDataDictionary([
71
+ appComponentsEntry([APP_COMPONENTS_COMPONENT_ID, ...supportedIds]),
72
+ componentEntry(SAFE_AAD_COMPONENT_ID, encodeComponentsList([])),
73
+ ]));
74
+ }
75
+ function defineCodec(id, decode, encode) {
76
+ return { id, decode, encode };
77
+ }
78
+ const APP_COMPONENTS_CODEC = defineCodec(APP_COMPONENTS_COMPONENT_ID, decodeComponentsList, encodeComponentsList);
79
+ const GROUP_PROFILE_CODEC = defineCodec(GROUP_PROFILE_COMPONENT_ID, decodeGroupProfileV1, encodeGroupProfileV1);
80
+ const ADMIN_POLICY_CODEC = defineCodec(GROUP_ADMIN_POLICY_COMPONENT_ID, decodeAdminPolicyV1, encodeAdminPolicyV1);
81
+ const NOSTR_ROUTING_CODEC = defineCodec(NOSTR_ROUTING_COMPONENT_ID, decodeNostrRoutingV1, encodeNostrRoutingV1);
82
+ const MESSAGE_RETENTION_CODEC = defineCodec(GROUP_MESSAGE_RETENTION_COMPONENT_ID, decodeMessageRetentionV1,
83
+ // The builder accepts number | bigint; the decoder yields bigint.
84
+ (seconds) => encodeMessageRetentionV1(seconds));
85
+ const AGENT_TEXT_STREAM_CODEC = defineCodec(AGENT_TEXT_STREAM_QUIC_COMPONENT_ID, decodeAgentTextStreamQuicPolicyV1, encodeAgentTextStreamQuicPolicyV1);
86
+ const GROUP_AVATAR_URL_CODEC = defineCodec(GROUP_AVATAR_URL_COMPONENT_ID, decodeGroupAvatarUrlV1, encodeGroupAvatarUrlV1);
87
+ const ENCRYPTED_MEDIA_CODEC = defineCodec(GROUP_ENCRYPTED_MEDIA_COMPONENT_ID, decodeEncryptedMediaPolicyV1, encodeEncryptedMediaPolicyV1);
88
+ const GROUP_LIFECYCLE_CODEC = defineCodec(GROUP_LIFECYCLE_COMPONENT_ID, decodeGroupLifecycleV1, encodeGroupLifecycleV1);
89
+ /** Reads + decodes a component from the dictionary, or `undefined` if absent. */
90
+ function getComponent(extensions, codec) {
91
+ const data = getComponentData(extensions, codec.id);
92
+ return data === undefined ? undefined : codec.decode(data);
93
+ }
94
+ /** Builds a {@link ComponentData} entry by encoding `value` with `codec`. */
95
+ function entryFor(codec, value) {
96
+ return componentEntry(codec.id, codec.encode(value));
97
+ }
98
+ // ---------------------------------------------------------------------------
99
+ // Typed accessors
100
+ // ---------------------------------------------------------------------------
101
+ /** The `app_components` advertising list (`0x0001`). */
102
+ export function getAppComponents(extensions) {
103
+ return getComponent(extensions, APP_COMPONENTS_CODEC);
104
+ }
105
+ /** The `group.profile.v1` component (`0x8001`). */
106
+ export function getGroupProfile(extensions) {
107
+ return getComponent(extensions, GROUP_PROFILE_CODEC);
108
+ }
109
+ /** The `admin-policy.v1` admin pubkey set (`0x8003`). */
110
+ export function getAdminPolicy(extensions) {
111
+ return getComponent(extensions, ADMIN_POLICY_CODEC);
112
+ }
113
+ /** The `transport.nostr.routing.v1` component (`0x8004`). */
114
+ export function getNostrRouting(extensions) {
115
+ return getComponent(extensions, NOSTR_ROUTING_CODEC);
116
+ }
117
+ /** The `message-retention.v1` timer in seconds (`0x8005`). */
118
+ export function getMessageRetention(extensions) {
119
+ return getComponent(extensions, MESSAGE_RETENTION_CODEC);
120
+ }
121
+ /** The `agent-text-stream.quic.v1` policy (`0x8006`). */
122
+ export function getAgentTextStreamPolicy(extensions) {
123
+ return getComponent(extensions, AGENT_TEXT_STREAM_CODEC);
124
+ }
125
+ /** The `group.avatar-url.v1` component (`0x8007`). */
126
+ export function getGroupAvatarUrl(extensions) {
127
+ return getComponent(extensions, GROUP_AVATAR_URL_CODEC);
128
+ }
129
+ /** The `group.encrypted-media.v1` policy (`0x8008`). */
130
+ export function getEncryptedMediaPolicy(extensions) {
131
+ return getComponent(extensions, ENCRYPTED_MEDIA_CODEC);
132
+ }
133
+ /** The `group.lifecycle.v1` protocol state (`0x800c`). */
134
+ export function getGroupLifecycle(extensions) {
135
+ return getComponent(extensions, GROUP_LIFECYCLE_CODEC);
136
+ }
137
+ // ---------------------------------------------------------------------------
138
+ // Typed entry builders (for create-time dictionaries and updates)
139
+ // ---------------------------------------------------------------------------
140
+ /** Builds the `app_components` advertising entry from a list of ids. */
141
+ export function appComponentsEntry(ids) {
142
+ return entryFor(APP_COMPONENTS_CODEC, ids);
143
+ }
144
+ /** Builds the `group.profile.v1` entry. */
145
+ export function groupProfileEntry(profile) {
146
+ return entryFor(GROUP_PROFILE_CODEC, profile);
147
+ }
148
+ /** Builds the `admin-policy.v1` entry from hex admin pubkeys. */
149
+ export function adminPolicyEntry(adminPubkeys) {
150
+ return entryFor(ADMIN_POLICY_CODEC, adminPubkeys);
151
+ }
152
+ /** Builds the `transport.nostr.routing.v1` entry. */
153
+ export function nostrRoutingEntry(routing) {
154
+ return entryFor(NOSTR_ROUTING_CODEC, routing);
155
+ }
156
+ /** Builds the `message-retention.v1` entry. */
157
+ export function messageRetentionEntry(seconds) {
158
+ return entryFor(MESSAGE_RETENTION_CODEC, seconds);
159
+ }
160
+ /** Builds the `agent-text-stream.quic.v1` entry. */
161
+ export function agentTextStreamEntry(policy) {
162
+ return entryFor(AGENT_TEXT_STREAM_CODEC, policy);
163
+ }
164
+ /** Builds the `group.avatar-url.v1` entry. */
165
+ export function groupAvatarUrlEntry(avatar) {
166
+ return entryFor(GROUP_AVATAR_URL_CODEC, avatar);
167
+ }
168
+ /** Builds the `group.encrypted-media.v1` entry. */
169
+ export function encryptedMediaEntry(policy) {
170
+ return entryFor(ENCRYPTED_MEDIA_CODEC, policy);
171
+ }
172
+ /** Builds the `group.lifecycle.v1` entry. */
173
+ export function groupLifecycleEntry(value) {
174
+ return entryFor(GROUP_LIFECYCLE_CODEC, value);
175
+ }
@@ -0,0 +1,21 @@
1
+ /** @module @category Core - App Components */
2
+ import { type ClientState, type ProposalWithSender } from "ts-mls";
3
+ export type DisbandClassification = {
4
+ kind: "notDisband";
5
+ } | {
6
+ kind: "validDisband";
7
+ actorPubkey: string;
8
+ } | {
9
+ kind: "validEnablement";
10
+ actorPubkey: string;
11
+ } | {
12
+ kind: "violation";
13
+ detail: string;
14
+ };
15
+ /** Classifies lifecycle enablement and terminal commits against their authenticated parent. */
16
+ export declare function classifyDisbandCommit(args: {
17
+ parentState: ClientState;
18
+ resultingState: ClientState;
19
+ proposals: readonly ProposalWithSender[];
20
+ committerLeafIndex: number | undefined;
21
+ }): DisbandClassification;
@@ -0,0 +1,173 @@
1
+ /** @module @category Core - App Components */
2
+ import { appDataUpdateProposalType, defaultProposalTypes, getCredentialFromLeafIndex, nodeTypes, } from "ts-mls";
3
+ import { getCredentialPubkey } from "../credential.js";
4
+ import { bytesEqual } from "./bytes.js";
5
+ import { getAdminPolicy, getAppComponents, getGroupLifecycle, } from "./dictionary.js";
6
+ import { APP_COMPONENTS_COMPONENT_ID, GROUP_ADMIN_POLICY_COMPONENT_ID, GROUP_LIFECYCLE_COMPONENT_ID, } from "./ids.js";
7
+ import { encodeAdminPolicyV1 } from "./admin-policy.js";
8
+ import { encodeComponentsList } from "./app-components-list.js";
9
+ import { encodeGroupLifecycleV1 } from "./group-lifecycle.js";
10
+ function violation(detail) {
11
+ return { kind: "violation", detail };
12
+ }
13
+ function leafIndexes(state) {
14
+ const indexes = [];
15
+ for (let nodeIndex = 0; nodeIndex < state.ratchetTree.length; nodeIndex += 2) {
16
+ const node = state.ratchetTree[nodeIndex];
17
+ if (node?.nodeType === nodeTypes.leaf)
18
+ indexes.push(nodeIndex / 2);
19
+ }
20
+ return indexes;
21
+ }
22
+ function actorOf(state, leafIndex) {
23
+ try {
24
+ return getCredentialPubkey(getCredentialFromLeafIndex(state.ratchetTree, leafIndex));
25
+ }
26
+ catch {
27
+ return undefined;
28
+ }
29
+ }
30
+ function updateBytes(proposal, componentId) {
31
+ if (proposal.proposal.proposalType !== appDataUpdateProposalType ||
32
+ !("appDataUpdate" in proposal.proposal) ||
33
+ proposal.proposal.appDataUpdate.componentId !== componentId ||
34
+ proposal.proposal.appDataUpdate.operation !== "update")
35
+ return undefined;
36
+ return proposal.proposal.appDataUpdate.update;
37
+ }
38
+ /** Classifies lifecycle enablement and terminal commits against their authenticated parent. */
39
+ export function classifyDisbandCommit(args) {
40
+ let parentLifecycle;
41
+ let resultingLifecycle;
42
+ let parentRequired;
43
+ let resultingRequired;
44
+ try {
45
+ parentLifecycle = getGroupLifecycle(args.parentState.groupContext.extensions);
46
+ resultingLifecycle = getGroupLifecycle(args.resultingState.groupContext.extensions);
47
+ parentRequired =
48
+ getAppComponents(args.parentState.groupContext.extensions) ?? [];
49
+ resultingRequired =
50
+ getAppComponents(args.resultingState.groupContext.extensions) ?? [];
51
+ }
52
+ catch {
53
+ return violation("lifecycle or required-component state did not decode");
54
+ }
55
+ const parentRequires = parentRequired.includes(GROUP_LIFECYCLE_COMPONENT_ID);
56
+ let resultRequires = resultingRequired.includes(GROUP_LIFECYCLE_COMPONENT_ID);
57
+ // ts-mls exposes a removed receiver as a tombstone carrying the authenticated
58
+ // parent GroupContext, not the post-Commit roster/context. The exact inline
59
+ // proposal set below is therefore the only available resulting-state evidence
60
+ // on that receiver; MLS acceptance has already authenticated those bytes.
61
+ const receiverRemoved = args.resultingState.groupActiveState?.kind === "removedFromGroup";
62
+ const touchesLifecycle = args.proposals.some((proposal) => updateBytes(proposal, GROUP_LIFECYCLE_COMPONENT_ID) !== undefined ||
63
+ (proposal.proposal.proposalType === appDataUpdateProposalType &&
64
+ "appDataUpdate" in proposal.proposal &&
65
+ proposal.proposal.appDataUpdate.componentId ===
66
+ GROUP_LIFECYCLE_COMPONENT_ID));
67
+ if (receiverRemoved && touchesLifecycle) {
68
+ resultRequires = parentRequires;
69
+ resultingLifecycle = "disbanded";
70
+ }
71
+ if (!touchesLifecycle &&
72
+ parentLifecycle === resultingLifecycle &&
73
+ parentRequires === resultRequires)
74
+ return { kind: "notDisband" };
75
+ if (args.committerLeafIndex === undefined)
76
+ return violation("lifecycle transition has no authenticated member committer");
77
+ const actorPubkey = actorOf(args.parentState, args.committerLeafIndex);
78
+ if (!actorPubkey)
79
+ return violation("lifecycle committer credential is invalid");
80
+ let parentAdmins;
81
+ try {
82
+ parentAdmins =
83
+ getAdminPolicy(args.parentState.groupContext.extensions) ?? [];
84
+ }
85
+ catch {
86
+ return violation("parent admin-policy component did not decode");
87
+ }
88
+ if (!parentAdmins.includes(actorPubkey))
89
+ return violation("lifecycle transition committer is not a parent admin");
90
+ if (!parentRequires) {
91
+ if (parentLifecycle !== undefined)
92
+ return violation("legacy lifecycle state exists without being required");
93
+ if (resultingLifecycle !== "active" || !resultRequires)
94
+ return violation("legacy lifecycle enablement must atomically add active and required state");
95
+ if (args.proposals.length !== 2)
96
+ return violation("legacy lifecycle enablement contains an unrelated proposal");
97
+ const requiredUpdate = args.proposals.find((proposal) => updateBytes(proposal, APP_COMPONENTS_COMPONENT_ID) !== undefined);
98
+ const lifecycleUpdate = args.proposals.find((proposal) => updateBytes(proposal, GROUP_LIFECYCLE_COMPONENT_ID) !== undefined);
99
+ if (!requiredUpdate ||
100
+ !lifecycleUpdate ||
101
+ requiredUpdate.senderLeafIndex !== args.committerLeafIndex ||
102
+ lifecycleUpdate.senderLeafIndex !== args.committerLeafIndex ||
103
+ !bytesEqual(updateBytes(requiredUpdate, APP_COMPONENTS_COMPONENT_ID), encodeComponentsList(resultingRequired)) ||
104
+ !bytesEqual(updateBytes(lifecycleUpdate, GROUP_LIFECYCLE_COMPONENT_ID), encodeGroupLifecycleV1("active")))
105
+ return violation("legacy lifecycle enablement must use exact inline replacements");
106
+ for (const index of leafIndexes(args.resultingState)) {
107
+ const node = args.resultingState.ratchetTree[index * 2];
108
+ try {
109
+ if (node?.nodeType !== nodeTypes.leaf ||
110
+ !(getAppComponents(node.leaf.extensions) ?? []).includes(GROUP_LIFECYCLE_COMPONENT_ID))
111
+ return violation("resulting member does not advertise lifecycle support");
112
+ }
113
+ catch {
114
+ return violation("resulting member capability state did not decode");
115
+ }
116
+ }
117
+ return { kind: "validEnablement", actorPubkey };
118
+ }
119
+ if (!resultRequires || resultingLifecycle !== "disbanded")
120
+ return violation("required lifecycle state is absorbing and may only become disbanded");
121
+ if (parentLifecycle !== "active")
122
+ return violation("disband requires an active lifecycle parent");
123
+ const expectedRemovals = leafIndexes(args.parentState).filter((index) => index !== args.committerLeafIndex);
124
+ const removals = [];
125
+ let lifecycleUpdates = 0;
126
+ let adminUpdates = 0;
127
+ for (const proposal of args.proposals) {
128
+ if (proposal.proposal.proposalType === defaultProposalTypes.remove &&
129
+ "remove" in proposal.proposal) {
130
+ removals.push(Number(proposal.proposal.remove.removed));
131
+ continue;
132
+ }
133
+ const lifecycle = updateBytes(proposal, GROUP_LIFECYCLE_COMPONENT_ID);
134
+ if (lifecycle !== undefined) {
135
+ if (proposal.senderLeafIndex !== args.committerLeafIndex ||
136
+ !bytesEqual(lifecycle, encodeGroupLifecycleV1("disbanded")))
137
+ return violation("disband lifecycle replacement is not exact and inline");
138
+ lifecycleUpdates++;
139
+ continue;
140
+ }
141
+ const admins = updateBytes(proposal, GROUP_ADMIN_POLICY_COMPONENT_ID);
142
+ if (admins !== undefined) {
143
+ if (proposal.senderLeafIndex !== args.committerLeafIndex ||
144
+ !bytesEqual(admins, encodeAdminPolicyV1([actorPubkey])))
145
+ return violation("disband admin-policy replacement is not exact and inline");
146
+ adminUpdates++;
147
+ continue;
148
+ }
149
+ return violation("disband commit contains a forbidden proposal");
150
+ }
151
+ if (lifecycleUpdates !== 1 || adminUpdates !== 1)
152
+ return violation("disband requires exactly one lifecycle and admin-policy replacement");
153
+ if (removals.length !== expectedRemovals.length ||
154
+ expectedRemovals.some((index) => !removals.includes(index)) ||
155
+ new Set(removals).size !== removals.length)
156
+ return violation("disband must remove every parent leaf except the exact committer leaf");
157
+ try {
158
+ const resultingAdmins = receiverRemoved
159
+ ? [actorPubkey]
160
+ : (getAdminPolicy(args.resultingState.groupContext.extensions) ?? []);
161
+ if (resultingAdmins.length !== 1 ||
162
+ resultingAdmins[0] !== actorPubkey ||
163
+ (!receiverRemoved &&
164
+ (leafIndexes(args.resultingState).length !== 1 ||
165
+ actorOf(args.resultingState, args.committerLeafIndex) !==
166
+ actorPubkey)))
167
+ return violation("disband resulting roster or admin policy is not committer-only");
168
+ }
169
+ catch {
170
+ return violation("resulting disband admin-policy component did not decode");
171
+ }
172
+ return { kind: "validDisband", actorPubkey };
173
+ }
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Codec for `marmot.group.encrypted-media.v1` (`0x8008`) — the group's
3
+ * encrypted-media policy: the media format, the allowed blob locator kinds, and
4
+ * the default blob-store endpoints.
5
+ *
6
+ * Wire (Marmot binary profile):
7
+ * opaque media_format<V>; // must be "encrypted-media-v1"
8
+ * opaque allowed_locator_kinds<V>; // concat of opaque kind<V>
9
+ * opaque default_blob_endpoints<V>; // concat of opaque endpoint<V>
10
+ * // each endpoint = opaque locator_kind<V> ++ opaque base_url<V>
11
+ *
12
+ * Locator kinds are normalized (trim, ASCII-lowercase, `[a-z0-9-]`, ≤64 bytes)
13
+ * and deduped; endpoint URLs are validated + normalized (loopback `http`
14
+ * allowed). At least one allowed kind and one endpoint are required.
15
+ *
16
+ * @see darkmatter `crates/traits/src/app_components.rs` `encode_encrypted_media_policy_v1`
17
+ */
18
+ export declare const ENCRYPTED_MEDIA_FORMAT_V1 = "encrypted-media-v1";
19
+ export declare const BLOSSOM_LOCATOR_KIND_V1 = "blossom-v1";
20
+ export interface BlobStoreEndpointV1 {
21
+ locatorKind: string;
22
+ baseUrl: string;
23
+ }
24
+ export interface EncryptedMediaPolicyV1 {
25
+ mediaFormat: string;
26
+ allowedLocatorKinds: string[];
27
+ defaultBlobEndpoints: BlobStoreEndpointV1[];
28
+ }
29
+ /** Builds the default Blossom-backed policy for the given endpoint base URLs. */
30
+ export declare function encryptedMediaBlossomDefault(baseUrls: string[]): EncryptedMediaPolicyV1;
31
+ /** Encodes an {@link EncryptedMediaPolicyV1} to its component `data` bytes. */
32
+ export declare function encodeEncryptedMediaPolicyV1(policy: EncryptedMediaPolicyV1): Uint8Array;
33
+ /**
34
+ * Decodes `marmot.group.encrypted-media.v1` component `data` bytes strictly.
35
+ *
36
+ * Per darkmatter `decode_encrypted_media_policy_v1` and
37
+ * `foundation/canonical-encoding.md` ("Canonical decoding"), this is a decoder
38
+ * of signed, state-selecting Marmot bytes: it MUST reject input that is not
39
+ * already canonical and MUST NOT trim, case-fold, normalize, deduplicate, or
40
+ * reorder anything. Every check is a validation; a failure throws. Repairing
41
+ * non-canonical state here (as the old producer-`normalizePolicy` reuse did)
42
+ * forks commit acceptance against conformant implementations.
43
+ */
44
+ export declare function decodeEncryptedMediaPolicyV1(data: Uint8Array): EncryptedMediaPolicyV1;