Skip to content

Commit 35384f3

Browse files
author
Benedikt Strehle
committed
simplify stuff with pointers + 'using' keyword
1 parent 6a7715c commit 35384f3

File tree

4 files changed

+75
-50
lines changed

4 files changed

+75
-50
lines changed

backend/entrypoint.tsx

Lines changed: 0 additions & 6 deletions
This file was deleted.

common/TOR-Worker.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@ const ed = await import("https://unpkg.com/@noble/ed25519@2.0.0/index.js");
22
const base32 = await import("https://cdn.jsdelivr.net/npm/hi-base32@0.5.1/+esm");
33
await import("https://cdn.jsdelivr.net/npm/js-sha3@0.9.2/src/sha3.min.js");
44

5+
6+
export type AddressData = {
7+
address: string;
8+
public: {
9+
raw: Uint8Array;
10+
b64: string;
11+
};
12+
private: {
13+
raw: Uint8Array;
14+
b64: string;
15+
};
16+
}
17+
518
declare const sha3_256: any;
619
type KeyPair = {
720
private: PrivateKey,
@@ -23,15 +36,15 @@ class PrivateKey {
2336
}
2437
}
2538

26-
const generateKeys = async (privateKey = ed.utils.randomPrivateKey()): Promise<KeyPair> => {
39+
async function generateKeys(privateKey = ed.utils.randomPrivateKey()): Promise<KeyPair> {
2740
const publicKey = await ed.getPublicKeyAsync(privateKey);
2841
return {
2942
private: new PrivateKey(privateKey),
3043
public: new PublicKey(publicKey)
3144
};
3245
}
3346

34-
const getPublicKey = (address: string) => {
47+
function getPublicKey(address: string) {
3548
if (!/\.onion$/i.test(address) || address.length != 56 + 6)
3649
throw new Error("Invalid length");
3750
const base32Encoded = address.substring(0, address.length - 6).toUpperCase();
@@ -55,7 +68,7 @@ const getPublicKey = (address: string) => {
5568
return new PublicKey(new Uint8Array(publicKey));
5669
}
5770

58-
const generateOnionV3 = async (keys: KeyPair | Promise<KeyPair> = generateKeys()) => {
71+
async function generateOnionV3(keys: KeyPair | Promise<KeyPair> = generateKeys()):Promise<AddressData> {
5972
keys = await keys;
6073

6174
const publicKey = keys.public.value;
@@ -87,7 +100,7 @@ const generateOnionV3 = async (keys: KeyPair | Promise<KeyPair> = generateKeys()
87100
}
88101
}
89102

90-
export const generateVanityAddress = async (prefix?: string) => {
103+
export async function generateVanityAddress(prefix?: string) {
91104
let onion = await generateOnionV3();
92105
while (prefix && !onion.address.startsWith(prefix))
93106
onion = await generateOnionV3();

common/components/MainPage.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
flex-direction: column;
7070
}
7171
}
72-
&>.tor {
72+
&>#tor {
7373
span {
7474
max-width: 1000px;
7575
margin: auto;

common/components/MainPage.tsx

Lines changed: 57 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,74 @@
1-
import { Path } from "uix/utils/path.ts";
21
import { UIX } from "uix";
3-
import { spawnThreads, spawnThread, disposeThread } from "unyt_core/threads/threads.ts";
2+
import { spawnThreads, spawnThread } from "unyt_core/threads/threads.ts";
3+
import { always, map } from "unyt_core/functions.ts";
4+
import type { AddressData } from "common/TOR-Worker.ts";
45

56
@UIX.template(function(this: MainPage) {
67
return <div>
7-
<div class="tor">
8-
<h2>Multi-Threading TOR Address</h2>
9-
<input id="torAddress" maxlength={3} type={"text"} placeholder={"Prefix of vanity address"}/>
8+
<div id="tor" class={always(()=>this.calculatingAddress?'hidden':'')}>
9+
<h2>Create TOR Address</h2>
10+
<input id="torAddress" maxlength="3" type="text" placeholder="Prefix of vanity address" value={this.$.addressPrefix}/>
1011
<div onclick={() => this.createVanityAddress()} class="button">Compute</div>
11-
<section class="results"></section>
12+
<section class="results">
13+
{map(this.resultAddresses, address =>
14+
<span>
15+
<a>{address.address}</a>
16+
<b>Pub: {address.public.b64}</b>
17+
<b>Priv: {address.private.b64}</b>
18+
</span>
19+
)}
20+
</section>
1221
</div>
13-
<div class="pi">
14-
<h2>How many digits of PI to calculate?</h2>
15-
<input id="inputPiDigits" type={"number"} placeholder={"Input number"}/>
22+
<div id="pi" class={always(()=>this.calculatingPI?'hidden':'')}>
23+
<h2>Calculate PI</h2>
24+
<input id="inputPiDigits" type="number" placeholder="Number of digits" value={this.$.piDigits}/>
1625
<div onclick={() => this.computePI()} class="button">Compute</div>
17-
<section class="results"></section>
26+
<section class="results">
27+
{map(this.resultPIs, pi =>
28+
<span>{pi}</span>
29+
)}
30+
</section>
1831
</div>
1932
</div>
2033
})
2134
export class MainPage extends UIX.BaseComponent {
22-
@id declare inputPiDigits: HTMLInputElement;
23-
@id declare torAddress: HTMLInputElement;
35+
36+
// references properties to read input values
37+
@property piDigits = 5;
38+
@property addressPrefix = "";
39+
40+
// reference properties to get calculation state
41+
@property calculatingPI = false
42+
@property calculatingAddress = false
43+
44+
// arrays containing history of calculated results
45+
@property resultPIs:string[] = []
46+
@property resultAddresses:AddressData[] = []
2447

2548
async createVanityAddress() {
26-
const parent = this.torAddress.parentElement!;
27-
if (parent.classList.contains("hidden"))
28-
return;
29-
parent.classList.add("hidden");
30-
31-
const threads = await spawnThreads<typeof import('../TOR-Worker.ts')>(new Path('../TOR-Worker.ts'), 10);
32-
const calculations = threads.map(thread => thread.generateVanityAddress(this.torAddress.value));
33-
const result = await Promise.any(calculations);
34-
console.log("Found address", result);
35-
parent.querySelector("section")!.prepend(<span>
36-
<a>{result.address}</a>
37-
<b>Pub: {result.public.b64}</b>
38-
<b>Priv: {result.private.b64}</b>
39-
</span>)
40-
parent.classList.remove("hidden");
41-
disposeThread(...threads);
49+
this.calculatingAddress = true;
50+
51+
// spawn 10 new threads
52+
using threads = await spawnThreads<typeof import('../TOR-Worker.ts')>('../TOR-Worker.ts', 10);
53+
// try to find an address in parallel
54+
const address = await Promise.any(
55+
threads.map(thread => thread.generateVanityAddress(this.addressPrefix))
56+
);
57+
this.resultAddresses.unshift(address);
58+
59+
this.calculatingAddress = false;
4260
}
4361

4462
async computePI() {
45-
const parent = this.inputPiDigits.parentElement!;
46-
if (parent.classList.contains("hidden"))
47-
return;
48-
parent.classList.add("hidden");
49-
50-
const thread = await spawnThread<typeof import('../PI-Worker.ts')>(new Path('../PI-Worker.ts'));
51-
const pi = await thread.calculatePI(+this.inputPiDigits.value || 10);
52-
parent.querySelector("section")!.prepend(<span>{pi}</span>)
53-
parent.classList.remove("hidden");
54-
disposeThread(thread);
63+
this.calculatingPI = true;
64+
65+
// spawn a new thread
66+
using thread = await spawnThread<typeof import('../PI-Worker.ts')>('../PI-Worker.ts');
67+
// calculate pi in the thread
68+
const pi = await thread.calculatePI(this.piDigits);
69+
this.resultPIs.unshift(pi);
70+
71+
this.calculatingPI = false;
72+
5573
}
56-
}
74+
}

0 commit comments

Comments
 (0)