@waku/rln 0.1.1-fa49e29 → 0.1.2-86d4f56

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.
@@ -1,7 +1,10 @@
1
+ import { hexToBytes } from "@waku/utils/bytes";
1
2
  import { ethers } from "ethers";
2
3
 
4
+ import { zeroPadLE } from "./byte_utils.js";
3
5
  import { RLN_REGISTRY_ABI, RLN_STORAGE_ABI } from "./constants.js";
4
- import { IdentityCredential, RLNInstance } from "./rln.js";
6
+ import type { DecryptedCredentials } from "./keystore/index.js";
7
+ import { type IdentityCredential, RLNInstance } from "./rln.js";
5
8
  import { MerkleRootTracker } from "./root_tracker.js";
6
9
 
7
10
  type Member = {
@@ -9,10 +12,10 @@ type Member = {
9
12
  index: ethers.BigNumber;
10
13
  };
11
14
 
12
- type Provider = ethers.Signer | ethers.providers.Provider;
15
+ type Signer = ethers.Signer;
13
16
 
14
17
  type RLNContractOptions = {
15
- provider: Provider;
18
+ signer: Signer;
16
19
  registryAddress: string;
17
20
  };
18
21
 
@@ -45,7 +48,7 @@ export class RLNContract {
45
48
  ): Promise<RLNContract> {
46
49
  const rlnContract = new RLNContract(rlnInstance, options);
47
50
 
48
- await rlnContract.initStorageContract(options.provider);
51
+ await rlnContract.initStorageContract(options.signer);
49
52
  await rlnContract.fetchMembers(rlnInstance);
50
53
  rlnContract.subscribeToMembers(rlnInstance);
51
54
 
@@ -54,20 +57,20 @@ export class RLNContract {
54
57
 
55
58
  constructor(
56
59
  rlnInstance: RLNInstance,
57
- { registryAddress, provider }: RLNContractOptions
60
+ { registryAddress, signer }: RLNContractOptions
58
61
  ) {
59
62
  const initialRoot = rlnInstance.getMerkleRoot();
60
63
 
61
64
  this.registryContract = new ethers.Contract(
62
65
  registryAddress,
63
66
  RLN_REGISTRY_ABI,
64
- provider
67
+ signer
65
68
  );
66
69
  this.merkleRootTracker = new MerkleRootTracker(5, initialRoot);
67
70
  }
68
71
 
69
72
  private async initStorageContract(
70
- provider: Provider,
73
+ signer: Signer,
71
74
  options: RLNStorageOptions = {}
72
75
  ): Promise<void> {
73
76
  const storageIndex = options?.storageIndex
@@ -83,13 +86,20 @@ export class RLNContract {
83
86
  this.storageContract = new ethers.Contract(
84
87
  storageAddress,
85
88
  RLN_STORAGE_ABI,
86
- provider
89
+ signer
87
90
  );
88
91
  this._membersFilter = this.storageContract.filters.MemberRegistered();
89
92
 
90
93
  this.deployBlock = await this.storageContract.deployedBlockNumber();
91
94
  }
92
95
 
96
+ public get registry(): ethers.Contract {
97
+ if (!this.registryContract) {
98
+ throw Error("Registry contract was not initialized");
99
+ }
100
+ return this.registryContract as ethers.Contract;
101
+ }
102
+
93
103
  public get contract(): ethers.Contract {
94
104
  if (!this.storageContract) {
95
105
  throw Error("Storage contract was not initialized");
@@ -150,10 +160,10 @@ export class RLNContract {
150
160
  eventsPerBlock.push(evt);
151
161
  toInsertTable.set(evt.blockNumber, eventsPerBlock);
152
162
  }
153
-
154
- this.removeMembers(rlnInstance, toRemoveTable);
155
- this.insertMembers(rlnInstance, toInsertTable);
156
163
  });
164
+
165
+ this.removeMembers(rlnInstance, toRemoveTable);
166
+ this.insertMembers(rlnInstance, toInsertTable);
157
167
  }
158
168
 
159
169
  private insertMembers(
@@ -169,15 +179,11 @@ export class RLNContract {
169
179
  return;
170
180
  }
171
181
 
172
- const idCommitment = ethers.utils.zeroPad(
173
- ethers.utils.arrayify(_idCommitment),
174
- 32
175
- );
182
+ const idCommitment = zeroPadLE(hexToBytes(_idCommitment?._hex), 32);
176
183
  rlnInstance.insertMember(idCommitment);
177
184
  this._members.set(index.toNumber(), {
178
185
  index,
179
- idCommitment:
180
- _idCommitment?._hex || ethers.utils.hexlify(idCommitment),
186
+ idCommitment: _idCommitment?._hex,
181
187
  });
182
188
  });
183
189
 
@@ -209,19 +215,9 @@ export class RLNContract {
209
215
  );
210
216
  }
211
217
 
212
- public async registerWithSignature(
213
- rlnInstance: RLNInstance,
214
- signature: string
215
- ): Promise<Member | undefined> {
216
- const identityCredential =
217
- await rlnInstance.generateSeededIdentityCredential(signature);
218
-
219
- return this.registerWithKey(identityCredential);
220
- }
221
-
222
- public async registerWithKey(
223
- credential: IdentityCredential
224
- ): Promise<Member | undefined> {
218
+ public async registerWithIdentity(
219
+ identity: IdentityCredential
220
+ ): Promise<DecryptedCredentials | undefined> {
225
221
  if (this.storageIndex === undefined) {
226
222
  throw Error(
227
223
  "Cannot register credential, no storage contract index found."
@@ -230,7 +226,7 @@ export class RLNContract {
230
226
  const txRegisterResponse: ethers.ContractTransaction =
231
227
  await this.registryContract["register(uint16,uint256)"](
232
228
  this.storageIndex,
233
- credential.IDCommitmentBigInt,
229
+ identity.IDCommitmentBigInt,
234
230
  { gasLimit: 100000 }
235
231
  );
236
232
  const txRegisterReceipt = await txRegisterResponse.wait();
@@ -247,9 +243,17 @@ export class RLNContract {
247
243
  memberRegistered.data
248
244
  );
249
245
 
246
+ const network = await this.registryContract.provider.getNetwork();
247
+ const address = this.registryContract.address;
248
+ const membershipId = decodedData.index.toNumber();
249
+
250
250
  return {
251
- idCommitment: decodedData.idCommitment,
252
- index: decodedData.index,
251
+ identity,
252
+ membership: {
253
+ address,
254
+ treeIndex: membershipId,
255
+ chainId: network.chainId,
256
+ },
253
257
  };
254
258
  }
255
259