-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(rln-js): convert to typescript (#54)
- Loading branch information
1 parent
066be09
commit cb50310
Showing
12 changed files
with
3,648 additions
and
10,885 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14,165 changes: 3,396 additions & 10,769 deletions
14,165
examples/experimental/rln-js/package-lock.json
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export type StatusChangeArgs = { | ||
newStatus: string, | ||
className?: string | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
}; |
Oops, something went wrong.