Files
Shade/packages/shade-cli/src/commands/backup.ts

79 lines
2.2 KiB
TypeScript
Raw Normal View History

feat(advanced): M-Adv 1-3 — multi-device, backup/restore, group messaging 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>
2026-04-11 00:51:34 +02:00
import { createShade } from '@shade/sdk';
import { readFileSync, writeFileSync } from 'fs';
import { loadConfig } from '../config.js';
/**
* Export the current Shade state to a file.
*
* Usage: shade backup export <file> [--addresses bob,charlie]
*/
export async function backupExportCommand(
file: string,
options: { addresses?: string[] } = {},
): Promise<void> {
const config = loadConfig();
const shade = await createShade({
prekeyServer: config.prekeyServer,
storage: config.storage,
address: config.address,
autoReplenish: false,
});
try {
// Prompt for passphrase via stdin (simple prompt, no hiding)
process.stdout.write('Backup passphrase (min 12 chars): ');
const passphrase = await readLine();
const backupString = await shade.exportBackup(passphrase, options.addresses ?? []);
writeFileSync(file, backupString);
console.log(`\n\x1b[32m✓\x1b[0m Backup written to ${file}`);
console.log(` Size: ${backupString.length} bytes`);
console.log(` Keep this passphrase safe — it's the only way to restore.`);
} finally {
await shade.shutdown();
}
}
/**
* Restore a backup file into the current Shade storage.
*
* Usage: shade backup restore <file>
*/
export async function backupRestoreCommand(file: string): Promise<void> {
const config = loadConfig();
const backupString = readFileSync(file, 'utf-8').trim();
const shade = await createShade({
prekeyServer: config.prekeyServer,
storage: config.storage,
address: config.address,
autoReplenish: false,
});
try {
process.stdout.write('Passphrase: ');
const passphrase = await readLine();
await shade.importBackup(backupString, passphrase);
console.log(`\n\x1b[32m✓\x1b[0m Backup restored`);
console.log(` Fingerprint: ${await shade.fingerprint}`);
} finally {
await shade.shutdown();
}
}
async function readLine(): Promise<string> {
const decoder = new TextDecoder();
const buffer: string[] = [];
for await (const chunk of Bun.stdin.stream()) {
const text = decoder.decode(chunk as Uint8Array);
const newlineIdx = text.indexOf('\n');
if (newlineIdx >= 0) {
buffer.push(text.slice(0, newlineIdx));
break;
}
buffer.push(text);
}
return buffer.join('').trim();
}