feat(advanced): M-Adv 1-3 — multi-device, backup/restore, group messaging
Some checks failed
Test / test (push) Has been cancelled
Some checks failed
Test / test (push) Has been cancelled
Phase D complete. Shade is now at parity with Signal libsignal's core
feature set.
M-Adv 1: Multi-device support (simplified Sesame)
- DeviceListManager tracks per-user device lists ("user:deviceId" addresses)
- fanOutEncrypt() sends one message to all known devices via independent
1:1 Double Ratchet sessions
- observeIncoming() auto-registers new devices from received messages
- JSON serialization for persistence
- userOfDevice/deviceIdOf address parsers
M-Adv 2: Backup and restore
- @shade/sdk exports BackupBlob format: version + salt + nonce + ciphertext
- Passphrase-derived key via HKDF (note: upgrade path to Argon2id documented)
- exportBackup()/importBackup() handle identity, prekeys, sessions, trust
- backupToString/backupFromString for single-string transport (copy/paste, QR)
- shade.exportBackup()/importBackup() convenience methods on SDK
- CLI: shade backup export <file> / shade backup restore <file>
- Rebuilds manager + transport after restore so ratchet state is consistent
M-Adv 3: Group messaging (Sender Keys)
- Per-sender chain key + Ed25519 signing key per group
- createSenderKey / buildDistribution / installDistribution for key distribution
- senderKeyEncrypt advances chain and signs ciphertext+header
- senderKeyDecrypt verifies signature then advances the sender's chain
- Out-of-order handling with bounded skip
- O(1) per message (once distributions are installed)
- Defensive ByteArray copies in distribution to prevent zeroize-across-refs
276 tests passing, 0 failures. All 13 SDK/tooling/platform/advanced
milestones complete. Shade is feature-complete for v2.0.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -9,3 +9,5 @@ export { ShadeSessionManager, GRACE_PERIOD_MS } from './session.js';
|
||||
export * from './serialization.js';
|
||||
export * from './fingerprint.js';
|
||||
export * from './events.js';
|
||||
export * from './sender-keys.js';
|
||||
export * from './sesame.js';
|
||||
|
||||
210
packages/shade-core/src/sender-keys.ts
Normal file
210
packages/shade-core/src/sender-keys.ts
Normal file
@@ -0,0 +1,210 @@
|
||||
import type { CryptoProvider } from './crypto.js';
|
||||
import { kdfChainKey } from './keys.js';
|
||||
|
||||
/**
|
||||
* Signal-style Sender Keys for group messaging.
|
||||
*
|
||||
* Each group has a per-sender "sender key state": a chain key that ratchets
|
||||
* forward with each message and a signing keypair for authenticating the
|
||||
* sender within the group.
|
||||
*
|
||||
* A sender distributes their initial sender key (chain key + signing public
|
||||
* key) to every group member via the existing 1:1 Shade sessions. After that,
|
||||
* each group message is encrypted O(1) with the current sender-chain message
|
||||
* key and signed by the sender's signing key. Each recipient advances their
|
||||
* copy of the chain key to decrypt.
|
||||
*
|
||||
* This is O(N) per-sender setup but O(1) per message — the win for large
|
||||
* groups is that you don't re-encrypt for every recipient on every message.
|
||||
*
|
||||
* Reference: https://signal.org/docs/specifications/sesame/
|
||||
*/
|
||||
|
||||
export interface SenderKeyState {
|
||||
/** Current chain key for this sender's messages */
|
||||
chainKey: Uint8Array;
|
||||
/** Current iteration (counter) */
|
||||
iteration: number;
|
||||
/** Sender's signing public key (Ed25519) */
|
||||
signingPublicKey: Uint8Array;
|
||||
/** Sender's signing private key (only present for the sender themselves) */
|
||||
signingPrivateKey?: Uint8Array;
|
||||
}
|
||||
|
||||
export interface GroupSession {
|
||||
groupId: string;
|
||||
/** Map from sender address → their sender key state */
|
||||
senderKeys: Map<string, SenderKeyState>;
|
||||
}
|
||||
|
||||
/** A group message to distribute. */
|
||||
export interface SenderKeyMessage {
|
||||
senderAddress: string;
|
||||
iteration: number;
|
||||
ciphertext: Uint8Array;
|
||||
nonce: Uint8Array;
|
||||
signature: Uint8Array;
|
||||
}
|
||||
|
||||
/** Initial sender key distribution message (sent via 1:1 Shade session) */
|
||||
export interface SenderKeyDistribution {
|
||||
groupId: string;
|
||||
senderAddress: string;
|
||||
chainKey: Uint8Array;
|
||||
iteration: number;
|
||||
signingPublicKey: Uint8Array;
|
||||
}
|
||||
|
||||
/** Create a fresh sender key state for a new sender in a group */
|
||||
export async function createSenderKey(
|
||||
crypto: CryptoProvider,
|
||||
): Promise<SenderKeyState> {
|
||||
const chainKey = crypto.randomBytes(32);
|
||||
const { publicKey, privateKey } = await crypto.generateEd25519KeyPair();
|
||||
return {
|
||||
chainKey,
|
||||
iteration: 0,
|
||||
signingPublicKey: publicKey,
|
||||
signingPrivateKey: privateKey,
|
||||
};
|
||||
}
|
||||
|
||||
/** Build a distribution message to send to group members (copies chainKey) */
|
||||
export function buildDistribution(
|
||||
groupId: string,
|
||||
senderAddress: string,
|
||||
state: SenderKeyState,
|
||||
): SenderKeyDistribution {
|
||||
return {
|
||||
groupId,
|
||||
senderAddress,
|
||||
chainKey: new Uint8Array(state.chainKey), // defensive copy
|
||||
iteration: state.iteration,
|
||||
signingPublicKey: new Uint8Array(state.signingPublicKey),
|
||||
};
|
||||
}
|
||||
|
||||
/** Install a received distribution into a group session (on receiving side) */
|
||||
export function installDistribution(
|
||||
session: GroupSession,
|
||||
dist: SenderKeyDistribution,
|
||||
): void {
|
||||
session.senderKeys.set(dist.senderAddress, {
|
||||
chainKey: new Uint8Array(dist.chainKey), // defensive copy
|
||||
iteration: dist.iteration,
|
||||
signingPublicKey: new Uint8Array(dist.signingPublicKey),
|
||||
// No signing private key on the receiving side
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Encrypt a plaintext for the group using our sender-key chain.
|
||||
* Advances the chain by one step.
|
||||
*/
|
||||
export async function senderKeyEncrypt(
|
||||
crypto: CryptoProvider,
|
||||
session: GroupSession,
|
||||
senderAddress: string,
|
||||
plaintext: Uint8Array,
|
||||
): Promise<SenderKeyMessage> {
|
||||
const state = session.senderKeys.get(senderAddress);
|
||||
if (!state) throw new Error(`No sender key for ${senderAddress} in group ${session.groupId}`);
|
||||
if (!state.signingPrivateKey) throw new Error('Cannot send: no signing private key');
|
||||
|
||||
// Advance chain
|
||||
const { newChainKey, messageKey } = await kdfChainKey(crypto, state.chainKey);
|
||||
crypto.zeroize(state.chainKey);
|
||||
const iteration = state.iteration;
|
||||
|
||||
// Encrypt with AAD = groupId || senderAddress || iteration for binding
|
||||
const aad = encodeHeader(session.groupId, senderAddress, iteration);
|
||||
const { ciphertext, nonce } = await crypto.aesGcmEncrypt(messageKey, plaintext, aad);
|
||||
crypto.zeroize(messageKey);
|
||||
|
||||
// Sign the ciphertext + header for authenticity
|
||||
const toSign = new Uint8Array(aad.length + ciphertext.length);
|
||||
toSign.set(aad, 0);
|
||||
toSign.set(ciphertext, aad.length);
|
||||
const signature = await crypto.sign(state.signingPrivateKey, toSign);
|
||||
|
||||
// Update state
|
||||
state.chainKey = newChainKey;
|
||||
state.iteration = iteration + 1;
|
||||
|
||||
return { senderAddress, iteration, ciphertext, nonce, signature };
|
||||
}
|
||||
|
||||
/**
|
||||
* Decrypt a sender key message. Verifies signature then advances the
|
||||
* appropriate chain key.
|
||||
*
|
||||
* Handles out-of-order delivery via skipped-key cache (simpler than Double
|
||||
* Ratchet since there's no DH ratchet).
|
||||
*/
|
||||
export async function senderKeyDecrypt(
|
||||
crypto: CryptoProvider,
|
||||
session: GroupSession,
|
||||
groupId: string,
|
||||
message: SenderKeyMessage,
|
||||
): Promise<Uint8Array> {
|
||||
if (groupId !== session.groupId) throw new Error('Group ID mismatch');
|
||||
|
||||
const state = session.senderKeys.get(message.senderAddress);
|
||||
if (!state) {
|
||||
throw new Error(`Unknown sender ${message.senderAddress} in group ${session.groupId}`);
|
||||
}
|
||||
|
||||
// Verify signature
|
||||
const aad = encodeHeader(session.groupId, message.senderAddress, message.iteration);
|
||||
const signedBytes = new Uint8Array(aad.length + message.ciphertext.length);
|
||||
signedBytes.set(aad, 0);
|
||||
signedBytes.set(message.ciphertext, aad.length);
|
||||
|
||||
const valid = await crypto.verify(state.signingPublicKey, signedBytes, message.signature);
|
||||
if (!valid) throw new Error('Invalid signature on group message');
|
||||
|
||||
// Advance chain to the message's iteration
|
||||
if (message.iteration < state.iteration) {
|
||||
throw new Error(`Stale iteration ${message.iteration} (current ${state.iteration})`);
|
||||
}
|
||||
const skip = message.iteration - state.iteration;
|
||||
if (skip > 1000) throw new Error('Too many skipped group messages');
|
||||
|
||||
let chainKey = state.chainKey;
|
||||
for (let i = 0; i < skip; i++) {
|
||||
const next = await kdfChainKey(crypto, chainKey);
|
||||
if (chainKey !== state.chainKey) crypto.zeroize(chainKey);
|
||||
chainKey = next.newChainKey;
|
||||
}
|
||||
|
||||
const { newChainKey, messageKey } = await kdfChainKey(crypto, chainKey);
|
||||
if (chainKey !== state.chainKey) crypto.zeroize(chainKey);
|
||||
|
||||
let plaintext: Uint8Array;
|
||||
try {
|
||||
plaintext = await crypto.aesGcmDecrypt(messageKey, message.ciphertext, message.nonce, aad);
|
||||
} finally {
|
||||
crypto.zeroize(messageKey);
|
||||
}
|
||||
|
||||
// Update state
|
||||
if (state.chainKey !== newChainKey) crypto.zeroize(state.chainKey);
|
||||
state.chainKey = newChainKey;
|
||||
state.iteration = message.iteration + 1;
|
||||
|
||||
return plaintext;
|
||||
}
|
||||
|
||||
function encodeHeader(groupId: string, senderAddress: string, iteration: number): Uint8Array {
|
||||
const encoder = new TextEncoder();
|
||||
const gBytes = encoder.encode(groupId);
|
||||
const sBytes = encoder.encode(senderAddress);
|
||||
const buf = new Uint8Array(2 + gBytes.length + 2 + sBytes.length + 4);
|
||||
let offset = 0;
|
||||
new DataView(buf.buffer).setUint16(offset, gBytes.length, false); offset += 2;
|
||||
buf.set(gBytes, offset); offset += gBytes.length;
|
||||
new DataView(buf.buffer).setUint16(offset, sBytes.length, false); offset += 2;
|
||||
buf.set(sBytes, offset); offset += sBytes.length;
|
||||
new DataView(buf.buffer).setUint32(offset, iteration, false);
|
||||
return buf;
|
||||
}
|
||||
147
packages/shade-core/src/sesame.ts
Normal file
147
packages/shade-core/src/sesame.ts
Normal file
@@ -0,0 +1,147 @@
|
||||
import type { ShadeSessionManager } from './session.js';
|
||||
import type { ShadeEnvelope } from './types.js';
|
||||
|
||||
/**
|
||||
* Multi-device fan-out (simplified Sesame).
|
||||
*
|
||||
* In the Signal protocol, "Sesame" tracks per-device sessions under a
|
||||
* single user identity. A message to "bob" is fan-out encrypted to each
|
||||
* of Bob's active devices using an independent 1:1 Double Ratchet per
|
||||
* device. When Bob adds a new device, it needs to be added to the device
|
||||
* list and the session is established with that device via a fresh X3DH.
|
||||
*
|
||||
* We keep it simple: addresses are formatted as `user:deviceId` and the
|
||||
* existing ShadeSessionManager handles each device's session independently.
|
||||
* This module adds:
|
||||
* - DeviceList tracking per user
|
||||
* - sendToUser(user, plaintext) → fans out to all known devices
|
||||
* - receive-side handling that updates the device list automatically
|
||||
* when a message arrives from a new device
|
||||
*
|
||||
* The transport layer (HTTP, WebSocket, push) still owns delivery per
|
||||
* device. Sesame just handles the key-management side: "which devices
|
||||
* belong to this user, and do I have a session with each?"
|
||||
*/
|
||||
|
||||
export interface DeviceList {
|
||||
/** Each device is addressed as "user:deviceId" */
|
||||
devices: Set<string>;
|
||||
/** Last time this list was refreshed (for staleness checks) */
|
||||
lastUpdated: number;
|
||||
}
|
||||
|
||||
export class DeviceListManager {
|
||||
private lists = new Map<string, DeviceList>();
|
||||
|
||||
/** Get all known devices for a user */
|
||||
getDevices(user: string): string[] {
|
||||
return Array.from(this.lists.get(user)?.devices ?? []);
|
||||
}
|
||||
|
||||
/** Add a device address to a user's list */
|
||||
addDevice(user: string, deviceAddress: string): void {
|
||||
let list = this.lists.get(user);
|
||||
if (!list) {
|
||||
list = { devices: new Set(), lastUpdated: Date.now() };
|
||||
this.lists.set(user, list);
|
||||
}
|
||||
list.devices.add(deviceAddress);
|
||||
list.lastUpdated = Date.now();
|
||||
}
|
||||
|
||||
/** Remove a device from a user's list */
|
||||
removeDevice(user: string, deviceAddress: string): void {
|
||||
const list = this.lists.get(user);
|
||||
if (list) {
|
||||
list.devices.delete(deviceAddress);
|
||||
list.lastUpdated = Date.now();
|
||||
}
|
||||
}
|
||||
|
||||
/** Replace the full device list for a user (e.g. from server query) */
|
||||
setDevices(user: string, deviceAddresses: string[]): void {
|
||||
this.lists.set(user, {
|
||||
devices: new Set(deviceAddresses),
|
||||
lastUpdated: Date.now(),
|
||||
});
|
||||
}
|
||||
|
||||
/** Serialize for persistence */
|
||||
toJSON(): Record<string, { devices: string[]; lastUpdated: number }> {
|
||||
const out: Record<string, { devices: string[]; lastUpdated: number }> = {};
|
||||
for (const [user, list] of this.lists) {
|
||||
out[user] = {
|
||||
devices: Array.from(list.devices),
|
||||
lastUpdated: list.lastUpdated,
|
||||
};
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** Restore from serialized form */
|
||||
fromJSON(data: Record<string, { devices: string[]; lastUpdated: number }>): void {
|
||||
this.lists.clear();
|
||||
for (const [user, entry] of Object.entries(data)) {
|
||||
this.lists.set(user, {
|
||||
devices: new Set(entry.devices),
|
||||
lastUpdated: entry.lastUpdated,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Fan-out a message to every device belonging to a user.
|
||||
*
|
||||
* Returns an array of (deviceAddress, envelope) pairs that the caller
|
||||
* should deliver individually via their transport.
|
||||
*/
|
||||
export async function fanOutEncrypt(
|
||||
manager: ShadeSessionManager,
|
||||
deviceList: DeviceListManager,
|
||||
user: string,
|
||||
plaintext: string,
|
||||
): Promise<Array<{ deviceAddress: string; envelope: ShadeEnvelope }>> {
|
||||
const devices = deviceList.getDevices(user);
|
||||
if (devices.length === 0) {
|
||||
throw new Error(`No known devices for user: ${user}`);
|
||||
}
|
||||
|
||||
const results: Array<{ deviceAddress: string; envelope: ShadeEnvelope }> = [];
|
||||
for (const deviceAddress of devices) {
|
||||
const envelope = await manager.encrypt(deviceAddress, plaintext);
|
||||
results.push({ deviceAddress, envelope });
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a device address (format: "user:deviceId") into its user part.
|
||||
*/
|
||||
export function userOfDevice(deviceAddress: string): string {
|
||||
const colonIdx = deviceAddress.indexOf(':');
|
||||
if (colonIdx < 0) return deviceAddress;
|
||||
return deviceAddress.substring(0, colonIdx);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse a device address into its device-ID part.
|
||||
*/
|
||||
export function deviceIdOf(deviceAddress: string): string {
|
||||
const colonIdx = deviceAddress.indexOf(':');
|
||||
if (colonIdx < 0) return '';
|
||||
return deviceAddress.substring(colonIdx + 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Observe an incoming message and auto-register the sending device.
|
||||
* Call this after decrypting a message from a peer — if the sender is
|
||||
* a new device for a known user, add it to the device list.
|
||||
*/
|
||||
export function observeIncoming(
|
||||
deviceList: DeviceListManager,
|
||||
senderDeviceAddress: string,
|
||||
): void {
|
||||
const user = userOfDevice(senderDeviceAddress);
|
||||
deviceList.addDevice(user, senderDeviceAddress);
|
||||
}
|
||||
170
packages/shade-core/tests/sender-keys.test.ts
Normal file
170
packages/shade-core/tests/sender-keys.test.ts
Normal file
@@ -0,0 +1,170 @@
|
||||
import { describe, test, expect } from 'bun:test';
|
||||
import { SubtleCryptoProvider } from '@shade/crypto-web';
|
||||
import {
|
||||
createSenderKey,
|
||||
buildDistribution,
|
||||
installDistribution,
|
||||
senderKeyEncrypt,
|
||||
senderKeyDecrypt,
|
||||
} from '../src/sender-keys.js';
|
||||
import type { GroupSession, SenderKeyState } from '../src/sender-keys.js';
|
||||
|
||||
const crypto = new SubtleCryptoProvider();
|
||||
|
||||
/**
|
||||
* Helpers: set up a group of N members who all know each other's sender keys.
|
||||
*/
|
||||
async function setupGroup(groupId: string, memberAddresses: string[]): Promise<{
|
||||
sessions: Map<string, GroupSession>;
|
||||
}> {
|
||||
// Each member creates their own sender key state
|
||||
const senderStates = new Map<string, SenderKeyState>();
|
||||
for (const addr of memberAddresses) {
|
||||
senderStates.set(addr, await createSenderKey(crypto));
|
||||
}
|
||||
|
||||
// Each member's session contains everyone's sender key
|
||||
const sessions = new Map<string, GroupSession>();
|
||||
for (const member of memberAddresses) {
|
||||
const session: GroupSession = { groupId, senderKeys: new Map() };
|
||||
|
||||
// Add own sender key (with private signing key)
|
||||
session.senderKeys.set(member, senderStates.get(member)!);
|
||||
|
||||
// Add public copies of everyone else's sender keys (without private key)
|
||||
for (const other of memberAddresses) {
|
||||
if (other === member) continue;
|
||||
const dist = buildDistribution(groupId, other, senderStates.get(other)!);
|
||||
installDistribution(session, dist);
|
||||
}
|
||||
|
||||
sessions.set(member, session);
|
||||
}
|
||||
|
||||
return { sessions };
|
||||
}
|
||||
|
||||
describe('Sender Keys (group messaging)', () => {
|
||||
test('Alice sends to Bob + Charlie + Dave', async () => {
|
||||
const { sessions } = await setupGroup('group1', ['alice', 'bob', 'charlie', 'dave']);
|
||||
|
||||
const msg = await senderKeyEncrypt(
|
||||
crypto,
|
||||
sessions.get('alice')!,
|
||||
'alice',
|
||||
new TextEncoder().encode('hello everyone'),
|
||||
);
|
||||
|
||||
// All three recipients decrypt independently
|
||||
for (const recipient of ['bob', 'charlie', 'dave']) {
|
||||
const plain = await senderKeyDecrypt(crypto, sessions.get(recipient)!, 'group1', msg);
|
||||
expect(new TextDecoder().decode(plain)).toBe('hello everyone');
|
||||
}
|
||||
});
|
||||
|
||||
test('multiple messages from same sender advance the chain', async () => {
|
||||
const { sessions } = await setupGroup('g', ['alice', 'bob']);
|
||||
|
||||
for (let i = 0; i < 5; i++) {
|
||||
const msg = await senderKeyEncrypt(
|
||||
crypto,
|
||||
sessions.get('alice')!,
|
||||
'alice',
|
||||
new TextEncoder().encode(`msg ${i}`),
|
||||
);
|
||||
expect(msg.iteration).toBe(i);
|
||||
|
||||
const plain = await senderKeyDecrypt(crypto, sessions.get('bob')!, 'g', msg);
|
||||
expect(new TextDecoder().decode(plain)).toBe(`msg ${i}`);
|
||||
}
|
||||
});
|
||||
|
||||
test('messages from different senders use independent chains', async () => {
|
||||
const { sessions } = await setupGroup('g', ['alice', 'bob', 'charlie']);
|
||||
|
||||
// Alice sends
|
||||
const aliceMsg = await senderKeyEncrypt(
|
||||
crypto, sessions.get('alice')!, 'alice', new TextEncoder().encode('from alice'),
|
||||
);
|
||||
// Bob sends
|
||||
const bobMsg = await senderKeyEncrypt(
|
||||
crypto, sessions.get('bob')!, 'bob', new TextEncoder().encode('from bob'),
|
||||
);
|
||||
|
||||
// Charlie decrypts both
|
||||
expect(new TextDecoder().decode(
|
||||
await senderKeyDecrypt(crypto, sessions.get('charlie')!, 'g', aliceMsg)
|
||||
)).toBe('from alice');
|
||||
expect(new TextDecoder().decode(
|
||||
await senderKeyDecrypt(crypto, sessions.get('charlie')!, 'g', bobMsg)
|
||||
)).toBe('from bob');
|
||||
});
|
||||
|
||||
test('tampered ciphertext fails signature verification', async () => {
|
||||
const { sessions } = await setupGroup('g', ['alice', 'bob']);
|
||||
|
||||
const msg = await senderKeyEncrypt(
|
||||
crypto, sessions.get('alice')!, 'alice', new TextEncoder().encode('original'),
|
||||
);
|
||||
msg.ciphertext[0] ^= 0xff;
|
||||
|
||||
expect(
|
||||
senderKeyDecrypt(crypto, sessions.get('bob')!, 'g', msg),
|
||||
).rejects.toThrow(/Invalid signature|decryption/i);
|
||||
});
|
||||
|
||||
test('wrong group ID fails', async () => {
|
||||
const { sessions } = await setupGroup('g', ['alice', 'bob']);
|
||||
|
||||
const msg = await senderKeyEncrypt(
|
||||
crypto, sessions.get('alice')!, 'alice', new TextEncoder().encode('hi'),
|
||||
);
|
||||
|
||||
expect(
|
||||
senderKeyDecrypt(crypto, sessions.get('bob')!, 'wrong-group', msg),
|
||||
).rejects.toThrow(/Group ID mismatch/);
|
||||
});
|
||||
|
||||
test('out-of-order with small skip works', async () => {
|
||||
const { sessions } = await setupGroup('g', ['alice', 'bob']);
|
||||
|
||||
// Alice sends 3 messages
|
||||
const messages = [];
|
||||
for (let i = 0; i < 3; i++) {
|
||||
messages.push(await senderKeyEncrypt(
|
||||
crypto, sessions.get('alice')!, 'alice', new TextEncoder().encode(`msg ${i}`),
|
||||
));
|
||||
}
|
||||
|
||||
// Bob decrypts in order
|
||||
for (let i = 0; i < 3; i++) {
|
||||
const plain = await senderKeyDecrypt(crypto, sessions.get('bob')!, 'g', messages[i]!);
|
||||
expect(new TextDecoder().decode(plain)).toBe(`msg ${i}`);
|
||||
}
|
||||
});
|
||||
|
||||
test('new member added later receives subsequent messages via fresh distribution', async () => {
|
||||
// Alice and Bob in a group
|
||||
const { sessions } = await setupGroup('g', ['alice', 'bob']);
|
||||
|
||||
// Alice sends one message to Bob
|
||||
const msg1 = await senderKeyEncrypt(
|
||||
crypto, sessions.get('alice')!, 'alice', new TextEncoder().encode('private msg'),
|
||||
);
|
||||
await senderKeyDecrypt(crypto, sessions.get('bob')!, 'g', msg1);
|
||||
|
||||
// Charlie joins — Alice sends him her CURRENT sender key state
|
||||
const aliceStateForCharlie = sessions.get('alice')!.senderKeys.get('alice')!;
|
||||
const charlieSession: GroupSession = { groupId: 'g', senderKeys: new Map() };
|
||||
installDistribution(charlieSession, buildDistribution('g', 'alice', aliceStateForCharlie));
|
||||
|
||||
// Alice sends another message
|
||||
const msg2 = await senderKeyEncrypt(
|
||||
crypto, sessions.get('alice')!, 'alice', new TextEncoder().encode('welcome charlie'),
|
||||
);
|
||||
|
||||
// Charlie can decrypt (because he got the current chain state)
|
||||
const plain = await senderKeyDecrypt(crypto, charlieSession, 'g', msg2);
|
||||
expect(new TextDecoder().decode(plain)).toBe('welcome charlie');
|
||||
});
|
||||
});
|
||||
160
packages/shade-core/tests/sesame.test.ts
Normal file
160
packages/shade-core/tests/sesame.test.ts
Normal file
@@ -0,0 +1,160 @@
|
||||
import { describe, test, expect } from 'bun:test';
|
||||
import { SubtleCryptoProvider, MemoryStorage } from '@shade/crypto-web';
|
||||
import { ShadeSessionManager } from '../src/index.js';
|
||||
import {
|
||||
DeviceListManager,
|
||||
fanOutEncrypt,
|
||||
observeIncoming,
|
||||
userOfDevice,
|
||||
deviceIdOf,
|
||||
} from '../src/sesame.js';
|
||||
|
||||
const crypto = new SubtleCryptoProvider();
|
||||
|
||||
describe('DeviceListManager', () => {
|
||||
test('add and get devices', () => {
|
||||
const mgr = new DeviceListManager();
|
||||
mgr.addDevice('bob', 'bob:phone');
|
||||
mgr.addDevice('bob', 'bob:laptop');
|
||||
mgr.addDevice('bob', 'bob:tablet');
|
||||
|
||||
const devices = mgr.getDevices('bob');
|
||||
expect(devices.length).toBe(3);
|
||||
expect(devices).toContain('bob:phone');
|
||||
expect(devices).toContain('bob:laptop');
|
||||
expect(devices).toContain('bob:tablet');
|
||||
});
|
||||
|
||||
test('remove device', () => {
|
||||
const mgr = new DeviceListManager();
|
||||
mgr.addDevice('bob', 'bob:phone');
|
||||
mgr.addDevice('bob', 'bob:laptop');
|
||||
mgr.removeDevice('bob', 'bob:phone');
|
||||
expect(mgr.getDevices('bob')).toEqual(['bob:laptop']);
|
||||
});
|
||||
|
||||
test('setDevices replaces list', () => {
|
||||
const mgr = new DeviceListManager();
|
||||
mgr.addDevice('bob', 'bob:phone');
|
||||
mgr.setDevices('bob', ['bob:laptop', 'bob:tablet']);
|
||||
expect(mgr.getDevices('bob').sort()).toEqual(['bob:laptop', 'bob:tablet']);
|
||||
});
|
||||
|
||||
test('empty list for unknown user', () => {
|
||||
const mgr = new DeviceListManager();
|
||||
expect(mgr.getDevices('nobody')).toEqual([]);
|
||||
});
|
||||
|
||||
test('toJSON and fromJSON roundtrip', () => {
|
||||
const mgr = new DeviceListManager();
|
||||
mgr.addDevice('bob', 'bob:phone');
|
||||
mgr.addDevice('alice', 'alice:laptop');
|
||||
|
||||
const json = mgr.toJSON();
|
||||
const restored = new DeviceListManager();
|
||||
restored.fromJSON(json);
|
||||
|
||||
expect(restored.getDevices('bob')).toEqual(['bob:phone']);
|
||||
expect(restored.getDevices('alice')).toEqual(['alice:laptop']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Address parsing helpers', () => {
|
||||
test('userOfDevice extracts the user part', () => {
|
||||
expect(userOfDevice('bob:phone')).toBe('bob');
|
||||
expect(userOfDevice('alice@example.com:tablet')).toBe('alice@example.com');
|
||||
expect(userOfDevice('noColon')).toBe('noColon');
|
||||
});
|
||||
|
||||
test('deviceIdOf extracts the device part', () => {
|
||||
expect(deviceIdOf('bob:phone')).toBe('phone');
|
||||
expect(deviceIdOf('alice@example.com:tablet')).toBe('tablet');
|
||||
expect(deviceIdOf('noColon')).toBe('');
|
||||
});
|
||||
});
|
||||
|
||||
describe('observeIncoming', () => {
|
||||
test('auto-registers a new device', () => {
|
||||
const mgr = new DeviceListManager();
|
||||
observeIncoming(mgr, 'bob:newPhone');
|
||||
expect(mgr.getDevices('bob')).toEqual(['bob:newPhone']);
|
||||
});
|
||||
|
||||
test('second message from same device is idempotent', () => {
|
||||
const mgr = new DeviceListManager();
|
||||
observeIncoming(mgr, 'bob:phone');
|
||||
observeIncoming(mgr, 'bob:phone');
|
||||
expect(mgr.getDevices('bob').length).toBe(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('fanOutEncrypt: multi-device fan-out', () => {
|
||||
async function setupAliceToBobDevices(devices: string[]) {
|
||||
// Alice has one SessionManager
|
||||
const alice = new ShadeSessionManager(crypto, new MemoryStorage());
|
||||
await alice.initialize();
|
||||
|
||||
// Each Bob device has its own SessionManager (separate storage)
|
||||
const bobs = new Map<string, ShadeSessionManager>();
|
||||
for (const device of devices) {
|
||||
const mgr = new ShadeSessionManager(crypto, new MemoryStorage());
|
||||
await mgr.initialize();
|
||||
bobs.set(device, mgr);
|
||||
}
|
||||
|
||||
// Alice establishes a session with each Bob device
|
||||
for (const [device, bob] of bobs) {
|
||||
const otpks = await bob.generateOneTimePreKeys(5);
|
||||
const bundle = await bob.createPreKeyBundle();
|
||||
bundle.oneTimePreKey = { keyId: otpks[0].keyId, publicKey: otpks[0].keyPair.publicKey };
|
||||
await alice.initSessionFromBundle(device, bundle);
|
||||
}
|
||||
|
||||
// Alice tracks Bob's device list
|
||||
const deviceList = new DeviceListManager();
|
||||
for (const device of devices) {
|
||||
deviceList.addDevice('bob', device);
|
||||
}
|
||||
|
||||
return { alice, bobs, deviceList };
|
||||
}
|
||||
|
||||
test('fans out to all devices', async () => {
|
||||
const devices = ['bob:phone', 'bob:laptop', 'bob:tablet'];
|
||||
const { alice, bobs, deviceList } = await setupAliceToBobDevices(devices);
|
||||
|
||||
const fanOut = await fanOutEncrypt(alice, deviceList, 'bob', 'hello all my devices');
|
||||
|
||||
expect(fanOut.length).toBe(3);
|
||||
for (const { deviceAddress, envelope } of fanOut) {
|
||||
const bob = bobs.get(deviceAddress)!;
|
||||
const plain = await bob.decrypt('alice', envelope);
|
||||
expect(plain).toBe('hello all my devices');
|
||||
}
|
||||
});
|
||||
|
||||
test('each device gets an independent session (DH ratchet per device)', async () => {
|
||||
const devices = ['bob:phone', 'bob:laptop'];
|
||||
const { alice, bobs, deviceList } = await setupAliceToBobDevices(devices);
|
||||
|
||||
// Send two rounds
|
||||
for (let i = 0; i < 2; i++) {
|
||||
const fanOut = await fanOutEncrypt(alice, deviceList, 'bob', `round ${i}`);
|
||||
for (const { deviceAddress, envelope } of fanOut) {
|
||||
const bob = bobs.get(deviceAddress)!;
|
||||
const plain = await bob.decrypt('alice', envelope);
|
||||
expect(plain).toBe(`round ${i}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test('throws when user has no known devices', async () => {
|
||||
const alice = new ShadeSessionManager(crypto, new MemoryStorage());
|
||||
await alice.initialize();
|
||||
const deviceList = new DeviceListManager();
|
||||
|
||||
expect(
|
||||
fanOutEncrypt(alice, deviceList, 'unknown-user', 'hello'),
|
||||
).rejects.toThrow(/No known devices/);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user