Skip to content

Commit cb50310

Browse files
chore(rln-js): convert to typescript (#54)
1 parent 066be09 commit cb50310

File tree

12 files changed

+3648
-10885
lines changed

12 files changed

+3648
-10885
lines changed

examples/experimental/rln-identity/webpack.config.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,27 @@ const CopyWebpackPlugin = require("copy-webpack-plugin");
22
const path = require("path");
33

44
module.exports = {
5-
entry: "./src/index.js",
5+
entry: "./src/index.ts",
66
output: {
77
path: path.resolve(__dirname, "build"),
8-
filename: "./index.js",
8+
filename: "index.js",
99
},
1010
experiments: {
1111
asyncWebAssembly: true,
1212
},
1313
mode: "development",
14+
module: {
15+
rules: [
16+
{
17+
test: /\.tsx?$/,
18+
use: 'ts-loader',
19+
exclude: /node_modules/,
20+
},
21+
],
22+
},
23+
resolve: {
24+
extensions: ['.tsx', '.ts', '.js'],
25+
},
1426
plugins: [
1527
new CopyWebpackPlugin({
1628
patterns: ["index.html", "favicon.ico", "favicon.png", "manifest.json"],

examples/experimental/rln-js/package-lock.json

Lines changed: 3396 additions & 10769 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/experimental/rln-js/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
"copy-webpack-plugin": "^11.0.0",
1818
"eslint": "^8",
1919
"eslint-config-next": "13.5.6",
20+
"ts-loader": "^9.5.1",
21+
"typescript": "^5.3.3",
2022
"webpack": "^5.74.0",
2123
"webpack-cli": "^4.10.0",
2224
"webpack-dev-server": "^4.11.1"

examples/experimental/rln-js/src/rln.js

Lines changed: 0 additions & 41 deletions
This file was deleted.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { RLNInstance, createRLN, extractMetaMaskSigner } from "@waku/rln";
2+
import { Signer } from "ethers";
3+
import { StatusChangeArgs } from "./types";
4+
5+
export async function initRLN(onStatusChange: ({}: StatusChangeArgs) => void) {
6+
onStatusChange({
7+
newStatus: "Initializing RLN..."
8+
});
9+
10+
let rln: RLNInstance;
11+
try {
12+
console.time("createRLN")
13+
rln = await createRLN();
14+
console.timeEnd("createRLN")
15+
} catch (err) {
16+
onStatusChange({
17+
newStatus: `Failed to initialize RLN: ${err}`,
18+
className: "error"
19+
});
20+
throw err;
21+
}
22+
23+
onStatusChange({
24+
newStatus: "RLN initialized",
25+
className: "success"
26+
});
27+
28+
const connectWallet = async () => {
29+
let signer: Signer;
30+
try {
31+
onStatusChange({
32+
newStatus: "Connecting to wallet..."
33+
});
34+
signer = await extractMetaMaskSigner();
35+
console.log("done")
36+
} catch (err) {
37+
onStatusChange({
38+
newStatus: `Failed to access MetaMask: ${err}`,
39+
className: "error"
40+
});
41+
throw err;
42+
}
43+
44+
try {
45+
onStatusChange({
46+
newStatus: "Connecting to Ethereum..."
47+
});
48+
await rln.start({ signer });
49+
} catch (err) {
50+
onStatusChange({
51+
newStatus: `Failed to connect to Ethereum: ${err}`,
52+
className: "error"
53+
});
54+
throw err;
55+
}
56+
57+
onStatusChange({
58+
newStatus: "RLN started",
59+
className: "success"
60+
});
61+
};
62+
63+
return {
64+
rln,
65+
connectWallet,
66+
};
67+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export type StatusChangeArgs = {
2+
newStatus: string,
3+
className?: string
4+
}

examples/experimental/rln-js/src/ui.js

Lines changed: 0 additions & 61 deletions
This file was deleted.
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import { StatusChangeArgs } from "./types";
2+
3+
const status = document.getElementById("status");
4+
const chat = document.getElementById("chat-area");
5+
const messages = document.getElementById("messages");
6+
7+
const nickInput = document.getElementById("nick") as HTMLInputElement;
8+
const textInput = document.getElementById("text") as HTMLInputElement;
9+
const sendButton = document.getElementById("send") as HTMLInputElement;
10+
11+
const connectWalletButton = document.getElementById("connect");
12+
13+
export const initUI = () => {
14+
const onStatusChange = ({ newStatus, className }: StatusChangeArgs) => {
15+
if (!status) {
16+
console.log("Status element not found.");
17+
return;
18+
}
19+
status.innerText = newStatus;
20+
status.className = className || "progress";
21+
};
22+
23+
const onLoaded = () => {
24+
if (!chat) {
25+
console.log("Chat element not found.");
26+
return;
27+
}
28+
chat.style.display = "block";
29+
};
30+
31+
const _renderMessage = (nick: string, text: string, timestamp: number, proofStatus: string) => {
32+
if (!messages) {
33+
console.log("Messages element not found.");
34+
return;
35+
}
36+
messages.innerHTML += `
37+
<li>
38+
(${nick})(${proofStatus})
39+
<strong>${text}</strong>
40+
<i>[${new Date(timestamp).toISOString()}]</i>
41+
</li>
42+
`;
43+
};
44+
45+
type Events = {
46+
connectWallet: () => Promise<void>,
47+
onInitWaku: () => Promise<void>,
48+
onSubscribe: (cb: (nick: string, text: string, timestamp: number, proofStatus: string) => void) => void,
49+
onSend: (nick: string, text: string) => Promise<void>,
50+
}
51+
52+
const registerEvents = (events: Events) => {
53+
if (!connectWalletButton) {
54+
console.log("Connect wallet button not found.");
55+
return;
56+
}
57+
58+
if (!sendButton) {
59+
console.log("Send button not found.");
60+
return;
61+
}
62+
63+
connectWalletButton.addEventListener("click", async () => {
64+
await events.connectWallet();
65+
await events.onInitWaku();
66+
67+
onLoaded();
68+
69+
events.onSubscribe((nick: string, text: string, timestamp: number, proofStatus: string) => {
70+
_renderMessage(nick, text, timestamp, proofStatus);
71+
});
72+
73+
74+
sendButton.addEventListener("click", async () => {
75+
if (!nickInput || !textInput) {
76+
console.log("Nick or text input not found.");
77+
return;
78+
}
79+
if (!events.onSend) {
80+
console.log("onSend event not found.");
81+
return;
82+
}
83+
84+
const nick = nickInput.value;
85+
const text = textInput.value;
86+
87+
if (!nick || !text) {
88+
console.log("Not sending message: missing nick or text.");
89+
return;
90+
}
91+
92+
await events.onSend(nick, text);
93+
textInput.value = "";
94+
});
95+
});
96+
};
97+
98+
return {
99+
registerEvents,
100+
onStatusChange,
101+
};
102+
};

0 commit comments

Comments
 (0)