@waku/rln 0.0.2-8a6571f.0 → 0.0.2-9094860.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.
package/src/rln.ts CHANGED
@@ -151,8 +151,6 @@ export class RLNInstance {
151
151
  }
152
152
 
153
153
  public async start(options: StartRLNOptions = {}): Promise<void> {
154
- // eslint-disable-next-line no-console
155
- console.log("starting", options);
156
154
  if (this.started || this.starting) {
157
155
  return;
158
156
  }
@@ -198,13 +196,6 @@ export class RLNInstance {
198
196
  chainId = SEPOLIA_CONTRACT.chainId;
199
197
  }
200
198
 
201
- // eslint-disable-next-line no-console
202
- console.log({
203
- chainId,
204
- address,
205
- SEPOLIA_CONTRACT
206
- });
207
-
208
199
  const signer = options.signer || (await extractMetaMaskSigner());
209
200
  const currentChainId = await signer.getChainId();
210
201
 
@@ -0,0 +1,304 @@
1
+ import { Logger } from "@waku/utils";
2
+ import { ethers } from "ethers";
3
+
4
+ import { RLNContract, SEPOLIA_CONTRACT } from "./contract/index.js";
5
+ import { IdentityCredential } from "./identity.js";
6
+ import { IdentityGenerator } from "./identity_generator.js";
7
+ import { Keystore } from "./keystore/index.js";
8
+ import type {
9
+ DecryptedCredentials,
10
+ EncryptedCredentials
11
+ } from "./keystore/index.js";
12
+ import { KeystoreEntity, Password } from "./keystore/types.js";
13
+ import { extractMetaMaskSigner } from "./utils/index.js";
14
+
15
+ const log = new Logger("waku:rln-light");
16
+
17
+ /**
18
+ * Options for starting the RLNLight instance
19
+ */
20
+ type StartRLNLightOptions = {
21
+ /**
22
+ * If not set - will extract MetaMask account and get signer from it.
23
+ */
24
+ signer?: ethers.Signer;
25
+ /**
26
+ * If not set - will use default SEPOLIA_CONTRACT address.
27
+ */
28
+ address?: string;
29
+ /**
30
+ * Credentials to use for membership registration.
31
+ * If provided used for validating the network chainId and connecting to registry contract.
32
+ */
33
+ credentials?: EncryptedCredentials | DecryptedCredentials;
34
+ /**
35
+ * Rate limit for the member.
36
+ */
37
+ rateLimit?: number;
38
+ };
39
+
40
+ /**
41
+ * Options for registering membership
42
+ */
43
+ type RegisterMembershipOptions =
44
+ | { signature: string }
45
+ | { identity: IdentityCredential };
46
+
47
+ /**
48
+ * Lightweight RLN implementation without Zerokit
49
+ * Only supports Keystore and membership registration
50
+ */
51
+ export class RLNLight {
52
+ private started = false;
53
+ private starting = false;
54
+
55
+ private _contract: undefined | RLNContract;
56
+ private _signer: undefined | ethers.Signer;
57
+
58
+ private keystore = Keystore.create();
59
+ private _credentials: undefined | DecryptedCredentials;
60
+ private identityGenerator = new IdentityGenerator();
61
+
62
+ public get contract(): undefined | RLNContract {
63
+ return this._contract;
64
+ }
65
+
66
+ public get signer(): undefined | ethers.Signer {
67
+ return this._signer;
68
+ }
69
+
70
+ /**
71
+ * Get the current credentials
72
+ */
73
+ public get credentials(): undefined | DecryptedCredentials {
74
+ return this._credentials;
75
+ }
76
+
77
+ /**
78
+ * Start the RLNLight instance
79
+ * @param options Configuration options
80
+ */
81
+ public async start(options: StartRLNLightOptions = {}): Promise<void> {
82
+ if (this.started || this.starting) {
83
+ log.warn("RLNLight already started or starting");
84
+ return;
85
+ }
86
+
87
+ this.starting = true;
88
+
89
+ try {
90
+ // Determine correct options based on credentials, if any
91
+ const keystoreCredentials = options.address
92
+ ? await this.findCredentialsByAddress(options.address)
93
+ : undefined;
94
+
95
+ const startOptions = await this.determineStartOptions(
96
+ options,
97
+ keystoreCredentials
98
+ );
99
+
100
+ // Set the signer
101
+ if (startOptions.signer) {
102
+ this._signer = startOptions.signer;
103
+ } else {
104
+ this._signer = await extractMetaMaskSigner();
105
+ }
106
+
107
+ // Initialize the contract with the correct address format
108
+ const finalAddress =
109
+ typeof startOptions.address === "string"
110
+ ? startOptions.address
111
+ : SEPOLIA_CONTRACT.address;
112
+
113
+ const contractOptions = {
114
+ signer: this._signer,
115
+ address: finalAddress,
116
+ rateLimit: startOptions.rateLimit
117
+ };
118
+
119
+ // We need to create a minimal compatible object with RLNInstance interface
120
+ // but we only need it for contract initialization
121
+ const compatibleRLN = {
122
+ zerokit: {
123
+ getMerkleRoot: () => new Uint8Array(32),
124
+ insertMember: (_idCommitment: Uint8Array) => {},
125
+ insertMembers: (
126
+ _index: number,
127
+ ..._idCommitments: Array<Uint8Array>
128
+ ) => {},
129
+ deleteMember: (_index: number) => {}
130
+ }
131
+ };
132
+
133
+ this._contract = await RLNContract.init(
134
+ // Type assertion needed for compatibility with RLNInstance interface
135
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
136
+ compatibleRLN as any,
137
+ contractOptions
138
+ );
139
+
140
+ this.started = true;
141
+ } catch (error) {
142
+ log.error("Failed to start RLNLight:", error);
143
+ throw error;
144
+ } finally {
145
+ this.starting = false;
146
+ }
147
+ }
148
+
149
+ /**
150
+ * Find credentials in the keystore by contract address
151
+ */
152
+ private async findCredentialsByAddress(
153
+ address: string
154
+ ): Promise<KeystoreEntity | undefined> {
155
+ // Since Keystore doesn't have a direct method to find by address,
156
+ // we need to iterate through all credentials
157
+ const hashes = this.keystore.keys();
158
+
159
+ for (const hash of hashes) {
160
+ try {
161
+ // Try with an empty password - this won't work but lets us check schema
162
+ const credential = await this.keystore.readCredential(
163
+ hash,
164
+ new Uint8Array(0)
165
+ );
166
+ if (credential?.membership.address === address) {
167
+ return credential;
168
+ }
169
+ } catch (e) {
170
+ // Ignore errors, just means we couldn't read this credential
171
+ }
172
+ }
173
+
174
+ return undefined;
175
+ }
176
+
177
+ /**
178
+ * Determine the correct options for starting RLNLight
179
+ */
180
+ private async determineStartOptions(
181
+ options: StartRLNLightOptions,
182
+ credentials: KeystoreEntity | undefined
183
+ ): Promise<StartRLNLightOptions> {
184
+ if (options.credentials) {
185
+ const { credentials: decrypted } =
186
+ await RLNLight.decryptCredentialsIfNeeded(options.credentials);
187
+
188
+ if (decrypted?.membership) {
189
+ this._credentials = decrypted;
190
+ return {
191
+ ...options,
192
+ address: decrypted.membership.address
193
+ };
194
+ }
195
+ } else if (credentials) {
196
+ return {
197
+ ...options,
198
+ address: credentials.membership.address
199
+ };
200
+ }
201
+
202
+ return options;
203
+ }
204
+
205
+ /**
206
+ * Decrypt credentials if they are encrypted
207
+ */
208
+ private static async decryptCredentialsIfNeeded(
209
+ credentials?: EncryptedCredentials | DecryptedCredentials
210
+ ): Promise<{ credentials?: DecryptedCredentials; keystore?: Keystore }> {
211
+ if (!credentials) {
212
+ return {};
213
+ }
214
+
215
+ if ("identity" in credentials) {
216
+ return { credentials };
217
+ } else {
218
+ const keystore = Keystore.create();
219
+ try {
220
+ const decrypted = await keystore.readCredential(
221
+ credentials.id,
222
+ credentials.password
223
+ );
224
+
225
+ if (!decrypted) {
226
+ return {};
227
+ }
228
+
229
+ return {
230
+ credentials: {
231
+ identity: decrypted.identity,
232
+ membership: decrypted.membership
233
+ },
234
+ keystore
235
+ };
236
+ } catch (e) {
237
+ log.error("Failed to decrypt credentials", e);
238
+ return {};
239
+ }
240
+ }
241
+ }
242
+
243
+ /**
244
+ * Register a membership using an identity or signature
245
+ * @param options Options including identity or signature
246
+ * @returns Decrypted credentials if successful
247
+ */
248
+ public async registerMembership(
249
+ options: RegisterMembershipOptions
250
+ ): Promise<undefined | DecryptedCredentials> {
251
+ if (!this.contract) {
252
+ throw Error("RLN Contract is not initialized.");
253
+ }
254
+
255
+ let identity = "identity" in options && options.identity;
256
+
257
+ if ("signature" in options) {
258
+ identity = this.identityGenerator.generateSeededIdentityCredential(
259
+ options.signature
260
+ );
261
+ }
262
+
263
+ if (!identity) {
264
+ throw Error("Missing signature or identity to register membership.");
265
+ }
266
+
267
+ return this.contract.registerWithIdentity(identity);
268
+ }
269
+
270
+ /**
271
+ * Changes credentials in use by relying on provided Keystore
272
+ * @param id Hash of credentials to select from Keystore
273
+ * @param password Password to decrypt credentials from Keystore
274
+ */
275
+ public async useCredentials(id: string, password: Password): Promise<void> {
276
+ const decrypted = await this.keystore.readCredential(id, password);
277
+
278
+ if (!decrypted) {
279
+ throw new Error("Credentials not found or incorrect password");
280
+ }
281
+
282
+ this._credentials = {
283
+ identity: decrypted.identity,
284
+ membership: decrypted.membership
285
+ };
286
+ }
287
+
288
+ /**
289
+ * Generate a new identity credential
290
+ * @returns New identity credential
291
+ */
292
+ public generateIdentityCredential(): IdentityCredential {
293
+ return this.identityGenerator.generateIdentityCredentials();
294
+ }
295
+
296
+ /**
297
+ * Generate a seeded identity credential
298
+ * @param seed Seed string to generate deterministic identity
299
+ * @returns Seeded identity credential
300
+ */
301
+ public generateSeededIdentityCredential(seed: string): IdentityCredential {
302
+ return this.identityGenerator.generateSeededIdentityCredential(seed);
303
+ }
304
+ }