Fix keys storage

This commit is contained in:
2025-09-17 22:38:08 +03:00
Unverified
parent 6625ce71fb
commit b23513b846
3 changed files with 32 additions and 2 deletions
+27 -2
View File
@@ -65,6 +65,17 @@ export function getCurrentKeys(): UserKeyPairMemory | null {
return null;
}
function saveKeys(
publicKey: Uint8Array<ArrayBufferLike>,
privateKey: Uint8Array<ArrayBufferLike>
) {
const encodedPublicKey = b64(publicKey);
const encodedPrivateKey = b64(privateKey);
localStorage.setItem("publicKey", encodedPublicKey);
localStorage.setItem("privateKey", encodedPrivateKey);
}
export async function ensureKeysOnLogin(password: string, token: string): Promise<UserKeyPairMemory> {
// Try to restore from backup
const blobJson = await fetchBackupBlob(token);
@@ -86,7 +97,13 @@ export async function ensureKeysOnLogin(password: string, token: string): Promis
const newBlob = await encryptBackupWithPassword(password, { version: 1, privateKey: currentPrivateKey });
await uploadBackupBlob(encodeBlob(newBlob), token);
}
return { publicKey: currentPublicKey!, privateKey: currentPrivateKey! };
saveKeys(currentPublicKey!, currentPrivateKey!);
return {
publicKey: currentPublicKey!,
privateKey: currentPrivateKey!
};
}
// First-time setup: generate keys and upload
@@ -96,5 +113,13 @@ export async function ensureKeysOnLogin(password: string, token: string): Promis
await uploadPublicKey(currentPublicKey, token);
const encBlob = await encryptBackupWithPassword(password, { version: 1, privateKey: currentPrivateKey });
await uploadBackupBlob(encodeBlob(encBlob), token);
return { publicKey: currentPublicKey, privateKey: currentPrivateKey };
saveKeys(pair.publicKey, pair.privateKey);
return pair;
}
export function restoreKeys() {
currentPublicKey = ub64(localStorage.getItem("publicKey")!);
currentPrivateKey = ub64(localStorage.getItem("privateKey")!);
}
+3
View File
@@ -5,6 +5,7 @@ import { MessagePanel } from "./panels/MessagePanel";
import { PublicChatPanel } from "./panels/PublicChatPanel";
import { DMPanel, type DMPanelData } from "./panels/DMPanel";
import { getAuthHeaders } from "../auth/api";
import { restoreKeys } from "../auth/crypto";
type Page = "login" | "register" | "chat"
export type ChatTabs = "chats" | "channels" | "contacts" | "dms"
@@ -202,6 +203,8 @@ export const useAppState = create<AppState>((set, get) => ({
headers: getAuthHeaders(token)
})).json();
restoreKeys();
set(() => ({
user: {
currentUser: user,
+2
View File
@@ -6,6 +6,8 @@ export interface X25519KeyPair {
privateKey: Uint8Array;
}
export type KeyPair = X25519KeyPair;
export function generateX25519KeyPair(): X25519KeyPair {
const kp = nacl.box.keyPair();
return { publicKey: kp.publicKey, privateKey: kp.secretKey };