@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,103 @@
1
+ /** @module @category Audit */
2
+ import { sha256 } from "@noble/hashes/sha2.js";
3
+ import { bytesToHex, utf8ToBytes } from "@noble/hashes/utils.js";
4
+ import { MARMOT_AUDIT_SCHEMA_VERSION } from "./types.js";
5
+ export function auditNowMs() {
6
+ return Date.now();
7
+ }
8
+ export function digestBytes(bytes) {
9
+ return bytesToHex(sha256(bytes));
10
+ }
11
+ export function digestString(value) {
12
+ return digestBytes(utf8ToBytes(value));
13
+ }
14
+ export function toAuditBytes(value) {
15
+ return typeof value === "string" ? utf8ToBytes(value) : value;
16
+ }
17
+ function taggedRef(tag, value) {
18
+ const bytes = toAuditBytes(value);
19
+ const input = new Uint8Array(tag.length + bytes.length);
20
+ input.set(utf8ToBytes(tag));
21
+ input.set(bytes, tag.length);
22
+ return bytesToHex(sha256(input).slice(0, 16));
23
+ }
24
+ export function deriveAccountRef(accountId) {
25
+ return taggedRef("marmot-audit-account-ref/v1", accountId);
26
+ }
27
+ export function deriveMemberRef(memberIdentity) {
28
+ return taggedRef("marmot-audit-member-ref/v1", memberIdentity);
29
+ }
30
+ export function deriveEngineId(accountId, deviceId) {
31
+ const account = toAuditBytes(accountId);
32
+ const device = toAuditBytes(deviceId);
33
+ const tag = utf8ToBytes("marmot-audit-engine-id/v2");
34
+ const input = new Uint8Array(tag.length + account.length + device.length);
35
+ input.set(tag);
36
+ input.set(account, tag.length);
37
+ input.set(device, tag.length + account.length);
38
+ return bytesToHex(sha256(input).slice(0, 16));
39
+ }
40
+ export function auditEpochStateName(value) {
41
+ switch (value) {
42
+ case "Stable":
43
+ return "stable";
44
+ case "PendingPublish":
45
+ return "pending_publish";
46
+ case "Merging":
47
+ return "merging";
48
+ case "Recovering":
49
+ return "recovering";
50
+ case "Unrecoverable":
51
+ return "unrecoverable";
52
+ case "Disbanded":
53
+ return "disbanded";
54
+ default:
55
+ throw new Error(`Unknown Marmot group lifecycle state: ${value}`);
56
+ }
57
+ }
58
+ export function messageArtifactKindFromNostrKind(kind) {
59
+ switch (kind) {
60
+ case 444:
61
+ return "welcome";
62
+ case 445:
63
+ return "unknown";
64
+ default:
65
+ return "unknown";
66
+ }
67
+ }
68
+ export function createAuditEvent(context, seq, kind, options) {
69
+ const eventContext = mergeAuditContexts(context.source ? { source: context.source } : undefined, options?.context);
70
+ return {
71
+ schema_version: MARMOT_AUDIT_SCHEMA_VERSION,
72
+ seq,
73
+ wall_time_ms: context.now(),
74
+ recorder_session_id: context.recorderSessionId,
75
+ audit_data_mode: context.dataMode,
76
+ account_ref: context.accountRef,
77
+ engine_id: context.engineId,
78
+ group_ref: options?.groupRef,
79
+ context: eventContext,
80
+ kind,
81
+ };
82
+ }
83
+ export function mergeAuditContexts(a, b) {
84
+ if (!a)
85
+ return b;
86
+ if (!b)
87
+ return a;
88
+ return {
89
+ ...a,
90
+ ...b,
91
+ human_action: b.human_action ?? a.human_action,
92
+ transport: b.transport ?? a.transport,
93
+ engine: b.engine ?? a.engine,
94
+ group: b.group ?? a.group,
95
+ convergence: b.convergence ?? a.convergence,
96
+ source: b.source ?? a.source,
97
+ };
98
+ }
99
+ export function errorDetail(error) {
100
+ if (error instanceof Error)
101
+ return error.message;
102
+ return String(error);
103
+ }
@@ -0,0 +1,6 @@
1
+ /** @module @category Audit */
2
+ export * from "./types.js";
3
+ export * from "./helpers.js";
4
+ export * from "./sink.js";
5
+ export * from "./emitter.js";
6
+ export * from "./recorder.js";
@@ -0,0 +1,6 @@
1
+ /** @module @category Audit */
2
+ export * from "./types.js";
3
+ export * from "./helpers.js";
4
+ export * from "./sink.js";
5
+ export * from "./emitter.js";
6
+ export * from "./recorder.js";
@@ -0,0 +1,13 @@
1
+ /** @module @category Audit */
2
+ import type { AuditContextOptions, AuditLogWriter, AuditRecorder, MarmotAuditEvent } from "./types-internal.js";
3
+ export type { AuditLogWriter } from "./types-internal.js";
4
+ export declare class JsonlAuditRecorder implements AuditRecorder {
5
+ #private;
6
+ readonly writer: AuditLogWriter;
7
+ constructor(options: Partial<AuditContextOptions> & {
8
+ writer: AuditLogWriter;
9
+ });
10
+ record(event: MarmotAuditEvent): void;
11
+ flush(): Promise<void>;
12
+ close(): Promise<void>;
13
+ }
@@ -0,0 +1,24 @@
1
+ export class JsonlAuditRecorder {
2
+ writer;
3
+ #writeChain = Promise.resolve();
4
+ #closed = false;
5
+ constructor(options) {
6
+ this.writer = options.writer;
7
+ }
8
+ record(event) {
9
+ if (this.#closed)
10
+ return;
11
+ this.#writeChain = this.#writeChain.then(() => Promise.resolve(this.writer.appendLine(`${JSON.stringify(event)}\n`)));
12
+ }
13
+ async flush() {
14
+ await this.#writeChain;
15
+ await this.writer.flush?.();
16
+ }
17
+ async close() {
18
+ if (this.#closed)
19
+ return;
20
+ this.#closed = true;
21
+ await this.flush();
22
+ await this.writer.close?.();
23
+ }
24
+ }
@@ -0,0 +1,16 @@
1
+ /** @module @category Audit */
2
+ import type { AuditSink, MarmotAuditEvent } from "./types.js";
3
+ export declare class NoopAuditSink implements AuditSink {
4
+ record(_event: MarmotAuditEvent): void;
5
+ }
6
+ export declare class MemoryAuditSink implements AuditSink {
7
+ readonly events: MarmotAuditEvent[];
8
+ record(event: MarmotAuditEvent): void;
9
+ }
10
+ export declare class SafeAuditSink implements AuditSink {
11
+ readonly inner: AuditSink;
12
+ constructor(inner: AuditSink);
13
+ record(event: MarmotAuditEvent): void;
14
+ }
15
+ export declare const noopAuditSink: NoopAuditSink;
16
+ export declare function safeAuditSink(sink: AuditSink | undefined): AuditSink;
@@ -0,0 +1,27 @@
1
+ export class NoopAuditSink {
2
+ record(_event) { }
3
+ }
4
+ export class MemoryAuditSink {
5
+ events = [];
6
+ record(event) {
7
+ this.events.push(event);
8
+ }
9
+ }
10
+ export class SafeAuditSink {
11
+ inner;
12
+ constructor(inner) {
13
+ this.inner = inner;
14
+ }
15
+ record(event) {
16
+ try {
17
+ this.inner.record(event);
18
+ }
19
+ catch {
20
+ // Audit must never affect protocol progress.
21
+ }
22
+ }
23
+ }
24
+ export const noopAuditSink = new NoopAuditSink();
25
+ export function safeAuditSink(sink) {
26
+ return sink ? new SafeAuditSink(sink) : noopAuditSink;
27
+ }
@@ -0,0 +1,7 @@
1
+ /** @module @category Audit */
2
+ export type { AuditContextOptions, AuditEmitContext, AuditEventKind, AuditRecorder, MarmotAuditEvent, } from "./types.js";
3
+ export interface AuditLogWriter {
4
+ appendLine(line: string): void | Promise<void>;
5
+ flush?(): void | Promise<void>;
6
+ close?(): void | Promise<void>;
7
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,289 @@
1
+ /** @module @category Audit */
2
+ export declare const MARMOT_AUDIT_SCHEMA_VERSION = "marmot-forensics-audit/v2";
3
+ export type AuditDataMode = "obfuscated_sensitive_data" | "full_data";
4
+ export type AuditEpochState = "stable" | "pending_publish" | "merging" | "recovering" | "unrecoverable" | "disbanded";
5
+ export type AuditMessageArtifactKind = "application_message" | "commit" | "proposal" | "welcome" | "group_info" | "unknown";
6
+ export type AuditTransportWireEnvelope = {
7
+ transport?: string;
8
+ delivery_plane?: string;
9
+ wire_id?: string;
10
+ wire_kind?: string;
11
+ wire_pubkey_hex?: string;
12
+ transport_group_id?: string;
13
+ relay_url?: string;
14
+ subscription_id?: string;
15
+ nostr_event_id?: string;
16
+ nostr_kind?: number;
17
+ nostr_pubkey_hex?: string;
18
+ gift_wrap_event_id?: string;
19
+ welcome_event_id?: string;
20
+ publish_result_id?: string;
21
+ };
22
+ export type AuditHumanActionContext = {
23
+ action: string;
24
+ origin: string;
25
+ fields?: string[];
26
+ component_ids?: number[];
27
+ target_count?: number;
28
+ };
29
+ export type AuditTransportContext = {
30
+ transport_source: string;
31
+ delivery_plane?: string;
32
+ relay_url?: string;
33
+ subscription_id?: string;
34
+ wire?: AuditTransportWireEnvelope;
35
+ };
36
+ export type AuditEngineContext = {
37
+ ciphersuite?: number;
38
+ max_past_epochs?: number;
39
+ convergence_max_rewind_commits?: number;
40
+ supported_app_component_count?: number;
41
+ feature_count?: number;
42
+ };
43
+ export type AuditGroupContext = {
44
+ epoch?: number;
45
+ member_count?: number;
46
+ required_app_component_count?: number;
47
+ admin_count?: number;
48
+ convergence_max_rewind_commits?: number;
49
+ };
50
+ export type AuditConvergenceContext = {
51
+ run_id: string;
52
+ phase?: "started" | "waiting" | "evaluating" | "selected" | "blocked" | "applied" | "failed" | "stable" | "unrecoverable";
53
+ inferred?: boolean;
54
+ };
55
+ export type AuditSourceContext = {
56
+ account_label?: string;
57
+ device_label?: string;
58
+ device_id?: string;
59
+ device_name?: string;
60
+ platform?: string;
61
+ app_version?: string;
62
+ upload_trigger?: string;
63
+ account_pubkey_hex?: string;
64
+ account_npub?: string;
65
+ };
66
+ export type AuditEventContext = {
67
+ operation_id?: string;
68
+ human_action?: AuditHumanActionContext;
69
+ transport?: AuditTransportContext;
70
+ engine?: AuditEngineContext;
71
+ group?: AuditGroupContext;
72
+ convergence?: AuditConvergenceContext;
73
+ source?: AuditSourceContext;
74
+ };
75
+ export type AuditBaseEvent = {
76
+ schema_version: typeof MARMOT_AUDIT_SCHEMA_VERSION;
77
+ seq: number;
78
+ wall_time_ms: number;
79
+ recorder_session_id?: string;
80
+ audit_data_mode: AuditDataMode;
81
+ account_ref?: string;
82
+ engine_id: string;
83
+ group_ref?: string;
84
+ context?: AuditEventContext;
85
+ };
86
+ export type AuditPublishRelayFailure = {
87
+ relay_url: string;
88
+ reason: string;
89
+ };
90
+ export type AuditOutboundMessage = {
91
+ msg_id: string;
92
+ artifact_kind: AuditMessageArtifactKind;
93
+ transport?: AuditTransportWireEnvelope;
94
+ };
95
+ export type AuditConvergenceCandidate = {
96
+ branch_id: string;
97
+ fork_epoch: number;
98
+ tip_epoch: number;
99
+ commit_ids?: string[];
100
+ commit_count?: number;
101
+ state_digest?: string;
102
+ tip_digest?: string;
103
+ tip_priority?: string;
104
+ tip_committer_ref?: string;
105
+ retained_anchor_status?: string;
106
+ last_input_time_ms?: number;
107
+ eligible?: boolean;
108
+ rejection_reasons?: string[];
109
+ };
110
+ export type AuditEventKind = {
111
+ type: "recorder_started";
112
+ recorder: string;
113
+ } | {
114
+ type: "audit_data_mode_changed";
115
+ previous_mode: AuditDataMode;
116
+ new_mode: AuditDataMode;
117
+ reason: string;
118
+ recorder_restarted?: boolean;
119
+ } | {
120
+ type: "source_context";
121
+ source: AuditSourceContext;
122
+ } | {
123
+ type: "engine_context";
124
+ context: AuditEngineContext;
125
+ } | {
126
+ type: "group_context";
127
+ reason: string;
128
+ context: AuditGroupContext;
129
+ } | {
130
+ type: "recorder_health";
131
+ serialization_failures: number;
132
+ write_failures: number;
133
+ flush_failures: number;
134
+ } | {
135
+ type: "human_action";
136
+ action: string;
137
+ origin: string;
138
+ phase: string;
139
+ fields?: string[];
140
+ component_ids?: number[];
141
+ target_count?: number;
142
+ message_ids?: string[];
143
+ from_epoch?: number;
144
+ to_epoch?: number;
145
+ error_kind?: string;
146
+ detail?: string;
147
+ } | {
148
+ type: "ingest_entry";
149
+ msg_id: string;
150
+ envelope_kind: string;
151
+ transport_source: string;
152
+ transport?: AuditTransportWireEnvelope;
153
+ payload_len: number;
154
+ payload_digest: string;
155
+ } | {
156
+ type: "ingest_outcome";
157
+ msg_id: string;
158
+ outcome_kind: string;
159
+ stale_reason?: string;
160
+ epoch?: number;
161
+ } | {
162
+ type: "ingest_error";
163
+ msg_id: string;
164
+ error_kind: string;
165
+ detail?: string;
166
+ } | {
167
+ type: "send_entry";
168
+ intent_kind: string;
169
+ } | {
170
+ type: "send_outcome";
171
+ intent_kind: string;
172
+ result_kind: string;
173
+ outbound_messages?: AuditOutboundMessage[];
174
+ } | {
175
+ type: "send_error";
176
+ intent_kind: string;
177
+ error_kind: string;
178
+ detail?: string;
179
+ } | {
180
+ type: "publish_attempt";
181
+ msg_id: string;
182
+ artifact_kind?: AuditMessageArtifactKind;
183
+ target_kind: string;
184
+ transport?: AuditTransportWireEnvelope;
185
+ relay_urls?: string[];
186
+ required_acks: number;
187
+ } | {
188
+ type: "publish_outcome";
189
+ msg_id: string;
190
+ artifact_kind?: AuditMessageArtifactKind;
191
+ target_kind: string;
192
+ transport?: AuditTransportWireEnvelope;
193
+ accepted_relay_urls?: string[];
194
+ failed_relays?: AuditPublishRelayFailure[];
195
+ required_acks: number;
196
+ met_required_acks: boolean;
197
+ } | {
198
+ type: "publish_failure";
199
+ msg_id: string;
200
+ artifact_kind?: AuditMessageArtifactKind;
201
+ stage: string;
202
+ target_kind: string;
203
+ transport?: AuditTransportWireEnvelope;
204
+ relay_urls?: string[];
205
+ reason: string;
206
+ } | {
207
+ type: "epoch_confirmed";
208
+ from_epoch: number;
209
+ to_epoch: number;
210
+ pending_kind: string;
211
+ origin_commit_id?: string;
212
+ } | {
213
+ type: "epoch_rolled_back";
214
+ pending_epoch: number;
215
+ restored_epoch: number;
216
+ pending_kind: string;
217
+ } | {
218
+ type: "epoch_state_changed";
219
+ previous_state?: AuditEpochState;
220
+ new_state: AuditEpochState;
221
+ epoch: number;
222
+ reason: string;
223
+ pending_ref?: number;
224
+ pending_kind?: string;
225
+ } | {
226
+ type: "convergence_decision";
227
+ current_tip_epoch: number;
228
+ max_rewind_commits: number;
229
+ candidates: AuditConvergenceCandidate[];
230
+ selected_branch_id?: string;
231
+ selected_fork_epoch?: number;
232
+ selected_tip_epoch?: number;
233
+ selected_tip_digest?: string;
234
+ selected_tip_committer?: string;
235
+ decisive_rule?: string;
236
+ witness_quorum_met?: boolean;
237
+ app_witness_score?: number;
238
+ losing_branch_ids?: string[];
239
+ error_kinds?: string[];
240
+ } | {
241
+ type: "peeler_outcome";
242
+ msg_id: string;
243
+ outcome: "success" | "decrypt_failed" | "stale_epoch" | "malformed" | "other";
244
+ artifact_kind?: AuditMessageArtifactKind;
245
+ fallback_snapshot_used: boolean;
246
+ fallback_snapshot_name?: string;
247
+ fallback_snapshot_source_epoch?: number;
248
+ fallback_attempt_count?: number;
249
+ error_kind?: string;
250
+ detail?: string;
251
+ } | {
252
+ type: "message_state_changed";
253
+ msg_id: string;
254
+ artifact_kind?: AuditMessageArtifactKind;
255
+ previous_state?: string;
256
+ new_state: string;
257
+ epoch?: number;
258
+ reason: string;
259
+ } | {
260
+ type: "rejection";
261
+ msg_id: string;
262
+ reason: string;
263
+ };
264
+ export type MarmotAuditEvent = AuditBaseEvent & {
265
+ kind: AuditEventKind;
266
+ };
267
+ export interface AuditSink {
268
+ record(event: MarmotAuditEvent): void;
269
+ }
270
+ export interface AuditRecorder extends AuditSink {
271
+ flush(): Promise<void>;
272
+ close(): Promise<void>;
273
+ }
274
+ export type AuditContextOptions = {
275
+ engineId: string;
276
+ accountRef?: string;
277
+ recorderSessionId?: string;
278
+ dataMode?: AuditDataMode;
279
+ source?: AuditSourceContext;
280
+ now?: () => number;
281
+ };
282
+ export type AuditEmitContext = {
283
+ engineId: string;
284
+ accountRef?: string;
285
+ recorderSessionId?: string;
286
+ dataMode: AuditDataMode;
287
+ source?: AuditSourceContext;
288
+ now: () => number;
289
+ };
@@ -0,0 +1,2 @@
1
+ /** @module @category Audit */
2
+ export const MARMOT_AUDIT_SCHEMA_VERSION = "marmot-forensics-audit/v2";
@@ -0,0 +1,165 @@
1
+ import { type UnsignedEvent } from "../utils/nostr.js";
2
+ import { type ClientState, type CustomExtension, type LeafNode } from "ts-mls";
3
+ /**
4
+ * The Marmot account identity proof, carried as a custom MLS LeafNode extension
5
+ * (`marmot.account-identity-proof.v2`).
6
+ *
7
+ * An MLS BasicCredential names a Marmot (Nostr) account, but the MLS signature
8
+ * key is a separate per-leaf key. This extension binds the two by having the
9
+ * Nostr account key sign — with BIP-340 Schnorr — a canonical, unpublished
10
+ * Nostr kind-450 event whose tags carry the account/leaf binding, so
11
+ * account-scoped policy (e.g. admin authorization) can trust the credential
12
+ * identity. Signing a real (if unpublished) Nostr event, rather than a
13
+ * bespoke digest, lets external Nostr signers (NIP-07/NIP-46, hardware
14
+ * signers) produce the proof via a normal `signEvent` path — see
15
+ * {@link buildAccountIdentityProofEvent} and
16
+ * {@link accountIdentityProofSignatureFromSignedEvent}.
17
+ *
18
+ * The signed message is the NIP-01 event id of the canonical kind-450 event:
19
+ * pubkey = account identity (x-only Nostr pubkey, lowercase hex)
20
+ * created_at = 0
21
+ * kind = 450
22
+ * content = ""
23
+ * tags = [
24
+ * ["d", "marmot.account-identity-proof.v2"],
25
+ * ["extension", "0xf2f1"],
26
+ * ["version", "2"],
27
+ * ["ciphersuite", "<decimal>"],
28
+ * ["signature_scheme", "<decimal>"],
29
+ * ["mls_signature_key", "<lowercase hex>"],
30
+ * ]
31
+ *
32
+ * Wire (`encode_proof`, fixed-width, big-endian; unchanged from v1 except the
33
+ * version byte value and the meaning of the signature):
34
+ * uint8 version; // 2
35
+ * uint16 ciphersuite;
36
+ * uint16 signature_scheme;
37
+ * opaque account_identity[32]; // x-only Nostr pubkey, no length prefix
38
+ * uint16 mls_signature_public_key_len;
39
+ * opaque mls_signature_public_key[len];
40
+ * opaque signature[64]; // BIP-340 Schnorr over the kind-450 event id
41
+ *
42
+ * @see darkmatter `crates/cgka-engine/src/account_identity_proof.rs`
43
+ */
44
+ export declare const ACCOUNT_IDENTITY_PROOF_EXTENSION_TYPE = 62193;
45
+ /** The (unpublished, local-only) Nostr event kind the proof signs. */
46
+ export declare const ACCOUNT_IDENTITY_PROOF_EVENT_KIND = 450;
47
+ /** Returns the MLS signature scheme code point for a ciphersuite id. */
48
+ export declare function mlsSignatureScheme(ciphersuite: number): number;
49
+ /** The values a Marmot account key signs to bind an MLS leaf to that account. */
50
+ export interface AccountIdentityProofRequest {
51
+ /** The 32-byte x-only Nostr account pubkey (the credential identity). */
52
+ accountIdentity: Uint8Array;
53
+ /** The MLS leaf signature public key. */
54
+ mlsSignaturePublicKey: Uint8Array;
55
+ /** The MLS ciphersuite id. */
56
+ ciphersuite: number;
57
+ /** The MLS signature scheme code point (see {@link mlsSignatureScheme}). */
58
+ signatureScheme: number;
59
+ }
60
+ /** A decoded account identity proof: the signed request plus its signature. */
61
+ export interface AccountIdentityProof {
62
+ request: AccountIdentityProofRequest;
63
+ /** 64-byte BIP-340 Schnorr signature by the account key. */
64
+ signature: Uint8Array;
65
+ }
66
+ /**
67
+ * A Nostr event, signed by an external signer (NIP-07/NIP-46, hardware, etc).
68
+ * Only the members needed to extract/verify the proof signature are required.
69
+ */
70
+ export interface SignedAccountIdentityProofEvent {
71
+ id: string;
72
+ pubkey: string;
73
+ sig: string;
74
+ }
75
+ /**
76
+ * An external Nostr event signer (e.g. NIP-07 `window.nostr.signEvent`,
77
+ * NIP-46 remote signer, or a hardware signer) capable of signing the
78
+ * canonical unsigned kind-450 proof event and returning the signed event.
79
+ */
80
+ export type AccountIdentityProofEventSigner = (event: UnsignedEvent) => SignedAccountIdentityProofEvent | Promise<SignedAccountIdentityProofEvent>;
81
+ /**
82
+ * A hook that produces the 64-byte BIP-340 proof signature. Two shapes:
83
+ * - a plain function: the raw-secret-key digest path (see
84
+ * {@link signAccountIdentityProof}) — signs the 32-byte kind-450 event id
85
+ * directly.
86
+ * - `{ signEvent }`: the external-signer path — signs the canonical kind-450
87
+ * {@link buildAccountIdentityProofEvent} via a normal Nostr `signEvent` API;
88
+ * the 64-byte signature is extracted with
89
+ * {@link accountIdentityProofSignatureFromSignedEvent}.
90
+ */
91
+ export type AccountIdentityProofSigner = ((request: AccountIdentityProofRequest) => Uint8Array | Promise<Uint8Array>) | {
92
+ signEvent: AccountIdentityProofEventSigner;
93
+ };
94
+ /**
95
+ * Builds the canonical, unsigned Nostr kind-450 proof event for a request.
96
+ * This event is a **local signing template only** — it is never published or
97
+ * relayed (`foundation/registries.md`: kind 450 is "Local signing template,
98
+ * not relayed"). Mirrors darkmatter `AccountIdentityProofRequest::proof_event`.
99
+ */
100
+ export declare function buildAccountIdentityProofEvent(request: AccountIdentityProofRequest): UnsignedEvent;
101
+ /**
102
+ * Returns the canonical unsigned kind-450 proof event as a JSON string, for
103
+ * handing to an external signer that accepts a serialized event template.
104
+ * Mirrors darkmatter `AccountIdentityProofRequest::proof_event_json`.
105
+ */
106
+ export declare function accountIdentityProofEventJson(request: AccountIdentityProofRequest): string;
107
+ /**
108
+ * Returns the lowercase-hex NIP-01 event id of the canonical kind-450 proof
109
+ * event. Mirrors darkmatter `AccountIdentityProofRequest::proof_event_id`.
110
+ */
111
+ export declare function accountIdentityProofEventId(request: AccountIdentityProofRequest): string;
112
+ /** The 32-byte BIP-340 message digest the account key signs: the NIP-01 event
113
+ * id of the canonical, unpublished kind-450 proof event (see
114
+ * {@link buildAccountIdentityProofEvent}). */
115
+ export declare function accountIdentityProofSigningDigest(request: AccountIdentityProofRequest): Uint8Array;
116
+ /** Signs a proof request with a raw 32-byte Nostr secret key (BIP-340 Schnorr). */
117
+ export declare function signAccountIdentityProof(request: AccountIdentityProofRequest, secretKey: Uint8Array): Uint8Array;
118
+ /**
119
+ * Validates a signed kind-450 proof event against a request and extracts the
120
+ * 64-byte BIP-340 Schnorr signature. Throws if the signed event's pubkey
121
+ * differs from the request's account identity, if its id differs from the
122
+ * rebuilt canonical proof-event id, or if the signature does not verify.
123
+ * Mirrors darkmatter `AccountIdentityProofRequest::signature_from_signed_event`.
124
+ */
125
+ export declare function accountIdentityProofSignatureFromSignedEvent(request: AccountIdentityProofRequest, event: SignedAccountIdentityProofEvent): Uint8Array;
126
+ /** Encodes an {@link AccountIdentityProof} to its LeafNode extension bytes. */
127
+ export declare function encodeAccountIdentityProof(proof: AccountIdentityProof): Uint8Array;
128
+ /** Decodes account identity proof LeafNode extension bytes. */
129
+ export declare function decodeAccountIdentityProof(data: Uint8Array): AccountIdentityProof;
130
+ /** Builds the `marmot.account-identity-proof.v2` LeafNode extension. */
131
+ export declare function makeAccountIdentityProofExtension(proof: AccountIdentityProof): CustomExtension;
132
+ /**
133
+ * Builds the account identity proof request for a leaf, signs it with the given
134
+ * account signer, and returns the LeafNode extension. The MLS ciphersuite id and
135
+ * leaf signature key are bound into the proof.
136
+ *
137
+ * `params.signer` may be either a raw-secret-key digest signer (a plain
138
+ * function; see {@link signAccountIdentityProof}) or an external Nostr event
139
+ * signer (`{ signEvent }`; see {@link AccountIdentityProofEventSigner}) —
140
+ * both produce the same 64-byte BIP-340 signature.
141
+ */
142
+ export declare function buildAccountIdentityProofExtension(params: {
143
+ accountIdentity: Uint8Array;
144
+ mlsSignaturePublicKey: Uint8Array;
145
+ ciphersuite: number;
146
+ signer: AccountIdentityProofSigner;
147
+ }): Promise<CustomExtension>;
148
+ /**
149
+ * Verifies a leaf's account identity proof: the embedded account identity and
150
+ * MLS signature key match the leaf, the ciphersuite/scheme match, and the
151
+ * BIP-340 signature verifies under the credential's x-only Nostr pubkey.
152
+ *
153
+ * Throws on any mismatch. Mirrors darkmatter
154
+ * `validate_leaf_account_identity_proof`.
155
+ */
156
+ export declare function verifyLeafAccountIdentityProof(leaf: LeafNode, ciphersuite: number): void;
157
+ /**
158
+ * Verifies the Marmot account identity proof on every member leaf in the group.
159
+ *
160
+ * The spec requires a valid proof on every member leaf and KeyPackage — "there
161
+ * is no legacy fallback" (`foundation/account-identity-proof` spec §Validation;
162
+ * the spec doc is still filed under its pre-v2 name, see PROOF-V2.md).
163
+ * Throws on the first leaf whose proof is missing or invalid, naming the member.
164
+ */
165
+ export declare function verifyAllLeafAccountIdentityProofs(state: ClientState, ciphersuite: number): void;