@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/group.js
CHANGED
|
@@ -3,6 +3,7 @@ export { createGroup, restoreGroup } from './group-create.js';
|
|
|
3
3
|
export { createKeyPackageBundle, createLastResortKeyPackageBundle, LAST_RESORT_EXTENSION_TYPE, LAST_RESORT_LIFETIME_DAYS, makeMLSCredential, ORDINARY_KEY_PACKAGE_LIFETIME_DAYS } from './group-credential.js';
|
|
4
4
|
export { addDevice, announceControllerBeacon, labelDevice, registerDevice, revokeDevice } from './group-device.js';
|
|
5
5
|
export { CommitRejectedError, GroupHandle } from './group-handle.js';
|
|
6
|
-
export { exportGroupInfo, inspectGroupInfo, readGroupInfoBinding, readMessageEpoch } from './group-info.js';
|
|
6
|
+
export { exportGroupInfo, inspectGroupInfo, readCommitEntryIDs, readGroupInfoBinding, readMessageAAD, readMessageEpoch } from './group-info.js';
|
|
7
|
+
export { removeLapsedLeaves, renewLeaf, revokeWithProof } from './group-lifecycle.js';
|
|
7
8
|
export { removeMember } from './group-membership.js';
|
|
8
9
|
export { joinGroupExternal, processWelcome, processWelcomeOnce } from './group-welcome.js';
|
package/lib/history.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { type ClientState } from 'ts-mls';
|
|
2
|
+
import type { VerifiedLedgerEntry } from './ledger.js';
|
|
3
|
+
export declare const HISTORY_HORIZON = 393216;
|
|
4
|
+
export declare function controllerLogSize(events: ReadonlyArray<unknown>): number;
|
|
5
|
+
/** Count validated controller history. Callers apply the horizon only to lifecycle groups. */
|
|
6
|
+
export declare function historySize(tree: ClientState['ratchetTree'], entries: ReadonlyArray<VerifiedLedgerEntry>): number;
|
package/lib/history.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { defaultCredentialTypes, isDefaultCredential, nodeTypes } from 'ts-mls';
|
|
2
|
+
import { parseMLSCredentialIdentity } from './credential.js';
|
|
3
|
+
import { DEVICE_ENTRY_TYPE } from './registry.js';
|
|
4
|
+
export const HISTORY_HORIZON = 393_216;
|
|
5
|
+
const utf8 = new TextEncoder();
|
|
6
|
+
export function controllerLogSize(events) {
|
|
7
|
+
return events.reduce((total, event)=>total + utf8.encode(JSON.stringify(event)).length, 0);
|
|
8
|
+
}
|
|
9
|
+
/** Count validated controller history. Callers apply the horizon only to lifecycle groups. */ export function historySize(tree, entries) {
|
|
10
|
+
let size = 0;
|
|
11
|
+
for (const node of tree){
|
|
12
|
+
if (node == null || node.nodeType !== nodeTypes.leaf) continue;
|
|
13
|
+
const credential = node.leaf.credential;
|
|
14
|
+
if (!isDefaultCredential(credential) || credential.credentialType !== defaultCredentialTypes.basic) continue;
|
|
15
|
+
const binding = parseMLSCredentialIdentity(credential.identity).controller;
|
|
16
|
+
if (binding != null) size += controllerLogSize(binding.prefix);
|
|
17
|
+
}
|
|
18
|
+
for (const { entry } of entries){
|
|
19
|
+
if (entry.type !== DEVICE_ENTRY_TYPE || entry.value == null || typeof entry.value !== 'object') {
|
|
20
|
+
continue;
|
|
21
|
+
}
|
|
22
|
+
const proof = entry.value.proof;
|
|
23
|
+
if (Array.isArray(proof)) size += controllerLogSize(proof);
|
|
24
|
+
}
|
|
25
|
+
return size;
|
|
26
|
+
}
|
package/lib/index.d.ts
CHANGED
|
@@ -3,20 +3,26 @@ export type { Capabilities, GroupContextExtension, IncomingMessageCallback, KeyP
|
|
|
3
3
|
export { defaultCapabilities, defaultProposalTypes, makeCustomExtension } from 'ts-mls';
|
|
4
4
|
export { buildCurrentGroupAnchorExtension, buildGroupAnchorExtension, controlCapabilities, decodeGroupAnchor, encodeGroupAnchor, GROUP_ANCHOR_EXTENSION_TYPE, type GroupAnchor, LEDGER_HEAD_EXTENSION_TYPE, readGroupAnchor, readGroupAnchorExtension, } from './anchor.js';
|
|
5
5
|
export { createDIDAuthenticationService, MLS_DEVICES_ACT, MLS_DEVICES_RES, MLS_LEAF_ACT, MLS_LEAF_RES, verifyManagementCapability, } from './authentication.js';
|
|
6
|
+
export { type MintLeafCapabilityParams, type MintTrustedGrantParams, mintLeafCapability, mintTrustedGrant, } from './capability.js';
|
|
6
7
|
export { type ClientState, decodeClientState, encodeClientState, sanitizeRatchetTree, } from './codec.js';
|
|
7
8
|
export { type ControllerBinding, type GroupMember, type MemberCredential, type MLSCredentialIdentity, parseMLSCredentialIdentity, } from './credential.js';
|
|
8
9
|
export { createNobleCryptoProvider, type NobleCryptoProviderOptions, nobleCryptoProvider, } from './crypto.js';
|
|
9
10
|
export type { LeafBinding } from './device-proof.js';
|
|
10
11
|
export { CONTROL_ENVELOPE_VERSION, type ControlEnvelope, type DecodeResult, decodeControlEnvelope, encodeControlEnvelope, } from './envelope.js';
|
|
11
|
-
export { type EnvelopeFoldResult, foldEnvelope } from './envelope-fold.js';
|
|
12
|
+
export { type EnvelopeFoldResult, type FoldEnvelopeContext, type FoldEnvelopeParams, foldEnvelope, } from './envelope-fold.js';
|
|
13
|
+
export { LeafBindingError, type LeafBindingReason, LeafLapsedError, type LeafLapsedReason, RevokeProofError, type RevokeProofReason, } from './errors.js';
|
|
12
14
|
export { type FoldDrop, type FoldInput, foldLedger, type LedgerReducer, } from './fold.js';
|
|
13
|
-
export { addDevice, announceControllerBeacon, type CommitInviteResult, type CommitLedgerEntriesResult, CommitRejectedError, type CreateGroupResult, type CreateInviteParams, type CreateInviteResult, commitInvite, commitLedgerEntries, createGroup, createInvite, createKeyPackageBundle, createLastResortKeyPackageBundle, type DeviceWriteResult, type ExportGroupInfoParams, type ExportGroupInfoResult, exportGroupInfo, GroupHandle, type GroupHandleEvents, type GroupHandleParams, type HeldLedgerEntry, type InspectGroupInfoResult, InviteRecipientMismatchError, type InviteRecipientMismatchErrorParams, inspectGroupInfo, type JoinGroupExternalParams, type JoinGroupExternalResult, joinGroupExternal, LAST_RESORT_EXTENSION_TYPE, LAST_RESORT_LIFETIME_DAYS, type LedgerLogEntry, labelDevice, makeMLSCredential, ORDINARY_KEY_PACKAGE_LIFETIME_DAYS, type ProcessWelcomeOnceParams, type ProcessWelcomeParams, type ProcessWelcomeResult, processWelcome, processWelcomeOnce, type RemoveMemberResult, type RestoreGroupParams, readMessageEpoch, registerDevice, removeMember, restoreGroup, revokeDevice, } from './group.js';
|
|
15
|
+
export { addDevice, announceControllerBeacon, type CommitInviteResult, type CommitLedgerEntriesResult, CommitRejectedError, type CreateGroupResult, type CreateInviteParams, type CreateInviteResult, commitInvite, commitLedgerEntries, createGroup, createInvite, createKeyPackageBundle, createLastResortKeyPackageBundle, type DeviceWriteResult, type ExportGroupInfoParams, type ExportGroupInfoResult, exportGroupInfo, GroupHandle, type GroupHandleEvents, type GroupHandleParams, type HeldLedgerEntry, type InspectGroupInfoResult, InviteRecipientMismatchError, type InviteRecipientMismatchErrorParams, inspectGroupInfo, type JoinGroupExternalParams, type JoinGroupExternalResult, joinGroupExternal, LAST_RESORT_EXTENSION_TYPE, LAST_RESORT_LIFETIME_DAYS, type LedgerLogEntry, labelDevice, makeMLSCredential, ORDINARY_KEY_PACKAGE_LIFETIME_DAYS, type ProcessWelcomeOnceParams, type ProcessWelcomeParams, type ProcessWelcomeResult, processWelcome, processWelcomeOnce, type RemoveMemberResult, type RestoreGroupParams, readCommitEntryIDs, readMessageAAD, readMessageEpoch, registerDevice, removeMember, restoreGroup, revokeDevice, type SendAdmission, } from './group.js';
|
|
16
|
+
export { type RemoveLapsedLeavesResult, type RevokeBuildResult, type RevokeWithProofParams, removeLapsedLeaves, renewLeaf, revokeWithProof, } from './group-lifecycle.js';
|
|
14
17
|
export { assertHeadMatches, buildLedgerHeadExtension, computeHead, decodeLedgerHead, encodeLedgerHead, extendHead, genesisHead, headsMatch, LEDGER_HEAD_VERSION, type LedgerHead, LedgerIncompleteError, readLedgerHead, readLedgerHeadExtension, } from './head.js';
|
|
18
|
+
export { HISTORY_HORIZON, historySize } from './history.js';
|
|
15
19
|
export { decodeKeyPackage, decodePrivateKeyPackage, encodeKeyPackage, encodePrivateKeyPackage, keyPackageRef, } from './key-package-codec.js';
|
|
16
20
|
export { type LedgerEntry, ledgerEntryDigest, signLedgerEntry, type VerifiedLedgerEntry, verifyLedgerEntry, } from './ledger.js';
|
|
21
|
+
export { type AssertRecoveryBindingParams, assertRecoveryBinding } from './lifecycle.js';
|
|
17
22
|
export { type CommitPolicyContext, defaultCommitPolicy, MissingLedgerEntriesError, } from './policy.js';
|
|
18
|
-
export { type CreateRecoveryRequestParams, type CreateRecoveryRequestResult, createRecoveryRequest, type OpenSealedGroupInfoParams, type OpenSealedLedgerParams, openSealedGroupInfo, openSealedLedger, RECOVERY_REQUEST_TYPE, type RecoveryRequest, RecoveryRequestError, type RecoveryRequestRejection, SEALED_GROUP_INFO_VERSION, SEALED_LEDGER_VERSION, SealedGroupInfoError, type SealedGroupInfoRejection, SealedLedgerError, type SealedLedgerRejection, type SealedReplyRejection, type SealGroupInfoParams, type SealLedgerParams, sealGroupInfo, sealLedger, type VerifiedRecoveryRequest, } from './recovery.js';
|
|
19
|
-
export {
|
|
23
|
+
export { type CreateRecoveryRequestParams, type CreateRecoveryRequestResult, createRecoveryRequest, type OpenSealedGroupInfoParams, type OpenSealedLedgerParams, openRecoveryGroupInfo, openSealedGroupInfo, openSealedLedger, RECOVERY_REQUEST_TYPE, type RecoveryGroupInfo, type RecoveryRequest, RecoveryRequestError, type RecoveryRequestRejection, SEALED_GROUP_INFO_VERSION, SEALED_LEDGER_VERSION, SealedGroupInfoError, type SealedGroupInfoRejection, SealedLedgerError, type SealedLedgerRejection, type SealedReplyRejection, type SealGroupInfoParams, type SealLedgerParams, sealGroupInfo, sealLedger, type VerifiedRecoveryRequest, verifyRecoveryRequest, } from './recovery.js';
|
|
24
|
+
export { confirmationKey, confirmationTag, type OpenedRecoveryVerdict, openRecoveryVerdict, type RecoveryRefusalReason, type RecoverySignerEligibleParams, type RecoveryVerdict, recoverySignerEligible, type SealRecoveryVerdictParams, sealRecoveryVerdict, } from './recovery-verdict.js';
|
|
25
|
+
export { authority, beaconOf, type ControllerBeacon, type ControllerProjection, controllerOf, DEVICE_ENTRY_TYPE, type DeviceOp, type DeviceRecord, type DeviceRegistry, type DeviceValue, denySetOf, foldControl, type Revocation, type RevokedEffect, registrySeed, revocationOf, } from './registry.js';
|
|
20
26
|
export { adminCount, foldRoster, type GroupPermission, ROLE_ENTRY_TYPE, type RoleValue, type RosterState, roleReducer, } from './roster.js';
|
|
21
27
|
export type { GroupOptions, Invite, KeyPackageBundle } from './types.js';
|
|
22
28
|
export { welcomeKeyPackageRefs } from './welcome-refs.js';
|
package/lib/index.js
CHANGED
|
@@ -1,18 +1,24 @@
|
|
|
1
1
|
export { defaultCapabilities, defaultProposalTypes, makeCustomExtension } from 'ts-mls';
|
|
2
2
|
export { buildCurrentGroupAnchorExtension, buildGroupAnchorExtension, controlCapabilities, decodeGroupAnchor, encodeGroupAnchor, GROUP_ANCHOR_EXTENSION_TYPE, LEDGER_HEAD_EXTENSION_TYPE, readGroupAnchor, readGroupAnchorExtension } from './anchor.js';
|
|
3
3
|
export { createDIDAuthenticationService, MLS_DEVICES_ACT, MLS_DEVICES_RES, MLS_LEAF_ACT, MLS_LEAF_RES, verifyManagementCapability } from './authentication.js';
|
|
4
|
+
export { mintLeafCapability, mintTrustedGrant } from './capability.js';
|
|
4
5
|
export { decodeClientState, encodeClientState, sanitizeRatchetTree } from './codec.js';
|
|
5
6
|
export { parseMLSCredentialIdentity } from './credential.js';
|
|
6
7
|
export { createNobleCryptoProvider, nobleCryptoProvider } from './crypto.js';
|
|
7
8
|
export { CONTROL_ENVELOPE_VERSION, decodeControlEnvelope, encodeControlEnvelope } from './envelope.js';
|
|
8
9
|
export { foldEnvelope } from './envelope-fold.js';
|
|
10
|
+
export { LeafBindingError, LeafLapsedError, RevokeProofError } from './errors.js';
|
|
9
11
|
export { foldLedger } from './fold.js';
|
|
10
|
-
export { addDevice, announceControllerBeacon, CommitRejectedError, commitInvite, commitLedgerEntries, createGroup, createInvite, createKeyPackageBundle, createLastResortKeyPackageBundle, exportGroupInfo, GroupHandle, InviteRecipientMismatchError, inspectGroupInfo, joinGroupExternal, LAST_RESORT_EXTENSION_TYPE, LAST_RESORT_LIFETIME_DAYS, labelDevice, makeMLSCredential, ORDINARY_KEY_PACKAGE_LIFETIME_DAYS, processWelcome, processWelcomeOnce, readMessageEpoch, registerDevice, removeMember, restoreGroup, revokeDevice } from './group.js';
|
|
12
|
+
export { addDevice, announceControllerBeacon, CommitRejectedError, commitInvite, commitLedgerEntries, createGroup, createInvite, createKeyPackageBundle, createLastResortKeyPackageBundle, exportGroupInfo, GroupHandle, InviteRecipientMismatchError, inspectGroupInfo, joinGroupExternal, LAST_RESORT_EXTENSION_TYPE, LAST_RESORT_LIFETIME_DAYS, labelDevice, makeMLSCredential, ORDINARY_KEY_PACKAGE_LIFETIME_DAYS, processWelcome, processWelcomeOnce, readCommitEntryIDs, readMessageAAD, readMessageEpoch, registerDevice, removeMember, restoreGroup, revokeDevice } from './group.js';
|
|
13
|
+
export { removeLapsedLeaves, renewLeaf, revokeWithProof } from './group-lifecycle.js';
|
|
11
14
|
export { assertHeadMatches, buildLedgerHeadExtension, computeHead, decodeLedgerHead, encodeLedgerHead, extendHead, genesisHead, headsMatch, LEDGER_HEAD_VERSION, LedgerIncompleteError, readLedgerHead, readLedgerHeadExtension } from './head.js';
|
|
15
|
+
export { HISTORY_HORIZON, historySize } from './history.js';
|
|
12
16
|
export { decodeKeyPackage, decodePrivateKeyPackage, encodeKeyPackage, encodePrivateKeyPackage, keyPackageRef } from './key-package-codec.js';
|
|
13
17
|
export { ledgerEntryDigest, signLedgerEntry, verifyLedgerEntry } from './ledger.js';
|
|
18
|
+
export { assertRecoveryBinding } from './lifecycle.js';
|
|
14
19
|
export { defaultCommitPolicy, MissingLedgerEntriesError } from './policy.js';
|
|
15
|
-
export { createRecoveryRequest, openSealedGroupInfo, openSealedLedger, RECOVERY_REQUEST_TYPE, RecoveryRequestError, SEALED_GROUP_INFO_VERSION, SEALED_LEDGER_VERSION, SealedGroupInfoError, SealedLedgerError, sealGroupInfo, sealLedger } from './recovery.js';
|
|
16
|
-
export {
|
|
20
|
+
export { createRecoveryRequest, openRecoveryGroupInfo, openSealedGroupInfo, openSealedLedger, RECOVERY_REQUEST_TYPE, RecoveryRequestError, SEALED_GROUP_INFO_VERSION, SEALED_LEDGER_VERSION, SealedGroupInfoError, SealedLedgerError, sealGroupInfo, sealLedger, verifyRecoveryRequest } from './recovery.js';
|
|
21
|
+
export { confirmationKey, confirmationTag, openRecoveryVerdict, recoverySignerEligible, sealRecoveryVerdict } from './recovery-verdict.js';
|
|
22
|
+
export { authority, beaconOf, controllerOf, DEVICE_ENTRY_TYPE, denySetOf, foldControl, registrySeed, revocationOf } from './registry.js';
|
|
17
23
|
export { adminCount, foldRoster, ROLE_ENTRY_TYPE, roleReducer } from './roster.js';
|
|
18
24
|
export { welcomeKeyPackageRefs } from './welcome-refs.js';
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type SignedEvent } from '@kokuin/controller';
|
|
2
|
+
import type { GroupHandle } from './group-handle.js';
|
|
3
|
+
import type { VerifiedLedgerEntry } from './ledger.js';
|
|
4
|
+
import type { DeviceRegistry, DeviceValue, RevokedEffect } from './registry.js';
|
|
5
|
+
export { controllerLogSize, HISTORY_HORIZON } from './history.js';
|
|
6
|
+
/** Derived from a proof and the pre-commit tree; proposal comparison belongs to the commit gate. */
|
|
7
|
+
export type LifecycleProofEffects = {
|
|
8
|
+
controller: string;
|
|
9
|
+
recordedLog: Array<SignedEvent>;
|
|
10
|
+
genFloor: number;
|
|
11
|
+
timeFloor: number;
|
|
12
|
+
logPosition?: number;
|
|
13
|
+
revoked: Array<RevokedEffect>;
|
|
14
|
+
removeLeafIndices: Array<number>;
|
|
15
|
+
};
|
|
16
|
+
type AuthenticatedProof = {
|
|
17
|
+
recordedLog: Array<SignedEvent>;
|
|
18
|
+
genFloor: number;
|
|
19
|
+
logPosition: number;
|
|
20
|
+
};
|
|
21
|
+
/** Authenticate replayable log data without reading any membership state. */
|
|
22
|
+
export declare function authenticateLifecycleProof(controllerID: string, verified: VerifiedLedgerEntry<DeviceValue>, registry: DeviceRegistry): AuthenticatedProof;
|
|
23
|
+
/** Verify live effects while all leaves still describe the epoch before the commit. */
|
|
24
|
+
export declare function verifyLifecycleProof(group: GroupHandle, verified: VerifiedLedgerEntry<DeviceValue>): Promise<LifecycleProofEffects>;
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import { didFromInception, foldLog } from '@kokuin/controller';
|
|
2
|
+
import { normalizeDID } from '@kokuin/token';
|
|
3
|
+
import { readCapability } from './capability.js';
|
|
4
|
+
import { RevokeProofError } from './errors.js';
|
|
5
|
+
import { controllerLogSize, HISTORY_HORIZON, historySize } from './history.js';
|
|
6
|
+
export { controllerLogSize, HISTORY_HORIZON } from './history.js';
|
|
7
|
+
/** Authenticate replayable log data without reading any membership state. */ export function authenticateLifecycleProof(controllerID, verified, registry) {
|
|
8
|
+
const controller = normalizeDID(controllerID);
|
|
9
|
+
const subject = normalizeDID(verified.entry.subject);
|
|
10
|
+
const value = verified.entry.value;
|
|
11
|
+
const previous = registry.controllers.get(controller);
|
|
12
|
+
const recorded = previous?.recordedLog ?? [];
|
|
13
|
+
const floor = previous?.genFloor ?? 0;
|
|
14
|
+
if (value.controller != null && normalizeDID(value.controller) !== controller || value.op === 'reset' && subject !== controller) throw new RevokeProofError('wrong-controller');
|
|
15
|
+
const proof = value.proof;
|
|
16
|
+
if (!Array.isArray(proof)) throw new RevokeProofError('no-rev');
|
|
17
|
+
if (recorded.length === 0 && proof.length === 0) throw new RevokeProofError('no-rev');
|
|
18
|
+
for (const signed of proof){
|
|
19
|
+
const event = signed?.event;
|
|
20
|
+
if (event == null || event.t !== 'icp' && event.t !== 'rot' && event.t !== 'rev') {
|
|
21
|
+
throw new RevokeProofError('not-authority-signed');
|
|
22
|
+
}
|
|
23
|
+
if ('cap' in event && event.cap != null) throw new RevokeProofError('not-authority-signed');
|
|
24
|
+
if (event.t === 'icp') {
|
|
25
|
+
let inceptionDID;
|
|
26
|
+
try {
|
|
27
|
+
inceptionDID = didFromInception(event);
|
|
28
|
+
} catch (cause) {
|
|
29
|
+
const rejection = new RevokeProofError('not-authority-signed');
|
|
30
|
+
rejection.cause = cause;
|
|
31
|
+
throw rejection;
|
|
32
|
+
}
|
|
33
|
+
if (normalizeDID(inceptionDID) !== controller) throw new RevokeProofError('wrong-controller');
|
|
34
|
+
} else if (event.i !== controller) {
|
|
35
|
+
throw new RevokeProofError('wrong-controller');
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
// An old full chain must report the floor even though it cannot attach after a reset.
|
|
39
|
+
if (proof.length > 0 && proof.every(({ event })=>event.g < floor)) {
|
|
40
|
+
throw new RevokeProofError('generation-floor');
|
|
41
|
+
}
|
|
42
|
+
if (value.op === 'revoke' && recorded.length > 0 && proof[0]?.event.t === 'icp') {
|
|
43
|
+
throw new RevokeProofError('detached');
|
|
44
|
+
}
|
|
45
|
+
if (value.op === 'revoke' && recorded.length > 0) {
|
|
46
|
+
const generation = recorded.at(-1)?.event.g ?? 0;
|
|
47
|
+
if (proof.some(({ event })=>event.g > generation)) throw new RevokeProofError('needs-reset');
|
|
48
|
+
}
|
|
49
|
+
const log = value.op === 'reset' ? proof : [
|
|
50
|
+
...recorded,
|
|
51
|
+
...proof
|
|
52
|
+
];
|
|
53
|
+
const folded = foldLog(controller, log);
|
|
54
|
+
if (!folded.ok) {
|
|
55
|
+
const detached = folded.reason.includes('sequence') || folded.reason.includes('digest') || folded.reason.includes('prior') || folded.reason.includes('inception must');
|
|
56
|
+
throw new RevokeProofError(detached ? 'detached' : 'not-authority-signed');
|
|
57
|
+
}
|
|
58
|
+
for(let index = 1; index < folded.states.length; index++){
|
|
59
|
+
if (folded.states[index]?.digest === folded.states[index - 1]?.digest) {
|
|
60
|
+
throw new RevokeProofError('not-authority-signed');
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
if (value.op === 'reset') {
|
|
64
|
+
let position = -1;
|
|
65
|
+
for(let index = 1; index < log.length; index++){
|
|
66
|
+
if (log[index]?.event.g !== log[index - 1]?.event.g) position = index;
|
|
67
|
+
}
|
|
68
|
+
const generation = folded.states[position]?.gen;
|
|
69
|
+
if (generation == null) throw new RevokeProofError('no-rev');
|
|
70
|
+
if (generation <= floor) throw new RevokeProofError('generation-floor');
|
|
71
|
+
return {
|
|
72
|
+
recordedLog: [
|
|
73
|
+
...log
|
|
74
|
+
],
|
|
75
|
+
genFloor: generation,
|
|
76
|
+
logPosition: position
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
let belowFloor = false;
|
|
80
|
+
for(let index = 0; index < log.length; index++){
|
|
81
|
+
const event = log[index]?.event;
|
|
82
|
+
const state = folded.states[index];
|
|
83
|
+
if (event?.t !== 'rev' || !('x' in event) || typeof event.x !== 'string' || state == null) continue;
|
|
84
|
+
if (normalizeDID(event.x) !== subject) continue;
|
|
85
|
+
if (![
|
|
86
|
+
...state.deny
|
|
87
|
+
].some((did)=>normalizeDID(did) === subject)) continue;
|
|
88
|
+
if (state.gen < floor) {
|
|
89
|
+
belowFloor = true;
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
return {
|
|
93
|
+
recordedLog: [
|
|
94
|
+
...log
|
|
95
|
+
],
|
|
96
|
+
genFloor: floor,
|
|
97
|
+
logPosition: index
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
throw new RevokeProofError(belowFloor ? 'generation-floor' : 'no-rev');
|
|
101
|
+
}
|
|
102
|
+
/** The entry must declare exactly the derived effects, in derivation order. */ function effectsMatch(declared, derived) {
|
|
103
|
+
if (declared?.length !== derived.length) return false;
|
|
104
|
+
return derived.every((effect, index)=>{
|
|
105
|
+
const held = declared[index];
|
|
106
|
+
if (held == null || normalizeDID(held.did) !== effect.did) return false;
|
|
107
|
+
const cascadedFrom = held.cascadedFrom == null ? undefined : normalizeDID(held.cascadedFrom);
|
|
108
|
+
return cascadedFrom === effect.cascadedFrom;
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
/** Verify live effects while all leaves still describe the epoch before the commit. */ export async function verifyLifecycleProof(group, verified) {
|
|
112
|
+
const controllerID = group.anchor.controller;
|
|
113
|
+
if (controllerID == null) throw new RevokeProofError('wrong-controller');
|
|
114
|
+
const controller = normalizeDID(controllerID);
|
|
115
|
+
const subject = normalizeDID(verified.entry.subject);
|
|
116
|
+
const value = verified.entry.value;
|
|
117
|
+
const issuerController = group.bindingOfDID(verified.issuer)?.controller;
|
|
118
|
+
if (verified.entry.groupID !== group.groupID || issuerController == null || normalizeDID(issuerController) !== controller) {
|
|
119
|
+
throw new RevokeProofError('wrong-controller');
|
|
120
|
+
}
|
|
121
|
+
const previous = group.registry.controllers.get(controller);
|
|
122
|
+
const leaves = group.listMembers().map((member)=>{
|
|
123
|
+
const binding = group.bindingOfDID(member.id);
|
|
124
|
+
const capability = binding?.capability == null ? undefined : readCapability(binding.capability);
|
|
125
|
+
const parent = capability?.payload.cap;
|
|
126
|
+
const parentRaw = Array.isArray(parent) ? parent[0] : parent;
|
|
127
|
+
const issuer = parentRaw == null || capability == null ? undefined : normalizeDID(capability.payload.iss);
|
|
128
|
+
const attested = parentRaw == null ? capability?.payload.iat : readCapability(parentRaw).payload.iat;
|
|
129
|
+
const folded = binding?.prefix == null ? undefined : foldLog(controller, binding.prefix);
|
|
130
|
+
return {
|
|
131
|
+
did: normalizeDID(member.id),
|
|
132
|
+
leafIndex: member.leafIndex,
|
|
133
|
+
issuer,
|
|
134
|
+
generation: folded?.ok ? folded.states.at(-1)?.gen : undefined,
|
|
135
|
+
attested: binding?.controller != null && normalizeDID(binding.controller) === controller ? attested : undefined,
|
|
136
|
+
prefix: binding?.prefix ?? []
|
|
137
|
+
};
|
|
138
|
+
});
|
|
139
|
+
const timeFloor = previous?.timeFloor ?? 0;
|
|
140
|
+
if (value.op === 'clock') {
|
|
141
|
+
const treeTime = Math.max(timeFloor, ...leaves.map((leaf)=>leaf.attested ?? 0));
|
|
142
|
+
if (subject !== controller) throw new RevokeProofError('wrong-controller');
|
|
143
|
+
if (value.time == null || !Number.isFinite(value.time) || value.time <= timeFloor || value.time > treeTime) {
|
|
144
|
+
throw new RevokeProofError('effects-mismatch');
|
|
145
|
+
}
|
|
146
|
+
return {
|
|
147
|
+
controller,
|
|
148
|
+
recordedLog: previous?.recordedLog ?? [],
|
|
149
|
+
genFloor: previous?.genFloor ?? 0,
|
|
150
|
+
timeFloor: value.time,
|
|
151
|
+
revoked: [],
|
|
152
|
+
removeLeafIndices: []
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
const authenticated = authenticateLifecycleProof(controller, verified, group.registry);
|
|
156
|
+
const revoked = [];
|
|
157
|
+
if (value.op === 'revoke') {
|
|
158
|
+
if (group.registry.devices.get(subject)?.status === 'revoked') throw new RevokeProofError('effects-mismatch');
|
|
159
|
+
revoked.push({
|
|
160
|
+
did: subject
|
|
161
|
+
});
|
|
162
|
+
} else if (value.op === 'reset') {
|
|
163
|
+
for (const leaf of leaves){
|
|
164
|
+
if (leaf.generation != null && leaf.generation < authenticated.genFloor) revoked.push({
|
|
165
|
+
did: leaf.did
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
} else {
|
|
169
|
+
throw new RevokeProofError('no-rev');
|
|
170
|
+
}
|
|
171
|
+
const direct = new Set(revoked.map(({ did })=>did));
|
|
172
|
+
for (const leaf of leaves){
|
|
173
|
+
if (leaf.issuer != null && direct.has(leaf.issuer) && !direct.has(leaf.did)) {
|
|
174
|
+
revoked.push({
|
|
175
|
+
did: leaf.did,
|
|
176
|
+
cascadedFrom: leaf.issuer
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
if (!effectsMatch(value.revoked, revoked)) throw new RevokeProofError('effects-mismatch');
|
|
181
|
+
const affected = new Set(revoked.map(({ did })=>did));
|
|
182
|
+
const removed = leaves.filter(({ did })=>affected.has(did));
|
|
183
|
+
const before = historySize(group.state.ratchetTree, group.ledger.map(({ verified })=>verified));
|
|
184
|
+
const after = before + controllerLogSize(value.proof ?? []) - removed.reduce((size, leaf)=>size + controllerLogSize(leaf.prefix), 0);
|
|
185
|
+
if (after > HISTORY_HORIZON && after > before) throw new RevokeProofError('too-large');
|
|
186
|
+
return {
|
|
187
|
+
controller,
|
|
188
|
+
...authenticated,
|
|
189
|
+
timeFloor,
|
|
190
|
+
revoked,
|
|
191
|
+
removeLeafIndices: removed.map(({ leafIndex })=>leafIndex)
|
|
192
|
+
};
|
|
193
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { type OwnIdentity } from '@kokuin/token';
|
|
2
|
+
import { type ClientState, type GroupContextExtension, type IncomingMessageCallback, type LeafNode } from 'ts-mls';
|
|
3
|
+
import { type ControllerBinding } from './credential.js';
|
|
4
|
+
import type { GroupHandle } from './group-handle.js';
|
|
5
|
+
import type { VerifiedLedgerEntry } from './ledger.js';
|
|
6
|
+
import { type CommitPolicyContext } from './policy.js';
|
|
7
|
+
import { type DeviceRegistry } from './registry.js';
|
|
8
|
+
export type CommitRejectionReason = 'binding' | 'lapse' | 'floor' | 'policy' | 'invalid';
|
|
9
|
+
export declare function rejectionReason(error: unknown): CommitRejectionReason;
|
|
10
|
+
export declare function leafAt(tree: ClientState['ratchetTree'], index: number): LeafNode | undefined;
|
|
11
|
+
export declare function treeTime(group: GroupHandle, controllerID: string): number;
|
|
12
|
+
export declare function isLapsed(group: GroupHandle, leaf: LeafNode): boolean;
|
|
13
|
+
export declare function validateEntry(group: GroupHandle, leaf: LeafNode, previous?: LeafNode): Promise<void>;
|
|
14
|
+
export declare function validateWelcomeTree(group: GroupHandle): Promise<void>;
|
|
15
|
+
type IncomingMessage = Parameters<IncomingMessageCallback>[0];
|
|
16
|
+
export type PrepareLifecycleGateParams = {
|
|
17
|
+
group: GroupHandle;
|
|
18
|
+
entries: Array<VerifiedLedgerEntry>;
|
|
19
|
+
candidateRegistry: DeviceRegistry;
|
|
20
|
+
context: CommitPolicyContext;
|
|
21
|
+
externalLeaf?: LeafNode;
|
|
22
|
+
};
|
|
23
|
+
export type LifecycleGate = {
|
|
24
|
+
check: (incoming: IncomingMessage) => void;
|
|
25
|
+
postApply: (state: ClientState) => Promise<void>;
|
|
26
|
+
};
|
|
27
|
+
/** The callback judges proposals; survivors check the path before any state is installed. */
|
|
28
|
+
export declare function prepareLifecycleGate(params: PrepareLifecycleGateParams): Promise<LifecycleGate>;
|
|
29
|
+
export type AssertSenderNotLapsedParams = {
|
|
30
|
+
group: GroupHandle;
|
|
31
|
+
tree: ClientState['ratchetTree'];
|
|
32
|
+
leafIndex: number | null | undefined;
|
|
33
|
+
extensions: Array<GroupContextExtension>;
|
|
34
|
+
};
|
|
35
|
+
/** Historical messages use the tree and clock floor authenticated in their own epoch. */
|
|
36
|
+
export declare function assertSenderNotLapsed(params: AssertSenderNotLapsedParams): void;
|
|
37
|
+
export type AssertRecoveryBindingParams = {
|
|
38
|
+
group: GroupHandle;
|
|
39
|
+
identity: OwnIdentity;
|
|
40
|
+
controller: ControllerBinding;
|
|
41
|
+
};
|
|
42
|
+
/** @internal Check a replacement against the complete external admission predicates. */
|
|
43
|
+
export declare function assertRecoveryBinding(params: AssertRecoveryBindingParams): Promise<void>;
|
|
44
|
+
export {};
|