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
|
|
|
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;
|
2026-05-08 00:11:59 +02:00
|
|
|
senderFp?: string;
|
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
|
|
|
}): 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 }>>`
|
2026-05-08 00:11:59 +02:00
|
|
|
INSERT INTO shade_inbox_blobs (address, msg_id, ciphertext, received_at, expires_at, sender_fp)
|
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
|
|
|
VALUES (
|
|
|
|
|
${args.address},
|
|
|
|
|
${args.msgId},
|
|
|
|
|
${toBase64(args.ciphertext)},
|
|
|
|
|
nextval('shade_inbox_seq'),
|
2026-05-08 00:11:59 +02:00
|
|
|
${args.expiresAt},
|
|
|
|
|
${args.senderFp ?? null}
|
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
|
|
|
)
|
|
|
|
|
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;
|
2026-05-08 00:11:59 +02:00
|
|
|
}): Promise<
|
|
|
|
|
Array<{
|
|
|
|
|
msgId: string;
|
|
|
|
|
ciphertext: Uint8Array;
|
|
|
|
|
receivedAt: number;
|
|
|
|
|
expiresAt: number;
|
|
|
|
|
senderFp?: string;
|
|
|
|
|
}>
|
|
|
|
|
> {
|
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
|
|
|
const rows = await this.sql<Array<{
|
|
|
|
|
msg_id: string;
|
|
|
|
|
ciphertext: string;
|
|
|
|
|
received_at: string;
|
|
|
|
|
expires_at: string;
|
2026-05-08 00:11:59 +02:00
|
|
|
sender_fp: string | null;
|
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
|
|
|
}>>`
|
2026-05-08 00:11:59 +02:00
|
|
|
SELECT msg_id, ciphertext, received_at::text, expires_at::text, sender_fp
|
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
|
|
|
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}
|
|
|
|
|
`;
|
2026-05-08 00:11:59 +02:00
|
|
|
return rows.map((r) => {
|
|
|
|
|
const out: {
|
|
|
|
|
msgId: string;
|
|
|
|
|
ciphertext: Uint8Array;
|
|
|
|
|
receivedAt: number;
|
|
|
|
|
expiresAt: number;
|
|
|
|
|
senderFp?: string;
|
|
|
|
|
} = {
|
|
|
|
|
msgId: r.msg_id,
|
|
|
|
|
ciphertext: fromBase64(r.ciphertext),
|
|
|
|
|
receivedAt: parseInt(r.received_at, 10),
|
|
|
|
|
expiresAt: parseInt(r.expires_at, 10),
|
|
|
|
|
};
|
|
|
|
|
if (r.sender_fp) out.senderFp = r.sender_fp;
|
|
|
|
|
return out;
|
|
|
|
|
});
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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}`;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|