Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(rln-js): convert to typescript #54

Merged
merged 3 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4,550 changes: 2,454 additions & 2,096 deletions examples/rln-js/package-lock.json

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions examples/rln-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@
"start": "webpack-dev-server"
},
"dependencies": {
"@waku/rln": "0.1.2-126bce3",
"@waku/rln": "0.1.2-e1679b6",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

@danisharora099 danisharora099 Jan 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's now already upgraded to latest - 0.13.0; rebased

"@waku/sdk": "^0.0.21",
"@waku/utils": "^0.0.14",
"multiaddr": "^10.0.1",
"protobufjs": "^7.2.5"
},
"devDependencies": {
"copy-webpack-plugin": "^11.0.0",
"eslint": "^8",
"eslint-config-next": "13.5.6",
"copy-webpack-plugin": "^11.0.0",
"ts-loader": "^9.5.1",
"typescript": "^5.3.3",
"webpack": "^5.74.0",
"webpack-cli": "^4.10.0",
"webpack-dev-server": "^4.11.1"
Expand Down
File renamed without changes.
File renamed without changes.
41 changes: 0 additions & 41 deletions examples/rln-js/src/rln.js

This file was deleted.

67 changes: 67 additions & 0 deletions examples/rln-js/src/rln.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { RLNInstance, createRLN, extractMetaMaskSigner } from "@waku/rln";
import { Signer } from "ethers";
import { StatusChangeArgs } from "./types";

export async function initRLN(onStatusChange: ({}: StatusChangeArgs) => void) {
onStatusChange({
newStatus: "Initializing RLN..."
});

let rln: RLNInstance;
try {
console.time("createRLN")
rln = await createRLN();
console.timeEnd("createRLN")
} catch (err) {
onStatusChange({
newStatus: `Failed to initialize RLN: ${err}`,
className: "error"
});
throw err;
}

onStatusChange({
newStatus: "RLN initialized",
className: "success"
});

const connectWallet = async () => {
let signer: Signer;
try {
onStatusChange({
newStatus: "Connecting to wallet..."
});
signer = await extractMetaMaskSigner();
console.log("done")
} catch (err) {
onStatusChange({
newStatus: `Failed to access MetaMask: ${err}`,
className: "error"
});
throw err;
}

try {
onStatusChange({
newStatus: "Connecting to Ethereum..."
});
await rln.start({ signer });
} catch (err) {
onStatusChange({
newStatus: `Failed to connect to Ethereum: ${err}`,
className: "error"
});
throw err;
}

onStatusChange({
newStatus: "RLN started",
className: "success"
});
};

return {
rln,
connectWallet,
};
}
4 changes: 4 additions & 0 deletions examples/rln-js/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type StatusChangeArgs = {
newStatus: string,
className?: string
}
61 changes: 0 additions & 61 deletions examples/rln-js/src/ui.js

This file was deleted.

102 changes: 102 additions & 0 deletions examples/rln-js/src/ui.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { StatusChangeArgs } from "./types";

const status = document.getElementById("status");
const chat = document.getElementById("chat-area");
const messages = document.getElementById("messages");

const nickInput = document.getElementById("nick") as HTMLInputElement;
const textInput = document.getElementById("text") as HTMLInputElement;
const sendButton = document.getElementById("send") as HTMLInputElement;

const connectWalletButton = document.getElementById("connect");

export const initUI = () => {
const onStatusChange = ({ newStatus, className }: StatusChangeArgs) => {
if (!status) {
console.log("Status element not found.");
return;
}
status.innerText = newStatus;
status.className = className || "progress";
};

const onLoaded = () => {
if (!chat) {
console.log("Chat element not found.");
return;
}
chat.style.display = "block";
};

const _renderMessage = (nick: string, text: string, timestamp: number, proofStatus: string) => {
if (!messages) {
console.log("Messages element not found.");
return;
}
messages.innerHTML += `
<li>
(${nick})(${proofStatus})
<strong>${text}</strong>
<i>[${new Date(timestamp).toISOString()}]</i>
</li>
`;
};

type Events = {
connectWallet: () => Promise<void>,
onInitWaku: () => Promise<void>,
onSubscribe: (cb: (nick: string, text: string, timestamp: number, proofStatus: string) => void) => void,
onSend: (nick: string, text: string) => Promise<void>,
}

const registerEvents = (events: Events) => {
if (!connectWalletButton) {
console.log("Connect wallet button not found.");
return;
}

if (!sendButton) {
console.log("Send button not found.");
return;
}

connectWalletButton.addEventListener("click", async () => {
await events.connectWallet();
await events.onInitWaku();

onLoaded();

events.onSubscribe((nick: string, text: string, timestamp: number, proofStatus: string) => {
_renderMessage(nick, text, timestamp, proofStatus);
});


sendButton.addEventListener("click", async () => {
if (!nickInput || !textInput) {
console.log("Nick or text input not found.");
return;
}
if (!events.onSend) {
console.log("onSend event not found.");
return;
}

const nick = nickInput.value;
const text = textInput.value;

if (!nick || !text) {
console.log("Not sending message: missing nick or text.");
return;
}

await events.onSend(nick, text);
textInput.value = "";
});
});
};

return {
registerEvents,
onStatusChange,
};
};
Loading
Loading