@waku/rln 0.0.2-c86e056.0 → 0.0.2-caeafce.0

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.
Files changed (36) hide show
  1. package/bundle/index.js +1 -1
  2. package/bundle/packages/interfaces/dist/protocols.js +40 -45
  3. package/bundle/packages/rln/dist/contract/abi.js +648 -0
  4. package/bundle/packages/rln/dist/contract/constants.js +8 -13
  5. package/bundle/packages/rln/dist/contract/rln_contract.js +14 -3
  6. package/bundle/packages/rln/dist/rln.js +37 -11
  7. package/bundle/packages/rln/dist/zerokit.js +22 -16
  8. package/dist/.tsbuildinfo +1 -1
  9. package/dist/contract/{abi/rlnv2.d.ts → abi.d.ts} +22 -18
  10. package/dist/contract/abi.js +647 -0
  11. package/dist/contract/abi.js.map +1 -0
  12. package/dist/contract/constants.d.ts +22 -23
  13. package/dist/contract/constants.js +7 -12
  14. package/dist/contract/constants.js.map +1 -1
  15. package/dist/contract/rln_contract.d.ts +8 -0
  16. package/dist/contract/rln_contract.js +14 -3
  17. package/dist/contract/rln_contract.js.map +1 -1
  18. package/dist/index.d.ts +2 -2
  19. package/dist/index.js +2 -2
  20. package/dist/index.js.map +1 -1
  21. package/dist/rln.js +37 -10
  22. package/dist/rln.js.map +1 -1
  23. package/dist/zerokit.d.ts +5 -1
  24. package/dist/zerokit.js +22 -16
  25. package/dist/zerokit.js.map +1 -1
  26. package/package.json +1 -1
  27. package/src/contract/abi.ts +646 -0
  28. package/src/contract/constants.ts +8 -14
  29. package/src/contract/rln_contract.ts +16 -3
  30. package/src/index.ts +2 -2
  31. package/src/rln.ts +51 -10
  32. package/src/zerokit.ts +45 -16
  33. package/bundle/packages/rln/dist/contract/abi/rlnv2.js +0 -394
  34. package/dist/contract/abi/rlnv2.js +0 -393
  35. package/dist/contract/abi/rlnv2.js.map +0 -1
  36. package/src/contract/abi/rlnv2.ts +0 -392
@@ -8,7 +8,7 @@ import type { RLNInstance } from "../rln.js";
8
8
  import { MerkleRootTracker } from "../root_tracker.js";
9
9
  import { zeroPadLE } from "../utils/bytes.js";
10
10
 
11
- import { RLN_V2_ABI } from "./abi/rlnv2.js";
11
+ import { RLN_ABI } from "./abi.js";
12
12
  import { DEFAULT_RATE_LIMIT, RATE_LIMIT_PARAMS } from "./constants.js";
13
13
 
14
14
  const log = new Logger("waku:rln:contract");
@@ -108,8 +108,7 @@ export class RLNContract {
108
108
  const initialRoot = rlnInstance.zerokit.getMerkleRoot();
109
109
 
110
110
  // Use the injected contract if provided; otherwise, instantiate a new one.
111
- this.contract =
112
- contract || new ethers.Contract(address, RLN_V2_ABI, signer);
111
+ this.contract = contract || new ethers.Contract(address, RLN_ABI, signer);
113
112
  this.merkleRootTracker = new MerkleRootTracker(5, initialRoot);
114
113
 
115
114
  // Initialize event filters for MembershipRegistered and MembershipRemoved
@@ -124,6 +123,20 @@ export class RLNContract {
124
123
  return this.rateLimit;
125
124
  }
126
125
 
126
+ /**
127
+ * Gets the contract address
128
+ */
129
+ public get address(): string {
130
+ return this.contract.address;
131
+ }
132
+
133
+ /**
134
+ * Gets the contract provider
135
+ */
136
+ public get provider(): ethers.providers.Provider {
137
+ return this.contract.provider;
138
+ }
139
+
127
140
  /**
128
141
  * Gets the minimum allowed rate limit from the contract
129
142
  * @returns Promise<number> The minimum rate limit in messages per epoch
package/src/index.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { RLNDecoder, RLNEncoder } from "./codec.js";
2
- import { RLN_V2_ABI } from "./contract/abi/rlnv2.js";
2
+ import { RLN_ABI } from "./contract/abi.js";
3
3
  import { RLNContract, SEPOLIA_CONTRACT } from "./contract/index.js";
4
4
  import { createRLN } from "./create.js";
5
5
  import { IdentityCredential } from "./identity.js";
@@ -21,5 +21,5 @@ export {
21
21
  RLNContract,
22
22
  SEPOLIA_CONTRACT,
23
23
  extractMetaMaskSigner,
24
- RLN_V2_ABI
24
+ RLN_ABI
25
25
  };
package/src/rln.ts CHANGED
@@ -15,6 +15,7 @@ import {
15
15
  type RLNDecoder,
16
16
  type RLNEncoder
17
17
  } from "./codec.js";
18
+ import { DEFAULT_RATE_LIMIT } from "./contract/constants.js";
18
19
  import { RLNContract, SEPOLIA_CONTRACT } from "./contract/index.js";
19
20
  import { IdentityCredential } from "./identity.js";
20
21
  import { Keystore } from "./keystore/index.js";
@@ -32,15 +33,46 @@ import { Zerokit } from "./zerokit.js";
32
33
  const log = new Logger("waku:rln");
33
34
 
34
35
  async function loadWitnessCalculator(): Promise<WitnessCalculator> {
35
- const url = new URL("./resources/rln.wasm", import.meta.url);
36
- const response = await fetch(url);
37
- return await wc.builder(new Uint8Array(await response.arrayBuffer()), false);
36
+ try {
37
+ const url = new URL("./resources/rln.wasm", import.meta.url);
38
+ const response = await fetch(url);
39
+
40
+ if (!response.ok) {
41
+ throw new Error(
42
+ `Failed to fetch witness calculator: ${response.status} ${response.statusText}`
43
+ );
44
+ }
45
+
46
+ return await wc.builder(
47
+ new Uint8Array(await response.arrayBuffer()),
48
+ false
49
+ );
50
+ } catch (error) {
51
+ log.error("Error loading witness calculator:", error);
52
+ throw new Error(
53
+ `Failed to load witness calculator: ${error instanceof Error ? error.message : String(error)}`
54
+ );
55
+ }
38
56
  }
39
57
 
40
58
  async function loadZkey(): Promise<Uint8Array> {
41
- const url = new URL("./resources/rln_final.zkey", import.meta.url);
42
- const response = await fetch(url);
43
- return new Uint8Array(await response.arrayBuffer());
59
+ try {
60
+ const url = new URL("./resources/rln_final.zkey", import.meta.url);
61
+ const response = await fetch(url);
62
+
63
+ if (!response.ok) {
64
+ throw new Error(
65
+ `Failed to fetch zkey: ${response.status} ${response.statusText}`
66
+ );
67
+ }
68
+
69
+ return new Uint8Array(await response.arrayBuffer());
70
+ } catch (error) {
71
+ log.error("Error loading zkey:", error);
72
+ throw new Error(
73
+ `Failed to load zkey: ${error instanceof Error ? error.message : String(error)}`
74
+ );
75
+ }
44
76
  }
45
77
 
46
78
  /**
@@ -61,7 +93,7 @@ export async function create(): Promise<RLNInstance> {
61
93
 
62
94
  const DEPTH = 20;
63
95
  const zkRLN = zerokitRLN.newRLN(DEPTH, zkey, vkey);
64
- const zerokit = new Zerokit(zkRLN, witnessCalculator);
96
+ const zerokit = new Zerokit(zkRLN, witnessCalculator, DEFAULT_RATE_LIMIT);
65
97
 
66
98
  return new RLNInstance(zerokit);
67
99
  } catch (error) {
@@ -119,6 +151,8 @@ export class RLNInstance {
119
151
  }
120
152
 
121
153
  public async start(options: StartRLNOptions = {}): Promise<void> {
154
+ // eslint-disable-next-line no-console
155
+ console.log("starting", options);
122
156
  if (this.started || this.starting) {
123
157
  return;
124
158
  }
@@ -142,7 +176,7 @@ export class RLNInstance {
142
176
  this._contract = await RLNContract.init(this, {
143
177
  address: address!,
144
178
  signer: signer!,
145
- rateLimit: options.rateLimit
179
+ rateLimit: options.rateLimit ?? this.zerokit.getRateLimit
146
180
  });
147
181
  this.started = true;
148
182
  } finally {
@@ -164,6 +198,13 @@ export class RLNInstance {
164
198
  chainId = SEPOLIA_CONTRACT.chainId;
165
199
  }
166
200
 
201
+ // eslint-disable-next-line no-console
202
+ console.log({
203
+ chainId,
204
+ address,
205
+ SEPOLIA_CONTRACT
206
+ });
207
+
167
208
  const signer = options.signer || (await extractMetaMaskSigner());
168
209
  const currentChainId = await signer.getChainId();
169
210
 
@@ -271,7 +312,7 @@ export class RLNInstance {
271
312
  }
272
313
 
273
314
  const registryAddress = credentials.membership.address;
274
- const currentRegistryAddress = this._contract.contract.address;
315
+ const currentRegistryAddress = this._contract.address;
275
316
  if (registryAddress !== currentRegistryAddress) {
276
317
  throw Error(
277
318
  `Failed to verify chain coordinates: credentials contract address=${registryAddress} is not equal to registryContract address=${currentRegistryAddress}`
@@ -279,7 +320,7 @@ export class RLNInstance {
279
320
  }
280
321
 
281
322
  const chainId = credentials.membership.chainId;
282
- const network = await this._contract.contract.provider.getNetwork();
323
+ const network = await this._contract.provider.getNetwork();
283
324
  const currentChainId = network.chainId;
284
325
  if (chainId !== currentChainId) {
285
326
  throw Error(
package/src/zerokit.ts CHANGED
@@ -15,9 +15,22 @@ import {
15
15
  export class Zerokit {
16
16
  public constructor(
17
17
  private readonly zkRLN: number,
18
- private readonly witnessCalculator: WitnessCalculator
18
+ private readonly witnessCalculator: WitnessCalculator,
19
+ private readonly rateLimit: number = DEFAULT_RATE_LIMIT
19
20
  ) {}
20
21
 
22
+ public get getZkRLN(): number {
23
+ return this.zkRLN;
24
+ }
25
+
26
+ public get getWitnessCalculator(): WitnessCalculator {
27
+ return this.witnessCalculator;
28
+ }
29
+
30
+ public get getRateLimit(): number {
31
+ return this.rateLimit;
32
+ }
33
+
21
34
  public generateIdentityCredentials(): IdentityCredential {
22
35
  const memKeys = zerokitRLN.generateExtendedMembershipKey(this.zkRLN); // TODO: rename this function in zerokit rln-wasm
23
36
  return IdentityCredential.fromBytes(memKeys);
@@ -67,12 +80,17 @@ export class Zerokit {
67
80
  memIndex: number,
68
81
  epoch: Uint8Array,
69
82
  idKey: Uint8Array,
70
- rateLimit: number = DEFAULT_RATE_LIMIT
83
+ rateLimit?: number
71
84
  ): Uint8Array {
72
85
  // calculate message length
73
86
  const msgLen = writeUIntLE(new Uint8Array(8), uint8Msg.length, 0, 8);
74
87
  const memIndexBytes = writeUIntLE(new Uint8Array(8), memIndex, 0, 8);
75
- const rateLimitBytes = writeUIntLE(new Uint8Array(8), rateLimit, 0, 8);
88
+ const rateLimitBytes = writeUIntLE(
89
+ new Uint8Array(8),
90
+ rateLimit ?? this.rateLimit,
91
+ 0,
92
+ 8
93
+ );
76
94
 
77
95
  // [ id_key<32> | id_index<8> | epoch<32> | signal_len<8> | signal<var> | rate_limit<8> ]
78
96
  return concatenate(
@@ -90,7 +108,7 @@ export class Zerokit {
90
108
  index: number,
91
109
  epoch: Uint8Array | Date | undefined,
92
110
  idSecretHash: Uint8Array,
93
- rateLimit: number = DEFAULT_RATE_LIMIT
111
+ rateLimit?: number
94
112
  ): Promise<IRateLimitProof> {
95
113
  if (epoch === undefined) {
96
114
  epoch = epochIntToBytes(dateToEpoch(new Date()));
@@ -98,12 +116,14 @@ export class Zerokit {
98
116
  epoch = epochIntToBytes(dateToEpoch(epoch));
99
117
  }
100
118
 
119
+ const effectiveRateLimit = rateLimit ?? this.rateLimit;
120
+
101
121
  if (epoch.length !== 32) throw new Error("invalid epoch");
102
122
  if (idSecretHash.length !== 32) throw new Error("invalid id secret hash");
103
123
  if (index < 0) throw new Error("index must be >= 0");
104
124
  if (
105
- rateLimit < RATE_LIMIT_PARAMS.MIN_RATE ||
106
- rateLimit > RATE_LIMIT_PARAMS.MAX_RATE
125
+ effectiveRateLimit < RATE_LIMIT_PARAMS.MIN_RATE ||
126
+ effectiveRateLimit > RATE_LIMIT_PARAMS.MAX_RATE
107
127
  ) {
108
128
  throw new Error(
109
129
  `Rate limit must be between ${RATE_LIMIT_PARAMS.MIN_RATE} and ${RATE_LIMIT_PARAMS.MAX_RATE}`
@@ -115,7 +135,7 @@ export class Zerokit {
115
135
  index,
116
136
  epoch,
117
137
  idSecretHash,
118
- rateLimit
138
+ effectiveRateLimit
119
139
  );
120
140
  const rlnWitness = zerokitRLN.getSerializedRLNWitness(
121
141
  this.zkRLN,
@@ -150,9 +170,12 @@ export class Zerokit {
150
170
 
151
171
  // calculate message length
152
172
  const msgLen = writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
153
- const rateLimitBytes = rateLimit
154
- ? writeUIntLE(new Uint8Array(8), rateLimit, 0, 8)
155
- : new Uint8Array(8); // Zero if not specified
173
+ const rateLimitBytes = writeUIntLE(
174
+ new Uint8Array(8),
175
+ rateLimit ?? this.rateLimit,
176
+ 0,
177
+ 8
178
+ );
156
179
 
157
180
  return zerokitRLN.verifyRLNProof(
158
181
  this.zkRLN,
@@ -174,9 +197,12 @@ export class Zerokit {
174
197
  }
175
198
  // calculate message length
176
199
  const msgLen = writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
177
- const rateLimitBytes = rateLimit
178
- ? writeUIntLE(new Uint8Array(8), rateLimit, 0, 8)
179
- : new Uint8Array(8); // Zero if not specified
200
+ const rateLimitBytes = writeUIntLE(
201
+ new Uint8Array(8),
202
+ rateLimit ?? this.rateLimit,
203
+ 0,
204
+ 8
205
+ );
180
206
 
181
207
  const rootsBytes = concatenate(...roots);
182
208
 
@@ -201,9 +227,12 @@ export class Zerokit {
201
227
 
202
228
  // calculate message length
203
229
  const msgLen = writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
204
- const rateLimitBytes = rateLimit
205
- ? writeUIntLE(new Uint8Array(8), rateLimit, 0, 8)
206
- : new Uint8Array(8); // Zero if not specified
230
+ const rateLimitBytes = writeUIntLE(
231
+ new Uint8Array(8),
232
+ rateLimit ?? this.rateLimit,
233
+ 0,
234
+ 8
235
+ );
207
236
 
208
237
  return zerokitRLN.verifyWithRoots(
209
238
  this.zkRLN,
@@ -1,394 +0,0 @@
1
- const RLN_V2_ABI = [
2
- {
3
- type: "constructor",
4
- inputs: [],
5
- stateMutability: "nonpayable"
6
- },
7
- {
8
- type: "error",
9
- name: "DuplicateIdCommitment",
10
- inputs: []
11
- },
12
- {
13
- type: "error",
14
- name: "InvalidIdCommitment",
15
- inputs: [
16
- {
17
- name: "idCommitment",
18
- type: "uint256"
19
- }
20
- ]
21
- },
22
- {
23
- type: "error",
24
- name: "InvalidPaginationQuery",
25
- inputs: [
26
- {
27
- name: "startIndex",
28
- type: "uint256"
29
- },
30
- {
31
- name: "endIndex",
32
- type: "uint256"
33
- }
34
- ]
35
- },
36
- {
37
- type: "function",
38
- name: "MAX_MEMBERSHIP_SET_SIZE",
39
- inputs: [],
40
- outputs: [
41
- {
42
- name: "",
43
- type: "uint32"
44
- }
45
- ],
46
- stateMutability: "view"
47
- },
48
- {
49
- type: "function",
50
- name: "MERKLE_TREE_DEPTH",
51
- inputs: [],
52
- outputs: [
53
- {
54
- name: "",
55
- type: "uint8"
56
- }
57
- ],
58
- stateMutability: "view"
59
- },
60
- {
61
- type: "function",
62
- name: "Q",
63
- inputs: [],
64
- outputs: [
65
- {
66
- name: "",
67
- type: "uint256"
68
- }
69
- ],
70
- stateMutability: "view"
71
- },
72
- {
73
- type: "function",
74
- name: "activeDurationForNewMemberships",
75
- inputs: [],
76
- outputs: [
77
- {
78
- name: "",
79
- type: "uint32"
80
- }
81
- ],
82
- stateMutability: "view"
83
- },
84
- {
85
- type: "function",
86
- name: "currentTotalRateLimit",
87
- inputs: [],
88
- outputs: [
89
- {
90
- name: "",
91
- type: "uint256"
92
- }
93
- ],
94
- stateMutability: "view"
95
- },
96
- {
97
- type: "function",
98
- name: "deployedBlockNumber",
99
- inputs: [],
100
- outputs: [
101
- {
102
- name: "",
103
- type: "uint32"
104
- }
105
- ],
106
- stateMutability: "view"
107
- },
108
- {
109
- type: "function",
110
- name: "depositsToWithdraw",
111
- inputs: [
112
- {
113
- name: "holder",
114
- type: "address"
115
- },
116
- {
117
- name: "token",
118
- type: "address"
119
- }
120
- ],
121
- outputs: [
122
- {
123
- name: "balance",
124
- type: "uint256"
125
- }
126
- ],
127
- stateMutability: "view"
128
- },
129
- {
130
- type: "function",
131
- name: "eraseMemberships",
132
- inputs: [
133
- {
134
- name: "idCommitments",
135
- type: "uint256[]"
136
- }
137
- ],
138
- outputs: [],
139
- stateMutability: "nonpayable"
140
- },
141
- {
142
- type: "function",
143
- name: "eraseMemberships",
144
- inputs: [
145
- {
146
- name: "idCommitments",
147
- type: "uint256[]"
148
- },
149
- {
150
- name: "eraseFromMembershipSet",
151
- type: "bool"
152
- }
153
- ],
154
- outputs: [],
155
- stateMutability: "nonpayable"
156
- },
157
- {
158
- type: "function",
159
- name: "extendMemberships",
160
- inputs: [
161
- {
162
- name: "idCommitments",
163
- type: "uint256[]"
164
- }
165
- ],
166
- outputs: [],
167
- stateMutability: "nonpayable"
168
- },
169
- {
170
- type: "function",
171
- name: "getMembershipInfo",
172
- inputs: [
173
- {
174
- name: "idCommitment",
175
- type: "uint256"
176
- }
177
- ],
178
- outputs: [
179
- {
180
- name: "",
181
- type: "uint32"
182
- },
183
- {
184
- name: "",
185
- type: "uint32"
186
- },
187
- {
188
- name: "",
189
- type: "uint256"
190
- }
191
- ],
192
- stateMutability: "view"
193
- },
194
- {
195
- type: "function",
196
- name: "getMerkleProof",
197
- inputs: [
198
- {
199
- name: "index",
200
- type: "uint40"
201
- }
202
- ],
203
- outputs: [
204
- {
205
- name: "",
206
- type: "uint256[20]"
207
- }
208
- ],
209
- stateMutability: "view"
210
- },
211
- {
212
- type: "function",
213
- name: "getRateCommitmentsInRangeBoundsInclusive",
214
- inputs: [
215
- {
216
- name: "startIndex",
217
- type: "uint32"
218
- },
219
- {
220
- name: "endIndex",
221
- type: "uint32"
222
- }
223
- ],
224
- outputs: [
225
- {
226
- name: "",
227
- type: "uint256[]"
228
- }
229
- ],
230
- stateMutability: "view"
231
- },
232
- {
233
- type: "function",
234
- name: "gracePeriodDurationForNewMemberships",
235
- inputs: [],
236
- outputs: [
237
- {
238
- name: "",
239
- type: "uint32"
240
- }
241
- ],
242
- stateMutability: "view"
243
- },
244
- {
245
- type: "function",
246
- name: "initialize",
247
- inputs: [
248
- {
249
- name: "_priceCalculator",
250
- type: "address"
251
- },
252
- {
253
- name: "_maxTotalRateLimit",
254
- type: "uint32"
255
- },
256
- {
257
- name: "_minMembershipRateLimit",
258
- type: "uint32"
259
- },
260
- {
261
- name: "_maxMembershipRateLimit",
262
- type: "uint32"
263
- },
264
- {
265
- name: "_activeDuration",
266
- type: "uint32"
267
- },
268
- {
269
- name: "_gracePeriod",
270
- type: "uint32"
271
- }
272
- ],
273
- outputs: [],
274
- stateMutability: "nonpayable"
275
- },
276
- {
277
- type: "function",
278
- name: "isExpired",
279
- inputs: [
280
- {
281
- name: "_idCommitment",
282
- type: "uint256"
283
- }
284
- ],
285
- outputs: [
286
- {
287
- name: "",
288
- type: "bool"
289
- }
290
- ],
291
- stateMutability: "view"
292
- },
293
- {
294
- type: "function",
295
- name: "register",
296
- inputs: [
297
- {
298
- name: "idCommitment",
299
- type: "uint256"
300
- },
301
- {
302
- name: "rateLimit",
303
- type: "uint32"
304
- },
305
- {
306
- name: "idCommitmentsToErase",
307
- type: "uint256[]"
308
- }
309
- ],
310
- outputs: [],
311
- stateMutability: "nonpayable"
312
- },
313
- {
314
- type: "function",
315
- name: "registerWithPermit",
316
- inputs: [
317
- {
318
- name: "owner",
319
- type: "address"
320
- },
321
- {
322
- name: "deadline",
323
- type: "uint256"
324
- },
325
- {
326
- name: "v",
327
- type: "uint8"
328
- },
329
- {
330
- name: "r",
331
- type: "bytes32"
332
- },
333
- {
334
- name: "s",
335
- type: "bytes32"
336
- },
337
- {
338
- name: "idCommitment",
339
- type: "uint256"
340
- },
341
- {
342
- name: "rateLimit",
343
- type: "uint32"
344
- },
345
- {
346
- name: "idCommitmentsToErase",
347
- type: "uint256[]"
348
- }
349
- ],
350
- outputs: [],
351
- stateMutability: "nonpayable"
352
- },
353
- {
354
- type: "event",
355
- name: "MembershipRegistered",
356
- inputs: [
357
- {
358
- name: "idCommitment",
359
- type: "uint256",
360
- indexed: false
361
- },
362
- {
363
- name: "rateLimit",
364
- type: "uint32",
365
- indexed: false
366
- },
367
- {
368
- name: "index",
369
- type: "uint256",
370
- indexed: false
371
- }
372
- ],
373
- anonymous: false
374
- },
375
- {
376
- type: "event",
377
- name: "MembershipRemoved",
378
- inputs: [
379
- {
380
- name: "idCommitment",
381
- type: "uint256",
382
- indexed: false
383
- },
384
- {
385
- name: "index",
386
- type: "uint256",
387
- indexed: false
388
- }
389
- ],
390
- anonymous: false
391
- }
392
- ];
393
-
394
- export { RLN_V2_ABI };