@waku/rln 0.0.13-fa70837 → 0.0.13-fae4bea

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,104 @@
1
+ import { ethers } from "ethers";
2
+
3
+ import { RLN_ABI } from "./constants.js";
4
+ import { RLNInstance } from "./rln.js";
5
+
6
+ type Member = {
7
+ pubkey: string;
8
+ index: number;
9
+ };
10
+
11
+ type ContractOptions = {
12
+ address: string;
13
+ provider: ethers.Signer | ethers.providers.Provider;
14
+ };
15
+
16
+ export class RLNContract {
17
+ private _contract: ethers.Contract;
18
+ private membersFilter: ethers.EventFilter;
19
+
20
+ private _members: Member[] = [];
21
+
22
+ public static async init(
23
+ rlnInstance: RLNInstance,
24
+ options: ContractOptions
25
+ ): Promise<RLNContract> {
26
+ const rlnContract = new RLNContract(options);
27
+
28
+ await rlnContract.fetchMembers(rlnInstance);
29
+ rlnContract.subscribeToMembers(rlnInstance);
30
+
31
+ return rlnContract;
32
+ }
33
+
34
+ constructor({ address, provider }: ContractOptions) {
35
+ this._contract = new ethers.Contract(address, RLN_ABI, provider);
36
+ this.membersFilter = this.contract.filters.MemberRegistered();
37
+ }
38
+
39
+ public get contract(): ethers.Contract {
40
+ return this._contract;
41
+ }
42
+
43
+ public get members(): Member[] {
44
+ return this._members;
45
+ }
46
+
47
+ public async fetchMembers(
48
+ rlnInstance: RLNInstance,
49
+ fromBlock?: number
50
+ ): Promise<void> {
51
+ const registeredMemberEvents = await this.contract.queryFilter(
52
+ this.membersFilter,
53
+ fromBlock
54
+ );
55
+
56
+ for (const event of registeredMemberEvents) {
57
+ this.addMemberFromEvent(rlnInstance, event);
58
+ }
59
+ }
60
+
61
+ public subscribeToMembers(rlnInstance: RLNInstance): void {
62
+ this.contract.on(this.membersFilter, (_pubkey, _index, event) =>
63
+ this.addMemberFromEvent(rlnInstance, event)
64
+ );
65
+ }
66
+
67
+ private addMemberFromEvent(
68
+ rlnInstance: RLNInstance,
69
+ event: ethers.Event
70
+ ): void {
71
+ if (!event.args) {
72
+ return;
73
+ }
74
+
75
+ const pubkey: string = event.args.pubkey;
76
+ const index: number = event.args.index;
77
+
78
+ this.members.push({ index, pubkey });
79
+
80
+ const idCommitment = ethers.utils.zeroPad(
81
+ ethers.utils.arrayify(pubkey),
82
+ 32
83
+ );
84
+ rlnInstance.insertMember(idCommitment);
85
+ }
86
+
87
+ public async registerMember(
88
+ rlnInstance: RLNInstance,
89
+ signature: string
90
+ ): Promise<ethers.Event | undefined> {
91
+ const membershipKey = await rlnInstance.generateSeededMembershipKey(
92
+ signature
93
+ );
94
+ const depositValue = await this.contract.MEMBERSHIP_DEPOSIT();
95
+
96
+ const txRegisterResponse: ethers.ContractTransaction =
97
+ await this.contract.register(membershipKey.IDCommitmentBigInt, {
98
+ value: depositValue,
99
+ });
100
+ const txRegisterReceipt = await txRegisterResponse.wait();
101
+
102
+ return txRegisterReceipt?.events?.[0];
103
+ }
104
+ }