@941design/marmot-engine 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 (196) hide show
  1. package/CHANGELOG.md +434 -0
  2. package/LICENSE +49 -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 +74 -0
  55. package/dist/core/components/ids.js +73 -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 +45 -0
  91. package/dist/core/group.js +78 -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 +28 -0
  95. package/dist/core/index.js +26 -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 +102 -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 +55 -0
  107. package/dist/core/key-package.js +115 -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.d.ts +51 -0
  123. package/dist/core/protocol.js +41 -0
  124. package/dist/core/relay-lists.d.ts +52 -0
  125. package/dist/core/relay-lists.js +86 -0
  126. package/dist/core/retained-history.d.ts +78 -0
  127. package/dist/core/retained-history.js +59 -0
  128. package/dist/core/transport.d.ts +43 -0
  129. package/dist/core/transport.js +21 -0
  130. package/dist/core/welcome-event.d.ts +61 -0
  131. package/dist/core/welcome-event.js +116 -0
  132. package/dist/core/welcome-join.d.ts +155 -0
  133. package/dist/core/welcome-join.js +243 -0
  134. package/dist/core/welcome.d.ts +3 -0
  135. package/dist/core/welcome.js +6 -0
  136. package/dist/engine/admin-policy.d.ts +58 -0
  137. package/dist/engine/admin-policy.js +137 -0
  138. package/dist/engine/auto-committer.d.ts +39 -0
  139. package/dist/engine/auto-committer.js +42 -0
  140. package/dist/engine/delivered-payloads.d.ts +61 -0
  141. package/dist/engine/delivered-payloads.js +84 -0
  142. package/dist/engine/disband-request.d.ts +41 -0
  143. package/dist/engine/disband-request.js +86 -0
  144. package/dist/engine/disband-tombstone.d.ts +14 -0
  145. package/dist/engine/disband-tombstone.js +66 -0
  146. package/dist/engine/fork-recovery.d.ts +672 -0
  147. package/dist/engine/fork-recovery.js +1615 -0
  148. package/dist/engine/group-engine.d.ts +571 -0
  149. package/dist/engine/group-engine.js +5112 -0
  150. package/dist/engine/history-tree.d.ts +232 -0
  151. package/dist/engine/history-tree.js +651 -0
  152. package/dist/engine/index.d.ts +16 -0
  153. package/dist/engine/index.js +15 -0
  154. package/dist/engine/ingest-disposition.d.ts +5 -0
  155. package/dist/engine/ingest-disposition.js +63 -0
  156. package/dist/engine/ingest.d.ts +390 -0
  157. package/dist/engine/ingest.js +1258 -0
  158. package/dist/engine/ingestion-pool.d.ts +74 -0
  159. package/dist/engine/ingestion-pool.js +95 -0
  160. package/dist/engine/message-dedup.d.ts +16 -0
  161. package/dist/engine/message-dedup.js +20 -0
  162. package/dist/engine/own-commit-stamp.d.ts +30 -0
  163. package/dist/engine/own-commit-stamp.js +71 -0
  164. package/dist/engine/retained-store.d.ts +79 -0
  165. package/dist/engine/retained-store.js +133 -0
  166. package/dist/engine/sender-ratchet.d.ts +50 -0
  167. package/dist/engine/sender-ratchet.js +196 -0
  168. package/dist/engine/state-notifications.d.ts +122 -0
  169. package/dist/engine/state-notifications.js +173 -0
  170. package/dist/engine/tree-convergence.d.ts +41 -0
  171. package/dist/engine/tree-convergence.js +90 -0
  172. package/dist/engine/types.d.ts +366 -0
  173. package/dist/engine/types.js +14 -0
  174. package/dist/engine/wire-format.d.ts +77 -0
  175. package/dist/engine/wire-format.js +120 -0
  176. package/dist/index.d.ts +12 -0
  177. package/dist/index.js +1 -0
  178. package/dist/mls.d.ts +11 -0
  179. package/dist/mls.js +11 -0
  180. package/dist/utils/debug.d.ts +3 -0
  181. package/dist/utils/debug.js +3 -0
  182. package/dist/utils/encoding.d.ts +38 -0
  183. package/dist/utils/encoding.js +91 -0
  184. package/dist/utils/key-value.d.ts +18 -0
  185. package/dist/utils/key-value.js +2 -0
  186. package/dist/utils/nostr.d.ts +35 -0
  187. package/dist/utils/nostr.js +26 -0
  188. package/dist/utils/relay-url.d.ts +10 -0
  189. package/dist/utils/relay-url.js +23 -0
  190. package/dist/utils/tag-cardinality.d.ts +50 -0
  191. package/dist/utils/tag-cardinality.js +91 -0
  192. package/dist/utils/timestamp.d.ts +50 -0
  193. package/dist/utils/timestamp.js +95 -0
  194. package/dist/version.d.ts +112 -0
  195. package/dist/version.js +112 -0
  196. package/package.json +68 -3
@@ -0,0 +1,210 @@
1
+ /** @module @category Core - App Components */
2
+ import { BinaryReader, BinaryWriter, decodeUtf8, encodeUtf8, } from "../binary.js";
3
+ import { validateAndNormalizeHttpsUrl } from "./url.js";
4
+ /**
5
+ * Codec for `marmot.group.encrypted-media.v1` (`0x8008`) — the group's
6
+ * encrypted-media policy: the media format, the allowed blob locator kinds, and
7
+ * the default blob-store endpoints.
8
+ *
9
+ * Wire (Marmot binary profile):
10
+ * opaque media_format<V>; // must be "encrypted-media-v1"
11
+ * opaque allowed_locator_kinds<V>; // concat of opaque kind<V>
12
+ * opaque default_blob_endpoints<V>; // concat of opaque endpoint<V>
13
+ * // each endpoint = opaque locator_kind<V> ++ opaque base_url<V>
14
+ *
15
+ * Locator kinds are normalized (trim, ASCII-lowercase, `[a-z0-9-]`, ≤64 bytes)
16
+ * and deduped; endpoint URLs are validated + normalized (loopback `http`
17
+ * allowed). At least one allowed kind and one endpoint are required.
18
+ *
19
+ * @see darkmatter `crates/traits/src/app_components.rs` `encode_encrypted_media_policy_v1`
20
+ */
21
+ export const ENCRYPTED_MEDIA_FORMAT_V1 = "encrypted-media-v1";
22
+ export const BLOSSOM_LOCATOR_KIND_V1 = "blossom-v1";
23
+ const LOCATOR_KIND_MAX_LEN = 64;
24
+ const ENDPOINT_URL_MAX_LEN = 2048;
25
+ const MAX_LOCATOR_KINDS = 16;
26
+ const MAX_BLOB_ENDPOINTS = 16;
27
+ function normalizeLocatorKind(value, label) {
28
+ const kind = value.trim().replace(/[A-Z]/g, (c) => c.toLowerCase());
29
+ if (kind.length === 0)
30
+ throw new Error(`${label} must not be empty`);
31
+ if (encodeUtf8(kind).length > LOCATOR_KIND_MAX_LEN) {
32
+ throw new Error(`${label} exceeds ${LOCATOR_KIND_MAX_LEN} bytes`);
33
+ }
34
+ if (!/^[a-z0-9-]+$/.test(kind)) {
35
+ throw new Error(`${label} must contain only lowercase ASCII letters, digits, and '-'`);
36
+ }
37
+ return kind;
38
+ }
39
+ function normalizeEndpointUrl(raw) {
40
+ return validateAndNormalizeHttpsUrl(raw, {
41
+ maxLen: ENDPOINT_URL_MAX_LEN,
42
+ allowLoopbackHttp: true,
43
+ trimInput: true,
44
+ label: "encrypted media endpoint URL",
45
+ });
46
+ }
47
+ /**
48
+ * Canonical locator-kind rule (group-encrypted-media-v1.md): 1..64 bytes,
49
+ * lowercase ASCII letters, digits, and `-`. Pure validation — no trimming or
50
+ * case-folding — so it can run on the strict decode path (darkmatter
51
+ * `validate_locator_kind`).
52
+ */
53
+ function validateLocatorKind(value, label) {
54
+ if (value.length === 0)
55
+ throw new Error(`${label} must not be empty`);
56
+ if (encodeUtf8(value).length > LOCATOR_KIND_MAX_LEN) {
57
+ throw new Error(`${label} exceeds ${LOCATOR_KIND_MAX_LEN} bytes`);
58
+ }
59
+ if (!/^[a-z0-9-]+$/.test(value)) {
60
+ throw new Error(`${label} must contain only lowercase ASCII letters, digits, and '-'`);
61
+ }
62
+ }
63
+ /**
64
+ * Strict decode-side check that a stored endpoint base URL is already
65
+ * canonical: it validates AND is byte-equal to its own producer-side
66
+ * normalization. A non-normalized URL is rejected, never repaired (darkmatter
67
+ * `validate_blob_endpoint_url_is_canonical`).
68
+ */
69
+ function validateEndpointUrlIsCanonical(baseUrl) {
70
+ if (normalizeEndpointUrl(baseUrl) !== baseUrl) {
71
+ throw new Error("encrypted media endpoint base URL is not normalized");
72
+ }
73
+ }
74
+ /** Normalizes + validates a policy the way the Rust `EncryptedMediaPolicyV1::new` does. */
75
+ function normalizePolicy(policy) {
76
+ const mediaFormat = policy.mediaFormat.trim();
77
+ if (mediaFormat !== ENCRYPTED_MEDIA_FORMAT_V1) {
78
+ throw new Error(`encrypted media format must be ${ENCRYPTED_MEDIA_FORMAT_V1}`);
79
+ }
80
+ const allowed = [];
81
+ for (const kind of policy.allowedLocatorKinds) {
82
+ const normalized = normalizeLocatorKind(kind, "allowed locator kind");
83
+ if (!allowed.includes(normalized))
84
+ allowed.push(normalized);
85
+ }
86
+ if (allowed.length === 0) {
87
+ throw new Error("encrypted media policy must allow at least one locator kind");
88
+ }
89
+ if (allowed.length > MAX_LOCATOR_KINDS) {
90
+ throw new Error(`encrypted media policy allows more than ${MAX_LOCATOR_KINDS} locator kinds`);
91
+ }
92
+ const endpoints = [];
93
+ for (const endpoint of policy.defaultBlobEndpoints) {
94
+ const locatorKind = normalizeLocatorKind(endpoint.locatorKind, "endpoint locator kind");
95
+ if (!allowed.includes(locatorKind)) {
96
+ throw new Error("encrypted media endpoint locator kind is not allowed");
97
+ }
98
+ const baseUrl = normalizeEndpointUrl(endpoint.baseUrl);
99
+ const normalized = { locatorKind, baseUrl };
100
+ if (!endpoints.some((e) => e.locatorKind === locatorKind && e.baseUrl === baseUrl)) {
101
+ endpoints.push(normalized);
102
+ }
103
+ }
104
+ if (endpoints.length === 0) {
105
+ throw new Error("encrypted media policy must include at least one default blob endpoint");
106
+ }
107
+ if (endpoints.length > MAX_BLOB_ENDPOINTS) {
108
+ throw new Error(`encrypted media policy includes more than ${MAX_BLOB_ENDPOINTS} default blob endpoints`);
109
+ }
110
+ return {
111
+ mediaFormat,
112
+ allowedLocatorKinds: allowed,
113
+ defaultBlobEndpoints: endpoints,
114
+ };
115
+ }
116
+ /** Builds the default Blossom-backed policy for the given endpoint base URLs. */
117
+ export function encryptedMediaBlossomDefault(baseUrls) {
118
+ return normalizePolicy({
119
+ mediaFormat: ENCRYPTED_MEDIA_FORMAT_V1,
120
+ allowedLocatorKinds: [BLOSSOM_LOCATOR_KIND_V1],
121
+ defaultBlobEndpoints: baseUrls.map((baseUrl) => ({
122
+ locatorKind: BLOSSOM_LOCATOR_KIND_V1,
123
+ baseUrl,
124
+ })),
125
+ });
126
+ }
127
+ /** Encodes an {@link EncryptedMediaPolicyV1} to its component `data` bytes. */
128
+ export function encodeEncryptedMediaPolicyV1(policy) {
129
+ const normalized = normalizePolicy(policy);
130
+ const allowed = new BinaryWriter();
131
+ for (const kind of normalized.allowedLocatorKinds) {
132
+ allowed.opaque(encodeUtf8(kind));
133
+ }
134
+ // Each endpoint is the bare concatenation opaque(locator_kind) ++
135
+ // opaque(base_url) with NO per-item length wrapper — one outer length for the
136
+ // whole vector, then the concatenated items (darkmatter #171; spec
137
+ // `Type items<V>`). The sibling `allowed_locator_kinds` uses the same shape.
138
+ const endpoints = new BinaryWriter();
139
+ for (const endpoint of normalized.defaultBlobEndpoints) {
140
+ endpoints.opaque(encodeUtf8(endpoint.locatorKind));
141
+ endpoints.opaque(encodeUtf8(endpoint.baseUrl));
142
+ }
143
+ // encode_component_vectors: bare concatenation of opaque(part) for each part.
144
+ return new BinaryWriter()
145
+ .opaque(encodeUtf8(normalized.mediaFormat))
146
+ .opaque(allowed.build())
147
+ .opaque(endpoints.build())
148
+ .build();
149
+ }
150
+ /**
151
+ * Decodes `marmot.group.encrypted-media.v1` component `data` bytes strictly.
152
+ *
153
+ * Per darkmatter `decode_encrypted_media_policy_v1` and
154
+ * `foundation/canonical-encoding.md` ("Canonical decoding"), this is a decoder
155
+ * of signed, state-selecting Marmot bytes: it MUST reject input that is not
156
+ * already canonical and MUST NOT trim, case-fold, normalize, deduplicate, or
157
+ * reorder anything. Every check is a validation; a failure throws. Repairing
158
+ * non-canonical state here (as the old producer-`normalizePolicy` reuse did)
159
+ * forks commit acceptance against conformant implementations.
160
+ */
161
+ export function decodeEncryptedMediaPolicyV1(data) {
162
+ const reader = new BinaryReader(data);
163
+ const mediaFormat = decodeUtf8(reader.opaque());
164
+ const allowedBytes = reader.opaque();
165
+ const endpointsBytes = reader.opaque();
166
+ reader.end();
167
+ if (mediaFormat !== ENCRYPTED_MEDIA_FORMAT_V1) {
168
+ throw new Error(`encrypted media format must be ${ENCRYPTED_MEDIA_FORMAT_V1}`);
169
+ }
170
+ const allowedLocatorKinds = [];
171
+ const allowedReader = new BinaryReader(allowedBytes);
172
+ while (allowedReader.hasMore()) {
173
+ const kind = decodeUtf8(allowedReader.opaque());
174
+ validateLocatorKind(kind, "allowed locator kind");
175
+ if (allowedLocatorKinds.includes(kind)) {
176
+ throw new Error("encrypted media policy has a duplicate allowed locator kind");
177
+ }
178
+ allowedLocatorKinds.push(kind);
179
+ }
180
+ if (allowedLocatorKinds.length === 0) {
181
+ throw new Error("encrypted media policy must allow at least one locator kind");
182
+ }
183
+ if (allowedLocatorKinds.length > MAX_LOCATOR_KINDS) {
184
+ throw new Error(`encrypted media policy allows more than ${MAX_LOCATOR_KINDS} locator kinds`);
185
+ }
186
+ // Endpoints: bare concatenation of opaque(locator_kind) ++ opaque(base_url),
187
+ // no per-item length wrapper (darkmatter #171).
188
+ const defaultBlobEndpoints = [];
189
+ const endpointsReader = new BinaryReader(endpointsBytes);
190
+ while (endpointsReader.hasMore()) {
191
+ const locatorKind = decodeUtf8(endpointsReader.opaque());
192
+ const baseUrl = decodeUtf8(endpointsReader.opaque());
193
+ validateLocatorKind(locatorKind, "endpoint locator kind");
194
+ if (!allowedLocatorKinds.includes(locatorKind)) {
195
+ throw new Error("encrypted media endpoint locator kind is not allowed");
196
+ }
197
+ validateEndpointUrlIsCanonical(baseUrl);
198
+ if (defaultBlobEndpoints.some((e) => e.locatorKind === locatorKind && e.baseUrl === baseUrl)) {
199
+ throw new Error("encrypted media policy has a duplicate default blob endpoint");
200
+ }
201
+ defaultBlobEndpoints.push({ locatorKind, baseUrl });
202
+ }
203
+ if (defaultBlobEndpoints.length === 0) {
204
+ throw new Error("encrypted media policy must include at least one default blob endpoint");
205
+ }
206
+ if (defaultBlobEndpoints.length > MAX_BLOB_ENDPOINTS) {
207
+ throw new Error(`encrypted media policy includes more than ${MAX_BLOB_ENDPOINTS} default blob endpoints`);
208
+ }
209
+ return { mediaFormat, allowedLocatorKinds, defaultBlobEndpoints };
210
+ }
@@ -0,0 +1,10 @@
1
+ /** Protocol lifecycle values carried by `marmot.group.lifecycle.v1`. */
2
+ export declare const groupProtocolLifecycleValues: {
3
+ readonly active: "active";
4
+ readonly disbanded: "disbanded";
5
+ };
6
+ export type GroupProtocolLifecycleValue = (typeof groupProtocolLifecycleValues)[keyof typeof groupProtocolLifecycleValues];
7
+ /** Encodes the lifecycle state as its exact one-byte wire value. */
8
+ export declare function encodeGroupLifecycleV1(value: GroupProtocolLifecycleValue): Uint8Array;
9
+ /** Decodes an exact one-byte `marmot.group.lifecycle.v1` value. */
10
+ export declare function decodeGroupLifecycleV1(data: Uint8Array): GroupProtocolLifecycleValue;
@@ -0,0 +1,32 @@
1
+ /** @module @category Core - App Components */
2
+ import { BinaryReader, BinaryWriter } from "../binary.js";
3
+ /** Protocol lifecycle values carried by `marmot.group.lifecycle.v1`. */
4
+ export const groupProtocolLifecycleValues = {
5
+ active: "active",
6
+ disbanded: "disbanded",
7
+ };
8
+ /** Encodes the lifecycle state as its exact one-byte wire value. */
9
+ export function encodeGroupLifecycleV1(value) {
10
+ const encoded = value === groupProtocolLifecycleValues.active
11
+ ? 0
12
+ : value === groupProtocolLifecycleValues.disbanded
13
+ ? 1
14
+ : undefined;
15
+ if (encoded === undefined)
16
+ throw new Error("unknown group lifecycle value");
17
+ return new BinaryWriter().uint8(encoded).build();
18
+ }
19
+ /** Decodes an exact one-byte `marmot.group.lifecycle.v1` value. */
20
+ export function decodeGroupLifecycleV1(data) {
21
+ const reader = new BinaryReader(data);
22
+ const encoded = reader.uint8();
23
+ reader.end();
24
+ switch (encoded) {
25
+ case 0:
26
+ return groupProtocolLifecycleValues.active;
27
+ case 1:
28
+ return groupProtocolLifecycleValues.disbanded;
29
+ default:
30
+ throw new Error(`unknown group lifecycle value: ${encoded}`);
31
+ }
32
+ }
@@ -0,0 +1,24 @@
1
+ /**
2
+ * Codec for `marmot.group.profile.v1` (`0x8001`) — the group's display name and
3
+ * description. Replaces the `name`/`description` fields of the legacy
4
+ * `marmot_group_data` monolith.
5
+ *
6
+ * Wire (Marmot binary profile, `encode_component_vectors([name, description])`):
7
+ * opaque name<0..256>; // QUIC-varint length + UTF-8 bytes
8
+ * opaque description<0..4096>; // QUIC-varint length + UTF-8 bytes
9
+ *
10
+ * @see darkmatter `crates/cgka-engine/src/app_components.rs` `encode_group_profile`
11
+ * @see Marmot v2 spec: `app-components/group-profile-v1.md`
12
+ */
13
+ /** Maximum UTF-8 byte length of the group name. */
14
+ export declare const GROUP_NAME_MAX_BYTES = 256;
15
+ /** Maximum UTF-8 byte length of the group description. */
16
+ export declare const GROUP_DESCRIPTION_MAX_BYTES = 4096;
17
+ export interface GroupProfileV1 {
18
+ name: string;
19
+ description: string;
20
+ }
21
+ /** Encodes a {@link GroupProfileV1} to its component `data` bytes. */
22
+ export declare function encodeGroupProfileV1(profile: GroupProfileV1): Uint8Array;
23
+ /** Decodes `marmot.group.profile.v1` component `data` bytes. */
24
+ export declare function decodeGroupProfileV1(data: Uint8Array): GroupProfileV1;
@@ -0,0 +1,35 @@
1
+ /** @module @category Core - App Components */
2
+ import { BinaryReader, BinaryWriter, decodeUtf8, encodeUtf8, } from "../binary.js";
3
+ /**
4
+ * Codec for `marmot.group.profile.v1` (`0x8001`) — the group's display name and
5
+ * description. Replaces the `name`/`description` fields of the legacy
6
+ * `marmot_group_data` monolith.
7
+ *
8
+ * Wire (Marmot binary profile, `encode_component_vectors([name, description])`):
9
+ * opaque name<0..256>; // QUIC-varint length + UTF-8 bytes
10
+ * opaque description<0..4096>; // QUIC-varint length + UTF-8 bytes
11
+ *
12
+ * @see darkmatter `crates/cgka-engine/src/app_components.rs` `encode_group_profile`
13
+ * @see Marmot v2 spec: `app-components/group-profile-v1.md`
14
+ */
15
+ /** Maximum UTF-8 byte length of the group name. */
16
+ export const GROUP_NAME_MAX_BYTES = 256;
17
+ /** Maximum UTF-8 byte length of the group description. */
18
+ export const GROUP_DESCRIPTION_MAX_BYTES = 4096;
19
+ /** Encodes a {@link GroupProfileV1} to its component `data` bytes. */
20
+ export function encodeGroupProfileV1(profile) {
21
+ const name = encodeUtf8(profile.name);
22
+ const description = encodeUtf8(profile.description);
23
+ return new BinaryWriter()
24
+ .opaque(name, { max: GROUP_NAME_MAX_BYTES })
25
+ .opaque(description, { max: GROUP_DESCRIPTION_MAX_BYTES })
26
+ .build();
27
+ }
28
+ /** Decodes `marmot.group.profile.v1` component `data` bytes. */
29
+ export function decodeGroupProfileV1(data) {
30
+ const reader = new BinaryReader(data);
31
+ const name = decodeUtf8(reader.opaque({ max: GROUP_NAME_MAX_BYTES }));
32
+ const description = decodeUtf8(reader.opaque({ max: GROUP_DESCRIPTION_MAX_BYTES }));
33
+ reader.end();
34
+ return { name, description };
35
+ }
@@ -0,0 +1,9 @@
1
+ /** @module @category Core - App Components */
2
+ /** Returns `true` when `hostname` resolves to a loopback IP or localhost domain. */
3
+ export declare function isLoopbackHost(hostname: string): boolean;
4
+ /**
5
+ * Throws (with the given `label` prefix) when `hostname` points at a
6
+ * non-routable address or at localhost. Mirrors the darkmatter host-safety
7
+ * rejection performed inside `validate_and_normalize_*`.
8
+ */
9
+ export declare function rejectNonRoutableHost(hostname: string, label: string): void;
@@ -0,0 +1,148 @@
1
+ /** @module @category Core - App Components */
2
+ // A browser-forced reimplementation of the Rust `std::net` address checks used
3
+ // by darkmatter `host_safety.rs`. Security-critical: these decide whether a
4
+ // routing/media URL points at a non-routable (loopback/private/etc.) host.
5
+ /**
6
+ * Ports the darkmatter `reject_non_routable_ipv4` classifier. Returns `true`
7
+ * when the dotted-decimal IPv4 address is loopback, private, link-local,
8
+ * broadcast, documentation, unspecified, or multicast.
9
+ */
10
+ function isNonRoutableIpv4(octets) {
11
+ const [a, b, c, d] = octets;
12
+ const loopback = a === 127;
13
+ const priv = a === 10 || (a === 172 && b >= 16 && b <= 31) || (a === 192 && b === 168);
14
+ const linkLocal = a === 169 && b === 254;
15
+ const broadcast = a === 255 && b === 255 && c === 255 && d === 255;
16
+ const documentation = (a === 192 && b === 0 && c === 2) ||
17
+ (a === 198 && b === 51 && c === 100) ||
18
+ (a === 203 && b === 0 && c === 113);
19
+ const unspecified = a === 0 && b === 0 && c === 0 && d === 0;
20
+ const multicast = a >= 224 && a <= 239;
21
+ return (loopback ||
22
+ priv ||
23
+ linkLocal ||
24
+ broadcast ||
25
+ documentation ||
26
+ unspecified ||
27
+ multicast);
28
+ }
29
+ /** Parses a dotted-decimal IPv4 string into octets, or `null` if not IPv4. */
30
+ function parseIpv4(host) {
31
+ const parts = host.split(".");
32
+ if (parts.length !== 4)
33
+ return null;
34
+ const octets = [];
35
+ for (const part of parts) {
36
+ if (!/^\d{1,3}$/.test(part))
37
+ return null;
38
+ const n = Number(part);
39
+ if (n > 255)
40
+ return null;
41
+ octets.push(n);
42
+ }
43
+ return octets;
44
+ }
45
+ /** Expands an IPv6 string (with optional `::`) into eight 16-bit segments. */
46
+ function parseIpv6(host) {
47
+ // An embedded IPv4 tail (e.g. ::ffff:1.2.3.4) folds into two segments.
48
+ let text = host;
49
+ const tail = text.match(/(\d{1,3}(?:\.\d{1,3}){3})$/);
50
+ let tailSegments = [];
51
+ if (tail) {
52
+ const v4 = parseIpv4(tail[1]);
53
+ if (!v4)
54
+ return null;
55
+ tailSegments = [(v4[0] << 8) | v4[1], (v4[2] << 8) | v4[3]];
56
+ text = text.slice(0, tail.index);
57
+ if (text.endsWith(":") && !text.endsWith("::"))
58
+ text = text.slice(0, -1);
59
+ }
60
+ const halves = text.split("::");
61
+ if (halves.length > 2)
62
+ return null;
63
+ const parseGroups = (s) => {
64
+ if (s === "")
65
+ return [];
66
+ const out = [];
67
+ for (const g of s.split(":")) {
68
+ if (!/^[0-9a-fA-F]{1,4}$/.test(g))
69
+ return null;
70
+ out.push(parseInt(g, 16));
71
+ }
72
+ return out;
73
+ };
74
+ if (halves.length === 2) {
75
+ const head = parseGroups(halves[0]);
76
+ const back = parseGroups(halves[1]);
77
+ if (head === null || back === null)
78
+ return null;
79
+ const filled = [...head, ...back, ...tailSegments];
80
+ const missing = 8 - filled.length;
81
+ if (missing < 0)
82
+ return null;
83
+ return [...head, ...Array(missing).fill(0), ...back, ...tailSegments];
84
+ }
85
+ const groups = parseGroups(text);
86
+ if (groups === null)
87
+ return null;
88
+ const segments = [...groups, ...tailSegments];
89
+ return segments.length === 8 ? segments : null;
90
+ }
91
+ /** Ports the darkmatter `reject_non_routable_ipv6` classifier. */
92
+ function isNonRoutableIpv6(segments) {
93
+ // ::ffff:0:0/96 — IPv4-mapped — re-classify as IPv4.
94
+ const mappedPrefix = segments.slice(0, 5).every((s) => s === 0);
95
+ if (mappedPrefix && segments[5] === 0xffff) {
96
+ const a = segments[6] >> 8;
97
+ const b = segments[6] & 0xff;
98
+ const c = segments[7] >> 8;
99
+ const d = segments[7] & 0xff;
100
+ return isNonRoutableIpv4([a, b, c, d]);
101
+ }
102
+ const loopback = segments.slice(0, 7).every((s) => s === 0) && segments[7] === 1;
103
+ const unspecified = segments.every((s) => s === 0);
104
+ const multicast = (segments[0] & 0xff00) === 0xff00;
105
+ const uniqueLocal = (segments[0] & 0xfe00) === 0xfc00;
106
+ const linkLocal = (segments[0] & 0xffc0) === 0xfe80;
107
+ return loopback || unspecified || multicast || uniqueLocal || linkLocal;
108
+ }
109
+ function isLocalhostDomain(hostname) {
110
+ const lowered = hostname.toLowerCase();
111
+ return lowered === "localhost" || lowered.endsWith(".localhost");
112
+ }
113
+ /** Returns `true` when `hostname` resolves to a loopback IP or localhost domain. */
114
+ export function isLoopbackHost(hostname) {
115
+ const host = hostname.replace(/^\[|\]$/g, "");
116
+ const v4 = parseIpv4(host);
117
+ if (v4)
118
+ return v4[0] === 127;
119
+ if (host.includes(":")) {
120
+ const v6 = parseIpv6(host);
121
+ if (v6)
122
+ return v6.slice(0, 7).every((s) => s === 0) && v6[7] === 1;
123
+ return false;
124
+ }
125
+ return isLocalhostDomain(host);
126
+ }
127
+ /**
128
+ * Throws (with the given `label` prefix) when `hostname` points at a
129
+ * non-routable address or at localhost. Mirrors the darkmatter host-safety
130
+ * rejection performed inside `validate_and_normalize_*`.
131
+ */
132
+ export function rejectNonRoutableHost(hostname, label) {
133
+ const host = hostname.replace(/^\[|\]$/g, "");
134
+ const v4 = parseIpv4(host);
135
+ if (v4) {
136
+ if (isNonRoutableIpv4(v4))
137
+ throw new Error(`${label} must not point at a non-routable address`);
138
+ return;
139
+ }
140
+ if (host.includes(":")) {
141
+ const v6 = parseIpv6(host);
142
+ if (v6 && isNonRoutableIpv6(v6))
143
+ throw new Error(`${label} must not point at a non-routable address`);
144
+ return;
145
+ }
146
+ if (isLocalhostDomain(host))
147
+ throw new Error(`${label} must not point at localhost`);
148
+ }
@@ -0,0 +1,74 @@
1
+ /** @module @category Core - App Components */
2
+ /**
3
+ * Marmot MLS app-component identifiers (darkmatter / Marmot v2).
4
+ *
5
+ * Group state is carried as versioned app components inside the MLS
6
+ * `app_data_dictionary` extension. Each component owns the opaque `data` bytes
7
+ * stored under its {@link AppComponentId}. These ids are wire-significant and
8
+ * MUST match the darkmatter reference implementation for cross-implementation
9
+ * interop.
10
+ *
11
+ * @see darkmatter `crates/traits/src/app_components.rs`
12
+ * @see Marmot v2 spec: `app-components/README.md`, `foundation/registries.md`
13
+ */
14
+ /** An MLS `ComponentID` (`uint16`). */
15
+ export type AppComponentId = number;
16
+ /**
17
+ * Upstream MLS extensions-draft component (`0x0001`) that advertises the
18
+ * supported/required application component ids in an `AppDataDictionary` entry.
19
+ */
20
+ export declare const APP_COMPONENTS_COMPONENT_ID: AppComponentId;
21
+ /**
22
+ * Upstream MLS extensions-draft component that advertises SafeAAD support on
23
+ * LeafNodes. Marmot does not yet admit SafeAAD-framed group-component state.
24
+ */
25
+ export declare const SAFE_AAD_COMPONENT_ID: AppComponentId;
26
+ /**
27
+ * Marmot's KeyPackage-level "last resort" marker component (conformance
28
+ * patch, AC-VENDOR-8). Ported from the legacy standalone MLS `last_resort`
29
+ * extension (upstream's constant for it) to a component of the
30
+ * KeyPackage-level `app_data_dictionary`: presence of this component (with
31
+ * empty `data`) marks the KeyPackage reusable, matching
32
+ * draft-ietf-mls-extensions-09's component model rather than a standalone
33
+ * MLS extension. The old extension's numeric code point is now used only by
34
+ * the `self_remove` MLS proposal type (a different code-point namespace).
35
+ */
36
+ export declare const LAST_RESORT_COMPONENT_ID: AppComponentId;
37
+ /** Marmot private component ids live in the `0x8000..0xffff` range. */
38
+ export declare const GROUP_PROFILE_COMPONENT_ID: AppComponentId;
39
+ export declare const GROUP_BLOSSOM_IMAGE_COMPONENT_ID: AppComponentId;
40
+ export declare const GROUP_ADMIN_POLICY_COMPONENT_ID: AppComponentId;
41
+ export declare const NOSTR_ROUTING_COMPONENT_ID: AppComponentId;
42
+ export declare const GROUP_MESSAGE_RETENTION_COMPONENT_ID: AppComponentId;
43
+ export declare const AGENT_TEXT_STREAM_QUIC_COMPONENT_ID: AppComponentId;
44
+ export declare const GROUP_AVATAR_URL_COMPONENT_ID: AppComponentId;
45
+ export declare const GROUP_ENCRYPTED_MEDIA_COMPONENT_ID: AppComponentId;
46
+ export declare const GROUP_LIFECYCLE_COMPONENT_ID: AppComponentId;
47
+ /** Human-readable component names (the `v1` suffix is part of the name). */
48
+ export declare const GROUP_PROFILE_COMPONENT = "marmot.group.profile.v1";
49
+ export declare const GROUP_BLOSSOM_IMAGE_COMPONENT = "marmot.group.blossom.image.v1";
50
+ export declare const GROUP_ADMIN_POLICY_COMPONENT = "marmot.group.admin-policy.v1";
51
+ export declare const NOSTR_ROUTING_COMPONENT = "marmot.transport.nostr.routing.v1";
52
+ export declare const GROUP_MESSAGE_RETENTION_COMPONENT = "marmot.group.message-retention.v1";
53
+ export declare const AGENT_TEXT_STREAM_QUIC_COMPONENT = "marmot.group.agent-text-stream.quic.v1";
54
+ export declare const GROUP_AVATAR_URL_COMPONENT = "marmot.group.avatar-url.v1";
55
+ export declare const GROUP_ENCRYPTED_MEDIA_COMPONENT = "marmot.group.encrypted-media.v1";
56
+ export declare const GROUP_LIFECYCLE_COMPONENT = "marmot.group.lifecycle.v1";
57
+ /**
58
+ * Default group component ids provisioned for a new Marmot group, matching the
59
+ * darkmatter `default_group_components()` set (profile + admin-policy only;
60
+ * nostr routing is added by the transport layer, not the default group state).
61
+ */
62
+ export declare const DEFAULT_GROUP_COMPONENT_IDS: readonly AppComponentId[];
63
+ /**
64
+ * Component ids this implementation can encode/decode and therefore advertises
65
+ * support for in the `app_components` list carried on a key package's LeafNode.
66
+ * A group may only require components every member's leaf advertises here.
67
+ *
68
+ * This is a superset of the darkmatter reference app's supported set
69
+ * (`{0x8001, 0x8003, 0x8004, 0x8006, 0x8008}`); the negotiated required set for
70
+ * any group is the intersection across members, so advertising extra supported
71
+ * components is safe. Excludes `group.blossom.image` (`0x8002`), which has no
72
+ * wire codec, and the `app_components` list id (`0x0001`) itself.
73
+ */
74
+ export declare const SUPPORTED_APP_COMPONENT_IDS: readonly AppComponentId[];
@@ -0,0 +1,73 @@
1
+ /** @module @category Core - App Components */
2
+ /**
3
+ * Upstream MLS extensions-draft component (`0x0001`) that advertises the
4
+ * supported/required application component ids in an `AppDataDictionary` entry.
5
+ */
6
+ export const APP_COMPONENTS_COMPONENT_ID = 0x0001;
7
+ /**
8
+ * Upstream MLS extensions-draft component that advertises SafeAAD support on
9
+ * LeafNodes. Marmot does not yet admit SafeAAD-framed group-component state.
10
+ */
11
+ export const SAFE_AAD_COMPONENT_ID = 0x0002;
12
+ /**
13
+ * Marmot's KeyPackage-level "last resort" marker component (conformance
14
+ * patch, AC-VENDOR-8). Ported from the legacy standalone MLS `last_resort`
15
+ * extension (upstream's constant for it) to a component of the
16
+ * KeyPackage-level `app_data_dictionary`: presence of this component (with
17
+ * empty `data`) marks the KeyPackage reusable, matching
18
+ * draft-ietf-mls-extensions-09's component model rather than a standalone
19
+ * MLS extension. The old extension's numeric code point is now used only by
20
+ * the `self_remove` MLS proposal type (a different code-point namespace).
21
+ */
22
+ export const LAST_RESORT_COMPONENT_ID = 0x0004;
23
+ /** Marmot private component ids live in the `0x8000..0xffff` range. */
24
+ export const GROUP_PROFILE_COMPONENT_ID = 0x8001;
25
+ export const GROUP_BLOSSOM_IMAGE_COMPONENT_ID = 0x8002;
26
+ export const GROUP_ADMIN_POLICY_COMPONENT_ID = 0x8003;
27
+ export const NOSTR_ROUTING_COMPONENT_ID = 0x8004;
28
+ export const GROUP_MESSAGE_RETENTION_COMPONENT_ID = 0x8005;
29
+ export const AGENT_TEXT_STREAM_QUIC_COMPONENT_ID = 0x8006;
30
+ export const GROUP_AVATAR_URL_COMPONENT_ID = 0x8007;
31
+ export const GROUP_ENCRYPTED_MEDIA_COMPONENT_ID = 0x8008;
32
+ export const GROUP_LIFECYCLE_COMPONENT_ID = 0x800c;
33
+ /** Human-readable component names (the `v1` suffix is part of the name). */
34
+ export const GROUP_PROFILE_COMPONENT = "marmot.group.profile.v1";
35
+ export const GROUP_BLOSSOM_IMAGE_COMPONENT = "marmot.group.blossom.image.v1";
36
+ export const GROUP_ADMIN_POLICY_COMPONENT = "marmot.group.admin-policy.v1";
37
+ export const NOSTR_ROUTING_COMPONENT = "marmot.transport.nostr.routing.v1";
38
+ export const GROUP_MESSAGE_RETENTION_COMPONENT = "marmot.group.message-retention.v1";
39
+ export const AGENT_TEXT_STREAM_QUIC_COMPONENT = "marmot.group.agent-text-stream.quic.v1";
40
+ export const GROUP_AVATAR_URL_COMPONENT = "marmot.group.avatar-url.v1";
41
+ export const GROUP_ENCRYPTED_MEDIA_COMPONENT = "marmot.group.encrypted-media.v1";
42
+ export const GROUP_LIFECYCLE_COMPONENT = "marmot.group.lifecycle.v1";
43
+ /**
44
+ * Default group component ids provisioned for a new Marmot group, matching the
45
+ * darkmatter `default_group_components()` set (profile + admin-policy only;
46
+ * nostr routing is added by the transport layer, not the default group state).
47
+ */
48
+ export const DEFAULT_GROUP_COMPONENT_IDS = [
49
+ GROUP_PROFILE_COMPONENT_ID,
50
+ GROUP_ADMIN_POLICY_COMPONENT_ID,
51
+ GROUP_LIFECYCLE_COMPONENT_ID,
52
+ ];
53
+ /**
54
+ * Component ids this implementation can encode/decode and therefore advertises
55
+ * support for in the `app_components` list carried on a key package's LeafNode.
56
+ * A group may only require components every member's leaf advertises here.
57
+ *
58
+ * This is a superset of the darkmatter reference app's supported set
59
+ * (`{0x8001, 0x8003, 0x8004, 0x8006, 0x8008}`); the negotiated required set for
60
+ * any group is the intersection across members, so advertising extra supported
61
+ * components is safe. Excludes `group.blossom.image` (`0x8002`), which has no
62
+ * wire codec, and the `app_components` list id (`0x0001`) itself.
63
+ */
64
+ export const SUPPORTED_APP_COMPONENT_IDS = [
65
+ GROUP_PROFILE_COMPONENT_ID,
66
+ GROUP_ADMIN_POLICY_COMPONENT_ID,
67
+ NOSTR_ROUTING_COMPONENT_ID,
68
+ GROUP_MESSAGE_RETENTION_COMPONENT_ID,
69
+ AGENT_TEXT_STREAM_QUIC_COMPONENT_ID,
70
+ GROUP_AVATAR_URL_COMPONENT_ID,
71
+ GROUP_ENCRYPTED_MEDIA_COMPONENT_ID,
72
+ GROUP_LIFECYCLE_COMPONENT_ID,
73
+ ];
@@ -0,0 +1,14 @@
1
+ /** @module @category Core - App Components */
2
+ export * from "./ids.js";
3
+ export * from "./app-components-list.js";
4
+ export * from "./group-profile.js";
5
+ export * from "./admin-policy.js";
6
+ export * from "./nostr-routing.js";
7
+ export * from "./message-retention.js";
8
+ export * from "./avatar-url.js";
9
+ export * from "./encrypted-media.js";
10
+ export * from "./agent-text-stream.js";
11
+ export * from "./group-lifecycle.js";
12
+ export * from "./disband-validation.js";
13
+ export * from "./dictionary.js";
14
+ export * from "./integrity.js";
@@ -0,0 +1,14 @@
1
+ /** @module @category Core - App Components */
2
+ export * from "./ids.js";
3
+ export * from "./app-components-list.js";
4
+ export * from "./group-profile.js";
5
+ export * from "./admin-policy.js";
6
+ export * from "./nostr-routing.js";
7
+ export * from "./message-retention.js";
8
+ export * from "./avatar-url.js";
9
+ export * from "./encrypted-media.js";
10
+ export * from "./agent-text-stream.js";
11
+ export * from "./group-lifecycle.js";
12
+ export * from "./disband-validation.js";
13
+ export * from "./dictionary.js";
14
+ export * from "./integrity.js";