sipher/src/app/auth/scripts/makeKeys.ts
Nixyan df41cf4657 Update dependencies, add OLM support, and improve authentication flow
- Updated various dependencies in package.json and bun.lock for better compatibility and features.
- Added OLM (Object Location Management) support by including necessary files and updating authentication logic.
- Enhanced the authentication flow with better error handling and user feedback.
- Introduced new database schema for OLM accounts and updated related API components.
- Improved socket connection management and user interface elements for a smoother user experience.
2025-12-19 12:18:46 -03:00

75 lines
No EOL
2 KiB
TypeScript

import { db } from "@/lib/db";
// Load OLM via script tag to bypass bundler entirely
async function loadOlm() {
if (typeof window === "undefined") throw new Error("OLM requires browser");
if ((window as any).Olm) return (window as any).Olm;
return new Promise((resolve, reject) => {
const script = document.createElement("script");
script.src = "/olm.js";
script.onload = async () => {
const Olm = (window as any).Olm;
await Olm.init({ locateFile: () => "/olm.wasm" });
resolve(Olm);
};
script.onerror = () => reject(new Error("Failed to load OLM"));
document.head.appendChild(script);
});
}
type SendKeysToServerFn = (args: {
userId: string;
identityKey: { curve25519: string; ed25519: string };
oneTimeKeys: { keyId: string; publicKey: string }[];
forceInsert: boolean;
}) => Promise<unknown>;
export default async function makeKeysOnSignUp(
odId: string,
localPassword: string,
sendKeysToServer: SendKeysToServerFn,
forceInsert: boolean = false,
) {
const Olm = await loadOlm() as typeof import("@matrix-org/olm");
const account = new Olm.Account();
account.create();
const identityKey: { curve25519: string; ed25519: string } = JSON.parse(account.identity_keys());
console.debug("[makeKeysOnSignUp] Identity key: ", identityKey);
account.generate_one_time_keys(50);
const oneTimeKeys = JSON.parse(account.one_time_keys());
console.debug("[makeKeysOnSignUp] One time keys: ", oneTimeKeys);
account.mark_keys_as_published();
try {
await sendKeysToServer({
userId: odId,
identityKey: {
curve25519: identityKey.curve25519,
ed25519: identityKey.ed25519,
},
oneTimeKeys: Object.entries(oneTimeKeys.curve25519).map(([key, value]) => ({
keyId: key,
publicKey: value as string,
})),
forceInsert,
});
} catch (error) {
console.error("Failed to make keys", error);
return false;
}
const pickledAccount = account.pickle(localPassword);
await db.olmAccounts.put({
odId,
pickledAccount,
createdAt: Date.now(),
updatedAt: Date.now(),
});
return true;
}