Files
Shade/packages/shade-key-transparency/src/memory-store.ts
Sterister e6fdf31b49
Some checks failed
Test / test (push) Has been cancelled
Cross-platform vectors / TypeScript vectors (bun) (push) Has been cancelled
Cross-platform vectors / Kotlin vectors (gradle) (push) Has been cancelled
Docker build and publish / docker (push) Has been cancelled
Publish / publish (push) Has been cancelled
release(v4.0.0): Shade GA — V3.x consolidation + audit prep
V3.1 → V3.12 consolidated and tagged for the first GA release. Wire
format unchanged from 0.4.x — 4.0 peers interoperate with 0.4.x peers
byte-for-byte. The version bump is semantic: audit-cycle complete,
opt-in surface fully exposed, threat model refreshed for every new
surface.

Highlights:
- All 24 @shade/* packages bumped to 4.0.0 in lockstep.
- CHANGELOG 4.0.0 section is the canonical manifest of what landed.
- THREAT-MODEL extended (§10 fingerprint gates, §11 WebRTC P2P, §12
  Web-Worker boundary) + residual-risks table refreshed.
- OpenAPI now covers all 27 routes: prekey, transfer, KT, inbox,
  bridge, observer, /metrics, /healthz, /ready.
- MIGRATION 0.3.x → 4.0 documented + smoke-tested against
  shade migrate-storage on a real SQLite DB.
- docs/audit/REVIEW-BUNDLE.md + SCOPE.md ready for external reviewer.
- scripts/soak.ts harness for the GA-stable 2-week soak window.
- All V*.md plans archived under docs/archive/ with Status: Done.
- Voice/Video carved out into V5.0; 4.0 audit focuses on the frozen
  non-realtime stack.

Tests: TS 1000/1000 + Kotlin 11/11 cross-platform vectors green.
Docker: gt.zyon.no/stian/shade-prekey:4.0.0 builds and reports
  version 4.0.0 on /health.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-03 18:35:35 +02:00

150 lines
4.8 KiB
TypeScript

import type { KTLogLeaf, KTLogStore } from './store.js';
import type { AddressIndexEntry } from './index-tree.js';
import type { SignedTreeHead } from './sth.js';
import { compareAddresses } from './index-tree.js';
import { constantTimeEqual } from './util.js';
/**
* In-memory KTLogStore for testing and embedded servers.
*
* Maintains the same append-only invariants as a persistent store —
* `appendLeaf` cannot mutate prior entries, only push.
*/
export class MemoryKTLogStore implements KTLogStore {
private leaves: KTLogLeaf[] = [];
private indexByAddress = new Map<string, AddressIndexEntry>();
private sthsByTreeSize = new Map<number, SignedTreeHead[]>();
private latestSth: SignedTreeHead | null = null;
async appendLeaf(input: Omit<KTLogLeaf, 'index'>): Promise<number> {
const index = this.leaves.length;
this.leaves.push({
...input,
index,
leafHash: new Uint8Array(input.leafHash),
bundleHash: new Uint8Array(input.bundleHash),
});
return index;
}
async getLeaves(fromIndex: number, toIndex: number): Promise<KTLogLeaf[]> {
return this.leaves.slice(fromIndex, toIndex).map((l) => ({
...l,
leafHash: new Uint8Array(l.leafHash),
bundleHash: new Uint8Array(l.bundleHash),
}));
}
async getLeaf(index: number): Promise<KTLogLeaf | null> {
const l = this.leaves[index];
if (!l) return null;
return {
...l,
leafHash: new Uint8Array(l.leafHash),
bundleHash: new Uint8Array(l.bundleHash),
};
}
async size(): Promise<number> {
return this.leaves.length;
}
async upsertIndexEntry(entry: AddressIndexEntry): Promise<void> {
this.indexByAddress.set(entry.address, {
...entry,
bundleHash: new Uint8Array(entry.bundleHash),
});
}
async tombstoneIndexEntry(address: string, latestLeafIndex: number): Promise<void> {
const e = this.indexByAddress.get(address);
if (!e) return;
this.indexByAddress.set(address, {
...e,
deleted: true,
latestLeafIndex,
bundleHash: new Uint8Array(0),
});
}
async getAllIndexEntries(): Promise<AddressIndexEntry[]> {
const all = Array.from(this.indexByAddress.values()).map((e) => ({
...e,
bundleHash: new Uint8Array(e.bundleHash),
}));
all.sort((a, b) => compareAddresses(a.address, b.address));
return all;
}
async getIndexEntry(address: string): Promise<AddressIndexEntry | null> {
const e = this.indexByAddress.get(address);
if (!e) return null;
return { ...e, bundleHash: new Uint8Array(e.bundleHash) };
}
async saveSTH(sth: SignedTreeHead): Promise<void> {
const cloned: SignedTreeHead = {
treeSize: sth.treeSize,
timestampMs: sth.timestampMs,
rootHash: new Uint8Array(sth.rootHash),
indexRoot: new Uint8Array(sth.indexRoot),
logId: new Uint8Array(sth.logId),
signature: new Uint8Array(sth.signature),
};
const list = this.sthsByTreeSize.get(sth.treeSize) ?? [];
// De-duplicate (same root_hash + signature == same STH)
const dup = list.find(
(existing) =>
existing.timestampMs === cloned.timestampMs &&
constantTimeEqual(existing.rootHash, cloned.rootHash) &&
constantTimeEqual(existing.signature, cloned.signature),
);
if (!dup) list.push(cloned);
this.sthsByTreeSize.set(sth.treeSize, list);
if (
!this.latestSth ||
cloned.treeSize > this.latestSth.treeSize ||
(cloned.treeSize === this.latestSth.treeSize && cloned.timestampMs > this.latestSth.timestampMs)
) {
this.latestSth = cloned;
}
}
async getLatestSTH(): Promise<SignedTreeHead | null> {
return this.latestSth ? cloneSth(this.latestSth) : null;
}
async getSTHByTreeSize(treeSize: number): Promise<SignedTreeHead | null> {
const list = this.sthsByTreeSize.get(treeSize);
if (!list || list.length === 0) return null;
// Pick the most recent one for this tree size.
let best = list[0]!;
for (const s of list) if (s.timestampMs > best.timestampMs) best = s;
return cloneSth(best);
}
async listSTHs(fromTimestampMs?: number, toTimestampMs?: number): Promise<SignedTreeHead[]> {
const all: SignedTreeHead[] = [];
for (const list of this.sthsByTreeSize.values()) {
for (const s of list) {
if (fromTimestampMs !== undefined && s.timestampMs < fromTimestampMs) continue;
if (toTimestampMs !== undefined && s.timestampMs > toTimestampMs) continue;
all.push(cloneSth(s));
}
}
all.sort((a, b) => a.timestampMs - b.timestampMs);
return all;
}
}
function cloneSth(sth: SignedTreeHead): SignedTreeHead {
return {
treeSize: sth.treeSize,
timestampMs: sth.timestampMs,
rootHash: new Uint8Array(sth.rootHash),
indexRoot: new Uint8Array(sth.indexRoot),
logId: new Uint8Array(sth.logId),
signature: new Uint8Array(sth.signature),
};
}