@waku/rln 0.1.5-76f86de.0 → 0.1.5-9901863.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 -0
- package/bundle/packages/rln/dist/contract/constants.js +1 -1
- package/bundle/packages/rln/dist/contract/rln_base_contract.js +477 -0
- package/bundle/packages/rln/dist/contract/rln_contract.js +9 -419
- package/bundle/packages/rln/dist/contract/types.js +9 -0
- package/bundle/packages/rln/dist/create.js +1 -1
- package/bundle/packages/rln/dist/credentials_manager.js +233 -0
- package/bundle/packages/rln/dist/identity.js +8 -0
- package/bundle/packages/rln/dist/keystore/keystore.js +19 -28
- package/bundle/packages/rln/dist/rln.js +56 -166
- package/bundle/packages/rln/node_modules/@noble/hashes/esm/_assert.js +43 -0
- package/bundle/packages/rln/node_modules/@noble/hashes/esm/_sha2.js +116 -0
- package/bundle/packages/rln/node_modules/@noble/hashes/esm/hmac.js +79 -0
- package/bundle/packages/rln/node_modules/@noble/hashes/esm/sha256.js +126 -0
- package/bundle/packages/rln/node_modules/@noble/hashes/esm/utils.js +43 -0
- package/dist/.tsbuildinfo +1 -1
- package/dist/contract/constants.d.ts +1 -1
- package/dist/contract/constants.js +1 -1
- package/dist/contract/constants.js.map +1 -1
- package/dist/contract/rln_base_contract.d.ts +96 -0
- package/dist/contract/rln_base_contract.js +460 -0
- package/dist/contract/rln_base_contract.js.map +1 -0
- package/dist/contract/rln_contract.d.ts +5 -122
- package/dist/contract/rln_contract.js +8 -417
- package/dist/contract/rln_contract.js.map +1 -1
- package/dist/contract/test-utils.js +1 -1
- package/dist/contract/test-utils.js.map +1 -1
- package/dist/contract/types.d.ts +40 -0
- package/dist/contract/types.js +8 -0
- package/dist/contract/types.js.map +1 -0
- package/dist/create.js +1 -1
- package/dist/create.js.map +1 -1
- package/dist/credentials_manager.d.ts +50 -0
- package/dist/credentials_manager.js +215 -0
- package/dist/credentials_manager.js.map +1 -0
- package/dist/identity.d.ts +1 -0
- package/dist/identity.js +8 -0
- package/dist/identity.js.map +1 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/keystore/keystore.d.ts +0 -1
- package/dist/keystore/keystore.js +19 -28
- package/dist/keystore/keystore.js.map +1 -1
- package/dist/keystore/types.d.ts +1 -1
- package/dist/rln.d.ts +9 -52
- package/dist/rln.js +54 -163
- package/dist/rln.js.map +1 -1
- package/dist/types.d.ts +27 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +1 -1
- package/src/contract/constants.ts +1 -1
- package/src/contract/rln_base_contract.ts +707 -0
- package/src/contract/rln_contract.ts +9 -663
- package/src/contract/test-utils.ts +1 -1
- package/src/contract/types.ts +48 -0
- package/src/create.ts +1 -1
- package/src/credentials_manager.ts +306 -0
- package/src/identity.ts +9 -0
- package/src/index.ts +4 -0
- package/src/keystore/keystore.ts +31 -46
- package/src/keystore/types.ts +1 -1
- package/src/rln.ts +67 -259
- package/src/types.ts +31 -0
package/src/rln.ts
CHANGED
@@ -7,7 +7,6 @@ import type {
|
|
7
7
|
import { Logger } from "@waku/utils";
|
8
8
|
import init from "@waku/zerokit-rln-wasm";
|
9
9
|
import * as zerokitRLN from "@waku/zerokit-rln-wasm";
|
10
|
-
import { ethers } from "ethers";
|
11
10
|
|
12
11
|
import {
|
13
12
|
createRLNDecoder,
|
@@ -16,258 +15,51 @@ import {
|
|
16
15
|
type RLNEncoder
|
17
16
|
} from "./codec.js";
|
18
17
|
import { DEFAULT_RATE_LIMIT } from "./contract/constants.js";
|
19
|
-
import {
|
20
|
-
import { IdentityCredential } from "./identity.js";
|
21
|
-
import { Keystore } from "./keystore/index.js";
|
18
|
+
import { RLNCredentialsManager } from "./credentials_manager.js";
|
22
19
|
import type {
|
23
20
|
DecryptedCredentials,
|
24
21
|
EncryptedCredentials
|
25
22
|
} from "./keystore/index.js";
|
26
|
-
import { KeystoreEntity, Password } from "./keystore/types.js";
|
27
23
|
import verificationKey from "./resources/verification_key";
|
28
24
|
import * as wc from "./resources/witness_calculator";
|
29
25
|
import { WitnessCalculator } from "./resources/witness_calculator";
|
30
|
-
import { extractMetaMaskSigner } from "./utils/index.js";
|
31
26
|
import { Zerokit } from "./zerokit.js";
|
32
27
|
|
33
28
|
const log = new Logger("waku:rln");
|
34
29
|
|
35
|
-
async function loadWitnessCalculator(): Promise<WitnessCalculator> {
|
36
|
-
try {
|
37
|
-
const url = new URL("./resources/rln.wasm", import.meta.url);
|
38
|
-
const response = await fetch(url);
|
39
|
-
|
40
|
-
if (!response.ok) {
|
41
|
-
throw new Error(
|
42
|
-
`Failed to fetch witness calculator: ${response.status} ${response.statusText}`
|
43
|
-
);
|
44
|
-
}
|
45
|
-
|
46
|
-
return await wc.builder(
|
47
|
-
new Uint8Array(await response.arrayBuffer()),
|
48
|
-
false
|
49
|
-
);
|
50
|
-
} catch (error) {
|
51
|
-
log.error("Error loading witness calculator:", error);
|
52
|
-
throw new Error(
|
53
|
-
`Failed to load witness calculator: ${error instanceof Error ? error.message : String(error)}`
|
54
|
-
);
|
55
|
-
}
|
56
|
-
}
|
57
|
-
|
58
|
-
async function loadZkey(): Promise<Uint8Array> {
|
59
|
-
try {
|
60
|
-
const url = new URL("./resources/rln_final.zkey", import.meta.url);
|
61
|
-
const response = await fetch(url);
|
62
|
-
|
63
|
-
if (!response.ok) {
|
64
|
-
throw new Error(
|
65
|
-
`Failed to fetch zkey: ${response.status} ${response.statusText}`
|
66
|
-
);
|
67
|
-
}
|
68
|
-
|
69
|
-
return new Uint8Array(await response.arrayBuffer());
|
70
|
-
} catch (error) {
|
71
|
-
log.error("Error loading zkey:", error);
|
72
|
-
throw new Error(
|
73
|
-
`Failed to load zkey: ${error instanceof Error ? error.message : String(error)}`
|
74
|
-
);
|
75
|
-
}
|
76
|
-
}
|
77
|
-
|
78
|
-
/**
|
79
|
-
* Create an instance of RLN
|
80
|
-
* @returns RLNInstance
|
81
|
-
*/
|
82
|
-
export async function create(): Promise<RLNInstance> {
|
83
|
-
try {
|
84
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
85
|
-
await (init as any)?.();
|
86
|
-
zerokitRLN.init_panic_hook();
|
87
|
-
|
88
|
-
const witnessCalculator = await loadWitnessCalculator();
|
89
|
-
const zkey = await loadZkey();
|
90
|
-
|
91
|
-
const stringEncoder = new TextEncoder();
|
92
|
-
const vkey = stringEncoder.encode(JSON.stringify(verificationKey));
|
93
|
-
|
94
|
-
const DEPTH = 20;
|
95
|
-
const zkRLN = zerokitRLN.newRLN(DEPTH, zkey, vkey);
|
96
|
-
const zerokit = new Zerokit(zkRLN, witnessCalculator, DEFAULT_RATE_LIMIT);
|
97
|
-
|
98
|
-
return new RLNInstance(zerokit);
|
99
|
-
} catch (error) {
|
100
|
-
log.error("Failed to initialize RLN:", error);
|
101
|
-
throw error;
|
102
|
-
}
|
103
|
-
}
|
104
|
-
|
105
|
-
type StartRLNOptions = {
|
106
|
-
/**
|
107
|
-
* If not set - will extract MetaMask account and get signer from it.
|
108
|
-
*/
|
109
|
-
signer?: ethers.Signer;
|
110
|
-
/**
|
111
|
-
* If not set - will use default LINEA_CONTRACT address.
|
112
|
-
*/
|
113
|
-
address?: string;
|
114
|
-
/**
|
115
|
-
* Credentials to use for generating proofs and connecting to the contract and network.
|
116
|
-
* If provided used for validating the network chainId and connecting to registry contract.
|
117
|
-
*/
|
118
|
-
credentials?: EncryptedCredentials | DecryptedCredentials;
|
119
|
-
/**
|
120
|
-
* Rate limit for the member.
|
121
|
-
*/
|
122
|
-
rateLimit?: number;
|
123
|
-
};
|
124
|
-
|
125
|
-
type RegisterMembershipOptions =
|
126
|
-
| { signature: string }
|
127
|
-
| { identity: IdentityCredential };
|
128
|
-
|
129
30
|
type WakuRLNEncoderOptions = WakuEncoderOptions & {
|
130
31
|
credentials: EncryptedCredentials | DecryptedCredentials;
|
131
32
|
};
|
132
33
|
|
133
|
-
export class RLNInstance {
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
private keystore = Keystore.create();
|
141
|
-
private _credentials: undefined | DecryptedCredentials;
|
142
|
-
|
143
|
-
public constructor(public zerokit: Zerokit) {}
|
144
|
-
|
145
|
-
public get contract(): undefined | RLNContract {
|
146
|
-
return this._contract;
|
147
|
-
}
|
148
|
-
|
149
|
-
public get signer(): undefined | ethers.Signer {
|
150
|
-
return this._signer;
|
151
|
-
}
|
152
|
-
|
153
|
-
public async start(options: StartRLNOptions = {}): Promise<void> {
|
154
|
-
if (this.started || this.starting) {
|
155
|
-
return;
|
156
|
-
}
|
157
|
-
|
158
|
-
this.starting = true;
|
159
|
-
|
34
|
+
export class RLNInstance extends RLNCredentialsManager {
|
35
|
+
/**
|
36
|
+
* Create an instance of RLN
|
37
|
+
* @returns RLNInstance
|
38
|
+
*/
|
39
|
+
public static async create(): Promise<RLNInstance> {
|
160
40
|
try {
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
options,
|
165
|
-
credentials
|
166
|
-
);
|
167
|
-
|
168
|
-
if (keystore) {
|
169
|
-
this.keystore = keystore;
|
170
|
-
}
|
41
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
42
|
+
await (init as any)?.();
|
43
|
+
zerokitRLN.init_panic_hook();
|
171
44
|
|
172
|
-
|
173
|
-
|
174
|
-
this._contract = await RLNContract.init(this, {
|
175
|
-
address: address!,
|
176
|
-
signer: signer!,
|
177
|
-
rateLimit: options.rateLimit ?? this.zerokit.getRateLimit
|
178
|
-
});
|
179
|
-
this.started = true;
|
180
|
-
} finally {
|
181
|
-
this.starting = false;
|
182
|
-
}
|
183
|
-
}
|
45
|
+
const witnessCalculator = await RLNInstance.loadWitnessCalculator();
|
46
|
+
const zkey = await RLNInstance.loadZkey();
|
184
47
|
|
185
|
-
|
186
|
-
|
187
|
-
credentials: KeystoreEntity | undefined
|
188
|
-
): Promise<StartRLNOptions> {
|
189
|
-
let chainId = credentials?.membership.chainId;
|
190
|
-
const address =
|
191
|
-
credentials?.membership.address ||
|
192
|
-
options.address ||
|
193
|
-
LINEA_CONTRACT.address;
|
194
|
-
|
195
|
-
if (address === LINEA_CONTRACT.address) {
|
196
|
-
chainId = LINEA_CONTRACT.chainId;
|
197
|
-
}
|
48
|
+
const stringEncoder = new TextEncoder();
|
49
|
+
const vkey = stringEncoder.encode(JSON.stringify(verificationKey));
|
198
50
|
|
199
|
-
|
200
|
-
|
51
|
+
const DEPTH = 20;
|
52
|
+
const zkRLN = zerokitRLN.newRLN(DEPTH, zkey, vkey);
|
53
|
+
const zerokit = new Zerokit(zkRLN, witnessCalculator, DEFAULT_RATE_LIMIT);
|
201
54
|
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
55
|
+
return new RLNInstance(zerokit);
|
56
|
+
} catch (error) {
|
57
|
+
log.error("Failed to initialize RLN:", error);
|
58
|
+
throw error;
|
206
59
|
}
|
207
|
-
|
208
|
-
return {
|
209
|
-
signer,
|
210
|
-
address
|
211
|
-
};
|
212
60
|
}
|
213
|
-
|
214
|
-
|
215
|
-
credentials?: EncryptedCredentials | DecryptedCredentials
|
216
|
-
): Promise<{ credentials?: DecryptedCredentials; keystore?: Keystore }> {
|
217
|
-
if (!credentials) {
|
218
|
-
return {};
|
219
|
-
}
|
220
|
-
|
221
|
-
if ("identity" in credentials) {
|
222
|
-
return { credentials };
|
223
|
-
}
|
224
|
-
|
225
|
-
const keystore = Keystore.fromString(credentials.keystore);
|
226
|
-
|
227
|
-
if (!keystore) {
|
228
|
-
return {};
|
229
|
-
}
|
230
|
-
|
231
|
-
const decryptedCredentials = await keystore.readCredential(
|
232
|
-
credentials.id,
|
233
|
-
credentials.password
|
234
|
-
);
|
235
|
-
|
236
|
-
return {
|
237
|
-
keystore,
|
238
|
-
credentials: decryptedCredentials
|
239
|
-
};
|
240
|
-
}
|
241
|
-
|
242
|
-
public async registerMembership(
|
243
|
-
options: RegisterMembershipOptions
|
244
|
-
): Promise<undefined | DecryptedCredentials> {
|
245
|
-
if (!this.contract) {
|
246
|
-
throw Error("RLN Contract is not initialized.");
|
247
|
-
}
|
248
|
-
|
249
|
-
let identity = "identity" in options && options.identity;
|
250
|
-
|
251
|
-
if ("signature" in options) {
|
252
|
-
identity = this.zerokit.generateSeededIdentityCredential(
|
253
|
-
options.signature
|
254
|
-
);
|
255
|
-
}
|
256
|
-
|
257
|
-
if (!identity) {
|
258
|
-
throw Error("Missing signature or identity to register membership.");
|
259
|
-
}
|
260
|
-
|
261
|
-
return this.contract.registerWithIdentity(identity);
|
262
|
-
}
|
263
|
-
|
264
|
-
/**
|
265
|
-
* Changes credentials in use by relying on provided Keystore earlier in rln.start
|
266
|
-
* @param id: string, hash of credentials to select from Keystore
|
267
|
-
* @param password: string or bytes to use to decrypt credentials from Keystore
|
268
|
-
*/
|
269
|
-
public async useCredentials(id: string, password: Password): Promise<void> {
|
270
|
-
this._credentials = await this.keystore?.readCredential(id, password);
|
61
|
+
private constructor(public zerokit: Zerokit) {
|
62
|
+
super(zerokit);
|
271
63
|
}
|
272
64
|
|
273
65
|
public async createEncoder(
|
@@ -275,7 +67,7 @@ export class RLNInstance {
|
|
275
67
|
): Promise<RLNEncoder> {
|
276
68
|
const { credentials: decryptedCredentials } =
|
277
69
|
await RLNInstance.decryptCredentialsIfNeeded(options.credentials);
|
278
|
-
const credentials = decryptedCredentials || this.
|
70
|
+
const credentials = decryptedCredentials || this.credentials;
|
279
71
|
|
280
72
|
if (!credentials) {
|
281
73
|
throw Error(
|
@@ -293,33 +85,6 @@ export class RLNInstance {
|
|
293
85
|
});
|
294
86
|
}
|
295
87
|
|
296
|
-
private async verifyCredentialsAgainstContract(
|
297
|
-
credentials: KeystoreEntity
|
298
|
-
): Promise<void> {
|
299
|
-
if (!this._contract) {
|
300
|
-
throw Error(
|
301
|
-
"Failed to verify chain coordinates: no contract initialized."
|
302
|
-
);
|
303
|
-
}
|
304
|
-
|
305
|
-
const registryAddress = credentials.membership.address;
|
306
|
-
const currentRegistryAddress = this._contract.address;
|
307
|
-
if (registryAddress !== currentRegistryAddress) {
|
308
|
-
throw Error(
|
309
|
-
`Failed to verify chain coordinates: credentials contract address=${registryAddress} is not equal to registryContract address=${currentRegistryAddress}`
|
310
|
-
);
|
311
|
-
}
|
312
|
-
|
313
|
-
const chainId = credentials.membership.chainId;
|
314
|
-
const network = await this._contract.provider.getNetwork();
|
315
|
-
const currentChainId = network.chainId;
|
316
|
-
if (chainId !== currentChainId) {
|
317
|
-
throw Error(
|
318
|
-
`Failed to verify chain coordinates: credentials chainID=${chainId} is not equal to registryContract chainID=${currentChainId}`
|
319
|
-
);
|
320
|
-
}
|
321
|
-
}
|
322
|
-
|
323
88
|
public createDecoder(
|
324
89
|
contentTopic: ContentTopic
|
325
90
|
): RLNDecoder<IDecodedMessage> {
|
@@ -328,4 +93,47 @@ export class RLNInstance {
|
|
328
93
|
decoder: createDecoder(contentTopic)
|
329
94
|
});
|
330
95
|
}
|
96
|
+
|
97
|
+
public static async loadWitnessCalculator(): Promise<WitnessCalculator> {
|
98
|
+
try {
|
99
|
+
const url = new URL("./resources/rln.wasm", import.meta.url);
|
100
|
+
const response = await fetch(url);
|
101
|
+
|
102
|
+
if (!response.ok) {
|
103
|
+
throw new Error(
|
104
|
+
`Failed to fetch witness calculator: ${response.status} ${response.statusText}`
|
105
|
+
);
|
106
|
+
}
|
107
|
+
|
108
|
+
return await wc.builder(
|
109
|
+
new Uint8Array(await response.arrayBuffer()),
|
110
|
+
false
|
111
|
+
);
|
112
|
+
} catch (error) {
|
113
|
+
log.error("Error loading witness calculator:", error);
|
114
|
+
throw new Error(
|
115
|
+
`Failed to load witness calculator: ${error instanceof Error ? error.message : String(error)}`
|
116
|
+
);
|
117
|
+
}
|
118
|
+
}
|
119
|
+
|
120
|
+
public static async loadZkey(): Promise<Uint8Array> {
|
121
|
+
try {
|
122
|
+
const url = new URL("./resources/rln_final.zkey", import.meta.url);
|
123
|
+
const response = await fetch(url);
|
124
|
+
|
125
|
+
if (!response.ok) {
|
126
|
+
throw new Error(
|
127
|
+
`Failed to fetch zkey: ${response.status} ${response.statusText}`
|
128
|
+
);
|
129
|
+
}
|
130
|
+
|
131
|
+
return new Uint8Array(await response.arrayBuffer());
|
132
|
+
} catch (error) {
|
133
|
+
log.error("Error loading zkey:", error);
|
134
|
+
throw new Error(
|
135
|
+
`Failed to load zkey: ${error instanceof Error ? error.message : String(error)}`
|
136
|
+
);
|
137
|
+
}
|
138
|
+
}
|
331
139
|
}
|
package/src/types.ts
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
import { ethers } from "ethers";
|
2
|
+
|
3
|
+
import { IdentityCredential } from "./identity.js";
|
4
|
+
import {
|
5
|
+
DecryptedCredentials,
|
6
|
+
EncryptedCredentials
|
7
|
+
} from "./keystore/types.js";
|
8
|
+
|
9
|
+
export type StartRLNOptions = {
|
10
|
+
/**
|
11
|
+
* If not set - will extract MetaMask account and get signer from it.
|
12
|
+
*/
|
13
|
+
signer?: ethers.Signer;
|
14
|
+
/**
|
15
|
+
* If not set - will use default SEPOLIA_CONTRACT address.
|
16
|
+
*/
|
17
|
+
address?: string;
|
18
|
+
/**
|
19
|
+
* Credentials to use for generating proofs and connecting to the contract and network.
|
20
|
+
* If provided used for validating the network chainId and connecting to registry contract.
|
21
|
+
*/
|
22
|
+
credentials?: EncryptedCredentials | DecryptedCredentials;
|
23
|
+
/**
|
24
|
+
* Rate limit for the member.
|
25
|
+
*/
|
26
|
+
rateLimit?: number;
|
27
|
+
};
|
28
|
+
|
29
|
+
export type RegisterMembershipOptions =
|
30
|
+
| { signature: string }
|
31
|
+
| { identity: IdentityCredential };
|