@waku/rln 0.1.5-ea6daae.0 → 0.1.5-ec5db88.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 (143) hide show
  1. package/README.md +5 -0
  2. package/bundle/_virtual/index2.js +1 -1
  3. package/bundle/_virtual/utils.js +2 -2
  4. package/bundle/_virtual/utils2.js +2 -2
  5. package/bundle/index.js +3 -1
  6. package/bundle/node_modules/@ethersproject/abi/lib.esm/_version.js +1 -1
  7. package/bundle/node_modules/@ethersproject/abstract-provider/lib.esm/_version.js +1 -1
  8. package/bundle/node_modules/@ethersproject/abstract-signer/lib.esm/_version.js +1 -1
  9. package/bundle/node_modules/@ethersproject/address/lib.esm/_version.js +1 -1
  10. package/bundle/node_modules/@ethersproject/bignumber/lib.esm/_version.js +1 -1
  11. package/bundle/node_modules/@ethersproject/contracts/lib.esm/_version.js +1 -1
  12. package/bundle/node_modules/@ethersproject/contracts/lib.esm/index.js +1 -1
  13. package/bundle/node_modules/@ethersproject/hash/lib.esm/_version.js +1 -1
  14. package/bundle/node_modules/@ethersproject/keccak256/lib.esm/index.js +1 -1
  15. package/bundle/node_modules/{js-sha3 → @ethersproject/keccak256/node_modules/js-sha3}/src/sha3.js +2 -2
  16. package/bundle/node_modules/@ethersproject/networks/lib.esm/_version.js +1 -1
  17. package/bundle/node_modules/@ethersproject/networks/lib.esm/index.js +1 -19
  18. package/bundle/node_modules/@ethersproject/properties/lib.esm/_version.js +1 -1
  19. package/bundle/node_modules/@ethersproject/providers/lib.esm/_version.js +1 -1
  20. package/bundle/node_modules/@ethersproject/signing-key/lib.esm/_version.js +1 -1
  21. package/bundle/node_modules/@ethersproject/signing-key/lib.esm/elliptic.js +10 -58
  22. package/bundle/node_modules/@ethersproject/strings/lib.esm/_version.js +1 -1
  23. package/bundle/node_modules/@ethersproject/transactions/lib.esm/_version.js +1 -1
  24. package/bundle/node_modules/@ethersproject/web/lib.esm/_version.js +1 -1
  25. package/bundle/packages/rln/dist/contract/abi.js +502 -248
  26. package/bundle/packages/rln/dist/contract/constants.js +5 -8
  27. package/bundle/packages/rln/dist/contract/rln_base_contract.js +465 -0
  28. package/bundle/packages/rln/dist/contract/rln_contract.js +31 -357
  29. package/bundle/packages/rln/dist/contract/types.js +9 -0
  30. package/bundle/packages/rln/dist/create.js +1 -1
  31. package/bundle/packages/rln/dist/credentials_manager.js +215 -0
  32. package/bundle/packages/rln/dist/keystore/cipher.js +3 -3
  33. package/bundle/packages/rln/dist/keystore/keystore.js +12 -7
  34. package/bundle/packages/rln/dist/rln.js +56 -166
  35. package/bundle/packages/rln/dist/zerokit.js +5 -5
  36. package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/lib/checksum.js +3 -3
  37. package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/lib/cipher.js +4 -4
  38. package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/lib/class.js +7 -7
  39. package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/lib/functional.js +7 -7
  40. package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/lib/index.js +6 -6
  41. package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/lib/kdf.js +5 -5
  42. package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/lib/password.js +1 -1
  43. package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/lib/schema-validation-generated.js +1 -1
  44. package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/lib/schema-validation.js +2 -2
  45. package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/lib/types.js +1 -1
  46. package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/aes.js +3 -3
  47. package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/pbkdf2.js +7 -7
  48. package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/random.js +3 -3
  49. package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/scrypt.js +3 -3
  50. package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/sha256.js +3 -3
  51. package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/utils.js +7 -7
  52. package/bundle/{node_modules/@chainsafe/bls-keystore → packages/rln}/node_modules/@noble/hashes/_assert.js +1 -1
  53. package/bundle/{node_modules/@chainsafe/bls-keystore → packages/rln}/node_modules/@noble/hashes/_sha2.js +3 -3
  54. package/bundle/{node_modules/@chainsafe/bls-keystore → packages/rln}/node_modules/@noble/hashes/_u64.js +1 -1
  55. package/bundle/{node_modules/@chainsafe/bls-keystore → packages/rln}/node_modules/@noble/hashes/cryptoBrowser.js +1 -1
  56. package/bundle/packages/rln/node_modules/@noble/hashes/esm/_assert.js +43 -0
  57. package/bundle/packages/rln/node_modules/@noble/hashes/esm/_sha2.js +116 -0
  58. package/bundle/packages/rln/node_modules/@noble/hashes/esm/hmac.js +79 -0
  59. package/bundle/packages/rln/node_modules/@noble/hashes/esm/sha256.js +126 -0
  60. package/bundle/packages/rln/node_modules/@noble/hashes/esm/utils.js +43 -0
  61. package/bundle/{node_modules/@chainsafe/bls-keystore → packages/rln}/node_modules/@noble/hashes/hmac.js +3 -3
  62. package/bundle/{node_modules/@chainsafe/bls-keystore → packages/rln}/node_modules/@noble/hashes/pbkdf2.js +4 -4
  63. package/bundle/{node_modules/@chainsafe/bls-keystore → packages/rln}/node_modules/@noble/hashes/scrypt.js +5 -5
  64. package/bundle/{node_modules/@chainsafe/bls-keystore → packages/rln}/node_modules/@noble/hashes/sha256.js +3 -3
  65. package/bundle/{node_modules/@chainsafe/bls-keystore → packages/rln}/node_modules/@noble/hashes/sha512.js +4 -4
  66. package/bundle/{node_modules/@chainsafe/bls-keystore → packages/rln}/node_modules/@noble/hashes/utils.js +2 -2
  67. package/dist/.tsbuildinfo +1 -1
  68. package/dist/codec.test-utils.d.ts +36 -0
  69. package/dist/codec.test-utils.js +56 -0
  70. package/dist/codec.test-utils.js.map +1 -0
  71. package/dist/contract/abi.d.ts +21 -17
  72. package/dist/contract/abi.js +502 -248
  73. package/dist/contract/abi.js.map +1 -1
  74. package/dist/contract/constants.d.ts +22 -18
  75. package/dist/contract/constants.js +3 -3
  76. package/dist/contract/constants.js.map +1 -1
  77. package/dist/contract/rln_base_contract.d.ts +90 -0
  78. package/dist/contract/rln_base_contract.js +448 -0
  79. package/dist/contract/rln_base_contract.js.map +1 -0
  80. package/dist/contract/rln_contract.d.ts +5 -115
  81. package/dist/contract/rln_contract.js +30 -355
  82. package/dist/contract/rln_contract.js.map +1 -1
  83. package/dist/contract/test-setup.d.ts +26 -0
  84. package/dist/contract/test-setup.js +56 -0
  85. package/dist/contract/test-setup.js.map +1 -0
  86. package/dist/contract/test-utils.d.ts +39 -0
  87. package/dist/contract/test-utils.js +118 -0
  88. package/dist/contract/test-utils.js.map +1 -0
  89. package/dist/contract/types.d.ts +45 -0
  90. package/dist/contract/types.js +8 -0
  91. package/dist/contract/types.js.map +1 -0
  92. package/dist/create.js +1 -1
  93. package/dist/create.js.map +1 -1
  94. package/dist/credentials_manager.d.ts +44 -0
  95. package/dist/credentials_manager.js +197 -0
  96. package/dist/credentials_manager.js.map +1 -0
  97. package/dist/index.d.ts +5 -2
  98. package/dist/index.js +4 -2
  99. package/dist/index.js.map +1 -1
  100. package/dist/keystore/keystore.js +11 -6
  101. package/dist/keystore/keystore.js.map +1 -1
  102. package/dist/keystore/types.d.ts +2 -1
  103. package/dist/rln.d.ts +9 -52
  104. package/dist/rln.js +54 -163
  105. package/dist/rln.js.map +1 -1
  106. package/dist/types.d.ts +27 -0
  107. package/dist/types.js +2 -0
  108. package/dist/types.js.map +1 -0
  109. package/dist/zerokit.d.ts +3 -3
  110. package/dist/zerokit.js +5 -5
  111. package/dist/zerokit.js.map +1 -1
  112. package/package.json +1 -1
  113. package/src/codec.test-utils.ts +80 -0
  114. package/src/contract/abi.ts +502 -248
  115. package/src/contract/constants.ts +3 -3
  116. package/src/contract/rln_base_contract.ts +684 -0
  117. package/src/contract/rln_contract.ts +37 -579
  118. package/src/contract/test-setup.ts +86 -0
  119. package/src/contract/test-utils.ts +179 -0
  120. package/src/contract/types.ts +53 -0
  121. package/src/create.ts +1 -1
  122. package/src/credentials_manager.ts +282 -0
  123. package/src/index.ts +17 -2
  124. package/src/keystore/keystore.ts +19 -10
  125. package/src/keystore/types.ts +2 -1
  126. package/src/rln.ts +67 -258
  127. package/src/types.ts +31 -0
  128. package/src/zerokit.ts +3 -3
  129. /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/index.js +0 -0
  130. /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/md5.js +0 -0
  131. /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/nil.js +0 -0
  132. /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/parse.js +0 -0
  133. /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/regex.js +0 -0
  134. /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/rng.js +0 -0
  135. /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/sha1.js +0 -0
  136. /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/stringify.js +0 -0
  137. /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/v1.js +0 -0
  138. /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/v3.js +0 -0
  139. /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/v35.js +0 -0
  140. /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/v4.js +0 -0
  141. /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/v5.js +0 -0
  142. /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/validate.js +0 -0
  143. /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/version.js +0 -0
@@ -8,9 +8,10 @@ export type Password = string | Uint8Array;
8
8
  // see reference
9
9
  // https://github.com/waku-org/nwaku/blob/f05528d4be3d3c876a8b07f9bb7dfaae8aa8ec6e/waku/waku_keystore/protocol_types.nim#L111
10
10
  export type MembershipInfo = {
11
- chainId: number;
11
+ chainId: string;
12
12
  address: string;
13
13
  treeIndex: number;
14
+ rateLimit: number;
14
15
  };
15
16
 
16
17
  export type KeystoreEntity = {
package/src/rln.ts CHANGED
@@ -7,7 +7,6 @@ import type {
7
7
  import { Logger } from "@waku/utils";
8
8
  import init from "@waku/zerokit-rln-wasm";
9
9
  import * as zerokitRLN from "@waku/zerokit-rln-wasm";
10
- import { ethers } from "ethers";
11
10
 
12
11
  import {
13
12
  createRLNDecoder,
@@ -16,258 +15,52 @@ import {
16
15
  type RLNEncoder
17
16
  } from "./codec.js";
18
17
  import { DEFAULT_RATE_LIMIT } from "./contract/constants.js";
19
- import { RLNContract, SEPOLIA_CONTRACT } from "./contract/index.js";
20
- import { IdentityCredential } from "./identity.js";
21
- import { Keystore } from "./keystore/index.js";
18
+ import { RLNCredentialsManager } from "./credentials_manager.js";
22
19
  import type {
23
20
  DecryptedCredentials,
24
21
  EncryptedCredentials
25
22
  } from "./keystore/index.js";
26
- import { KeystoreEntity, Password } from "./keystore/types.js";
27
23
  import verificationKey from "./resources/verification_key";
28
24
  import * as wc from "./resources/witness_calculator";
29
25
  import { WitnessCalculator } from "./resources/witness_calculator";
30
- import { extractMetaMaskSigner } from "./utils/index.js";
31
26
  import { Zerokit } from "./zerokit.js";
32
27
 
33
28
  const log = new Logger("waku:rln");
34
29
 
35
- async function loadWitnessCalculator(): Promise<WitnessCalculator> {
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
- }
56
- }
57
-
58
- async function loadZkey(): Promise<Uint8Array> {
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
- }
76
- }
77
-
78
- /**
79
- * Create an instance of RLN
80
- * @returns RLNInstance
81
- */
82
- export async function create(): Promise<RLNInstance> {
83
- try {
84
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
85
- await (init as any)?.();
86
- zerokitRLN.init_panic_hook();
87
-
88
- const witnessCalculator = await loadWitnessCalculator();
89
- const zkey = await loadZkey();
90
-
91
- const stringEncoder = new TextEncoder();
92
- const vkey = stringEncoder.encode(JSON.stringify(verificationKey));
93
-
94
- const DEPTH = 20;
95
- const zkRLN = zerokitRLN.newRLN(DEPTH, zkey, vkey);
96
- const zerokit = new Zerokit(zkRLN, witnessCalculator, DEFAULT_RATE_LIMIT);
97
-
98
- return new RLNInstance(zerokit);
99
- } catch (error) {
100
- log.error("Failed to initialize RLN:", error);
101
- throw error;
102
- }
103
- }
104
-
105
- type StartRLNOptions = {
106
- /**
107
- * If not set - will extract MetaMask account and get signer from it.
108
- */
109
- signer?: ethers.Signer;
110
- /**
111
- * If not set - will use default SEPOLIA_CONTRACT address.
112
- */
113
- address?: string;
114
- /**
115
- * Credentials to use for generating proofs and connecting to the contract and network.
116
- * If provided used for validating the network chainId and connecting to registry contract.
117
- */
118
- credentials?: EncryptedCredentials | DecryptedCredentials;
119
- /**
120
- * Rate limit for the member.
121
- */
122
- rateLimit?: number;
123
- };
124
-
125
- type RegisterMembershipOptions =
126
- | { signature: string }
127
- | { identity: IdentityCredential };
128
-
129
30
  type WakuRLNEncoderOptions = WakuEncoderOptions & {
130
31
  credentials: EncryptedCredentials | DecryptedCredentials;
131
32
  };
132
33
 
133
- export class RLNInstance {
134
- private started = false;
135
- private starting = false;
136
-
137
- private _contract: undefined | RLNContract;
138
- private _signer: undefined | ethers.Signer;
139
-
140
- private keystore = Keystore.create();
141
- private _credentials: undefined | DecryptedCredentials;
142
-
143
- public constructor(public zerokit: Zerokit) {}
144
-
145
- public get contract(): undefined | RLNContract {
146
- return this._contract;
147
- }
148
-
149
- public get signer(): undefined | ethers.Signer {
150
- return this._signer;
151
- }
152
-
153
- public async start(options: StartRLNOptions = {}): Promise<void> {
154
- if (this.started || this.starting) {
155
- return;
156
- }
157
-
158
- this.starting = true;
159
-
34
+ export class RLNInstance extends RLNCredentialsManager {
35
+ /**
36
+ * Create an instance of RLN
37
+ * @returns RLNInstance
38
+ */
39
+ public static async create(): Promise<RLNInstance> {
160
40
  try {
161
- const { credentials, keystore } =
162
- await RLNInstance.decryptCredentialsIfNeeded(options.credentials);
163
- const { signer, address } = await this.determineStartOptions(
164
- options,
165
- credentials
166
- );
167
-
168
- if (keystore) {
169
- this.keystore = keystore;
170
- }
171
-
172
- this._credentials = credentials;
173
- this._signer = signer!;
174
- this._contract = await RLNContract.init(this, {
175
- address: address!,
176
- signer: signer!,
177
- rateLimit: options.rateLimit ?? this.zerokit.getRateLimit
178
- });
179
- this.started = true;
180
- } finally {
181
- this.starting = false;
182
- }
183
- }
184
-
185
- private async determineStartOptions(
186
- options: StartRLNOptions,
187
- credentials: KeystoreEntity | undefined
188
- ): Promise<StartRLNOptions> {
189
- let chainId = credentials?.membership.chainId;
190
- const address =
191
- credentials?.membership.address ||
192
- options.address ||
193
- SEPOLIA_CONTRACT.address;
194
-
195
- if (address === SEPOLIA_CONTRACT.address) {
196
- chainId = SEPOLIA_CONTRACT.chainId;
197
- }
41
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
42
+ await (init as any)?.();
43
+ zerokitRLN.init_panic_hook();
198
44
 
199
- const signer = options.signer || (await extractMetaMaskSigner());
200
- const currentChainId = await signer.getChainId();
45
+ const witnessCalculator = await RLNInstance.loadWitnessCalculator();
46
+ const zkey = await RLNInstance.loadZkey();
201
47
 
202
- if (chainId && chainId !== currentChainId) {
203
- throw Error(
204
- `Failed to start RLN contract, chain ID of contract is different from current one: contract-${chainId}, current network-${currentChainId}`
205
- );
206
- }
207
-
208
- return {
209
- signer,
210
- address
211
- };
212
- }
213
-
214
- private static async decryptCredentialsIfNeeded(
215
- credentials?: EncryptedCredentials | DecryptedCredentials
216
- ): Promise<{ credentials?: DecryptedCredentials; keystore?: Keystore }> {
217
- if (!credentials) {
218
- return {};
219
- }
220
-
221
- if ("identity" in credentials) {
222
- return { credentials };
223
- }
224
-
225
- const keystore = Keystore.fromString(credentials.keystore);
226
-
227
- if (!keystore) {
228
- return {};
229
- }
230
-
231
- const decryptedCredentials = await keystore.readCredential(
232
- credentials.id,
233
- credentials.password
234
- );
48
+ const stringEncoder = new TextEncoder();
49
+ const vkey = stringEncoder.encode(JSON.stringify(verificationKey));
235
50
 
236
- return {
237
- keystore,
238
- credentials: decryptedCredentials
239
- };
240
- }
241
-
242
- public async registerMembership(
243
- options: RegisterMembershipOptions
244
- ): Promise<undefined | DecryptedCredentials> {
245
- if (!this.contract) {
246
- throw Error("RLN Contract is not initialized.");
247
- }
248
-
249
- let identity = "identity" in options && options.identity;
250
-
251
- if ("signature" in options) {
252
- identity = this.zerokit.generateSeededIdentityCredential(
253
- options.signature
254
- );
255
- }
51
+ const DEPTH = 20;
52
+ const zkRLN = zerokitRLN.newRLN(DEPTH, zkey, vkey);
53
+ const zerokit = new Zerokit(zkRLN, witnessCalculator, DEFAULT_RATE_LIMIT);
256
54
 
257
- if (!identity) {
258
- throw Error("Missing signature or identity to register membership.");
55
+ return new RLNInstance(zerokit);
56
+ } catch (error) {
57
+ log.error("Failed to initialize RLN:", error);
58
+ throw error;
259
59
  }
260
-
261
- return this.contract.registerWithIdentity(identity);
262
60
  }
263
61
 
264
- /**
265
- * Changes credentials in use by relying on provided Keystore earlier in rln.start
266
- * @param id: string, hash of credentials to select from Keystore
267
- * @param password: string or bytes to use to decrypt credentials from Keystore
268
- */
269
- public async useCredentials(id: string, password: Password): Promise<void> {
270
- this._credentials = await this.keystore?.readCredential(id, password);
62
+ private constructor(public zerokit: Zerokit) {
63
+ super(zerokit);
271
64
  }
272
65
 
273
66
  public async createEncoder(
@@ -275,7 +68,7 @@ export class RLNInstance {
275
68
  ): Promise<RLNEncoder> {
276
69
  const { credentials: decryptedCredentials } =
277
70
  await RLNInstance.decryptCredentialsIfNeeded(options.credentials);
278
- const credentials = decryptedCredentials || this._credentials;
71
+ const credentials = decryptedCredentials || this.credentials;
279
72
 
280
73
  if (!credentials) {
281
74
  throw Error(
@@ -293,33 +86,6 @@ export class RLNInstance {
293
86
  });
294
87
  }
295
88
 
296
- private async verifyCredentialsAgainstContract(
297
- credentials: KeystoreEntity
298
- ): Promise<void> {
299
- if (!this._contract) {
300
- throw Error(
301
- "Failed to verify chain coordinates: no contract initialized."
302
- );
303
- }
304
-
305
- const registryAddress = credentials.membership.address;
306
- const currentRegistryAddress = this._contract.address;
307
- if (registryAddress !== currentRegistryAddress) {
308
- throw Error(
309
- `Failed to verify chain coordinates: credentials contract address=${registryAddress} is not equal to registryContract address=${currentRegistryAddress}`
310
- );
311
- }
312
-
313
- const chainId = credentials.membership.chainId;
314
- const network = await this._contract.provider.getNetwork();
315
- const currentChainId = network.chainId;
316
- if (chainId !== currentChainId) {
317
- throw Error(
318
- `Failed to verify chain coordinates: credentials chainID=${chainId} is not equal to registryContract chainID=${currentChainId}`
319
- );
320
- }
321
- }
322
-
323
89
  public createDecoder(
324
90
  contentTopic: ContentTopic
325
91
  ): RLNDecoder<IDecodedMessage> {
@@ -328,4 +94,47 @@ export class RLNInstance {
328
94
  decoder: createDecoder(contentTopic)
329
95
  });
330
96
  }
97
+
98
+ public static async loadWitnessCalculator(): Promise<WitnessCalculator> {
99
+ try {
100
+ const url = new URL("./resources/rln.wasm", import.meta.url);
101
+ const response = await fetch(url);
102
+
103
+ if (!response.ok) {
104
+ throw new Error(
105
+ `Failed to fetch witness calculator: ${response.status} ${response.statusText}`
106
+ );
107
+ }
108
+
109
+ return await wc.builder(
110
+ new Uint8Array(await response.arrayBuffer()),
111
+ false
112
+ );
113
+ } catch (error) {
114
+ log.error("Error loading witness calculator:", error);
115
+ throw new Error(
116
+ `Failed to load witness calculator: ${error instanceof Error ? error.message : String(error)}`
117
+ );
118
+ }
119
+ }
120
+
121
+ public static async loadZkey(): Promise<Uint8Array> {
122
+ try {
123
+ const url = new URL("./resources/rln_final.zkey", import.meta.url);
124
+ const response = await fetch(url);
125
+
126
+ if (!response.ok) {
127
+ throw new Error(
128
+ `Failed to fetch zkey: ${response.status} ${response.statusText}`
129
+ );
130
+ }
131
+
132
+ return new Uint8Array(await response.arrayBuffer());
133
+ } catch (error) {
134
+ log.error("Error loading zkey:", error);
135
+ throw new Error(
136
+ `Failed to load zkey: ${error instanceof Error ? error.message : String(error)}`
137
+ );
138
+ }
139
+ }
331
140
  }
package/src/types.ts ADDED
@@ -0,0 +1,31 @@
1
+ import { ethers } from "ethers";
2
+
3
+ import { IdentityCredential } from "./identity.js";
4
+ import {
5
+ DecryptedCredentials,
6
+ EncryptedCredentials
7
+ } from "./keystore/types.js";
8
+
9
+ export type StartRLNOptions = {
10
+ /**
11
+ * If not set - will extract MetaMask account and get signer from it.
12
+ */
13
+ signer?: ethers.Signer;
14
+ /**
15
+ * If not set - will use default SEPOLIA_CONTRACT address.
16
+ */
17
+ address?: string;
18
+ /**
19
+ * Credentials to use for generating proofs and connecting to the contract and network.
20
+ * If provided used for validating the network chainId and connecting to registry contract.
21
+ */
22
+ credentials?: EncryptedCredentials | DecryptedCredentials;
23
+ /**
24
+ * Rate limit for the member.
25
+ */
26
+ rateLimit?: number;
27
+ };
28
+
29
+ export type RegisterMembershipOptions =
30
+ | { signature: string }
31
+ | { identity: IdentityCredential };
package/src/zerokit.ts CHANGED
@@ -16,7 +16,7 @@ export class Zerokit {
16
16
  public constructor(
17
17
  private readonly zkRLN: number,
18
18
  private readonly witnessCalculator: WitnessCalculator,
19
- private readonly rateLimit: number = DEFAULT_RATE_LIMIT
19
+ private readonly _rateLimit: number = DEFAULT_RATE_LIMIT
20
20
  ) {}
21
21
 
22
22
  public get getZkRLN(): number {
@@ -27,8 +27,8 @@ export class Zerokit {
27
27
  return this.witnessCalculator;
28
28
  }
29
29
 
30
- public get getRateLimit(): number {
31
- return this.rateLimit;
30
+ public get rateLimit(): number {
31
+ return this._rateLimit;
32
32
  }
33
33
 
34
34
  public generateIdentityCredentials(): IdentityCredential {