@waku/rln 0.0.2-3ab8023.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 +30 -150
  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 -10
  24. package/dist/contract/rln_contract.js +30 -150
  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 +36 -218
  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
package/src/index.ts CHANGED
@@ -1,15 +1,19 @@
1
1
  import { RLNDecoder, RLNEncoder } from "./codec.js";
2
2
  import { RLN_ABI } from "./contract/abi.js";
3
3
  import { RLNContract, SEPOLIA_CONTRACT } from "./contract/index.js";
4
+ import { RLNLightContract } from "./contract/rln_light_contract.js";
4
5
  import { createRLN } from "./create.js";
5
6
  import { IdentityCredential } from "./identity.js";
6
7
  import { Keystore } from "./keystore/index.js";
7
8
  import { Proof } from "./proof.js";
8
9
  import { RLNInstance } from "./rln.js";
10
+ import { RLNLightInstance } from "./rln_light.js";
9
11
  import { MerkleRootTracker } from "./root_tracker.js";
10
12
  import { extractMetaMaskSigner } from "./utils/index.js";
11
13
 
12
14
  export {
15
+ RLNLightInstance,
16
+ RLNLightContract,
13
17
  createRLN,
14
18
  Keystore,
15
19
  RLNInstance,
@@ -23,3 +27,14 @@ export {
23
27
  extractMetaMaskSigner,
24
28
  RLN_ABI
25
29
  };
30
+
31
+ export type {
32
+ DecryptedCredentials,
33
+ EncryptedCredentials,
34
+ Keccak256Hash,
35
+ KeystoreEntity,
36
+ MembershipHash,
37
+ MembershipInfo,
38
+ Password,
39
+ Sha256Hash
40
+ } from "./keystore/types.js";
package/src/rln.ts CHANGED
@@ -160,7 +160,7 @@ export class RLNInstance {
160
160
  try {
161
161
  const { credentials, keystore } =
162
162
  await RLNInstance.decryptCredentialsIfNeeded(options.credentials);
163
- const { signer, address } = await this.determineStartOptions(
163
+ const { signer, address, rateLimit } = await this.determineStartOptions(
164
164
  options,
165
165
  credentials
166
166
  );
@@ -174,7 +174,7 @@ export class RLNInstance {
174
174
  this._contract = await RLNContract.init(this, {
175
175
  address: address!,
176
176
  signer: signer!,
177
- rateLimit: options.rateLimit ?? this.zerokit.getRateLimit
177
+ rateLimit: rateLimit ?? this.zerokit.getRateLimit
178
178
  });
179
179
  this.started = true;
180
180
  } finally {
@@ -254,9 +254,6 @@ export class RLNInstance {
254
254
  );
255
255
  }
256
256
 
257
- // eslint-disable-next-line no-console
258
- console.log("registering membership", identity);
259
-
260
257
  if (!identity) {
261
258
  throw Error("Missing signature or identity to register membership.");
262
259
  }
@@ -0,0 +1,235 @@
1
+ import { hmac } from "@noble/hashes/hmac";
2
+ import { sha256 } from "@noble/hashes/sha256";
3
+ import { Logger } from "@waku/utils";
4
+ import { ethers } from "ethers";
5
+
6
+ import { SEPOLIA_CONTRACT } from "./contract/constants.js";
7
+ import { RLNLightContract } from "./contract/rln_light_contract.js";
8
+ import { IdentityCredential } from "./identity.js";
9
+ import { Keystore } from "./keystore/index.js";
10
+ import type {
11
+ DecryptedCredentials,
12
+ EncryptedCredentials
13
+ } from "./keystore/index.js";
14
+ import { KeystoreEntity, Password } from "./keystore/types.js";
15
+ import {
16
+ buildBigIntFromUint8Array,
17
+ extractMetaMaskSigner
18
+ } from "./utils/index.js";
19
+
20
+ const log = new Logger("waku:rln");
21
+
22
+ /**
23
+ * Create an instance of RLN
24
+ * @returns RLNInstance
25
+ */
26
+ export async function create(): Promise<RLNLightInstance> {
27
+ try {
28
+ return new RLNLightInstance();
29
+ } catch (error) {
30
+ log.error("Failed to initialize RLN:", error);
31
+ throw error;
32
+ }
33
+ }
34
+
35
+ type StartRLNOptions = {
36
+ /**
37
+ * If not set - will extract MetaMask account and get signer from it.
38
+ */
39
+ signer?: ethers.Signer;
40
+ /**
41
+ * If not set - will use default SEPOLIA_CONTRACT address.
42
+ */
43
+ address?: string;
44
+ /**
45
+ * Credentials to use for generating proofs and connecting to the contract and network.
46
+ * If provided used for validating the network chainId and connecting to registry contract.
47
+ */
48
+ credentials?: EncryptedCredentials | DecryptedCredentials;
49
+ /**
50
+ * Rate limit for the member.
51
+ */
52
+ rateLimit?: number;
53
+ };
54
+
55
+ type RegisterMembershipOptions =
56
+ | { signature: string }
57
+ | { identity: IdentityCredential };
58
+
59
+ export class RLNLightInstance {
60
+ private started = false;
61
+ private starting = false;
62
+
63
+ private _contract: undefined | RLNLightContract;
64
+ private _signer: undefined | ethers.Signer;
65
+
66
+ private keystore = Keystore.create();
67
+ private _credentials: undefined | DecryptedCredentials;
68
+
69
+ public constructor() {}
70
+
71
+ public get contract(): undefined | RLNLightContract {
72
+ return this._contract;
73
+ }
74
+
75
+ public get signer(): undefined | ethers.Signer {
76
+ return this._signer;
77
+ }
78
+
79
+ public async start(options: StartRLNOptions = {}): Promise<void> {
80
+ if (this.started || this.starting) {
81
+ return;
82
+ }
83
+
84
+ this.starting = true;
85
+
86
+ try {
87
+ const { credentials, keystore } =
88
+ await RLNLightInstance.decryptCredentialsIfNeeded(options.credentials);
89
+ const { signer, address, rateLimit } = await this.determineStartOptions(
90
+ options,
91
+ credentials
92
+ );
93
+
94
+ if (keystore) {
95
+ this.keystore = keystore;
96
+ }
97
+
98
+ this._credentials = credentials;
99
+ this._signer = signer!;
100
+ this._contract = await RLNLightContract.init({
101
+ address: address!,
102
+ signer: signer!,
103
+ rateLimit: rateLimit
104
+ });
105
+ this.started = true;
106
+ } finally {
107
+ this.starting = false;
108
+ }
109
+ }
110
+
111
+ public get credentials(): DecryptedCredentials | undefined {
112
+ return this._credentials;
113
+ }
114
+
115
+ private async determineStartOptions(
116
+ options: StartRLNOptions,
117
+ credentials: KeystoreEntity | undefined
118
+ ): Promise<StartRLNOptions> {
119
+ let chainId = credentials?.membership.chainId;
120
+ const address =
121
+ credentials?.membership.address ||
122
+ options.address ||
123
+ SEPOLIA_CONTRACT.address;
124
+
125
+ if (address === SEPOLIA_CONTRACT.address) {
126
+ chainId = SEPOLIA_CONTRACT.chainId;
127
+ }
128
+
129
+ const signer = options.signer || (await extractMetaMaskSigner());
130
+ const currentChainId = await signer.getChainId();
131
+
132
+ if (chainId && chainId !== currentChainId) {
133
+ throw Error(
134
+ `Failed to start RLN contract, chain ID of contract is different from current one: contract-${chainId}, current network-${currentChainId}`
135
+ );
136
+ }
137
+
138
+ return {
139
+ signer,
140
+ address
141
+ };
142
+ }
143
+
144
+ private static async decryptCredentialsIfNeeded(
145
+ credentials?: EncryptedCredentials | DecryptedCredentials
146
+ ): Promise<{ credentials?: DecryptedCredentials; keystore?: Keystore }> {
147
+ if (!credentials) {
148
+ return {};
149
+ }
150
+
151
+ if ("identity" in credentials) {
152
+ return { credentials };
153
+ }
154
+
155
+ const keystore = Keystore.fromString(credentials.keystore);
156
+
157
+ if (!keystore) {
158
+ return {};
159
+ }
160
+
161
+ const decryptedCredentials = await keystore.readCredential(
162
+ credentials.id,
163
+ credentials.password
164
+ );
165
+
166
+ return {
167
+ keystore,
168
+ credentials: decryptedCredentials
169
+ };
170
+ }
171
+
172
+ /**
173
+ * Generates an identity credential from a seed string
174
+ * This is a pure implementation that doesn't rely on Zerokit
175
+ * @param seed A string seed to generate the identity from
176
+ * @returns IdentityCredential
177
+ */
178
+ private generateSeededIdentityCredential(seed: string): IdentityCredential {
179
+ // Convert the seed to bytes
180
+ const encoder = new TextEncoder();
181
+ const seedBytes = encoder.encode(seed);
182
+
183
+ // Generate deterministic values using HMAC-SHA256
184
+ // We use different context strings for each component to ensure they're different
185
+ const idTrapdoor = hmac(sha256, seedBytes, encoder.encode("IDTrapdoor"));
186
+ const idNullifier = hmac(sha256, seedBytes, encoder.encode("IDNullifier"));
187
+
188
+ // Generate IDSecretHash as a hash of IDTrapdoor and IDNullifier
189
+ const combinedBytes = new Uint8Array([...idTrapdoor, ...idNullifier]);
190
+ const idSecretHash = sha256(combinedBytes);
191
+
192
+ // Generate IDCommitment as a hash of IDSecretHash
193
+ const idCommitment = sha256(idSecretHash);
194
+
195
+ // Convert IDCommitment to BigInt
196
+ const idCommitmentBigInt = buildBigIntFromUint8Array(idCommitment);
197
+
198
+ return new IdentityCredential(
199
+ idTrapdoor,
200
+ idNullifier,
201
+ idSecretHash,
202
+ idCommitment,
203
+ idCommitmentBigInt
204
+ );
205
+ }
206
+
207
+ public async registerMembership(
208
+ options: RegisterMembershipOptions
209
+ ): Promise<undefined | DecryptedCredentials> {
210
+ if (!this.contract) {
211
+ throw Error("RLN Contract is not initialized.");
212
+ }
213
+
214
+ let identity = "identity" in options && options.identity;
215
+
216
+ if ("signature" in options) {
217
+ identity = this.generateSeededIdentityCredential(options.signature);
218
+ }
219
+
220
+ if (!identity) {
221
+ throw Error("Missing signature or identity to register membership.");
222
+ }
223
+
224
+ return this.contract.registerWithIdentity(identity);
225
+ }
226
+
227
+ /**
228
+ * Changes credentials in use by relying on provided Keystore earlier in rln.start
229
+ * @param id: string, hash of credentials to select from Keystore
230
+ * @param password: string or bytes to use to decrypt credentials from Keystore
231
+ */
232
+ public async useCredentials(id: string, password: Password): Promise<void> {
233
+ this._credentials = await this.keystore?.readCredential(id, password);
234
+ }
235
+ }