From 35384f31244489c44107934f743a67d41392e3dd Mon Sep 17 00:00:00 2001 From: Benedikt Strehle Date: Sat, 23 Sep 2023 23:14:57 +0200 Subject: [PATCH] simplify stuff with pointers + 'using' keyword --- backend/entrypoint.tsx | 6 --- common/TOR-Worker.ts | 21 ++++++-- common/components/MainPage.scss | 2 +- common/components/MainPage.tsx | 96 +++++++++++++++++++-------------- 4 files changed, 75 insertions(+), 50 deletions(-) delete mode 100644 backend/entrypoint.tsx diff --git a/backend/entrypoint.tsx b/backend/entrypoint.tsx deleted file mode 100644 index b898f71..0000000 --- a/backend/entrypoint.tsx +++ /dev/null @@ -1,6 +0,0 @@ -import { UIX } from "uix/uix.ts"; - -// The backend routes definition -export default { - '/': null, -} satisfies UIX.Entrypoint; \ No newline at end of file diff --git a/common/TOR-Worker.ts b/common/TOR-Worker.ts index aa00027..cbc0029 100644 --- a/common/TOR-Worker.ts +++ b/common/TOR-Worker.ts @@ -2,6 +2,19 @@ const ed = await import("https://unpkg.com/@noble/ed25519@2.0.0/index.js"); const base32 = await import("https://cdn.jsdelivr.net/npm/hi-base32@0.5.1/+esm"); await import("https://cdn.jsdelivr.net/npm/js-sha3@0.9.2/src/sha3.min.js"); + +export type AddressData = { + address: string; + public: { + raw: Uint8Array; + b64: string; + }; + private: { + raw: Uint8Array; + b64: string; + }; +} + declare const sha3_256: any; type KeyPair = { private: PrivateKey, @@ -23,7 +36,7 @@ class PrivateKey { } } -const generateKeys = async (privateKey = ed.utils.randomPrivateKey()): Promise => { +async function generateKeys(privateKey = ed.utils.randomPrivateKey()): Promise { const publicKey = await ed.getPublicKeyAsync(privateKey); return { private: new PrivateKey(privateKey), @@ -31,7 +44,7 @@ const generateKeys = async (privateKey = ed.utils.randomPrivateKey()): Promise { +function getPublicKey(address: string) { if (!/\.onion$/i.test(address) || address.length != 56 + 6) throw new Error("Invalid length"); const base32Encoded = address.substring(0, address.length - 6).toUpperCase(); @@ -55,7 +68,7 @@ const getPublicKey = (address: string) => { return new PublicKey(new Uint8Array(publicKey)); } -const generateOnionV3 = async (keys: KeyPair | Promise = generateKeys()) => { +async function generateOnionV3(keys: KeyPair | Promise = generateKeys()):Promise { keys = await keys; const publicKey = keys.public.value; @@ -87,7 +100,7 @@ const generateOnionV3 = async (keys: KeyPair | Promise = generateKeys() } } -export const generateVanityAddress = async (prefix?: string) => { +export async function generateVanityAddress(prefix?: string) { let onion = await generateOnionV3(); while (prefix && !onion.address.startsWith(prefix)) onion = await generateOnionV3(); diff --git a/common/components/MainPage.scss b/common/components/MainPage.scss index 1a2809c..bb3f2ec 100644 --- a/common/components/MainPage.scss +++ b/common/components/MainPage.scss @@ -69,7 +69,7 @@ flex-direction: column; } } - &>.tor { + &>#tor { span { max-width: 1000px; margin: auto; diff --git a/common/components/MainPage.tsx b/common/components/MainPage.tsx index f59b4c0..52ddaf5 100644 --- a/common/components/MainPage.tsx +++ b/common/components/MainPage.tsx @@ -1,56 +1,74 @@ -import { Path } from "uix/utils/path.ts"; import { UIX } from "uix"; -import { spawnThreads, spawnThread, disposeThread } from "unyt_core/threads/threads.ts"; +import { spawnThreads, spawnThread } from "unyt_core/threads/threads.ts"; +import { always, map } from "unyt_core/functions.ts"; +import type { AddressData } from "common/TOR-Worker.ts"; @UIX.template(function(this: MainPage) { return
-
-

Multi-Threading TOR Address

- +
this.calculatingAddress?'hidden':'')}> +

Create TOR Address

+
this.createVanityAddress()} class="button">Compute
-
+
+ {map(this.resultAddresses, address => + + {address.address} + Pub: {address.public.b64} + Priv: {address.private.b64} + + )} +
-
-

How many digits of PI to calculate?

- +
this.calculatingPI?'hidden':'')}> +

Calculate PI

+
this.computePI()} class="button">Compute
-
+
+ {map(this.resultPIs, pi => + {pi} + )} +
}) export class MainPage extends UIX.BaseComponent { - @id declare inputPiDigits: HTMLInputElement; - @id declare torAddress: HTMLInputElement; + + // references properties to read input values + @property piDigits = 5; + @property addressPrefix = ""; + + // reference properties to get calculation state + @property calculatingPI = false + @property calculatingAddress = false + + // arrays containing history of calculated results + @property resultPIs:string[] = [] + @property resultAddresses:AddressData[] = [] async createVanityAddress() { - const parent = this.torAddress.parentElement!; - if (parent.classList.contains("hidden")) - return; - parent.classList.add("hidden"); - - const threads = await spawnThreads(new Path('../TOR-Worker.ts'), 10); - const calculations = threads.map(thread => thread.generateVanityAddress(this.torAddress.value)); - const result = await Promise.any(calculations); - console.log("Found address", result); - parent.querySelector("section")!.prepend( - {result.address} - Pub: {result.public.b64} - Priv: {result.private.b64} - ) - parent.classList.remove("hidden"); - disposeThread(...threads); + this.calculatingAddress = true; + + // spawn 10 new threads + using threads = await spawnThreads('../TOR-Worker.ts', 10); + // try to find an address in parallel + const address = await Promise.any( + threads.map(thread => thread.generateVanityAddress(this.addressPrefix)) + ); + this.resultAddresses.unshift(address); + + this.calculatingAddress = false; } async computePI() { - const parent = this.inputPiDigits.parentElement!; - if (parent.classList.contains("hidden")) - return; - parent.classList.add("hidden"); - - const thread = await spawnThread(new Path('../PI-Worker.ts')); - const pi = await thread.calculatePI(+this.inputPiDigits.value || 10); - parent.querySelector("section")!.prepend({pi}) - parent.classList.remove("hidden"); - disposeThread(thread); + this.calculatingPI = true; + + // spawn a new thread + using thread = await spawnThread('../PI-Worker.ts'); + // calculate pi in the thread + const pi = await thread.calculatePI(this.piDigits); + this.resultPIs.unshift(pi); + + this.calculatingPI = false; + } -} +} \ No newline at end of file