@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.
- package/CHANGELOG.md +434 -0
- package/LICENSE +49 -0
- package/README.md +33 -2
- package/dist/audit/emitter.d.ts +17 -0
- package/dist/audit/emitter.js +30 -0
- package/dist/audit/helpers.d.ts +16 -0
- package/dist/audit/helpers.js +103 -0
- package/dist/audit/index.d.ts +6 -0
- package/dist/audit/index.js +6 -0
- package/dist/audit/recorder.d.ts +13 -0
- package/dist/audit/recorder.js +24 -0
- package/dist/audit/sink.d.ts +16 -0
- package/dist/audit/sink.js +27 -0
- package/dist/audit/types-internal.d.ts +7 -0
- package/dist/audit/types-internal.js +1 -0
- package/dist/audit/types.d.ts +289 -0
- package/dist/audit/types.js +2 -0
- package/dist/core/account-identity-proof.d.ts +165 -0
- package/dist/core/account-identity-proof.js +277 -0
- package/dist/core/application-rumor.d.ts +45 -0
- package/dist/core/application-rumor.js +120 -0
- package/dist/core/auth-service.d.ts +2 -0
- package/dist/core/auth-service.js +52 -0
- package/dist/core/binary.d.ts +142 -0
- package/dist/core/binary.js +363 -0
- package/dist/core/capabilities.d.ts +51 -0
- package/dist/core/capabilities.js +102 -0
- package/dist/core/client-state.d.ts +119 -0
- package/dist/core/client-state.js +226 -0
- package/dist/core/commit-authorization.d.ts +26 -0
- package/dist/core/commit-authorization.js +26 -0
- package/dist/core/components/admin-policy.d.ts +4 -0
- package/dist/core/components/admin-policy.js +69 -0
- package/dist/core/components/agent-text-stream.d.ts +51 -0
- package/dist/core/components/agent-text-stream.js +112 -0
- package/dist/core/components/app-components-list.d.ts +17 -0
- package/dist/core/components/app-components-list.js +35 -0
- package/dist/core/components/avatar-url.d.ts +12 -0
- package/dist/core/components/avatar-url.js +85 -0
- package/dist/core/components/bytes.d.ts +5 -0
- package/dist/core/components/bytes.js +18 -0
- package/dist/core/components/dictionary.d.ts +80 -0
- package/dist/core/components/dictionary.js +175 -0
- package/dist/core/components/disband-validation.d.ts +21 -0
- package/dist/core/components/disband-validation.js +173 -0
- package/dist/core/components/encrypted-media.d.ts +44 -0
- package/dist/core/components/encrypted-media.js +210 -0
- package/dist/core/components/group-lifecycle.d.ts +10 -0
- package/dist/core/components/group-lifecycle.js +32 -0
- package/dist/core/components/group-profile.d.ts +24 -0
- package/dist/core/components/group-profile.js +35 -0
- package/dist/core/components/host-safety.d.ts +9 -0
- package/dist/core/components/host-safety.js +148 -0
- package/dist/core/components/ids.d.ts +74 -0
- package/dist/core/components/ids.js +73 -0
- package/dist/core/components/index.d.ts +14 -0
- package/dist/core/components/index.js +14 -0
- package/dist/core/components/integrity.d.ts +141 -0
- package/dist/core/components/integrity.js +293 -0
- package/dist/core/components/message-retention.d.ts +17 -0
- package/dist/core/components/message-retention.js +26 -0
- package/dist/core/components/nostr-routing.d.ts +8 -0
- package/dist/core/components/nostr-routing.js +92 -0
- package/dist/core/components/url.d.ts +26 -0
- package/dist/core/components/url.js +44 -0
- package/dist/core/convergence-status.d.ts +84 -0
- package/dist/core/convergence-status.js +82 -0
- package/dist/core/convergence.d.ts +567 -0
- package/dist/core/convergence.js +1150 -0
- package/dist/core/credential.d.ts +16 -0
- package/dist/core/credential.js +64 -0
- package/dist/core/default-capabilities.d.ts +9 -0
- package/dist/core/default-capabilities.js +34 -0
- package/dist/core/extensions.d.ts +9 -0
- package/dist/core/extensions.js +5 -0
- package/dist/core/grease.d.ts +2 -0
- package/dist/core/grease.js +6 -0
- package/dist/core/group-event.d.ts +18 -0
- package/dist/core/group-event.js +30 -0
- package/dist/core/group-lifecycle.d.ts +42 -0
- package/dist/core/group-lifecycle.js +66 -0
- package/dist/core/group-members.d.ts +37 -0
- package/dist/core/group-members.js +99 -0
- package/dist/core/group-message-classify.d.ts +42 -0
- package/dist/core/group-message-classify.js +57 -0
- package/dist/core/group-message-crypto.d.ts +53 -0
- package/dist/core/group-message-crypto.js +140 -0
- package/dist/core/group-message.d.ts +5 -0
- package/dist/core/group-message.js +11 -0
- package/dist/core/group.d.ts +45 -0
- package/dist/core/group.js +78 -0
- package/dist/core/inbound.d.ts +102 -0
- package/dist/core/inbound.js +79 -0
- package/dist/core/index.d.ts +28 -0
- package/dist/core/index.js +26 -0
- package/dist/core/key-package-eligibility.d.ts +31 -0
- package/dist/core/key-package-eligibility.js +116 -0
- package/dist/core/key-package-event-decode.d.ts +43 -0
- package/dist/core/key-package-event-decode.js +118 -0
- package/dist/core/key-package-event-delete.d.ts +16 -0
- package/dist/core/key-package-event-delete.js +46 -0
- package/dist/core/key-package-event-encode.d.ts +28 -0
- package/dist/core/key-package-event-encode.js +102 -0
- package/dist/core/key-package-event.d.ts +4 -0
- package/dist/core/key-package-event.js +8 -0
- package/dist/core/key-package.d.ts +55 -0
- package/dist/core/key-package.js +115 -0
- package/dist/core/leaf-succession.d.ts +83 -0
- package/dist/core/leaf-succession.js +97 -0
- package/dist/core/media/canonical.d.ts +34 -0
- package/dist/core/media/canonical.js +57 -0
- package/dist/core/media/crypto.d.ts +79 -0
- package/dist/core/media/crypto.js +215 -0
- package/dist/core/media/imeta.d.ts +33 -0
- package/dist/core/media/imeta.js +188 -0
- package/dist/core/media/locator.d.ts +44 -0
- package/dist/core/media/locator.js +69 -0
- package/dist/core/media/types.d.ts +83 -0
- package/dist/core/media/types.js +12 -0
- package/dist/core/media.d.ts +6 -0
- package/dist/core/media.js +12 -0
- package/dist/core/protocol.d.ts +51 -0
- package/dist/core/protocol.js +41 -0
- package/dist/core/relay-lists.d.ts +52 -0
- package/dist/core/relay-lists.js +86 -0
- package/dist/core/retained-history.d.ts +78 -0
- package/dist/core/retained-history.js +59 -0
- package/dist/core/transport.d.ts +43 -0
- package/dist/core/transport.js +21 -0
- package/dist/core/welcome-event.d.ts +61 -0
- package/dist/core/welcome-event.js +116 -0
- package/dist/core/welcome-join.d.ts +155 -0
- package/dist/core/welcome-join.js +243 -0
- package/dist/core/welcome.d.ts +3 -0
- package/dist/core/welcome.js +6 -0
- package/dist/engine/admin-policy.d.ts +58 -0
- package/dist/engine/admin-policy.js +137 -0
- package/dist/engine/auto-committer.d.ts +39 -0
- package/dist/engine/auto-committer.js +42 -0
- package/dist/engine/delivered-payloads.d.ts +61 -0
- package/dist/engine/delivered-payloads.js +84 -0
- package/dist/engine/disband-request.d.ts +41 -0
- package/dist/engine/disband-request.js +86 -0
- package/dist/engine/disband-tombstone.d.ts +14 -0
- package/dist/engine/disband-tombstone.js +66 -0
- package/dist/engine/fork-recovery.d.ts +672 -0
- package/dist/engine/fork-recovery.js +1615 -0
- package/dist/engine/group-engine.d.ts +571 -0
- package/dist/engine/group-engine.js +5112 -0
- package/dist/engine/history-tree.d.ts +232 -0
- package/dist/engine/history-tree.js +651 -0
- package/dist/engine/index.d.ts +16 -0
- package/dist/engine/index.js +15 -0
- package/dist/engine/ingest-disposition.d.ts +5 -0
- package/dist/engine/ingest-disposition.js +63 -0
- package/dist/engine/ingest.d.ts +390 -0
- package/dist/engine/ingest.js +1258 -0
- package/dist/engine/ingestion-pool.d.ts +74 -0
- package/dist/engine/ingestion-pool.js +95 -0
- package/dist/engine/message-dedup.d.ts +16 -0
- package/dist/engine/message-dedup.js +20 -0
- package/dist/engine/own-commit-stamp.d.ts +30 -0
- package/dist/engine/own-commit-stamp.js +71 -0
- package/dist/engine/retained-store.d.ts +79 -0
- package/dist/engine/retained-store.js +133 -0
- package/dist/engine/sender-ratchet.d.ts +50 -0
- package/dist/engine/sender-ratchet.js +196 -0
- package/dist/engine/state-notifications.d.ts +122 -0
- package/dist/engine/state-notifications.js +173 -0
- package/dist/engine/tree-convergence.d.ts +41 -0
- package/dist/engine/tree-convergence.js +90 -0
- package/dist/engine/types.d.ts +366 -0
- package/dist/engine/types.js +14 -0
- package/dist/engine/wire-format.d.ts +77 -0
- package/dist/engine/wire-format.js +120 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +1 -0
- package/dist/mls.d.ts +11 -0
- package/dist/mls.js +11 -0
- package/dist/utils/debug.d.ts +3 -0
- package/dist/utils/debug.js +3 -0
- package/dist/utils/encoding.d.ts +38 -0
- package/dist/utils/encoding.js +91 -0
- package/dist/utils/key-value.d.ts +18 -0
- package/dist/utils/key-value.js +2 -0
- package/dist/utils/nostr.d.ts +35 -0
- package/dist/utils/nostr.js +26 -0
- package/dist/utils/relay-url.d.ts +10 -0
- package/dist/utils/relay-url.js +23 -0
- package/dist/utils/tag-cardinality.d.ts +50 -0
- package/dist/utils/tag-cardinality.js +91 -0
- package/dist/utils/timestamp.d.ts +50 -0
- package/dist/utils/timestamp.js +95 -0
- package/dist/version.d.ts +112 -0
- package/dist/version.js +112 -0
- package/package.json +68 -3
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/** @module @category Core - Retained History */
|
|
2
|
+
/**
|
|
3
|
+
* Retained-history decision logic (Marmot v2 `protocol-core/retained-history.md`).
|
|
4
|
+
*
|
|
5
|
+
* Clients retain group state so they can rebuild candidate branches and recover
|
|
6
|
+
* from forks and late delivery. The **retained anchor** is the oldest state from
|
|
7
|
+
* which a client can rebuild a candidate branch; the **rollback horizon**
|
|
8
|
+
* (`max_rewind_commits`) bounds how far back a branch may fork. These pure
|
|
9
|
+
* functions classify late commits, expire old app payloads, and decide what
|
|
10
|
+
* must be retained vs. may be pruned — without consulting transport order.
|
|
11
|
+
*/
|
|
12
|
+
/** The outcome of judging a late commit by its source epoch. */
|
|
13
|
+
export type LateCommitOutcome =
|
|
14
|
+
/** Source epoch is at/after the anchor with retained state present: replayable. */
|
|
15
|
+
{
|
|
16
|
+
kind: "replay";
|
|
17
|
+
}
|
|
18
|
+
/** Source epoch is older than the retained anchor: drop as BeyondAnchor. */
|
|
19
|
+
| {
|
|
20
|
+
kind: "beyond_anchor";
|
|
21
|
+
}
|
|
22
|
+
/** Inside the rollback horizon but required retained state was lost: → Unrecoverable. */
|
|
23
|
+
| {
|
|
24
|
+
kind: "missing_retained_anchor";
|
|
25
|
+
}
|
|
26
|
+
/** Parent commit has not arrived yet: wait for it (a transport gap, not loss). */
|
|
27
|
+
| {
|
|
28
|
+
kind: "deferred";
|
|
29
|
+
}
|
|
30
|
+
/** At/after the anchor but forks from outside the rollback horizon: not selectable. */
|
|
31
|
+
| {
|
|
32
|
+
kind: "ineligible";
|
|
33
|
+
};
|
|
34
|
+
/** Inputs for {@link classifyLateCommit}. */
|
|
35
|
+
export interface LateCommitContext {
|
|
36
|
+
/** The late commit's MLS source epoch (the epoch it advances from). */
|
|
37
|
+
sourceEpoch: number;
|
|
38
|
+
/** The oldest retained state epoch (the retained anchor). */
|
|
39
|
+
anchorEpoch: number;
|
|
40
|
+
/** The current canonical tip epoch. */
|
|
41
|
+
currentTipEpoch: number;
|
|
42
|
+
/** The group's rollback horizon (`max_rewind_commits`). */
|
|
43
|
+
maxRewindCommits: number;
|
|
44
|
+
/** Whether the commit's parent commit has arrived (so its parent epoch is known). */
|
|
45
|
+
parentArrived: boolean;
|
|
46
|
+
/** Whether the retained parent state needed to replay is still in storage. */
|
|
47
|
+
retainedParentStateAvailable: boolean;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Classifies a late commit per `retained-history.md` "Late commits".
|
|
51
|
+
*
|
|
52
|
+
* Precedence: a commit below the anchor is always `beyond_anchor`; otherwise a
|
|
53
|
+
* commit whose parent has not arrived is `deferred` (a transport gap, not
|
|
54
|
+
* storage loss); within the rollback horizon it `replay`s when its retained
|
|
55
|
+
* parent state is present and is `missing_retained_anchor` (→ `Unrecoverable`)
|
|
56
|
+
* when that state was lost from storage; at/after the anchor but outside the
|
|
57
|
+
* horizon it is `ineligible` for selection.
|
|
58
|
+
*/
|
|
59
|
+
export declare function classifyLateCommit(ctx: LateCommitContext): LateCommitOutcome;
|
|
60
|
+
/**
|
|
61
|
+
* Whether an MLS application message has fallen outside the retained app-payload
|
|
62
|
+
* window and MUST expire: it is more than `appPayloadPastEpochLimit` past epochs
|
|
63
|
+
* behind the current tip.
|
|
64
|
+
*/
|
|
65
|
+
export declare function isAppPayloadExpired(messageEpoch: number, currentTipEpoch: number, appPayloadPastEpochLimit: number): boolean;
|
|
66
|
+
/**
|
|
67
|
+
* The minimum set of epochs a client must retain to replay candidate branches
|
|
68
|
+
* inside the rollback horizon: every epoch from `tip - maxRewindCommits` (floored
|
|
69
|
+
* at 0) through the current tip. Staged-commit and deferred-parent states are
|
|
70
|
+
* additional and tracked separately by the caller.
|
|
71
|
+
*/
|
|
72
|
+
export declare function requiredRetainedEpochs(currentTipEpoch: number, maxRewindCommits: number): number[];
|
|
73
|
+
/**
|
|
74
|
+
* The retained epochs a client SHOULD prune after convergence settles: those
|
|
75
|
+
* older than the rollback horizon, excluding any `pinnedEpochs` still needed to
|
|
76
|
+
* resolve an active PendingPublish / Merging / Recovering / Unrecoverable state.
|
|
77
|
+
*/
|
|
78
|
+
export declare function prunableRetainedEpochs(retainedEpochs: Iterable<number>, currentTipEpoch: number, maxRewindCommits: number, pinnedEpochs?: Iterable<number>): number[];
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/** @module @category Core - Retained History */
|
|
2
|
+
/**
|
|
3
|
+
* Classifies a late commit per `retained-history.md` "Late commits".
|
|
4
|
+
*
|
|
5
|
+
* Precedence: a commit below the anchor is always `beyond_anchor`; otherwise a
|
|
6
|
+
* commit whose parent has not arrived is `deferred` (a transport gap, not
|
|
7
|
+
* storage loss); within the rollback horizon it `replay`s when its retained
|
|
8
|
+
* parent state is present and is `missing_retained_anchor` (→ `Unrecoverable`)
|
|
9
|
+
* when that state was lost from storage; at/after the anchor but outside the
|
|
10
|
+
* horizon it is `ineligible` for selection.
|
|
11
|
+
*/
|
|
12
|
+
export function classifyLateCommit(ctx) {
|
|
13
|
+
if (ctx.sourceEpoch < ctx.anchorEpoch)
|
|
14
|
+
return { kind: "beyond_anchor" };
|
|
15
|
+
if (!ctx.parentArrived)
|
|
16
|
+
return { kind: "deferred" };
|
|
17
|
+
const withinHorizon = ctx.currentTipEpoch - ctx.sourceEpoch <= ctx.maxRewindCommits;
|
|
18
|
+
if (!withinHorizon)
|
|
19
|
+
return { kind: "ineligible" };
|
|
20
|
+
return ctx.retainedParentStateAvailable
|
|
21
|
+
? { kind: "replay" }
|
|
22
|
+
: { kind: "missing_retained_anchor" };
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Whether an MLS application message has fallen outside the retained app-payload
|
|
26
|
+
* window and MUST expire: it is more than `appPayloadPastEpochLimit` past epochs
|
|
27
|
+
* behind the current tip.
|
|
28
|
+
*/
|
|
29
|
+
export function isAppPayloadExpired(messageEpoch, currentTipEpoch, appPayloadPastEpochLimit) {
|
|
30
|
+
return currentTipEpoch - messageEpoch > appPayloadPastEpochLimit;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* The minimum set of epochs a client must retain to replay candidate branches
|
|
34
|
+
* inside the rollback horizon: every epoch from `tip - maxRewindCommits` (floored
|
|
35
|
+
* at 0) through the current tip. Staged-commit and deferred-parent states are
|
|
36
|
+
* additional and tracked separately by the caller.
|
|
37
|
+
*/
|
|
38
|
+
export function requiredRetainedEpochs(currentTipEpoch, maxRewindCommits) {
|
|
39
|
+
const floor = Math.max(0, currentTipEpoch - maxRewindCommits);
|
|
40
|
+
const epochs = [];
|
|
41
|
+
for (let e = floor; e <= currentTipEpoch; e++)
|
|
42
|
+
epochs.push(e);
|
|
43
|
+
return epochs;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* The retained epochs a client SHOULD prune after convergence settles: those
|
|
47
|
+
* older than the rollback horizon, excluding any `pinnedEpochs` still needed to
|
|
48
|
+
* resolve an active PendingPublish / Merging / Recovering / Unrecoverable state.
|
|
49
|
+
*/
|
|
50
|
+
export function prunableRetainedEpochs(retainedEpochs, currentTipEpoch, maxRewindCommits, pinnedEpochs = []) {
|
|
51
|
+
const floor = Math.max(0, currentTipEpoch - maxRewindCommits);
|
|
52
|
+
const pinned = new Set(pinnedEpochs);
|
|
53
|
+
const prunable = [];
|
|
54
|
+
for (const epoch of retainedEpochs) {
|
|
55
|
+
if (epoch < floor && !pinned.has(epoch))
|
|
56
|
+
prunable.push(epoch);
|
|
57
|
+
}
|
|
58
|
+
return prunable.sort((a, b) => a - b);
|
|
59
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Marmot defines its group semantics independently of any one transport; a
|
|
3
|
+
* transport binding maps those semantics onto a concrete delivery network's
|
|
4
|
+
* wire identity (`transports/nostr.md`, target-architecture §4). Nostr is the
|
|
5
|
+
* binding shipped here, but the protocol is written so another binding (a relay
|
|
6
|
+
* mesh, a queue, etc.) could implement the same shape and carry the identical
|
|
7
|
+
* MLS bytes.
|
|
8
|
+
*
|
|
9
|
+
* The binding owns only *transport addressing* — event kinds and routing tags.
|
|
10
|
+
* The encrypted MLS payloads and the canonical signed group state (including
|
|
11
|
+
* `transport.nostr.routing.v1`) are transport-independent.
|
|
12
|
+
*/
|
|
13
|
+
export interface TransportBinding {
|
|
14
|
+
/** Stable binding name (matches the `transport.<name>.*` component family). */
|
|
15
|
+
readonly name: string;
|
|
16
|
+
/** Event kind carrying an encrypted group message (commit/proposal/app). */
|
|
17
|
+
readonly groupMessageKind: number;
|
|
18
|
+
/** Event kind carrying a Welcome. */
|
|
19
|
+
readonly welcomeKind: number;
|
|
20
|
+
/** Event kind for an addressable KeyPackage. */
|
|
21
|
+
readonly addressableKeyPackageKind: number;
|
|
22
|
+
/**
|
|
23
|
+
* Event kind for the NIP-65 relay list used to discover an account's
|
|
24
|
+
* KeyPackage relays (there is no dedicated KeyPackage relay list).
|
|
25
|
+
*/
|
|
26
|
+
readonly nip65RelayListKind: number;
|
|
27
|
+
/** Event kind for the inbox relay list welcomes are delivered to. */
|
|
28
|
+
readonly inboxRelayListKind: number;
|
|
29
|
+
/** Event kind for the privacy-preserving outer wrap of a Welcome. */
|
|
30
|
+
readonly giftWrapKind: number;
|
|
31
|
+
/** Single-letter event tag naming the (public) group a message routes to. */
|
|
32
|
+
readonly groupIdTag: string;
|
|
33
|
+
}
|
|
34
|
+
/** The Nostr group routing tag (`h`) naming the public nostr group id. */
|
|
35
|
+
export declare const NOSTR_GROUP_ID_TAG = "h";
|
|
36
|
+
/** NIP-59 gift-wrap event kind used to wrap a Welcome for its recipient. */
|
|
37
|
+
export declare const GIFT_WRAP_KIND = 1059;
|
|
38
|
+
/**
|
|
39
|
+
* The Nostr transport binding: the default (and currently only) Marmot delivery
|
|
40
|
+
* binding. All kinds and the routing tag are gathered here so the transport
|
|
41
|
+
* seam is explicit and a future binding can mirror the shape.
|
|
42
|
+
*/
|
|
43
|
+
export declare const nostrTransportBinding: TransportBinding;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** @module @category Core - Transport */
|
|
2
|
+
import { ADDRESSABLE_KEY_PACKAGE_KIND, GROUP_EVENT_KIND, INBOX_RELAY_LIST_KIND, NIP65_RELAY_LIST_KIND, WELCOME_EVENT_KIND, } from "./protocol.js";
|
|
3
|
+
/** The Nostr group routing tag (`h`) naming the public nostr group id. */
|
|
4
|
+
export const NOSTR_GROUP_ID_TAG = "h";
|
|
5
|
+
/** NIP-59 gift-wrap event kind used to wrap a Welcome for its recipient. */
|
|
6
|
+
export const GIFT_WRAP_KIND = 1059;
|
|
7
|
+
/**
|
|
8
|
+
* The Nostr transport binding: the default (and currently only) Marmot delivery
|
|
9
|
+
* binding. All kinds and the routing tag are gathered here so the transport
|
|
10
|
+
* seam is explicit and a future binding can mirror the shape.
|
|
11
|
+
*/
|
|
12
|
+
export const nostrTransportBinding = {
|
|
13
|
+
name: "nostr",
|
|
14
|
+
groupMessageKind: GROUP_EVENT_KIND,
|
|
15
|
+
welcomeKind: WELCOME_EVENT_KIND,
|
|
16
|
+
addressableKeyPackageKind: ADDRESSABLE_KEY_PACKAGE_KIND,
|
|
17
|
+
nip65RelayListKind: NIP65_RELAY_LIST_KIND,
|
|
18
|
+
inboxRelayListKind: INBOX_RELAY_LIST_KIND,
|
|
19
|
+
giftWrapKind: GIFT_WRAP_KIND,
|
|
20
|
+
groupIdTag: NOSTR_GROUP_ID_TAG,
|
|
21
|
+
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/** @module @category Core - Welcome */
|
|
2
|
+
import { Rumor } from "../utils/nostr.js";
|
|
3
|
+
import { type Welcome } from "ts-mls";
|
|
4
|
+
/**
|
|
5
|
+
* Creates a welcome rumor (kind 444) for a welcome message.
|
|
6
|
+
*
|
|
7
|
+
* @returns Welcome rumor with precomputed ID
|
|
8
|
+
*/
|
|
9
|
+
export declare function createWelcomeRumor({ welcome, author, groupRelays, keyPackageEventId, }: {
|
|
10
|
+
/** The MLS welcome message */
|
|
11
|
+
welcome: Welcome;
|
|
12
|
+
/** The author's public key (hex string) */
|
|
13
|
+
author: string;
|
|
14
|
+
/**
|
|
15
|
+
* The ID of the KeyPackage event consumed for this add. Required: the spec
|
|
16
|
+
* mandates a 32-byte-hex `e` tag on every welcome rumor
|
|
17
|
+
* (transports/nostr.md "Welcome delivery").
|
|
18
|
+
*/
|
|
19
|
+
keyPackageEventId: string;
|
|
20
|
+
/**
|
|
21
|
+
* Array of relay URLs for the group (becomes the `relays` tag). Must be
|
|
22
|
+
* non-empty and every URL must be distinct — matching the #236
|
|
23
|
+
* list-cardinality rule this rumor's own consumer (`getWelcome` /
|
|
24
|
+
* `getWelcomeGroupRelays`, via `getListTag`) enforces (WIRE-02).
|
|
25
|
+
*/
|
|
26
|
+
groupRelays: string[];
|
|
27
|
+
}): Rumor;
|
|
28
|
+
/**
|
|
29
|
+
* Returns the key package event ID from a welcome rumor. Strict (#236): a
|
|
30
|
+
* repeated, missing, or empty-valued `e` tag yields `undefined` rather than
|
|
31
|
+
* silently resolving to the first match (WIRE-02).
|
|
32
|
+
*/
|
|
33
|
+
export declare function getWelcomeKeyPackageEventId(event: Rumor): string | undefined;
|
|
34
|
+
/**
|
|
35
|
+
* Returns the group relays from a welcome rumor. Strict (#236): a repeated
|
|
36
|
+
* `relays` tag, an empty/absent one, or one carrying duplicate URLs yields
|
|
37
|
+
* `[]` rather than silently resolving to the first match (WIRE-02).
|
|
38
|
+
*
|
|
39
|
+
* NOTE: The "relays" tag is a normal Nostr tag vector: ["relays", ...urls]
|
|
40
|
+
* (see transports/nostr.md "Welcome delivery" and createWelcomeRumor()).
|
|
41
|
+
*/
|
|
42
|
+
export declare function getWelcomeGroupRelays(event: Rumor): string[];
|
|
43
|
+
/**
|
|
44
|
+
* Returns the KeyPackageRefs of the intended recipients from a Welcome message.
|
|
45
|
+
*
|
|
46
|
+
* Each entry in `welcome.secrets` contains a plaintext `newMember` field which
|
|
47
|
+
* is the RFC 9420 KeyPackageRef (a hash of the recipient's KeyPackage). No
|
|
48
|
+
* decryption is required to read these.
|
|
49
|
+
*
|
|
50
|
+
* @param welcome - The MLS Welcome message
|
|
51
|
+
* @returns Array of KeyPackageRefs (one per recipient)
|
|
52
|
+
*/
|
|
53
|
+
export declare function getWelcomeKeyPackageRefs(welcome: Welcome | Rumor): Uint8Array[];
|
|
54
|
+
/**
|
|
55
|
+
* Gets the Welcome message from a kind 444 event.
|
|
56
|
+
*
|
|
57
|
+
* @param event - The Nostr event containing the welcome message
|
|
58
|
+
* @returns The decoded Welcome message
|
|
59
|
+
* @throws Error if the content cannot be decoded
|
|
60
|
+
*/
|
|
61
|
+
export declare function getWelcome(event: Rumor): Welcome;
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/** @module @category Core - Welcome */
|
|
2
|
+
import { getEventHash, isRumor } from "../utils/nostr.js";
|
|
3
|
+
import { decode, encode, mlsMessageDecoder, mlsMessageEncoder, protocolVersions, wireformats, } from "ts-mls";
|
|
4
|
+
import { decodeContent, encodeContent } from "../utils/encoding.js";
|
|
5
|
+
import { unixNow } from "../utils/nostr.js";
|
|
6
|
+
import { getListTag, getSingletonTagValue } from "../utils/tag-cardinality.js";
|
|
7
|
+
import { WELCOME_EVENT_KIND } from "./protocol.js";
|
|
8
|
+
/** True when `value` is a 32-byte (64-char) lowercase-or-uppercase hex string. */
|
|
9
|
+
function isEventId(value) {
|
|
10
|
+
return typeof value === "string" && /^[0-9a-fA-F]{64}$/.test(value);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Creates a welcome rumor (kind 444) for a welcome message.
|
|
14
|
+
*
|
|
15
|
+
* @returns Welcome rumor with precomputed ID
|
|
16
|
+
*/
|
|
17
|
+
export function createWelcomeRumor({ welcome, author, groupRelays, keyPackageEventId, }) {
|
|
18
|
+
if (!isEventId(keyPackageEventId))
|
|
19
|
+
throw new Error("Welcome rumor requires a 32-byte hex KeyPackage event id (e tag)");
|
|
20
|
+
if (groupRelays.length === 0 || groupRelays.some((r) => r.length === 0))
|
|
21
|
+
throw new Error("Welcome rumor requires a non-empty relays tag with no empty relay URLs");
|
|
22
|
+
if (new Set(groupRelays).size !== groupRelays.length)
|
|
23
|
+
throw new Error("Welcome rumor requires distinct relay URLs — a duplicate relay tag value would be rejected by this library's own getWelcome consumer");
|
|
24
|
+
// Serialize the welcome message as a full MLSMessage (RFC 9420)
|
|
25
|
+
const mlsMessage = {
|
|
26
|
+
version: protocolVersions.mls10,
|
|
27
|
+
wireformat: wireformats.mls_welcome,
|
|
28
|
+
welcome,
|
|
29
|
+
};
|
|
30
|
+
const serializedWelcome = encode(mlsMessageEncoder, mlsMessage);
|
|
31
|
+
const content = encodeContent(serializedWelcome, "base64");
|
|
32
|
+
// No `encoding` tag: the spec forbids it and content is always standard
|
|
33
|
+
// base64 (transports/nostr.md "Transport byte encoding"). The `e` tag is
|
|
34
|
+
// mandatory and validated above.
|
|
35
|
+
const draft = {
|
|
36
|
+
kind: WELCOME_EVENT_KIND,
|
|
37
|
+
pubkey: author,
|
|
38
|
+
created_at: unixNow(),
|
|
39
|
+
content,
|
|
40
|
+
tags: [
|
|
41
|
+
["relays", ...groupRelays],
|
|
42
|
+
["e", keyPackageEventId],
|
|
43
|
+
],
|
|
44
|
+
};
|
|
45
|
+
// Calculate the event ID for the rumor
|
|
46
|
+
const id = getEventHash(draft);
|
|
47
|
+
return {
|
|
48
|
+
...draft,
|
|
49
|
+
id,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Returns the key package event ID from a welcome rumor. Strict (#236): a
|
|
54
|
+
* repeated, missing, or empty-valued `e` tag yields `undefined` rather than
|
|
55
|
+
* silently resolving to the first match (WIRE-02).
|
|
56
|
+
*/
|
|
57
|
+
export function getWelcomeKeyPackageEventId(event) {
|
|
58
|
+
return getSingletonTagValue(event, "e");
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Returns the group relays from a welcome rumor. Strict (#236): a repeated
|
|
62
|
+
* `relays` tag, an empty/absent one, or one carrying duplicate URLs yields
|
|
63
|
+
* `[]` rather than silently resolving to the first match (WIRE-02).
|
|
64
|
+
*
|
|
65
|
+
* NOTE: The "relays" tag is a normal Nostr tag vector: ["relays", ...urls]
|
|
66
|
+
* (see transports/nostr.md "Welcome delivery" and createWelcomeRumor()).
|
|
67
|
+
*/
|
|
68
|
+
export function getWelcomeGroupRelays(event) {
|
|
69
|
+
return getListTag(event, "relays") ?? [];
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Returns the KeyPackageRefs of the intended recipients from a Welcome message.
|
|
73
|
+
*
|
|
74
|
+
* Each entry in `welcome.secrets` contains a plaintext `newMember` field which
|
|
75
|
+
* is the RFC 9420 KeyPackageRef (a hash of the recipient's KeyPackage). No
|
|
76
|
+
* decryption is required to read these.
|
|
77
|
+
*
|
|
78
|
+
* @param welcome - The MLS Welcome message
|
|
79
|
+
* @returns Array of KeyPackageRefs (one per recipient)
|
|
80
|
+
*/
|
|
81
|
+
export function getWelcomeKeyPackageRefs(welcome) {
|
|
82
|
+
// Unwrap welcome rumor if provided
|
|
83
|
+
if (isRumor(welcome))
|
|
84
|
+
welcome = getWelcome(welcome);
|
|
85
|
+
return welcome.secrets.map((s) => s.newMember);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Gets the Welcome message from a kind 444 event.
|
|
89
|
+
*
|
|
90
|
+
* @param event - The Nostr event containing the welcome message
|
|
91
|
+
* @returns The decoded Welcome message
|
|
92
|
+
* @throws Error if the content cannot be decoded
|
|
93
|
+
*/
|
|
94
|
+
export function getWelcome(event) {
|
|
95
|
+
if (event.kind !== WELCOME_EVENT_KIND)
|
|
96
|
+
throw new Error(`Expected welcome event kind ${WELCOME_EVENT_KIND}, got ${event.kind}`);
|
|
97
|
+
// Validate the transport-level rumor shape the spec mandates before decoding
|
|
98
|
+
// (transports/nostr.md "Welcome delivery"): a singleton 32-byte-hex `e` tag
|
|
99
|
+
// and a singleton non-empty, non-duplicate `relays` tag (#236 strict
|
|
100
|
+
// cardinality — WIRE-02). getSingletonTagValue/getListTag reject repeated,
|
|
101
|
+
// empty, or duplicate-valued tags instead of first-match-resolving them.
|
|
102
|
+
const keyPackageEventId = getSingletonTagValue(event, "e");
|
|
103
|
+
if (!isEventId(keyPackageEventId))
|
|
104
|
+
throw new Error("Invalid welcome event: missing or malformed e tag (expected 32-byte hex KeyPackage event id)");
|
|
105
|
+
const relays = getWelcomeGroupRelays(event);
|
|
106
|
+
if (relays.length === 0 || relays.some((r) => r.length === 0))
|
|
107
|
+
throw new Error("Invalid welcome event: relays tag must contain at least one non-empty relay URL");
|
|
108
|
+
// Content is always standard base64; the spec forbids an `encoding` tag.
|
|
109
|
+
const content = decodeContent(event.content, "base64");
|
|
110
|
+
const mlsMessage = decode(mlsMessageDecoder, content);
|
|
111
|
+
if (!mlsMessage)
|
|
112
|
+
throw new Error("Failed to decode welcome message");
|
|
113
|
+
if (mlsMessage.wireformat !== wireformats.mls_welcome)
|
|
114
|
+
throw new Error(`Expected MLSMessage with mls_welcome wireformat, got wireformat ${mlsMessage.wireformat}`);
|
|
115
|
+
return mlsMessage.welcome;
|
|
116
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
/** @module @category Core - Welcome */
|
|
2
|
+
import { Rumor } from "../utils/nostr.js";
|
|
3
|
+
import { CiphersuiteImpl, type ClientConfig, type ClientState, type Credential, type GroupInfo, KeyPackage, PrivateKeyPackage, type Welcome } from "ts-mls";
|
|
4
|
+
import { type MarmotGroupView } from "./client-state.js";
|
|
5
|
+
/**
|
|
6
|
+
* Decrypts the {@link GroupInfo} from a Welcome message using the provided key package.
|
|
7
|
+
*
|
|
8
|
+
* This executes a full ts-mls `joinGroup` — the same protocol run
|
|
9
|
+
* {@link joinGroupFromWelcome} performs — and then discards everything but
|
|
10
|
+
* the `GroupInfo`. It is not a lighter-weight path: the Welcome is
|
|
11
|
+
* validated exactly as it would be by a real join, giving access to
|
|
12
|
+
* `groupContext` (group ID, epoch, extensions) and `GroupInfo`-level
|
|
13
|
+
* extensions (ratchet tree, external pub) without keeping the resulting
|
|
14
|
+
* `ClientState` around.
|
|
15
|
+
*
|
|
16
|
+
* `clientConfig` MUST match the config the group's creator (and every prior
|
|
17
|
+
* committer) used, for the same reason it matters to
|
|
18
|
+
* {@link joinGroupFromWelcome} — see the note there. Because this function
|
|
19
|
+
* performs a real join internally, omitting `clientConfig` here while
|
|
20
|
+
* passing one to `joinGroupFromWelcome` for the same Welcome is itself a
|
|
21
|
+
* silent-fork hazard: one call validates under ts-mls defaults, the other
|
|
22
|
+
* under the caller's config (AC-VENDOR-6).
|
|
23
|
+
*
|
|
24
|
+
* @returns The decrypted GroupInfo
|
|
25
|
+
* @throws Error if the key package does not match any secret in the welcome
|
|
26
|
+
*/
|
|
27
|
+
export declare function readWelcomeGroupInfo({ welcome, keyPackage, ciphersuiteImpl, clientConfig, }: {
|
|
28
|
+
/** The MLS Welcome message (or a kind 444 Rumor) */
|
|
29
|
+
welcome: Welcome | Rumor;
|
|
30
|
+
/** The full key package (public + private) used to receive the invite */
|
|
31
|
+
keyPackage: {
|
|
32
|
+
publicPackage: KeyPackage;
|
|
33
|
+
privatePackage: PrivateKeyPackage;
|
|
34
|
+
};
|
|
35
|
+
/** The ciphersuite implementation */
|
|
36
|
+
ciphersuiteImpl: CiphersuiteImpl;
|
|
37
|
+
/** ts-mls `ClientConfig` — see the module-level note on config agreement. */
|
|
38
|
+
clientConfig?: ClientConfig;
|
|
39
|
+
}): Promise<GroupInfo>;
|
|
40
|
+
/**
|
|
41
|
+
* Joins a group from a Welcome message (or a kind 444 Rumor), performing a
|
|
42
|
+
* full ts-mls join and returning the resulting {@link ClientState}.
|
|
43
|
+
*
|
|
44
|
+
* {@link readWelcomeGroupInfo} performs this SAME full join internally and
|
|
45
|
+
* then discards everything but the `GroupInfo`; this function keeps the
|
|
46
|
+
* live, ready-to-use group state instead of discarding it.
|
|
47
|
+
*
|
|
48
|
+
* `clientConfig` MUST match the config the group's creator (and every prior
|
|
49
|
+
* committer) used — a mismatched `keyPackageEqualityConfig` in particular is
|
|
50
|
+
* a silent-fork hazard: this member can accept a commit the group's other
|
|
51
|
+
* members would reject, or vice versa (AC-VENDOR-6). Defaults to ts-mls's
|
|
52
|
+
* own default when omitted.
|
|
53
|
+
*
|
|
54
|
+
* @returns The joined {@link ClientState}
|
|
55
|
+
* @throws Error if the key package does not match any secret in the welcome
|
|
56
|
+
*/
|
|
57
|
+
export declare function joinGroupFromWelcome({ welcome, keyPackage, ciphersuiteImpl, clientConfig, }: {
|
|
58
|
+
/** The MLS Welcome message (or a kind 444 Rumor) */
|
|
59
|
+
welcome: Welcome | Rumor;
|
|
60
|
+
/** The full key package (public + private) used to receive the invite */
|
|
61
|
+
keyPackage: {
|
|
62
|
+
publicPackage: KeyPackage;
|
|
63
|
+
privatePackage: PrivateKeyPackage;
|
|
64
|
+
};
|
|
65
|
+
/** The ciphersuite implementation */
|
|
66
|
+
ciphersuiteImpl: CiphersuiteImpl;
|
|
67
|
+
/** ts-mls `ClientConfig` — see the module-level note on config agreement. */
|
|
68
|
+
clientConfig?: ClientConfig;
|
|
69
|
+
}): Promise<ClientState>;
|
|
70
|
+
/**
|
|
71
|
+
* The member leaf that signed a Welcome's GroupInfo — the author of the
|
|
72
|
+
* commit the Welcome belongs to — as ts-mls verified it during the join.
|
|
73
|
+
*/
|
|
74
|
+
export interface GroupInfoSigner {
|
|
75
|
+
/** `GroupInfo.signer`: the signer's leaf index in the joined ratchet tree. */
|
|
76
|
+
readonly leafIndex: number;
|
|
77
|
+
/**
|
|
78
|
+
* That leaf's credential: its Marmot account identity
|
|
79
|
+
* ({@link getCredentialPubkey}). Several leaves of one account share it.
|
|
80
|
+
*/
|
|
81
|
+
readonly credential: Credential;
|
|
82
|
+
/** That leaf's signature key: the key the GroupInfo signature verified under. */
|
|
83
|
+
readonly signaturePublicKey: Uint8Array;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* The `GroupInfo.signer` leaf index when `message` is exactly the input of a
|
|
87
|
+
* GroupInfo signature — `SignContent { label = "MLS 1.0 GroupInfoTBS",
|
|
88
|
+
* content = GroupInfoTBS }` (RFC 9420 §5.1.2, §12.4.3) as ts-mls's
|
|
89
|
+
* `verifyWithLabel` encodes it — and `undefined` for any other input.
|
|
90
|
+
*
|
|
91
|
+
* Matched on the whole layout, never on a length or a prefix: the label
|
|
92
|
+
* bytes, a minimal length prefix whose content runs exactly to the end, and
|
|
93
|
+
* content that decodes as a GroupInfoTBS whose canonical re-encoding
|
|
94
|
+
* reproduces it byte for byte. `groupInfoTBSEncoder` writes `signer` last as
|
|
95
|
+
* a big-endian uint32, so the decoded `signer` is also the content's final
|
|
96
|
+
* four bytes.
|
|
97
|
+
*/
|
|
98
|
+
export declare function groupInfoSignerOfSignatureInput(message: Uint8Array): number | undefined;
|
|
99
|
+
/**
|
|
100
|
+
* {@link joinGroupFromWelcome}, also returning the leaf that signed the
|
|
101
|
+
* Welcome's GroupInfo.
|
|
102
|
+
*
|
|
103
|
+
* ts-mls's `joinGroup` verifies the GroupInfo signature against the signer
|
|
104
|
+
* leaf of the joined tree but returns neither the GroupInfo nor the signer.
|
|
105
|
+
* The join here runs over `ciphersuiteImpl` with `signature.verify` wrapped:
|
|
106
|
+
* every call is passed through unchanged (same arguments, same result) and
|
|
107
|
+
* the one SUCCESSFUL call whose input is a GroupInfo signature
|
|
108
|
+
* ({@link groupInfoSignerOfSignatureInput}) records the signer leaf index
|
|
109
|
+
* and the key it verified under. Every other call — leaf-node, KeyPackage,
|
|
110
|
+
* framed-content signatures — is only passed through.
|
|
111
|
+
*
|
|
112
|
+
* Fails closed: throws when the join did not verify exactly one GroupInfo
|
|
113
|
+
* signature, or when the recorded leaf of the joined tree is not a leaf
|
|
114
|
+
* holding the key the signature verified under. Same join and the same
|
|
115
|
+
* errors as {@link joinGroupFromWelcome} otherwise.
|
|
116
|
+
*/
|
|
117
|
+
export declare function joinGroupFromWelcomeWithSigner(params: {
|
|
118
|
+
/** The MLS Welcome message (or a kind 444 Rumor) */
|
|
119
|
+
welcome: Welcome | Rumor;
|
|
120
|
+
/** The full key package (public + private) used to receive the invite */
|
|
121
|
+
keyPackage: {
|
|
122
|
+
publicPackage: KeyPackage;
|
|
123
|
+
privatePackage: PrivateKeyPackage;
|
|
124
|
+
};
|
|
125
|
+
/** The ciphersuite implementation */
|
|
126
|
+
ciphersuiteImpl: CiphersuiteImpl;
|
|
127
|
+
/** ts-mls `ClientConfig` — see the module-level note on config agreement. */
|
|
128
|
+
clientConfig?: ClientConfig;
|
|
129
|
+
}): Promise<{
|
|
130
|
+
state: ClientState;
|
|
131
|
+
signer: GroupInfoSigner;
|
|
132
|
+
}>;
|
|
133
|
+
/**
|
|
134
|
+
* Reads the {@link MarmotGroupView} from a Welcome message using the provided
|
|
135
|
+
* key package.
|
|
136
|
+
*
|
|
137
|
+
* Convenience wrapper around {@link readWelcomeGroupInfo} — which performs a
|
|
138
|
+
* full ts-mls join and discards everything but the `GroupInfo` — that
|
|
139
|
+
* projects the app-component state from `groupInfo.groupContext.extensions`.
|
|
140
|
+
*
|
|
141
|
+
* @returns The group view, or null if no app components are present
|
|
142
|
+
*/
|
|
143
|
+
export declare function readWelcomeMarmotGroupView({ welcome, keyPackage, ciphersuiteImpl, clientConfig, }: {
|
|
144
|
+
/** The MLS Welcome message (or a kind 444 Rumor) */
|
|
145
|
+
welcome: Welcome | Rumor;
|
|
146
|
+
/** The full key package (public + private) used to receive the invite */
|
|
147
|
+
keyPackage: {
|
|
148
|
+
publicPackage: KeyPackage;
|
|
149
|
+
privatePackage: PrivateKeyPackage;
|
|
150
|
+
};
|
|
151
|
+
/** The ciphersuite implementation */
|
|
152
|
+
ciphersuiteImpl: CiphersuiteImpl;
|
|
153
|
+
/** ts-mls `ClientConfig` — see the module-level note on config agreement. */
|
|
154
|
+
clientConfig?: ClientConfig;
|
|
155
|
+
}): Promise<MarmotGroupView | null>;
|