diff --git a/src-tauri/src/commands/accounts.rs b/src-tauri/src/commands/accounts.rs index 0be3371..c5ff008 100644 --- a/src-tauri/src/commands/accounts.rs +++ b/src-tauri/src/commands/accounts.rs @@ -12,7 +12,7 @@ pub struct Account { #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub struct AccountsFile { - pub active_account: Option, + pub active_account: usize, pub accounts: Vec, } @@ -39,7 +39,7 @@ pub fn get_accounts(app: AppHandle) -> Result { let path = accounts_file_path(&app)?; if !path.exists() { return Ok(AccountsFile { - active_account: None, + active_account: 0, accounts: Vec::new(), }); } diff --git a/src/App.tsx b/src/App.tsx index 0331cdc..48af353 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,23 +1,28 @@ -import { useRef } from "react"; +import { useReducer, useRef } from "react"; import Enclave from "@/app/app"; import ServerList from "./components/view/serverList"; import { NewProfilePage } from "./components/view/NewProfileView"; export default function App() { const appRef = useRef(null); + const [, forceRender] = useReducer((x) => x + 1, 0); if (!appRef.current) { appRef.current = new Enclave(); + + appRef.current.forceRender = forceRender; + appRef.current .init() .then(() => { console.log("Encalve initialized"); + forceRender(); }) .catch(console.error); } return (
- {!appRef.current.accounts || + {appRef.current?.accounts && appRef.current.accounts.accounts.length === 0 ? ( ) : ( diff --git a/src/app/app.ts b/src/app/app.ts index 56ea1e9..17f5168 100644 --- a/src/app/app.ts +++ b/src/app/app.ts @@ -1,8 +1,7 @@ import { + getHostname, getHTTPUrl, getServerList, - getWSUrl, - KnownServer, ServerList, } from "@/lib/serverList"; import EnclaveServer from "./server"; @@ -29,16 +28,14 @@ ed.hashes.sha512 = sha512; * reach into `EnclaveServer` or `EnclaveWebSocket` directly. */ export default class Enclave { - private clientSecretKey: Uint8Array; - private clientPublicKey: Uint8Array; public accounts?: AccountsFile; public server?: EnclaveServer; public serverList: ServerList; + public forceRender: () => void; public constructor() { - this.clientSecretKey = ed.utils.randomSecretKey(); - this.clientPublicKey = ed.getPublicKey(this.clientSecretKey); this.serverList = {}; + this.forceRender = () => {}; } public async init() { @@ -51,7 +48,18 @@ export default class Enclave { this.server.disconnect(); } - this.server = new EnclaveServer(getWSUrl(hostname, isSecure)); + this.server = new EnclaveServer(getHostname(hostname), isSecure); + + const clientSecretKey = this.getClientSecretKey(); + + if (!clientSecretKey) { + console.error("Failed to get client key"); + return; + } + + const clientPublicKey = ed.getPublicKey(clientSecretKey); + + await this.server.connect(clientPublicKey, clientSecretKey); if (!this.server.serverPublicKey) { console.error("Failed to get public key"); @@ -68,4 +76,12 @@ export default class Enclave { publicKey: base58.encode(this.server.serverPublicKey), }; } + + public getClientSecretKey() { + if (!this.accounts) return null; + + const account = this.accounts?.accounts[this.accounts.activeAccount]; + + return account ? base58.decode(account.privateKey) : null; + } } diff --git a/src/app/server.ts b/src/app/server.ts index 92307e7..a926d7c 100644 --- a/src/app/server.ts +++ b/src/app/server.ts @@ -2,6 +2,7 @@ import { base58 } from "@scure/base"; import * as ed from "@noble/ed25519"; import EnclaveWebSocket from "./ws"; import { sha512 } from "@noble/hashes/sha2.js"; +import { getWSUrl } from "@/lib/serverList"; ed.hashes.sha512 = sha512; @@ -24,10 +25,12 @@ ed.hashes.sha512 = sha512; export default class EnclaveServer { public serverPublicKey?: Uint8Array; public hostname: string; + public isSecure: boolean; public websocket?: EnclaveWebSocket; - public constructor(hostname: string) { + public constructor(hostname: string, isSecure: boolean) { this.hostname = hostname; + this.isSecure = isSecure; } public disconnect() { @@ -39,7 +42,9 @@ export default class EnclaveServer { clientPublicKey: Uint8Array, clientSecretKey: Uint8Array, ) { - this.websocket = new EnclaveWebSocket(this.hostname); + this.websocket = new EnclaveWebSocket( + getWSUrl(this.hostname, this.isSecure), + ); const publicKeyString = base58.encode(clientPublicKey); diff --git a/src/app/ws.ts b/src/app/ws.ts index a34a7c5..6b08d04 100644 --- a/src/app/ws.ts +++ b/src/app/ws.ts @@ -14,7 +14,7 @@ export default class EnclaveWebSocket { public constructor(hostname: string) { this.onOpenQueue = new Array(); - this.websocket = new WebSocket("ws://" + hostname); + this.websocket = new WebSocket(hostname); this.websocket.onopen = () => { this.onOpenQueue.forEach((fun) => fun()); }; diff --git a/src/lib/accounts.ts b/src/lib/accounts.ts index b6496e2..8d2a20d 100644 --- a/src/lib/accounts.ts +++ b/src/lib/accounts.ts @@ -6,7 +6,7 @@ export interface Account { } export interface AccountsFile { - activeAccount: number | null; + activeAccount: number; accounts: Account[]; } diff --git a/src/lib/serverList.ts b/src/lib/serverList.ts index 33bdcc5..89a52e1 100644 --- a/src/lib/serverList.ts +++ b/src/lib/serverList.ts @@ -18,16 +18,13 @@ export async function getServerList(): Promise { } export function getHTTPUrl(hostname: string, isSecure: boolean, path: string) { - return ( - (isSecure ? "https://" : "http://") + - (hostname.includes(":") ? hostname : hostname + ":3415") + - path - ); + return (isSecure ? "https://" : "http://") + getHostname(hostname) + path; } export function getWSUrl(hostname: string, isSecure: boolean) { - return ( - (isSecure ? "wss://" : "ws://") + - (hostname.includes(":") ? hostname : hostname + ":3415") - ); + return (isSecure ? "wss://" : "ws://") + getHostname(hostname); +} + +export function getHostname(hostname: string) { + return hostname.includes(":") ? hostname : hostname + ":3415"; }