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:
144
packages/shade-storage-postgres/src/postgres-inbox-store.ts
Normal file
144
packages/shade-storage-postgres/src/postgres-inbox-store.ts
Normal file
@@ -0,0 +1,144 @@
|
||||
import postgres, { type Sql } from 'postgres';
|
||||
import type { InboxStore } from '@shade/inbox-server';
|
||||
import { toBase64, fromBase64 } from '@shade/core';
|
||||
import { ensureInboxServerTables } from './ensure-tables.js';
|
||||
|
||||
/**
|
||||
* PostgreSQL-backed InboxStore for the Shade Inbox Server (V3.6).
|
||||
*
|
||||
* Concurrent-safe: insertions are unique on (address, msg_id), so two
|
||||
* simultaneous PUTs of the same blob fold into one row via
|
||||
* ON CONFLICT. `received_at` is generated server-side from a sequence
|
||||
* — strictly monotonic across processes — so cursor pagination remains
|
||||
* total-ordered even on multi-instance deployments.
|
||||
*/
|
||||
export class PostgresInboxStore implements InboxStore {
|
||||
private constructor(
|
||||
private readonly sql: Sql,
|
||||
private readonly ownsConnection: boolean,
|
||||
) {}
|
||||
|
||||
static async create(connectionString: string): Promise<PostgresInboxStore> {
|
||||
const sql = postgres(connectionString);
|
||||
const store = new PostgresInboxStore(sql, true);
|
||||
await ensureInboxServerTables(sql);
|
||||
return store;
|
||||
}
|
||||
|
||||
static async fromClient(sql: Sql): Promise<PostgresInboxStore> {
|
||||
const store = new PostgresInboxStore(sql, false);
|
||||
await ensureInboxServerTables(sql);
|
||||
return store;
|
||||
}
|
||||
|
||||
async close(): Promise<void> {
|
||||
if (this.ownsConnection) await this.sql.end();
|
||||
}
|
||||
|
||||
async saveAddressOwner(address: string, signingKey: Uint8Array): Promise<void> {
|
||||
await this.sql`
|
||||
INSERT INTO shade_inbox_owners (address, signing_key)
|
||||
VALUES (${address}, ${toBase64(signingKey)})
|
||||
ON CONFLICT (address) DO UPDATE SET signing_key = EXCLUDED.signing_key
|
||||
`;
|
||||
}
|
||||
|
||||
async getAddressOwner(address: string): Promise<Uint8Array | null> {
|
||||
const rows = await this.sql<Array<{ signing_key: string }>>`
|
||||
SELECT signing_key FROM shade_inbox_owners WHERE address = ${address}
|
||||
`;
|
||||
if (rows.length === 0) return null;
|
||||
return fromBase64(rows[0]!.signing_key);
|
||||
}
|
||||
|
||||
async putBlob(args: {
|
||||
address: string;
|
||||
msgId: string;
|
||||
ciphertext: Uint8Array;
|
||||
expiresAt: number;
|
||||
}): Promise<{ created: boolean; receivedAt: number }> {
|
||||
// ON CONFLICT DO NOTHING + RETURNING keeps it idempotent and atomic.
|
||||
// When a row already exists, we look up its received_at in a follow-up
|
||||
// SELECT.
|
||||
const inserted = await this.sql<Array<{ received_at: string }>>`
|
||||
INSERT INTO shade_inbox_blobs (address, msg_id, ciphertext, received_at, expires_at)
|
||||
VALUES (
|
||||
${args.address},
|
||||
${args.msgId},
|
||||
${toBase64(args.ciphertext)},
|
||||
nextval('shade_inbox_seq'),
|
||||
${args.expiresAt}
|
||||
)
|
||||
ON CONFLICT (address, msg_id) DO NOTHING
|
||||
RETURNING received_at::text
|
||||
`;
|
||||
if (inserted.length > 0) {
|
||||
return { created: true, receivedAt: parseInt(inserted[0]!.received_at, 10) };
|
||||
}
|
||||
const existing = await this.sql<Array<{ received_at: string }>>`
|
||||
SELECT received_at::text FROM shade_inbox_blobs
|
||||
WHERE address = ${args.address} AND msg_id = ${args.msgId}
|
||||
`;
|
||||
return {
|
||||
created: false,
|
||||
receivedAt: parseInt(existing[0]!.received_at, 10),
|
||||
};
|
||||
}
|
||||
|
||||
async fetchBlobs(args: {
|
||||
address: string;
|
||||
sinceCursor: number;
|
||||
now: number;
|
||||
limit: number;
|
||||
}): Promise<Array<{ msgId: string; ciphertext: Uint8Array; receivedAt: number; expiresAt: number }>> {
|
||||
const rows = await this.sql<Array<{
|
||||
msg_id: string;
|
||||
ciphertext: string;
|
||||
received_at: string;
|
||||
expires_at: string;
|
||||
}>>`
|
||||
SELECT msg_id, ciphertext, received_at::text, expires_at::text
|
||||
FROM shade_inbox_blobs
|
||||
WHERE address = ${args.address}
|
||||
AND received_at > ${args.sinceCursor}
|
||||
AND expires_at > ${args.now}
|
||||
ORDER BY received_at ASC
|
||||
LIMIT ${args.limit}
|
||||
`;
|
||||
return rows.map((r) => ({
|
||||
msgId: r.msg_id,
|
||||
ciphertext: fromBase64(r.ciphertext),
|
||||
receivedAt: parseInt(r.received_at, 10),
|
||||
expiresAt: parseInt(r.expires_at, 10),
|
||||
}));
|
||||
}
|
||||
|
||||
async deleteBlob(address: string, msgId: string): Promise<boolean> {
|
||||
const result = await this.sql`
|
||||
DELETE FROM shade_inbox_blobs WHERE address = ${address} AND msg_id = ${msgId}
|
||||
`;
|
||||
return result.count > 0;
|
||||
}
|
||||
|
||||
async countBlobs(address: string, now: number): Promise<number> {
|
||||
const rows = await this.sql<Array<{ count: string }>>`
|
||||
SELECT COUNT(*)::text AS count FROM shade_inbox_blobs
|
||||
WHERE address = ${address} AND expires_at > ${now}
|
||||
`;
|
||||
return parseInt(rows[0]!.count, 10);
|
||||
}
|
||||
|
||||
async purgeExpired(now: number): Promise<number> {
|
||||
const result = await this.sql`
|
||||
DELETE FROM shade_inbox_blobs WHERE expires_at <= ${now}
|
||||
`;
|
||||
return result.count;
|
||||
}
|
||||
|
||||
async deleteAddress(address: string): Promise<void> {
|
||||
await this.sql.begin(async (tx) => {
|
||||
await tx`DELETE FROM shade_inbox_owners WHERE address = ${address}`;
|
||||
await tx`DELETE FROM shade_inbox_blobs WHERE address = ${address}`;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user