Skip to content

Commit 98aa21b

Browse files
authored
Merge pull request #82 from Rate-Limiting-Nullifier/build/add-browser-example
Add browser example
2 parents abfb6a0 + 580aaa2 commit 98aa21b

18 files changed

+2209
-8
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,8 @@ await rln.slash(secret) // funds go to the signer
318318
```
319319

320320
## Example
321-
Please see the examples [here](./examples/node/) which can be run and demonstrate how to use RLNjs.
321+
322+
Please see the examples [here](./examples/). We have examples for [NodeJS](./examples/node/) and [browser](./examples/browser/).
322323

323324
## Tests
324325

examples/browser/README.md

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Example: use RLNjs in browser
2+
3+
The example go through the following steps:
4+
1. Deploy the necessary contracts: verifier (the mock proof verifier since it's not yet generated), ERC20 token, and the RLN contract
5+
2. Create a RLN instance `rln`, and demonstrate how to `register`, `createProof`, `verifyProof`, `withdraw`, and `releaseWithdrawal`.
6+
3. Create a RLN instance `rlnAnother` and demonstrate how to slash a spammer. `rlnAnother` simply `register` and send more proofs than they should. `rln` can detect the spam by examine the output from `saveProof`. If the status of the output is BREACH, the secret is leaked. `rln` can use the recovered secret to slash the spammer by calling `slash`.
7+
8+
To install and run the example, follow the steps below.
9+
10+
1. Install the project
11+
```bash
12+
$ npm install
13+
```
14+
15+
2. Run a local testing RPC
16+
Here we use a hardhat node. If the RPC is not listening `http://localhost:8545`, you need to change the `url` in [config.ts](./src/configs.ts).
17+
18+
In a new terminal, run:
19+
```bash
20+
$ npx hardhat node
21+
```
22+
23+
3. Run the web server
24+
```bash
25+
$ npm run test-browser
26+
27+
28+
...
29+
Available on:
30+
http://127.0.0.1:8080
31+
http://192.168.50.66:8080
32+
Hit CTRL-C to stop the server
33+
```
34+
35+
4. Open the web page at `http://localhost:8080`. You should see the following output in the browser console.
36+
```
37+
Connecting to endpoint at http://localhost:8545
38+
Deploying contracts...
39+
40+
...
41+
42+
Successfully breached rlnAnother's secret=xxx
43+
Successfully slashed rlnAnother
44+
```
45+
2.29 MB
Binary file not shown.
3.25 MB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
{
2+
"protocol": "groth16",
3+
"curve": "bn128",
4+
"nPublic": 5,
5+
"vk_alpha_1": [
6+
"20491192805390485299153009773594534940189261866228447918068658471970481763042",
7+
"9383485363053290200918347156157836566562967994039712273449902621266178545958",
8+
"1"
9+
],
10+
"vk_beta_2": [
11+
[
12+
"6375614351688725206403948262868962793625744043794305715222011528459656738731",
13+
"4252822878758300859123897981450591353533073413197771768651442665752259397132"
14+
],
15+
[
16+
"10505242626370262277552901082094356697409835680220590971873171140371331206856",
17+
"21847035105528745403288232691147584728191162732299865338377159692350059136679"
18+
],
19+
[
20+
"1",
21+
"0"
22+
]
23+
],
24+
"vk_gamma_2": [
25+
[
26+
"10857046999023057135944570762232829481370756359578518086990519993285655852781",
27+
"11559732032986387107991004021392285783925812861821192530917403151452391805634"
28+
],
29+
[
30+
"8495653923123431417604973247489272438418190587263600148770280649306958101930",
31+
"4082367875863433681332203403145435568316851327593401208105741076214120093531"
32+
],
33+
[
34+
"1",
35+
"0"
36+
]
37+
],
38+
"vk_delta_2": [
39+
[
40+
"16029836322750290643870193897573373837316166289970119892734311883948567740332",
41+
"6931711345992101006283663471076438657814279313789677141122363676613977552455"
42+
],
43+
[
44+
"21551005661494722100004016865694054742875015816263826565588722362294531686039",
45+
"2573906357689002234296455676810664524188934712889533221366750601116338155576"
46+
],
47+
[
48+
"1",
49+
"0"
50+
]
51+
],
52+
"vk_alphabeta_12": [
53+
[
54+
[
55+
"2029413683389138792403550203267699914886160938906632433982220835551125967885",
56+
"21072700047562757817161031222997517981543347628379360635925549008442030252106"
57+
],
58+
[
59+
"5940354580057074848093997050200682056184807770593307860589430076672439820312",
60+
"12156638873931618554171829126792193045421052652279363021382169897324752428276"
61+
],
62+
[
63+
"7898200236362823042373859371574133993780991612861777490112507062703164551277",
64+
"7074218545237549455313236346927434013100842096812539264420499035217050630853"
65+
]
66+
],
67+
[
68+
[
69+
"7077479683546002997211712695946002074877511277312570035766170199895071832130",
70+
"10093483419865920389913245021038182291233451549023025229112148274109565435465"
71+
],
72+
[
73+
"4595479056700221319381530156280926371456704509942304414423590385166031118820",
74+
"19831328484489333784475432780421641293929726139240675179672856274388269393268"
75+
],
76+
[
77+
"11934129596455521040620786944827826205713621633706285934057045369193958244500",
78+
"8037395052364110730298837004334506829870972346962140206007064471173334027475"
79+
]
80+
]
81+
],
82+
"IC": [
83+
[
84+
"4920513730204767532050733107749276406754520419375654722016092399980613788208",
85+
"10950491564509418434657706642388934308456795265036074733953533582377584967294",
86+
"1"
87+
],
88+
[
89+
"6815064660695497986531118446154820702646540722664044216580897159556261271171",
90+
"17838140936832571103329556013529166877877534025488014783346458943575275015438",
91+
"1"
92+
],
93+
[
94+
"16364982450206976302246609763791333525052810246590359380676749324389440643932",
95+
"17092624338100676284548565502349491320314889021833923882585524649862570629227",
96+
"1"
97+
],
98+
[
99+
"3679639231485547795420532910726924727560917141402837495597760107842698404034",
100+
"16213191511474848247596810551723578773353083440353745908057321946068926848382",
101+
"1"
102+
],
103+
[
104+
"9215428431027260354679105025212521481930206886203677270216204485256690813172",
105+
"934602510541226149881779979217731465262250233587980565969044391353665291792",
106+
"1"
107+
],
108+
[
109+
"8935861545794299876685457331391349387048184820319250771243971382360441890897",
110+
"4993459033694759724715904486381952906869986989682015547152342336961693234616",
111+
"1"
112+
]
113+
]
114+
}
1.55 MB
Binary file not shown.
113 KB
Binary file not shown.

examples/browser/index.html

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>My Webpage</title>
7+
</head>
8+
<body>
9+
<h1>Hello World!</h1>
10+
11+
<!-- Include your JavaScript file here -->
12+
<script src="index.mjs"></script>
13+
</body>
14+
</html>

0 commit comments

Comments
 (0)