2022-09-26 00:41:30 +00:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html lang='en'>
|
|
|
|
|
|
|
|
<head>
|
|
|
|
<meta charset='UTF-8'/>
|
|
|
|
<meta content='width=device-width, initial-scale=1.0' name='viewport'/>
|
|
|
|
<title>JS-Waku light node example</title>
|
|
|
|
</head>
|
|
|
|
|
|
|
|
<body>
|
|
|
|
|
|
|
|
<div><h1>RLN</h1>
|
2022-09-27 16:24:43 +00:00
|
|
|
<div><h2>Wallet</h2>
|
2022-10-04 04:03:14 +00:00
|
|
|
<div id='wallet'></div>
|
|
|
|
<button disabled id='connect-wallet' type='button'>Connect Wallet</button>
|
2022-09-27 16:24:43 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div><h2>RLN</h2>
|
2022-10-04 04:18:26 +00:00
|
|
|
<button disabled id='retrieve-rln-details' type='button'>Retrieve RLN contract state from blockchain</button>
|
2022-09-27 16:24:43 +00:00
|
|
|
</div>
|
|
|
|
|
2022-09-27 14:35:36 +00:00
|
|
|
<p>You can either generate new credentials:</p><br/>
|
2022-10-04 04:03:14 +00:00
|
|
|
<button disabled id='generate-credentials' type='button'>Generate RLN Credentials</button>
|
2022-09-27 14:35:36 +00:00
|
|
|
<p>Or import existing ones:</p><br/>
|
|
|
|
|
|
|
|
<div>
|
|
|
|
<label for="membership-id">Membership ID (your index in the RLN smart contract):</label>
|
|
|
|
<input id="membership-id" name="membership-id" type="text"><br>
|
|
|
|
<label for="id-key">RLN Identity Key (hex string):</label>
|
|
|
|
<input id="id-key" name="id-key" type="text"><br>
|
|
|
|
<label for="commitment-key">RLN Commitment Key (hex string):</label>
|
|
|
|
<input id="commitment-key" name="commitment-key" type="text"><br>
|
|
|
|
<button disabled id='import-button' type='button'>Import RLN Credentials</button>
|
|
|
|
</div>
|
|
|
|
|
2022-09-26 00:41:30 +00:00
|
|
|
<div><h2>Credentials</h2>
|
2022-09-27 14:35:36 +00:00
|
|
|
<div><h3>Membership id</h3>
|
|
|
|
<div id="id"></div>
|
|
|
|
</div>
|
2022-09-26 00:41:30 +00:00
|
|
|
<div><h3>Key</h3>
|
|
|
|
<div id="key"></div>
|
|
|
|
</div>
|
|
|
|
<div><h3>Commitment</h3>
|
|
|
|
<div id="commitment"></div>
|
|
|
|
</div>
|
2022-10-04 04:03:14 +00:00
|
|
|
<button disabled id='register-button' type='button'>Register Credentials in Contract</button>
|
2022-09-26 00:41:30 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div><h1>Waku</h1>
|
|
|
|
<div id='status'></div>
|
|
|
|
<br/>
|
2022-10-04 04:18:26 +00:00
|
|
|
|
|
|
|
<label for='remote-multiaddr'>Remote peer's multiaddr</label>
|
|
|
|
<input id='remote-multiaddr'
|
|
|
|
type='text'
|
|
|
|
value="/dns4/node-01.ac-cn-hongkong-c.wakuv2.test.statusim.net/tcp/443/wss/p2p/16Uiu2HAkvWiyFsgRhuJEb9JfjYxEkoHLgnUQmr1N5mKWnYjxYRVm">
|
|
|
|
<button id='dial' type='button' disabled>Dial</button>
|
|
|
|
<br/>
|
2022-09-27 14:35:36 +00:00
|
|
|
<label for='nick-input'>Your nickname</label>
|
|
|
|
<input id='nick-input' placeholder='Choose a nickname' type='text'>
|
|
|
|
<button disabled id='set-nick' type='button'>Set nickname</button>
|
|
|
|
<br/>
|
|
|
|
|
2022-09-26 00:41:30 +00:00
|
|
|
<label for='textInput'>Message text</label>
|
2022-10-04 04:03:14 +00:00
|
|
|
<input disabled id='textInput' placeholder='Type your message here' type='text'>
|
|
|
|
<button disabled id='sendButton' type='button'>Send message using Light Push</button>
|
2022-09-26 00:41:30 +00:00
|
|
|
<br/>
|
|
|
|
<div id="messages"></div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<script type='module'>
|
2022-10-01 13:58:07 +00:00
|
|
|
import {utils} from 'https://unpkg.com/js-waku@0.29.0-29436ea/bundle/index.js';
|
|
|
|
import {createLightNode} from 'https://unpkg.com/js-waku@0.29.0-29436ea/bundle/lib/create_waku.js'
|
|
|
|
import {waitForRemotePeer} from 'https://unpkg.com/js-waku@0.29.0-29436ea/bundle/lib/wait_for_remote_peer.js'
|
|
|
|
import {EncoderV0, DecoderV0} from 'https://unpkg.com/js-waku@0.29.0-29436ea/bundle/lib/waku_message/version_0.js'
|
2022-09-27 14:35:36 +00:00
|
|
|
|
|
|
|
import {protobuf} from "https://taisukef.github.io/protobuf-es.js/dist/protobuf-es.js";
|
|
|
|
|
2022-10-01 13:58:07 +00:00
|
|
|
import {create, MembershipKey, RLNEncoder} from "https://unpkg.com/@waku/rln@0.0.10/bundle/index.js";
|
2022-09-26 00:41:30 +00:00
|
|
|
|
2022-09-27 16:24:43 +00:00
|
|
|
import {ethers} from "https://unpkg.com/ethers@5.0.7/dist/ethers-all.esm.min.js"
|
|
|
|
|
2022-09-27 14:35:36 +00:00
|
|
|
// RLN Elements
|
2022-09-26 00:41:30 +00:00
|
|
|
const generateCredsButton = document.getElementById('generate-credentials')
|
2022-09-27 14:35:36 +00:00
|
|
|
const idDiv = document.getElementById('id');
|
2022-09-26 00:41:30 +00:00
|
|
|
const keyDiv = document.getElementById('key');
|
|
|
|
const commitmentDiv = document.getElementById('commitment');
|
2022-09-27 14:35:36 +00:00
|
|
|
const membershipIdInput = document.getElementById('membership-id')
|
|
|
|
const identityKeyInput = document.getElementById('id-key')
|
|
|
|
const commitmentKeyInput = document.getElementById('commitment-key')
|
|
|
|
const importButton = document.getElementById('import-button')
|
2022-09-27 16:24:43 +00:00
|
|
|
const connectWalletButton = document.getElementById('connect-wallet');
|
|
|
|
const retrieveRLNDetailsButton = document.getElementById('retrieve-rln-details')
|
|
|
|
const registerButton = document.getElementById('register-button');
|
2022-09-26 00:41:30 +00:00
|
|
|
|
2022-09-27 14:35:36 +00:00
|
|
|
// Waku Elements
|
|
|
|
const statusDiv = document.getElementById('status');
|
|
|
|
|
2022-10-04 04:18:26 +00:00
|
|
|
const dialButton = document.getElementById('dial')
|
|
|
|
const remoteMultiAddrInput = document.getElementById('remote-multiaddr')
|
|
|
|
|
2022-09-27 14:35:36 +00:00
|
|
|
const nicknameInput = document.getElementById('nick-input')
|
|
|
|
const setNickButton = document.getElementById('set-nick')
|
|
|
|
|
|
|
|
const textInput = document.getElementById('textInput');
|
|
|
|
const sendButton = document.getElementById('sendButton');
|
2022-09-27 16:24:43 +00:00
|
|
|
const walletDiv = document.getElementById('wallet');
|
2022-09-27 14:35:36 +00:00
|
|
|
|
|
|
|
const messagesDiv = document.getElementById('messages')
|
|
|
|
|
|
|
|
let membershipId;
|
|
|
|
let membershipKey;
|
|
|
|
let encoder;
|
|
|
|
let nodeConnected;
|
|
|
|
let nick;
|
2022-09-27 16:24:43 +00:00
|
|
|
let retrievedRLNEvents = false;
|
2022-10-04 04:03:14 +00:00
|
|
|
let rlnInstance;
|
|
|
|
|
|
|
|
// Load zero-kit WASM blob.
|
|
|
|
(async () => {
|
|
|
|
rlnInstance = await create()
|
|
|
|
generateCredsButton.disabled = false;
|
|
|
|
connectWalletButton.disabled = false;
|
|
|
|
})();
|
|
|
|
|
2022-09-27 14:35:36 +00:00
|
|
|
const updateFields = () => {
|
2022-09-27 16:24:43 +00:00
|
|
|
if (membershipKey) {
|
2022-09-27 14:35:36 +00:00
|
|
|
keyDiv.innerHTML = utils.bytesToHex(membershipKey.IDKey)
|
|
|
|
commitmentDiv.innerHTML = utils.bytesToHex(membershipKey.IDCommitment)
|
2022-09-27 16:24:43 +00:00
|
|
|
idDiv.innerHTML = membershipId || "-"
|
2022-09-27 14:35:36 +00:00
|
|
|
|
2022-09-27 16:24:43 +00:00
|
|
|
if (membershipId) {
|
|
|
|
encoder = new RLNEncoder(
|
|
|
|
new EncoderV0(ContentTopic),
|
|
|
|
rlnInstance,
|
|
|
|
membershipId,
|
|
|
|
membershipKey
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2022-10-04 04:03:14 +00:00
|
|
|
|
2022-09-27 14:35:36 +00:00
|
|
|
importButton.disabled = !(membershipIdInput.value
|
|
|
|
&& identityKeyInput.value
|
|
|
|
&& commitmentKeyInput.value);
|
|
|
|
|
|
|
|
const readyToSend = (membershipKey && membershipId && nodeConnected && nick)
|
|
|
|
textInput.disabled = !readyToSend;
|
|
|
|
sendButton.disabled = !readyToSend;
|
|
|
|
|
|
|
|
setNickButton.disabled = !nicknameInput.value;
|
|
|
|
}
|
|
|
|
|
2022-09-26 00:41:30 +00:00
|
|
|
generateCredsButton.onclick = () => {
|
2022-09-27 14:35:36 +00:00
|
|
|
membershipKey = rlnInstance.generateMembershipKey()
|
2022-09-27 16:24:43 +00:00
|
|
|
updateFields();
|
2022-09-27 14:35:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
membershipIdInput.onchange = updateFields;
|
|
|
|
identityKeyInput.onchange = updateFields;
|
|
|
|
commitmentKeyInput.onchange = updateFields;
|
|
|
|
|
|
|
|
importButton.onclick = () => {
|
|
|
|
const idKey = utils.hexToBytes(identityKeyInput.value)
|
|
|
|
const idCommitment = utils.hexToBytes(commitmentKeyInput.value)
|
|
|
|
membershipKey = new MembershipKey(idKey, idCommitment)
|
2022-10-04 04:03:14 +00:00
|
|
|
membershipId = membershipIdInput.value;
|
2022-09-27 14:35:36 +00:00
|
|
|
updateFields()
|
2022-09-26 00:41:30 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 14:35:36 +00:00
|
|
|
// Protobuf
|
|
|
|
const ProtoChatMessage = new protobuf.Type("ChatMessage")
|
|
|
|
.add(new protobuf.Field("timestamp", 1, "uint64"))
|
|
|
|
.add(new protobuf.Field("nick", 2, "string"))
|
|
|
|
.add(new protobuf.Field("text", 3, "bytes"));
|
|
|
|
|
2022-10-04 04:03:14 +00:00
|
|
|
// Wallet
|
2022-09-27 16:24:43 +00:00
|
|
|
const checkChain = async (chainId) => {
|
2022-10-04 04:03:14 +00:00
|
|
|
retrieveRLNDetailsButton.disabled = retrievedRLNEvents || chainId !== 5;
|
|
|
|
registerButton.disabled = !(chainId === 5 && retrievedRLNEvents);
|
|
|
|
if (chainId !== 5) {
|
2022-09-27 16:24:43 +00:00
|
|
|
alert("Switch to Goerli")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const checkCurrentChain = async () => {
|
|
|
|
const network = await provider.getNetwork();
|
|
|
|
checkChain(network.chainId);
|
|
|
|
}
|
|
|
|
|
|
|
|
const rlnDeployBlk = 7677135;
|
|
|
|
const rlnAddress = "0x367F3e869cF2E1DD28644E308b42652cb6fcDA72";
|
|
|
|
const rlnAbi = [
|
|
|
|
"function MEMBERSHIP_DEPOSIT() public view returns(uint256)",
|
|
|
|
"function register(uint256 pubkey) external payable",
|
|
|
|
"function withdraw(uint256 secret, uint256 _pubkeyIndex, address payable receiver) external",
|
|
|
|
"event MemberRegistered(uint256 pubkey, uint256 index)",
|
|
|
|
"event MemberWithdrawn(uint256 pubkey, uint256 index)"
|
|
|
|
];
|
|
|
|
|
|
|
|
const provider = new ethers.providers.Web3Provider(window.ethereum, "any");
|
|
|
|
|
|
|
|
let accounts;
|
|
|
|
let rlnContract;
|
|
|
|
|
2022-10-04 04:03:14 +00:00
|
|
|
const handleMembership = (pubkey, index) => {
|
2022-09-27 16:24:43 +00:00
|
|
|
try {
|
|
|
|
const idCommitment = ethers.utils.zeroPad(ethers.utils.arrayify(pubkey), 32);
|
|
|
|
rlnInstance.insertMember(idCommitment);
|
|
|
|
console.debug("IDCommitment registered in tree", idCommitment, index.toNumber());
|
2022-10-04 04:03:14 +00:00
|
|
|
} catch (err) {
|
2022-09-27 16:24:43 +00:00
|
|
|
alert(err); // TODO: the merkle tree can be in a wrong state. The app should be disabled
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const setAccounts = acc => {
|
|
|
|
accounts = acc;
|
|
|
|
walletDiv.innerHTML = accounts.length ? accounts[0] : "";
|
|
|
|
}
|
|
|
|
|
|
|
|
connectWalletButton.onclick = async () => {
|
|
|
|
try {
|
|
|
|
accounts = await provider.send("eth_requestAccounts", []);
|
|
|
|
setAccounts(accounts);
|
|
|
|
checkCurrentChain();
|
|
|
|
} catch (e) {
|
2022-10-04 04:03:14 +00:00
|
|
|
console.log("No web3 provider available", e);
|
2022-09-27 16:24:43 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
retrieveRLNDetailsButton.onclick = async () => {
|
|
|
|
rlnContract = new ethers.Contract(rlnAddress, rlnAbi, provider);
|
2022-10-04 04:03:14 +00:00
|
|
|
|
2022-09-27 16:24:43 +00:00
|
|
|
const filter = rlnContract.filters.MemberRegistered()
|
|
|
|
|
|
|
|
// populating merkle tree:
|
|
|
|
const alreadyRegisteredMembers = await rlnContract.queryFilter(filter, rlnDeployBlk)
|
|
|
|
alreadyRegisteredMembers.forEach(event => {
|
|
|
|
handleMembership(event.args.pubkey, event.args.index, event);
|
|
|
|
});
|
|
|
|
|
|
|
|
alert("Loaded all events from contract");
|
|
|
|
retrievedRLNEvents = true;
|
|
|
|
// TODO: at this point it should be possible to register a credential and send messages
|
|
|
|
// TODO: emit an event to enable message form and register button
|
|
|
|
|
|
|
|
retrieveRLNDetailsButton.disabled = true;
|
|
|
|
registerButton.disabled = false;
|
|
|
|
|
|
|
|
// reacting to new registrations
|
|
|
|
rlnContract.on(filter, (pubkey, index, event) => {
|
|
|
|
handleMembership(event.args.pubkey, event.args.index, event);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
window.ethereum.on('accountsChanged', setAccounts);
|
|
|
|
window.ethereum.on('chainChanged', chainId => {
|
|
|
|
checkChain(parseInt(chainId, 16));
|
|
|
|
});
|
|
|
|
|
|
|
|
registerButton.onclick = async () => {
|
|
|
|
try {
|
|
|
|
registerButton.disabled = true;
|
2022-10-04 04:03:14 +00:00
|
|
|
|
2022-09-27 16:24:43 +00:00
|
|
|
const pubkey = ethers.BigNumber.from(membershipKey.IDCommitment);
|
|
|
|
const price = await rlnContract.MEMBERSHIP_DEPOSIT();
|
|
|
|
|
|
|
|
const signer = provider.getSigner()
|
|
|
|
const rlnContractWithSigner = rlnContract.connect(signer);
|
|
|
|
|
|
|
|
const txResponse = await rlnContractWithSigner.register(pubkey, {value: price});
|
|
|
|
console.log("Transaction broadcasted:", txResponse);
|
|
|
|
|
|
|
|
const txReceipt = await txResponse.wait();
|
|
|
|
|
|
|
|
console.log("Transaction receipt", txReceipt);
|
|
|
|
|
|
|
|
// Update membershipId
|
|
|
|
membershipId = txReceipt.events[0].args.index.toNumber();
|
|
|
|
console.log("Obtained index for current membership credentials", membershipId);
|
|
|
|
updateFields();
|
|
|
|
registerButton.disabled = false;
|
2022-10-04 04:03:14 +00:00
|
|
|
} catch (err) {
|
2022-09-27 16:24:43 +00:00
|
|
|
alert(err);
|
|
|
|
registerButton.disabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-10-04 04:03:14 +00:00
|
|
|
// Waku
|
|
|
|
nicknameInput.onchange = updateFields
|
|
|
|
setNickButton.onclick = () => {
|
|
|
|
nick = nicknameInput.value;
|
|
|
|
updateFields()
|
|
|
|
}
|
|
|
|
|
|
|
|
const ContentTopic = "/toy-chat/2/luzhou/proto";
|
|
|
|
const decoder = new DecoderV0(ContentTopic);
|
|
|
|
|
|
|
|
let messages = [];
|
|
|
|
|
|
|
|
const updateMessages = (msgs, div) => {
|
|
|
|
div.innerHTML = "<ul>"
|
|
|
|
messages.forEach(msg => div.innerHTML += "<li>" + msg + "</li>")
|
|
|
|
div.innerHTML += "</ul>"
|
|
|
|
}
|
|
|
|
|
|
|
|
const callback = (wakuMessage) => {
|
|
|
|
const {timestamp, nick, text} = ProtoChatMessage.decode(wakuMessage.payload)
|
|
|
|
const time = new Date();
|
|
|
|
time.setTime(Number(timestamp));
|
|
|
|
|
|
|
|
messages.push(`(${nick}) <strong>${utils.bytesToUtf8(text)}</strong> <i>[${time.toString()}]</i>`);
|
|
|
|
updateMessages(messages, messagesDiv)
|
|
|
|
}
|
|
|
|
|
2022-09-27 14:35:36 +00:00
|
|
|
let node;
|
|
|
|
(async () => {
|
|
|
|
statusDiv.innerHTML = '<p>Creating Waku node.</p>';
|
2022-10-04 04:18:26 +00:00
|
|
|
node = await createLightNode();
|
2022-09-27 14:35:36 +00:00
|
|
|
|
|
|
|
statusDiv.innerHTML = '<p>Starting Waku node.</p>';
|
|
|
|
await node.start();
|
|
|
|
statusDiv.innerHTML = '<p>Waku node started.</p>';
|
2022-10-04 04:18:26 +00:00
|
|
|
dialButton.disabled = false;
|
|
|
|
updateFields()
|
|
|
|
})()
|
|
|
|
|
|
|
|
dialButton.onclick = async () => {
|
|
|
|
const ma = remoteMultiAddrInput.value
|
|
|
|
if (!ma) {
|
|
|
|
statusDiv.innerHTML = '<p>Error: No multiaddr provided.</p>';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
statusDiv.innerHTML = '<p>Dialing peer.</p>';
|
|
|
|
await node.dial(ma, ["filter", "lightpush"])
|
2022-09-26 00:41:30 +00:00
|
|
|
await waitForRemotePeer(node, ["filter", "lightpush"]);
|
2022-09-27 14:35:36 +00:00
|
|
|
statusDiv.innerHTML = '<p>Waku node connected.</p>';
|
2022-09-26 00:41:30 +00:00
|
|
|
await node.filter.subscribe([decoder], callback)
|
2022-10-04 04:18:26 +00:00
|
|
|
statusDiv.innerHTML = '<p>Waku node subscribed.</p>';
|
2022-09-27 14:35:36 +00:00
|
|
|
nodeConnected = true;
|
2022-10-04 04:18:26 +00:00
|
|
|
}
|
2022-09-26 00:41:30 +00:00
|
|
|
|
|
|
|
sendButton.onclick = async () => {
|
2022-09-27 14:35:36 +00:00
|
|
|
const text = utils.utf8ToBytes(textInput.value);
|
|
|
|
const timestamp = new Date();
|
|
|
|
const msg = ProtoChatMessage.create({text, nick, timestamp: timestamp.valueOf()});
|
|
|
|
const payload = ProtoChatMessage.encode(msg).finish();
|
2022-09-27 14:37:34 +00:00
|
|
|
console.log("Sending message with proof...")
|
2022-09-27 14:35:36 +00:00
|
|
|
await node.lightPush.push(encoder, {payload, timestamp});
|
2022-09-27 14:37:34 +00:00
|
|
|
console.log("Message sent!")
|
2022-09-26 00:41:30 +00:00
|
|
|
textInput.value = null;
|
|
|
|
};
|
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
|
|
|
|
</html>
|