sipher/src/lib/federation/keygen.ts
Nixyan 66ebebd105 refactor: modularize plugins with federation and encryption infrastructure
Major changes:
- Restructure plugin architecture: moved federation logic into a dedicated `federation` plugin with Better Auth integration, defining schemas for server registry, key rotation, and blacklist management
- Extract encryption layer: new `oven` plugin handles end-to-end encryption (E2EE) with OLM client/server implementations
- Reorganize social features: consolidated social endpoints (posts, follows, blocks, mutes) and removed legacy plugin patterns in favor of unified plugin structure
- Decentralized key management: refactored `keytools` and `keygen` to support federation key rotation with challenge tokens and health checks

Infrastructure updates:
- Upgrade dependencies: bump Better Auth to 1.6.9, React to 19.2.5, Next.js to 16.2.3, Tailwind to 4.2.4
- Add cryptographic libraries: @scure/bip39, @signalapp/libsignal-client, @matrix-org/matrix-sdk-crypto-wasm for enhanced federation security
- Add utilities: base58-js, uuid for federation identifier handling
- Update database schema with new federation tables (serverRegistry, rotateChallengeTokens, blacklistedServers)

Minor updates: test suite alignment, storage client cleanup, PostFederationSchema refinements

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-05 11:40:14 -03:00

43 lines
1.4 KiB
TypeScript

import Bun from "bun";
import nacl from "tweetnacl";
export async function generateEnvKeyPair() {
const envFile = Bun.file(".env.local");
if (!await envFile.exists()) {
throw new Error("No .env.local file found");
}
const signing = nacl.sign.keyPair();
const encryption = nacl.box.keyPair();
const env = await envFile.text();
if (
env.includes("FEDERATION_PUBLIC_KEY") ||
env.includes("FEDERATION_PRIVATE_KEY") ||
env.includes("FEDERATION_ENCRYPTION_PUBLIC_KEY") ||
env.includes("FEDERATION_ENCRYPTION_PRIVATE_KEY")
) {
throw new Error(
"Federation keys already exist in .env.local. Delete them first if you want to regenerate.",
);
}
const signingPublicKey = Buffer.from(signing.publicKey).toString("base64");
const signingPrivateKey = Buffer.from(signing.secretKey).toString("base64");
const encryptionPublicKey = Buffer.from(encryption.publicKey).toString("base64");
const encryptionPrivateKey = Buffer.from(encryption.secretKey).toString("base64");
const block = [
"",
"# Federation keys (Ed25519 signing + X25519 encryption)",
`FEDERATION_PUBLIC_KEY="${signingPublicKey}"`,
`FEDERATION_PRIVATE_KEY="${signingPrivateKey}"`,
`FEDERATION_ENCRYPTION_PUBLIC_KEY="${encryptionPublicKey}"`,
`FEDERATION_ENCRYPTION_PRIVATE_KEY="${encryptionPrivateKey}"`,
].join("\n");
await Bun.write(".env.local", env + block);
console.log("Federation keys generated and written to .env.local");
}
generateEnvKeyPair();