@kumiai/mls 0.9.0 → 0.10.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +23 -0
- package/lib/anchor.d.ts +10 -4
- package/lib/anchor.js +26 -2
- package/lib/authentication.d.ts +11 -14
- package/lib/authentication.js +127 -86
- package/lib/capability.d.ts +37 -0
- package/lib/capability.js +141 -0
- package/lib/codec.js +2 -1
- package/lib/device-proof.d.ts +1 -0
- package/lib/device-proof.js +8 -12
- package/lib/envelope-fold.d.ts +16 -1
- package/lib/envelope-fold.js +49 -9
- package/lib/errors.d.ts +18 -0
- package/lib/errors.js +33 -0
- package/lib/group-commit.d.ts +22 -35
- package/lib/group-commit.js +122 -103
- package/lib/group-context.d.ts +6 -2
- package/lib/group-context.js +11 -4
- package/lib/group-create.js +56 -11
- package/lib/group-credential.d.ts +3 -1
- package/lib/group-credential.js +17 -3
- package/lib/group-device.js +34 -21
- package/lib/group-handle.d.ts +60 -24
- package/lib/group-handle.js +331 -77
- package/lib/group-info.d.ts +8 -0
- package/lib/group-info.js +29 -1
- package/lib/group-lifecycle.d.ts +32 -0
- package/lib/group-lifecycle.js +300 -0
- package/lib/group-membership.js +7 -3
- package/lib/group-welcome.d.ts +13 -4
- package/lib/group-welcome.js +113 -21
- package/lib/group.d.ts +3 -2
- package/lib/group.js +2 -1
- package/lib/history.d.ts +6 -0
- package/lib/history.js +26 -0
- package/lib/index.d.ts +10 -4
- package/lib/index.js +9 -3
- package/lib/lifecycle-proof.d.ts +24 -0
- package/lib/lifecycle-proof.js +193 -0
- package/lib/lifecycle.d.ts +44 -0
- package/lib/lifecycle.js +385 -0
- package/lib/policy.d.ts +20 -0
- package/lib/policy.js +8 -4
- package/lib/recovery-verdict.d.ts +43 -0
- package/lib/recovery-verdict.js +135 -0
- package/lib/recovery.d.ts +70 -0
- package/lib/recovery.js +110 -36
- package/lib/registry.d.ts +34 -7
- package/lib/registry.js +113 -15
- package/lib/roster.d.ts +1 -1
- package/lib/roster.js +2 -2
- package/lib/types.d.ts +13 -8
- package/lib/welcome-refs.d.ts +2 -1
- package/package.json +6 -6
package/lib/envelope-fold.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { RevokeProofError } from './errors.js';
|
|
1
2
|
import type { FoldInput } from './fold.js';
|
|
2
3
|
import type { VerifiedLedgerEntry } from './ledger.js';
|
|
3
4
|
import { type DeviceRegistry } from './registry.js';
|
|
@@ -18,8 +19,22 @@ export type EnvelopeFoldResult = {
|
|
|
18
19
|
ok: false;
|
|
19
20
|
reason: string;
|
|
20
21
|
entryID: string;
|
|
22
|
+
error?: RevokeProofError;
|
|
21
23
|
};
|
|
22
24
|
export declare const GROUP_TYPE_PREFIX = "kumiai.";
|
|
25
|
+
/** Present for a lifecycle group: consumer entries must come from a controller-bound leaf. */
|
|
26
|
+
export type FoldEnvelopeContext = {
|
|
27
|
+
controllerID: string;
|
|
28
|
+
/** The controller bound to the issuer's pre-commit leaf, if any. */
|
|
29
|
+
memberController: (did: string) => string | undefined;
|
|
30
|
+
};
|
|
31
|
+
export type FoldEnvelopeParams = {
|
|
32
|
+
baseRoster: RosterState;
|
|
33
|
+
baseRegistry: DeviceRegistry;
|
|
34
|
+
entries: Array<FoldInput>;
|
|
35
|
+
groupID: string;
|
|
36
|
+
context?: FoldEnvelopeContext;
|
|
37
|
+
};
|
|
23
38
|
/**
|
|
24
39
|
* Fold an envelope's verified entries against a base roster, enforcing that every
|
|
25
40
|
* entry is admin-authored in state-so-far. Pure, never throws.
|
|
@@ -36,4 +51,4 @@ export declare const GROUP_TYPE_PREFIX = "kumiai.";
|
|
|
36
51
|
* subsumes `kumiai.role`'s own authority rule. State-so-far, not a pre-commit
|
|
37
52
|
* snapshot, so an envelope of `[promote Bob, entry-issued-by-Bob]` is accepted.
|
|
38
53
|
*/
|
|
39
|
-
export declare function foldEnvelope(
|
|
54
|
+
export declare function foldEnvelope(params: FoldEnvelopeParams): EnvelopeFoldResult;
|
package/lib/envelope-fold.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { normalizeDID } from '@kokuin/token';
|
|
2
|
+
import { RevokeProofError } from './errors.js';
|
|
3
|
+
import { authority, DEVICE_ENTRY_TYPE, isDeviceValue, isLifecycleDeviceValue, registryApply } from './registry.js';
|
|
2
4
|
import { adminCount, ROLE_ENTRY_TYPE, roleReducer } from './roster.js';
|
|
3
5
|
export const GROUP_TYPE_PREFIX = 'kumiai.';
|
|
4
6
|
function isRoleValue(value) {
|
|
@@ -19,7 +21,8 @@ function isRoleValue(value) {
|
|
|
19
21
|
* entry's issuer must be an admin at its own position — the universal invariant that
|
|
20
22
|
* subsumes `kumiai.role`'s own authority rule. State-so-far, not a pre-commit
|
|
21
23
|
* snapshot, so an envelope of `[promote Bob, entry-issued-by-Bob]` is accepted.
|
|
22
|
-
*/ export function foldEnvelope(
|
|
24
|
+
*/ export function foldEnvelope(params) {
|
|
25
|
+
const { baseRoster, baseRegistry, entries, groupID, context } = params;
|
|
23
26
|
let workingRoster = {
|
|
24
27
|
roles: new Map(baseRoster.roles)
|
|
25
28
|
};
|
|
@@ -50,13 +53,50 @@ function isRoleValue(value) {
|
|
|
50
53
|
};
|
|
51
54
|
}
|
|
52
55
|
const value = entry.value;
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}
|
|
59
|
-
}
|
|
56
|
+
if (context != null ? !isLifecycleDeviceValue(value) : value.op === 'reset' || value.op === 'clock' || value.proof !== undefined) {
|
|
57
|
+
return {
|
|
58
|
+
ok: false,
|
|
59
|
+
reason: 'device operation is not allowed in this group',
|
|
60
|
+
entryID
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
try {
|
|
64
|
+
workingRegistry = registryApply({
|
|
65
|
+
issuer,
|
|
66
|
+
entry: {
|
|
67
|
+
...entry,
|
|
68
|
+
value
|
|
69
|
+
}
|
|
70
|
+
}, workingRegistry, context?.controllerID);
|
|
71
|
+
} catch (error) {
|
|
72
|
+
return {
|
|
73
|
+
ok: false,
|
|
74
|
+
reason: 'invalid lifecycle proof',
|
|
75
|
+
entryID,
|
|
76
|
+
...error instanceof RevokeProofError ? {
|
|
77
|
+
error
|
|
78
|
+
} : {}
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
if (context != null) {
|
|
84
|
+
if (entry.type.startsWith(GROUP_TYPE_PREFIX)) {
|
|
85
|
+
return {
|
|
86
|
+
ok: false,
|
|
87
|
+
reason: 'reserved lifecycle entry type',
|
|
88
|
+
entryID
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
const bound = context.memberController(normalizeDID(issuer));
|
|
92
|
+
if (bound == null || normalizeDID(bound) !== normalizeDID(context.controllerID)) {
|
|
93
|
+
return {
|
|
94
|
+
ok: false,
|
|
95
|
+
reason: 'issuer has no pre-commit controller-bound leaf',
|
|
96
|
+
entryID
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
surfaced.push(verified);
|
|
60
100
|
continue;
|
|
61
101
|
}
|
|
62
102
|
// The universal invariant, now authority-aware: the issuer's AUTHORITY (controller ?? id,
|
package/lib/errors.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export type LeafBindingReason = 'issuer-mismatch' | 'audience-mismatch' | 'permission-denied' | 'signature-invalid' | 'confirmation-invalid' | 'subject-mismatch' | 'chain-depth' | 'self-issued' | 'child-outlives-parent' | 'denied-issuer' | 'denied-id' | 'renewal-order' | 'lifetime-cap' | 'generation-floor' | 'identity-change' | 'controller-mismatch' | 'floating-refused' | 'history-horizon';
|
|
2
|
+
export declare class LeafBindingError extends Error {
|
|
3
|
+
#private;
|
|
4
|
+
constructor(reason: LeafBindingReason);
|
|
5
|
+
get reason(): LeafBindingReason;
|
|
6
|
+
}
|
|
7
|
+
export type LeafLapsedReason = 'lapsed';
|
|
8
|
+
export declare class LeafLapsedError extends Error {
|
|
9
|
+
#private;
|
|
10
|
+
constructor(reason: LeafLapsedReason);
|
|
11
|
+
get reason(): LeafLapsedReason;
|
|
12
|
+
}
|
|
13
|
+
export type RevokeProofReason = 'no-rev' | 'wrong-controller' | 'not-authority-signed' | 'generation-floor' | 'too-large' | 'detached' | 'needs-reset' | 'time-regression' | 'effects-mismatch' | 'removes-mismatch';
|
|
14
|
+
export declare class RevokeProofError extends Error {
|
|
15
|
+
#private;
|
|
16
|
+
constructor(reason: RevokeProofReason);
|
|
17
|
+
get reason(): RevokeProofReason;
|
|
18
|
+
}
|
package/lib/errors.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export class LeafBindingError extends Error {
|
|
2
|
+
#reason;
|
|
3
|
+
constructor(reason){
|
|
4
|
+
super(`Invalid leaf binding: ${reason}`);
|
|
5
|
+
this.name = 'LeafBindingError';
|
|
6
|
+
this.#reason = reason;
|
|
7
|
+
}
|
|
8
|
+
get reason() {
|
|
9
|
+
return this.#reason;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export class LeafLapsedError extends Error {
|
|
13
|
+
#reason;
|
|
14
|
+
constructor(reason){
|
|
15
|
+
super(`Leaf lapsed: ${reason}`);
|
|
16
|
+
this.name = 'LeafLapsedError';
|
|
17
|
+
this.#reason = reason;
|
|
18
|
+
}
|
|
19
|
+
get reason() {
|
|
20
|
+
return this.#reason;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
export class RevokeProofError extends Error {
|
|
24
|
+
#reason;
|
|
25
|
+
constructor(reason){
|
|
26
|
+
super(`Invalid revoke proof: ${reason}`);
|
|
27
|
+
this.name = 'RevokeProofError';
|
|
28
|
+
this.#reason = reason;
|
|
29
|
+
}
|
|
30
|
+
get reason() {
|
|
31
|
+
return this.#reason;
|
|
32
|
+
}
|
|
33
|
+
}
|
package/lib/group-commit.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type SigningIdentity } from '@kokuin/token';
|
|
2
|
-
import { createCommit, type DefaultProposal, type KeyPackage } from 'ts-mls';
|
|
2
|
+
import { type ClientState, createCommit, type DefaultProposal, type KeyPackage } from 'ts-mls';
|
|
3
3
|
import { type GroupHandle } from './group-handle.js';
|
|
4
4
|
import { type GroupPermission } from './roster.js';
|
|
5
5
|
import type { Invite } from './types.js';
|
|
@@ -8,30 +8,12 @@ export type InviteRecipientMismatchErrorParams = {
|
|
|
8
8
|
expectedDID: string;
|
|
9
9
|
actualDID: string;
|
|
10
10
|
};
|
|
11
|
-
/**
|
|
12
|
-
* Thrown by {@link commitInvite} when the supplied key package's credential DID is not the
|
|
13
|
-
* identity the invite's enacted role entry grants a role to.
|
|
14
|
-
*
|
|
15
|
-
* Distinct from an ordinary rejection on purpose: the expected trigger is a key-package store
|
|
16
|
-
* that served the wrong owner's package, and adding the package anyway would put a different
|
|
17
|
-
* identity in the group than the one the roster grants the role to. A host should alert on this.
|
|
18
|
-
*
|
|
19
|
-
* The same error also fires for any invite whose trailing `kumiai.role` entry names someone
|
|
20
|
-
* other than the intended invitee — hand-built, tampered, or reordered — with an otherwise
|
|
21
|
-
* honest key package. A host seeing this should not assume a store compromise on that basis
|
|
22
|
-
* alone.
|
|
23
|
-
*
|
|
24
|
-
* The receive-side add rule in {@link defaultCommitPolicy} does NOT close this residual, though it
|
|
25
|
-
* is easy to assume it would. That rule rejects an Add whose DID the candidate roster grants
|
|
26
|
-
* nothing; in the reordered case the trailing grant's subject IS granted, so the Add passes and the
|
|
27
|
-
* intended invitee is left holding a grant it never joined against. Closing it needs this binding to
|
|
28
|
-
* seek the enacted entry matching the key package's DID rather than reading the last one.
|
|
29
|
-
*/
|
|
11
|
+
/** The supplied key package names a different identity from the invite's recipient. */
|
|
30
12
|
export declare class InviteRecipientMismatchError extends Error {
|
|
31
13
|
#private;
|
|
32
14
|
constructor(params: InviteRecipientMismatchErrorParams);
|
|
33
15
|
get groupID(): string;
|
|
34
|
-
/** DID the invite
|
|
16
|
+
/** DID the invite names. */
|
|
35
17
|
get expectedDID(): string;
|
|
36
18
|
/** DID the supplied key package presents. */
|
|
37
19
|
get actualDID(): string;
|
|
@@ -40,20 +22,28 @@ export type CreateInviteParams = {
|
|
|
40
22
|
group: GroupHandle;
|
|
41
23
|
identity: SigningIdentity;
|
|
42
24
|
recipientDID: string;
|
|
25
|
+
} & ({
|
|
43
26
|
permission: GroupPermission;
|
|
44
|
-
|
|
27
|
+
entries?: never;
|
|
28
|
+
} | {
|
|
29
|
+
permission?: never;
|
|
30
|
+
entries?: Array<string>;
|
|
31
|
+
});
|
|
45
32
|
export type CreateInviteResult = {
|
|
46
33
|
invite: Invite;
|
|
47
34
|
};
|
|
48
|
-
/**
|
|
49
|
-
* Create an invite for a new member. Does NOT add them — call commitInvite with
|
|
50
|
-
* their key package for that.
|
|
51
|
-
*
|
|
52
|
-
* Only an admin may invite: a role entry from a non-admin issuer is dropped by every
|
|
53
|
-
* receiver's fold, so refusing here turns a silent downstream rejection into a local
|
|
54
|
-
* error.
|
|
55
|
-
*/
|
|
35
|
+
/** Create an invite without adding its recipient; commitInvite enacts the admission. */
|
|
56
36
|
export declare function createInvite(params: CreateInviteParams): Promise<CreateInviteResult>;
|
|
37
|
+
export type CommitWithEntriesParams = {
|
|
38
|
+
group: GroupHandle;
|
|
39
|
+
extraProposals: Array<DefaultProposal>;
|
|
40
|
+
enacted: Array<string>;
|
|
41
|
+
ratchetTreeExtension?: boolean;
|
|
42
|
+
/** Defaults to true; device and lifecycle entries carry their own authority. */
|
|
43
|
+
requireAdmin?: boolean;
|
|
44
|
+
/** State to commit from instead of the live one, e.g. a tree with a renewed own leaf. */
|
|
45
|
+
commitState?: ClientState;
|
|
46
|
+
};
|
|
57
47
|
/**
|
|
58
48
|
* The one place a commit carrying control-ledger entries is built: `commitInvite`,
|
|
59
49
|
* `removeMember`, and `commitLedgerEntries` all route through it, so envelope and
|
|
@@ -73,10 +63,7 @@ export declare function createInvite(params: CreateInviteParams): Promise<Create
|
|
|
73
63
|
* proposal advancing the head by exactly those ids, in envelope order. An empty list
|
|
74
64
|
* moves no head and carries no envelope.
|
|
75
65
|
*/
|
|
76
|
-
export declare function commitWithEntries(
|
|
77
|
-
ratchetTreeExtension?: boolean;
|
|
78
|
-
requireAdmin?: boolean;
|
|
79
|
-
}): Promise<Awaited<ReturnType<typeof createCommit>>>;
|
|
66
|
+
export declare function commitWithEntries(params: CommitWithEntriesParams): Promise<Awaited<ReturnType<typeof createCommit>>>;
|
|
80
67
|
export type CommitLedgerEntriesResult = {
|
|
81
68
|
/** Framed MLSMessage bytes. Broadcast to existing members via the DS. */
|
|
82
69
|
commitMessage: Uint8Array;
|
|
@@ -119,7 +106,7 @@ export type CommitInviteResult = {
|
|
|
119
106
|
*
|
|
120
107
|
* The invite's ledger entries are enacted here: their content ids ride the commit's
|
|
121
108
|
* control envelope and advance the head by exactly those ids, so every receiver folds
|
|
122
|
-
* the
|
|
109
|
+
* the appended entries as it applies the Add. The envelope carries ids, not
|
|
123
110
|
* bodies — a receiver holding neither the entry nor a `resolveLedgerEntries` resolver
|
|
124
111
|
* throws MissingLedgerEntriesError.
|
|
125
112
|
*
|
package/lib/group-commit.js
CHANGED
|
@@ -5,36 +5,19 @@ import { parseMLSCredentialIdentity } from './credential.js';
|
|
|
5
5
|
import { verifyDeviceEntry } from './device-proof.js';
|
|
6
6
|
import { encodeControlEnvelope } from './envelope.js';
|
|
7
7
|
import { foldEnvelope } from './envelope-fold.js';
|
|
8
|
+
import { assertBindingAuthorTime } from './group-credential.js';
|
|
8
9
|
import { buildCommitPolicyContext, deriveGroup, mutexFor } from './group-handle.js';
|
|
9
10
|
import { buildLedgerHeadExtension, extendHead, readLedgerHead } from './head.js';
|
|
10
11
|
import { ledgerEntryDigest, signLedgerEntry, verifyLedgerEntry } from './ledger.js';
|
|
11
|
-
import {
|
|
12
|
+
import { prepareLifecycleGate, validateEntry } from './lifecycle.js';
|
|
12
13
|
import { authority, controllerOf, DEVICE_ENTRY_TYPE } from './registry.js';
|
|
13
14
|
import { ROLE_ENTRY_TYPE } from './roster.js';
|
|
14
|
-
/**
|
|
15
|
-
* Thrown by {@link commitInvite} when the supplied key package's credential DID is not the
|
|
16
|
-
* identity the invite's enacted role entry grants a role to.
|
|
17
|
-
*
|
|
18
|
-
* Distinct from an ordinary rejection on purpose: the expected trigger is a key-package store
|
|
19
|
-
* that served the wrong owner's package, and adding the package anyway would put a different
|
|
20
|
-
* identity in the group than the one the roster grants the role to. A host should alert on this.
|
|
21
|
-
*
|
|
22
|
-
* The same error also fires for any invite whose trailing `kumiai.role` entry names someone
|
|
23
|
-
* other than the intended invitee — hand-built, tampered, or reordered — with an otherwise
|
|
24
|
-
* honest key package. A host seeing this should not assume a store compromise on that basis
|
|
25
|
-
* alone.
|
|
26
|
-
*
|
|
27
|
-
* The receive-side add rule in {@link defaultCommitPolicy} does NOT close this residual, though it
|
|
28
|
-
* is easy to assume it would. That rule rejects an Add whose DID the candidate roster grants
|
|
29
|
-
* nothing; in the reordered case the trailing grant's subject IS granted, so the Add passes and the
|
|
30
|
-
* intended invitee is left holding a grant it never joined against. Closing it needs this binding to
|
|
31
|
-
* seek the enacted entry matching the key package's DID rather than reading the last one.
|
|
32
|
-
*/ export class InviteRecipientMismatchError extends Error {
|
|
15
|
+
/** The supplied key package names a different identity from the invite's recipient. */ export class InviteRecipientMismatchError extends Error {
|
|
33
16
|
#groupID;
|
|
34
17
|
#expectedDID;
|
|
35
18
|
#actualDID;
|
|
36
19
|
constructor(params){
|
|
37
|
-
super(`commitInvite: the key package presents ${params.actualDID}, but the invite
|
|
20
|
+
super(`commitInvite: the key package presents ${params.actualDID}, but the invite names recipient ${params.expectedDID}`);
|
|
38
21
|
this.name = 'InviteRecipientMismatchError';
|
|
39
22
|
this.#groupID = params.groupID;
|
|
40
23
|
this.#expectedDID = params.expectedDID;
|
|
@@ -43,49 +26,50 @@ import { ROLE_ENTRY_TYPE } from './roster.js';
|
|
|
43
26
|
get groupID() {
|
|
44
27
|
return this.#groupID;
|
|
45
28
|
}
|
|
46
|
-
/** DID the invite
|
|
29
|
+
/** DID the invite names. */ get expectedDID() {
|
|
47
30
|
return this.#expectedDID;
|
|
48
31
|
}
|
|
49
32
|
/** DID the supplied key package presents. */ get actualDID() {
|
|
50
33
|
return this.#actualDID;
|
|
51
34
|
}
|
|
52
35
|
}
|
|
53
|
-
/**
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
36
|
+
/** Create an invite without adding its recipient; commitInvite enacts the admission. */ export async function createInvite(params) {
|
|
37
|
+
const { group, identity, recipientDID } = params;
|
|
38
|
+
let appended;
|
|
39
|
+
if (group.anchor.controller != null) {
|
|
40
|
+
if (params.permission != null) throw new Error('createInvite: lifecycle invites have no permission');
|
|
41
|
+
if (group.findMemberLeafIndex(identity.id) == null) {
|
|
42
|
+
throw new Error('createInvite: the inviter must hold a leaf');
|
|
43
|
+
}
|
|
44
|
+
appended = params.entries ?? [];
|
|
45
|
+
for (const token of appended){
|
|
46
|
+
const verified = await verifyLedgerEntry(token);
|
|
47
|
+
if (verified == null || verified.entry.groupID !== group.groupID || verified.entry.type.startsWith('kumiai.') || normalizeDID(verified.issuer) !== normalizeDID(identity.id)) throw new Error('createInvite: entries must be consumer entries signed by the inviter');
|
|
48
|
+
}
|
|
49
|
+
} else {
|
|
50
|
+
if (params.permission == null) throw new Error('createInvite: standard invites require permission');
|
|
51
|
+
if (group.roster.roles.get(authority(group.registry, identity.id)) !== 'admin') {
|
|
52
|
+
throw new Error('createInvite: the inviter must be an admin in the group roster');
|
|
53
|
+
}
|
|
54
|
+
appended = [
|
|
55
|
+
await signLedgerEntry(identity, {
|
|
56
|
+
type: ROLE_ENTRY_TYPE,
|
|
57
|
+
groupID: group.groupID,
|
|
58
|
+
subject: recipientDID,
|
|
59
|
+
value: params.permission
|
|
60
|
+
})
|
|
61
|
+
];
|
|
64
62
|
}
|
|
65
|
-
// The role entry naming the invitee. Its issuer is the inviter (authenticated by
|
|
66
|
-
// the token signature) and its value is the permission granted.
|
|
67
|
-
const roleToken = await signLedgerEntry(identity, {
|
|
68
|
-
type: ROLE_ENTRY_TYPE,
|
|
69
|
-
groupID: group.groupID,
|
|
70
|
-
subject: recipientDID,
|
|
71
|
-
value: permission
|
|
72
|
-
});
|
|
73
|
-
const invite = {
|
|
74
|
-
groupID: group.groupID,
|
|
75
|
-
inviterID: identity.id,
|
|
76
|
-
// The whole log, new role entry last: a joiner handed only its own entry would
|
|
77
|
-
// never learn of earlier role changes and would reject every commit by an admin
|
|
78
|
-
// promoted since — a permanent fork nothing re-sends. The new entry must fold
|
|
79
|
-
// after the history it depends on, hence last. Re-granting a role the log already
|
|
80
|
-
// carries appends it again (a legal re-enactment). The joiner still folds from the
|
|
81
|
-
// anchor, so padding this list cannot promote anyone.
|
|
82
|
-
ledgerEntries: [
|
|
83
|
-
...group.ledgerTokens,
|
|
84
|
-
roleToken
|
|
85
|
-
]
|
|
86
|
-
};
|
|
87
63
|
return {
|
|
88
|
-
invite
|
|
64
|
+
invite: {
|
|
65
|
+
groupID: group.groupID,
|
|
66
|
+
inviterID: identity.id,
|
|
67
|
+
recipientDID,
|
|
68
|
+
ledgerEntries: [
|
|
69
|
+
...group.ledgerTokens,
|
|
70
|
+
...appended
|
|
71
|
+
]
|
|
72
|
+
}
|
|
89
73
|
};
|
|
90
74
|
}
|
|
91
75
|
/**
|
|
@@ -120,14 +104,18 @@ import { ROLE_ENTRY_TYPE } from './roster.js';
|
|
|
120
104
|
* When `enacted` is non-empty the commit also carries a group-context-extensions
|
|
121
105
|
* proposal advancing the head by exactly those ids, in envelope order. An empty list
|
|
122
106
|
* moves no head and carries no envelope.
|
|
123
|
-
*/ export async function commitWithEntries(
|
|
124
|
-
const
|
|
125
|
-
|
|
107
|
+
*/ export async function commitWithEntries(params) {
|
|
108
|
+
const { group, extraProposals, enacted } = params;
|
|
109
|
+
if (group.anchor.controller != null && group.findMemberLeafIndex(group.credential.id) == null) {
|
|
110
|
+
throw new Error('the committer must hold a leaf');
|
|
111
|
+
}
|
|
112
|
+
const ratchetTreeExtension = params.ratchetTreeExtension ?? false;
|
|
113
|
+
const requireAdmin = params.requireAdmin ?? true;
|
|
126
114
|
// Same reason createInvite guards the inviter: a non-admin's commit is rejected by
|
|
127
115
|
// every receiver, so fail here rather than emitting a commit nobody will apply.
|
|
128
116
|
// Authority-aware: a device of an admin profile commits as that profile. Device-only commits
|
|
129
117
|
// (register/add/revoke/label) are authorized by proofs, not a role, so they pass requireAdmin:false.
|
|
130
|
-
if (requireAdmin && group.roster.roles.get(authority(group.registry, group.credential.id)) !== 'admin') {
|
|
118
|
+
if (requireAdmin && group.anchor.controller == null && group.roster.roles.get(authority(group.registry, group.credential.id)) !== 'admin') {
|
|
131
119
|
throw new Error('the committer must be an admin in the group roster');
|
|
132
120
|
}
|
|
133
121
|
// Fold the entries exactly as every receiver will; refuse to author a commit the
|
|
@@ -147,8 +135,18 @@ import { ROLE_ENTRY_TYPE } from './roster.js';
|
|
|
147
135
|
entryID: ledgerEntryDigest(token)
|
|
148
136
|
});
|
|
149
137
|
}
|
|
150
|
-
const fold = foldEnvelope(
|
|
138
|
+
const fold = foldEnvelope({
|
|
139
|
+
baseRoster: group.roster,
|
|
140
|
+
baseRegistry: group.registry,
|
|
141
|
+
entries: inputs,
|
|
142
|
+
groupID: group.groupID,
|
|
143
|
+
context: group.anchor.controller == null ? undefined : {
|
|
144
|
+
controllerID: group.anchor.controller,
|
|
145
|
+
memberController: (did)=>group.bindingOfDID(did)?.controller
|
|
146
|
+
}
|
|
147
|
+
});
|
|
151
148
|
if (!fold.ok) {
|
|
149
|
+
if (fold.error != null) throw fold.error;
|
|
152
150
|
throw new Error(`cannot enact ledger entry ${fold.entryID}: ${fold.reason}`);
|
|
153
151
|
}
|
|
154
152
|
// Same reason as the fold guard above: verify device entries against THIS group's tree and
|
|
@@ -159,39 +157,28 @@ import { ROLE_ENTRY_TYPE } from './roster.js';
|
|
|
159
157
|
};
|
|
160
158
|
for (const input of inputs){
|
|
161
159
|
if (input.verified.entry.type !== DEVICE_ENTRY_TYPE) continue;
|
|
160
|
+
if (group.anchor.controller != null && input.verified.entry.value.op !== 'beacon') {
|
|
161
|
+
continue;
|
|
162
|
+
}
|
|
162
163
|
const ok = await verifyDeviceEntry(input.verified, proofCtx);
|
|
163
164
|
if (!ok) {
|
|
164
165
|
throw new Error(`cannot enact device entry ${input.entryID}: proof verification failed`);
|
|
165
166
|
}
|
|
166
167
|
}
|
|
167
168
|
const entryIDs = enacted.map(ledgerEntryDigest);
|
|
168
|
-
// Filter the pending-proposal set the committer would otherwise absorb: ts-mls folds
|
|
169
|
-
// every unappliedProposal into the commit, so a non-admin's pending proposal would
|
|
170
|
-
// ride it and every peer would reject the whole thing — one member could stall the
|
|
171
|
-
// group. Judge each against the same defaultCommitPolicy and context receivers build,
|
|
172
|
-
// dropping any the group would reject.
|
|
173
169
|
const enactedDeviceEntries = inputs.filter((i)=>i.verified.entry.type === DEVICE_ENTRY_TYPE).map((i)=>({
|
|
174
170
|
subject: normalizeDID(i.verified.entry.subject),
|
|
175
171
|
op: i.verified.entry.value.op
|
|
176
172
|
}));
|
|
177
|
-
const
|
|
173
|
+
const gateContext = buildCommitPolicyContext(group, {
|
|
178
174
|
baseRoster: group.roster,
|
|
179
175
|
candidateRoster: fold.roster,
|
|
180
176
|
entryIDs,
|
|
181
177
|
enactedDeviceEntries
|
|
182
178
|
});
|
|
183
|
-
const keptPending = {};
|
|
184
|
-
for (const [ref, pws] of Object.entries(group.state.unappliedProposals)){
|
|
185
|
-
if (defaultCommitPolicy({
|
|
186
|
-
kind: 'proposal',
|
|
187
|
-
proposal: pws
|
|
188
|
-
}, filterContext) !== 'reject') {
|
|
189
|
-
keptPending[ref] = pws;
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
179
|
const commitState = {
|
|
193
|
-
...group.state,
|
|
194
|
-
unappliedProposals:
|
|
180
|
+
...params.commitState ?? group.state,
|
|
181
|
+
unappliedProposals: {}
|
|
195
182
|
};
|
|
196
183
|
const proposals = [
|
|
197
184
|
...extraProposals
|
|
@@ -204,7 +191,31 @@ import { ROLE_ENTRY_TYPE } from './roster.js';
|
|
|
204
191
|
}
|
|
205
192
|
});
|
|
206
193
|
}
|
|
207
|
-
|
|
194
|
+
const gate = await prepareLifecycleGate({
|
|
195
|
+
group,
|
|
196
|
+
entries: inputs.map(({ verified })=>verified),
|
|
197
|
+
candidateRegistry: fold.registry,
|
|
198
|
+
context: gateContext
|
|
199
|
+
});
|
|
200
|
+
const incoming = {
|
|
201
|
+
kind: 'commit',
|
|
202
|
+
senderLeafIndex: group.state.privatePath.leafIndex,
|
|
203
|
+
proposals: proposals.map((proposal)=>({
|
|
204
|
+
proposal,
|
|
205
|
+
senderLeafIndex: group.state.privatePath.leafIndex
|
|
206
|
+
}))
|
|
207
|
+
};
|
|
208
|
+
gate.check(incoming);
|
|
209
|
+
for (const proposal of proposals){
|
|
210
|
+
if (proposal.proposalType === defaultProposalTypes.add) {
|
|
211
|
+
const leaf = proposal.add.keyPackage.leafNode;
|
|
212
|
+
if (isDefaultCredential(leaf.credential) && leaf.credential.credentialType === defaultCredentialTypes.basic) {
|
|
213
|
+
assertBindingAuthorTime(parseMLSCredentialIdentity(leaf.credential.identity).controller);
|
|
214
|
+
}
|
|
215
|
+
await validateEntry(group, leaf);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
const result = await createCommit({
|
|
208
219
|
context: group.context,
|
|
209
220
|
state: commitState,
|
|
210
221
|
extraProposals: proposals,
|
|
@@ -218,6 +229,8 @@ import { ROLE_ENTRY_TYPE } from './roster.js';
|
|
|
218
229
|
})
|
|
219
230
|
}
|
|
220
231
|
});
|
|
232
|
+
await gate.postApply(result.newState);
|
|
233
|
+
return result;
|
|
221
234
|
}
|
|
222
235
|
/**
|
|
223
236
|
* The entries an invite adds beyond the committer's own log: everything past the
|
|
@@ -256,7 +269,11 @@ import { ROLE_ENTRY_TYPE } from './roster.js';
|
|
|
256
269
|
if (tokens.length === 0) {
|
|
257
270
|
throw new Error('commitLedgerEntries: no ledger entries to commit');
|
|
258
271
|
}
|
|
259
|
-
const result = await commitWithEntries(
|
|
272
|
+
const result = await commitWithEntries({
|
|
273
|
+
group,
|
|
274
|
+
extraProposals: [],
|
|
275
|
+
enacted: tokens
|
|
276
|
+
});
|
|
260
277
|
const newGroup = deriveGroup(group, result.newState);
|
|
261
278
|
await newGroup.applyLedgerEntries(tokens);
|
|
262
279
|
return {
|
|
@@ -272,7 +289,7 @@ import { ROLE_ENTRY_TYPE } from './roster.js';
|
|
|
272
289
|
*
|
|
273
290
|
* The invite's ledger entries are enacted here: their content ids ride the commit's
|
|
274
291
|
* control envelope and advance the head by exactly those ids, so every receiver folds
|
|
275
|
-
* the
|
|
292
|
+
* the appended entries as it applies the Add. The envelope carries ids, not
|
|
276
293
|
* bodies — a receiver holding neither the entry nor a `resolveLedgerEntries` resolver
|
|
277
294
|
* throws MissingLedgerEntriesError.
|
|
278
295
|
*
|
|
@@ -291,27 +308,26 @@ import { ROLE_ENTRY_TYPE } from './roster.js';
|
|
|
291
308
|
throw new Error(`commitInvite: invite is for group ${invite.groupID}, not ${group.groupID}`);
|
|
292
309
|
}
|
|
293
310
|
const enacted = entriesAddedByInvite(group, invite);
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
311
|
+
const expectedDID = normalizeDID(invite.recipientDID);
|
|
312
|
+
if (group.anchor.controller == null) {
|
|
313
|
+
let grantedTo = null;
|
|
314
|
+
for (const token of enacted){
|
|
315
|
+
const verified = await verifyLedgerEntry(token);
|
|
316
|
+
if (verified?.entry.type === ROLE_ENTRY_TYPE && verified.entry.groupID === group.groupID) {
|
|
317
|
+
grantedTo = verified.entry.subject;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
if (grantedTo == null) {
|
|
321
|
+
throw new Error(`commitInvite: the invite enacts no ${ROLE_ENTRY_TYPE} entry for this group`);
|
|
322
|
+
}
|
|
323
|
+
if (normalizeDID(grantedTo) !== expectedDID) {
|
|
324
|
+
throw new InviteRecipientMismatchError({
|
|
325
|
+
groupID: group.groupID,
|
|
326
|
+
expectedDID,
|
|
327
|
+
actualDID: normalizeDID(grantedTo)
|
|
328
|
+
});
|
|
309
329
|
}
|
|
310
330
|
}
|
|
311
|
-
if (grantedTo == null) {
|
|
312
|
-
throw new Error(`commitInvite: the invite enacts no ${ROLE_ENTRY_TYPE} entry for this group, so there is no recipient to bind the key package to`);
|
|
313
|
-
}
|
|
314
|
-
const expectedDID = normalizeDID(grantedTo);
|
|
315
331
|
// `credentialType !== basic` does not narrow on its own: CredentialCustom.credentialType is a
|
|
316
332
|
// bare `number`, so the compiler cannot rule it out. ts-mls's own guard can.
|
|
317
333
|
//
|
|
@@ -338,9 +354,12 @@ import { ROLE_ENTRY_TYPE } from './roster.js';
|
|
|
338
354
|
keyPackage
|
|
339
355
|
}
|
|
340
356
|
};
|
|
341
|
-
const result = await commitWithEntries(
|
|
342
|
-
|
|
343
|
-
|
|
357
|
+
const result = await commitWithEntries({
|
|
358
|
+
group,
|
|
359
|
+
extraProposals: [
|
|
360
|
+
addProposal
|
|
361
|
+
],
|
|
362
|
+
enacted,
|
|
344
363
|
ratchetTreeExtension: true
|
|
345
364
|
});
|
|
346
365
|
const newGroup = deriveGroup(group, result.newState);
|
package/lib/group-context.d.ts
CHANGED
|
@@ -1,12 +1,16 @@
|
|
|
1
1
|
import { type Capabilities, type GroupContextExtension, type MlsContext } from 'ts-mls';
|
|
2
|
+
import type { GroupAnchor } from './anchor.js';
|
|
2
3
|
import type { GroupOptions } from './types.js';
|
|
3
4
|
/** Late-bound provider of a context's device deny set, pointed at the live handle after construction. */
|
|
4
5
|
export type DeviceDenyHolder = {
|
|
5
6
|
provider: () => ReadonlySet<string>;
|
|
7
|
+
leafLifetime?: number;
|
|
8
|
+
trustedGrantLifetime?: number;
|
|
6
9
|
};
|
|
7
|
-
/** The deny holder for
|
|
10
|
+
/** The deny holder for an internally built context, or undefined for others. */
|
|
8
11
|
export declare function deviceDenyHolderFor(context: MlsContext): DeviceDenyHolder | undefined;
|
|
9
|
-
export declare function resolveMlsContext(options?: GroupOptions): Promise<MlsContext>;
|
|
12
|
+
export declare function resolveMlsContext(options?: GroupOptions, anchor?: GroupAnchor): Promise<MlsContext>;
|
|
13
|
+
export declare function createMlsContext(cipherSuite: MlsContext['cipherSuite'], anchor?: GroupAnchor): MlsContext;
|
|
10
14
|
/**
|
|
11
15
|
* Build the leaf-node capabilities for a member joining or creating a group. RFC
|
|
12
16
|
* 9420 requires a leaf to advertise every non-default GroupContext extension type
|
package/lib/group-context.js
CHANGED
|
@@ -4,19 +4,26 @@ import { nobleCryptoProvider } from './crypto.js';
|
|
|
4
4
|
const DEFAULT_CIPHERSUITE = 'MLS_128_DHKEMX25519_AES128GCM_SHA256_Ed25519';
|
|
5
5
|
const EMPTY_DENY = new Set();
|
|
6
6
|
const DENY_HOLDERS = new WeakMap();
|
|
7
|
-
/** The deny holder for
|
|
7
|
+
/** The deny holder for an internally built context, or undefined for others. */ export function deviceDenyHolderFor(context) {
|
|
8
8
|
return DENY_HOLDERS.get(context);
|
|
9
9
|
}
|
|
10
|
-
export async function resolveMlsContext(options) {
|
|
10
|
+
export async function resolveMlsContext(options, anchor) {
|
|
11
11
|
const name = options?.ciphersuiteName ?? DEFAULT_CIPHERSUITE;
|
|
12
12
|
const cipherSuite = await getCiphersuiteImpl(name, options?.cryptoProvider ?? nobleCryptoProvider);
|
|
13
|
+
return createMlsContext(cipherSuite, anchor);
|
|
14
|
+
}
|
|
15
|
+
export function createMlsContext(cipherSuite, anchor) {
|
|
13
16
|
// The deny set cannot be known here — the handle that folds it does not exist yet. Bind a mutable
|
|
14
17
|
// holder the auth service reads; the GroupHandle constructor points it at its own currentDenySet().
|
|
15
18
|
const holder = {
|
|
16
|
-
provider: ()=>EMPTY_DENY
|
|
19
|
+
provider: ()=>EMPTY_DENY,
|
|
20
|
+
leafLifetime: anchor?.controller == null ? undefined : anchor.leafLifetime ?? 86_400,
|
|
21
|
+
trustedGrantLifetime: anchor?.controller == null ? undefined : anchor.trustedGrantLifetime ?? 2_592_000
|
|
17
22
|
};
|
|
18
23
|
const authService = createDIDAuthenticationService({
|
|
19
|
-
deviceDenySet: ()=>holder.provider()
|
|
24
|
+
deviceDenySet: ()=>holder.provider(),
|
|
25
|
+
leafLifetime: ()=>holder.leafLifetime,
|
|
26
|
+
trustedGrantLifetime: ()=>holder.trustedGrantLifetime
|
|
20
27
|
});
|
|
21
28
|
const context = {
|
|
22
29
|
cipherSuite,
|