@kumiai/mls 0.12.1 → 0.12.2

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.
@@ -70,6 +70,7 @@ export type HeldLedgerEntry = {
70
70
  export type LedgerLogEntry = HeldLedgerEntry & {
71
71
  entryID: string;
72
72
  };
73
+ export declare const VERIFIED_LEDGER_TOKEN_CACHE_SIZE = 4096;
73
74
  export type GroupHandleParams = {
74
75
  state: ClientState;
75
76
  credential: MemberCredential;
@@ -1,4 +1,5 @@
1
1
  import { assertTimeClaimsValid, decodeSignedToken, normalizeDID } from '@kokuin/token';
2
+ import { maybeYield } from '@sozai/async';
2
3
  import { toB64 } from '@sozai/codec';
3
4
  import { EventEmitter } from '@sozai/event';
4
5
  import { contentTypes, createApplicationMessage, createProposal, decode, defaultCredentialTypes, defaultKeyRetentionConfig, defaultProposalTypes, encode, isDefaultCredential, isDefaultProposal, leafNodeSources, mlsExporter, mlsMessageDecoder, mlsMessageEncoder, processMessage as mlsProcessMessage, nodeTypes, senderTypes, UsageError, wireformats } from 'ts-mls';
@@ -235,6 +236,35 @@ export function mutexFor(handle) {
235
236
  }
236
237
  const VERIFIED_LEDGER_ENTRIES = new WeakMap();
237
238
  const VERIFIED_LEDGER_TOKENS = new WeakMap();
239
+ export const VERIFIED_LEDGER_TOKEN_CACHE_SIZE = 4096;
240
+ const VERIFIED_LEDGER_TOKEN_CACHE = new Map();
241
+ function cachedLedgerEntry(token) {
242
+ const verified = VERIFIED_LEDGER_TOKEN_CACHE.get(token);
243
+ if (verified == null) return undefined;
244
+ VERIFIED_LEDGER_TOKEN_CACHE.delete(token);
245
+ VERIFIED_LEDGER_TOKEN_CACHE.set(token, verified);
246
+ try {
247
+ assertTimeClaimsValid(decodeSignedToken(token).payload);
248
+ return structuredClone(verified);
249
+ } catch {
250
+ return null;
251
+ }
252
+ }
253
+ function cacheLedgerEntry(token, verified) {
254
+ VERIFIED_LEDGER_TOKEN_CACHE.delete(token);
255
+ VERIFIED_LEDGER_TOKEN_CACHE.set(token, structuredClone(verified));
256
+ if (VERIFIED_LEDGER_TOKEN_CACHE.size > VERIFIED_LEDGER_TOKEN_CACHE_SIZE) {
257
+ const oldest = VERIFIED_LEDGER_TOKEN_CACHE.keys().next().value;
258
+ if (oldest != null) VERIFIED_LEDGER_TOKEN_CACHE.delete(oldest);
259
+ }
260
+ }
261
+ async function verifyCachedLedgerEntry(token) {
262
+ const cached = cachedLedgerEntry(token);
263
+ if (cached !== undefined) return cached;
264
+ const verified = await verifyLedgerEntry(token);
265
+ if (verified != null) cacheLedgerEntry(token, verified);
266
+ return verified;
267
+ }
238
268
  // Record provenance before exposing claims; re-enactments keep the original snapshot.
239
269
  function ledgerLogEntry(entryID, token, verified) {
240
270
  const held = {
@@ -552,6 +582,7 @@ function ledgerLogEntry(entryID, token, verified) {
552
582
  const memo = VERIFIED_LEDGER_TOKENS.get(tokens);
553
583
  const nextMemo = [];
554
584
  for (const [index, token] of tokens.entries()){
585
+ await maybeYield();
555
586
  const held = memo?.[index];
556
587
  let verified;
557
588
  if (held?.token === token) {
@@ -562,7 +593,7 @@ function ledgerLogEntry(entryID, token, verified) {
562
593
  }
563
594
  verified = structuredClone(held.verified);
564
595
  } else {
565
- verified = await verifyLedgerEntry(token);
596
+ verified = await verifyCachedLedgerEntry(token);
566
597
  }
567
598
  if (verified == null || verified.entry.groupID !== this.groupID) continue;
568
599
  const entryID = ledgerEntryDigest(token);
@@ -660,8 +691,9 @@ function ledgerLogEntry(entryID, token, verified) {
660
691
  assertHeadMatches(authenticated.head, computeHead(this.groupID, entryIDs));
661
692
  const log = [];
662
693
  for(let index = 0; index < tokens.length; index++){
694
+ await maybeYield();
663
695
  const token = tokens[index];
664
- const verified = await verifyLedgerEntry(token);
696
+ const verified = await verifyCachedLedgerEntry(token);
665
697
  if (verified == null || verified.entry.groupID !== this.groupID) {
666
698
  // Unreachable past the gate — the chain digests the token bytes, so an
667
699
  // unverifiable or cross-group token cannot sit at a position the head
@@ -1261,10 +1293,11 @@ function ledgerLogEntry(entryID, token, verified) {
1261
1293
  }
1262
1294
  const tokens = await this.#resolveLedgerEntries(missing);
1263
1295
  for (const token of tokens){
1296
+ await maybeYield();
1264
1297
  const id = ledgerEntryDigest(token);
1265
1298
  // Content-addressing binds the untrusted body to the requested id.
1266
1299
  if (resolved.has(id) || !missing.includes(id)) continue;
1267
- const verified = await verifyLedgerEntry(token);
1300
+ const verified = await verifyCachedLedgerEntry(token);
1268
1301
  if (verified == null) continue;
1269
1302
  resolved.set(id, ledgerLogEntry(id, token, verified));
1270
1303
  }
package/lib/ledger.d.ts CHANGED
@@ -1,4 +1,7 @@
1
1
  import { type SigningIdentity } from '@kokuin/token';
2
+ export declare const LEDGER_ENTRY_DIGEST_CACHE_SIZE = 4096;
3
+ /** @internal */
4
+ export declare function ledgerEntryDigestCacheSize(): number;
2
5
  /**
3
6
  * A control-ledger claim. The payload of a signed ledger token: `type` selects
4
7
  * the reducer, `groupID` binds the claim to a single group, `subject` is the
package/lib/ledger.js CHANGED
@@ -1,5 +1,10 @@
1
1
  import { encodeMultibase, isVerifiedToken, multihashSHA256, normalizeDID, stringifyToken, verifyToken } from '@kokuin/token';
2
2
  const textEncoder = new TextEncoder();
3
+ export const LEDGER_ENTRY_DIGEST_CACHE_SIZE = 4096;
4
+ const LEDGER_ENTRY_DIGEST_CACHE = new Map();
5
+ /** @internal */ export function ledgerEntryDigestCacheSize() {
6
+ return LEDGER_ENTRY_DIGEST_CACHE.size;
7
+ }
3
8
  /**
4
9
  * Sign a ledger entry. The signer fills `iss` with its DID, so the signature
5
10
  * covers every claim field — including `groupID` and `ord` — and binds the
@@ -63,5 +68,17 @@ const textEncoder = new TextEncoder();
63
68
  * Content-addressed digest of a signed ledger token, used as the append-only
64
69
  * store's dedup key. Multibase-encoded SHA-256 multihash over the token bytes.
65
70
  */ export function ledgerEntryDigest(signedToken) {
66
- return encodeMultibase(multihashSHA256(textEncoder.encode(signedToken)));
71
+ const cached = LEDGER_ENTRY_DIGEST_CACHE.get(signedToken);
72
+ if (cached != null) {
73
+ LEDGER_ENTRY_DIGEST_CACHE.delete(signedToken);
74
+ LEDGER_ENTRY_DIGEST_CACHE.set(signedToken, cached);
75
+ return cached;
76
+ }
77
+ const digest = encodeMultibase(multihashSHA256(textEncoder.encode(signedToken)));
78
+ LEDGER_ENTRY_DIGEST_CACHE.set(signedToken, digest);
79
+ if (LEDGER_ENTRY_DIGEST_CACHE.size > LEDGER_ENTRY_DIGEST_CACHE_SIZE) {
80
+ const oldest = LEDGER_ENTRY_DIGEST_CACHE.keys().next().value;
81
+ if (oldest != null) LEDGER_ENTRY_DIGEST_CACHE.delete(oldest);
82
+ }
83
+ return digest;
67
84
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kumiai/mls",
3
- "version": "0.12.1",
3
+ "version": "0.12.2",
4
4
  "description": "E2EE group identity and membership for Enkaku using MLS",
5
5
  "keywords": [
6
6
  "mls",
@@ -27,10 +27,11 @@
27
27
  "dependencies": {
28
28
  "@kokuin/capability": "^0.5.2",
29
29
  "@kokuin/controller": "^0.2.5",
30
- "@kokuin/token": "^0.6.2",
30
+ "@kokuin/token": "^0.6.3",
31
31
  "@noble/ciphers": "^2.4.0",
32
32
  "@noble/curves": "^2.4.0",
33
33
  "@noble/hashes": "^2.4.0",
34
+ "@sozai/async": "^0.4.3",
34
35
  "@sozai/codec": "^0.4.3",
35
36
  "@sozai/event": "^0.1.6",
36
37
  "@sozai/runtime": "^0.1.0",