feat: M5 Prekey Server + M7 Wire Format
M5: Shade Prekey Server (Hono) - REST API: register, fetch bundle, replenish, count, delete - MemoryPrekeyStore for testing/embedded use - Standalone Docker deployment (Dockerfile + standalone.ts) - One-time prekey consumption on bundle fetch M7: Compact binary wire format - Version-tagged envelopes (PreKeyMessage, RatchetMessage) - Length-prefixed byte arrays, big-endian integers - Significantly smaller than JSON (no base64 bloat) - Roundtrip encode/decode for all message types 100 tests, 0 failures across M1-M5+M7. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
64
packages/shade-server/src/memory-store.ts
Normal file
64
packages/shade-server/src/memory-store.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import type { PrekeyStore } from './store.js';
|
||||
|
||||
interface IdentityRecord {
|
||||
identitySigningKey: Uint8Array;
|
||||
identityDHKey: Uint8Array;
|
||||
}
|
||||
|
||||
interface SignedPreKeyRecord {
|
||||
keyId: number;
|
||||
publicKey: Uint8Array;
|
||||
signature: Uint8Array;
|
||||
}
|
||||
|
||||
interface OneTimePreKeyRecord {
|
||||
keyId: number;
|
||||
publicKey: Uint8Array;
|
||||
}
|
||||
|
||||
/**
|
||||
* In-memory PrekeyStore for testing and embedded use.
|
||||
*/
|
||||
export class MemoryPrekeyStore implements PrekeyStore {
|
||||
private identities = new Map<string, IdentityRecord>();
|
||||
private signedPreKeys = new Map<string, SignedPreKeyRecord>();
|
||||
private oneTimePreKeys = new Map<string, OneTimePreKeyRecord[]>();
|
||||
|
||||
async saveIdentity(address: string, identitySigningKey: Uint8Array, identityDHKey: Uint8Array): Promise<void> {
|
||||
this.identities.set(address, { identitySigningKey, identityDHKey });
|
||||
}
|
||||
|
||||
async getIdentity(address: string): Promise<IdentityRecord | null> {
|
||||
return this.identities.get(address) ?? null;
|
||||
}
|
||||
|
||||
async saveSignedPreKey(address: string, keyId: number, publicKey: Uint8Array, signature: Uint8Array): Promise<void> {
|
||||
this.signedPreKeys.set(address, { keyId, publicKey, signature });
|
||||
}
|
||||
|
||||
async getSignedPreKey(address: string): Promise<SignedPreKeyRecord | null> {
|
||||
return this.signedPreKeys.get(address) ?? null;
|
||||
}
|
||||
|
||||
async saveOneTimePreKeys(address: string, keys: Array<{ keyId: number; publicKey: Uint8Array }>): Promise<void> {
|
||||
const existing = this.oneTimePreKeys.get(address) ?? [];
|
||||
existing.push(...keys);
|
||||
this.oneTimePreKeys.set(address, existing);
|
||||
}
|
||||
|
||||
async consumeOneTimePreKey(address: string): Promise<OneTimePreKeyRecord | null> {
|
||||
const keys = this.oneTimePreKeys.get(address);
|
||||
if (!keys || keys.length === 0) return null;
|
||||
return keys.shift()!;
|
||||
}
|
||||
|
||||
async getOneTimePreKeyCount(address: string): Promise<number> {
|
||||
return this.oneTimePreKeys.get(address)?.length ?? 0;
|
||||
}
|
||||
|
||||
async deleteAll(address: string): Promise<void> {
|
||||
this.identities.delete(address);
|
||||
this.signedPreKeys.delete(address);
|
||||
this.oneTimePreKeys.delete(address);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user