Client key
This commit is contained in:
+23
-7
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
+7
-2
@@ -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);
|
||||
|
||||
|
||||
+1
-1
@@ -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());
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user