js-rln/example/index.js
Sasha d77370fbec
Add RLN Contract abstraction (#43)
* implement rln contract abstraction, add basic tests, add usefull constants

* remove test command

* resolve simple comments

* move to getter for members, add init method

* fix naming

* remove default signature message

* use direct path to js file

* try different karma config

* try generic import

* update test

* address comments: rename const file, return prev regexp

* remove test

* bring back test file

* fix mock approach

* use any for type casting

* use another approach for typecasting

* update mocks

* update mocked event

* use correct value for mock

* fix spy definition

* add BigInt to MembershipKey

* fix joining

* use slice

* remove accidentally commited junk

* fix typo, use DataView for conversion, use BigInt directly

Co-authored-by: weboko <anon@mail.com>
2023-01-26 18:58:18 +01:00

59 lines
2.0 KiB
JavaScript

import * as rln from "@waku/rln";
rln.create().then(async rlnInstance => {
let memKeys = rlnInstance.generateMembershipKey();
//peer's index in the Merkle Tree
const index = 5
// Create a Merkle tree with random members
for (let i = 0; i < 10; i++) {
if (i == index) {
// insert the current peer's pk
rlnInstance.insertMember(memKeys.IDCommitment);
} else {
// create a new key pair
let memKeys = rlnInstance.generateMembershipKey(); // TODO: handle error
rlnInstance.insertMember(memKeys.IDCommitment);
}
}
// prepare the message
const uint8Msg = Uint8Array.from("Hello World".split("").map(x => x.charCodeAt()));
// setting up the epoch
const epoch = new Date();
console.log("Generating proof...");
console.time("proof_gen_timer");
let proof = await rlnInstance.generateRLNProof(uint8Msg, index, epoch, memKeys.IDKey)
console.timeEnd("proof_gen_timer");
console.log("Proof", proof)
try {
// verify the proof
let verifResult = rlnInstance.verifyRLNProof(proof, uint8Msg);
console.log("Is proof verified?", verifResult ? "yes" : "no");
} catch (err) {
console.log("Invalid proof")
}
const provider = new ethers.providers.Web3Provider(
window.ethereum,
"any"
);
const DEFAULT_SIGNATURE_MESSAGE =
"The signature of this message will be used to generate your RLN credentials. Anyone accessing it may send messages on your behalf, please only share with the RLN dApp";
const signer = provider.getSigner();
const signature = await signer.signMessage(DEFAULT_SIGNATURE_MESSAGE);
console.log(`Got signature: ${signature}`);
const contract = await rln.RLNContract.init(rlnInstance, {address: rln.GOERLI_CONTRACT.address, provider: signer });
const event = await contract.registerMember(rlnInstance, signature);
console.log(`Registered as member with ${event}`);
});