@waku/rln 0.1.1-891ee34 → 0.1.1-9b1e818
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/assets/rln_wasm_bg-a503e304.wasm +0 -0
- package/bundle/index.js +284 -152
- package/dist/.tsbuildinfo +1 -1
- package/dist/byte_utils.d.ts +7 -0
- package/dist/byte_utils.js +13 -0
- package/dist/byte_utils.js.map +1 -1
- package/dist/codec.d.ts +5 -3
- package/dist/codec.js +8 -2
- package/dist/codec.js.map +1 -1
- package/dist/constants.d.ts +2 -2
- package/dist/constants.js +62 -9
- package/dist/constants.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/keystore/keystore.d.ts +4 -3
- package/dist/keystore/keystore.js +16 -5
- package/dist/keystore/keystore.js.map +1 -1
- package/dist/keystore/types.d.ts +5 -5
- package/dist/message.d.ts +1 -1
- package/dist/message.js +1 -1
- package/dist/rln.js +2 -2
- package/dist/rln.js.map +1 -1
- package/dist/rln_contract.d.ts +23 -13
- package/dist/rln_contract.js +65 -28
- package/dist/rln_contract.js.map +1 -1
- package/package.json +6 -6
- package/src/byte_utils.ts +14 -0
- package/src/codec.ts +10 -2
- package/src/constants.ts +63 -9
- package/src/index.ts +7 -2
- package/src/message.ts +1 -1
- package/src/rln.ts +2 -2
- package/src/rln_contract.ts +117 -41
- package/bundle/assets/rln_wasm_bg-6f96f821.wasm +0 -0
package/src/codec.ts
CHANGED
@@ -56,6 +56,10 @@ export class RLNEncoder implements IEncoder {
|
|
56
56
|
return proof;
|
57
57
|
}
|
58
58
|
|
59
|
+
get pubsubTopic(): string {
|
60
|
+
return this.encoder.pubsubTopic;
|
61
|
+
}
|
62
|
+
|
59
63
|
get contentTopic(): string {
|
60
64
|
return this.encoder.contentTopic;
|
61
65
|
}
|
@@ -86,6 +90,10 @@ export class RLNDecoder<T extends IDecodedMessage>
|
|
86
90
|
{
|
87
91
|
constructor(private rlnInstance: RLNInstance, private decoder: IDecoder<T>) {}
|
88
92
|
|
93
|
+
get pubsubTopic(): string {
|
94
|
+
return this.decoder.pubsubTopic;
|
95
|
+
}
|
96
|
+
|
89
97
|
get contentTopic(): string {
|
90
98
|
return this.decoder.contentTopic;
|
91
99
|
}
|
@@ -97,11 +105,11 @@ export class RLNDecoder<T extends IDecodedMessage>
|
|
97
105
|
}
|
98
106
|
|
99
107
|
async fromProtoObj(
|
100
|
-
|
108
|
+
pubsubTopic: string,
|
101
109
|
proto: IProtoMessage
|
102
110
|
): Promise<RlnMessage<T> | undefined> {
|
103
111
|
const msg: T | undefined = await this.decoder.fromProtoObj(
|
104
|
-
|
112
|
+
pubsubTopic,
|
105
113
|
proto
|
106
114
|
);
|
107
115
|
if (!msg) return;
|
package/src/constants.ts
CHANGED
@@ -1,14 +1,68 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
"
|
4
|
-
"
|
5
|
-
"
|
6
|
-
"
|
1
|
+
// ref https://github.com/waku-org/waku-rln-contract/blob/19fded82bca07e7b535b429dc507cfb83f10dfcf/deployments/sepolia/WakuRlnRegistry_Implementation.json#L3
|
2
|
+
export const RLN_REGISTRY_ABI = [
|
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
|
+
|
32
|
+
// ref https://github.com/waku-org/waku-rln-contract/blob/19fded82bca07e7b535b429dc507cfb83f10dfcf/deployments/sepolia/WakuRlnStorage_0.json#L3
|
33
|
+
export const RLN_STORAGE_ABI = [
|
34
|
+
"constructor(address _poseidonHasher, uint16 _contractIndex)",
|
35
|
+
"error DuplicateIdCommitment()",
|
36
|
+
"error FullTree()",
|
37
|
+
"error InvalidIdCommitment(uint256 idCommitment)",
|
38
|
+
"error NotImplemented()",
|
39
|
+
"event MemberRegistered(uint256 idCommitment, uint256 index)",
|
40
|
+
"event MemberWithdrawn(uint256 idCommitment, uint256 index)",
|
41
|
+
"event OwnershipTransferred(address indexed previousOwner, address indexed newOwner)",
|
42
|
+
"function DEPTH() view returns (uint256)",
|
43
|
+
"function MEMBERSHIP_DEPOSIT() view returns (uint256)",
|
44
|
+
"function SET_SIZE() view returns (uint256)",
|
45
|
+
"function contractIndex() view returns (uint16)",
|
46
|
+
"function deployedBlockNumber() view returns (uint32)",
|
47
|
+
"function idCommitmentIndex() view returns (uint256)",
|
48
|
+
"function isValidCommitment(uint256 idCommitment) view returns (bool)",
|
49
|
+
"function memberExists(uint256) view returns (bool)",
|
50
|
+
"function members(uint256) view returns (uint256)",
|
51
|
+
"function owner() view returns (address)",
|
52
|
+
"function poseidonHasher() view returns (address)",
|
53
|
+
"function register(uint256[] idCommitments)",
|
54
|
+
"function register(uint256 idCommitment) payable",
|
55
|
+
"function renounceOwnership()",
|
56
|
+
"function slash(uint256 idCommitment, address receiver, uint256[8] proof) pure",
|
57
|
+
"function stakedAmounts(uint256) view returns (uint256)",
|
58
|
+
"function transferOwnership(address newOwner)",
|
59
|
+
"function verifier() view returns (address)",
|
60
|
+
"function withdraw() pure",
|
61
|
+
"function withdrawalBalance(address) view returns (uint256)",
|
7
62
|
];
|
8
63
|
|
9
64
|
export const SEPOLIA_CONTRACT = {
|
10
65
|
chainId: 11155111,
|
11
|
-
|
12
|
-
|
13
|
-
abi: RLN_ABI,
|
66
|
+
address: "0xF471d71E9b1455bBF4b85d475afb9BB0954A29c4",
|
67
|
+
abi: RLN_REGISTRY_ABI,
|
14
68
|
};
|
package/src/index.ts
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
import { RLNDecoder, RLNEncoder } from "./codec.js";
|
2
|
-
import {
|
2
|
+
import {
|
3
|
+
RLN_REGISTRY_ABI,
|
4
|
+
RLN_STORAGE_ABI,
|
5
|
+
SEPOLIA_CONTRACT,
|
6
|
+
} from "./constants.js";
|
3
7
|
import { Keystore } from "./keystore/index.js";
|
4
8
|
import {
|
5
9
|
IdentityCredential,
|
@@ -29,6 +33,7 @@ export {
|
|
29
33
|
RLNDecoder,
|
30
34
|
MerkleRootTracker,
|
31
35
|
RLNContract,
|
32
|
-
|
36
|
+
RLN_STORAGE_ABI,
|
37
|
+
RLN_REGISTRY_ABI,
|
33
38
|
SEPOLIA_CONTRACT,
|
34
39
|
};
|
package/src/message.ts
CHANGED
@@ -14,7 +14,7 @@ export function toRLNSignal(contentTopic: string, msg: IMessage): Uint8Array {
|
|
14
14
|
}
|
15
15
|
|
16
16
|
export class RlnMessage<T extends IDecodedMessage> implements IDecodedMessage {
|
17
|
-
public
|
17
|
+
public pubsubTopic = "";
|
18
18
|
|
19
19
|
constructor(
|
20
20
|
public rlnInstance: RLNInstance,
|
package/src/rln.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
import type { IRateLimitProof } from "@waku/interfaces";
|
2
|
-
import
|
2
|
+
import init from "@waku/zerokit-rln-wasm";
|
3
3
|
import * as zerokitRLN from "@waku/zerokit-rln-wasm";
|
4
4
|
|
5
5
|
import { buildBigIntFromUint8Array, writeUIntLE } from "./byte_utils.js";
|
@@ -48,7 +48,7 @@ async function loadZkey(): Promise<Uint8Array> {
|
|
48
48
|
* @returns RLNInstance
|
49
49
|
*/
|
50
50
|
export async function create(): Promise<RLNInstance> {
|
51
|
-
await init();
|
51
|
+
await (init as any)?.();
|
52
52
|
zerokitRLN.init_panic_hook();
|
53
53
|
const witnessCalculator = await loadWitnessCalculator();
|
54
54
|
const zkey = await loadZkey();
|
package/src/rln_contract.ts
CHANGED
@@ -1,19 +1,29 @@
|
|
1
|
+
import { hexToBytes } from "@waku/utils/bytes";
|
1
2
|
import { ethers } from "ethers";
|
2
3
|
|
3
|
-
import {
|
4
|
+
import { zeroPadLE } from "./byte_utils.js";
|
5
|
+
import { RLN_REGISTRY_ABI, RLN_STORAGE_ABI } from "./constants.js";
|
4
6
|
import { IdentityCredential, RLNInstance } from "./rln.js";
|
5
7
|
import { MerkleRootTracker } from "./root_tracker.js";
|
6
8
|
|
7
9
|
type Member = {
|
8
|
-
|
9
|
-
index:
|
10
|
+
idCommitment: string;
|
11
|
+
index: ethers.BigNumber;
|
10
12
|
};
|
11
13
|
|
12
|
-
type
|
13
|
-
|
14
|
-
|
14
|
+
type Provider = ethers.Signer | ethers.providers.Provider;
|
15
|
+
|
16
|
+
type RLNContractOptions = {
|
17
|
+
provider: Provider;
|
18
|
+
registryAddress: string;
|
19
|
+
};
|
20
|
+
|
21
|
+
type RLNStorageOptions = {
|
22
|
+
storageIndex?: number;
|
15
23
|
};
|
16
24
|
|
25
|
+
type RLNContractInitOptions = RLNContractOptions & RLNStorageOptions;
|
26
|
+
|
17
27
|
type FetchMembersOptions = {
|
18
28
|
fromBlock?: number;
|
19
29
|
fetchRange?: number;
|
@@ -21,18 +31,23 @@ type FetchMembersOptions = {
|
|
21
31
|
};
|
22
32
|
|
23
33
|
export class RLNContract {
|
24
|
-
private
|
25
|
-
private membersFilter: ethers.EventFilter;
|
34
|
+
private registryContract: ethers.Contract;
|
26
35
|
private merkleRootTracker: MerkleRootTracker;
|
27
36
|
|
28
|
-
private
|
37
|
+
private deployBlock: undefined | number;
|
38
|
+
private storageIndex: undefined | number;
|
39
|
+
private storageContract: undefined | ethers.Contract;
|
40
|
+
private _membersFilter: undefined | ethers.EventFilter;
|
41
|
+
|
42
|
+
private _members: Map<number, Member> = new Map();
|
29
43
|
|
30
44
|
public static async init(
|
31
45
|
rlnInstance: RLNInstance,
|
32
|
-
options:
|
46
|
+
options: RLNContractInitOptions
|
33
47
|
): Promise<RLNContract> {
|
34
48
|
const rlnContract = new RLNContract(rlnInstance, options);
|
35
49
|
|
50
|
+
await rlnContract.initStorageContract(options.provider);
|
36
51
|
await rlnContract.fetchMembers(rlnInstance);
|
37
52
|
rlnContract.subscribeToMembers(rlnInstance);
|
38
53
|
|
@@ -41,21 +56,61 @@ export class RLNContract {
|
|
41
56
|
|
42
57
|
constructor(
|
43
58
|
rlnInstance: RLNInstance,
|
44
|
-
{
|
59
|
+
{ registryAddress, provider }: RLNContractOptions
|
45
60
|
) {
|
46
61
|
const initialRoot = rlnInstance.getMerkleRoot();
|
47
62
|
|
48
|
-
this.
|
63
|
+
this.registryContract = new ethers.Contract(
|
64
|
+
registryAddress,
|
65
|
+
RLN_REGISTRY_ABI,
|
66
|
+
provider
|
67
|
+
);
|
49
68
|
this.merkleRootTracker = new MerkleRootTracker(5, initialRoot);
|
50
|
-
|
69
|
+
}
|
70
|
+
|
71
|
+
private async initStorageContract(
|
72
|
+
provider: Provider,
|
73
|
+
options: RLNStorageOptions = {}
|
74
|
+
): Promise<void> {
|
75
|
+
const storageIndex = options?.storageIndex
|
76
|
+
? options.storageIndex
|
77
|
+
: await this.registryContract.usingStorageIndex();
|
78
|
+
const storageAddress = await this.registryContract.storages(storageIndex);
|
79
|
+
|
80
|
+
if (!storageAddress || storageAddress === ethers.constants.AddressZero) {
|
81
|
+
throw Error("No RLN Storage initialized on registry contract.");
|
82
|
+
}
|
83
|
+
|
84
|
+
this.storageIndex = storageIndex;
|
85
|
+
this.storageContract = new ethers.Contract(
|
86
|
+
storageAddress,
|
87
|
+
RLN_STORAGE_ABI,
|
88
|
+
provider
|
89
|
+
);
|
90
|
+
this._membersFilter = this.storageContract.filters.MemberRegistered();
|
91
|
+
|
92
|
+
this.deployBlock = await this.storageContract.deployedBlockNumber();
|
51
93
|
}
|
52
94
|
|
53
95
|
public get contract(): ethers.Contract {
|
54
|
-
|
96
|
+
if (!this.storageContract) {
|
97
|
+
throw Error("Storage contract was not initialized");
|
98
|
+
}
|
99
|
+
return this.storageContract as ethers.Contract;
|
55
100
|
}
|
56
101
|
|
57
102
|
public get members(): Member[] {
|
58
|
-
|
103
|
+
const sortedMembers = Array.from(this._members.values()).sort(
|
104
|
+
(left, right) => left.index.toNumber() - right.index.toNumber()
|
105
|
+
);
|
106
|
+
return sortedMembers;
|
107
|
+
}
|
108
|
+
|
109
|
+
private get membersFilter(): ethers.EventFilter {
|
110
|
+
if (!this._membersFilter) {
|
111
|
+
throw Error("Members filter was not initialized.");
|
112
|
+
}
|
113
|
+
return this._membersFilter as ethers.EventFilter;
|
59
114
|
}
|
60
115
|
|
61
116
|
public async fetchMembers(
|
@@ -63,6 +118,7 @@ export class RLNContract {
|
|
63
118
|
options: FetchMembersOptions = {}
|
64
119
|
): Promise<void> {
|
65
120
|
const registeredMemberEvents = await queryFilter(this.contract, {
|
121
|
+
fromBlock: this.deployBlock,
|
66
122
|
...options,
|
67
123
|
membersFilter: this.membersFilter,
|
68
124
|
});
|
@@ -79,13 +135,13 @@ export class RLNContract {
|
|
79
135
|
}
|
80
136
|
|
81
137
|
if (evt.removed) {
|
82
|
-
const index:
|
138
|
+
const index: ethers.BigNumber = evt.args.index;
|
83
139
|
const toRemoveVal = toRemoveTable.get(evt.blockNumber);
|
84
140
|
if (toRemoveVal != undefined) {
|
85
|
-
toRemoveVal.push(index);
|
141
|
+
toRemoveVal.push(index.toNumber());
|
86
142
|
toRemoveTable.set(evt.blockNumber, toRemoveVal);
|
87
143
|
} else {
|
88
|
-
toRemoveTable.set(evt.blockNumber, [index]);
|
144
|
+
toRemoveTable.set(evt.blockNumber, [index.toNumber()]);
|
89
145
|
}
|
90
146
|
} else {
|
91
147
|
let eventsPerBlock = toInsertTable.get(evt.blockNumber);
|
@@ -96,10 +152,10 @@ export class RLNContract {
|
|
96
152
|
eventsPerBlock.push(evt);
|
97
153
|
toInsertTable.set(evt.blockNumber, eventsPerBlock);
|
98
154
|
}
|
99
|
-
|
100
|
-
this.removeMembers(rlnInstance, toRemoveTable);
|
101
|
-
this.insertMembers(rlnInstance, toInsertTable);
|
102
155
|
});
|
156
|
+
|
157
|
+
this.removeMembers(rlnInstance, toRemoveTable);
|
158
|
+
this.insertMembers(rlnInstance, toInsertTable);
|
103
159
|
}
|
104
160
|
|
105
161
|
private insertMembers(
|
@@ -108,18 +164,19 @@ export class RLNContract {
|
|
108
164
|
): void {
|
109
165
|
toInsert.forEach((events: ethers.Event[], blockNumber: number) => {
|
110
166
|
events.forEach((evt) => {
|
111
|
-
|
167
|
+
const _idCommitment = evt?.args?.idCommitment;
|
168
|
+
const index: ethers.BigNumber = evt?.args?.index;
|
169
|
+
|
170
|
+
if (!_idCommitment || !index) {
|
112
171
|
return;
|
113
172
|
}
|
114
173
|
|
115
|
-
const
|
116
|
-
const index = evt.args.index;
|
117
|
-
const idCommitment = ethers.utils.zeroPad(
|
118
|
-
ethers.utils.arrayify(pubkey),
|
119
|
-
32
|
120
|
-
);
|
174
|
+
const idCommitment = zeroPadLE(hexToBytes(_idCommitment?._hex), 32);
|
121
175
|
rlnInstance.insertMember(idCommitment);
|
122
|
-
this.
|
176
|
+
this._members.set(index.toNumber(), {
|
177
|
+
index,
|
178
|
+
idCommitment: _idCommitment?._hex,
|
179
|
+
});
|
123
180
|
});
|
124
181
|
|
125
182
|
const currentRoot = rlnInstance.getMerkleRoot();
|
@@ -134,9 +191,8 @@ export class RLNContract {
|
|
134
191
|
const removeDescending = new Map([...toRemove].sort().reverse());
|
135
192
|
removeDescending.forEach((indexes: number[], blockNumber: number) => {
|
136
193
|
indexes.forEach((index) => {
|
137
|
-
|
138
|
-
|
139
|
-
this.members.splice(idx, 1);
|
194
|
+
if (this._members.has(index)) {
|
195
|
+
this._members.delete(index);
|
140
196
|
}
|
141
197
|
rlnInstance.deleteMember(index);
|
142
198
|
});
|
@@ -147,14 +203,14 @@ export class RLNContract {
|
|
147
203
|
|
148
204
|
public subscribeToMembers(rlnInstance: RLNInstance): void {
|
149
205
|
this.contract.on(this.membersFilter, (_pubkey, _index, event) =>
|
150
|
-
this.processEvents(rlnInstance, event)
|
206
|
+
this.processEvents(rlnInstance, [event])
|
151
207
|
);
|
152
208
|
}
|
153
209
|
|
154
210
|
public async registerWithSignature(
|
155
211
|
rlnInstance: RLNInstance,
|
156
212
|
signature: string
|
157
|
-
): Promise<
|
213
|
+
): Promise<Member | undefined> {
|
158
214
|
const identityCredential =
|
159
215
|
await rlnInstance.generateSeededIdentityCredential(signature);
|
160
216
|
|
@@ -163,16 +219,36 @@ export class RLNContract {
|
|
163
219
|
|
164
220
|
public async registerWithKey(
|
165
221
|
credential: IdentityCredential
|
166
|
-
): Promise<
|
167
|
-
|
168
|
-
|
222
|
+
): Promise<Member | undefined> {
|
223
|
+
if (this.storageIndex === undefined) {
|
224
|
+
throw Error(
|
225
|
+
"Cannot register credential, no storage contract index found."
|
226
|
+
);
|
227
|
+
}
|
169
228
|
const txRegisterResponse: ethers.ContractTransaction =
|
170
|
-
await this.
|
171
|
-
|
172
|
-
|
229
|
+
await this.registryContract["register(uint16,uint256)"](
|
230
|
+
this.storageIndex,
|
231
|
+
credential.IDCommitmentBigInt,
|
232
|
+
{ gasLimit: 100000 }
|
233
|
+
);
|
173
234
|
const txRegisterReceipt = await txRegisterResponse.wait();
|
174
235
|
|
175
|
-
|
236
|
+
// assumption: register(uint16,uint256) emits one event
|
237
|
+
const memberRegistered = txRegisterReceipt?.events?.[0];
|
238
|
+
|
239
|
+
if (!memberRegistered) {
|
240
|
+
return undefined;
|
241
|
+
}
|
242
|
+
|
243
|
+
const decodedData = this.contract.interface.decodeEventLog(
|
244
|
+
"MemberRegistered",
|
245
|
+
memberRegistered.data
|
246
|
+
);
|
247
|
+
|
248
|
+
return {
|
249
|
+
idCommitment: decodedData.idCommitment,
|
250
|
+
index: decodedData.index,
|
251
|
+
};
|
176
252
|
}
|
177
253
|
|
178
254
|
public roots(): Uint8Array[] {
|
Binary file
|