release(v4.0.0): Shade GA — V3.x consolidation + audit prep
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
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
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>
This commit is contained in:
188
packages/shade-storage-sqlite/src/sqlite-inbox-store.ts
Normal file
188
packages/shade-storage-sqlite/src/sqlite-inbox-store.ts
Normal file
@@ -0,0 +1,188 @@
|
||||
import { Database } from 'bun:sqlite';
|
||||
import type { InboxStore } from '@shade/inbox-server';
|
||||
import { toBase64, fromBase64 } from '@shade/core';
|
||||
|
||||
/**
|
||||
* SQLite-backed InboxStore for the Shade Inbox Server (V3.6).
|
||||
*
|
||||
* Stores ciphertext blobs keyed by (address, msgId) with an
|
||||
* (address, expires_at) index so prune scans don't need a full table walk.
|
||||
*
|
||||
* Docker usage:
|
||||
* Volume mount /data, set SHADE_INBOX_DB_PATH=/data/shade-inbox.db
|
||||
*/
|
||||
export class SqliteInboxStore implements InboxStore {
|
||||
private db: Database;
|
||||
|
||||
private stmts!: {
|
||||
saveOwner: ReturnType<Database['prepare']>;
|
||||
getOwner: ReturnType<Database['prepare']>;
|
||||
deleteOwner: ReturnType<Database['prepare']>;
|
||||
deleteOwnerBlobs: ReturnType<Database['prepare']>;
|
||||
insertBlob: ReturnType<Database['prepare']>;
|
||||
findBlob: ReturnType<Database['prepare']>;
|
||||
fetchSince: ReturnType<Database['prepare']>;
|
||||
deleteBlob: ReturnType<Database['prepare']>;
|
||||
countBlobs: ReturnType<Database['prepare']>;
|
||||
purgeExpired: ReturnType<Database['prepare']>;
|
||||
nextSeq: ReturnType<Database['prepare']>;
|
||||
};
|
||||
|
||||
// Monotonic in-process sequence for receivedAt — guarantees a strict
|
||||
// ordering even when many writes land in the same millisecond.
|
||||
private seq = 0;
|
||||
|
||||
constructor(dbPath?: string) {
|
||||
const path = dbPath ?? process.env.SHADE_INBOX_DB_PATH ?? '/data/shade-inbox.db';
|
||||
this.db = new Database(path, { create: true });
|
||||
this.db.exec('PRAGMA journal_mode=WAL');
|
||||
this.ensureTables();
|
||||
this.prepareStatements();
|
||||
this.bootstrapSeq();
|
||||
}
|
||||
|
||||
private ensureTables() {
|
||||
this.db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS inbox_owners (
|
||||
address TEXT PRIMARY KEY,
|
||||
signing_key TEXT NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS inbox_blobs (
|
||||
address TEXT NOT NULL,
|
||||
msg_id TEXT NOT NULL,
|
||||
ciphertext TEXT NOT NULL,
|
||||
received_at INTEGER NOT NULL,
|
||||
expires_at INTEGER NOT NULL,
|
||||
PRIMARY KEY (address, msg_id)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_inbox_addr_expires
|
||||
ON inbox_blobs(address, expires_at);
|
||||
CREATE INDEX IF NOT EXISTS idx_inbox_addr_received
|
||||
ON inbox_blobs(address, received_at);
|
||||
CREATE INDEX IF NOT EXISTS idx_inbox_expires
|
||||
ON inbox_blobs(expires_at);
|
||||
`);
|
||||
}
|
||||
|
||||
private prepareStatements() {
|
||||
this.stmts = {
|
||||
saveOwner: this.db.prepare(
|
||||
'INSERT OR REPLACE INTO inbox_owners (address, signing_key) VALUES (?, ?)',
|
||||
),
|
||||
getOwner: this.db.prepare('SELECT signing_key FROM inbox_owners WHERE address = ?'),
|
||||
deleteOwner: this.db.prepare('DELETE FROM inbox_owners WHERE address = ?'),
|
||||
deleteOwnerBlobs: this.db.prepare('DELETE FROM inbox_blobs WHERE address = ?'),
|
||||
insertBlob: this.db.prepare(
|
||||
'INSERT INTO inbox_blobs (address, msg_id, ciphertext, received_at, expires_at) VALUES (?, ?, ?, ?, ?)',
|
||||
),
|
||||
findBlob: this.db.prepare(
|
||||
'SELECT received_at FROM inbox_blobs WHERE address = ? AND msg_id = ?',
|
||||
),
|
||||
fetchSince: this.db.prepare(
|
||||
`SELECT msg_id, ciphertext, received_at, expires_at
|
||||
FROM inbox_blobs
|
||||
WHERE address = ? AND received_at > ? AND expires_at > ?
|
||||
ORDER BY received_at ASC
|
||||
LIMIT ?`,
|
||||
),
|
||||
deleteBlob: this.db.prepare(
|
||||
'DELETE FROM inbox_blobs WHERE address = ? AND msg_id = ?',
|
||||
),
|
||||
countBlobs: this.db.prepare(
|
||||
'SELECT COUNT(*) AS count FROM inbox_blobs WHERE address = ? AND expires_at > ?',
|
||||
),
|
||||
purgeExpired: this.db.prepare(
|
||||
'DELETE FROM inbox_blobs WHERE expires_at <= ?',
|
||||
),
|
||||
nextSeq: this.db.prepare(
|
||||
'SELECT MAX(received_at) AS max FROM inbox_blobs',
|
||||
),
|
||||
};
|
||||
}
|
||||
|
||||
private bootstrapSeq() {
|
||||
const row = this.stmts.nextSeq.get() as { max: number | null };
|
||||
this.seq = Math.max(row?.max ?? 0, Date.now());
|
||||
}
|
||||
|
||||
close() {
|
||||
this.db.close();
|
||||
}
|
||||
|
||||
async saveAddressOwner(address: string, signingKey: Uint8Array): Promise<void> {
|
||||
this.stmts.saveOwner.run(address, toBase64(signingKey));
|
||||
}
|
||||
|
||||
async getAddressOwner(address: string): Promise<Uint8Array | null> {
|
||||
const row = this.stmts.getOwner.get(address) as { signing_key: string } | undefined;
|
||||
if (!row) return null;
|
||||
return fromBase64(row.signing_key);
|
||||
}
|
||||
|
||||
async putBlob(args: {
|
||||
address: string;
|
||||
msgId: string;
|
||||
ciphertext: Uint8Array;
|
||||
expiresAt: number;
|
||||
}): Promise<{ created: boolean; receivedAt: number }> {
|
||||
const existing = this.stmts.findBlob.get(args.address, args.msgId) as
|
||||
| { received_at: number }
|
||||
| undefined;
|
||||
if (existing) {
|
||||
return { created: false, receivedAt: existing.received_at };
|
||||
}
|
||||
this.seq = Math.max(this.seq + 1, Date.now());
|
||||
const receivedAt = this.seq;
|
||||
this.stmts.insertBlob.run(
|
||||
args.address,
|
||||
args.msgId,
|
||||
toBase64(args.ciphertext),
|
||||
receivedAt,
|
||||
args.expiresAt,
|
||||
);
|
||||
return { created: true, receivedAt };
|
||||
}
|
||||
|
||||
async fetchBlobs(args: {
|
||||
address: string;
|
||||
sinceCursor: number;
|
||||
now: number;
|
||||
limit: number;
|
||||
}): Promise<Array<{ msgId: string; ciphertext: Uint8Array; receivedAt: number; expiresAt: number }>> {
|
||||
const rows = this.stmts.fetchSince.all(
|
||||
args.address,
|
||||
args.sinceCursor,
|
||||
args.now,
|
||||
args.limit,
|
||||
) as Array<{ msg_id: string; ciphertext: string; received_at: number; expires_at: number }>;
|
||||
return rows.map((r) => ({
|
||||
msgId: r.msg_id,
|
||||
ciphertext: fromBase64(r.ciphertext),
|
||||
receivedAt: r.received_at,
|
||||
expiresAt: r.expires_at,
|
||||
}));
|
||||
}
|
||||
|
||||
async deleteBlob(address: string, msgId: string): Promise<boolean> {
|
||||
const result = this.stmts.deleteBlob.run(address, msgId);
|
||||
return result.changes > 0;
|
||||
}
|
||||
|
||||
async countBlobs(address: string, now: number): Promise<number> {
|
||||
const row = this.stmts.countBlobs.get(address, now) as { count: number };
|
||||
return row.count;
|
||||
}
|
||||
|
||||
async purgeExpired(now: number): Promise<number> {
|
||||
const result = this.stmts.purgeExpired.run(now);
|
||||
return result.changes;
|
||||
}
|
||||
|
||||
async deleteAddress(address: string): Promise<void> {
|
||||
const tx = this.db.transaction(() => {
|
||||
this.stmts.deleteOwner.run(address);
|
||||
this.stmts.deleteOwnerBlobs.run(address);
|
||||
});
|
||||
tx();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user