From 350ced81e624a18442d7051e4a7add5d28417289 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Sun, 16 Aug 2026 06:34:14 +0200 Subject: [PATCH] Protocol --- src/protocol/protocol.ts | 35 +++++++++++++++++++++++++++++++++++ src/protocol/ws.ts | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 src/protocol/protocol.ts create mode 100644 src/protocol/ws.ts diff --git a/src/protocol/protocol.ts b/src/protocol/protocol.ts new file mode 100644 index 0000000..49a57d2 --- /dev/null +++ b/src/protocol/protocol.ts @@ -0,0 +1,35 @@ +export interface ClientMeta {} + +export interface ServerMeta { + name: string; + description: string; +} + +export type ClientMethod = + | { + method: "Initialized"; + public_key: string; + signature: string; + timestamp: number; + hostname: string; + } + | { + method: "Error"; + error: string; + }; + +export type ServerMethod = + | { + method: "Initialize"; + public_key: string; + signature: string; + timestamp: number; + hostname: string; + } + | { + method: "Meta"; + } + | { + method: "Error"; + error: string; + }; diff --git a/src/protocol/ws.ts b/src/protocol/ws.ts new file mode 100644 index 0000000..2a4fa7e --- /dev/null +++ b/src/protocol/ws.ts @@ -0,0 +1,35 @@ +import { ClientMethod, ServerMethod } from "./protocol"; + +export class EnclaveWebSocket { + public websocket: WebSocket; + onmessage: ((this: WebSocket, ev: MessageEvent) => any) | null; + + public clientPublicKey: Uint8Array; + public clientSecretKey: Uint8Array; + + public serverPublicKey: Uint8Array; + + public constructor(url: string | URL) { + this.websocket = new WebSocket(url); + this.clientPublicKey = new Uint8Array(); + this.clientSecretKey = new Uint8Array(); + + this.serverPublicKey = new Uint8Array(); + + this.onmessage = null; + } + + public send(method: ServerMethod) { + this.websocket.send(JSON.stringify(method)); + } + + public async read(): Promise { + return new Promise((ok) => { + this.onmessage = (msg) => { + const data = JSON.parse(msg.data); + ok(data); + this.onmessage = null; + }; + }); + } +}