Init
This commit is contained in:
+2
-48
@@ -4,7 +4,6 @@ import { invoke } from "@tauri-apps/api/core";
|
||||
import "./App.css";
|
||||
import { EnclaveWebSocket } from "./protocol/ws";
|
||||
import * as ed from "@noble/ed25519";
|
||||
import { base58 } from "@scure/base";
|
||||
import { sha512 } from "@noble/hashes/sha2.js";
|
||||
|
||||
ed.hashes.sha512 = sha512;
|
||||
@@ -20,55 +19,10 @@ function App() {
|
||||
initialized.current = true;
|
||||
|
||||
(async () => {
|
||||
const ws = new EnclaveWebSocket("ws://localhost:3415");
|
||||
|
||||
const timestamp = Date.now();
|
||||
const hostname = "localhost:3415";
|
||||
|
||||
const msg = new TextEncoder().encode(`${timestamp}@${hostname}`);
|
||||
|
||||
const ws = new EnclaveWebSocket("localhost:3415");
|
||||
ws.clientSecretKey = ed.utils.randomSecretKey();
|
||||
|
||||
ws.clientPublicKey = ed.getPublicKey(ws.clientSecretKey);
|
||||
|
||||
const publicKeyString = base58.encode(ws.clientPublicKey);
|
||||
|
||||
ws.send({
|
||||
method: "Initialize",
|
||||
public_key: publicKeyString,
|
||||
signature: base58.encode(ed.sign(msg, ws.clientSecretKey)),
|
||||
|
||||
timestamp,
|
||||
hostname,
|
||||
});
|
||||
|
||||
const initialized = await ws.read();
|
||||
|
||||
if (initialized.method !== "Initialized") {
|
||||
console.error("Invalid method from server, closing. ", initialized);
|
||||
ws.websocket.close();
|
||||
return;
|
||||
}
|
||||
|
||||
if (initialized.hostname !== hostname) {
|
||||
console.error("Server is trying to be a middle man", initialized);
|
||||
ws.websocket.close();
|
||||
return;
|
||||
}
|
||||
|
||||
const sigMsg = new TextEncoder().encode(
|
||||
`${initialized.timestamp}@${hostname}@${publicKeyString}`,
|
||||
);
|
||||
|
||||
ws.serverPublicKey = base58.decode(initialized.public_key);
|
||||
const signature = base58.decode(initialized.signature);
|
||||
|
||||
if (!ed.verify(signature, sigMsg, ws.serverPublicKey)) {
|
||||
console.error("Invalid signature", initialized);
|
||||
ws.websocket.close();
|
||||
return;
|
||||
}
|
||||
|
||||
ws.init();
|
||||
console.log("OK");
|
||||
})();
|
||||
}, []);
|
||||
|
||||
Reference in New Issue
Block a user