@waku/rln 0.1.5-cad3e7a.0 → 0.1.5-d0da8f8.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 (179) hide show
  1. package/bundle/_virtual/utils.js +2 -2
  2. package/bundle/_virtual/utils2.js +2 -2
  3. package/bundle/index.js +4 -3
  4. package/bundle/packages/rln/dist/contract/constants.js +2 -1
  5. package/bundle/packages/rln/dist/contract/{rln_light_contract.js → rln_base_contract.js} +189 -186
  6. package/bundle/packages/rln/dist/contract/rln_contract.js +10 -420
  7. package/bundle/packages/rln/dist/contract/types.js +9 -0
  8. package/bundle/packages/rln/dist/create.js +1 -1
  9. package/bundle/packages/rln/dist/{rln_light.js → credentials_manager.js} +114 -48
  10. package/bundle/packages/rln/dist/identity.js +0 -9
  11. package/bundle/packages/rln/dist/keystore/keystore.js +31 -17
  12. package/bundle/packages/rln/dist/rln.js +57 -167
  13. package/bundle/packages/rln/dist/utils/bytes.js +8 -2
  14. package/bundle/packages/rln/dist/utils/metamask.js +2 -2
  15. package/bundle/packages/rln/dist/zerokit.js +5 -5
  16. package/bundle/packages/rln/node_modules/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/random.js +1 -1
  17. package/bundle/packages/rln/node_modules/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/utils.js +2 -2
  18. package/bundle/packages/rln/node_modules/@noble/hashes/_sha2.js +1 -1
  19. package/bundle/packages/rln/node_modules/@noble/hashes/hmac.js +1 -1
  20. package/bundle/packages/rln/node_modules/@noble/hashes/pbkdf2.js +1 -1
  21. package/bundle/packages/rln/node_modules/@noble/hashes/scrypt.js +1 -1
  22. package/bundle/packages/rln/node_modules/@noble/hashes/sha256.js +1 -1
  23. package/bundle/packages/rln/node_modules/@noble/hashes/sha512.js +1 -1
  24. package/bundle/packages/rln/node_modules/@noble/hashes/utils.js +1 -1
  25. package/dist/.tsbuildinfo +1 -1
  26. package/dist/contract/constants.d.ts +1 -1
  27. package/dist/contract/constants.js +1 -1
  28. package/dist/contract/constants.js.map +1 -1
  29. package/dist/contract/index.d.ts +1 -0
  30. package/dist/contract/index.js +1 -0
  31. package/dist/contract/index.js.map +1 -1
  32. package/dist/contract/{rln_light_contract.d.ts → rln_base_contract.d.ts} +24 -58
  33. package/dist/contract/{rln_light_contract.js → rln_base_contract.js} +187 -184
  34. package/dist/contract/rln_base_contract.js.map +1 -0
  35. package/dist/contract/rln_contract.d.ts +5 -122
  36. package/dist/contract/rln_contract.js +8 -417
  37. package/dist/contract/rln_contract.js.map +1 -1
  38. package/dist/contract/types.d.ts +45 -0
  39. package/dist/contract/types.js +8 -0
  40. package/dist/contract/types.js.map +1 -0
  41. package/dist/create.js +1 -1
  42. package/dist/create.js.map +1 -1
  43. package/dist/credentials_manager.d.ts +44 -0
  44. package/dist/credentials_manager.js +197 -0
  45. package/dist/credentials_manager.js.map +1 -0
  46. package/dist/identity.d.ts +0 -1
  47. package/dist/identity.js +0 -9
  48. package/dist/identity.js.map +1 -1
  49. package/dist/index.d.ts +5 -4
  50. package/dist/index.js +4 -3
  51. package/dist/index.js.map +1 -1
  52. package/dist/keystore/keystore.d.ts +1 -0
  53. package/dist/keystore/keystore.js +31 -17
  54. package/dist/keystore/keystore.js.map +1 -1
  55. package/dist/keystore/types.d.ts +3 -3
  56. package/dist/rln.d.ts +9 -52
  57. package/dist/rln.js +55 -164
  58. package/dist/rln.js.map +1 -1
  59. package/dist/types.d.ts +27 -0
  60. package/dist/types.js +2 -0
  61. package/dist/types.js.map +1 -0
  62. package/dist/utils/bytes.js +8 -2
  63. package/dist/utils/bytes.js.map +1 -1
  64. package/dist/zerokit.d.ts +3 -3
  65. package/dist/zerokit.js +5 -5
  66. package/dist/zerokit.js.map +1 -1
  67. package/package.json +1 -1
  68. package/src/contract/constants.ts +1 -1
  69. package/src/contract/index.ts +1 -0
  70. package/src/contract/{rln_light_contract.ts → rln_base_contract.ts} +306 -323
  71. package/src/contract/rln_contract.ts +9 -663
  72. package/src/contract/types.ts +53 -0
  73. package/src/create.ts +1 -1
  74. package/src/credentials_manager.ts +282 -0
  75. package/src/identity.ts +0 -10
  76. package/src/index.ts +7 -5
  77. package/src/keystore/keystore.ts +57 -31
  78. package/src/keystore/types.ts +3 -3
  79. package/src/rln.ts +68 -259
  80. package/src/types.ts +31 -0
  81. package/src/utils/bytes.ts +10 -2
  82. package/src/zerokit.ts +3 -3
  83. package/bundle/_virtual/__node-resolve_empty.js +0 -6
  84. package/bundle/_virtual/_node-resolve_empty.js +0 -3
  85. package/bundle/_virtual/bn.js +0 -3
  86. package/bundle/_virtual/common.js +0 -3
  87. package/bundle/_virtual/common2.js +0 -3
  88. package/bundle/_virtual/hash.js +0 -3
  89. package/bundle/_virtual/inherits_browser.js +0 -3
  90. package/bundle/_virtual/ripemd.js +0 -3
  91. package/bundle/_virtual/sha.js +0 -3
  92. package/bundle/_virtual/sha3.js +0 -3
  93. package/bundle/_virtual/utils3.js +0 -3
  94. package/bundle/node_modules/@ethersproject/abi/lib.esm/_version.js +0 -3
  95. package/bundle/node_modules/@ethersproject/abi/lib.esm/abi-coder.js +0 -96
  96. package/bundle/node_modules/@ethersproject/abi/lib.esm/coders/abstract-coder.js +0 -148
  97. package/bundle/node_modules/@ethersproject/abi/lib.esm/coders/address.js +0 -26
  98. package/bundle/node_modules/@ethersproject/abi/lib.esm/coders/anonymous.js +0 -20
  99. package/bundle/node_modules/@ethersproject/abi/lib.esm/coders/array.js +0 -210
  100. package/bundle/node_modules/@ethersproject/abi/lib.esm/coders/boolean.js +0 -18
  101. package/bundle/node_modules/@ethersproject/abi/lib.esm/coders/bytes.js +0 -30
  102. package/bundle/node_modules/@ethersproject/abi/lib.esm/coders/fixed-bytes.js +0 -26
  103. package/bundle/node_modules/@ethersproject/abi/lib.esm/coders/null.js +0 -22
  104. package/bundle/node_modules/@ethersproject/abi/lib.esm/coders/number.js +0 -43
  105. package/bundle/node_modules/@ethersproject/abi/lib.esm/coders/string.js +0 -19
  106. package/bundle/node_modules/@ethersproject/abi/lib.esm/coders/tuple.js +0 -58
  107. package/bundle/node_modules/@ethersproject/abi/lib.esm/fragments.js +0 -854
  108. package/bundle/node_modules/@ethersproject/abi/lib.esm/interface.js +0 -609
  109. package/bundle/node_modules/@ethersproject/abstract-provider/lib.esm/_version.js +0 -3
  110. package/bundle/node_modules/@ethersproject/abstract-provider/lib.esm/index.js +0 -66
  111. package/bundle/node_modules/@ethersproject/abstract-signer/lib.esm/_version.js +0 -3
  112. package/bundle/node_modules/@ethersproject/abstract-signer/lib.esm/index.js +0 -302
  113. package/bundle/node_modules/@ethersproject/address/lib.esm/_version.js +0 -3
  114. package/bundle/node_modules/@ethersproject/address/lib.esm/index.js +0 -110
  115. package/bundle/node_modules/@ethersproject/base64/lib.esm/base64.js +0 -20
  116. package/bundle/node_modules/@ethersproject/basex/lib.esm/index.js +0 -120
  117. package/bundle/node_modules/@ethersproject/bignumber/lib.esm/_version.js +0 -3
  118. package/bundle/node_modules/@ethersproject/bignumber/lib.esm/bignumber.js +0 -287
  119. package/bundle/node_modules/@ethersproject/bytes/lib.esm/_version.js +0 -3
  120. package/bundle/node_modules/@ethersproject/bytes/lib.esm/index.js +0 -402
  121. package/bundle/node_modules/@ethersproject/constants/lib.esm/addresses.js +0 -3
  122. package/bundle/node_modules/@ethersproject/constants/lib.esm/bignumbers.js +0 -8
  123. package/bundle/node_modules/@ethersproject/constants/lib.esm/hashes.js +0 -3
  124. package/bundle/node_modules/@ethersproject/contracts/lib.esm/_version.js +0 -3
  125. package/bundle/node_modules/@ethersproject/contracts/lib.esm/index.js +0 -893
  126. package/bundle/node_modules/@ethersproject/hash/lib.esm/_version.js +0 -3
  127. package/bundle/node_modules/@ethersproject/hash/lib.esm/ens-normalize/decoder.js +0 -256
  128. package/bundle/node_modules/@ethersproject/hash/lib.esm/ens-normalize/include.js +0 -36
  129. package/bundle/node_modules/@ethersproject/hash/lib.esm/ens-normalize/lib.js +0 -135
  130. package/bundle/node_modules/@ethersproject/hash/lib.esm/id.js +0 -8
  131. package/bundle/node_modules/@ethersproject/hash/lib.esm/namehash.js +0 -64
  132. package/bundle/node_modules/@ethersproject/hash/lib.esm/typed-data.js +0 -443
  133. package/bundle/node_modules/@ethersproject/keccak256/lib.esm/index.js +0 -8
  134. package/bundle/node_modules/@ethersproject/keccak256/node_modules/js-sha3/src/sha3.js +0 -660
  135. package/bundle/node_modules/@ethersproject/logger/lib.esm/_version.js +0 -3
  136. package/bundle/node_modules/@ethersproject/logger/lib.esm/index.js +0 -352
  137. package/bundle/node_modules/@ethersproject/networks/lib.esm/_version.js +0 -3
  138. package/bundle/node_modules/@ethersproject/networks/lib.esm/index.js +0 -248
  139. package/bundle/node_modules/@ethersproject/properties/lib.esm/_version.js +0 -3
  140. package/bundle/node_modules/@ethersproject/properties/lib.esm/index.js +0 -127
  141. package/bundle/node_modules/@ethersproject/providers/lib.esm/_version.js +0 -3
  142. package/bundle/node_modules/@ethersproject/providers/lib.esm/base-provider.js +0 -2007
  143. package/bundle/node_modules/@ethersproject/providers/lib.esm/formatter.js +0 -422
  144. package/bundle/node_modules/@ethersproject/providers/lib.esm/json-rpc-provider.js +0 -674
  145. package/bundle/node_modules/@ethersproject/providers/lib.esm/web3-provider.js +0 -132
  146. package/bundle/node_modules/@ethersproject/rlp/lib.esm/_version.js +0 -3
  147. package/bundle/node_modules/@ethersproject/rlp/lib.esm/index.js +0 -120
  148. package/bundle/node_modules/@ethersproject/sha2/lib.esm/sha2.js +0 -8
  149. package/bundle/node_modules/@ethersproject/signing-key/lib.esm/_version.js +0 -3
  150. package/bundle/node_modules/@ethersproject/signing-key/lib.esm/elliptic.js +0 -2430
  151. package/bundle/node_modules/@ethersproject/signing-key/lib.esm/index.js +0 -76
  152. package/bundle/node_modules/@ethersproject/strings/lib.esm/_version.js +0 -3
  153. package/bundle/node_modules/@ethersproject/strings/lib.esm/utf8.js +0 -219
  154. package/bundle/node_modules/@ethersproject/transactions/lib.esm/_version.js +0 -3
  155. package/bundle/node_modules/@ethersproject/transactions/lib.esm/index.js +0 -279
  156. package/bundle/node_modules/@ethersproject/web/lib.esm/_version.js +0 -3
  157. package/bundle/node_modules/@ethersproject/web/lib.esm/geturl.js +0 -69
  158. package/bundle/node_modules/@ethersproject/web/lib.esm/index.js +0 -404
  159. package/bundle/node_modules/bech32/index.js +0 -187
  160. package/bundle/node_modules/bn.js/lib/bn.js +0 -3361
  161. package/bundle/node_modules/hash.js/lib/hash/common.js +0 -97
  162. package/bundle/node_modules/hash.js/lib/hash/hmac.js +0 -51
  163. package/bundle/node_modules/hash.js/lib/hash/ripemd.js +0 -152
  164. package/bundle/node_modules/hash.js/lib/hash/sha/1.js +0 -81
  165. package/bundle/node_modules/hash.js/lib/hash/sha/224.js +0 -33
  166. package/bundle/node_modules/hash.js/lib/hash/sha/256.js +0 -113
  167. package/bundle/node_modules/hash.js/lib/hash/sha/384.js +0 -39
  168. package/bundle/node_modules/hash.js/lib/hash/sha/512.js +0 -336
  169. package/bundle/node_modules/hash.js/lib/hash/sha/common.js +0 -53
  170. package/bundle/node_modules/hash.js/lib/hash/sha.js +0 -14
  171. package/bundle/node_modules/hash.js/lib/hash/utils.js +0 -282
  172. package/bundle/node_modules/hash.js/lib/hash.js +0 -33
  173. package/bundle/node_modules/inherits/inherits_browser.js +0 -33
  174. package/bundle/node_modules/minimalistic-assert/index.js +0 -13
  175. package/dist/contract/rln_light_contract.js.map +0 -1
  176. package/dist/rln_light.d.ts +0 -64
  177. package/dist/rln_light.js +0 -144
  178. package/dist/rln_light.js.map +0 -1
  179. package/src/rln_light.ts +0 -235
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 { LINEA_CONTRACT, RLNContract } 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 LINEA_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, rateLimit } = 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: 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
- LINEA_CONTRACT.address;
194
-
195
- if (address === LINEA_CONTRACT.address) {
196
- chainId = LINEA_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()).toString();
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(
@@ -288,38 +81,11 @@ export class RLNInstance {
288
81
  return createRLNEncoder({
289
82
  encoder: createEncoder(options),
290
83
  rlnInstance: this,
291
- index: parseInt(credentials.membership.treeIndex),
84
+ index: credentials.membership.treeIndex,
292
85
  credential: credentials.identity
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.toString();
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 };
@@ -65,8 +65,16 @@ export function buildBigIntFromUint8Array(
65
65
  array: Uint8Array,
66
66
  byteOffset: number = 0
67
67
  ): bigint {
68
- const dataView = new DataView(array.buffer);
69
- return dataView.getBigUint64(byteOffset, true);
68
+ // Convert byte array to BigInt in big-endian format (to match nwaku)
69
+ let result = 0n;
70
+ // Process all 32 bytes (or the available bytes if less)
71
+ const length = Math.min(array.length - byteOffset, 32);
72
+
73
+ for (let i = 0; i < length; i++) {
74
+ result = (result << 8n) | BigInt(array[byteOffset + i]);
75
+ }
76
+
77
+ return result;
70
78
  }
71
79
 
72
80
  /**
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 {
@@ -1,6 +0,0 @@
1
- import * as _nodeResolve_empty from './_node-resolve_empty.js';
2
- import { getAugmentedNamespace } from './_commonjsHelpers.js';
3
-
4
- var require$$0 = /*@__PURE__*/getAugmentedNamespace(_nodeResolve_empty);
5
-
6
- export { require$$0 as default };
@@ -1,3 +0,0 @@
1
- var _nodeResolve_empty = {};
2
-
3
- export { _nodeResolve_empty as default };
@@ -1,3 +0,0 @@
1
- var bn = {exports: {}};
2
-
3
- export { bn as __module };
@@ -1,3 +0,0 @@
1
- var common = {};
2
-
3
- export { common as __exports };
@@ -1,3 +0,0 @@
1
- var common = {};
2
-
3
- export { common as __exports };
@@ -1,3 +0,0 @@
1
- var hash = {};
2
-
3
- export { hash as __exports };
@@ -1,3 +0,0 @@
1
- var inherits_browser = {exports: {}};
2
-
3
- export { inherits_browser as __module };
@@ -1,3 +0,0 @@
1
- var ripemd = {};
2
-
3
- export { ripemd as __exports };
@@ -1,3 +0,0 @@
1
- var sha = {};
2
-
3
- export { sha as __exports };
@@ -1,3 +0,0 @@
1
- var sha3 = {exports: {}};
2
-
3
- export { sha3 as __module };
@@ -1,3 +0,0 @@
1
- var utils = {};
2
-
3
- export { utils as __exports };
@@ -1,3 +0,0 @@
1
- const version = "abi/5.7.0";
2
-
3
- export { version };
@@ -1,96 +0,0 @@
1
- import { arrayify } from '../../bytes/lib.esm/index.js';
2
- import { defineReadOnly } from '../../properties/lib.esm/index.js';
3
- import { Logger } from '../../logger/lib.esm/index.js';
4
- import { version } from './_version.js';
5
- import { Reader, Writer } from './coders/abstract-coder.js';
6
- import { AddressCoder } from './coders/address.js';
7
- import { ArrayCoder } from './coders/array.js';
8
- import { BooleanCoder } from './coders/boolean.js';
9
- import { BytesCoder } from './coders/bytes.js';
10
- import { FixedBytesCoder } from './coders/fixed-bytes.js';
11
- import { NullCoder } from './coders/null.js';
12
- import { NumberCoder } from './coders/number.js';
13
- import { StringCoder } from './coders/string.js';
14
- import { TupleCoder } from './coders/tuple.js';
15
- import { ParamType } from './fragments.js';
16
-
17
- const logger = new Logger(version);
18
- const paramTypeBytes = new RegExp(/^bytes([0-9]*)$/);
19
- const paramTypeNumber = new RegExp(/^(u?int)([0-9]*)$/);
20
- class AbiCoder {
21
- constructor(coerceFunc) {
22
- defineReadOnly(this, "coerceFunc", coerceFunc || null);
23
- }
24
- _getCoder(param) {
25
- switch (param.baseType) {
26
- case "address":
27
- return new AddressCoder(param.name);
28
- case "bool":
29
- return new BooleanCoder(param.name);
30
- case "string":
31
- return new StringCoder(param.name);
32
- case "bytes":
33
- return new BytesCoder(param.name);
34
- case "array":
35
- return new ArrayCoder(this._getCoder(param.arrayChildren), param.arrayLength, param.name);
36
- case "tuple":
37
- return new TupleCoder((param.components || []).map((component) => {
38
- return this._getCoder(component);
39
- }), param.name);
40
- case "":
41
- return new NullCoder(param.name);
42
- }
43
- // u?int[0-9]*
44
- let match = param.type.match(paramTypeNumber);
45
- if (match) {
46
- let size = parseInt(match[2] || "256");
47
- if (size === 0 || size > 256 || (size % 8) !== 0) {
48
- logger.throwArgumentError("invalid " + match[1] + " bit length", "param", param);
49
- }
50
- return new NumberCoder(size / 8, (match[1] === "int"), param.name);
51
- }
52
- // bytes[0-9]+
53
- match = param.type.match(paramTypeBytes);
54
- if (match) {
55
- let size = parseInt(match[1]);
56
- if (size === 0 || size > 32) {
57
- logger.throwArgumentError("invalid bytes length", "param", param);
58
- }
59
- return new FixedBytesCoder(size, param.name);
60
- }
61
- return logger.throwArgumentError("invalid type", "type", param.type);
62
- }
63
- _getWordSize() { return 32; }
64
- _getReader(data, allowLoose) {
65
- return new Reader(data, this._getWordSize(), this.coerceFunc, allowLoose);
66
- }
67
- _getWriter() {
68
- return new Writer(this._getWordSize());
69
- }
70
- getDefaultValue(types) {
71
- const coders = types.map((type) => this._getCoder(ParamType.from(type)));
72
- const coder = new TupleCoder(coders, "_");
73
- return coder.defaultValue();
74
- }
75
- encode(types, values) {
76
- if (types.length !== values.length) {
77
- logger.throwError("types/values length mismatch", Logger.errors.INVALID_ARGUMENT, {
78
- count: { types: types.length, values: values.length },
79
- value: { types: types, values: values }
80
- });
81
- }
82
- const coders = types.map((type) => this._getCoder(ParamType.from(type)));
83
- const coder = (new TupleCoder(coders, "_"));
84
- const writer = this._getWriter();
85
- coder.encode(writer, values);
86
- return writer.data;
87
- }
88
- decode(types, data, loose) {
89
- const coders = types.map((type) => this._getCoder(ParamType.from(type)));
90
- const coder = new TupleCoder(coders, "_");
91
- return coder.decode(this._getReader(arrayify(data), loose));
92
- }
93
- }
94
- const defaultAbiCoder = new AbiCoder();
95
-
96
- export { AbiCoder, defaultAbiCoder };