mirror of
https://github.com/fromchat-messenger/web.git
synced 2026-09-22 19:15:08 +03:00
Fix keys storage
This commit is contained in:
@@ -65,6 +65,17 @@ export function getCurrentKeys(): UserKeyPairMemory | null {
|
|||||||
return 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> {
|
export async function ensureKeysOnLogin(password: string, token: string): Promise<UserKeyPairMemory> {
|
||||||
// Try to restore from backup
|
// Try to restore from backup
|
||||||
const blobJson = await fetchBackupBlob(token);
|
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 });
|
const newBlob = await encryptBackupWithPassword(password, { version: 1, privateKey: currentPrivateKey });
|
||||||
await uploadBackupBlob(encodeBlob(newBlob), token);
|
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
|
// First-time setup: generate keys and upload
|
||||||
@@ -96,5 +113,13 @@ export async function ensureKeysOnLogin(password: string, token: string): Promis
|
|||||||
await uploadPublicKey(currentPublicKey, token);
|
await uploadPublicKey(currentPublicKey, token);
|
||||||
const encBlob = await encryptBackupWithPassword(password, { version: 1, privateKey: currentPrivateKey });
|
const encBlob = await encryptBackupWithPassword(password, { version: 1, privateKey: currentPrivateKey });
|
||||||
await uploadBackupBlob(encodeBlob(encBlob), token);
|
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")!);
|
||||||
}
|
}
|
||||||
@@ -5,6 +5,7 @@ import { MessagePanel } from "./panels/MessagePanel";
|
|||||||
import { PublicChatPanel } from "./panels/PublicChatPanel";
|
import { PublicChatPanel } from "./panels/PublicChatPanel";
|
||||||
import { DMPanel, type DMPanelData } from "./panels/DMPanel";
|
import { DMPanel, type DMPanelData } from "./panels/DMPanel";
|
||||||
import { getAuthHeaders } from "../auth/api";
|
import { getAuthHeaders } from "../auth/api";
|
||||||
|
import { restoreKeys } from "../auth/crypto";
|
||||||
|
|
||||||
type Page = "login" | "register" | "chat"
|
type Page = "login" | "register" | "chat"
|
||||||
export type ChatTabs = "chats" | "channels" | "contacts" | "dms"
|
export type ChatTabs = "chats" | "channels" | "contacts" | "dms"
|
||||||
@@ -202,6 +203,8 @@ export const useAppState = create<AppState>((set, get) => ({
|
|||||||
headers: getAuthHeaders(token)
|
headers: getAuthHeaders(token)
|
||||||
})).json();
|
})).json();
|
||||||
|
|
||||||
|
restoreKeys();
|
||||||
|
|
||||||
set(() => ({
|
set(() => ({
|
||||||
user: {
|
user: {
|
||||||
currentUser: user,
|
currentUser: user,
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ export interface X25519KeyPair {
|
|||||||
privateKey: Uint8Array;
|
privateKey: Uint8Array;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type KeyPair = X25519KeyPair;
|
||||||
|
|
||||||
export function generateX25519KeyPair(): X25519KeyPair {
|
export function generateX25519KeyPair(): X25519KeyPair {
|
||||||
const kp = nacl.box.keyPair();
|
const kp = nacl.box.keyPair();
|
||||||
return { publicKey: kp.publicKey, privateKey: kp.secretKey };
|
return { publicKey: kp.publicKey, privateKey: kp.secretKey };
|
||||||
|
|||||||
Reference in New Issue
Block a user