aliasSession knew that two labels name the same peer, then threw that knowledge away. The binding lived only in the caller's memory, so a restart lost it — and the peer could not repair it from its side. First contact forces the receiver to label a session by the only sender hint a relay surfaces, an 8-byte signing-key fingerprint (`fp:<hex>`). Once the peer announces its canonical address, aliasSession moves the session there. But the peer keeps sending under `fp:<hex>`, because its transport derives the same label from the same hint every time. After a restart the session sat under the canonical address, inbound frames resolved to `fp:<hex>`, and nothing matched. The peer held a valid session so it never re-ran X3DH: the failure was permanent, and only a manual re-link cleared it. Observed in Prism as `No session for address: fp:579c3b335d66e2c0` on every receive for three days, with a phone whose every RPC timed out. StorageProvider gains saveSessionAlias / getSessionAlias / removeSessionAliasesFor, optional so third-party implementations keep compiling, and implemented across all seven backends. Lookups resolve through resolveLabel(), which runs BEFORE the peer mutex — locking the alias while mutating the canonical session would let an aliased and a canonical caller ratchet the same state concurrently. A live session under a label always wins over an alias, and prekey envelopes never resolve: both keep a re-link establishing a fresh session instead of being redirected into the stale one. Aliases are dropped in resetSession and acceptIdentityChange, and memoized so the hot path costs no extra read. The sdk.test.ts case that asserted a dead fp-label encoded the old behaviour; it now pins the new contract. Verified: 1166 tests pass (from 1160). With alias persistence disabled as a negative control, 5 of the 6 new tests fail, including both restart cases. Also drops `baseUrl` from the consumer-strict tsconfig — removed in TS 6.0, and it was failing the typecheck that gates publishing. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
251 lines
9.0 KiB
TypeScript
251 lines
9.0 KiB
TypeScript
import type {
|
|
StorageProvider, IdentityKeyPair, SignedPreKey, OneTimePreKey,
|
|
SessionState, RetiredIdentity, PersistedStreamState, PeerVerification,
|
|
BroadcastChannelRecord, BroadcastMemberRecord,
|
|
} from '@shade/core';
|
|
import { constantTimeEqual } from '@shade/core';
|
|
|
|
/**
|
|
* In-memory StorageProvider for testing and embedded use.
|
|
* All data is lost when the instance is garbage collected.
|
|
*/
|
|
export class MemoryStorage implements StorageProvider {
|
|
private identityKeyPair: IdentityKeyPair | null = null;
|
|
private registrationId: number = 0;
|
|
private signedPreKeys = new Map<number, SignedPreKey>();
|
|
private oneTimePreKeys = new Map<number, OneTimePreKey>();
|
|
private sessions = new Map<string, SessionState>();
|
|
/** alias → canonical session label (V4.12). */
|
|
private sessionAliases = new Map<string, string>();
|
|
private trustedIdentities = new Map<string, Uint8Array>();
|
|
private retiredIdentities: RetiredIdentity[] = [];
|
|
|
|
// ─── Identity ──────────────────────────────────────────────
|
|
|
|
async getIdentityKeyPair(): Promise<IdentityKeyPair | null> {
|
|
return this.identityKeyPair;
|
|
}
|
|
|
|
async saveIdentityKeyPair(keyPair: IdentityKeyPair): Promise<void> {
|
|
this.identityKeyPair = keyPair;
|
|
}
|
|
|
|
async getLocalRegistrationId(): Promise<number> {
|
|
return this.registrationId;
|
|
}
|
|
|
|
async saveLocalRegistrationId(id: number): Promise<void> {
|
|
this.registrationId = id;
|
|
}
|
|
|
|
// ─── Signed Pre-Keys ──────────────────────────────────────
|
|
|
|
async getSignedPreKey(keyId: number): Promise<SignedPreKey | null> {
|
|
return this.signedPreKeys.get(keyId) ?? null;
|
|
}
|
|
|
|
async saveSignedPreKey(key: SignedPreKey): Promise<void> {
|
|
this.signedPreKeys.set(key.keyId, key);
|
|
}
|
|
|
|
async removeSignedPreKey(keyId: number): Promise<void> {
|
|
this.signedPreKeys.delete(keyId);
|
|
}
|
|
|
|
// ─── One-Time Pre-Keys ────────────────────────────────────
|
|
|
|
async getOneTimePreKey(keyId: number): Promise<OneTimePreKey | null> {
|
|
return this.oneTimePreKeys.get(keyId) ?? null;
|
|
}
|
|
|
|
async saveOneTimePreKey(key: OneTimePreKey): Promise<void> {
|
|
this.oneTimePreKeys.set(key.keyId, key);
|
|
}
|
|
|
|
async removeOneTimePreKey(keyId: number): Promise<void> {
|
|
this.oneTimePreKeys.delete(keyId);
|
|
}
|
|
|
|
async getOneTimePreKeyCount(): Promise<number> {
|
|
return this.oneTimePreKeys.size;
|
|
}
|
|
|
|
// ─── Sessions ─────────────────────────────────────────────
|
|
|
|
async getSession(address: string): Promise<SessionState | null> {
|
|
return this.sessions.get(address) ?? null;
|
|
}
|
|
|
|
async saveSession(address: string, state: SessionState): Promise<void> {
|
|
this.sessions.set(address, state);
|
|
}
|
|
|
|
async removeSession(address: string): Promise<void> {
|
|
this.sessions.delete(address);
|
|
}
|
|
|
|
// ─── Session label aliases ────────────────────────────────
|
|
|
|
async getSessionAlias(alias: string): Promise<string | null> {
|
|
return this.sessionAliases.get(alias) ?? null;
|
|
}
|
|
|
|
async saveSessionAlias(alias: string, canonical: string): Promise<void> {
|
|
this.sessionAliases.set(alias, canonical);
|
|
}
|
|
|
|
async removeSessionAliasesFor(canonical: string): Promise<void> {
|
|
for (const [alias, target] of this.sessionAliases) {
|
|
if (target === canonical) this.sessionAliases.delete(alias);
|
|
}
|
|
}
|
|
|
|
// ─── Trust ────────────────────────────────────────────────
|
|
|
|
async isTrustedIdentity(address: string, identityKey: Uint8Array): Promise<boolean> {
|
|
const stored = this.trustedIdentities.get(address);
|
|
if (!stored) return true; // TOFU: trust on first use
|
|
return constantTimeEqual(stored, identityKey);
|
|
}
|
|
|
|
async saveTrustedIdentity(address: string, identityKey: Uint8Array): Promise<void> {
|
|
this.trustedIdentities.set(address, identityKey);
|
|
}
|
|
|
|
// ─── Identity History ─────────────────────────────────────
|
|
|
|
async addRetiredIdentity(identity: RetiredIdentity): Promise<void> {
|
|
this.retiredIdentities.push(identity);
|
|
}
|
|
|
|
async getRetiredIdentities(): Promise<RetiredIdentity[]> {
|
|
return [...this.retiredIdentities];
|
|
}
|
|
|
|
async pruneRetiredIdentities(olderThan: number): Promise<void> {
|
|
this.retiredIdentities = this.retiredIdentities.filter((r) => r.retiredAt >= olderThan);
|
|
}
|
|
|
|
// ─── Peer verifications (V3.3) ────────────────────────────
|
|
|
|
private peerVerifications = new Map<string, PeerVerification>();
|
|
private peerIdentityVersions = new Map<string, number>();
|
|
|
|
async savePeerVerification(v: PeerVerification): Promise<void> {
|
|
this.peerVerifications.set(v.peerAddress, { ...v });
|
|
}
|
|
|
|
async getPeerVerification(address: string): Promise<PeerVerification | null> {
|
|
const v = this.peerVerifications.get(address);
|
|
return v ? { ...v } : null;
|
|
}
|
|
|
|
async removePeerVerification(address: string): Promise<void> {
|
|
this.peerVerifications.delete(address);
|
|
}
|
|
|
|
async getPeerIdentityVersion(address: string): Promise<number> {
|
|
return this.peerIdentityVersions.get(address) ?? 1;
|
|
}
|
|
|
|
async bumpPeerIdentityVersion(address: string): Promise<number> {
|
|
const next = (this.peerIdentityVersions.get(address) ?? 1) + 1;
|
|
this.peerIdentityVersions.set(address, next);
|
|
return next;
|
|
}
|
|
|
|
// ─── Stream-transfer resume state (v0.2.0) ────────────────
|
|
|
|
private streamStates = new Map<string, PersistedStreamState>();
|
|
|
|
async saveStreamState(state: PersistedStreamState): Promise<void> {
|
|
this.streamStates.set(state.streamId, { ...state });
|
|
}
|
|
|
|
async getStreamState(streamId: string): Promise<PersistedStreamState | null> {
|
|
const v = this.streamStates.get(streamId);
|
|
return v ? { ...v } : null;
|
|
}
|
|
|
|
async removeStreamState(streamId: string): Promise<void> {
|
|
this.streamStates.delete(streamId);
|
|
}
|
|
|
|
async listActiveStreamStates(direction?: 'send' | 'receive'): Promise<PersistedStreamState[]> {
|
|
const out: PersistedStreamState[] = [];
|
|
for (const s of this.streamStates.values()) {
|
|
if (s.status !== 'active' && s.status !== 'paused') continue;
|
|
if (direction !== undefined && s.direction !== direction) continue;
|
|
out.push({ ...s });
|
|
}
|
|
out.sort((a, b) => b.updatedAt - a.updatedAt);
|
|
return out;
|
|
}
|
|
|
|
async pruneStreamStates(olderThan: number): Promise<void> {
|
|
for (const [id, s] of this.streamStates) {
|
|
if ((s.status === 'finished' || s.status === 'aborted') && s.updatedAt < olderThan) {
|
|
this.streamStates.delete(id);
|
|
}
|
|
}
|
|
}
|
|
|
|
// ─── Broadcast channels (V4.6) ────────────────────────────
|
|
|
|
private broadcastChannels = new Map<string, BroadcastChannelRecord>();
|
|
private broadcastMembers = new Map<string, Map<string, BroadcastMemberRecord>>();
|
|
|
|
async saveBroadcastChannel(channel: BroadcastChannelRecord): Promise<void> {
|
|
this.broadcastChannels.set(channel.channelId, cloneChannel(channel));
|
|
}
|
|
|
|
async getBroadcastChannel(channelId: string): Promise<BroadcastChannelRecord | null> {
|
|
const v = this.broadcastChannels.get(channelId);
|
|
return v ? cloneChannel(v) : null;
|
|
}
|
|
|
|
async listBroadcastChannels(): Promise<BroadcastChannelRecord[]> {
|
|
return [...this.broadcastChannels.values()].map(cloneChannel);
|
|
}
|
|
|
|
async removeBroadcastChannel(channelId: string): Promise<void> {
|
|
this.broadcastChannels.delete(channelId);
|
|
this.broadcastMembers.delete(channelId);
|
|
}
|
|
|
|
async saveBroadcastMember(member: BroadcastMemberRecord): Promise<void> {
|
|
let inner = this.broadcastMembers.get(member.channelId);
|
|
if (!inner) {
|
|
inner = new Map();
|
|
this.broadcastMembers.set(member.channelId, inner);
|
|
}
|
|
inner.set(member.peerAddress, { ...member });
|
|
}
|
|
|
|
async getBroadcastMembers(channelId: string): Promise<BroadcastMemberRecord[]> {
|
|
const inner = this.broadcastMembers.get(channelId);
|
|
return inner ? [...inner.values()].map((m) => ({ ...m })) : [];
|
|
}
|
|
|
|
async removeBroadcastMember(channelId: string, peerAddress: string): Promise<void> {
|
|
this.broadcastMembers.get(channelId)?.delete(peerAddress);
|
|
}
|
|
}
|
|
|
|
function cloneChannel(c: BroadcastChannelRecord): BroadcastChannelRecord {
|
|
const out: BroadcastChannelRecord = {
|
|
channelId: c.channelId,
|
|
ownerRole: c.ownerRole,
|
|
ownerAddress: c.ownerAddress,
|
|
generation: c.generation,
|
|
chainKey: new Uint8Array(c.chainKey),
|
|
iteration: c.iteration,
|
|
signingPublicKey: new Uint8Array(c.signingPublicKey),
|
|
createdAt: c.createdAt,
|
|
updatedAt: c.updatedAt,
|
|
};
|
|
if (c.label !== undefined) out.label = c.label;
|
|
if (c.signingPrivateKey !== undefined) out.signingPrivateKey = new Uint8Array(c.signingPrivateKey);
|
|
return out;
|
|
}
|