Protocol
This commit is contained in:
@@ -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;
|
||||
};
|
||||
@@ -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<ClientMethod> {
|
||||
return new Promise((ok) => {
|
||||
this.onmessage = (msg) => {
|
||||
const data = JSON.parse(msg.data);
|
||||
ok(data);
|
||||
this.onmessage = null;
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user