@waku/rln 0.0.2-09108d9.0 → 0.0.2-8a6571f.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/bundle/index.js +2 -1
- package/bundle/packages/rln/dist/contract/abi.js +648 -0
- package/bundle/packages/rln/dist/contract/constants.js +25 -65
- package/bundle/packages/rln/dist/contract/rln_contract.js +291 -71
- package/bundle/packages/rln/dist/message.js +1 -1
- package/bundle/packages/rln/dist/rln.js +42 -20
- package/bundle/packages/rln/dist/zerokit.js +34 -14
- package/dist/.tsbuildinfo +1 -1
- package/dist/contract/abi.d.ts +46 -0
- package/dist/contract/abi.js +647 -0
- package/dist/contract/abi.js.map +1 -0
- package/dist/contract/constants.d.ts +63 -3
- package/dist/contract/constants.js +23 -64
- package/dist/contract/constants.js.map +1 -1
- package/dist/contract/rln_contract.d.ts +98 -17
- package/dist/contract/rln_contract.js +290 -69
- package/dist/contract/rln_contract.js.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/dist/message.js +1 -1
- package/dist/message.js.map +1 -1
- package/dist/rln.d.ts +5 -1
- package/dist/rln.js +42 -19
- package/dist/rln.js.map +1 -1
- package/dist/zerokit.d.ts +10 -6
- package/dist/zerokit.js +34 -14
- package/dist/zerokit.js.map +1 -1
- package/package.json +1 -1
- package/src/contract/abi.ts +646 -0
- package/src/contract/constants.ts +26 -65
- package/src/contract/rln_contract.ts +439 -105
- package/src/index.ts +4 -9
- package/src/message.ts +1 -1
- package/src/rln.ts +63 -21
- package/src/zerokit.ts +74 -15
@@ -1,68 +1,28 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
"error IncompatibleStorage()",
|
4
|
-
"error IncompatibleStorageIndex()",
|
5
|
-
"error NoStorageContractAvailable()",
|
6
|
-
"error StorageAlreadyExists(address storageAddress)",
|
7
|
-
"event AdminChanged(address previousAdmin, address newAdmin)",
|
8
|
-
"event BeaconUpgraded(address indexed beacon)",
|
9
|
-
"event Initialized(uint8 version)",
|
10
|
-
"event NewStorageContract(uint16 index, address storageAddress)",
|
11
|
-
"event OwnershipTransferred(address indexed previousOwner, address indexed newOwner)",
|
12
|
-
"event Upgraded(address indexed implementation)",
|
13
|
-
"function forceProgress()",
|
14
|
-
"function initialize(address _poseidonHasher)",
|
15
|
-
"function newStorage()",
|
16
|
-
"function nextStorageIndex() view returns (uint16)",
|
17
|
-
"function owner() view returns (address)",
|
18
|
-
"function poseidonHasher() view returns (address)",
|
19
|
-
"function proxiableUUID() view returns (bytes32)",
|
20
|
-
"function register(uint16 storageIndex, uint256 commitment)",
|
21
|
-
"function register(uint256[] commitments)",
|
22
|
-
"function register(uint16 storageIndex, uint256[] commitments)",
|
23
|
-
"function registerStorage(address storageAddress)",
|
24
|
-
"function renounceOwnership()",
|
25
|
-
"function storages(uint16) view returns (address)",
|
26
|
-
"function transferOwnership(address newOwner)",
|
27
|
-
"function upgradeTo(address newImplementation)",
|
28
|
-
"function upgradeToAndCall(address newImplementation, bytes data) payable",
|
29
|
-
"function usingStorageIndex() view returns (uint16)"
|
30
|
-
];
|
31
|
-
// ref https://github.com/waku-org/waku-rln-contract/blob/19fded82bca07e7b535b429dc507cfb83f10dfcf/deployments/sepolia/WakuRlnStorage_0.json#L3
|
32
|
-
const RLN_STORAGE_ABI = [
|
33
|
-
"constructor(address _poseidonHasher, uint16 _contractIndex)",
|
34
|
-
"error DuplicateIdCommitment()",
|
35
|
-
"error FullTree()",
|
36
|
-
"error InvalidIdCommitment(uint256 idCommitment)",
|
37
|
-
"error NotImplemented()",
|
38
|
-
"event MemberRegistered(uint256 idCommitment, uint256 index)",
|
39
|
-
"event MemberWithdrawn(uint256 idCommitment, uint256 index)",
|
40
|
-
"event OwnershipTransferred(address indexed previousOwner, address indexed newOwner)",
|
41
|
-
"function DEPTH() view returns (uint256)",
|
42
|
-
"function MEMBERSHIP_DEPOSIT() view returns (uint256)",
|
43
|
-
"function SET_SIZE() view returns (uint256)",
|
44
|
-
"function contractIndex() view returns (uint16)",
|
45
|
-
"function deployedBlockNumber() view returns (uint32)",
|
46
|
-
"function idCommitmentIndex() view returns (uint256)",
|
47
|
-
"function isValidCommitment(uint256 idCommitment) view returns (bool)",
|
48
|
-
"function memberExists(uint256) view returns (bool)",
|
49
|
-
"function members(uint256) view returns (uint256)",
|
50
|
-
"function owner() view returns (address)",
|
51
|
-
"function poseidonHasher() view returns (address)",
|
52
|
-
"function register(uint256[] idCommitments)",
|
53
|
-
"function register(uint256 idCommitment) payable",
|
54
|
-
"function renounceOwnership()",
|
55
|
-
"function slash(uint256 idCommitment, address receiver, uint256[8] proof) pure",
|
56
|
-
"function stakedAmounts(uint256) view returns (uint256)",
|
57
|
-
"function transferOwnership(address newOwner)",
|
58
|
-
"function verifier() view returns (address)",
|
59
|
-
"function withdraw() pure",
|
60
|
-
"function withdrawalBalance(address) view returns (uint256)"
|
61
|
-
];
|
1
|
+
import { RLN_ABI } from './abi.js';
|
2
|
+
|
62
3
|
const SEPOLIA_CONTRACT = {
|
63
|
-
chainId:
|
64
|
-
|
65
|
-
|
4
|
+
chainId: 59141,
|
5
|
+
// Implementation contract: 0xde2260ca49300357d5af4153cda0d18f7b3ea9b3
|
6
|
+
address: "0xb9cd878c90e49f797b4431fbf4fb333108cb90e6",
|
7
|
+
abi: RLN_ABI
|
8
|
+
};
|
9
|
+
/**
|
10
|
+
* Rate limit tiers (messages per epoch)
|
11
|
+
* Each membership can specify a rate limit within these bounds.
|
12
|
+
* @see https://github.com/waku-org/specs/blob/master/standards/core/rln-contract.md#implementation-suggestions
|
13
|
+
*/
|
14
|
+
const RATE_LIMIT_TIERS = {
|
15
|
+
LOW: 20, // Suggested minimum rate - 20 messages per epoch
|
16
|
+
MEDIUM: 200,
|
17
|
+
HIGH: 600 // Suggested maximum rate - 600 messages per epoch
|
18
|
+
};
|
19
|
+
// Global rate limit parameters
|
20
|
+
const RATE_LIMIT_PARAMS = {
|
21
|
+
MIN_RATE: RATE_LIMIT_TIERS.LOW,
|
22
|
+
MAX_RATE: RATE_LIMIT_TIERS.HIGH,
|
23
|
+
MAX_TOTAL_RATE: 160_000, // Maximum total rate limit across all memberships
|
24
|
+
EPOCH_LENGTH: 600 // Epoch length in seconds (10 minutes)
|
66
25
|
};
|
26
|
+
const DEFAULT_RATE_LIMIT = RATE_LIMIT_PARAMS.MAX_RATE;
|
67
27
|
|
68
|
-
export {
|
28
|
+
export { DEFAULT_RATE_LIMIT, RATE_LIMIT_PARAMS, RATE_LIMIT_TIERS, SEPOLIA_CONTRACT };
|
@@ -5,56 +5,119 @@ import { hexToBytes } from '../../../utils/dist/bytes/index.js';
|
|
5
5
|
import { Logger } from '../../../utils/dist/logger/index.js';
|
6
6
|
import { MerkleRootTracker } from '../root_tracker.js';
|
7
7
|
import { zeroPadLE } from '../utils/bytes.js';
|
8
|
-
import '
|
9
|
-
import {
|
8
|
+
import { RLN_ABI } from './abi.js';
|
9
|
+
import { RATE_LIMIT_PARAMS, DEFAULT_RATE_LIMIT } from './constants.js';
|
10
10
|
import { Contract } from '../../../../node_modules/@ethersproject/contracts/lib.esm/index.js';
|
11
|
-
import {
|
11
|
+
import { BigNumber } from '../../../../node_modules/@ethersproject/bignumber/lib.esm/bignumber.js';
|
12
12
|
|
13
13
|
const log = new Logger("waku:rln:contract");
|
14
|
+
var MembershipState;
|
15
|
+
(function (MembershipState) {
|
16
|
+
MembershipState["Active"] = "Active";
|
17
|
+
MembershipState["GracePeriod"] = "GracePeriod";
|
18
|
+
MembershipState["Expired"] = "Expired";
|
19
|
+
MembershipState["ErasedAwaitsWithdrawal"] = "ErasedAwaitsWithdrawal";
|
20
|
+
})(MembershipState || (MembershipState = {}));
|
14
21
|
class RLNContract {
|
15
|
-
|
22
|
+
contract;
|
16
23
|
merkleRootTracker;
|
17
24
|
deployBlock;
|
18
|
-
|
19
|
-
storageContract;
|
20
|
-
_membersFilter;
|
25
|
+
rateLimit;
|
21
26
|
_members = new Map();
|
27
|
+
_membersFilter;
|
28
|
+
_membersRemovedFilter;
|
29
|
+
/**
|
30
|
+
* Asynchronous initializer for RLNContract.
|
31
|
+
* Allows injecting a mocked contract for testing purposes.
|
32
|
+
*/
|
22
33
|
static async init(rlnInstance, options) {
|
23
34
|
const rlnContract = new RLNContract(rlnInstance, options);
|
24
|
-
await rlnContract.initStorageContract(options.signer);
|
25
35
|
await rlnContract.fetchMembers(rlnInstance);
|
26
36
|
rlnContract.subscribeToMembers(rlnInstance);
|
27
37
|
return rlnContract;
|
28
38
|
}
|
29
|
-
constructor(rlnInstance,
|
39
|
+
constructor(rlnInstance, options) {
|
40
|
+
const { address, signer, rateLimit = DEFAULT_RATE_LIMIT, contract } = options;
|
41
|
+
if (rateLimit < RATE_LIMIT_PARAMS.MIN_RATE ||
|
42
|
+
rateLimit > RATE_LIMIT_PARAMS.MAX_RATE) {
|
43
|
+
throw new Error(`Rate limit must be between ${RATE_LIMIT_PARAMS.MIN_RATE} and ${RATE_LIMIT_PARAMS.MAX_RATE} messages per epoch`);
|
44
|
+
}
|
45
|
+
this.rateLimit = rateLimit;
|
30
46
|
const initialRoot = rlnInstance.zerokit.getMerkleRoot();
|
31
|
-
|
47
|
+
// Use the injected contract if provided; otherwise, instantiate a new one.
|
48
|
+
this.contract = contract || new Contract(address, RLN_ABI, signer);
|
32
49
|
this.merkleRootTracker = new MerkleRootTracker(5, initialRoot);
|
50
|
+
// Initialize event filters for MembershipRegistered and MembershipErased
|
51
|
+
this._membersFilter = this.contract.filters.MembershipRegistered();
|
52
|
+
this._membersRemovedFilter = this.contract.filters.MembershipErased();
|
33
53
|
}
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
if (!storageAddress || storageAddress === AddressZero) {
|
40
|
-
throw Error("No RLN Storage initialized on registry contract.");
|
41
|
-
}
|
42
|
-
this.storageIndex = storageIndex;
|
43
|
-
this.storageContract = new Contract(storageAddress, RLN_STORAGE_ABI, signer);
|
44
|
-
this._membersFilter = this.storageContract.filters.MemberRegistered();
|
45
|
-
this.deployBlock = await this.storageContract.deployedBlockNumber();
|
54
|
+
/**
|
55
|
+
* Gets the current rate limit for this contract instance
|
56
|
+
*/
|
57
|
+
getRateLimit() {
|
58
|
+
return this.rateLimit;
|
46
59
|
}
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
return this.
|
60
|
+
/**
|
61
|
+
* Gets the contract address
|
62
|
+
*/
|
63
|
+
get address() {
|
64
|
+
return this.contract.address;
|
52
65
|
}
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
return this.
|
66
|
+
/**
|
67
|
+
* Gets the contract provider
|
68
|
+
*/
|
69
|
+
get provider() {
|
70
|
+
return this.contract.provider;
|
71
|
+
}
|
72
|
+
/**
|
73
|
+
* Gets the minimum allowed rate limit from the contract
|
74
|
+
* @returns Promise<number> The minimum rate limit in messages per epoch
|
75
|
+
*/
|
76
|
+
async getMinRateLimit() {
|
77
|
+
const minRate = await this.contract.minMembershipRateLimit();
|
78
|
+
return minRate.toNumber();
|
79
|
+
}
|
80
|
+
/**
|
81
|
+
* Gets the maximum allowed rate limit from the contract
|
82
|
+
* @returns Promise<number> The maximum rate limit in messages per epoch
|
83
|
+
*/
|
84
|
+
async getMaxRateLimit() {
|
85
|
+
const maxRate = await this.contract.maxMembershipRateLimit();
|
86
|
+
return maxRate.toNumber();
|
87
|
+
}
|
88
|
+
/**
|
89
|
+
* Gets the maximum total rate limit across all memberships
|
90
|
+
* @returns Promise<number> The maximum total rate limit in messages per epoch
|
91
|
+
*/
|
92
|
+
async getMaxTotalRateLimit() {
|
93
|
+
const maxTotalRate = await this.contract.maxTotalRateLimit();
|
94
|
+
return maxTotalRate.toNumber();
|
95
|
+
}
|
96
|
+
/**
|
97
|
+
* Gets the current total rate limit usage across all memberships
|
98
|
+
* @returns Promise<number> The current total rate limit usage in messages per epoch
|
99
|
+
*/
|
100
|
+
async getCurrentTotalRateLimit() {
|
101
|
+
const currentTotal = await this.contract.currentTotalRateLimit();
|
102
|
+
return currentTotal.toNumber();
|
103
|
+
}
|
104
|
+
/**
|
105
|
+
* Gets the remaining available total rate limit that can be allocated
|
106
|
+
* @returns Promise<number> The remaining rate limit that can be allocated
|
107
|
+
*/
|
108
|
+
async getRemainingTotalRateLimit() {
|
109
|
+
const [maxTotal, currentTotal] = await Promise.all([
|
110
|
+
this.contract.maxTotalRateLimit(),
|
111
|
+
this.contract.currentTotalRateLimit()
|
112
|
+
]);
|
113
|
+
return maxTotal.sub(currentTotal).toNumber();
|
114
|
+
}
|
115
|
+
/**
|
116
|
+
* Updates the rate limit for future registrations
|
117
|
+
* @param newRateLimit The new rate limit to use
|
118
|
+
*/
|
119
|
+
async setRateLimit(newRateLimit) {
|
120
|
+
this.rateLimit = newRateLimit;
|
58
121
|
}
|
59
122
|
get members() {
|
60
123
|
const sortedMembers = Array.from(this._members.values()).sort((left, right) => left.index.toNumber() - right.index.toNumber());
|
@@ -66,13 +129,25 @@ class RLNContract {
|
|
66
129
|
}
|
67
130
|
return this._membersFilter;
|
68
131
|
}
|
132
|
+
get membersRemovedFilter() {
|
133
|
+
if (!this._membersRemovedFilter) {
|
134
|
+
throw Error("MembersErased filter was not initialized.");
|
135
|
+
}
|
136
|
+
return this._membersRemovedFilter;
|
137
|
+
}
|
69
138
|
async fetchMembers(rlnInstance, options = {}) {
|
70
139
|
const registeredMemberEvents = await queryFilter(this.contract, {
|
71
140
|
fromBlock: this.deployBlock,
|
72
141
|
...options,
|
73
142
|
membersFilter: this.membersFilter
|
74
143
|
});
|
75
|
-
this.
|
144
|
+
const removedMemberEvents = await queryFilter(this.contract, {
|
145
|
+
fromBlock: this.deployBlock,
|
146
|
+
...options,
|
147
|
+
membersFilter: this.membersRemovedFilter
|
148
|
+
});
|
149
|
+
const events = [...registeredMemberEvents, ...removedMemberEvents];
|
150
|
+
this.processEvents(rlnInstance, events);
|
76
151
|
}
|
77
152
|
processEvents(rlnInstance, events) {
|
78
153
|
const toRemoveTable = new Map();
|
@@ -81,7 +156,7 @@ class RLNContract {
|
|
81
156
|
if (!evt.args) {
|
82
157
|
return;
|
83
158
|
}
|
84
|
-
if (evt.
|
159
|
+
if (evt.event === "MembershipErased") {
|
85
160
|
const index = evt.args.index;
|
86
161
|
const toRemoveVal = toRemoveTable.get(evt.blockNumber);
|
87
162
|
if (toRemoveVal != undefined) {
|
@@ -92,7 +167,7 @@ class RLNContract {
|
|
92
167
|
toRemoveTable.set(evt.blockNumber, [index.toNumber()]);
|
93
168
|
}
|
94
169
|
}
|
95
|
-
else {
|
170
|
+
else if (evt.event === "MembershipRegistered") {
|
96
171
|
let eventsPerBlock = toInsertTable.get(evt.blockNumber);
|
97
172
|
if (eventsPerBlock == undefined) {
|
98
173
|
eventsPerBlock = [];
|
@@ -107,16 +182,18 @@ class RLNContract {
|
|
107
182
|
insertMembers(rlnInstance, toInsert) {
|
108
183
|
toInsert.forEach((events, blockNumber) => {
|
109
184
|
events.forEach((evt) => {
|
110
|
-
|
111
|
-
|
185
|
+
if (!evt.args)
|
186
|
+
return;
|
187
|
+
const _idCommitment = evt.args.idCommitment;
|
188
|
+
const index = evt.args.index;
|
112
189
|
if (!_idCommitment || !index) {
|
113
190
|
return;
|
114
191
|
}
|
115
|
-
const idCommitment = zeroPadLE(hexToBytes(_idCommitment
|
192
|
+
const idCommitment = zeroPadLE(hexToBytes(_idCommitment), 32);
|
116
193
|
rlnInstance.zerokit.insertMember(idCommitment);
|
117
194
|
this._members.set(index.toNumber(), {
|
118
195
|
index,
|
119
|
-
idCommitment: _idCommitment
|
196
|
+
idCommitment: _idCommitment
|
120
197
|
});
|
121
198
|
});
|
122
199
|
const currentRoot = rlnInstance.zerokit.getMerkleRoot();
|
@@ -124,7 +201,7 @@ class RLNContract {
|
|
124
201
|
});
|
125
202
|
}
|
126
203
|
removeMembers(rlnInstance, toRemove) {
|
127
|
-
const removeDescending = new Map([...toRemove].
|
204
|
+
const removeDescending = new Map([...toRemove].reverse());
|
128
205
|
removeDescending.forEach((indexes, blockNumber) => {
|
129
206
|
indexes.forEach((index) => {
|
130
207
|
if (this._members.has(index)) {
|
@@ -136,50 +213,185 @@ class RLNContract {
|
|
136
213
|
});
|
137
214
|
}
|
138
215
|
subscribeToMembers(rlnInstance) {
|
139
|
-
this.contract.on(this.membersFilter, (
|
216
|
+
this.contract.on(this.membersFilter, (_idCommitment, _rateLimit, _index, event) => {
|
217
|
+
this.processEvents(rlnInstance, [event]);
|
218
|
+
});
|
219
|
+
this.contract.on(this.membersRemovedFilter, (_idCommitment, _rateLimit, _index, event) => {
|
220
|
+
this.processEvents(rlnInstance, [event]);
|
221
|
+
});
|
140
222
|
}
|
141
223
|
async registerWithIdentity(identity) {
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
224
|
+
try {
|
225
|
+
log.info(`Registering identity with rate limit: ${this.rateLimit} messages/epoch`);
|
226
|
+
const txRegisterResponse = await this.contract.register(identity.IDCommitmentBigInt, this.rateLimit, [], { gasLimit: 300000 });
|
227
|
+
const txRegisterReceipt = await txRegisterResponse.wait();
|
228
|
+
const memberRegistered = txRegisterReceipt.events?.find((event) => event.event === "MembershipRegistered");
|
229
|
+
if (!memberRegistered || !memberRegistered.args) {
|
230
|
+
log.error("Failed to register membership: No MembershipRegistered event found");
|
231
|
+
return undefined;
|
232
|
+
}
|
233
|
+
const decodedData = {
|
234
|
+
idCommitment: memberRegistered.args.idCommitment,
|
235
|
+
rateLimit: memberRegistered.args.rateLimit,
|
236
|
+
index: memberRegistered.args.index
|
237
|
+
};
|
238
|
+
log.info(`Successfully registered membership with index ${decodedData.index} ` +
|
239
|
+
`and rate limit ${decodedData.rateLimit}`);
|
240
|
+
const network = await this.contract.provider.getNetwork();
|
241
|
+
const address = this.contract.address;
|
242
|
+
const membershipId = decodedData.index.toNumber();
|
243
|
+
return {
|
244
|
+
identity,
|
245
|
+
membership: {
|
246
|
+
address,
|
247
|
+
treeIndex: membershipId,
|
248
|
+
chainId: network.chainId
|
249
|
+
}
|
250
|
+
};
|
251
|
+
}
|
252
|
+
catch (error) {
|
253
|
+
log.error(`Error in registerWithIdentity: ${error.message}`);
|
150
254
|
return undefined;
|
151
255
|
}
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
256
|
+
}
|
257
|
+
/**
|
258
|
+
* Helper method to get remaining messages in current epoch
|
259
|
+
* @param membershipId The ID of the membership to check
|
260
|
+
* @returns number of remaining messages allowed in current epoch
|
261
|
+
*/
|
262
|
+
async getRemainingMessages(membershipId) {
|
263
|
+
try {
|
264
|
+
const [startTime, , rateLimit] = await this.contract.getMembershipInfo(membershipId);
|
265
|
+
// Calculate current epoch
|
266
|
+
const currentTime = Math.floor(Date.now() / 1000);
|
267
|
+
const epochsPassed = Math.floor((currentTime - startTime) / RATE_LIMIT_PARAMS.EPOCH_LENGTH);
|
268
|
+
const currentEpochStart = startTime + epochsPassed * RATE_LIMIT_PARAMS.EPOCH_LENGTH;
|
269
|
+
// Get message count in current epoch using contract's function
|
270
|
+
const messageCount = await this.contract.getMessageCount(membershipId, currentEpochStart);
|
271
|
+
return Math.max(0, rateLimit.sub(messageCount).toNumber());
|
272
|
+
}
|
273
|
+
catch (error) {
|
274
|
+
log.error(`Error getting remaining messages: ${error.message}`);
|
275
|
+
return 0; // Fail safe: assume no messages remaining on error
|
276
|
+
}
|
277
|
+
}
|
278
|
+
async registerWithPermitAndErase(identity, permit, idCommitmentsToErase) {
|
279
|
+
try {
|
280
|
+
log.info(`Registering identity with permit and rate limit: ${this.rateLimit} messages/epoch`);
|
281
|
+
const txRegisterResponse = await this.contract.registerWithPermit(permit.owner, permit.deadline, permit.v, permit.r, permit.s, identity.IDCommitmentBigInt, this.rateLimit, idCommitmentsToErase.map((id) => BigNumber.from(id)));
|
282
|
+
const txRegisterReceipt = await txRegisterResponse.wait();
|
283
|
+
const memberRegistered = txRegisterReceipt.events?.find((event) => event.event === "MembershipRegistered");
|
284
|
+
if (!memberRegistered || !memberRegistered.args) {
|
285
|
+
log.error("Failed to register membership with permit: No MembershipRegistered event found");
|
286
|
+
return undefined;
|
162
287
|
}
|
163
|
-
|
288
|
+
const decodedData = {
|
289
|
+
idCommitment: memberRegistered.args.idCommitment,
|
290
|
+
rateLimit: memberRegistered.args.rateLimit,
|
291
|
+
index: memberRegistered.args.index
|
292
|
+
};
|
293
|
+
log.info(`Successfully registered membership with permit. Index: ${decodedData.index}, ` +
|
294
|
+
`Rate limit: ${decodedData.rateLimit}, Erased ${idCommitmentsToErase.length} commitments`);
|
295
|
+
const network = await this.contract.provider.getNetwork();
|
296
|
+
const address = this.contract.address;
|
297
|
+
const membershipId = decodedData.index.toNumber();
|
298
|
+
return {
|
299
|
+
identity,
|
300
|
+
membership: {
|
301
|
+
address,
|
302
|
+
treeIndex: membershipId,
|
303
|
+
chainId: network.chainId
|
304
|
+
}
|
305
|
+
};
|
306
|
+
}
|
307
|
+
catch (error) {
|
308
|
+
log.error(`Error in registerWithPermitAndErase: ${error.message}`);
|
309
|
+
return undefined;
|
310
|
+
}
|
164
311
|
}
|
165
312
|
roots() {
|
166
313
|
return this.merkleRootTracker.roots();
|
167
314
|
}
|
315
|
+
async withdraw(token, holder) {
|
316
|
+
try {
|
317
|
+
const tx = await this.contract.withdraw(token, { from: holder });
|
318
|
+
await tx.wait();
|
319
|
+
}
|
320
|
+
catch (error) {
|
321
|
+
log.error(`Error in withdraw: ${error.message}`);
|
322
|
+
}
|
323
|
+
}
|
324
|
+
async getMembershipInfo(idCommitment) {
|
325
|
+
try {
|
326
|
+
const [startBlock, endBlock, rateLimit] = await this.contract.getMembershipInfo(idCommitment);
|
327
|
+
const currentBlock = await this.contract.provider.getBlockNumber();
|
328
|
+
let state;
|
329
|
+
if (currentBlock < startBlock) {
|
330
|
+
state = MembershipState.Active;
|
331
|
+
}
|
332
|
+
else if (currentBlock < endBlock) {
|
333
|
+
state = MembershipState.GracePeriod;
|
334
|
+
}
|
335
|
+
else {
|
336
|
+
state = MembershipState.Expired;
|
337
|
+
}
|
338
|
+
const index = await this.getMemberIndex(idCommitment);
|
339
|
+
if (!index)
|
340
|
+
return undefined;
|
341
|
+
return {
|
342
|
+
index,
|
343
|
+
idCommitment,
|
344
|
+
rateLimit: rateLimit.toNumber(),
|
345
|
+
startBlock: startBlock.toNumber(),
|
346
|
+
endBlock: endBlock.toNumber(),
|
347
|
+
state
|
348
|
+
};
|
349
|
+
}
|
350
|
+
catch (error) {
|
351
|
+
return undefined;
|
352
|
+
}
|
353
|
+
}
|
354
|
+
async extendMembership(idCommitment) {
|
355
|
+
return this.contract.extendMemberships([idCommitment]);
|
356
|
+
}
|
357
|
+
async eraseMembership(idCommitment, eraseFromMembershipSet = true) {
|
358
|
+
return this.contract.eraseMemberships([idCommitment], eraseFromMembershipSet);
|
359
|
+
}
|
360
|
+
async registerMembership(idCommitment, rateLimit = DEFAULT_RATE_LIMIT) {
|
361
|
+
if (rateLimit < RATE_LIMIT_PARAMS.MIN_RATE ||
|
362
|
+
rateLimit > RATE_LIMIT_PARAMS.MAX_RATE) {
|
363
|
+
throw new Error(`Rate limit must be between ${RATE_LIMIT_PARAMS.MIN_RATE} and ${RATE_LIMIT_PARAMS.MAX_RATE}`);
|
364
|
+
}
|
365
|
+
return this.contract.register(idCommitment, rateLimit, []);
|
366
|
+
}
|
367
|
+
async getMemberIndex(idCommitment) {
|
368
|
+
try {
|
369
|
+
const events = await this.contract.queryFilter(this.contract.filters.MembershipRegistered(idCommitment));
|
370
|
+
if (events.length === 0)
|
371
|
+
return undefined;
|
372
|
+
// Get the most recent registration event
|
373
|
+
const event = events[events.length - 1];
|
374
|
+
return event.args?.index;
|
375
|
+
}
|
376
|
+
catch (error) {
|
377
|
+
return undefined;
|
378
|
+
}
|
379
|
+
}
|
168
380
|
}
|
169
|
-
//
|
381
|
+
// These values should be tested on other networks
|
170
382
|
const FETCH_CHUNK = 5;
|
171
383
|
const BLOCK_RANGE = 3000;
|
172
384
|
async function queryFilter(contract, options) {
|
173
385
|
const { fromBlock, membersFilter, fetchRange = BLOCK_RANGE, fetchChunks = FETCH_CHUNK } = options;
|
174
|
-
if (
|
386
|
+
if (fromBlock === undefined) {
|
175
387
|
return contract.queryFilter(membersFilter);
|
176
388
|
}
|
177
|
-
if (!contract.
|
178
|
-
throw Error("No provider found on the contract
|
389
|
+
if (!contract.provider) {
|
390
|
+
throw Error("No provider found on the contract.");
|
179
391
|
}
|
180
|
-
const toBlock = await contract.
|
392
|
+
const toBlock = await contract.provider.getBlockNumber();
|
181
393
|
if (toBlock - fromBlock < fetchRange) {
|
182
|
-
return contract.queryFilter(membersFilter);
|
394
|
+
return contract.queryFilter(membersFilter, fromBlock, toBlock);
|
183
395
|
}
|
184
396
|
const events = [];
|
185
397
|
const chunks = splitToChunks(fromBlock, toBlock, fetchRange);
|
@@ -208,11 +420,19 @@ function* takeN(array, size) {
|
|
208
420
|
start += size;
|
209
421
|
}
|
210
422
|
}
|
211
|
-
function ignoreErrors(promise, defaultValue) {
|
212
|
-
|
213
|
-
|
423
|
+
async function ignoreErrors(promise, defaultValue) {
|
424
|
+
try {
|
425
|
+
return await promise;
|
426
|
+
}
|
427
|
+
catch (err) {
|
428
|
+
if (err instanceof Error) {
|
429
|
+
log.info(`Ignoring an error during query: ${err.message}`);
|
430
|
+
}
|
431
|
+
else {
|
432
|
+
log.info(`Ignoring an unknown error during query`);
|
433
|
+
}
|
214
434
|
return defaultValue;
|
215
|
-
}
|
435
|
+
}
|
216
436
|
}
|
217
437
|
|
218
|
-
export { RLNContract };
|
438
|
+
export { MembershipState, RLNContract };
|
@@ -17,7 +17,7 @@ class RlnMessage {
|
|
17
17
|
}
|
18
18
|
verify(roots) {
|
19
19
|
return this.rateLimitProof
|
20
|
-
? this.rlnInstance.zerokit.verifyWithRoots(this.rateLimitProof, toRLNSignal(this.msg.contentTopic, this.msg),
|
20
|
+
? this.rlnInstance.zerokit.verifyWithRoots(this.rateLimitProof, toRLNSignal(this.msg.contentTopic, this.msg), roots) // this.rlnInstance.verifyRLNProof once issue status-im/nwaku#1248 is fixed
|
21
21
|
: undefined;
|
22
22
|
}
|
23
23
|
verifyNoRoot() {
|