@waku/rln 0.0.2-3670e82.0 → 0.0.2-5c50ed7.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 (42) hide show
  1. package/bundle/_virtual/utils.js +2 -2
  2. package/bundle/_virtual/utils2.js +2 -2
  3. package/bundle/index.js +2 -0
  4. package/bundle/packages/rln/dist/contract/rln_contract.js +58 -59
  5. package/bundle/packages/rln/dist/contract/rln_light_contract.js +471 -0
  6. package/bundle/packages/rln/dist/rln.js +2 -4
  7. package/bundle/packages/rln/dist/rln_light.js +149 -0
  8. package/bundle/packages/rln/node_modules/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/random.js +1 -1
  9. package/bundle/packages/rln/node_modules/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/utils.js +2 -2
  10. package/bundle/packages/rln/node_modules/@noble/hashes/_sha2.js +1 -1
  11. package/bundle/packages/rln/node_modules/@noble/hashes/esm/_assert.js +43 -0
  12. package/bundle/packages/rln/node_modules/@noble/hashes/esm/_sha2.js +116 -0
  13. package/bundle/packages/rln/node_modules/@noble/hashes/esm/hmac.js +79 -0
  14. package/bundle/packages/rln/node_modules/@noble/hashes/esm/sha256.js +126 -0
  15. package/bundle/packages/rln/node_modules/@noble/hashes/esm/utils.js +43 -0
  16. package/bundle/packages/rln/node_modules/@noble/hashes/hmac.js +1 -1
  17. package/bundle/packages/rln/node_modules/@noble/hashes/pbkdf2.js +1 -1
  18. package/bundle/packages/rln/node_modules/@noble/hashes/scrypt.js +1 -1
  19. package/bundle/packages/rln/node_modules/@noble/hashes/sha256.js +1 -1
  20. package/bundle/packages/rln/node_modules/@noble/hashes/sha512.js +1 -1
  21. package/bundle/packages/rln/node_modules/@noble/hashes/utils.js +1 -1
  22. package/dist/.tsbuildinfo +1 -1
  23. package/dist/contract/rln_contract.d.ts +4 -4
  24. package/dist/contract/rln_contract.js +58 -59
  25. package/dist/contract/rln_contract.js.map +1 -1
  26. package/dist/contract/rln_light_contract.d.ts +124 -0
  27. package/dist/contract/rln_light_contract.js +454 -0
  28. package/dist/contract/rln_light_contract.js.map +1 -0
  29. package/dist/index.d.ts +4 -1
  30. package/dist/index.js +3 -1
  31. package/dist/index.js.map +1 -1
  32. package/dist/rln.js +2 -4
  33. package/dist/rln.js.map +1 -1
  34. package/dist/rln_light.d.ts +64 -0
  35. package/dist/rln_light.js +144 -0
  36. package/dist/rln_light.js.map +1 -0
  37. package/package.json +1 -1
  38. package/src/contract/rln_contract.ts +73 -107
  39. package/src/contract/rln_light_contract.ts +716 -0
  40. package/src/index.ts +15 -0
  41. package/src/rln.ts +2 -5
  42. package/src/rln_light.ts +235 -0
@@ -0,0 +1,144 @@
1
+ import { hmac } from "@noble/hashes/hmac";
2
+ import { sha256 } from "@noble/hashes/sha256";
3
+ import { Logger } from "@waku/utils";
4
+ import { SEPOLIA_CONTRACT } from "./contract/constants.js";
5
+ import { RLNLightContract } from "./contract/rln_light_contract.js";
6
+ import { IdentityCredential } from "./identity.js";
7
+ import { Keystore } from "./keystore/index.js";
8
+ import { buildBigIntFromUint8Array, extractMetaMaskSigner } from "./utils/index.js";
9
+ const log = new Logger("waku:rln");
10
+ /**
11
+ * Create an instance of RLN
12
+ * @returns RLNInstance
13
+ */
14
+ export async function create() {
15
+ try {
16
+ return new RLNLightInstance();
17
+ }
18
+ catch (error) {
19
+ log.error("Failed to initialize RLN:", error);
20
+ throw error;
21
+ }
22
+ }
23
+ export class RLNLightInstance {
24
+ started = false;
25
+ starting = false;
26
+ _contract;
27
+ _signer;
28
+ keystore = Keystore.create();
29
+ _credentials;
30
+ constructor() { }
31
+ get contract() {
32
+ return this._contract;
33
+ }
34
+ get signer() {
35
+ return this._signer;
36
+ }
37
+ async start(options = {}) {
38
+ if (this.started || this.starting) {
39
+ return;
40
+ }
41
+ this.starting = true;
42
+ try {
43
+ const { credentials, keystore } = await RLNLightInstance.decryptCredentialsIfNeeded(options.credentials);
44
+ const { signer, address, rateLimit } = await this.determineStartOptions(options, credentials);
45
+ if (keystore) {
46
+ this.keystore = keystore;
47
+ }
48
+ this._credentials = credentials;
49
+ this._signer = signer;
50
+ this._contract = await RLNLightContract.init({
51
+ address: address,
52
+ signer: signer,
53
+ rateLimit: rateLimit
54
+ });
55
+ this.started = true;
56
+ }
57
+ finally {
58
+ this.starting = false;
59
+ }
60
+ }
61
+ get credentials() {
62
+ return this._credentials;
63
+ }
64
+ async determineStartOptions(options, credentials) {
65
+ let chainId = credentials?.membership.chainId;
66
+ const address = credentials?.membership.address ||
67
+ options.address ||
68
+ SEPOLIA_CONTRACT.address;
69
+ if (address === SEPOLIA_CONTRACT.address) {
70
+ chainId = SEPOLIA_CONTRACT.chainId;
71
+ }
72
+ const signer = options.signer || (await extractMetaMaskSigner());
73
+ const currentChainId = await signer.getChainId();
74
+ if (chainId && chainId !== currentChainId) {
75
+ throw Error(`Failed to start RLN contract, chain ID of contract is different from current one: contract-${chainId}, current network-${currentChainId}`);
76
+ }
77
+ return {
78
+ signer,
79
+ address
80
+ };
81
+ }
82
+ static async decryptCredentialsIfNeeded(credentials) {
83
+ if (!credentials) {
84
+ return {};
85
+ }
86
+ if ("identity" in credentials) {
87
+ return { credentials };
88
+ }
89
+ const keystore = Keystore.fromString(credentials.keystore);
90
+ if (!keystore) {
91
+ return {};
92
+ }
93
+ const decryptedCredentials = await keystore.readCredential(credentials.id, credentials.password);
94
+ return {
95
+ keystore,
96
+ credentials: decryptedCredentials
97
+ };
98
+ }
99
+ /**
100
+ * Generates an identity credential from a seed string
101
+ * This is a pure implementation that doesn't rely on Zerokit
102
+ * @param seed A string seed to generate the identity from
103
+ * @returns IdentityCredential
104
+ */
105
+ generateSeededIdentityCredential(seed) {
106
+ // Convert the seed to bytes
107
+ const encoder = new TextEncoder();
108
+ const seedBytes = encoder.encode(seed);
109
+ // Generate deterministic values using HMAC-SHA256
110
+ // We use different context strings for each component to ensure they're different
111
+ const idTrapdoor = hmac(sha256, seedBytes, encoder.encode("IDTrapdoor"));
112
+ const idNullifier = hmac(sha256, seedBytes, encoder.encode("IDNullifier"));
113
+ // Generate IDSecretHash as a hash of IDTrapdoor and IDNullifier
114
+ const combinedBytes = new Uint8Array([...idTrapdoor, ...idNullifier]);
115
+ const idSecretHash = sha256(combinedBytes);
116
+ // Generate IDCommitment as a hash of IDSecretHash
117
+ const idCommitment = sha256(idSecretHash);
118
+ // Convert IDCommitment to BigInt
119
+ const idCommitmentBigInt = buildBigIntFromUint8Array(idCommitment);
120
+ return new IdentityCredential(idTrapdoor, idNullifier, idSecretHash, idCommitment, idCommitmentBigInt);
121
+ }
122
+ async registerMembership(options) {
123
+ if (!this.contract) {
124
+ throw Error("RLN Contract is not initialized.");
125
+ }
126
+ let identity = "identity" in options && options.identity;
127
+ if ("signature" in options) {
128
+ identity = this.generateSeededIdentityCredential(options.signature);
129
+ }
130
+ if (!identity) {
131
+ throw Error("Missing signature or identity to register membership.");
132
+ }
133
+ return this.contract.registerWithIdentity(identity);
134
+ }
135
+ /**
136
+ * Changes credentials in use by relying on provided Keystore earlier in rln.start
137
+ * @param id: string, hash of credentials to select from Keystore
138
+ * @param password: string or bytes to use to decrypt credentials from Keystore
139
+ */
140
+ async useCredentials(id, password) {
141
+ this._credentials = await this.keystore?.readCredential(id, password);
142
+ }
143
+ }
144
+ //# sourceMappingURL=rln_light.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rln_light.js","sourceRoot":"","sources":["../src/rln_light.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,oBAAoB,CAAC;AAC1C,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAC9C,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAGrC,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAC3D,OAAO,EAAE,gBAAgB,EAAE,MAAM,kCAAkC,CAAC;AACpE,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AACnD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CAAC,CAAC;QAEnE,OAAO,IAAI,kBAAkB,CAC3B,UAAU,EACV,WAAW,EACX,YAAY,EACZ,YAAY,EACZ,kBAAkB,CACnB,CAAC;IACJ,CAAC;IAEM,KAAK,CAAC,kBAAkB,CAC7B,OAAkC;QAElC,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC;YACnB,MAAM,KAAK,CAAC,kCAAkC,CAAC,CAAC;QAClD,CAAC;QAED,IAAI,QAAQ,GAAG,UAAU,IAAI,OAAO,IAAI,OAAO,CAAC,QAAQ,CAAC;QAEzD,IAAI,WAAW,IAAI,OAAO,EAAE,CAAC;YAC3B,QAAQ,GAAG,IAAI,CAAC,gCAAgC,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;QACtE,CAAC;QAED,IAAI,CAAC,QAAQ,EAAE,CAAC;YACd,MAAM,KAAK,CAAC,uDAAuD,CAAC,CAAC;QACvE,CAAC;QAED,OAAO,IAAI,CAAC,QAAQ,CAAC,oBAAoB,CAAC,QAAQ,CAAC,CAAC;IACtD,CAAC;IAED;;;;OAIG;IACI,KAAK,CAAC,cAAc,CAAC,EAAU,EAAE,QAAkB;QACxD,IAAI,CAAC,YAAY,GAAG,MAAM,IAAI,CAAC,QAAQ,EAAE,cAAc,CAAC,EAAE,EAAE,QAAQ,CAAC,CAAC;IACxE,CAAC;CACF"}
package/package.json CHANGED
@@ -1 +1 @@
1
- {"name":"@waku/rln","version":"0.0.2-3670e82.0","description":"RLN (Rate Limiting Nullifier) implementation for Waku","types":"./dist/index.d.ts","module":"./dist/index.js","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.js"}},"type":"module","homepage":"https://github.com/waku-org/js-waku/tree/master/packages/rln#readme","repository":{"type":"git","url":"https://github.com/waku-org/js-waku.git"},"bugs":{"url":"https://github.com/waku-org/js-waku/issues"},"license":"MIT OR Apache-2.0","keywords":["waku","rln","rate-limiting","privacy","web3"],"scripts":{"build":"run-s build:**","build:copy":"mkdir -p dist/resources && cp -r src/resources/* dist/resources/","build:esm":"tsc","build:bundle":"rollup --config rollup.config.js","fix":"run-s fix:*","fix:lint":"eslint src *.js --fix","check":"run-s check:*","check:tsc":"tsc -p tsconfig.dev.json","check:lint":"eslint \"src/!(resources)/**/*.{ts,js}\" *.js","check:spelling":"cspell \"{README.md,src/**/*.ts}\"","test":"NODE_ENV=test run-s test:*","test:browser":"karma start karma.conf.cjs","watch:build":"tsc -p tsconfig.json -w","watch:test":"mocha --watch","prepublish":"npm run build","reset-hard":"git clean -dfx -e .idea && git reset --hard && npm i && npm run build"},"engines":{"node":">=20"},"devDependencies":{"@rollup/plugin-commonjs":"^25.0.7","@rollup/plugin-json":"^6.0.0","@rollup/plugin-node-resolve":"^15.2.3","@types/chai":"^5.0.1","@types/chai-spies":"^1.0.6","@types/deep-equal-in-any-order":"^1.0.4","@types/lodash":"^4.17.15","@types/sinon":"^17.0.3","@waku/build-utils":"^1.0.0","@waku/message-encryption":"0.0.32-3670e82.0","chai":"^5.1.2","chai-as-promised":"^8.0.1","chai-spies":"^1.1.0","chai-subset":"^1.6.0","deep-equal-in-any-order":"^2.0.6","fast-check":"^3.23.2","rollup-plugin-copy":"^3.5.0","sinon":"^19.0.2"},"files":["dist","bundle","src/**/*.ts","!**/*.spec.*","!**/*.json","CHANGELOG.md","LICENSE","README.md"],"dependencies":{"@chainsafe/bls-keystore":"3.0.0","@waku/core":"0.0.34-3670e82.0","@waku/utils":"0.0.22-3670e82.0","@waku/zerokit-rln-wasm":"^0.0.13","ethereum-cryptography":"^3.1.0","ethers":"^5.7.2","lodash":"^4.17.21","uuid":"^11.0.5"}}
1
+ {"name":"@waku/rln","version":"0.0.2-5c50ed7.0","description":"RLN (Rate Limiting Nullifier) implementation for Waku","types":"./dist/index.d.ts","module":"./dist/index.js","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.js"}},"type":"module","homepage":"https://github.com/waku-org/js-waku/tree/master/packages/rln#readme","repository":{"type":"git","url":"https://github.com/waku-org/js-waku.git"},"bugs":{"url":"https://github.com/waku-org/js-waku/issues"},"license":"MIT OR Apache-2.0","keywords":["waku","rln","rate-limiting","privacy","web3"],"scripts":{"build":"run-s build:**","build:copy":"mkdir -p dist/resources && cp -r src/resources/* dist/resources/","build:esm":"tsc","build:bundle":"rollup --config rollup.config.js","fix":"run-s fix:*","fix:lint":"eslint src *.js --fix","check":"run-s check:*","check:tsc":"tsc -p tsconfig.dev.json","check:lint":"eslint \"src/!(resources)/**/*.{ts,js}\" *.js","check:spelling":"cspell \"{README.md,src/**/*.ts}\"","test":"NODE_ENV=test run-s test:*","test:browser":"karma start karma.conf.cjs","watch:build":"tsc -p tsconfig.json -w","watch:test":"mocha --watch","prepublish":"npm run build","reset-hard":"git clean -dfx -e .idea && git reset --hard && npm i && npm run build"},"engines":{"node":">=20"},"devDependencies":{"@rollup/plugin-commonjs":"^25.0.7","@rollup/plugin-json":"^6.0.0","@rollup/plugin-node-resolve":"^15.2.3","@types/chai":"^5.0.1","@types/chai-spies":"^1.0.6","@types/deep-equal-in-any-order":"^1.0.4","@types/lodash":"^4.17.15","@types/sinon":"^17.0.3","@waku/build-utils":"^1.0.0","@waku/message-encryption":"0.0.32-5c50ed7.0","chai":"^5.1.2","chai-as-promised":"^8.0.1","chai-spies":"^1.1.0","chai-subset":"^1.6.0","deep-equal-in-any-order":"^2.0.6","fast-check":"^3.23.2","rollup-plugin-copy":"^3.5.0","sinon":"^19.0.2"},"files":["dist","bundle","src/**/*.ts","!**/*.spec.*","!**/*.json","CHANGELOG.md","LICENSE","README.md"],"dependencies":{"@chainsafe/bls-keystore":"3.0.0","@noble/hashes":"^1.2.0","@waku/core":"0.0.34-5c50ed7.0","@waku/utils":"0.0.22-5c50ed7.0","@waku/zerokit-rln-wasm":"^0.0.13","ethereum-cryptography":"^3.1.0","ethers":"^5.7.2","lodash":"^4.17.21","uuid":"^11.0.5"}}
@@ -1,4 +1,3 @@
1
- /* eslint-disable no-console */
2
1
  import { Logger } from "@waku/utils";
3
2
  import { hexToBytes } from "@waku/utils/bytes";
4
3
  import { ethers } from "ethers";
@@ -66,7 +65,7 @@ export class RLNContract {
66
65
 
67
66
  private _members: Map<number, Member> = new Map();
68
67
  private _membersFilter: ethers.EventFilter;
69
- private _membersRemovedFilter: ethers.EventFilter;
68
+ private _membershipErasedFilter: ethers.EventFilter;
70
69
  private _membersExpiredFilter: ethers.EventFilter;
71
70
 
72
71
  /**
@@ -115,7 +114,7 @@ export class RLNContract {
115
114
 
116
115
  // Initialize event filters
117
116
  this._membersFilter = this.contract.filters.MembershipRegistered();
118
- this._membersRemovedFilter = this.contract.filters.MembershipErased();
117
+ this._membershipErasedFilter = this.contract.filters.MembershipErased();
119
118
  this._membersExpiredFilter = this.contract.filters.MembershipExpired();
120
119
  }
121
120
 
@@ -185,7 +184,7 @@ export class RLNContract {
185
184
  this.contract.maxTotalRateLimit(),
186
185
  this.contract.currentTotalRateLimit()
187
186
  ]);
188
- return maxTotal.sub(currentTotal).toNumber();
187
+ return Number(maxTotal) - Number(currentTotal);
189
188
  }
190
189
 
191
190
  /**
@@ -210,11 +209,11 @@ export class RLNContract {
210
209
  return this._membersFilter;
211
210
  }
212
211
 
213
- private get membersRemovedFilter(): ethers.EventFilter {
214
- if (!this._membersRemovedFilter) {
215
- throw Error("MembersErased filter was not initialized.");
212
+ private get membershipErasedFilter(): ethers.EventFilter {
213
+ if (!this._membershipErasedFilter) {
214
+ throw Error("MembershipErased filter was not initialized.");
216
215
  }
217
- return this._membersRemovedFilter;
216
+ return this._membershipErasedFilter;
218
217
  }
219
218
 
220
219
  private get membersExpiredFilter(): ethers.EventFilter {
@@ -236,7 +235,7 @@ export class RLNContract {
236
235
  const removedMemberEvents = await queryFilter(this.contract, {
237
236
  fromBlock: this.deployBlock,
238
237
  ...options,
239
- membersFilter: this.membersRemovedFilter
238
+ membersFilter: this.membershipErasedFilter
240
239
  });
241
240
  const expiredMemberEvents = await queryFilter(this.contract, {
242
241
  fromBlock: this.deployBlock,
@@ -265,14 +264,12 @@ export class RLNContract {
265
264
  evt.event === "MembershipErased" ||
266
265
  evt.event === "MembershipExpired"
267
266
  ) {
268
- // Both MembershipErased and MembershipExpired events should remove members
269
267
  let index = evt.args.index;
270
268
 
271
269
  if (!index) {
272
270
  return;
273
271
  }
274
272
 
275
- // Convert index to ethers.BigNumber if it's not already
276
273
  if (typeof index === "number" || typeof index === "string") {
277
274
  index = ethers.BigNumber.from(index);
278
275
  }
@@ -310,12 +307,10 @@ export class RLNContract {
310
307
  const _idCommitment = evt.args.idCommitment as string;
311
308
  let index = evt.args.index;
312
309
 
313
- // Ensure index is an ethers.BigNumber
314
310
  if (!_idCommitment || !index) {
315
311
  return;
316
312
  }
317
313
 
318
- // Convert index to ethers.BigNumber if it's not already
319
314
  if (typeof index === "number" || typeof index === "string") {
320
315
  index = ethers.BigNumber.from(index);
321
316
  }
@@ -323,10 +318,9 @@ export class RLNContract {
323
318
  const idCommitment = zeroPadLE(hexToBytes(_idCommitment), 32);
324
319
  rlnInstance.zerokit.insertMember(idCommitment);
325
320
 
326
- // Always store the numeric index as the key, but the BigNumber as the value
327
321
  const numericIndex = index.toNumber();
328
322
  this._members.set(numericIndex, {
329
- index, // This is always a BigNumber
323
+ index,
330
324
  idCommitment: _idCommitment
331
325
  });
332
326
  });
@@ -367,7 +361,7 @@ export class RLNContract {
367
361
  );
368
362
 
369
363
  this.contract.on(
370
- this.membersRemovedFilter,
364
+ this.membershipErasedFilter,
371
365
  (
372
366
  _idCommitment: string,
373
367
  _membershipRateLimit: ethers.BigNumber,
@@ -395,111 +389,76 @@ export class RLNContract {
395
389
  identity: IdentityCredential
396
390
  ): Promise<DecryptedCredentials | undefined> {
397
391
  try {
398
- console.log("registerWithIdentity - starting registration process");
399
- console.log("registerWithIdentity - identity:", identity);
400
- console.log(
401
- "registerWithIdentity - IDCommitmentBigInt:",
392
+ log.info(
393
+ `Registering identity with rate limit: ${this.rateLimit} messages/epoch`
394
+ );
395
+
396
+ // Check if the ID commitment is already registered
397
+ const existingIndex = await this.getMemberIndex(
402
398
  identity.IDCommitmentBigInt.toString()
403
399
  );
404
- console.log("registerWithIdentity - rate limit:", this.rateLimit);
400
+ if (existingIndex) {
401
+ throw new Error(
402
+ `ID commitment is already registered with index ${existingIndex}`
403
+ );
404
+ }
405
405
 
406
- log.info(
407
- `Registering identity with rate limit: ${this.rateLimit} messages/epoch`
406
+ // Check if there's enough remaining rate limit
407
+ const remainingRateLimit = await this.getRemainingTotalRateLimit();
408
+ if (remainingRateLimit < this.rateLimit) {
409
+ throw new Error(
410
+ `Not enough remaining rate limit. Requested: ${this.rateLimit}, Available: ${remainingRateLimit}`
411
+ );
412
+ }
413
+
414
+ const estimatedGas = await this.contract.estimateGas.register(
415
+ identity.IDCommitmentBigInt,
416
+ this.rateLimit,
417
+ []
408
418
  );
419
+ const gasLimit = estimatedGas.add(10000);
409
420
 
410
- console.log("registerWithIdentity - calling contract.register");
411
421
  const txRegisterResponse: ethers.ContractTransaction =
412
422
  await this.contract.register(
413
423
  identity.IDCommitmentBigInt,
414
424
  this.rateLimit,
415
425
  [],
416
- { gasLimit: 300000 }
426
+ { gasLimit }
417
427
  );
418
- console.log(
419
- "registerWithIdentity - txRegisterResponse:",
420
- txRegisterResponse
421
- );
422
- console.log("registerWithIdentity - hash:", txRegisterResponse.hash);
423
- console.log(
424
- "registerWithIdentity - waiting for transaction confirmation..."
425
- );
426
428
 
427
429
  const txRegisterReceipt = await txRegisterResponse.wait();
428
- console.log(
429
- "registerWithIdentity - txRegisterReceipt:",
430
- txRegisterReceipt
431
- );
432
- console.log(
433
- "registerWithIdentity - transaction status:",
434
- txRegisterReceipt.status
435
- );
436
- console.log(
437
- "registerWithIdentity - block number:",
438
- txRegisterReceipt.blockNumber
439
- );
440
- console.log(
441
- "registerWithIdentity - gas used:",
442
- txRegisterReceipt.gasUsed.toString()
443
- );
430
+
431
+ if (txRegisterReceipt.status === 0) {
432
+ throw new Error("Transaction failed on-chain");
433
+ }
444
434
 
445
435
  const memberRegistered = txRegisterReceipt.events?.find(
446
436
  (event) => event.event === "MembershipRegistered"
447
437
  );
448
- console.log(
449
- "registerWithIdentity - memberRegistered event:",
450
- memberRegistered
451
- );
452
438
 
453
439
  if (!memberRegistered || !memberRegistered.args) {
454
- console.log(
455
- "registerWithIdentity - ERROR: no memberRegistered event found"
456
- );
457
- console.log(
458
- "registerWithIdentity - all events:",
459
- txRegisterReceipt.events
460
- );
461
440
  log.error(
462
441
  "Failed to register membership: No MembershipRegistered event found"
463
442
  );
464
443
  return undefined;
465
444
  }
466
445
 
467
- console.log(
468
- "registerWithIdentity - memberRegistered args:",
469
- memberRegistered.args
470
- );
471
446
  const decodedData: MembershipRegisteredEvent = {
472
447
  idCommitment: memberRegistered.args.idCommitment,
473
448
  membershipRateLimit: memberRegistered.args.membershipRateLimit,
474
449
  index: memberRegistered.args.index
475
450
  };
476
- console.log("registerWithIdentity - decodedData:", decodedData);
477
- console.log(
478
- "registerWithIdentity - index:",
479
- decodedData.index.toString()
480
- );
481
- console.log(
482
- "registerWithIdentity - membershipRateLimit:",
483
- decodedData.membershipRateLimit.toString()
484
- );
485
451
 
486
452
  log.info(
487
453
  `Successfully registered membership with index ${decodedData.index} ` +
488
454
  `and rate limit ${decodedData.membershipRateLimit}`
489
455
  );
490
456
 
491
- console.log("registerWithIdentity - getting network information");
492
457
  const network = await this.contract.provider.getNetwork();
493
- console.log("registerWithIdentity - network:", network);
494
- console.log("registerWithIdentity - chainId:", network.chainId);
495
-
496
458
  const address = this.contract.address;
497
- console.log("registerWithIdentity - contract address:", address);
498
-
499
- const membershipId = decodedData.index.toNumber();
500
- console.log("registerWithIdentity - membershipId:", membershipId);
459
+ const membershipId = Number(decodedData.index);
501
460
 
502
- const result = {
461
+ return {
503
462
  identity,
504
463
  membership: {
505
464
  address,
@@ -507,21 +466,33 @@ export class RLNContract {
507
466
  chainId: network.chainId
508
467
  }
509
468
  };
510
- console.log("registerWithIdentity - returning result:", result);
511
-
512
- return result;
513
469
  } catch (error) {
514
- console.log("registerWithIdentity - ERROR:", error);
515
- console.log(
516
- "registerWithIdentity - error message:",
517
- (error as Error).message
518
- );
519
- console.log(
520
- "registerWithIdentity - error stack:",
521
- (error as Error).stack
522
- );
523
- log.error(`Error in registerWithIdentity: ${(error as Error).message}`);
524
- return undefined;
470
+ if (error instanceof Error) {
471
+ const errorMessage = error.message;
472
+ log.error("registerWithIdentity - error message:", errorMessage);
473
+ log.error("registerWithIdentity - error stack:", error.stack);
474
+
475
+ // Try to extract more specific error information
476
+ if (errorMessage.includes("CannotExceedMaxTotalRateLimit")) {
477
+ throw new Error(
478
+ "Registration failed: Cannot exceed maximum total rate limit"
479
+ );
480
+ } else if (errorMessage.includes("InvalidIdCommitment")) {
481
+ throw new Error("Registration failed: Invalid ID commitment");
482
+ } else if (errorMessage.includes("InvalidMembershipRateLimit")) {
483
+ throw new Error("Registration failed: Invalid membership rate limit");
484
+ } else if (errorMessage.includes("execution reverted")) {
485
+ throw new Error(
486
+ "Contract execution reverted. Check contract requirements."
487
+ );
488
+ } else {
489
+ throw new Error(`Error in registerWithIdentity: ${errorMessage}`);
490
+ }
491
+ } else {
492
+ throw new Error("Unknown error in registerWithIdentity", {
493
+ cause: error
494
+ });
495
+ }
525
496
  }
526
497
  }
527
498
 
@@ -610,7 +581,7 @@ export class RLNContract {
610
581
 
611
582
  const network = await this.contract.provider.getNetwork();
612
583
  const address = this.contract.address;
613
- const membershipId = decodedData.index.toNumber();
584
+ const membershipId = Number(decodedData.index);
614
585
 
615
586
  return {
616
587
  identity,
@@ -676,20 +647,18 @@ export class RLNContract {
676
647
 
677
648
  public async extendMembership(
678
649
  idCommitment: string
679
- ): Promise<ethers.ContractReceipt> {
680
- const tx = await this.contract.extendMemberships([idCommitment]);
681
- return await tx.wait();
650
+ ): Promise<ethers.ContractTransaction> {
651
+ return this.contract.extendMemberships([idCommitment]);
682
652
  }
683
653
 
684
654
  public async eraseMembership(
685
655
  idCommitment: string,
686
656
  eraseFromMembershipSet: boolean = true
687
- ): Promise<ethers.ContractReceipt> {
688
- const tx = await this.contract.eraseMemberships(
657
+ ): Promise<ethers.ContractTransaction> {
658
+ return this.contract.eraseMemberships(
689
659
  [idCommitment],
690
660
  eraseFromMembershipSet
691
661
  );
692
- return await tx.wait();
693
662
  }
694
663
 
695
664
  public async registerMembership(
@@ -704,10 +673,7 @@ export class RLNContract {
704
673
  `Rate limit must be between ${RATE_LIMIT_PARAMS.MIN_RATE} and ${RATE_LIMIT_PARAMS.MAX_RATE}`
705
674
  );
706
675
  }
707
- console.log("registering membership", idCommitment, rateLimit);
708
- const txn = this.contract.register(idCommitment, rateLimit, []);
709
- console.log("txn", txn);
710
- return txn;
676
+ return this.contract.register(idCommitment, rateLimit, []);
711
677
  }
712
678
 
713
679
  private async getMemberIndex(